From: David Bolvansky Date: Mon, 7 Oct 2019 21:57:03 +0000 (+0000) Subject: [Diagnostics] Emit better -Wbool-operation's warning message if we known that the... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bfb7afed5cdfdf021937d28826861825feb84969;p=clang [Diagnostics] Emit better -Wbool-operation's warning message if we known that the result is always true git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@373973 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c54380639a..23cedcab27 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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>; def err_decrement_bool : Error<"cannot decrement expression of type bool">; def warn_increment_bool : Warning< diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index eeddff6c71..de8e1ef87a 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -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(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(E)) { diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 3cb999dacc..f08b616809 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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) { diff --git a/test/Sema/warn-bitwise-negation-bool.c b/test/Sema/warn-bitwise-negation-bool.c index 435d783439..c74705bc76 100644 --- a/test/Sema/warn-bitwise-negation-bool.c +++ b/test/Sema/warn-bitwise-negation-bool.c @@ -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}:"!" }