[PATCH] D52835: [Diagnostics] Check integer to floating point number implicit conversions

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 3 11:16:49 PDT 2018


xbolva00 updated this revision to Diff 168141.

https://reviews.llvm.org/D52835

Files:
  lib/Sema/SemaChecking.cpp
  test/Sema/ext_vector_casts.c
  test/Sema/impcast-integer-float.c


Index: test/Sema/impcast-integer-float.c
===================================================================
--- test/Sema/impcast-integer-float.c
+++ test/Sema/impcast-integer-float.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 %s -verify -Wconversion -fsyntax-only
+
+#define shift_plus_one(x) ((1ULL << x) + 1)
+
+void test(void) {
+    float a1 = (1ULL << 31) + 1; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'float'}}
+    float a2 = 1ULL << 31;
+    float a3 = shift_plus_one(31); // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'float'}}
+    float a4 = (1ULL << 31) - 1; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'float'}}
+
+    double b1 = (1ULL << 63) + 1; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'double'}}
+    double b2 = 1ULL << 63;
+    double b3 = shift_plus_one(63); // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'double'}}
+    double b4 = (1ULL << 63) - 1; // expected-warning {{implicit conversion loses integer precision: 'unsigned long long' to 'double'}}    
+}
Index: test/Sema/ext_vector_casts.c
===================================================================
--- test/Sema/ext_vector_casts.c
+++ test/Sema/ext_vector_casts.c
@@ -118,7 +118,7 @@
   vf = l + vf;
   vf = 2.0 + vf;
   vf = d + vf; // expected-warning {{implicit conversion loses floating-point precision}}
-  vf = vf + 0xffffffff;
+  vf = vf + 0xffffffff; // expected-warning {{implicit conversion loses integer precision: 'unsigned int' to 'float2' (vector of 2 'float' values)}}
   vf = vf + 2.1; // expected-warning {{implicit conversion loses floating-point precision}}
   
   vd = l + vd;
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -10852,6 +10852,31 @@
     return;
   }
 
+  if (Source->isIntegerType() && Target->isFloatingType()) {
+    const llvm::fltSemantics *FloatSem = nullptr;
+    if (Target->isSpecificBuiltinType(BuiltinType::Float)) {
+      FloatSem = &llvm::APFloat::IEEEsingle();
+    } else if (Target->isSpecificBuiltinType(BuiltinType::Double)) {
+      FloatSem = &llvm::APFloat::IEEEdouble();
+    }
+
+    if (FloatSem) {
+      llvm::APFloat FloatValue(*FloatSem);
+      llvm::APSInt IntValue;
+      if (E->EvaluateAsInt(IntValue, S.Context, Expr::SE_AllowSideEffects)) {
+        if (S.SourceMgr.isInSystemMacro(CC))
+          return;
+
+        if (FloatValue.convertFromAPInt(IntValue, Source->isSignedIntegerType(),
+                                        llvm::APFloat::rmTowardZero) !=
+            llvm::APFloat::opOK) {
+          return DiagnoseImpCast(S, E, T, CC,
+                                 diag::warn_impcast_integer_precision);
+        }
+      }
+    }
+  }
+
   DiagnoseNullConversion(S, E, T, CC);
 
   S.DiscardMisalignedMemberAddress(Target, E);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52835.168141.patch
Type: text/x-patch
Size: 3049 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181003/92712dbd/attachment-0001.bin>


More information about the cfe-commits mailing list