Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1 | //===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the CFG and CFGBuilder classes for representing and |
| 11 | // building Control-Flow Graphs (CFGs) from ASTs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | b1c170e | 2009-07-22 21:45:16 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Support/SaveAndRestore.h" |
Ted Kremenek | 6796fbd | 2009-07-16 18:13:04 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 1b8ac85 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 21 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | a59e006 | 2007-12-17 19:35:20 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 24 | #include <llvm/Support/Allocator.h> |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 25 | #include <llvm/Support/Format.h> |
Ted Kremenek | e5ccf9a | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | namespace { |
| 30 | |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 31 | static SourceLocation GetEndLoc(Decl* D) { |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 32 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) |
| 33 | if (Expr* Ex = VD->getInit()) |
| 34 | return Ex->getSourceRange().getEnd(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 35 | |
| 36 | return D->getLocation(); |
Ted Kremenek | 8889bb3 | 2008-08-06 23:20:50 +0000 | [diff] [blame] | 37 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | be9b33b | 2008-08-04 22:51:42 +0000 | [diff] [blame] | 39 | /// CFGBuilder - This class implements CFG construction from an AST. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 40 | /// The builder is stateful: an instance of the builder should be used to only |
| 41 | /// construct a single CFG. |
| 42 | /// |
| 43 | /// Example usage: |
| 44 | /// |
| 45 | /// CFGBuilder builder; |
| 46 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 47 | /// |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 48 | /// CFG construction is done via a recursive walk of an AST. We actually parse |
| 49 | /// the AST in reverse order so that the successor of a basic block is |
| 50 | /// constructed prior to its predecessor. This allows us to nicely capture |
| 51 | /// implicit fall-throughs without extra basic blocks. |
Ted Kremenek | 1b8ac85 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 52 | /// |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 53 | class VISIBILITY_HIDDEN CFGBuilder { |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 54 | ASTContext *Context; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 55 | CFG* cfg; |
| 56 | CFGBlock* Block; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 57 | CFGBlock* Succ; |
Ted Kremenek | 1aebee0 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 58 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | e1810de | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 59 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | 879d8e1 | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 60 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 61 | CFGBlock* DefaultCaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 63 | // LabelMap records the mapping from Label expressions to their blocks. |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 64 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 65 | LabelMapTy LabelMap; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 66 | |
| 67 | // A list of blocks that end with a "goto" that must be backpatched to their |
| 68 | // resolved targets upon completion of CFG construction. |
Ted Kremenek | de979ae | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 69 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | 8a63218 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 70 | BackpatchBlocksTy BackpatchBlocks; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 72 | // A list of labels whose address has been taken (for indirect gotos). |
| 73 | typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; |
| 74 | LabelSetTy AddressTakenLabels; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 75 | |
| 76 | public: |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 77 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), |
Ted Kremenek | e1810de | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 78 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 79 | SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) { |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 80 | // Create an empty CFG. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 81 | cfg = new CFG(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 82 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 84 | ~CFGBuilder() { delete cfg; } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 86 | // buildCFG - Used by external clients to construct the CFG. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 87 | CFG* buildCFG(Stmt *Statement, ASTContext *C); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 89 | private: |
| 90 | // Visitors to walk an AST and construct the CFG. |
| 91 | CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd); |
| 92 | CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 93 | CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd); |
| 94 | CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 95 | CFGBlock *VisitBreakStmt(BreakStmt *B); |
| 96 | CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd); |
| 97 | CFGBlock *VisitCaseStmt(CaseStmt *C); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 98 | CFGBlock *VisitChooseExpr(ChooseExpr *C); |
| 99 | CFGBlock *VisitCompoundStmt(CompoundStmt *C); |
| 100 | CFGBlock *VisitConditionalOperator(ConditionalOperator *C); |
| 101 | CFGBlock *VisitContinueStmt(ContinueStmt *C); |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 102 | CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 103 | CFGBlock *VisitDeclStmt(DeclStmt *DS); |
| 104 | CFGBlock *VisitDeclSubExpr(Decl* D); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 105 | CFGBlock *VisitDefaultStmt(DefaultStmt *D); |
| 106 | CFGBlock *VisitDoStmt(DoStmt *D); |
| 107 | CFGBlock *VisitForStmt(ForStmt *F); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 108 | CFGBlock *VisitGotoStmt(GotoStmt* G); |
| 109 | CFGBlock *VisitIfStmt(IfStmt *I); |
| 110 | CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I); |
| 111 | CFGBlock *VisitLabelStmt(LabelStmt *L); |
| 112 | CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S); |
| 113 | CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S); |
| 114 | CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S); |
| 115 | CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S); |
| 116 | CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S); |
| 117 | CFGBlock *VisitReturnStmt(ReturnStmt* R); |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 118 | CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd); |
| 119 | CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 120 | CFGBlock *VisitSwitchStmt(SwitchStmt *S); |
| 121 | CFGBlock *VisitWhileStmt(WhileStmt *W); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 123 | CFGBlock *Visit(Stmt *S, bool alwaysAdd = false); |
| 124 | CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd); |
| 125 | CFGBlock *VisitChildren(Stmt* S); |
Mike Stump | 48871a2 | 2009-07-17 01:04:31 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 127 | // NYS == Not Yet Supported |
| 128 | CFGBlock* NYS() { |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 129 | badCFG = true; |
| 130 | return Block; |
| 131 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 133 | void autoCreateBlock() { if (!Block) Block = createBlock(); } |
| 134 | CFGBlock *createBlock(bool add_successor = true); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 135 | bool FinishBlock(CFGBlock* B); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 136 | CFGBlock *addStmt(Stmt *S) { return Visit(S, true); } |
| 137 | |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 138 | bool badCFG; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 139 | }; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 141 | // FIXME: Add support for dependent-sized array types in C++? |
| 142 | // Does it even make sense to build a CFG for an uninstantiated template? |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 143 | static VariableArrayType* FindVA(Type* t) { |
| 144 | while (ArrayType* vt = dyn_cast<ArrayType>(t)) { |
| 145 | if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) |
| 146 | if (vat->getSizeExpr()) |
| 147 | return vat; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 148 | |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 149 | t = vt->getElementType().getTypePtr(); |
| 150 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | d86d39c | 2008-09-26 22:58:57 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 154 | |
| 155 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an |
| 156 | /// arbitrary statement. Examples include a single expression or a function |
| 157 | /// body (compound statement). The ownership of the returned CFG is |
| 158 | /// transferred to the caller. If CFG construction fails, this method returns |
| 159 | /// NULL. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 160 | CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) { |
| 161 | Context = C; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 162 | assert(cfg); |
| 163 | if (!Statement) |
| 164 | return NULL; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 166 | badCFG = false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 167 | |
| 168 | // Create an empty block that will serve as the exit block for the CFG. Since |
| 169 | // this is the first block added to the CFG, it will be implicitly registered |
| 170 | // as the exit block. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 171 | Succ = createBlock(); |
| 172 | assert (Succ == &cfg->getExit()); |
| 173 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 175 | // Visit the statements and create the CFG. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 176 | CFGBlock* B = addStmt(Statement); |
Ted Kremenek | 40d8763 | 2008-02-27 17:33:02 +0000 | [diff] [blame] | 177 | if (!B) B = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 40d8763 | 2008-02-27 17:33:02 +0000 | [diff] [blame] | 179 | if (B) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 180 | // Finalize the last constructed block. This usually involves reversing the |
| 181 | // order of the statements in the block. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 182 | if (Block) FinishBlock(B); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 183 | |
| 184 | // Backpatch the gotos whose label -> block mappings we didn't know when we |
| 185 | // encountered them. |
| 186 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 187 | E = BackpatchBlocks.end(); I != E; ++I ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 189 | CFGBlock* B = *I; |
| 190 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 191 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 192 | |
| 193 | // If there is no target for the goto, then we are looking at an |
| 194 | // incomplete AST. Handle this by not registering a successor. |
| 195 | if (LI == LabelMap.end()) continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 196 | |
| 197 | B->addSuccessor(LI->second); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 198 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 200 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
| 201 | if (CFGBlock* B = cfg->getIndirectGotoBlock()) |
| 202 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 203 | E = AddressTakenLabels.end(); I != E; ++I ) { |
| 204 | |
| 205 | // Lookup the target block. |
| 206 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 207 | |
| 208 | // If there is no target block that contains label, then we are looking |
| 209 | // at an incomplete AST. Handle this by not registering a successor. |
| 210 | if (LI == LabelMap.end()) continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 211 | |
| 212 | B->addSuccessor(LI->second); |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 213 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | cdc0bc4 | 2007-09-17 16:18:02 +0000 | [diff] [blame] | 215 | Succ = B; |
Ted Kremenek | 5c50fd1 | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 216 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 217 | |
| 218 | // Create an empty entry block that has no predecessors. |
Ted Kremenek | 5c50fd1 | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 219 | cfg->setEntry(createBlock()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | b64d183 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 221 | if (badCFG) { |
| 222 | delete cfg; |
| 223 | cfg = NULL; |
| 224 | return NULL; |
| 225 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 226 | |
| 227 | // NULL out cfg so that repeated calls to the builder will fail and that the |
| 228 | // ownership of the constructed CFG is passed to the caller. |
Ted Kremenek | 5c50fd1 | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 229 | CFG* t = cfg; |
| 230 | cfg = NULL; |
| 231 | return t; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 232 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 234 | /// createBlock - Used to lazily create blocks that are connected |
| 235 | /// to the current (global) succcessor. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 236 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
Ted Kremenek | 813dd67 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 237 | CFGBlock* B = cfg->createBlock(); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 238 | if (add_successor && Succ) |
| 239 | B->addSuccessor(Succ); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 240 | return B; |
| 241 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 242 | |
| 243 | /// FinishBlock - When the last statement has been added to the block, we must |
| 244 | /// reverse the statements because they have been inserted in reverse order. |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 245 | bool CFGBuilder::FinishBlock(CFGBlock* B) { |
| 246 | if (badCFG) |
| 247 | return false; |
| 248 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 249 | assert(B); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 250 | B->reverseStmts(); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 251 | return true; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 254 | /// Visit - Walk the subtree of a statement and add extra |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 255 | /// blocks for ternary operators, &&, and ||. We also process "," and |
| 256 | /// DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 257 | CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) { |
| 258 | tryAgain: |
| 259 | switch (S->getStmtClass()) { |
| 260 | default: |
| 261 | return VisitStmt(S, alwaysAdd); |
| 262 | |
| 263 | case Stmt::AddrLabelExprClass: |
| 264 | return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd); |
| 265 | |
| 266 | case Stmt::BinaryOperatorClass: |
| 267 | return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd); |
| 268 | |
| 269 | case Stmt::BlockExprClass: |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 270 | return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 271 | |
| 272 | case Stmt::BlockDeclRefExprClass: |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 273 | return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 274 | |
| 275 | case Stmt::BreakStmtClass: |
| 276 | return VisitBreakStmt(cast<BreakStmt>(S)); |
| 277 | |
| 278 | case Stmt::CallExprClass: |
| 279 | return VisitCallExpr(cast<CallExpr>(S), alwaysAdd); |
| 280 | |
| 281 | case Stmt::CaseStmtClass: |
| 282 | return VisitCaseStmt(cast<CaseStmt>(S)); |
| 283 | |
| 284 | case Stmt::ChooseExprClass: |
| 285 | return VisitChooseExpr(cast<ChooseExpr>(S)); |
| 286 | |
| 287 | case Stmt::CompoundStmtClass: |
| 288 | return VisitCompoundStmt(cast<CompoundStmt>(S)); |
| 289 | |
| 290 | case Stmt::ConditionalOperatorClass: |
| 291 | return VisitConditionalOperator(cast<ConditionalOperator>(S)); |
| 292 | |
| 293 | case Stmt::ContinueStmtClass: |
| 294 | return VisitContinueStmt(cast<ContinueStmt>(S)); |
| 295 | |
| 296 | case Stmt::DeclStmtClass: |
| 297 | return VisitDeclStmt(cast<DeclStmt>(S)); |
| 298 | |
| 299 | case Stmt::DefaultStmtClass: |
| 300 | return VisitDefaultStmt(cast<DefaultStmt>(S)); |
| 301 | |
| 302 | case Stmt::DoStmtClass: |
| 303 | return VisitDoStmt(cast<DoStmt>(S)); |
| 304 | |
| 305 | case Stmt::ForStmtClass: |
| 306 | return VisitForStmt(cast<ForStmt>(S)); |
| 307 | |
| 308 | case Stmt::GotoStmtClass: |
| 309 | return VisitGotoStmt(cast<GotoStmt>(S)); |
| 310 | |
| 311 | case Stmt::IfStmtClass: |
| 312 | return VisitIfStmt(cast<IfStmt>(S)); |
| 313 | |
| 314 | case Stmt::IndirectGotoStmtClass: |
| 315 | return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S)); |
| 316 | |
| 317 | case Stmt::LabelStmtClass: |
| 318 | return VisitLabelStmt(cast<LabelStmt>(S)); |
| 319 | |
| 320 | case Stmt::ObjCAtCatchStmtClass: |
| 321 | return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S)); |
| 322 | |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 323 | case Stmt::CXXThrowExprClass: |
| 324 | return VisitCXXThrowExpr(cast<CXXThrowExpr>(S)); |
| 325 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 326 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 327 | return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S)); |
| 328 | |
| 329 | case Stmt::ObjCAtThrowStmtClass: |
| 330 | return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S)); |
| 331 | |
| 332 | case Stmt::ObjCAtTryStmtClass: |
| 333 | return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S)); |
| 334 | |
| 335 | case Stmt::ObjCForCollectionStmtClass: |
| 336 | return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S)); |
| 337 | |
| 338 | case Stmt::ParenExprClass: |
| 339 | S = cast<ParenExpr>(S)->getSubExpr(); |
| 340 | goto tryAgain; |
| 341 | |
| 342 | case Stmt::NullStmtClass: |
| 343 | return Block; |
| 344 | |
| 345 | case Stmt::ReturnStmtClass: |
| 346 | return VisitReturnStmt(cast<ReturnStmt>(S)); |
| 347 | |
| 348 | case Stmt::SizeOfAlignOfExprClass: |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 349 | return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 350 | |
| 351 | case Stmt::StmtExprClass: |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 352 | return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 353 | |
| 354 | case Stmt::SwitchStmtClass: |
| 355 | return VisitSwitchStmt(cast<SwitchStmt>(S)); |
| 356 | |
| 357 | case Stmt::WhileStmtClass: |
| 358 | return VisitWhileStmt(cast<WhileStmt>(S)); |
| 359 | } |
| 360 | } |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 361 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 362 | CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) { |
| 363 | if (alwaysAdd) { |
| 364 | autoCreateBlock(); |
| 365 | Block->appendStmt(S); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 366 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 367 | |
| 368 | return VisitChildren(S); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 369 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 370 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 371 | /// VisitChildren - Visit the children of a Stmt. |
| 372 | CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) { |
| 373 | CFGBlock *B = Block; |
Mike Stump | d8ba7a2 | 2009-02-26 08:00:25 +0000 | [diff] [blame] | 374 | for (Stmt::child_iterator I = Terminator->child_begin(), |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 375 | E = Terminator->child_end(); I != E; ++I) { |
| 376 | if (*I) B = Visit(*I); |
| 377 | } |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 378 | return B; |
| 379 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 380 | |
| 381 | CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) { |
| 382 | AddressTakenLabels.insert(A->getLabel()); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 384 | if (alwaysAdd) { |
| 385 | autoCreateBlock(); |
| 386 | Block->appendStmt(A); |
| 387 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 389 | return Block; |
| 390 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 391 | |
| 392 | CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) { |
| 393 | if (B->isLogicalOp()) { // && or || |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 394 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 395 | // See if this is a known constant. |
| 396 | bool KnownTrue = false; |
| 397 | bool KnownFalse = false; |
| 398 | Expr::EvalResult Result; |
| 399 | if (B->getLHS()->Evaluate(Result, *Context) |
| 400 | && Result.Val.isInt()) { |
| 401 | if ((B->getOpcode() == BinaryOperator::LAnd) |
| 402 | == Result.Val.getInt().getBoolValue()) |
| 403 | KnownTrue = true; |
| 404 | else |
| 405 | KnownFalse = true; |
| 406 | } |
| 407 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 408 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
| 409 | ConfluenceBlock->appendStmt(B); |
| 410 | |
| 411 | if (!FinishBlock(ConfluenceBlock)) |
| 412 | return 0; |
| 413 | |
| 414 | // create the block evaluating the LHS |
| 415 | CFGBlock* LHSBlock = createBlock(false); |
| 416 | LHSBlock->setTerminator(B); |
| 417 | |
| 418 | // create the block evaluating the RHS |
| 419 | Succ = ConfluenceBlock; |
| 420 | Block = NULL; |
| 421 | CFGBlock* RHSBlock = addStmt(B->getRHS()); |
| 422 | if (!FinishBlock(RHSBlock)) |
| 423 | return 0; |
| 424 | |
| 425 | // Now link the LHSBlock with RHSBlock. |
| 426 | if (B->getOpcode() == BinaryOperator::LOr) { |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 427 | if (KnownTrue) |
| 428 | LHSBlock->addSuccessor(0); |
| 429 | else |
| 430 | LHSBlock->addSuccessor(ConfluenceBlock); |
| 431 | if (KnownFalse) |
| 432 | LHSBlock->addSuccessor(0); |
| 433 | else |
| 434 | LHSBlock->addSuccessor(RHSBlock); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 435 | } else { |
| 436 | assert (B->getOpcode() == BinaryOperator::LAnd); |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 437 | if (KnownFalse) |
| 438 | LHSBlock->addSuccessor(0); |
| 439 | else |
| 440 | LHSBlock->addSuccessor(RHSBlock); |
| 441 | if (KnownTrue) |
| 442 | LHSBlock->addSuccessor(0); |
| 443 | else |
| 444 | LHSBlock->addSuccessor(ConfluenceBlock); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Generate the blocks for evaluating the LHS. |
| 448 | Block = LHSBlock; |
| 449 | return addStmt(B->getLHS()); |
| 450 | } |
| 451 | else if (B->getOpcode() == BinaryOperator::Comma) { // , |
Ted Kremenek | fe9b768 | 2009-07-17 22:57:50 +0000 | [diff] [blame] | 452 | autoCreateBlock(); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 453 | Block->appendStmt(B); |
| 454 | addStmt(B->getRHS()); |
| 455 | return addStmt(B->getLHS()); |
| 456 | } |
| 457 | |
| 458 | return VisitStmt(B, alwaysAdd); |
| 459 | } |
| 460 | |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 461 | CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 462 | // FIXME |
| 463 | return NYS(); |
| 464 | } |
| 465 | |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 466 | CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E, |
| 467 | bool alwaysAdd) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 468 | // FIXME |
| 469 | return NYS(); |
| 470 | } |
| 471 | |
| 472 | CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) { |
| 473 | // "break" is a control-flow statement. Thus we stop processing the current |
| 474 | // block. |
| 475 | if (Block && !FinishBlock(Block)) |
| 476 | return 0; |
| 477 | |
| 478 | // Now create a new block that ends with the break statement. |
| 479 | Block = createBlock(false); |
| 480 | Block->setTerminator(B); |
| 481 | |
| 482 | // If there is no target for the break, then we are looking at an incomplete |
| 483 | // AST. This means that the CFG cannot be constructed. |
| 484 | if (BreakTargetBlock) |
| 485 | Block->addSuccessor(BreakTargetBlock); |
| 486 | else |
| 487 | badCFG = true; |
| 488 | |
| 489 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 490 | return Block; |
| 491 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 492 | |
| 493 | CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) { |
| 494 | // If this is a call to a no-return function, this stops the block here. |
| 495 | if (FunctionDecl *FD = C->getDirectCallee()) { |
| 496 | |
| 497 | if (!FD->hasAttr<NoReturnAttr>()) |
| 498 | return VisitStmt(C, alwaysAdd); |
| 499 | |
| 500 | if (Block && !FinishBlock(Block)) |
| 501 | return 0; |
| 502 | |
| 503 | // Create new block with no successor for the remaining pieces. |
| 504 | Block = createBlock(false); |
| 505 | Block->appendStmt(C); |
| 506 | |
| 507 | // Wire this to the exit block directly. |
| 508 | Block->addSuccessor(&cfg->getExit()); |
| 509 | |
| 510 | return VisitChildren(C); |
| 511 | } |
| 512 | |
| 513 | return VisitStmt(C, alwaysAdd); |
| 514 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 516 | CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) { |
Mike Stump | 3557ea8 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 517 | // See if this is a known constant. |
| 518 | bool KnownTrue = false; |
| 519 | bool KnownFalse = false; |
| 520 | Expr::EvalResult Result; |
| 521 | if (C->getCond()->Evaluate(Result, *Context) |
| 522 | && Result.Val.isInt()) { |
| 523 | if (Result.Val.getInt().getBoolValue()) |
| 524 | KnownTrue = true; |
| 525 | else |
| 526 | KnownFalse = true; |
| 527 | } |
| 528 | |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 529 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
| 530 | ConfluenceBlock->appendStmt(C); |
| 531 | if (!FinishBlock(ConfluenceBlock)) |
| 532 | return 0; |
| 533 | |
| 534 | Succ = ConfluenceBlock; |
| 535 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 536 | CFGBlock* LHSBlock = addStmt(C->getLHS()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 537 | if (!FinishBlock(LHSBlock)) |
| 538 | return 0; |
| 539 | |
| 540 | Succ = ConfluenceBlock; |
| 541 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 542 | CFGBlock* RHSBlock = addStmt(C->getRHS()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 543 | if (!FinishBlock(RHSBlock)) |
| 544 | return 0; |
| 545 | |
| 546 | Block = createBlock(false); |
Mike Stump | 3557ea8 | 2009-07-21 01:46:17 +0000 | [diff] [blame] | 547 | if (KnownFalse) |
| 548 | Block->addSuccessor(0); |
| 549 | else |
| 550 | Block->addSuccessor(LHSBlock); |
| 551 | if (KnownTrue) |
| 552 | Block->addSuccessor(0); |
| 553 | else |
| 554 | Block->addSuccessor(RHSBlock); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 555 | Block->setTerminator(C); |
| 556 | return addStmt(C->getCond()); |
| 557 | } |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 558 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 559 | |
| 560 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
| 561 | CFGBlock* LastBlock = Block; |
| 562 | |
| 563 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 564 | I != E; ++I ) { |
| 565 | LastBlock = addStmt(*I); |
| 566 | } |
| 567 | return LastBlock; |
| 568 | } |
| 569 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 570 | CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) { |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 571 | // See if this is a known constant. |
| 572 | bool KnownTrue = false; |
| 573 | bool KnownFalse = false; |
| 574 | Expr::EvalResult Result; |
| 575 | if (C->getCond()->Evaluate(Result, *Context) |
| 576 | && Result.Val.isInt()) { |
| 577 | if (Result.Val.getInt().getBoolValue()) |
| 578 | KnownTrue = true; |
| 579 | else |
| 580 | KnownFalse = true; |
| 581 | } |
| 582 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 583 | // Create the confluence block that will "merge" the results of the ternary |
| 584 | // expression. |
| 585 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
| 586 | ConfluenceBlock->appendStmt(C); |
| 587 | if (!FinishBlock(ConfluenceBlock)) |
| 588 | return 0; |
| 589 | |
| 590 | // Create a block for the LHS expression if there is an LHS expression. A |
| 591 | // GCC extension allows LHS to be NULL, causing the condition to be the |
| 592 | // value that is returned instead. |
| 593 | // e.g: x ?: y is shorthand for: x ? x : y; |
| 594 | Succ = ConfluenceBlock; |
| 595 | Block = NULL; |
| 596 | CFGBlock* LHSBlock = NULL; |
| 597 | if (C->getLHS()) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 598 | LHSBlock = addStmt(C->getLHS()); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 599 | if (!FinishBlock(LHSBlock)) |
| 600 | return 0; |
| 601 | Block = NULL; |
| 602 | } |
| 603 | |
| 604 | // Create the block for the RHS expression. |
| 605 | Succ = ConfluenceBlock; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 606 | CFGBlock* RHSBlock = addStmt(C->getRHS()); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 607 | if (!FinishBlock(RHSBlock)) |
| 608 | return 0; |
| 609 | |
| 610 | // Create the block that will contain the condition. |
| 611 | Block = createBlock(false); |
| 612 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 613 | if (LHSBlock) { |
| 614 | if (KnownFalse) |
| 615 | Block->addSuccessor(0); |
| 616 | else |
| 617 | Block->addSuccessor(LHSBlock); |
| 618 | } else { |
| 619 | if (KnownFalse) { |
| 620 | // If we know the condition is false, add NULL as the successor for |
| 621 | // the block containing the condition. In this case, the confluence |
| 622 | // block will have just one predecessor. |
| 623 | Block->addSuccessor(0); |
| 624 | assert (ConfluenceBlock->pred_size() == 1); |
| 625 | } else { |
| 626 | // If we have no LHS expression, add the ConfluenceBlock as a direct |
| 627 | // successor for the block containing the condition. Moreover, we need to |
| 628 | // reverse the order of the predecessors in the ConfluenceBlock because |
| 629 | // the RHSBlock will have been added to the succcessors already, and we |
| 630 | // want the first predecessor to the the block containing the expression |
| 631 | // for the case when the ternary expression evaluates to true. |
| 632 | Block->addSuccessor(ConfluenceBlock); |
| 633 | assert (ConfluenceBlock->pred_size() == 2); |
| 634 | std::reverse(ConfluenceBlock->pred_begin(), |
| 635 | ConfluenceBlock->pred_end()); |
| 636 | } |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 639 | if (KnownTrue) |
| 640 | Block->addSuccessor(0); |
| 641 | else |
| 642 | Block->addSuccessor(RHSBlock); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 643 | |
| 644 | Block->setTerminator(C); |
| 645 | return addStmt(C->getCond()); |
| 646 | } |
| 647 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 648 | CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { |
| 649 | autoCreateBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 650 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 651 | if (DS->isSingleDecl()) { |
| 652 | Block->appendStmt(DS); |
| 653 | return VisitDeclSubExpr(DS->getSingleDecl()); |
Ted Kremenek | f699882 | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 654 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 655 | |
| 656 | CFGBlock *B = 0; |
| 657 | |
| 658 | // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy. |
| 659 | typedef llvm::SmallVector<Decl*,10> BufTy; |
| 660 | BufTy Buf(DS->decl_begin(), DS->decl_end()); |
| 661 | |
| 662 | for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) { |
| 663 | // Get the alignment of the new DeclStmt, padding out to >=8 bytes. |
| 664 | unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8 |
| 665 | ? 8 : llvm::AlignOf<DeclStmt>::Alignment; |
| 666 | |
| 667 | // Allocate the DeclStmt using the BumpPtrAllocator. It will get |
| 668 | // automatically freed with the CFG. |
| 669 | DeclGroupRef DG(*I); |
| 670 | Decl *D = *I; |
| 671 | void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); |
| 672 | DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); |
| 673 | |
| 674 | // Append the fake DeclStmt to block. |
| 675 | Block->appendStmt(DSNew); |
| 676 | B = VisitDeclSubExpr(D); |
| 677 | } |
| 678 | |
| 679 | return B; |
| 680 | } |
| 681 | |
| 682 | /// VisitDeclSubExpr - Utility method to add block-level expressions for |
| 683 | /// initializers in Decls. |
| 684 | CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) { |
| 685 | assert(Block); |
Ted Kremenek | f699882 | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 686 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 687 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 688 | |
| 689 | if (!VD) |
| 690 | return Block; |
| 691 | |
| 692 | Expr *Init = VD->getInit(); |
| 693 | |
| 694 | if (Init) { |
| 695 | // Optimization: Don't create separate block-level statements for literals. |
| 696 | switch (Init->getStmtClass()) { |
| 697 | case Stmt::IntegerLiteralClass: |
| 698 | case Stmt::CharacterLiteralClass: |
| 699 | case Stmt::StringLiteralClass: |
| 700 | break; |
| 701 | default: |
| 702 | Block = addStmt(Init); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | // If the type of VD is a VLA, then we must process its size expressions. |
| 707 | for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; |
| 708 | VA = FindVA(VA->getElementType().getTypePtr())) |
| 709 | Block = addStmt(VA->getSizeExpr()); |
| 710 | |
| 711 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
Mike Stump | 23a443b | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 715 | // See if this is a known constant. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 716 | bool KnownTrue = false; |
| 717 | bool KnownFalse = false; |
| 718 | Expr::EvalResult Result; |
| 719 | if (I->getCond()->Evaluate(Result, *Context) |
| 720 | && Result.Val.isInt()) { |
| 721 | if (Result.Val.getInt().getBoolValue()) |
| 722 | KnownTrue = true; |
| 723 | else |
| 724 | KnownFalse = true; |
| 725 | } |
| 726 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 727 | // We may see an if statement in the middle of a basic block, or it may be the |
| 728 | // first statement we are processing. In either case, we create a new basic |
| 729 | // block. First, we create the blocks for the then...else statements, and |
| 730 | // then we create the block containing the if statement. If we were in the |
| 731 | // middle of a block, we stop processing that block and reverse its |
| 732 | // statements. That block is then the implicit successor for the "then" and |
| 733 | // "else" clauses. |
| 734 | |
| 735 | // The block we were proccessing is now finished. Make it the successor |
| 736 | // block. |
| 737 | if (Block) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 738 | Succ = Block; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 739 | if (!FinishBlock(Block)) |
| 740 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 741 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 742 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 743 | // Process the false branch. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 744 | CFGBlock* ElseBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 745 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 746 | if (Stmt* Else = I->getElse()) { |
| 747 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 748 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 749 | // NULL out Block so that the recursive call to Visit will |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 750 | // create a new basic block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 751 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 752 | ElseBlock = addStmt(Else); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 753 | |
Ted Kremenek | bbad8ce | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 754 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 755 | ElseBlock = sv.get(); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 756 | else if (Block) { |
| 757 | if (!FinishBlock(ElseBlock)) |
| 758 | return 0; |
| 759 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 760 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 761 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 762 | // Process the true branch. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 763 | CFGBlock* ThenBlock; |
| 764 | { |
| 765 | Stmt* Then = I->getThen(); |
| 766 | assert (Then); |
| 767 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 768 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 769 | ThenBlock = addStmt(Then); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 770 | |
Ted Kremenek | 1b37951 | 2009-04-01 03:52:47 +0000 | [diff] [blame] | 771 | if (!ThenBlock) { |
| 772 | // We can reach here if the "then" body has all NullStmts. |
| 773 | // Create an empty block so we can distinguish between true and false |
| 774 | // branches in path-sensitive analyses. |
| 775 | ThenBlock = createBlock(false); |
| 776 | ThenBlock->addSuccessor(sv.get()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 777 | } else if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 778 | if (!FinishBlock(ThenBlock)) |
| 779 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 780 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 783 | // Now create a new block containing the if statement. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 784 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 785 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 786 | // Set the terminator of the new block to the If statement. |
| 787 | Block->setTerminator(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 788 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 789 | // Now add the successors. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 790 | if (KnownFalse) |
| 791 | Block->addSuccessor(0); |
| 792 | else |
| 793 | Block->addSuccessor(ThenBlock); |
| 794 | if (KnownTrue) |
| 795 | Block->addSuccessor(0); |
| 796 | else |
| 797 | Block->addSuccessor(ElseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 798 | |
| 799 | // Add the condition as the last statement in the new block. This may create |
| 800 | // new blocks as the condition may contain control-flow. Any newly created |
| 801 | // blocks will be pointed to be "Block". |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 802 | return addStmt(I->getCond()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 803 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 804 | |
| 805 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 806 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 807 | // If we were in the middle of a block we stop processing that block and |
| 808 | // reverse its statements. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 809 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 810 | // NOTE: If a "return" appears in the middle of a block, this means that the |
| 811 | // code afterwards is DEAD (unreachable). We still keep a basic block |
| 812 | // for that code; a simple "mark-and-sweep" from the entry block will be |
| 813 | // able to report such dead blocks. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 814 | if (Block) FinishBlock(Block); |
| 815 | |
| 816 | // Create the new block. |
| 817 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 818 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 819 | // The Exit block is the only successor. |
| 820 | Block->addSuccessor(&cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 821 | |
| 822 | // Add the return statement to the block. This may create new blocks if R |
| 823 | // contains control-flow (short-circuit operations). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 824 | return VisitStmt(R, true); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 828 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 829 | addStmt(L->getSubStmt()); |
Ted Kremenek | cab47bd | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 830 | CFGBlock* LabelBlock = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 831 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 832 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 833 | LabelBlock = createBlock(); // scopes that only contains NullStmts. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 834 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 835 | assert(LabelMap.find(L) == LabelMap.end() && "label already in map"); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 836 | LabelMap[ L ] = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 837 | |
| 838 | // Labels partition blocks, so this is the end of the basic block we were |
| 839 | // processing (L is the block's label). Because this is label (and we have |
| 840 | // already processed the substatement) there is no extra control-flow to worry |
| 841 | // about. |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 842 | LabelBlock->setLabel(L); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 843 | if (!FinishBlock(LabelBlock)) |
| 844 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 845 | |
| 846 | // We set Block to NULL to allow lazy creation of a new block (if necessary); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 847 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 848 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 849 | // This block is now the implicit successor of other blocks. |
| 850 | Succ = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 851 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 852 | return LabelBlock; |
| 853 | } |
| 854 | |
| 855 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 856 | // Goto is a control-flow statement. Thus we stop processing the current |
| 857 | // block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 858 | if (Block) |
| 859 | FinishBlock(Block); |
| 860 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 861 | Block = createBlock(false); |
| 862 | Block->setTerminator(G); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 863 | |
| 864 | // If we already know the mapping to the label block add the successor now. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 865 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 866 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 867 | if (I == LabelMap.end()) |
| 868 | // We will need to backpatch this block later. |
| 869 | BackpatchBlocks.push_back(Block); |
| 870 | else |
| 871 | Block->addSuccessor(I->second); |
| 872 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 873 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 877 | // See if this is a known constant. |
| 878 | bool KnownTrue = false; |
| 879 | bool KnownFalse = false; |
| 880 | Expr::EvalResult Result; |
| 881 | if (F->getCond() && F->getCond()->Evaluate(Result, *Context) |
| 882 | && Result.Val.isInt()) { |
| 883 | if (Result.Val.getInt().getBoolValue()) |
| 884 | KnownTrue = true; |
| 885 | else |
| 886 | KnownFalse = true; |
| 887 | } |
| 888 | if (F->getCond() == 0) |
| 889 | KnownTrue = true; |
| 890 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 891 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 892 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 893 | // "for" is a control-flow statement. Thus we stop processing the current |
| 894 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 895 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 896 | if (!FinishBlock(Block)) |
| 897 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 898 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 899 | } else |
| 900 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 901 | |
| 902 | // Because of short-circuit evaluation, the condition of the loop can span |
| 903 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 904 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 905 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 906 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 907 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 908 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 909 | ExitConditionBlock->setTerminator(F); |
| 910 | |
| 911 | // Now add the actual condition to the condition block. Because the condition |
| 912 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 913 | if (Stmt* C = F->getCond()) { |
| 914 | Block = ExitConditionBlock; |
| 915 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 916 | if (Block) { |
| 917 | if (!FinishBlock(EntryConditionBlock)) |
| 918 | return 0; |
| 919 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 920 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 921 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 922 | // The condition block is the implicit successor for the loop body as well as |
| 923 | // any code above the loop. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 924 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 925 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 926 | // Now create the loop body. |
| 927 | { |
| 928 | assert (F->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 929 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 930 | // Save the current values for Block, Succ, and continue and break targets |
| 931 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 932 | save_continue(ContinueTargetBlock), |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 933 | save_break(BreakTargetBlock); |
| 934 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 935 | // Create a new block to contain the (bottom) of the loop body. |
| 936 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 937 | |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 938 | if (Stmt* I = F->getInc()) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 939 | // Generate increment code in its own basic block. This is the target of |
| 940 | // continue statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 941 | Succ = addStmt(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 942 | } else { |
| 943 | // No increment code. Create a special, empty, block that is used as the |
| 944 | // target block for "looping back" to the start of the loop. |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 945 | assert(Succ == EntryConditionBlock); |
| 946 | Succ = createBlock(); |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 947 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 949 | // Finish up the increment (or empty) block if it hasn't been already. |
| 950 | if (Block) { |
| 951 | assert(Block == Succ); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 952 | if (!FinishBlock(Block)) |
| 953 | return 0; |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 954 | Block = 0; |
| 955 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 956 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 957 | ContinueTargetBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 958 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 959 | // The starting block for the loop increment is the block that should |
| 960 | // represent the 'loop target' for looping back to the start of the loop. |
| 961 | ContinueTargetBlock->setLoopTarget(F); |
| 962 | |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 963 | // All breaks should go to the code following the loop. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 964 | BreakTargetBlock = LoopSuccessor; |
| 965 | |
| 966 | // Now populate the body block, and in the process create new blocks as we |
| 967 | // walk the body of the loop. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 968 | CFGBlock* BodyBlock = addStmt(F->getBody()); |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 969 | |
| 970 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 971 | BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 972 | else if (Block) { |
| 973 | if (!FinishBlock(BodyBlock)) |
| 974 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 977 | if (KnownFalse) |
| 978 | ExitConditionBlock->addSuccessor(0); |
| 979 | else { |
| 980 | // This new body block is a successor to our "exit" condition block. |
| 981 | ExitConditionBlock->addSuccessor(BodyBlock); |
| 982 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 983 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 984 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 985 | if (KnownTrue) |
| 986 | ExitConditionBlock->addSuccessor(0); |
| 987 | else { |
| 988 | // Link up the condition block with the code that follows the loop. (the |
| 989 | // false branch). |
| 990 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 991 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 992 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 993 | // If the loop contains initialization, create a new block for those |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 994 | // statements. This block can also contain statements that precede the loop. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 995 | if (Stmt* I = F->getInit()) { |
| 996 | Block = createBlock(); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 997 | return addStmt(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 998 | } else { |
| 999 | // There is no loop initialization. We are thus basically a while loop. |
| 1000 | // NULL out Block to force lazy block construction. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1001 | Block = NULL; |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1002 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1003 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1007 | CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { |
| 1008 | // Objective-C fast enumeration 'for' statements: |
| 1009 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC |
| 1010 | // |
| 1011 | // for ( Type newVariable in collection_expression ) { statements } |
| 1012 | // |
| 1013 | // becomes: |
| 1014 | // |
| 1015 | // prologue: |
| 1016 | // 1. collection_expression |
| 1017 | // T. jump to loop_entry |
| 1018 | // loop_entry: |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1019 | // 1. side-effects of element expression |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1020 | // 1. ObjCForCollectionStmt [performs binding to newVariable] |
| 1021 | // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] |
| 1022 | // TB: |
| 1023 | // statements |
| 1024 | // T. jump to loop_entry |
| 1025 | // FB: |
| 1026 | // what comes after |
| 1027 | // |
| 1028 | // and |
| 1029 | // |
| 1030 | // Type existingItem; |
| 1031 | // for ( existingItem in expression ) { statements } |
| 1032 | // |
| 1033 | // becomes: |
| 1034 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1035 | // the same with newVariable replaced with existingItem; the binding works |
| 1036 | // the same except that for one ObjCForCollectionStmt::getElement() returns |
| 1037 | // a DeclStmt and the other returns a DeclRefExpr. |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1038 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1039 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1040 | CFGBlock* LoopSuccessor = 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1041 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1042 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1043 | if (!FinishBlock(Block)) |
| 1044 | return 0; |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1045 | LoopSuccessor = Block; |
| 1046 | Block = 0; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1047 | } else |
| 1048 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1049 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1050 | // Build the condition blocks. |
| 1051 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1052 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1053 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1054 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1055 | ExitConditionBlock->setTerminator(S); |
| 1056 | |
| 1057 | // The last statement in the block should be the ObjCForCollectionStmt, which |
| 1058 | // performs the actual binding to 'element' and determines if there are any |
| 1059 | // more items in the collection. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1060 | ExitConditionBlock->appendStmt(S); |
| 1061 | Block = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1062 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1063 | // Walk the 'element' expression to see if there are any side-effects. We |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1064 | // generate new blocks as necesary. We DON'T add the statement by default to |
| 1065 | // the CFG unless it contains control-flow. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1066 | EntryConditionBlock = Visit(S->getElement(), false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1067 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1068 | if (!FinishBlock(EntryConditionBlock)) |
| 1069 | return 0; |
| 1070 | Block = 0; |
| 1071 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1072 | |
| 1073 | // The condition block is the implicit successor for the loop body as well as |
| 1074 | // any code above the loop. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1075 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1076 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1077 | // Now create the true branch. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1078 | { |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1079 | // Save the current values for Succ, continue and break targets. |
| 1080 | SaveAndRestore<CFGBlock*> save_Succ(Succ), |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1081 | save_continue(ContinueTargetBlock), save_break(BreakTargetBlock); |
| 1082 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1083 | BreakTargetBlock = LoopSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1084 | ContinueTargetBlock = EntryConditionBlock; |
| 1085 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1086 | CFGBlock* BodyBlock = addStmt(S->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1087 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1088 | if (!BodyBlock) |
| 1089 | BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1090 | else if (Block) { |
| 1091 | if (!FinishBlock(BodyBlock)) |
| 1092 | return 0; |
| 1093 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1094 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1095 | // This new body block is a successor to our "exit" condition block. |
| 1096 | ExitConditionBlock->addSuccessor(BodyBlock); |
| 1097 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1098 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1099 | // Link up the condition block with the code that follows the loop. |
| 1100 | // (the false branch). |
| 1101 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 1102 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1103 | // Now create a prologue block to contain the collection expression. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1104 | Block = createBlock(); |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1105 | return addStmt(S->getCollection()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1108 | CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) { |
| 1109 | // FIXME: Add locking 'primitives' to CFG for @synchronized. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1110 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1111 | // Inline the body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1112 | CFGBlock *SyncBlock = addStmt(S->getSynchBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1113 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1114 | // The sync body starts its own basic block. This makes it a little easier |
| 1115 | // for diagnostic clients. |
| 1116 | if (SyncBlock) { |
| 1117 | if (!FinishBlock(SyncBlock)) |
| 1118 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1119 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1120 | Block = 0; |
| 1121 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1122 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1123 | Succ = SyncBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1124 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1125 | // Inline the sync expression. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1126 | return addStmt(S->getSynchExpr()); |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1127 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1128 | |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1129 | CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1130 | // FIXME |
Ted Kremenek | 89be652 | 2009-04-07 04:26:02 +0000 | [diff] [blame] | 1131 | return NYS(); |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1132 | } |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1133 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1134 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
Mike Stump | 23a443b | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 1135 | // See if this is a known constant. |
| 1136 | bool KnownTrue = false; |
| 1137 | bool KnownFalse = false; |
| 1138 | Expr::EvalResult Result; |
| 1139 | if (W->getCond()->Evaluate(Result, *Context) |
| 1140 | && Result.Val.isInt()) { |
| 1141 | if (Result.Val.getInt().getBoolValue()) |
| 1142 | KnownTrue = true; |
| 1143 | else |
| 1144 | KnownFalse = true; |
| 1145 | } |
| 1146 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1147 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1148 | |
Mike Stump | 014b3ea | 2009-07-21 01:12:51 +0000 | [diff] [blame] | 1149 | // "while" is a control-flow statement. Thus we stop processing the current |
| 1150 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1151 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1152 | if (!FinishBlock(Block)) |
| 1153 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1154 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1155 | } else |
| 1156 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1159 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1160 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1161 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1162 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1163 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1164 | // Set the terminator for the "exit" condition block. |
| 1165 | ExitConditionBlock->setTerminator(W); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1166 | |
| 1167 | // Now add the actual condition to the condition block. Because the condition |
| 1168 | // itself may contain control-flow, new blocks may be created. Thus we update |
| 1169 | // "Succ" after adding the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1170 | if (Stmt* C = W->getCond()) { |
| 1171 | Block = ExitConditionBlock; |
| 1172 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1173 | assert(Block == EntryConditionBlock); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1174 | if (Block) { |
| 1175 | if (!FinishBlock(EntryConditionBlock)) |
| 1176 | return 0; |
| 1177 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1178 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1179 | |
| 1180 | // The condition block is the implicit successor for the loop body as well as |
| 1181 | // any code above the loop. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1182 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1183 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1184 | // Process the loop body. |
| 1185 | { |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1186 | assert(W->getBody()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1187 | |
| 1188 | // Save the current values for Block, Succ, and continue and break targets |
| 1189 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 1190 | save_continue(ContinueTargetBlock), |
| 1191 | save_break(BreakTargetBlock); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1192 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1193 | // Create an empty block to represent the transition block for looping back |
| 1194 | // to the head of the loop. |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1195 | Block = 0; |
| 1196 | assert(Succ == EntryConditionBlock); |
| 1197 | Succ = createBlock(); |
| 1198 | Succ->setLoopTarget(W); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1199 | ContinueTargetBlock = Succ; |
| 1200 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1201 | // All breaks should go to the code following the loop. |
| 1202 | BreakTargetBlock = LoopSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1203 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1204 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1205 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1206 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1207 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1208 | CFGBlock* BodyBlock = addStmt(W->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1209 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1210 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 1211 | BodyBlock = EntryConditionBlock; // can happen for "while(...) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1212 | else if (Block) { |
| 1213 | if (!FinishBlock(BodyBlock)) |
| 1214 | return 0; |
| 1215 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1216 | |
Mike Stump | 23a443b | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 1217 | if (KnownFalse) |
| 1218 | ExitConditionBlock->addSuccessor(0); |
| 1219 | else { |
| 1220 | // Add the loop body entry as a successor to the condition. |
| 1221 | ExitConditionBlock->addSuccessor(BodyBlock); |
| 1222 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1223 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1224 | |
Mike Stump | 23a443b | 2009-07-21 00:38:52 +0000 | [diff] [blame] | 1225 | if (KnownTrue) |
| 1226 | ExitConditionBlock->addSuccessor(0); |
| 1227 | else { |
| 1228 | // Link up the condition block with the code that follows the loop. (the |
| 1229 | // false branch). |
| 1230 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 1231 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1232 | |
| 1233 | // There can be no more statements in the condition block since we loop back |
| 1234 | // to this block. NULL out Block to force lazy creation of another block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1235 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1236 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1237 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1238 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1239 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1240 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1241 | |
| 1242 | |
| 1243 | CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { |
| 1244 | // FIXME: For now we pretend that @catch and the code it contains does not |
| 1245 | // exit. |
| 1246 | return Block; |
| 1247 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1248 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1249 | CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { |
| 1250 | // FIXME: This isn't complete. We basically treat @throw like a return |
| 1251 | // statement. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1252 | |
| 1253 | // If we were in the middle of a block we stop processing that block and |
| 1254 | // reverse its statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1255 | if (Block && !FinishBlock(Block)) |
| 1256 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1257 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1258 | // Create the new block. |
| 1259 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1260 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1261 | // The Exit block is the only successor. |
| 1262 | Block->addSuccessor(&cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1263 | |
| 1264 | // Add the statement to the block. This may create new blocks if S contains |
| 1265 | // control-flow (short-circuit operations). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1266 | return VisitStmt(S, true); |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1267 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1268 | |
Mike Stump | 8dd1b6b | 2009-07-22 22:56:04 +0000 | [diff] [blame] | 1269 | CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) { |
| 1270 | // If we were in the middle of a block we stop processing that block and |
| 1271 | // reverse its statements. |
| 1272 | if (Block && !FinishBlock(Block)) |
| 1273 | return 0; |
| 1274 | |
| 1275 | // Create the new block. |
| 1276 | Block = createBlock(false); |
| 1277 | |
| 1278 | // The Exit block is the only successor. |
| 1279 | Block->addSuccessor(&cfg->getExit()); |
| 1280 | |
| 1281 | // Add the statement to the block. This may create new blocks if S contains |
| 1282 | // control-flow (short-circuit operations). |
| 1283 | return VisitStmt(T, true); |
| 1284 | } |
| 1285 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1286 | CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) { |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 1287 | // See if this is a known constant. |
| 1288 | bool KnownTrue = false; |
| 1289 | bool KnownFalse = false; |
| 1290 | Expr::EvalResult Result; |
| 1291 | if (D->getCond()->Evaluate(Result, *Context) |
| 1292 | && Result.Val.isInt()) { |
| 1293 | if (Result.Val.getInt().getBoolValue()) |
| 1294 | KnownTrue = true; |
| 1295 | else |
| 1296 | KnownFalse = true; |
| 1297 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1298 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1299 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1300 | |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 1301 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 1302 | // current block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1303 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1304 | if (!FinishBlock(Block)) |
| 1305 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1306 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1307 | } else |
| 1308 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1311 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1312 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1313 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1314 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1315 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1316 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1317 | ExitConditionBlock->setTerminator(D); |
| 1318 | |
| 1319 | // Now add the actual condition to the condition block. Because the condition |
| 1320 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1321 | if (Stmt* C = D->getCond()) { |
| 1322 | Block = ExitConditionBlock; |
| 1323 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1324 | if (Block) { |
| 1325 | if (!FinishBlock(EntryConditionBlock)) |
| 1326 | return 0; |
| 1327 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1328 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1329 | |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1330 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1331 | Succ = EntryConditionBlock; |
| 1332 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1333 | // Process the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1334 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1335 | { |
| 1336 | assert (D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1337 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1338 | // Save the current values for Block, Succ, and continue and break targets |
| 1339 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 1340 | save_continue(ContinueTargetBlock), |
| 1341 | save_break(BreakTargetBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1342 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1343 | // All continues within this loop should go to the condition block |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1344 | ContinueTargetBlock = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1345 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1346 | // All breaks should go to the code following the loop. |
| 1347 | BreakTargetBlock = LoopSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1348 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1349 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1350 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1351 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1352 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1353 | BodyBlock = addStmt(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1354 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1355 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 1356 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1357 | else if (Block) { |
| 1358 | if (!FinishBlock(BodyBlock)) |
| 1359 | return 0; |
| 1360 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1361 | |
Ted Kremenek | cb37bb0 | 2009-04-28 04:22:00 +0000 | [diff] [blame] | 1362 | // Add an intermediate block between the BodyBlock and the |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1363 | // ExitConditionBlock to represent the "loop back" transition. Create an |
| 1364 | // empty block to represent the transition block for looping back to the |
| 1365 | // head of the loop. |
Ted Kremenek | cb37bb0 | 2009-04-28 04:22:00 +0000 | [diff] [blame] | 1366 | // FIXME: Can we do this more efficiently without adding another block? |
| 1367 | Block = NULL; |
| 1368 | Succ = BodyBlock; |
| 1369 | CFGBlock *LoopBackBlock = createBlock(); |
| 1370 | LoopBackBlock->setLoopTarget(D); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1371 | |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 1372 | if (KnownFalse) |
| 1373 | ExitConditionBlock->addSuccessor(0); |
| 1374 | else { |
| 1375 | // Add the loop body entry as a successor to the condition. |
| 1376 | ExitConditionBlock->addSuccessor(LoopBackBlock); |
| 1377 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1378 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1379 | |
Mike Stump | 8d50b6a | 2009-07-21 01:27:50 +0000 | [diff] [blame] | 1380 | if (KnownTrue) |
| 1381 | ExitConditionBlock->addSuccessor(0); |
| 1382 | else { |
| 1383 | // Link up the condition block with the code that follows the loop. (the |
| 1384 | // false branch). |
| 1385 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 1386 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1387 | |
| 1388 | // There can be no more statements in the body block(s) since we loop back to |
| 1389 | // the body. NULL out Block to force lazy creation of another block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1390 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1391 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1392 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1393 | Succ = BodyBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1394 | return BodyBlock; |
| 1395 | } |
| 1396 | |
| 1397 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 1398 | // "continue" is a control-flow statement. Thus we stop processing the |
| 1399 | // current block. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1400 | if (Block && !FinishBlock(Block)) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1401 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1403 | // Now create a new block that ends with the continue statement. |
| 1404 | Block = createBlock(false); |
| 1405 | Block->setTerminator(C); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1406 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1407 | // If there is no target for the continue, then we are looking at an |
Ted Kremenek | 882cf06 | 2009-04-07 18:53:24 +0000 | [diff] [blame] | 1408 | // incomplete AST. This means the CFG cannot be constructed. |
| 1409 | if (ContinueTargetBlock) |
| 1410 | Block->addSuccessor(ContinueTargetBlock); |
| 1411 | else |
| 1412 | badCFG = true; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1413 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1414 | return Block; |
| 1415 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1416 | |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1417 | CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, |
| 1418 | bool alwaysAdd) { |
| 1419 | |
| 1420 | if (alwaysAdd) { |
| 1421 | autoCreateBlock(); |
| 1422 | Block->appendStmt(E); |
| 1423 | } |
| 1424 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1425 | // VLA types have expressions that must be evaluated. |
| 1426 | if (E->isArgumentType()) { |
| 1427 | for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr()); |
| 1428 | VA != 0; VA = FindVA(VA->getElementType().getTypePtr())) |
| 1429 | addStmt(VA->getSizeExpr()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1430 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1431 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1432 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1433 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1434 | |
| 1435 | /// VisitStmtExpr - Utility method to handle (nested) statement |
| 1436 | /// expressions (a GCC extension). |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1437 | CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) { |
| 1438 | if (alwaysAdd) { |
| 1439 | autoCreateBlock(); |
| 1440 | Block->appendStmt(SE); |
| 1441 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1442 | return VisitCompoundStmt(SE->getSubStmt()); |
| 1443 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1444 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1445 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1446 | // "switch" is a control-flow statement. Thus we stop processing the current |
| 1447 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1448 | CFGBlock* SwitchSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1449 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1450 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1451 | if (!FinishBlock(Block)) |
| 1452 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1453 | SwitchSuccessor = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1454 | } else SwitchSuccessor = Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Save the current "switch" context. |
| 1457 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1458 | save_break(BreakTargetBlock), |
| 1459 | save_default(DefaultCaseBlock); |
| 1460 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1461 | // Set the "default" case to be the block after the switch statement. If the |
| 1462 | // switch statement contains a "default:", this value will be overwritten with |
| 1463 | // the block for that code. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1464 | DefaultCaseBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1465 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1466 | // Create a new block that will contain the switch statement. |
| 1467 | SwitchTerminatedBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1468 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1469 | // Now process the switch body. The code after the switch is the implicit |
| 1470 | // successor. |
| 1471 | Succ = SwitchSuccessor; |
| 1472 | BreakTargetBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1473 | |
| 1474 | // When visiting the body, the case statements should automatically get linked |
| 1475 | // up to the switch. We also don't keep a pointer to the body, since all |
| 1476 | // control-flow from the switch goes to case/default statements. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1477 | assert (Terminator->getBody() && "switch must contain a non-NULL body"); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1478 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1479 | CFGBlock *BodyBlock = addStmt(Terminator->getBody()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1480 | if (Block) { |
| 1481 | if (!FinishBlock(BodyBlock)) |
| 1482 | return 0; |
| 1483 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1484 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1485 | // If we have no "default:" case, the default transition is to the code |
| 1486 | // following the switch body. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1487 | SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1488 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1489 | // Add the terminator and condition in the switch block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1490 | SwitchTerminatedBlock->setTerminator(Terminator); |
| 1491 | assert (Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1492 | Block = SwitchTerminatedBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1493 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1494 | return addStmt(Terminator->getCond()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1497 | CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1498 | // CaseStmts are essentially labels, so they are the first statement in a |
| 1499 | // block. |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 1500 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1501 | if (CS->getSubStmt()) |
| 1502 | addStmt(CS->getSubStmt()); |
| 1503 | |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 1504 | CFGBlock* CaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1505 | if (!CaseBlock) |
| 1506 | CaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1507 | |
| 1508 | // Cases statements partition blocks, so this is the top of the basic block we |
| 1509 | // were processing (the "case XXX:" is the label). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1510 | CaseBlock->setLabel(CS); |
| 1511 | |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1512 | if (!FinishBlock(CaseBlock)) |
| 1513 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1514 | |
| 1515 | // Add this block to the list of successors for the block with the switch |
| 1516 | // statement. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1517 | assert(SwitchTerminatedBlock); |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1518 | SwitchTerminatedBlock->addSuccessor(CaseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1519 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1520 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1521 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1522 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1523 | // This block is now the implicit successor of other blocks. |
| 1524 | Succ = CaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1525 | |
Ted Kremenek | cab47bd | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 1526 | return CaseBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1527 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1528 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1529 | CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1530 | if (Terminator->getSubStmt()) |
| 1531 | addStmt(Terminator->getSubStmt()); |
| 1532 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1533 | DefaultCaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1534 | |
| 1535 | if (!DefaultCaseBlock) |
| 1536 | DefaultCaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1537 | |
| 1538 | // Default statements partition blocks, so this is the top of the basic block |
| 1539 | // we were processing (the "default:" is the label). |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1540 | DefaultCaseBlock->setLabel(Terminator); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1541 | |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1542 | if (!FinishBlock(DefaultCaseBlock)) |
| 1543 | return 0; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1544 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1545 | // Unlike case statements, we don't add the default block to the successors |
| 1546 | // for the switch statement immediately. This is done when we finish |
| 1547 | // processing the switch statement. This allows for the default case |
| 1548 | // (including a fall-through to the code after the switch statement) to always |
| 1549 | // be the last successor of a switch-terminated block. |
| 1550 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1551 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1552 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1553 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1554 | // This block is now the implicit successor of other blocks. |
| 1555 | Succ = DefaultCaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1556 | |
| 1557 | return DefaultCaseBlock; |
Ted Kremenek | 9682be1 | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 1558 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1559 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1560 | CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1561 | // Lazily create the indirect-goto dispatch block if there isn't one already. |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1562 | CFGBlock* IBlock = cfg->getIndirectGotoBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1563 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1564 | if (!IBlock) { |
| 1565 | IBlock = createBlock(false); |
| 1566 | cfg->setIndirectGotoBlock(IBlock); |
| 1567 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1568 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1569 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 1570 | // current block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1571 | if (Block && !FinishBlock(Block)) |
| 1572 | return 0; |
| 1573 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1574 | Block = createBlock(false); |
| 1575 | Block->setTerminator(I); |
| 1576 | Block->addSuccessor(IBlock); |
| 1577 | return addStmt(I->getTarget()); |
| 1578 | } |
| 1579 | |
Ted Kremenek | 04cca64 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 1580 | } // end anonymous namespace |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1581 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1582 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has |
| 1583 | /// no successors or predecessors. If this is the first block created in the |
| 1584 | /// CFG, it is automatically set to be the Entry and Exit of the CFG. |
Ted Kremenek | 813dd67 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 1585 | CFGBlock* CFG::createBlock() { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1586 | bool first_block = begin() == end(); |
| 1587 | |
| 1588 | // Create the block. |
Ted Kremenek | 813dd67 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 1589 | Blocks.push_front(CFGBlock(NumBlockIDs++)); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1590 | |
| 1591 | // If this is the first block, set it as the Entry and Exit. |
| 1592 | if (first_block) Entry = Exit = &front(); |
| 1593 | |
| 1594 | // Return the block. |
| 1595 | return &front(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1598 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 1599 | /// CFG is returned to the caller. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1600 | CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1601 | CFGBuilder Builder; |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1602 | return Builder.buildCFG(Statement, C); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1606 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 1607 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1608 | //===----------------------------------------------------------------------===// |
| 1609 | // CFG: Queries for BlkExprs. |
| 1610 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1611 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1612 | namespace { |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1613 | typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1616 | static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) { |
| 1617 | if (!Terminator) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1618 | return; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1619 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1620 | for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1621 | if (!*I) continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1622 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1623 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I)) |
| 1624 | if (B->isAssignmentOp()) Set.insert(B); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1625 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1626 | FindSubExprAssignments(*I, Set); |
| 1627 | } |
| 1628 | } |
| 1629 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1630 | static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { |
| 1631 | BlkExprMapTy* M = new BlkExprMapTy(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1632 | |
| 1633 | // Look for assignments that are used as subexpressions. These are the only |
| 1634 | // assignments that we want to *possibly* register as a block-level |
| 1635 | // expression. Basically, if an assignment occurs both in a subexpression and |
| 1636 | // at the block-level, it is a block-level expression. |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1637 | llvm::SmallPtrSet<Expr*,50> SubExprAssignments; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1638 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1639 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) |
| 1640 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1641 | FindSubExprAssignments(*BI, SubExprAssignments); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1642 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1643 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1644 | |
| 1645 | // Iterate over the statements again on identify the Expr* and Stmt* at the |
| 1646 | // block-level that are block-level expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1647 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1648 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1649 | if (Expr* Exp = dyn_cast<Expr>(*BI)) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1650 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1651 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1652 | // Assignment expressions that are not nested within another |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1653 | // expression are really "statements" whose value is never used by |
| 1654 | // another expression. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1655 | if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1656 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1657 | } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { |
| 1658 | // Special handling for statement expressions. The last statement in |
| 1659 | // the statement expression is also a block-level expr. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1660 | const CompoundStmt* C = Terminator->getSubStmt(); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1661 | if (!C->body_empty()) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1662 | unsigned x = M->size(); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1663 | (*M)[C->body_back()] = x; |
| 1664 | } |
| 1665 | } |
Ted Kremenek | 0cb1ba2 | 2008-01-25 23:22:27 +0000 | [diff] [blame] | 1666 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1667 | unsigned x = M->size(); |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1668 | (*M)[Exp] = x; |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1669 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1670 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1671 | // Look at terminators. The condition is a block-level expression. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1672 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1673 | Stmt* S = I->getTerminatorCondition(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1674 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1675 | if (S && M->find(S) == M->end()) { |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1676 | unsigned x = M->size(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1677 | (*M)[S] = x; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1680 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1681 | return M; |
| 1682 | } |
| 1683 | |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1684 | CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { |
| 1685 | assert(S != NULL); |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1686 | if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1687 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1688 | BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1689 | BlkExprMapTy::iterator I = M->find(S); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1690 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1691 | if (I == M->end()) return CFG::BlkExprNumTy(); |
| 1692 | else return CFG::BlkExprNumTy(I->second); |
| 1693 | } |
| 1694 | |
| 1695 | unsigned CFG::getNumBlkExprs() { |
| 1696 | if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) |
| 1697 | return M->size(); |
| 1698 | else { |
| 1699 | // We assume callers interested in the number of BlkExprs will want |
| 1700 | // the map constructed if it doesn't already exist. |
| 1701 | BlkExprMap = (void*) PopulateBlkExprMap(*this); |
| 1702 | return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); |
| 1703 | } |
| 1704 | } |
| 1705 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1706 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1707 | // Cleanup: CFG dstor. |
| 1708 | //===----------------------------------------------------------------------===// |
| 1709 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1710 | CFG::~CFG() { |
| 1711 | delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); |
| 1712 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1713 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1714 | //===----------------------------------------------------------------------===// |
| 1715 | // CFG pretty printing |
| 1716 | //===----------------------------------------------------------------------===// |
| 1717 | |
Ted Kremenek | 7e776b1 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 1718 | namespace { |
| 1719 | |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 1720 | class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1721 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1722 | typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
| 1723 | StmtMapTy StmtMap; |
| 1724 | signed CurrentBlock; |
| 1725 | unsigned CurrentStmt; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1726 | const LangOptions &LangOpts; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1727 | public: |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1728 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1729 | StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) |
| 1730 | : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1731 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 1732 | unsigned j = 1; |
| 1733 | for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ; |
| 1734 | BI != BEnd; ++BI, ++j ) |
| 1735 | StmtMap[*BI] = std::make_pair(I->getBlockID(),j); |
| 1736 | } |
| 1737 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1738 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1739 | virtual ~StmtPrinterHelper() {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1740 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1741 | const LangOptions &getLangOpts() const { return LangOpts; } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1742 | void setBlockID(signed i) { CurrentBlock = i; } |
| 1743 | void setStmtID(unsigned i) { CurrentStmt = i; } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1744 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1745 | virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1746 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1747 | StmtMapTy::iterator I = StmtMap.find(Terminator); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1748 | |
| 1749 | if (I == StmtMap.end()) |
| 1750 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1751 | |
| 1752 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1753 | && I->second.second == CurrentStmt) |
| 1754 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1755 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1756 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 1757 | return true; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1758 | } |
| 1759 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1760 | } // end anonymous namespace |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1762 | |
| 1763 | namespace { |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 1764 | class VISIBILITY_HIDDEN CFGBlockTerminatorPrint |
| 1765 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1766 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1767 | llvm::raw_ostream& OS; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1768 | StmtPrinterHelper* Helper; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1769 | PrintingPolicy Policy; |
| 1770 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1771 | public: |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1772 | CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1773 | const PrintingPolicy &Policy) |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1774 | : OS(os), Helper(helper), Policy(Policy) {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1775 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1776 | void VisitIfStmt(IfStmt* I) { |
| 1777 | OS << "if "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1778 | I->getCond()->printPretty(OS,Helper,Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1779 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1780 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1781 | // Default case. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1782 | void VisitStmt(Stmt* Terminator) { |
| 1783 | Terminator->printPretty(OS, Helper, Policy); |
| 1784 | } |
| 1785 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1786 | void VisitForStmt(ForStmt* F) { |
| 1787 | OS << "for (" ; |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1788 | if (F->getInit()) OS << "..."; |
| 1789 | OS << "; "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1790 | if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy); |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1791 | OS << "; "; |
| 1792 | if (F->getInc()) OS << "..."; |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1793 | OS << ")"; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1794 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1795 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1796 | void VisitWhileStmt(WhileStmt* W) { |
| 1797 | OS << "while " ; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1798 | if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1799 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1800 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1801 | void VisitDoStmt(DoStmt* D) { |
| 1802 | OS << "do ... while "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1803 | if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1804 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1805 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1806 | void VisitSwitchStmt(SwitchStmt* Terminator) { |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1807 | OS << "switch "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1808 | Terminator->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1809 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1810 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1811 | void VisitConditionalOperator(ConditionalOperator* C) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1812 | C->getCond()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1813 | OS << " ? ... : ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1814 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1815 | |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1816 | void VisitChooseExpr(ChooseExpr* C) { |
| 1817 | OS << "__builtin_choose_expr( "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1818 | C->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1819 | OS << " )"; |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1820 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1821 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1822 | void VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 1823 | OS << "goto *"; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1824 | I->getTarget()->printPretty(OS, Helper, Policy); |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1825 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1826 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1827 | void VisitBinaryOperator(BinaryOperator* B) { |
| 1828 | if (!B->isLogicalOp()) { |
| 1829 | VisitExpr(B); |
| 1830 | return; |
| 1831 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1833 | B->getLHS()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1834 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1835 | switch (B->getOpcode()) { |
| 1836 | case BinaryOperator::LOr: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1837 | OS << " || ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1838 | return; |
| 1839 | case BinaryOperator::LAnd: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1840 | OS << " && ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1841 | return; |
| 1842 | default: |
| 1843 | assert(false && "Invalid logical operator."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1844 | } |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1845 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1846 | |
Ted Kremenek | 12687ff | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 1847 | void VisitExpr(Expr* E) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1848 | E->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1849 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1850 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1851 | } // end anonymous namespace |
| 1852 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1853 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1854 | static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper, |
| 1855 | Stmt* Terminator) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1856 | if (Helper) { |
| 1857 | // special printing for statement-expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1858 | if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1859 | CompoundStmt* Sub = SE->getSubStmt(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1860 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1861 | if (Sub->child_begin() != Sub->child_end()) { |
Ted Kremenek | cc77806 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1862 | OS << "({ ... ; "; |
Ted Kremenek | 6d845f0 | 2007-10-29 20:41:04 +0000 | [diff] [blame] | 1863 | Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
Ted Kremenek | cc77806 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1864 | OS << " })\n"; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1865 | return; |
| 1866 | } |
| 1867 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1868 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1869 | // special printing for comma expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1870 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1871 | if (B->getOpcode() == BinaryOperator::Comma) { |
| 1872 | OS << "... , "; |
| 1873 | Helper->handledStmt(B->getRHS(),OS); |
| 1874 | OS << '\n'; |
| 1875 | return; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1878 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1879 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1880 | Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1881 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1882 | // Expressions need a newline. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1883 | if (isa<Expr>(Terminator)) OS << '\n'; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1884 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1886 | static void print_block(llvm::raw_ostream& OS, const CFG* cfg, |
| 1887 | const CFGBlock& B, |
| 1888 | StmtPrinterHelper* Helper, bool print_edges) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1889 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1890 | if (Helper) Helper->setBlockID(B.getBlockID()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1891 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1892 | // Print the header. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1893 | OS << "\n [ B" << B.getBlockID(); |
| 1894 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1895 | if (&B == &cfg->getEntry()) |
| 1896 | OS << " (ENTRY) ]\n"; |
| 1897 | else if (&B == &cfg->getExit()) |
| 1898 | OS << " (EXIT) ]\n"; |
| 1899 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1900 | OS << " (INDIRECT GOTO DISPATCH) ]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1901 | else |
| 1902 | OS << " ]\n"; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1903 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1904 | // Print the label of this block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1905 | if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1906 | |
| 1907 | if (print_edges) |
| 1908 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1909 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1910 | if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1911 | OS << L->getName(); |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1912 | else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) { |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1913 | OS << "case "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1914 | C->getLHS()->printPretty(OS, Helper, |
| 1915 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1916 | if (C->getRHS()) { |
| 1917 | OS << " ... "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1918 | C->getRHS()->printPretty(OS, Helper, |
| 1919 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1920 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1921 | } else if (isa<DefaultStmt>(Terminator)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1922 | OS << "default"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1923 | else |
| 1924 | assert(false && "Invalid label statement in CFGBlock."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1925 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1926 | OS << ":\n"; |
| 1927 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1928 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1929 | // Iterate through the statements in the block and print them. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1930 | unsigned j = 1; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1931 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1932 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 1933 | I != E ; ++I, ++j ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1935 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1936 | if (print_edges) |
| 1937 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1938 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1939 | OS << llvm::format("%3d", j) << ": "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1940 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1941 | if (Helper) |
| 1942 | Helper->setStmtID(j); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1943 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1944 | print_stmt(OS,Helper,*I); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1945 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1946 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1947 | // Print the terminator of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1948 | if (B.getTerminator()) { |
| 1949 | if (print_edges) |
| 1950 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1951 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1952 | OS << " T: "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1953 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1954 | if (Helper) Helper->setBlockID(-1); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1955 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1956 | CFGBlockTerminatorPrint TPrinter(OS, Helper, |
| 1957 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1958 | TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1959 | OS << '\n'; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1960 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1961 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1962 | if (print_edges) { |
| 1963 | // Print the predecessors of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1964 | OS << " Predecessors (" << B.pred_size() << "):"; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1965 | unsigned i = 0; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1966 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1967 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 1968 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1969 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1970 | if (i == 8 || (i-8) == 0) |
| 1971 | OS << "\n "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1972 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1973 | OS << " B" << (*I)->getBlockID(); |
| 1974 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1975 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1976 | OS << '\n'; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1977 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1978 | // Print the successors of this block. |
| 1979 | OS << " Successors (" << B.succ_size() << "):"; |
| 1980 | i = 0; |
| 1981 | |
| 1982 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 1983 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1984 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1985 | if (i == 8 || (i-8) % 10 == 0) |
| 1986 | OS << "\n "; |
| 1987 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame] | 1988 | if (*I) |
| 1989 | OS << " B" << (*I)->getBlockID(); |
| 1990 | else |
| 1991 | OS << " NULL"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1992 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1993 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1994 | OS << '\n'; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1995 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1996 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1997 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1998 | |
| 1999 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2000 | void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2001 | |
| 2002 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2003 | void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const { |
| 2004 | StmtPrinterHelper Helper(this, LO); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2005 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2006 | // Print the entry block. |
| 2007 | print_block(OS, this, getEntry(), &Helper, true); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2008 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2009 | // Iterate through the CFGBlocks and print them one by one. |
| 2010 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 2011 | // Skip the entry block, because we already printed it. |
| 2012 | if (&(*I) == &getEntry() || &(*I) == &getExit()) |
| 2013 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2014 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2015 | print_block(OS, this, *I, &Helper, true); |
| 2016 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2017 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2018 | // Print the exit block. |
| 2019 | print_block(OS, this, getExit(), &Helper, true); |
Ted Kremenek | e03879b | 2008-11-24 20:50:24 +0000 | [diff] [blame] | 2020 | OS.flush(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2021 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2022 | |
| 2023 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2024 | void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { |
| 2025 | print(llvm::errs(), cfg, LO); |
| 2026 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2027 | |
| 2028 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 2029 | /// Generally this will only be called from CFG::print. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2030 | void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg, |
| 2031 | const LangOptions &LO) const { |
| 2032 | StmtPrinterHelper Helper(cfg, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2033 | print_block(OS, cfg, *this, &Helper, true); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 2034 | } |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2035 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2036 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2037 | void CFGBlock::printTerminator(llvm::raw_ostream &OS, |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2038 | const LangOptions &LO) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2039 | CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO)); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2040 | TPrinter.Visit(const_cast<Stmt*>(getTerminator())); |
| 2041 | } |
| 2042 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2043 | Stmt* CFGBlock::getTerminatorCondition() { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2044 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2045 | if (!Terminator) |
| 2046 | return NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2047 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2048 | Expr* E = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2049 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2050 | switch (Terminator->getStmtClass()) { |
| 2051 | default: |
| 2052 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2053 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2054 | case Stmt::ForStmtClass: |
| 2055 | E = cast<ForStmt>(Terminator)->getCond(); |
| 2056 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2057 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2058 | case Stmt::WhileStmtClass: |
| 2059 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 2060 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2061 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2062 | case Stmt::DoStmtClass: |
| 2063 | E = cast<DoStmt>(Terminator)->getCond(); |
| 2064 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2065 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2066 | case Stmt::IfStmtClass: |
| 2067 | E = cast<IfStmt>(Terminator)->getCond(); |
| 2068 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2069 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2070 | case Stmt::ChooseExprClass: |
| 2071 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 2072 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2073 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2074 | case Stmt::IndirectGotoStmtClass: |
| 2075 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 2076 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2077 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2078 | case Stmt::SwitchStmtClass: |
| 2079 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 2080 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2081 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2082 | case Stmt::ConditionalOperatorClass: |
| 2083 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 2084 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2085 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2086 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 2087 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2088 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2089 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2090 | case Stmt::ObjCForCollectionStmtClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2091 | return Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2092 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2093 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2094 | return E ? E->IgnoreParens() : NULL; |
| 2095 | } |
| 2096 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2097 | bool CFGBlock::hasBinaryBranchTerminator() const { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2098 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2099 | if (!Terminator) |
| 2100 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2101 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2102 | Expr* E = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2103 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2104 | switch (Terminator->getStmtClass()) { |
| 2105 | default: |
| 2106 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2107 | |
| 2108 | case Stmt::ForStmtClass: |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2109 | case Stmt::WhileStmtClass: |
| 2110 | case Stmt::DoStmtClass: |
| 2111 | case Stmt::IfStmtClass: |
| 2112 | case Stmt::ChooseExprClass: |
| 2113 | case Stmt::ConditionalOperatorClass: |
| 2114 | case Stmt::BinaryOperatorClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2115 | return true; |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2116 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2117 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2118 | return E ? E->IgnoreParens() : NULL; |
| 2119 | } |
| 2120 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2121 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2122 | //===----------------------------------------------------------------------===// |
| 2123 | // CFG Graphviz Visualization |
| 2124 | //===----------------------------------------------------------------------===// |
| 2125 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2126 | |
| 2127 | #ifndef NDEBUG |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2128 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2129 | #endif |
| 2130 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2131 | void CFG::viewCFG(const LangOptions &LO) const { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2132 | #ifndef NDEBUG |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2133 | StmtPrinterHelper H(this, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2134 | GraphHelper = &H; |
| 2135 | llvm::ViewGraph(this,"CFG"); |
| 2136 | GraphHelper = NULL; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2137 | #endif |
| 2138 | } |
| 2139 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2140 | namespace llvm { |
| 2141 | template<> |
| 2142 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
Owen Anderson | 4d9e93c | 2009-06-24 17:37:55 +0000 | [diff] [blame] | 2143 | static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph, |
| 2144 | bool ShortNames) { |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2145 | |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 2146 | #ifndef NDEBUG |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2147 | std::string OutSStr; |
| 2148 | llvm::raw_string_ostream Out(OutSStr); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2149 | print_block(Out,Graph, *Node, GraphHelper, false); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2150 | std::string& OutStr = Out.str(); |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2151 | |
| 2152 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 2153 | |
| 2154 | // Process string output to make it nicer... |
| 2155 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 2156 | if (OutStr[i] == '\n') { // Left justify |
| 2157 | OutStr[i] = '\\'; |
| 2158 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 2159 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2160 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2161 | return OutStr; |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 2162 | #else |
| 2163 | return ""; |
| 2164 | #endif |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2165 | } |
| 2166 | }; |
| 2167 | } // end namespace llvm |