From: Matt Beaumont-Gay Date: Fri, 14 Oct 2011 15:36:25 +0000 (+0000) Subject: Only warn in -Wliteral-conversion if the conversion loses information X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9ce6377714a8eb8f577c87028300421e72b00dc9;p=clang Only warn in -Wliteral-conversion if the conversion loses information git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141955 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 7d7b922a3a..0b3f0c6e84 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1560,8 +1560,6 @@ def warn_impcast_literal_float_to_integer : Warning< def warn_impcast_string_literal_to_bool : Warning< "implicit conversion turns string literal into bool: %0 to %1">, InGroup>, DefaultIgnore; -def note_fix_integral_float_as_integer : Note< - "this can be rewritten as an integer literal with the exact same value">; def warn_impcast_different_enum_types : Warning< "implicit conversion from enumeration type %0 to different enumeration type " "%1">, InGroup>; diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index e95feacbab..74c69a3ce3 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -3378,34 +3378,22 @@ void DiagnoseImpCast(Sema &S, Expr *E, QualType T, SourceLocation CContext, DiagnoseImpCast(S, E, E->getType(), T, CContext, diag); } -/// Diagnose an implicit cast from a literal expression. Also attemps to supply -/// fixit hints when the cast wouldn't lose information to simply write the -/// expression with the expected type. +/// Diagnose an implicit cast from a literal expression. Does not warn when the +/// cast wouldn't lose information. void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T, SourceLocation CContext) { - // Emit the primary warning first, then try to emit a fixit hint note if - // reasonable. - S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) - << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext); - - const llvm::APFloat &Value = FL->getValue(); - - // Don't attempt to fix PPC double double literals. - if (&Value.getSemantics() == &llvm::APFloat::PPCDoubleDouble) - return; - - // Try to convert this exactly to an integer. + // Try to convert the literal exactly to an integer. If we can, don't warn. bool isExact = false; + const llvm::APFloat &Value = FL->getValue(); llvm::APSInt IntegerValue(S.Context.getIntWidth(T), T->hasUnsignedIntegerRepresentation()); if (Value.convertToInteger(IntegerValue, llvm::APFloat::rmTowardZero, &isExact) - != llvm::APFloat::opOK || !isExact) + == llvm::APFloat::opOK && isExact) return; - std::string LiteralValue = IntegerValue.toString(10); - S.Diag(FL->getExprLoc(), diag::note_fix_integral_float_as_integer) - << FixItHint::CreateReplacement(FL->getSourceRange(), LiteralValue); + S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer) + << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext); } std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) { diff --git a/test/Sema/knr-def-call.c b/test/Sema/knr-def-call.c index 6243af6193..f41275d1e6 100644 --- a/test/Sema/knr-def-call.c +++ b/test/Sema/knr-def-call.c @@ -36,8 +36,6 @@ void proto(x) } void use_proto() { - proto(42.0); // expected-warning{{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} - (&proto)(42.0); // expected-warning{{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} + proto(42.1); // expected-warning{{implicit conversion turns literal floating-point number into integer}} + (&proto)(42.1); // expected-warning{{implicit conversion turns literal floating-point number into integer}} } diff --git a/test/SemaCXX/warn-literal-conversion.cpp b/test/SemaCXX/warn-literal-conversion.cpp index 3fc8a6fec7..5fcae5dc80 100644 --- a/test/SemaCXX/warn-literal-conversion.cpp +++ b/test/SemaCXX/warn-literal-conversion.cpp @@ -8,18 +8,13 @@ void test0() { int y0 = 1.2222F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} int y1 = (1.2222F); // expected-warning {{implicit conversion turns literal floating-point number into integer}} int y2 = (((1.2222F))); // expected-warning {{implicit conversion turns literal floating-point number into integer}} - int y3 = 12E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} - int y4 = 1.2E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} + int y3 = 12E-1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} + int y4 = 1.23E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}} // Double int y5 = 1.2222; // expected-warning {{implicit conversion turns literal floating-point number into integer}} - int y6 = 12E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} - int y7 = 1.2E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} - int y8 = (1.2E1); // expected-warning {{implicit conversion turns literal floating-point number into integer}} \ - // expected-note {{this can be rewritten as an integer literal with the exact same value}} + int y6 = 12E-1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} + int y7 = 1.23E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}} + int y8 = (1.23E1); // expected-warning {{implicit conversion turns literal floating-point number into integer}} // Test assignment to an existing variable. y8 = 2.22F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}