From e1ffd49165485a7485fd4ae2c52606d7ae2b753a Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 2 Feb 2012 00:40:20 +0000 Subject: [PATCH] Change the check for constant-conversion with width-1 bitfields so it doesn't suppress quite as many cases. Based off a testcase in the gcc testsuite. llvm-svn: 149572 --- clang/lib/Sema/SemaChecking.cpp | 4 ++-- clang/test/Sema/constant-conversion.c | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index ee0446303739..9dbee1b2b3c6 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -3758,8 +3758,8 @@ static bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, return false; // Special-case bitfields of width 1: booleans are naturally 0/1, and - // therefore don't strictly fit into a bitfield of width 1. - if (FieldWidth == 1 && Value.getBoolValue() == TruncatedValue.getBoolValue()) + // therefore don't strictly fit into a signed bitfield of width 1. + if (FieldWidth == 1 && Value == 1) return false; std::string PrettyValue = Value.toString(10); diff --git a/clang/test/Sema/constant-conversion.c b/clang/test/Sema/constant-conversion.c index e3097f8be30e..137633396712 100644 --- a/clang/test/Sema/constant-conversion.c +++ b/clang/test/Sema/constant-conversion.c @@ -74,3 +74,9 @@ void test7() { f.twoBits1 &= ~1; // no-warning f.twoBits2 &= ~2; // no-warning } + +void test8() { + enum E { A, B, C }; + struct { enum E x : 1; } f; + f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}} +} -- GitLab