]> granicus.if.org Git - clang/commitdiff
[Sema] Implement -Wdouble-promotion for clang.
authorGeorge Burgess IV <george.burgess.iv@gmail.com>
Thu, 29 Oct 2015 00:28:52 +0000 (00:28 +0000)
committerGeorge Burgess IV <george.burgess.iv@gmail.com>
Thu, 29 Oct 2015 00:28:52 +0000 (00:28 +0000)
GCC has a warning called -Wdouble-promotion, which warns you when
an implicit conversion increases the width of a floating point type.

This is useful when writing code for architectures that can perform
hardware FP ops on floats, but must fall back to software emulation for
larger types (i.e. double, long double).

This fixes PR15109 <https://llvm.org/bugs/show_bug.cgi?id=15109>.

Thanks to Carl Norum for the patch!

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

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/warn-double-promotion.c [new file with mode: 0644]

index 1f77181c6b615b9605e85a5a261f94bd72a5b66f..042244125b8d70e21c2ceb7c9516555de0641358 100644 (file)
@@ -46,6 +46,7 @@ def BoolConversion : DiagGroup<"bool-conversion", [PointerBoolConversion,
 def IntConversion : DiagGroup<"int-conversion">;
 def EnumConversion : DiagGroup<"enum-conversion">;
 def FloatConversion : DiagGroup<"float-conversion">;
+def DoublePromotion : DiagGroup<"double-promotion">;
 def EnumTooLarge : DiagGroup<"enum-too-large">;
 def UnsupportedNan : DiagGroup<"unsupported-nan">;
 def NonLiteralNullConversion : DiagGroup<"non-literal-null-conversion">;
index b902efb1ca9202d8ee84118ca68d4b773afd3b58..d2945a0bd41fc6e1c149cf7e4f0d1755ebde8ff0 100644 (file)
@@ -2590,6 +2590,9 @@ def warn_impcast_complex_scalar : Warning<
 def warn_impcast_float_precision : Warning<
   "implicit conversion loses floating-point precision: %0 to %1">,
   InGroup<Conversion>, DefaultIgnore;
+def warn_impcast_double_promotion : Warning<
+  "implicit conversion increases floating-point precision: %0 to %1">,
+  InGroup<DoublePromotion>, DefaultIgnore;
 def warn_impcast_float_integer : Warning<
   "implicit conversion turns floating-point number into integer: %0 to %1">,
   InGroup<FloatConversion>, DefaultIgnore;
index a8d882a635eee29bea139c36fa0ebaafb5701e1e..8d9a1b23bb47d757e4027063bc9b4cfb63c573f4 100644 (file)
@@ -7217,6 +7217,14 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
           return;
 
         DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_precision);
+
+      }
+      // ... or possibly if we're increasing rank, too
+      else if (TargetBT->getKind() > SourceBT->getKind()) {
+        if (S.SourceMgr.isInSystemMacro(CC))
+          return;
+
+        DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_double_promotion);
       }
       return;
     }
diff --git a/test/Sema/warn-double-promotion.c b/test/Sema/warn-double-promotion.c
new file mode 100644 (file)
index 0000000..b6fd0c5
--- /dev/null
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s -Wdouble-promotion
+
+float ReturnFloatFromDouble(double d) {
+  return d;
+}
+
+float ReturnFloatFromLongDouble(long double ld) {
+  return ld;
+}
+
+double ReturnDoubleFromLongDouble(long double ld) {
+  return ld;
+}
+
+double ReturnDoubleFromFloat(float f) {
+  return f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+}
+
+long double ReturnLongDoubleFromFloat(float f) {
+  return f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+}
+
+long double ReturnLongDoubleFromDouble(double d) {
+  return d;  //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+}
+
+void Convert(float f, double d, long double ld) {
+  d = f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+  ld = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+  ld = d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  f = d;
+  f = ld;
+  d = ld;
+}