]> granicus.if.org Git - clang/commitdiff
Only warn in -Wliteral-conversion if the conversion loses information
authorMatt Beaumont-Gay <matthewbg@google.com>
Fri, 14 Oct 2011 15:36:25 +0000 (15:36 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Fri, 14 Oct 2011 15:36:25 +0000 (15:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@141955 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/knr-def-call.c
test/SemaCXX/warn-literal-conversion.cpp

index 7d7b922a3a56cf3466b787e30316c81c56f7fc71..0b3f0c6e84725cd8a3488d7fceb190fedfabfc15 100644 (file)
@@ -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<DiagGroup<"string-conversion">>, 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<DiagGroup<"conversion">>;
index e95feacbab5da0ab65d1b30ea0217748d374d111..74c69a3ce34f6db167bcfb8440dcef311cd6cd9b 100644 (file)
@@ -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) {
index 6243af61939896ec3a18f2fdded426a45aca98da..f41275d1e61840e641330e2ed70880ad32fc3b8b 100644 (file)
@@ -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}}
 }
index 3fc8a6fec7931a413c7cc96829c93accecf5c6fe..5fcae5dc80e67c39a9fd232014a8fc95e081a712 100644 (file)
@@ -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}}