]> granicus.if.org Git - clang/commitdiff
-Wunreachable-code: treat 'const bool' locals as control values.
authorTed Kremenek <kremenek@apple.com>
Sat, 15 Mar 2014 06:47:45 +0000 (06:47 +0000)
committerTed Kremenek <kremenek@apple.com>
Sat, 15 Mar 2014 06:47:45 +0000 (06:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204001 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ReachableCode.cpp
test/SemaCXX/warn-unreachable.cpp

index d297d03775ddf669a41ff18d324a087bd6f5bf3d..71d41bb28398c2809ccb12f18b682ac5ee0b6d97 100644 (file)
@@ -236,7 +236,12 @@ static bool isConfigurationValue(const Stmt *S,
         // 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();
+        if (!VD->hasLocalStorage())
+          return true;
+
+        // As a heuristic, locals that have been marked 'const' explicitly
+        // can be treated as configuration values as well.
+        return VD->getType().isLocalConstQualified();
       }
       return false;
     }
index 17f0b9bf528b994ef63dfad34ffa2d93d7062063..2370fe0da24a23ad8b540c8ebad0d963043f1cbe 100644 (file)
@@ -199,3 +199,22 @@ int test_arithmetic() {
   return 2; // expected-warning {{never be executed}}
 }
 
+int test_treat_const_bool_local_as_config_value() {
+  const bool controlValue = false;
+  if (!controlValue)
+    return 1;
+  test_treat_const_bool_local_as_config_value(); // no-warning
+  return 0;
+}
+
+int test_treat_non_const_bool_local_as_non_config_value() {
+  bool controlValue = false;
+  if (!controlValue)
+    return 1;
+  // There is no warning here because 'controlValue' isn't really
+  // a control value at all.  The CFG will not treat this
+  // branch as unreachable.
+  test_treat_non_const_bool_local_as_non_config_value(); // no-warning
+  return 0;
+}
+