r203094 - [-Wunreachable-code] Refine treating all branches of 'switch' as reachable, which includes those with all cases covered but with no 'default:'.

Ted Kremenek kremenek at apple.com
Thu Mar 6 00:09:00 PST 2014


Author: kremenek
Date: Thu Mar  6 02:09:00 2014
New Revision: 203094

URL: http://llvm.org/viewvc/llvm-project?rev=203094&view=rev
Log:
[-Wunreachable-code] Refine treating all branches of 'switch' as reachable, which includes those with all cases covered but with no 'default:'.

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

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=203094&r1=203093&r2=203094&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Thu Mar  6 02:09:00 2014
@@ -457,6 +457,10 @@ static bool isConfigurationValue(const S
 
 /// Returns true if we should always explore all successors of a block.
 static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B) {
+  if (const Stmt *Term = B->getTerminator())
+    if (isa<SwitchStmt>(Term))
+      return true;
+
   return isConfigurationValue(B->getTerminatorCondition());
 }
 
@@ -500,24 +504,6 @@ unsigned ScanReachableFromBlock(const CF
           B = UB;
           break;
         }
-
-        // For switch statements, treat all cases as being reachable.
-        // There are many cases where a switch can contain values that
-        // are not in an enumeration but they are still reachable because
-        // other values are possible.
-        //
-        // Note that this is quite conservative.  If one saw:
-        //
-        //  switch (1) {
-        //    case 2: ...
-        //
-        // we should be able to say that 'case 2' is unreachable.  To do
-        // this we can either put more heuristics here, or possibly retain
-        // that information in the CFG itself.
-        //
-        const Stmt *Label = UB->getLabel();
-        if (Label && isa<SwitchCase>(Label))
-          B = UB;
       }
       while (false);
 

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=203094&r1=203093&r2=203094&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Thu Mar  6 02:09:00 2014
@@ -226,6 +226,17 @@ MyEnum nontrivial_dead_return_enum_2(int
   return calledFun(); // expected-warning {{will never be executed}}
 }
 
+enum X { A, B, C };
+
+int covered_switch(enum X x) {
+  switch (x) {
+  case A: return 1;
+  case B: return 2;
+  case C: return 3;
+  }
+  return 4; // no-warning
+}
+
 // Test unreachable code depending on configuration values
 #define CONFIG_CONSTANT 1
 int test_config_constant(int x) {





More information about the cfe-commits mailing list