[Diagnostics] Emit better -Wbool-operation's warning message if we known that the result is always true

llvm-svn: 373973
This commit is contained in:
David Bolvansky 2019-10-07 21:57:03 +00:00
parent ee33c61e34
commit aaea76ba02
4 changed files with 12 additions and 8 deletions

View File

@ -6638,7 +6638,8 @@ def note_member_declared_here : Note<
def note_member_first_declared_here : Note<
"member %0 first declared here">;
def warn_bitwise_negation_bool : Warning<
"bitwise negation of a boolean expression; did you mean logical negation?">,
"bitwise negation of a boolean expression%select{;| always evaluates to 'true';}0 "
"did you mean logical negation?">,
InGroup<DiagGroup<"bool-operation">>;
def err_decrement_bool : Error<"cannot decrement expression of type bool">;
def warn_increment_bool : Warning<

View File

@ -11896,6 +11896,13 @@ static void AnalyzeImplicitConversions(Sema &S, Expr *OrigE, SourceLocation CC,
if (E->isTypeDependent() || E->isValueDependent())
return;
if (const auto *UO = dyn_cast<UnaryOperator>(E))
if (UO->getOpcode() == UO_Not &&
UO->getSubExpr()->isKnownToHaveBooleanValue())
S.Diag(UO->getBeginLoc(), diag::warn_bitwise_negation_bool)
<< OrigE->getSourceRange() << T->isBooleanType()
<< FixItHint::CreateReplacement(UO->getBeginLoc(), "!");
// For conditional operators, we analyze the arguments as if they
// were being fed directly into the output.
if (isa<ConditionalOperator>(E)) {

View File

@ -13479,10 +13479,6 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
// C99 does not support '~' for complex conjugation.
Diag(OpLoc, diag::ext_integer_complement_complex)
<< resultType << Input.get()->getSourceRange();
else if (Input.get()->isKnownToHaveBooleanValue())
Diag(OpLoc, diag::warn_bitwise_negation_bool)
<< Input.get()->getSourceRange()
<< FixItHint::CreateReplacement(OpLoc, "!");
else if (resultType->hasIntegerRepresentation())
break;
else if (resultType->isExtVectorType() && Context.getLangOpts().OpenCL) {

View File

@ -12,13 +12,13 @@ typedef _Bool boolean;
#endif
void test(boolean b, int i) {
b = ~b; // expected-warning {{bitwise negation of a boolean expression; did you mean logical negation?}}
b = ~b; // expected-warning {{bitwise negation of a boolean expression always evaluates to 'true'; did you mean logical negation?}}
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:8}:"!"
b = ~(b); // expected-warning {{bitwise negation of a boolean expression; did you mean logical negation?}}
b = ~(b); // expected-warning {{bitwise negation of a boolean expression always evaluates to 'true'; did you mean logical negation?}}
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:8}:"!"
b = ~i;
i = ~b; // expected-warning {{bitwise negation of a boolean expression; did you mean logical negation?}}
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:8}:"!"
b = ~(i > 4); // expected-warning {{bitwise negation of a boolean expression; did you mean logical negation?}}
b = ~(i > 4); // expected-warning {{bitwise negation of a boolean expression always evaluates to 'true'; did you mean logical negation?}}
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:8}:"!"
}