summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2010-11-11 05:33:51 +0000
committerJohn McCall <rjmccall@apple.com>2010-11-11 05:33:51 +0000
commitdeebbcf20da9820f8aecdbe7b16e720e027d203a (patch)
treeabaf2e7c4ab5ee90eebef0b18e3277f605769a94
parent78e3fdb8a7a1150efa4d05341f57d5fd4012abb9 (diff)
downloadbcm5719-llvm-deebbcf20da9820f8aecdbe7b16e720e027d203a.tar.gz
bcm5719-llvm-deebbcf20da9820f8aecdbe7b16e720e027d203a.zip
Undo a refactor-o and base the bitfield-truncation warning on the
uncoerced value. Also, whitelist bool bitfields, which aren't really a truncation. llvm-svn: 118778
-rw-r--r--clang/lib/Sema/SemaChecking.cpp6
-rw-r--r--clang/test/Sema/constant-conversion.c18
2 files changed, 23 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4fc16f51ee0..56a0c076ad3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -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;
diff --git a/clang/test/Sema/constant-conversion.c b/clang/test/Sema/constant-conversion.c
index af77741fc13..7c6b9b81bd0 100644
--- a/clang/test/Sema/constant-conversion.c
+++ b/clang/test/Sema/constant-conversion.c
@@ -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;
+}
OpenPOWER on IntegriCloud