r331812 - Fix float->int conversion warnings when near barriers.

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue May 8 14:26:21 PDT 2018


Author: erichkeane
Date: Tue May  8 14:26:21 2018
New Revision: 331812

URL: http://llvm.org/viewvc/llvm-project?rev=331812&view=rev
Log:
Fix float->int conversion warnings when near barriers.

As Eli brought up here: https://reviews.llvm.org/D46535
I'd previously messed up this fix by missing conversions
that are just slightly outside the range.  This patch fixes
this by no longer ignoring the return value of 
convertToInteger.  Additionally, one of the error messages
wasn't very sensical (mentioning out of range value, when it 
really was not), so it was cleaned up as well.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/warn-float-conversion.cpp
    cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=331812&r1=331811&r2=331812&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue May  8 14:26:21 2018
@@ -3148,8 +3148,7 @@ def warn_impcast_float_integer : Warning
   InGroup<FloatConversion>, DefaultIgnore;
 
 def warn_impcast_float_to_integer : Warning<
-  "implicit conversion of out of range value from %0 to %1 changes value "
-  "from %2 to %3">,
+  "implicit conversion from %0 to %1 changes value from %2 to %3">,
   InGroup<FloatOverflowConversion>, DefaultIgnore;
 def warn_impcast_float_to_integer_out_of_range : Warning<
   "implicit conversion of out of range value from %0 to %1 is undefined">,

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=331812&r1=331811&r2=331812&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue May  8 14:26:21 2018
@@ -9419,26 +9419,25 @@ static void DiagnoseFloatingImpCast(Sema
 
   llvm::APSInt IntegerValue(S.Context.getIntWidth(T),
                             T->hasUnsignedIntegerRepresentation());
-  if (Value.convertToInteger(IntegerValue, llvm::APFloat::rmTowardZero,
-                             &isExact) == llvm::APFloat::opOK &&
-      isExact) {
+  llvm::APFloat::opStatus Result = Value.convertToInteger(
+      IntegerValue, llvm::APFloat::rmTowardZero, &isExact);
+
+  if (Result == llvm::APFloat::opOK && isExact) {
     if (IsLiteral) return;
     return DiagnoseImpCast(S, E, T, CContext, diag::warn_impcast_float_integer,
                            PruneWarnings);
   }
 
+  // Conversion of a floating-point value to a non-bool integer where the
+  // integral part cannot be represented by the integer type is undefined.
+  if (!IsBool && Result == llvm::APFloat::opInvalidOp)
+    return DiagnoseImpCast(
+        S, E, T, CContext,
+        IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range
+                  : diag::warn_impcast_float_to_integer_out_of_range);
+
   unsigned DiagID = 0;
   if (IsLiteral) {
-    // Conversion of a floating-point value to a non-bool integer where the
-    // integral part cannot be represented by the integer type is undefined.
-    if (!IsBool &&
-        ((IntegerValue.isSigned() && (IntegerValue.isMaxSignedValue() ||
-                                      IntegerValue.isMinSignedValue())) ||
-         (IntegerValue.isUnsigned() &&
-          (IntegerValue.isMaxValue() || IntegerValue.isMinValue()))))
-      return DiagnoseImpCast(
-          S, E, T, CContext,
-          diag::warn_impcast_literal_float_to_integer_out_of_range);
     // Warn on floating point literal to integer.
     DiagID = diag::warn_impcast_literal_float_to_integer;
   } else if (IntegerValue == 0) {
@@ -9454,19 +9453,12 @@ static void DiagnoseFloatingImpCast(Sema
         return DiagnoseImpCast(S, E, T, CContext,
                                diag::warn_impcast_float_integer, PruneWarnings);
       }
-      if (!IsBool && (IntegerValue.isMaxValue() || IntegerValue.isMinValue()))
-        return DiagnoseImpCast(S, E, T, CContext,
-                               diag::warn_impcast_float_to_integer_out_of_range,
-                               PruneWarnings);
     } else {  // IntegerValue.isSigned()
       if (!IntegerValue.isMaxSignedValue() &&
           !IntegerValue.isMinSignedValue()) {
         return DiagnoseImpCast(S, E, T, CContext,
                                diag::warn_impcast_float_integer, PruneWarnings);
       }
-      return DiagnoseImpCast(S, E, T, CContext,
-                             diag::warn_impcast_float_to_integer_out_of_range,
-                             PruneWarnings);
     }
     // Warn on evaluatable floating point expression to integer conversion.
     DiagID = diag::warn_impcast_float_to_integer;

Modified: cfe/trunk/test/SemaCXX/warn-float-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-float-conversion.cpp?rev=331812&r1=331811&r2=331812&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-float-conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-float-conversion.cpp Tue May  8 14:26:21 2018
@@ -63,6 +63,9 @@ void TestConstantFloat() {
 
   int b3 = five / 1.0;  //expected-warning{{conversion}}
   int b4 = five / 2.0;  //expected-warning{{conversion}}
+
+  int f = 2147483646.5 + 1; // expected-warning{{implicit conversion from 'double' to 'int' changes value from 2147483647.5 to 2147483647}}
+  unsigned g = -.5 + .01; // expected-warning{{implicit conversion from 'double' to 'unsigned int' changes non-zero value from -0.49 to 0}}
 }
 #endif  // FLOAT_CONVERSION
 

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=331812&r1=331811&r2=331812&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp Tue May  8 14:26:21 2018
@@ -56,3 +56,7 @@ void test1() {
   short s2 = -32769.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'short' is undefined}}
   unsigned short us2 = -65537.0; // expected-warning{{implicit conversion of out of range value from 'double' to 'unsigned short' is undefined}}
 }
+
+int a() { return 2147483647.5; } // expected-warning{{implicit conversion from 'double' to 'int' changes value from 2147483647.5 to 2147483647}}
+unsigned b() { return -.5; } // expected-warning{{implicit conversion from 'double' to 'unsigned int' changes value from -0.5 to 0}}
+




More information about the cfe-commits mailing list