From 0464fcd3b9d46cc137829b74e3f6742a312d4d13 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Sat, 29 Mar 2014 04:49:20 +0000 Subject: [PATCH] [-Wunreachable-code] Expand paren-suppression heuristic to C++/ObjC bools. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205074 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ReachableCode.cpp | 17 ++++++++++------ test/SemaCXX/warn-unreachable.cpp | 33 +++++++++++++++++++++++++++++++ test/SemaObjC/warn-unreachable.m | 33 +++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/lib/Analysis/ReachableCode.cpp b/lib/Analysis/ReachableCode.cpp index 7c8ea39dc5..3cc8ae4a08 100644 --- a/lib/Analysis/ReachableCode.cpp +++ b/lib/Analysis/ReachableCode.cpp @@ -141,12 +141,15 @@ static bool isConfigurationValue(const Stmt *S, // Special case looking for the sigil '()' around an integer literal. if (const ParenExpr *PE = dyn_cast(S)) - return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal, - IncludeIntegers, true); + if (!PE->getLocStart().isMacroID()) + return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal, + IncludeIntegers, true); if (const Expr *Ex = dyn_cast(S)) S = Ex->IgnoreParenCasts(); + bool IgnoreYES_NO = false; + switch (S->getStmtClass()) { case Stmt::CallExprClass: { const FunctionDecl *Callee = @@ -155,19 +158,21 @@ static bool isConfigurationValue(const Stmt *S, } case Stmt::DeclRefExprClass: return isConfigurationValue(cast(S)->getDecl(), PP); + case Stmt::ObjCBoolLiteralExprClass: + IgnoreYES_NO = true; + // Fallthrough. + case Stmt::CXXBoolLiteralExprClass: case Stmt::IntegerLiteralClass: { - const IntegerLiteral *E = cast(S); + const Expr *E = cast(S); if (IncludeIntegers) { if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid()) *SilenceableCondVal = E->getSourceRange(); - return WrappedInParens || isExpandedFromConfigurationMacro(E, PP); + return WrappedInParens || isExpandedFromConfigurationMacro(E, PP, IgnoreYES_NO); } return false; } case Stmt::MemberExprClass: return isConfigurationValue(cast(S)->getMemberDecl(), PP); - case Stmt::ObjCBoolLiteralExprClass: - return isExpandedFromConfigurationMacro(S, PP, /* IgnoreYES_NO */ true); case Stmt::UnaryExprOrTypeTraitExprClass: return true; case Stmt::BinaryOperatorClass: { diff --git a/test/SemaCXX/warn-unreachable.cpp b/test/SemaCXX/warn-unreachable.cpp index 8acaf42855..04bd74304b 100644 --- a/test/SemaCXX/warn-unreachable.cpp +++ b/test/SemaCXX/warn-unreachable.cpp @@ -294,3 +294,36 @@ void test_unreachable_forrange_increment() { } } +void calledFun() {} + +// Test "silencing" with parentheses. +void test_with_paren_silencing(int x) { + if (false) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}} + if ((false)) calledFun(); // no-warning + + if (true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}} + calledFun(); + else + calledFun(); // expected-warning {{will never be executed}} + + if ((true)) + calledFun(); + else + calledFun(); // no-warning + + if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}} + calledFun(); // expected-warning {{code will never be executed}} + else + calledFun(); + + if ((!true)) + calledFun(); // no-warning + else + calledFun(); + + if (!(true)) + calledFun(); // no-warning + else + calledFun(); +} + diff --git a/test/SemaObjC/warn-unreachable.m b/test/SemaObjC/warn-unreachable.m index 859bd00f31..366767629d 100644 --- a/test/SemaObjC/warn-unreachable.m +++ b/test/SemaObjC/warn-unreachable.m @@ -49,3 +49,36 @@ void test_loop_increment(id container) { } } +void calledFun() {} + +// Test "silencing" with parentheses. +void test_with_paren_silencing(int x) { + if (NO) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}} + if ((NO)) calledFun(); // no-warning + + if (YES) // expected-note {{silence by adding parentheses to mark code as explicitly dead}} + calledFun(); + else + calledFun(); // expected-warning {{will never be executed}} + + if ((YES)) + calledFun(); + else + calledFun(); // no-warning + + if (!YES) // expected-note {{silence by adding parentheses to mark code as explicitly dead}} + calledFun(); // expected-warning {{code will never be executed}} + else + calledFun(); + + if ((!YES)) + calledFun(); // no-warning + else + calledFun(); + + if (!(YES)) + calledFun(); // no-warning + else + calledFun(); +} + -- 2.40.0