diff options
author | Ted Kremenek <kremenek@apple.com> | 2010-09-09 00:06:10 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2010-09-09 00:06:10 +0000 |
commit | 8caec849a765de7b0b4ae8b9769397ce62236321 (patch) | |
tree | d035767e26077c15e3e69e3a7bd46f68ae700cd4 | |
parent | 90b828aa279542559f655d1af666580288cb1841 (diff) |
Enhance -Wunreachable-code to not consider the 'default:' branch of a switch statement live if a switch on an enum value has
explicit 'case:' statements for each enum value.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@113451 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Analysis/ReachableCode.cpp | 16 | ||||
-rw-r--r-- | test/Sema/warn-unreachable.c | 16 |
2 files changed, 27 insertions, 5 deletions
diff --git a/lib/Analysis/ReachableCode.cpp b/lib/Analysis/ReachableCode.cpp index 05439392f9..eb3f7d4c0f 100644 --- a/lib/Analysis/ReachableCode.cpp +++ b/lib/Analysis/ReachableCode.cpp @@ -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]) { diff --git a/test/Sema/warn-unreachable.c b/test/Sema/warn-unreachable.c index 10ed6961a5..17a2c7c9a6 100644 --- a/test/Sema/warn-unreachable.c +++ b/test/Sema/warn-unreachable.c @@ -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; + } + } +} + |