]> granicus.if.org Git - clang/commitdiff
When performing an implicit from float to bool, the floating point value must be...
authorAaron Ballman <aaron@aaronballman.com>
Wed, 30 Dec 2015 14:26:07 +0000 (14:26 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Wed, 30 Dec 2015 14:26:07 +0000 (14:26 +0000)
This patch address PR25876.

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

lib/Sema/SemaChecking.cpp
test/SemaCXX/warn-literal-conversion.cpp

index 59d51f7e84caa1d307da65796c16b98757130214..cbdcb5e48391d3314dadad405dbbcfedbfd97d9b 100644 (file)
@@ -6983,7 +6983,7 @@ void DiagnoseFloatingLiteralImpCast(Sema &S, FloatingLiteral *FL, QualType T,
 
   SmallString<16> PrettyTargetValue;
   if (T->isSpecificBuiltinType(BuiltinType::Bool))
-    PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
+    PrettyTargetValue = Value.isZero() ? "false" : "true";
   else
     IntegerValue.toString(PrettyTargetValue);
 
index d7bec4c73e5baebd28d13a6c5c69fbbbd417edc8..5d4b6f7f5bf8581b3d5646ddd661cc6349409a4f 100644 (file)
@@ -38,3 +38,14 @@ void test0() {
   int y = (24*60*60) * 0.25;
   int pennies = 123.45 * 100;
 }
+
+// Similarly, test floating point conversion to bool. Only float values of zero
+// are converted to false; everything else is converted to true.
+void test1() {
+  bool b1 = 0.99f; // expected-warning {{implicit conversion from 'float' to 'bool' changes value from 0.99 to true}}
+  bool b2 = 0.99; // expected-warning {{implicit conversion from 'double' to 'bool' changes value from 0.99 to true}}
+  // These do not warn because they can be directly converted to integral
+  // values.
+  bool b3 = 0.0f;
+  bool b4 = 0.0;
+}