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-Tag: llvmorg-11-init~7285 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=aaea76ba02301efd8aa0c8d5da4af400d03b2fb6;p=platform%2Fupstream%2Fllvm.git [Diagnostics] Emit better -Wbool-operation's warning message if we known that the result is always true llvm-svn: 373973 --- diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c543806..23cedca 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/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/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index eeddff6..de8e1ef 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/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/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 3cb999d..f08b616 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/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/clang/test/Sema/warn-bitwise-negation-bool.c b/clang/test/Sema/warn-bitwise-negation-bool.c index 435d783..c74705b 100644 --- a/clang/test/Sema/warn-bitwise-negation-bool.c +++ b/clang/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}:"!" }