diff options
author | John McCall <rjmccall@apple.com> | 2010-11-10 00:26:50 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2010-11-10 00:26:50 +0000 |
commit | fd81c5294794f85a02c3b032c3803a7b011a4dda (patch) | |
tree | 1a6d05e5cba18fdc4551f79377bb4a03b65af37f | |
parent | 55c4cc76ce27d8211a3ea1c708a98509a253d640 (diff) | |
download | bcm5719-llvm-fd81c5294794f85a02c3b032c3803a7b011a4dda.tar.gz bcm5719-llvm-fd81c5294794f85a02c3b032c3803a7b011a4dda.zip |
Tweak to bitfield-overflow warning: don't warn about storing
a positive value into a signed bitfield of the exact width of
the value.
llvm-svn: 118657
-rw-r--r-- | clang/lib/Sema/SemaChecking.cpp | 9 | ||||
-rw-r--r-- | clang/test/Sema/constant-conversion.c | 5 |
2 files changed, 13 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index caaa08c0bc2..e69ebe2250d 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -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); diff --git a/clang/test/Sema/constant-conversion.c b/clang/test/Sema/constant-conversion.c index 5f734f3649d..958621266b7 100644 --- a/clang/test/Sema/constant-conversion.c +++ b/clang/test/Sema/constant-conversion.c @@ -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 +} |