[PATCH] D42728: Add more warnings for implict conversions (e.g. double truncation to float).

Andrew V. Tischenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 07:40:48 PST 2018


avt77 updated this revision to Diff 134035.
avt77 added a comment.

I removed the ambiguity with changes in CheckImplicitConversion (required by rksimon): now it's absolutely clear what was changed.
And I minimized changes in diagnostics(required by rjmccall): the only float-double truncation was changed. As result only two tests were changed. Later we could easily extend the diagnostic updates if it will be necessary (but inside another patch).


https://reviews.llvm.org/D42728

Files:
  lib/Sema/SemaChecking.cpp
  test/Sema/constant-conversion.c
  test/Sema/conversion.c


Index: test/Sema/conversion.c
===================================================================
--- test/Sema/conversion.c
+++ test/Sema/conversion.c
@@ -436,7 +436,7 @@
 }
 
 void double2float_test2(double a, float *b) {
-    *b += a;
+    *b += a; // expected-warning {{implicit conversion loses floating-point precision: 'double' to 'float'}}
 }
 
 float sinf (float x);
Index: test/Sema/constant-conversion.c
===================================================================
--- test/Sema/constant-conversion.c
+++ test/Sema/constant-conversion.c
@@ -73,7 +73,7 @@
 	f.twoBits1 = ~1; // no-warning
 	f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to 1}}
 	f.twoBits1 &= ~1; // no-warning
-	f.twoBits2 &= ~2; // no-warning
+	f.twoBits2 &= ~2; // expected-warning {{implicit truncation from 'int' to bit-field changes value from -3 to 1}}
 }
 
 void test8() {
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -8625,6 +8625,9 @@
 }
 
 static void AnalyzeImplicitConversions(Sema &S, Expr *E, SourceLocation CC);
+static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
+                                    SourceLocation CC,
+                                    bool *ICContext = nullptr);
 
 static bool IsEnumConstOrFromMacro(Sema &S, Expr *E) {
   // Suppress cases where we are comparing against an enum constant.
@@ -9164,6 +9167,13 @@
   }
 
   AnalyzeImplicitConversions(S, E->getRHS(), E->getOperatorLoc());
+
+  QualType RQType = E->getRHS()->getType();
+  QualType LQType = E->getLHS()->getType();
+  if ((RQType != LQType) && RQType->isRealFloatingType() &&
+      LQType->isRealFloatingType())
+    CheckImplicitConversion(S, E->getRHS()->IgnoreParenImpCasts(),
+                            E->getLHS()->getType(), E->getOperatorLoc());
 }
 
 /// Diagnose an implicit cast;  purely a helper for CheckImplicitConversion.
@@ -9501,9 +9511,8 @@
   return true;
 }
 
-static void
-CheckImplicitConversion(Sema &S, Expr *E, QualType T, SourceLocation CC,
-                        bool *ICContext = nullptr) {
+static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
+                                    SourceLocation CC, bool *ICContext) {
   if (E->isTypeDependent() || E->isValueDependent()) return;
 
   const Type *Source = S.Context.getCanonicalType(E->getType()).getTypePtr();
@@ -9873,8 +9882,8 @@
     if (BO->isComparisonOp())
       return AnalyzeComparison(S, BO);
 
-    // And with simple assignments.
-    if (BO->getOpcode() == BO_Assign)
+    // And with assignments.
+    if (BO->isAssignmentOp())
       return AnalyzeAssignment(S, BO);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42728.134035.patch
Type: text/x-patch
Size: 2768 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180213/dd5f4b43/attachment.bin>


More information about the llvm-commits mailing list