[PATCH] D78661: [Sema] Split off warn_impcast_integer_float_precision_constant into -Wimplicit-const-int-float-conversion

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 22 12:30:53 PDT 2020


MaskRay created this revision.
MaskRay added reviewers: aaron.ballman, jfb, nickdesaulniers, rsmith, scanon, xbolva00, ziangwan.
Herald added subscribers: cfe-commits, dexonsmith.
Herald added a project: clang.

Currently, both `warn_impcast_integer_float_precision_constant` and
`warn_impcast_integer_float_precision` are covered by
-Wimplicit-int-float-conversion, but only the ..._constant warning is on
by default.

`warn_impcast_integer_float_precision_constant` likely flags real problems
while `warn_impcast_integer_float_precision` may flag legitimate use
cases (for example, `int` used with limited range support by `float`).

If -Wno-implicit-int-float-conversion is used, currently there is no way
to restore the ..._constant warning. This patch adds
-Wimplicit-const-int-float-conversion to address the issue. (Similar to
the reasoning in https://reviews.llvm.org/D64666#1598194)

Adapted from a patch by Brooks Moses.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78661

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/test/Sema/implicit-int-float-conversion.c


Index: clang/test/Sema/implicit-int-float-conversion.c
===================================================================
--- clang/test/Sema/implicit-int-float-conversion.c
+++ clang/test/Sema/implicit-int-float-conversion.c
@@ -1,8 +1,12 @@
-// RUN: %clang_cc1 %s -verify -Wno-conversion -Wimplicit-int-float-conversion
+// RUN: %clang_cc1 %s -verify
+// RUN: %clang_cc1 %s -verify -Wimplicit-const-int-float-conversion
+// RUN: %clang_cc1 %s -DNONCONST=1 -verify -Wimplicit-int-float-conversion
 
+#ifdef NONCOST
 long testReturn(long a, float b) {
   return a + b; // expected-warning {{implicit conversion from 'long' to 'float' may lose precision}}
 }
+#endif
 
 void testAssignment() {
   float f = 222222;
@@ -12,7 +16,9 @@
   float ffff = 222222222222UL; // expected-warning {{changes value from 222222222222 to 222222221312}}
 
   long l = 222222222222L;
+#ifdef NONCOST
   float fff = l; // expected-warning {{implicit conversion from 'long' to 'float' may lose precision}}
+#endif
 }
 
 void testExpression() {
@@ -24,7 +30,9 @@
   float c = 22222222 + 22222223; // expected-warning {{implicit conversion from 'int' to 'float' changes value from 44444445 to 44444444}}
 
   int i = 0;
+#ifdef NONCOST
   float d = i + a; // expected-warning {{implicit conversion from 'int' to 'float' may lose precision}}
+#endif
 
   double e = 0.0;
   double f = i + e;
@@ -36,5 +44,7 @@
   float a = {222222222222L}; // expected-warning {{changes value from 222222222222 to 222222221312}}
 
   long b = 222222222222L;
+#ifdef NONCOST
   float c = {b}; // expected-warning {{implicit conversion from 'long' to 'float' may lose precision}}
+#endif
 }
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3549,7 +3549,7 @@
   InGroup<ImplicitIntFloatConversion>, DefaultIgnore;
 def warn_impcast_integer_float_precision_constant : Warning<
   "implicit conversion from %2 to %3 changes value from %0 to %1">,
-  InGroup<ImplicitIntFloatConversion>;
+  InGroup<ImplicitConstIntFloatConversion>;
 
 def warn_impcast_float_to_integer : Warning<
   "implicit conversion from %0 to %1 changes value from %2 to %3">,
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ clang/include/clang/Basic/DiagnosticGroups.td
@@ -85,7 +85,9 @@
   DiagGroup<"objc-signed-char-bool-implicit-int-conversion">;
 def ImplicitIntConversion : DiagGroup<"implicit-int-conversion",
                                      [ObjCSignedCharBoolImplicitIntConversion]>;
-def ImplicitIntFloatConversion : DiagGroup<"implicit-int-float-conversion">;
+def ImplicitConstIntFloatConversion : DiagGroup<"implicit-const-int-float-conversion">;
+def ImplicitIntFloatConversion : DiagGroup<"implicit-int-float-conversion",
+ [ImplicitConstIntFloatConversion]>;
 def ObjCSignedCharBoolImplicitFloatConversion :
   DiagGroup<"objc-signed-char-bool-implicit-float-conversion">;
 def ImplicitFloatConversion : DiagGroup<"implicit-float-conversion",


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78661.259352.patch
Type: text/x-patch
Size: 3205 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200422/6c4c57c1/attachment-0001.bin>


More information about the cfe-commits mailing list