]> granicus.if.org Git - clang/commitdiff
Tweak to bitfield-overflow warning: don't warn about storing
authorJohn McCall <rjmccall@apple.com>
Wed, 10 Nov 2010 00:26:50 +0000 (00:26 +0000)
committerJohn McCall <rjmccall@apple.com>
Wed, 10 Nov 2010 00:26:50 +0000 (00:26 +0000)
a positive value into a signed bitfield of the exact width of
the value.

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

lib/Sema/SemaChecking.cpp
test/Sema/constant-conversion.c

index caaa08c0bc25522e35526a3ede3b9fad90a5990f..e69ebe2250d881eb5ffd4b180623805fdc7aca67 100644 (file)
@@ -2612,7 +2612,14 @@ void AnalyzeAssignment(Sema &S, BinaryOperator *E) {
       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);
index 5f734f3649dae2fee939eaa06e2257f43b63e02d..958621266b76e2ee0d73cb7e91b4daf1f6529774 100644 (file)
@@ -13,3 +13,8 @@ void test_7809123(void) {
 
   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
+}