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