[cfe-commits] r125745 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp test/Sema/knr-def-call.c test/SemaCXX/warn-literal-conversion.cpp
Chandler Carruth
chandlerc at gmail.com
Thu Feb 17 03:05:49 PST 2011
Author: chandlerc
Date: Thu Feb 17 05:05:49 2011
New Revision: 125745
URL: http://llvm.org/viewvc/llvm-project?rev=125745&view=rev
Log:
Implement a sub-group of -Wconversion: -Wliteral-conversion. This
specifically targets literals which are implicitly converted, a those
are more often unintended and trivial to fix. This can be especially
helpful for diagnosing what makes 'const int x = 1e6' not an ICE.
Original patch authored by Jim Meehan with contributions from other
Googlers and a few cleanups from myself.
Added:
cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaChecking.cpp
cfe/trunk/test/Sema/knr-def-call.c
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=125745&r1=125744&r2=125745&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Thu Feb 17 05:05:49 2011
@@ -181,10 +181,12 @@
// legacy reasons:
// - some people want just 64-to-32 warnings
// - conversion warnings with constant sources are on by default
+// - conversion warnings for literals are on by default
// - bool-to-pointer conversion warnings are on by default
def Conversion : DiagGroup<"conversion",
[DiagGroup<"shorten-64-to-32">,
DiagGroup<"constant-conversion">,
+ DiagGroup<"literal-conversion">,
BoolConversions]>,
DiagCategory<"Value Conversion Issue">;
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=125745&r1=125744&r2=125745&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Feb 17 05:05:49 2011
@@ -1127,6 +1127,10 @@
def warn_impcast_bitfield_precision_constant : Warning<
"implicit truncation from %2 to bitfield changes value from %0 to %1">,
InGroup<DiagGroup<"constant-conversion">>;
+def warn_impcast_literal_float_to_integer : Warning<
+ "implicit conversion turns literal floating-point number into integer: "
+ "%0 to %1">,
+ InGroup<DiagGroup<"literal-conversion">>, DefaultIgnore;
def warn_cast_align : Warning<
"cast from %0 to %1 increases required alignment from %2 to %3">,
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=125745&r1=125744&r2=125745&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Feb 17 05:05:49 2011
@@ -2799,9 +2799,15 @@
}
// If the target is integral, always warn.
- if ((TargetBT && TargetBT->isInteger()))
- // TODO: don't warn for integer values?
- DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
+ if ((TargetBT && TargetBT->isInteger())) {
+ Expr *InnerE = E->IgnoreParenImpCasts();
+ if (FloatingLiteral *LiteralExpr = dyn_cast<FloatingLiteral>(InnerE)) {
+ DiagnoseImpCast(S, LiteralExpr, T, CC,
+ diag::warn_impcast_literal_float_to_integer);
+ } else {
+ DiagnoseImpCast(S, E, T, CC, diag::warn_impcast_float_integer);
+ }
+ }
return;
}
Modified: cfe/trunk/test/Sema/knr-def-call.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/knr-def-call.c?rev=125745&r1=125744&r2=125745&view=diff
==============================================================================
--- cfe/trunk/test/Sema/knr-def-call.c (original)
+++ cfe/trunk/test/Sema/knr-def-call.c Thu Feb 17 05:05:49 2011
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -Wconversion -fsyntax-only -verify %s
+// RUN: %clang_cc1 -Wconversion -Wliteral-conversion -fsyntax-only -verify %s
// C DR #316, PR 3626.
void f0(a, b, c, d) int a,b,c,d; {}
@@ -36,6 +36,6 @@
}
void use_proto() {
- proto(42.0); // expected-warning{{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
- (&proto)(42.0); // expected-warning{{implicit conversion turns floating-point number into integer: 'double' to 'int'}}
+ proto(42.0); // expected-warning{{implicit conversion turns literal floating-point number into integer}}
+ (&proto)(42.0); // expected-warning{{implicit conversion turns literal floating-point number into integer}}
}
Added: cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp?rev=125745&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-literal-conversion.cpp Thu Feb 17 05:05:49 2011
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -fsyntax-only -Wliteral-conversion -verify %s
+
+void foo(int y);
+
+// Warn when a literal float or double is assigned or bound to an integer.
+void test0() {
+ // Float
+ int y0 = 1.2222F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y1 = (1.2222F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y2 = (((1.2222F))); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y3 = 12E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y4 = 1.2E1F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ // Double
+ int y5 = 1.2222; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y6 = 12E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y7 = 1.2E1; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+ int y8 = (1.2E1); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+
+ // Test assignment to an existing variable.
+ y8 = 2.22F; // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+
+ // Test direct initialization.
+ int y9(1.23F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+
+ // Test passing a literal floating-point value to a function that takes an integer.
+ foo(1.2F); // expected-warning {{implicit conversion turns literal floating-point number into integer}}
+
+ // FIXME: -Wconversion-literal doesn't catch "-1.2F".
+ int y10 = -1.2F;
+
+ // -Wconversion-literal does NOT catch const values.
+ // (-Wconversion DOES catch them.)
+ static const float sales_tax_rate = .095F;
+ int z = sales_tax_rate;
+ foo(sales_tax_rate);
+
+ // Expressions, such as those that indicate rounding-down, should NOT produce warnings.
+ int x = 24 * 0.5;
+ int y = (24*60*60) * 0.25;
+ int pennies = 123.45 * 100;
+}
More information about the cfe-commits
mailing list