From: Daniel Marjamaki Date: Thu, 22 Sep 2016 14:13:46 +0000 (+0000) Subject: Fix Wbitfield-constant-conversion false positives X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d9a3168f1086ee904474f354d0e3d64f43cc7e47;p=clang Fix Wbitfield-constant-conversion false positives Summary: The diagnostic did not handle ~ well. An expression such as ~0 is often used when 'all ones' is needed. Differential Revision: https://reviews.llvm.org/D24232 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@282156 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 0604c5aeac..119fe8c6d2 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -8006,11 +8006,10 @@ bool AnalyzeBitFieldAssignment(Sema &S, FieldDecl *Bitfield, Expr *Init, unsigned OriginalWidth = Value.getBitWidth(); unsigned FieldWidth = Bitfield->getBitWidthValue(S.Context); - if (Value.isSigned() && Value.isNegative()) + if (!Value.isSigned() || Value.isNegative()) if (UnaryOperator *UO = dyn_cast(OriginalInit)) - if (UO->getOpcode() == UO_Minus) - if (isa(UO->getSubExpr())) - OriginalWidth = Value.getMinSignedBits(); + if (UO->getOpcode() == UO_Minus || UO->getOpcode() == UO_Not) + OriginalWidth = Value.getMinSignedBits(); if (OriginalWidth <= FieldWidth) return false; diff --git a/test/Sema/constant-conversion.c b/test/Sema/constant-conversion.c index 203e737389..b67c286f53 100644 --- a/test/Sema/constant-conversion.c +++ b/test/Sema/constant-conversion.c @@ -69,7 +69,8 @@ void test7() { unsigned int reserved:28; } f; - f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}} + f.twoBits1 = ~0; // no-warning + f.twoBits1 = ~1; // no-warning f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}} f.twoBits1 &= ~1; // no-warning f.twoBits2 &= ~2; // no-warning @@ -114,6 +115,8 @@ void test9() { char array_init[] = { 255, 127, 128, 129, 0 }; } +#define A 1 + void test10() { struct S { unsigned a : 4; @@ -121,7 +124,10 @@ void test10() { s.a = -1; s.a = 15; s.a = -8; + s.a = ~0; + s.a = ~0U; + s.a = ~(1<