]> granicus.if.org Git - clang/commitdiff
Improve some of the conversion warnings to fire on conversion to bool.
authorDavid Blaikie <dblaikie@gmail.com>
Tue, 15 May 2012 16:56:36 +0000 (16:56 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Tue, 15 May 2012 16:56:36 +0000 (16:56 +0000)
Moves the bool bail-out down a little in SemaChecking - so now
-Wnull-conversion and -Wliteral-conversion can fire when the target type is
bool.

Also improve the wording/details in the -Wliteral-conversion warning to match
the -Wconstant-conversion.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156826 91177308-0d34-0410-b5e6-96231b3b80d8

14 files changed:
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Analysis/array-struct-region.c
test/CXX/expr/expr.unary/expr.unary.op/p6.cpp
test/CXX/temp/temp.spec/p5.cpp
test/PCH/exprs.h
test/Sema/array-init.c
test/Sema/knr-def-call.c
test/SemaCXX/bool.cpp
test/SemaCXX/conversion.cpp
test/SemaCXX/expressions.cpp
test/SemaCXX/overload-call.cpp
test/SemaCXX/warn-literal-conversion.cpp
test/SemaTemplate/member-template-access-expr.cpp

index 2a34b53e4d46ccca94c95fbbf76dff418515aba2..cd2bd278121816957a5d126ec63c655ff858f885 100644 (file)
@@ -1781,8 +1781,7 @@ def warn_impcast_bitfield_precision_constant : Warning<
   "implicit truncation from %2 to bitfield changes value from %0 to %1">,
   InGroup<ConstantConversion>;
 def warn_impcast_literal_float_to_integer : Warning<
-  "implicit conversion turns literal floating-point number into integer: "
-  "%0 to %1">,
+  "implicit conversion from %0 to %1 changes value from %2 to %3">,
   InGroup<LiteralConversion>;
 def warn_impcast_string_literal_to_bool : Warning<
   "implicit conversion turns string literal into bool: %0 to %1">,
index eddb612fc6eab98cd89afdf0875e3e34ce6eb51c..9c637b89dd8f7c71085e54db74c550c57af30e13 100644 (file)
@@ -22,6 +22,7 @@
 #include "clang/AST/CharUnits.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclObjC.h"
+#include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/EvaluatedExprVisitor.h"
@@ -4081,8 +4082,17 @@ void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
       == llvm::APFloat::opOK && isExact)
     return;
 
+  SmallString<16> PrettySourceValue;
+  Value.toString(PrettySourceValue);
+  std::string PrettyTargetValue;
+  if (T->isSpecificBuiltinType(BuiltinType::Bool))
+    PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
+  else
+    PrettyTargetValue = IntegerValue.toString(10);
+
   S.Diag(FL->getExprLoc(), diag::warn_impcast_literal_float_to_integer)
-    << FL->getType() << T << FL->getSourceRange() << SourceRange(CContext);
+    << FL->getType() << T.getUnqualifiedType() << PrettySourceValue
+    << PrettyTargetValue << FL->getSourceRange() << SourceRange(CContext);
 }
 
 std::string PrettyPrintInRange(const llvm::APSInt &Value, IntRange Range) {
@@ -4149,7 +4159,6 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
         }
       }
     }
-    return; // Other casts to bool are not checked.
   }
 
   // Strip vector types.
@@ -4213,7 +4222,7 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
     }
 
     // If the target is integral, always warn.    
