r256643 - When performing an implicit from float to bool, the floating point value must be *exactly* zero in order for the conversion to result in 0. This does not involve a conversion through an integer value, and so truncation of the value is not performed.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 30 06:26:07 PST 2015


Author: aaronballman
Date: Wed Dec 30 08:26:07 2015
New Revision: 256643

URL: http://llvm.org/viewvc/llvm-project?rev=256643&view=rev
Log:
When performing an implicit from float to bool, the floating point value must be *exactly* zero in order for the conversion to result in 0. This does not involve a conversion through an integer value, and so truncation of the value is not performed.

This patch address PR25876.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=256643&r1=256642&r2=256643&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Dec 30 08:26:07 2015
@@ -6983,7 +6983,7 @@ void DiagnoseFloatingLiteralImpCast(Sema
 
   SmallString<16> PrettyTargetValue;
   if (T->isSpecificBuiltinType(BuiltinType::Bool))
-    PrettyTargetValue = IntegerValue == 0 ? "false" : "true";
+    PrettyTargetValue = Value.isZero() ? "false" : "true";
   else
     IntegerValue.toString(PrettyTargetValue);
 

Modified: cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp?rev=256643&r1=256642&r2=256643&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp Wed Dec 30 08:26:07 2015
@@ -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;
+}




More information about the cfe-commits mailing list