From cccfdc452b63e64f1e838833c7f055d832559b0d Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Fri, 7 Mar 2014 20:51:13 +0000 Subject: [PATCH] [-Wunreachable-code] Treat constant globals as configuration values in unreachable code heuristics. This one could possibly be refined even further; e.g. looking at the initializer and see if it is truly a configuration value. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203283 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ReachableCode.cpp | 16 ++++++++++++++-- test/SemaCXX/warn-unreachable.cpp | 7 +++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/Analysis/ReachableCode.cpp b/lib/Analysis/ReachableCode.cpp index f563cbdc59..47f8f2b90c 100644 --- a/lib/Analysis/ReachableCode.cpp +++ b/lib/Analysis/ReachableCode.cpp @@ -194,8 +194,20 @@ static bool isConfigurationValue(const Stmt *S) { switch (S->getStmtClass()) { case Stmt::DeclRefExprClass: { const DeclRefExpr *DR = cast(S); - const EnumConstantDecl *ED = dyn_cast(DR->getDecl()); - return ED ? isConfigurationValue(ED->getInitExpr()) : false; + const ValueDecl *D = DR->getDecl(); + if (const EnumConstantDecl *ED = dyn_cast(D)) + return ED ? isConfigurationValue(ED->getInitExpr()) : false; + if (const VarDecl *VD = dyn_cast(D)) { + // As a heuristic, treat globals as configuration values. Note + // that we only will get here if Sema evaluated this + // condition to a constant expression, which means the global + // had to be declared in a way to be a truly constant value. + // We could generalize this to local variables, but it isn't + // clear if those truly represent configuration values that + // gate unreachable code. + return !VD->hasLocalStorage(); + } + return false; } case Stmt::IntegerLiteralClass: return isExpandedFromConfigurationMacro(S); diff --git a/test/SemaCXX/warn-unreachable.cpp b/test/SemaCXX/warn-unreachable.cpp index 1030c993ad..8c191f6fad 100644 --- a/test/SemaCXX/warn-unreachable.cpp +++ b/test/SemaCXX/warn-unreachable.cpp @@ -155,3 +155,10 @@ bool testBool() { return true; // no-warning } +static const bool ConditionVar = 1; +int test_global_as_conditionVariable() { + if (ConditionVar) + return 1; + return 0; // no-warning +} + -- 2.40.0