-    if ((TargetBT && TargetBT->isInteger())) {
+    if (TargetBT && TargetBT->isInteger()) {
       if (S.SourceMgr.isInSystemMacro(CC))
         return;
       
@@ -4247,6 +4256,11 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
     return;
   }
 
+  // TODO: remove this early return once the false positives for constant->bool
+  // in templates, macros, etc, are reduced or removed.
+  if (Target->isSpecificBuiltinType(BuiltinType::Bool))
+    return;
+
   IntRange SourceRange = GetExprRange(S.Context, E);
   IntRange TargetRange = IntRange::forTargetOfCanonicalType(S.Context, Target);
 
index 8be86883a00c8aa850ef042b2d6e09b7f7f93668..18d5b2a3752f2cd6264fc1a2ab804bc38055df34 100644 (file)
@@ -25,8 +25,8 @@ int string_literal_init() {
 }
 
 void nested_compound_literals(int rad) {
-  int vec[6][2] = {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169},  // expected-warning 6 {{implicit conversion turns literal floating-point number into integer}}
-                   {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}}; // expected-warning 6 {{implicit conversion turns literal floating-point number into integer}}
+  int vec[6][2] = {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169},  // expected-warning 6 {{implicit conversion from 'double' to 'int' changes value from}}
+                   {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}}; // expected-warning 6 {{implicit conversion from 'double' to 'int' changes value from}}
   int a;
 
   for (a = 0; a < 6; ++a) {
index ac11940c80daa5941f7c6c8e95ffe2138ce4dc07..2646264273ebe2414c78140fb1f6c1f1b405a508 100644 (file)
@@ -4,7 +4,7 @@
 
 bool b = !0;
 
-bool b2 = !1.2;
+bool b2 = !1.2; //expected-warning{{implicit conversion from 'double' to 'bool' changes value from 1.2 to true}}
 
 bool b3 = !4;
 
index 0e69a26b04e2674e15fe6774b3319155d085d024..ba92d41e3e8e328da7b0d7ffa6be330a663529c5 100644 (file)
@@ -14,7 +14,7 @@ struct X0 {
 };
 
 template<typename T>
-T X0<T>::value = 3.14; // expected-warning{{implicit conversion turns literal floating-point number into integer}}
+T X0<T>::value = 3.14; // expected-warning{{implicit conversion from 'double' to 'int' changes value from 3.14 to 3}}
 
 template struct X0<int>; // expected-note{{previous explicit instantiation}} \
                             expected-note{{requested here}}
index 09a50135e40e9425b8ace6fb06bc24a934aac4b1..d08b1f64edec82b02315e0d0771f2c5a457380c0 100644 (file)
@@ -87,7 +87,7 @@ struct {
   int x;
   float y;
 } designated_inits[3] = { [0].y = 17,
-                          [2].x = 12.3, // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+                          [2].x = 12.3, // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
                           3.5 };
 
 // TypesCompatibleExpr
index 26c0b2418221d3372a01d48d4cc0c5755213ad92..cfdf8e2bd7ca6fff266f796678f332609bc7924e 100644 (file)
@@ -50,7 +50,7 @@ void func() {
   
   static long x2[3] = { 1.0,
                         "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}}
-                         5.8 }; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+                         5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}}
 }
 
 void test() {
index f41275d1e61840e641330e2ed70880ad32fc3b8b..80ad0d820b1a11db76faef9df79a64e5b83548e9 100644 (file)
@@ -36,6 +36,6 @@ void proto(x)
 }
 
 void use_proto() {
-  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}}
+  proto(42.1); // expected-warning{{implicit conversion from 'double' to 'int' changes value from 42.1 to 42}}
+  (&proto)(42.1); // expected-warning{{implicit conversion from 'double' to 'int' changes value from 42.1 to 42}}
 }
