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

John McCall rjmccall at apple.com
Tue Nov 9 16:26:50 PST 2010


Author: rjmccall
Date: Tue Nov  9 18:26:50 2010
New Revision: 118657

URL: http://llvm.org/viewvc/llvm-project?rev=118657&view=rev
Log:
Tweak to bitfield-overflow warning:  don't warn about storing
a positive value into a signed bitfield of the exact width of
the value.


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

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=118657&r1=118656&r2=118657&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Nov  9 18:26:50 2010
@@ -2612,7 +2612,14 @@
       if (OriginalWidth > FieldWidth) {
         llvm::APSInt TruncatedValue = Value;
         TruncatedValue.trunc(FieldWidth);
-        TruncatedValue.extend(OriginalWidth);
+
+        // It's fairly common to write values into signed bitfields
+        // that, if sign-extended, would end up becoming a different
+        // value.  We don't want to warn about that.
+        if (Value.isSigned() && Value.isNegative())
+          TruncatedValue.sext(OriginalWidth);
+        else
+          TruncatedValue.zext(OriginalWidth);
 
         if (Value != TruncatedValue) {
           std::string PrettyValue = Value.toString(10);

Modified: cfe/trunk/test/Sema/constant-conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-conversion.c?rev=118657&r1=118656&r2=118657&view=diff
==============================================================================
--- cfe/trunk/test/Sema/constant-conversion.c (original)
+++ cfe/trunk/test/Sema/constant-conversion.c Tue Nov  9 18:26:50 2010
@@ -13,3 +13,8 @@
 
   a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}}
 }
+
+void test() {
+  struct { int bit : 1; } a;
+  a.bit = 1; // shouldn't warn
+}





More information about the cfe-commits mailing list