]> granicus.if.org Git - clang/commitdiff
Undo a refactor-o and base the bitfield-truncation warning on the
authorJohn McCall <rjmccall@apple.com>
Thu, 11 Nov 2010 05:33:51 +0000 (05:33 +0000)
committerJohn McCall <rjmccall@apple.com>
Thu, 11 Nov 2010 05:33:51 +0000 (05:33 +0000)
uncoerced value.  Also, whitelist bool bitfields, which aren't
really a truncation.

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

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

index 4fc16f51ee0face09488ded4c20f8ad4ab0b61e8..56a0c076ad31201c37a9b5bc2229b2df8f343dbb 100644 (file)
@@ -2617,12 +2617,16 @@ bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init,
   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;
 
index af77741fc137dc26d284bb9f42eec3d7af0d88cb..7c6b9b81bd0fa1a3bcf93abdcd6fc69510f8283c 100644 (file)
@@ -37,3 +37,21 @@ void test3() {
   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;
+}