index 2b3ab68848ebf072f78f196725030dcd91a714b2..f027186735b3e1248457a981a8bb5de247b1249c 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s 
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s
 
 // Bool literals can be enum values.
 enum {
index b019536e1a3ee666a38279a97fb2aa0273472e01..4b44bede8dbcb6c18d1ab67d40ad3e24b436b21f 100644 (file)
@@ -65,7 +65,7 @@ void test3() {
   int c = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
   int d;
   d = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
-  bool bl = NULL; // FIXME: this should warn but we currently suppress a bunch of conversion-to-bool warnings including this one
+  bool bl = NULL; // expected-warning {{implicit conversion of NULL constant to 'bool'}}
   char ch = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
   unsigned char uch = NULL; // expected-warning {{implicit conversion of NULL constant to 'unsigned char'}}
   short sh = NULL; // expected-warning {{implicit conversion of NULL constant to 'short'}}
@@ -104,3 +104,12 @@ namespace test4 {
     tmpl2<int*>();
   }
 }
+
+namespace test5 {
+  template<int I>
+  void func() {
+    bool b = I;
+  }
+
+  template void func<3>();
+}
index 355833e693fa2c4eee800b553e3fd7ba5f33dbe5..2635fb8d176a87199bc630720216cf3df58c23a8 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s 
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s
 
 void choice(int);
 int choice(bool);
index 09eb71d36d79f156636cf7d01ae30e8f16ae2cc9..2b5ebb962b636eae31137244dc6cb209220f567d 100644 (file)
@@ -233,7 +233,7 @@ float* intref(const int&);
 
 void intref_test() {
   float* ir1 = intref(5);
-  float* ir2 = intref(5.5); // expected-warning{{implicit conversion turns literal floating-point number into integer}}
+  float* ir2 = intref(5.5); // expected-warning{{implicit conversion from 'double' to 'int' changes value from 5.5 to 5}}
 }
 
 void derived5(C&); // expected-note{{candidate function not viable: cannot bind base class object of type 'A' to derived class reference 'C &' for 1st argument}}
index 5fcae5dc80e67c39a9fd232014a8fc95e081a712..d7bec4c73e5baebd28d13a6c5c69fbbbd417edc8 100644 (file)
@@ -5,29 +5,29 @@ void foo(int y);
 // Warn when a literal float or double is assigned or bound to an integer.
 void test0() {
   // Float
-  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 = 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}}
+  int y0 = 1.2222F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
+  int y1 = (1.2222F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
+  int y2 = (((1.2222F))); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2222 to 1}}
+  int y3 = 12E-1F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
+  int y4 = 1.23E1F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 12.3 to 12}}
   // Double
-  int y5 = 1.2222; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
-  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}}
+  int y5 = 1.2222; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 1.2222 to 1}}
+  int y6 = 12E-1; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 1.2 to 1}}
+  int y7 = 1.23E1; // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
+  int y8 = (1.23E1); // expected-warning {{implicit conversion from 'double' to 'int' changes value from 12.3 to 12}}
 
   // Test assignment to an existing variable.
-  y8 = 2.22F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+  y8 = 2.22F; // expected-warning {{implicit conversion from 'float' to 'int' changes value from 2.22 to 2}}
 
   // Test direct initialization.
-  int y9(1.23F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+  int y9(1.23F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.23 to 1}}
 
   // Test passing a literal floating-point value to a function that takes an integer.
-  foo(1.2F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+  foo(1.2F); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
 
-  int y10 = -1.2F;  // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+  int y10 = -1.2F;  // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.2 to 1}}
 
-  // -Wconversion-literal does NOT catch const values.
+  // -Wliteral-conversion does NOT catch const values.
   // (-Wconversion DOES catch them.)
   static const float sales_tax_rate = .095F;
   int z = sales_tax_rate;
index c95b57d4b4fddedd66e17d9f63c75d09125d4524..f105ba8e84b579084ff2e43306145a167711afc7 100644 (file)
@@ -60,7 +60,7 @@ struct X1 {
 
 void test_X1(X1 x1) {
   float *fp1 = x1.f1<>(17);
-  float *fp2 = x1.f1<int>(3.14); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+  float *fp2 = x1.f1<int>(3.14); // expected-warning {{implicit conversion from 'double' to 'int' changes value from 3.14 to 3}}
   int *ip1 = x1.f1(17);
   float *ip2 = x1.f1(3.14);