]> granicus.if.org Git - clang/commitdiff
[Sema] Fix-up a -Wfloat-conversion diagnostic
authorErik Pilkington <erik.pilkington@gmail.com>
Thu, 14 Feb 2019 22:48:01 +0000 (22:48 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Thu, 14 Feb 2019 22:48:01 +0000 (22:48 +0000)
We were warning on valid ObjC property reference exprs, and passing
in the wrong arguments to DiagnoseFloatingImpCast (leading to a badly
worded diagnostic).

rdar://47644670

Differential revision: https://reviews.llvm.org/D58145

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

lib/Sema/SemaChecking.cpp
test/SemaCXX/warn-float-conversion.cpp
test/SemaObjC/conversion.m

index c8e72adb5e4ef5af77d8162e9c866adc8efb3699..71882b571c429deeca3ae0c5af53432f003f3109 100644 (file)
@@ -10624,16 +10624,16 @@ static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
   // The below checks assume source is floating point.
   if (!ResultBT || !RBT || !RBT->isFloatingPoint()) return;
 
-  // If source is floating point but target is not.
-  if (!ResultBT->isFloatingPoint())
-    return DiagnoseFloatingImpCast(S, E, E->getRHS()->getType(),
-                                   E->getExprLoc());
-
-  // If both source and target are floating points.
-  // Builtin FP kinds are ordered by increasing FP rank.
-  if (ResultBT->getKind() < RBT->getKind() &&
-      // We don't want to warn for system macro.
-      !S.SourceMgr.isInSystemMacro(E->getOperatorLoc()))
+  // If source is floating point but target is an integer.
+  if (ResultBT->isInteger())
+    DiagnoseImpCast(S, E, E->getRHS()->getType(), E->getLHS()->getType(),
+                    E->getExprLoc(), diag::warn_impcast_float_integer);
+  // If both source and target are floating points. Builtin FP kinds are ordered
+  // by increasing FP rank. FIXME: except _Float16, we currently emit a bogus
+  // warning.
+  else if (ResultBT->isFloatingPoint() && ResultBT->getKind() < RBT->getKind() &&
+           // We don't want to warn for system macro.
+           !S.SourceMgr.isInSystemMacro(E->getOperatorLoc()))
     // warn about dropping FP rank.
     DiagnoseImpCast(S, E->getRHS(), E->getLHS()->getType(), E->getOperatorLoc(),
                     diag::warn_impcast_float_result_precision);
index 7dec4844b062e0a8424eff4eb64119b9f4cb6306..fad1ff147e492d73d152af2d9497f3d72e2c521a 100644 (file)
@@ -44,17 +44,17 @@ void Convert(float f, double d, long double ld) {
 void CompoundAssignment() {
   int x = 3;
 
-  x += 1.234;  //expected-warning{{conversion}}
-  x -= -0.0;  //expected-warning{{conversion}}
-  x *= 1.1f;  //expected-warning{{conversion}}
-  x /= -2.2f;  //expected-warning{{conversion}}
+  x += 1.234; // expected-warning {{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
+  x -= -0.0;  // expected-warning {{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
+  x *= 1.1f;  // expected-warning {{implicit conversion turns floating-point number into integer: 'float' to 'int'}}
+  x /= -2.2f; // expected-warning {{implicit conversion turns floating-point number into integer: 'float' to 'int'}}
 
-  int y = x += 1.4f;  //expected-warning{{conversion}}
+  int y = x += 1.4f; // expected-warning {{implicit conversion turns floating-point number into integer: 'float' to 'int'}}
 
   float z = 1.1f;
   double w = -2.2;
 
-  y += z + w;  //expected-warning{{conversion}}
+  y += z + w; // expected-warning {{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
 }
 
 # 1 "foo.h" 3
index 88a1a44b2177be673e079dc714b578b732a42151..743f7440e2e4317bf0194043176502710e859652 100644 (file)
@@ -14,4 +14,11 @@ void radar14415662(RDar14415662 *f, char x, int y) {
   x = y; // expected-warning {{implicit conversion loses integer precision: 'int' to 'char'}}
 }
 
+__attribute__((objc_root_class)) @interface DoubleProp
+@property double d;
+@end
 
+void use_double_prop(DoubleProp *dp) {
+  double local = 42;
+  dp.d += local; // no warning
+}