[cfe-commits] r142716 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/Sema/constant-conversion.c test/Sema/conversion.c

Ted Kremenek kremenek at apple.com
Fri Oct 21 19:37:34 PDT 2011


Author: kremenek
Date: Fri Oct 21 21:37:33 2011
New Revision: 142716

URL: http://llvm.org/viewvc/llvm-project?rev=142716&view=rev
Log:
Only emit implicit constant conversion truncation warnings in reachable code.  Apparently this is what GCC does, and some code depends on this.  Fixes <rdar://problem/10321089>.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/constant-conversion.c
    cfe/trunk/test/Sema/conversion.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=142716&r1=142715&r2=142716&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Oct 21 21:37:33 2011
@@ -3536,9 +3536,11 @@
       std::string PrettySourceValue = Value.toString(10);
       std::string PrettyTargetValue = PrettyPrintInRange(Value, TargetRange);
 
-      S.Diag(E->getExprLoc(), diag::warn_impcast_integer_precision_constant)
-        << PrettySourceValue << PrettyTargetValue
-        << E->getType() << T << E->getSourceRange() << clang::SourceRange(CC);
+      S.DiagRuntimeBehavior(E->getExprLoc(), E,
+        S.PDiag(diag::warn_impcast_integer_precision_constant)
+            << PrettySourceValue << PrettyTargetValue
+            << E->getType() << T << E->getSourceRange()
+            << clang::SourceRange(CC));
       return;
     }
 

Modified: cfe/trunk/test/Sema/constant-conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-conversion.c?rev=142716&r1=142715&r2=142716&view=diff
==============================================================================
--- cfe/trunk/test/Sema/constant-conversion.c (original)
+++ cfe/trunk/test/Sema/constant-conversion.c Fri Oct 21 21:37:33 2011
@@ -55,3 +55,10 @@
   // don't warn about it just because it's a bitfield.
   a.b = 100;
 }
+
+void test6() {
+  // Test that unreachable code doesn't trigger the truncation warning.
+  unsigned char x = 0 ? 65535 : 1; // no-warning
+  unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
+}
+

Modified: cfe/trunk/test/Sema/conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/conversion.c?rev=142716&r1=142715&r2=142716&view=diff
==============================================================================
--- cfe/trunk/test/Sema/conversion.c (original)
+++ cfe/trunk/test/Sema/conversion.c Fri Oct 21 21:37:33 2011
@@ -61,53 +61,125 @@
 
 char test1(long long ll) {
   return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+char test1_a(long long ll) {
   return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+char test1_b(long long ll) {
   return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+char test1_c(long long ll) {
   return (short) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+char test1_d(long long ll) {
   return (char) ll;
+}
+char test1_e(long long ll) {
   return (long long) BIG; // expected-warning {{implicit conversion from 'long long' to 'char' changes value}}
+}
+char test1_f(long long ll) {
   return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'char' changes value}}
+}
+char test1_g(long long ll) {
   return (int) BIG; // expected-warning {{implicit conversion from 'int' to 'char' changes value}}
+}
+char test1_h(long long ll) {
   return (short) BIG; // expected-warning {{implicit conversion from 'short' to 'char' changes value}}
+}
+char test1_i(long long ll) {
   return (char) BIG;
 }
 
 short test2(long long ll) {
   return (long long) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+short test2_a(long long ll) {
   return (long) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+short test2_b(long long ll) {
   return (int) ll; // expected-warning {{implicit conversion loses integer precision}}
+}
+short test2_c(long long ll) {
   return (short) ll;
+}
+short test2_d(long long ll) {
   return (char) ll;
+}
+short test2_e(long long ll) {
   return (long long) BIG;  // expected-warning {{implicit conversion from 'long long' to 'short' changes value}}
+}
+short test2_f(long long ll) {
   return (long) BIG;  // expected-warning {{implicit conversion from 'long' to 'short' changes value}}
+}
+short test2_g(long long ll) {
   return (int) BIG;  // expected-warning {{implicit conversion from 'int' to 'short' changes value}}
+}
+short test2_h(long long ll) {
   return (short) BIG;
+}
+short test2_i(long long ll) {
   return (char) BIG;
 }
 
 int test3(long long ll) {
   return (long long) ll;  // expected-warning {{implicit conversion loses integer precision}}
+}
+int test3_b(long long ll) {
   return (long) ll;  // expected-warning {{implicit conversion loses integer precision}}
+}
+int test3_c(long long ll) {
   return (int) ll;
+}
+int test3_d(long long ll) {
   return (short) ll;
+}
+int test3_e(long long ll) {
   return (char) ll;
+}
+int test3_f(long long ll) {
   return (long long) BIG;  // expected-warning {{implicit conversion from 'long long' to 'int' changes value}}
+}
+int test3_g(long long ll) {
   return (long) BIG; // expected-warning {{implicit conversion from 'long' to 'int' changes value}}
+}
+int test3_h(long long ll) {
   return (int) BIG;
+}
+int test3_i(long long ll) {
   return (short) BIG;
+}
+int test3_j(long long ll) {
   return (char) BIG;
 }
 
 long test4(long long ll) {
   return (long long) ll;
+}
+long test4_a(long long ll) {
   return (long) ll;
+}
+long test4_b(long long ll) {
   return (int) ll;
+}
+long test4_c(long long ll) {
   return (short) ll;
+}
+long test4_d(long long ll) {
   return (char) ll;
+}
+long test4_e(long long ll) {
   return (long long) BIG;
+}
+long test4_f(long long ll) {
   return (long) BIG;
+}
+long test4_g(long long ll) {
   return (int) BIG;
+}
+long test4_h(long long ll) {
   return (short) BIG;
+}
+long test4_i(long long ll) {
   return (char) BIG;
 }
 





More information about the cfe-commits mailing list