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