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

John McCall rjmccall at apple.com
Wed Nov 10 21:33:51 PST 2010


Author: rjmccall
Date: Wed Nov 10 23:33:51 2010
New Revision: 118778

URL: http://llvm.org/viewvc/llvm-project?rev=118778&view=rev
Log:
Undo a refactor-o and base the bitfield-truncation warning on the
uncoerced value.  Also, whitelist bool bitfields, which aren't
really a truncation.


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=118778&r1=118777&r2=118778&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Nov 10 23:33:51 2010
@@ -2617,12 +2617,16 @@
   if (Bitfield->isInvalidDecl())
     return false;
 
+  // White-list bool bitfields.
+  if (Bitfield->getType()->isBooleanType())
+    return false;
+
   Expr *OriginalInit = Init->IgnoreParenImpCasts();
 
   llvm::APSInt Width(32);
   Expr::EvalResult InitValue;
   if (!Bitfield->getBitWidth()->isIntegerConstantExpr(Width, S.Context) ||
-      !Init->Evaluate(InitValue, S.Context) ||
+      !OriginalInit->Evaluate(InitValue, S.Context) ||
       !InitValue.Val.isInt())
     return false;
 

Modified: cfe/trunk/test/Sema/constant-conversion.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-conversion.c?rev=118778&r1=118777&r2=118778&view=diff
==============================================================================
--- cfe/trunk/test/Sema/constant-conversion.c (original)
+++ cfe/trunk/test/Sema/constant-conversion.c Wed Nov 10 23:33:51 2010
@@ -37,3 +37,21 @@
   struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
   struct A e = { .foo = 10 };        // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
 }
+
+void test4() {
+  struct A {
+    char c : 2;
+  } a;
+
+  a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}}
+}
+
+void test5() {
+  struct A {
+    _Bool b : 1;
+  } a;
+
+  // Don't warn about this implicit conversion to bool, or at least
+  // don't warn about it just because it's a bitfield.
+  a.b = 100;
+}





More information about the cfe-commits mailing list