From 935cd6e8529edbb3b0eba660bd934b508766de37 Mon Sep 17 00:00:00 2001 From: John McCall Date: Wed, 10 Nov 2010 00:26:50 +0000 Subject: [PATCH] Tweak to bitfield-overflow warning: don't warn about storing 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 | 9 ++++++++- test/Sema/constant-conversion.c | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index caaa08c0bc..e69ebe2250 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/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/test/Sema/constant-conversion.c b/test/Sema/constant-conversion.c index 5f734f3649..958621266b 100644 --- a/test/Sema/constant-conversion.c +++ b/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 +} -- 2.50.1