Index: lib/Analysis/CFG.cpp =================================================================== --- lib/Analysis/CFG.cpp +++ lib/Analysis/CFG.cpp @@ -395,13 +395,15 @@ ASTContext *Context; std::unique_ptr cfg; - CFGBlock *Block; - CFGBlock *Succ; + CFGBlock *Block; // Current block. + CFGBlock *Succ; // Block after the current block. JumpTarget ContinueJumpTarget; JumpTarget BreakJumpTarget; + JumpTarget SEHLeaveJumpTarget; CFGBlock *SwitchTerminatedBlock; CFGBlock *DefaultCaseBlock; CFGBlock *TryTerminatedBlock; + CFGBlock *SEHTryTerminatedBlock; // Current position in local scope. LocalScope::const_iterator ScopePos; @@ -436,13 +438,13 @@ public: explicit CFGBuilder(ASTContext *astContext, - const CFG::BuildOptions &buildOpts) - : Context(astContext), cfg(new CFG()), // crew a new CFG - Block(nullptr), Succ(nullptr), - SwitchTerminatedBlock(nullptr), DefaultCaseBlock(nullptr), - TryTerminatedBlock(nullptr), badCFG(false), BuildOpts(buildOpts), - switchExclusivelyCovered(false), switchCond(nullptr), - cachedEntry(nullptr), lastLookup(nullptr) {} + const CFG::BuildOptions &buildOpts) + : Context(astContext), cfg(new CFG()), // crew a new CFG + Block(nullptr), Succ(nullptr), SwitchTerminatedBlock(nullptr), + DefaultCaseBlock(nullptr), TryTerminatedBlock(nullptr), + SEHTryTerminatedBlock(nullptr), badCFG(false), BuildOpts(buildOpts), + switchExclusivelyCovered(false), switchCond(nullptr), + cachedEntry(nullptr), lastLookup(nullptr) {} // buildCFG - Used by external clients to construct the CFG. std::unique_ptr buildCFG(const Decl *D, Stmt *Statement); @@ -501,6 +503,10 @@ CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); CFGBlock *VisitPseudoObjectExpr(PseudoObjectExpr *E); CFGBlock *VisitReturnStmt(ReturnStmt *R); + CFGBlock *VisitSEHExceptStmt(SEHExceptStmt *S); + CFGBlock *VisitSEHFinallyStmt(SEHFinallyStmt *S); + CFGBlock *VisitSEHLeaveStmt(SEHLeaveStmt *S); + CFGBlock *VisitSEHTryStmt(SEHTryStmt *S); CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc); CFGBlock *VisitSwitchStmt(SwitchStmt *S); CFGBlock *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E, @@ -1716,6 +1722,18 @@ case Stmt::ReturnStmtClass: return VisitReturnStmt(cast(S)); + case Stmt::SEHExceptStmtClass: + return VisitSEHExceptStmt(cast(S)); + + case Stmt::SEHFinallyStmtClass: + return VisitSEHFinallyStmt(cast(S)); + + case Stmt::SEHLeaveStmtClass: + return VisitSEHLeaveStmt(cast(S)); + + case Stmt::SEHTryStmtClass: + return VisitSEHTryStmt(cast(S)); + case Stmt::UnaryExprOrTypeTraitExprClass: return VisitUnaryExprOrTypeTraitExpr(cast(S), asc); @@ -2447,6 +2465,118 @@ return VisitStmt(R, AddStmtChoice::AlwaysAdd); } +CFGBlock *CFGBuilder::VisitSEHExceptStmt(SEHExceptStmt *ES) { + // SEHExceptStmt are treated like labels, so they are the first statement in a + // block. + + // Save local scope position because in case of exception variable ScopePos + // won't be restored when traversing AST. + SaveAndRestore save_scope_pos(ScopePos); + + addStmt(ES->getBlock()); + CFGBlock *SEHExceptBlock = Block; + if (!SEHExceptBlock) + SEHExceptBlock = createBlock(); + + appendStmt(SEHExceptBlock, ES); + + // Also add the SEHExceptBlock as a label, like with regular labels. + SEHExceptBlock->setLabel(ES); + + // Bail out if the CFG is bad. + if (badCFG) + return nullptr; + + // We set Block to NULL to allow lazy creation of a new block (if necessary). + Block = nullptr; + + return SEHExceptBlock; +} + +CFGBlock *CFGBuilder::VisitSEHFinallyStmt(SEHFinallyStmt *FS) { + return VisitCompoundStmt(FS->getBlock()); +} + +CFGBlock *CFGBuilder::VisitSEHLeaveStmt(SEHLeaveStmt *LS) { + // "__leave" is a control-flow statement. Thus we stop processing the current + // block. + if (badCFG) + return nullptr; + + // Now create a new block that ends with the __leave statement. + Block = createBlock(false); + Block->setTerminator(LS); + + // If there is no target for the __leave, then we are looking at an incomplete + // AST. This means that the CFG cannot be constructed. + if (SEHLeaveJumpTarget.block) { + addAutomaticObjHandling(ScopePos, SEHLeaveJumpTarget.scopePosition, LS); + addSuccessor(Block, SEHLeaveJumpTarget.block); + } else + badCFG = true; + + return Block; +} + +CFGBlock *CFGBuilder::VisitSEHTryStmt(SEHTryStmt *Terminator) { + // "__try"/"__except"/"__finally" is a control-flow statement. Thus we stop + // processing the current block. + CFGBlock *SEHTrySuccessor = nullptr; + + if (Block) { + if (badCFG) + return nullptr; + SEHTrySuccessor = Block; + } else SEHTrySuccessor = Succ; + + // FIXME: Implement __finally support. + if (Terminator->getFinallyHandler()) + return NYS(); + + CFGBlock *PrevSEHTryTerminatedBlock = SEHTryTerminatedBlock; + + // Create a new block that will contain the __try statement. + CFGBlock *NewSEHTryTerminatedBlock = createBlock(false); + + // Add the terminator in the __try block. + NewSEHTryTerminatedBlock->setTerminator(Terminator); + + if (SEHExceptStmt *Except = Terminator->getExceptHandler()) { + // The code after the try is the implicit successor if there's an __except. + Succ = SEHTrySuccessor; + Block = nullptr; + CFGBlock *ExceptBlock = VisitSEHExceptStmt(Except); + if (!ExceptBlock) + return nullptr; + // Add this block to the list of successors for the block with the try + // statement. + addSuccessor(NewSEHTryTerminatedBlock, ExceptBlock); + } + if (PrevSEHTryTerminatedBlock) + addSuccessor(NewSEHTryTerminatedBlock, PrevSEHTryTerminatedBlock); + else + addSuccessor(NewSEHTryTerminatedBlock, &cfg->getExit()); + + // The code after the try is the implicit successor. + Succ = SEHTrySuccessor; + + // Save the current "__try" context. + SaveAndRestore save_try(SEHTryTerminatedBlock, + NewSEHTryTerminatedBlock); + cfg->addTryDispatchBlock(SEHTryTerminatedBlock); + + // Save the current value for the __leave target. + // All __leaves should go to the code following the __try + // (FIXME: or if the __try // has a __finally, to the __finally.) + SaveAndRestore save_break(SEHLeaveJumpTarget); + SEHLeaveJumpTarget = JumpTarget(SEHTrySuccessor, ScopePos); + + assert(Terminator->getTryBlock() && "__try must contain a non-NULL body"); + Block = nullptr; + return addStmt(Terminator->getTryBlock()); + +} + CFGBlock *CFGBuilder::VisitLabelStmt(LabelStmt *L) { // Get the block of the labeled statement. Add it to our map. addStmt(L->getSubStmt()); @@ -4302,6 +4432,10 @@ OS << "try ..."; } + void VisitSEHTryStmt(SEHTryStmt *CS) { + OS << "__try ..."; + } + void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) { if (Stmt *Cond = C->getCond()) Cond->printPretty(OS, Helper, Policy); @@ -4531,7 +4665,11 @@ else OS << "..."; OS << ")"; - + } else if (SEHExceptStmt *ES = dyn_cast(Label)) { + OS << "__except ("; + ES->getFilterExpr()->printPretty(OS, &Helper, + PrintingPolicy(Helper.getLangOpts()), 0); + OS << ")"; } else llvm_unreachable("Invalid label statement in CFGBlock."); Index: test/Sema/warn-unreachable-ms.c =================================================================== --- test/Sema/warn-unreachable-ms.c +++ test/Sema/warn-unreachable-ms.c @@ -0,0 +1,42 @@ +// RUN: %clang_cc1 %s -triple=i686-pc-win32 -fsyntax-only -verify -fms-extensions -Wunreachable-code + +void f(); + +void g1() { + __try { + f(); + __leave; + f(); // expected-warning{{will never be executed}} + } __except(1) { + f(); + } + + // Completely empty. + __try { + } __except(1) { + } + + __try { + f(); + return; + } __except(1) { // Filter expression should not be marked as unreachable. + // Emtpy __except body. + } +} + +void g2() { + __try { + // Nested __try. + __try { + f(); + __leave; + f(); // expected-warning{{will never be executed}} + } __except(2) { + } + f(); + __leave; + f(); // expected-warning{{will never be executed}} + } __except(1) { + f(); + } +}