r203283 - [-Wunreachable-code] Treat constant globals as configuration values in unreachable code heuristics.

Ted Kremenek kremenek at apple.com
Fri Mar 7 12:51:13 PST 2014


Author: kremenek
Date: Fri Mar  7 14:51:13 2014
New Revision: 203283

URL: http://llvm.org/viewvc/llvm-project?rev=203283&view=rev
Log:
[-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.

Modified:
    cfe/trunk/lib/Analysis/ReachableCode.cpp
    cfe/trunk/test/SemaCXX/warn-unreachable.cpp

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=203283&r1=203282&r2=203283&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Fri Mar  7 14:51:13 2014
@@ -194,8 +194,20 @@ static bool isConfigurationValue(const S
   switch (S->getStmtClass()) {
     case Stmt::DeclRefExprClass: {
       const DeclRefExpr *DR = cast<DeclRefExpr>(S);
-      const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
-      return ED ? isConfigurationValue(ED->getInitExpr()) : false;
+      const ValueDecl *D = DR->getDecl();
+      if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D))
+        return ED ? isConfigurationValue(ED->getInitExpr()) : false;
+      if (const VarDecl *VD = dyn_cast<VarDecl>(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);

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=203283&r1=203282&r2=203283&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Fri Mar  7 14:51:13 2014
@@ -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
+}
+





More information about the cfe-commits mailing list