]> granicus.if.org Git - clang/commitdiff
Enhance -Wunreachable-code to not consider the 'default:' branch of a switch statemen...
authorTed Kremenek <kremenek@apple.com>
Thu, 9 Sep 2010 00:06:10 +0000 (00:06 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 9 Sep 2010 00:06:10 +0000 (00:06 +0000)
explicit 'case:' statements for each enum value.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@113451 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ReachableCode.cpp
test/Sema/warn-unreachable.c

index 05439392f916138314621b72efec523bb7159316..eb3f7d4c0f0f89d41f7814f41ba4d98da84b889e 100644 (file)
@@ -131,6 +131,9 @@ static SourceLocation MarkLiveTop(const CFGBlock *Start,
   }
 
   // Solve
+  CFGBlock::FilterOptions FO;
+  FO.IgnoreDefaultsWithCoveredEnums = 1;
+
   while (!WL.empty()) {
     const CFGBlock *item = WL.back();
     WL.pop_back();
@@ -147,8 +150,8 @@ static SourceLocation MarkLiveTop(const CFGBlock *Start,
         }
 
     reachable.set(item->getBlockID());
-    for (CFGBlock::const_succ_iterator I=item->succ_begin(), E=item->succ_end();
-         I != E; ++I)
+    for (CFGBlock::filtered_succ_iterator I =
+          item->filtered_succ_start_end(FO); I.hasMore(); ++I)
       if (const CFGBlock *B = *I) {
         unsigned blockID = B->getBlockID();
         if (!reachable[blockID]) {
@@ -190,14 +193,17 @@ unsigned ScanReachableFromBlock(const CFGBlock &Start,
   ++count;
   WL.push_back(&Start);
 
-    // Find the reachable blocks from 'Start'.
+  // Find the reachable blocks from 'Start'.
+  CFGBlock::FilterOptions FO;
+  FO.IgnoreDefaultsWithCoveredEnums = 1;
+
   while (!WL.empty()) {
     const CFGBlock *item = WL.back();
     WL.pop_back();
 
       // Look at the successors and mark then reachable.
-    for (CFGBlock::const_succ_iterator I=item->succ_begin(), E=item->succ_end();
-         I != E; ++I)
+    for (CFGBlock::filtered_succ_iterator I= item->filtered_succ_start_end(FO);
+         I.hasMore(); ++I)
       if (const CFGBlock *B = *I) {
         unsigned blockID = B->getBlockID();
         if (!Reachable[blockID]) {
index 10ed6961a556285c72d68fe67ad2712a9953ca35..17a2c7c9a67c8d28d17b4359b7597f146f0edb59 100644 (file)
@@ -98,3 +98,19 @@ void test2() {
   }
   }
 }
+
+enum Cases { C1, C2, C3 };
+int test_enum_cases(enum Cases C) {
+  switch (C) {
+    case C1:
+    case C2:
+    case C3:
+      return 1;
+    default: {
+      int i = 0; // expected-warning{{will never be executed}}
+      ++i;
+      return i;
+    }
+  }  
+}
+