From af663ae39c1c5da37303f6f1d956908e12d18716 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 27 Feb 2014 21:56:44 +0000 Subject: [PATCH] [CFG] record the original (now unreachable) block of 'case:' and 'default:' cases. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@202435 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/CFG.cpp | 31 +++++++++++++++++++++---------- test/Analysis/cfg.cpp | 7 ++++--- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index 180a01289b..c28513324b 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -2742,8 +2742,8 @@ CFGBlock *CFGBuilder::VisitSwitchStmt(SwitchStmt *Terminator) { SwitchAlwaysHasSuccessor |= switchExclusivelyCovered; SwitchAlwaysHasSuccessor |= Terminator->isAllEnumCasesCovered() && Terminator->getSwitchCaseList(); - addSuccessor(SwitchTerminatedBlock, - SwitchAlwaysHasSuccessor ? 0 : DefaultCaseBlock); + addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock, + !SwitchAlwaysHasSuccessor); // Add the terminator and condition in the switch block. SwitchTerminatedBlock->setTerminator(Terminator); @@ -2844,10 +2844,9 @@ CFGBlock *CFGBuilder::VisitCaseStmt(CaseStmt *CS) { // Add this block to the list of successors for the block with the switch // statement. assert(SwitchTerminatedBlock); - addSuccessor(SwitchTerminatedBlock, + addSuccessor(SwitchTerminatedBlock, CaseBlock, shouldAddCase(switchExclusivelyCovered, switchCond, - CS, *Context) - ? CaseBlock : 0); + CS, *Context)); // We set Block to NULL to allow lazy creation of a new block (if necessary) Block = NULL; @@ -4034,12 +4033,24 @@ static void print_block(raw_ostream &OS, const CFG* cfg, if (i % 10 == 8) OS << "\n "; - if (*I) - OS << " B" << (*I)->getBlockID(); - else - OS << " NULL"; + CFGBlock *B = *I; + + bool Reachable = true; + if (!B) { + Reachable = false; + B = I->getPossiblyUnreachableBlock(); + } + + if (B) { + OS << " B" << B->getBlockID(); + if (!Reachable) + OS << "(Unreachable)"; + } + else { + OS << " NULL"; + } } - + if (ShowColors) OS.resetColor(); OS << '\n'; diff --git a/test/Analysis/cfg.cpp b/test/Analysis/cfg.cpp index c465acb356..1f3bfac2c3 100644 --- a/test/Analysis/cfg.cpp +++ b/test/Analysis/cfg.cpp @@ -199,7 +199,7 @@ namespace NoReturnSingleSuccessor { // CHECK-NEXT: 1: x // CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, LValueToRValue, int) // CHECK-NEXT: 3: return [B1.2]; -// CHECK-NEXT: Preds (4): B3 B4 B5 B6 +// CHECK-NEXT: Preds (5): B3 B4 B5 B6 B2(Unreachable) // CHECK-NEXT: Succs (1): B0 // CHECK: [B2] // CHECK-NEXT: 1: 0 @@ -209,7 +209,7 @@ namespace NoReturnSingleSuccessor { // CHECK-NEXT: 5: [B2.4] (ImplicitCastExpr, IntegralCast, int) // CHECK-NEXT: T: switch [B2.5] // CHECK-NEXT: Preds (1): B7 -// CHECK-NEXT: Succs (5): B3 B4 B5 B6 NULL +// CHECK-NEXT: Succs (5): B3 B4 B5 B6 B1(Unreachable) // CHECK: [B3] // CHECK-NEXT: case D: // CHECK-NEXT: 1: 4 @@ -275,13 +275,14 @@ int test_enum_with_extension(enum MyEnum value) { // CHECK-NEXT: 5: [B2.4] (ImplicitCastExpr, IntegralCast, int) // CHECK-NEXT: T: switch [B2.5] // CHECK-NEXT: Preds (1): B7 -// CHECK-NEXT: Succs (4): B4 B5 B6 NULL +// CHECK-NEXT: Succs (4): B4 B5 B6 B3(Unreachable) // CHECK: [B3] // CHECK-NEXT: default: // CHECK-NEXT: 1: 4 // CHECK-NEXT: 2: x // CHECK-NEXT: 3: [B3.2] = [B3.1] // CHECK-NEXT: T: break; +// CHECK-NEXT: Preds (1): B2(Unreachable) // CHECK-NEXT: Succs (1): B1 // CHECK: [B4] // CHECK-NEXT: case C: -- 2.40.0