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) { |
| 524 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
| 525 | ConfluenceBlock->appendStmt(C); |
| 526 | if (!FinishBlock(ConfluenceBlock)) |
| 527 | return 0; |
| 528 | |
| 529 | Succ = ConfluenceBlock; |
| 530 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 531 | CFGBlock* LHSBlock = addStmt(C->getLHS()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 532 | if (!FinishBlock(LHSBlock)) |
| 533 | return 0; |
| 534 | |
| 535 | Succ = ConfluenceBlock; |
| 536 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 537 | CFGBlock* RHSBlock = addStmt(C->getRHS()); |
Ted Kremenek | 2182259 | 2009-07-17 18:20:32 +0000 | [diff] [blame] | 538 | if (!FinishBlock(RHSBlock)) |
| 539 | return 0; |
| 540 | |
| 541 | Block = createBlock(false); |
| 542 | Block->addSuccessor(LHSBlock); |
| 543 | Block->addSuccessor(RHSBlock); |
| 544 | Block->setTerminator(C); |
| 545 | return addStmt(C->getCond()); |
| 546 | } |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 547 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 548 | |
| 549 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
| 550 | CFGBlock* LastBlock = Block; |
| 551 | |
| 552 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 553 | I != E; ++I ) { |
| 554 | LastBlock = addStmt(*I); |
| 555 | } |
| 556 | return LastBlock; |
| 557 | } |
| 558 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 559 | CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) { |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 560 | // See if this is a known constant. |
| 561 | bool KnownTrue = false; |
| 562 | bool KnownFalse = false; |
| 563 | Expr::EvalResult Result; |
| 564 | if (C->getCond()->Evaluate(Result, *Context) |
| 565 | && Result.Val.isInt()) { |
| 566 | if (Result.Val.getInt().getBoolValue()) |
| 567 | KnownTrue = true; |
| 568 | else |
| 569 | KnownFalse = true; |
| 570 | } |
| 571 | |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 572 | // Create the confluence block that will "merge" the results of the ternary |
| 573 | // expression. |
| 574 | CFGBlock* ConfluenceBlock = Block ? Block : createBlock(); |
| 575 | ConfluenceBlock->appendStmt(C); |
| 576 | if (!FinishBlock(ConfluenceBlock)) |
| 577 | return 0; |
| 578 | |
| 579 | // Create a block for the LHS expression if there is an LHS expression. A |
| 580 | // GCC extension allows LHS to be NULL, causing the condition to be the |
| 581 | // value that is returned instead. |
| 582 | // e.g: x ?: y is shorthand for: x ? x : y; |
| 583 | Succ = ConfluenceBlock; |
| 584 | Block = NULL; |
| 585 | CFGBlock* LHSBlock = NULL; |
| 586 | if (C->getLHS()) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 587 | LHSBlock = addStmt(C->getLHS()); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 588 | if (!FinishBlock(LHSBlock)) |
| 589 | return 0; |
| 590 | Block = NULL; |
| 591 | } |
| 592 | |
| 593 | // Create the block for the RHS expression. |
| 594 | Succ = ConfluenceBlock; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 595 | CFGBlock* RHSBlock = addStmt(C->getRHS()); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 596 | if (!FinishBlock(RHSBlock)) |
| 597 | return 0; |
| 598 | |
| 599 | // Create the block that will contain the condition. |
| 600 | Block = createBlock(false); |
| 601 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 602 | if (LHSBlock) { |
| 603 | if (KnownFalse) |
| 604 | Block->addSuccessor(0); |
| 605 | else |
| 606 | Block->addSuccessor(LHSBlock); |
| 607 | } else { |
| 608 | if (KnownFalse) { |
| 609 | // If we know the condition is false, add NULL as the successor for |
| 610 | // the block containing the condition. In this case, the confluence |
| 611 | // block will have just one predecessor. |
| 612 | Block->addSuccessor(0); |
| 613 | assert (ConfluenceBlock->pred_size() == 1); |
| 614 | } else { |
| 615 | // If we have no LHS expression, add the ConfluenceBlock as a direct |
| 616 | // successor for the block containing the condition. Moreover, we need to |
| 617 | // reverse the order of the predecessors in the ConfluenceBlock because |
| 618 | // the RHSBlock will have been added to the succcessors already, and we |
| 619 | // want the first predecessor to the the block containing the expression |
| 620 | // for the case when the ternary expression evaluates to true. |
| 621 | Block->addSuccessor(ConfluenceBlock); |
| 622 | assert (ConfluenceBlock->pred_size() == 2); |
| 623 | std::reverse(ConfluenceBlock->pred_begin(), |
| 624 | ConfluenceBlock->pred_end()); |
| 625 | } |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 628 | if (KnownTrue) |
| 629 | Block->addSuccessor(0); |
| 630 | else |
| 631 | Block->addSuccessor(RHSBlock); |
Ted Kremenek | 51d40b0 | 2009-07-17 18:15:54 +0000 | [diff] [blame] | 632 | |
| 633 | Block->setTerminator(C); |
| 634 | return addStmt(C->getCond()); |
| 635 | } |
| 636 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 637 | CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) { |
| 638 | autoCreateBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 640 | if (DS->isSingleDecl()) { |
| 641 | Block->appendStmt(DS); |
| 642 | return VisitDeclSubExpr(DS->getSingleDecl()); |
Ted Kremenek | f699882 | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 643 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 644 | |
| 645 | CFGBlock *B = 0; |
| 646 | |
| 647 | // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy. |
| 648 | typedef llvm::SmallVector<Decl*,10> BufTy; |
| 649 | BufTy Buf(DS->decl_begin(), DS->decl_end()); |
| 650 | |
| 651 | for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) { |
| 652 | // Get the alignment of the new DeclStmt, padding out to >=8 bytes. |
| 653 | unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8 |
| 654 | ? 8 : llvm::AlignOf<DeclStmt>::Alignment; |
| 655 | |
| 656 | // Allocate the DeclStmt using the BumpPtrAllocator. It will get |
| 657 | // automatically freed with the CFG. |
| 658 | DeclGroupRef DG(*I); |
| 659 | Decl *D = *I; |
| 660 | void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A); |
| 661 | DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D)); |
| 662 | |
| 663 | // Append the fake DeclStmt to block. |
| 664 | Block->appendStmt(DSNew); |
| 665 | B = VisitDeclSubExpr(D); |
| 666 | } |
| 667 | |
| 668 | return B; |
| 669 | } |
| 670 | |
| 671 | /// VisitDeclSubExpr - Utility method to add block-level expressions for |
| 672 | /// initializers in Decls. |
| 673 | CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) { |
| 674 | assert(Block); |
Ted Kremenek | f699882 | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 676 | VarDecl *VD = dyn_cast<VarDecl>(D); |
| 677 | |
| 678 | if (!VD) |
| 679 | return Block; |
| 680 | |
| 681 | Expr *Init = VD->getInit(); |
| 682 | |
| 683 | if (Init) { |
| 684 | // Optimization: Don't create separate block-level statements for literals. |
| 685 | switch (Init->getStmtClass()) { |
| 686 | case Stmt::IntegerLiteralClass: |
| 687 | case Stmt::CharacterLiteralClass: |
| 688 | case Stmt::StringLiteralClass: |
| 689 | break; |
| 690 | default: |
| 691 | Block = addStmt(Init); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // If the type of VD is a VLA, then we must process its size expressions. |
| 696 | for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0; |
| 697 | VA = FindVA(VA->getElementType().getTypePtr())) |
| 698 | Block = addStmt(VA->getSizeExpr()); |
| 699 | |
| 700 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 704 | // See if this is a known constant first. |
| 705 | bool KnownTrue = false; |
| 706 | bool KnownFalse = false; |
| 707 | Expr::EvalResult Result; |
| 708 | if (I->getCond()->Evaluate(Result, *Context) |
| 709 | && Result.Val.isInt()) { |
| 710 | if (Result.Val.getInt().getBoolValue()) |
| 711 | KnownTrue = true; |
| 712 | else |
| 713 | KnownFalse = true; |
| 714 | } |
| 715 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 716 | // We may see an if statement in the middle of a basic block, or it may be the |
| 717 | // first statement we are processing. In either case, we create a new basic |
| 718 | // block. First, we create the blocks for the then...else statements, and |
| 719 | // then we create the block containing the if statement. If we were in the |
| 720 | // middle of a block, we stop processing that block and reverse its |
| 721 | // statements. That block is then the implicit successor for the "then" and |
| 722 | // "else" clauses. |
| 723 | |
| 724 | // The block we were proccessing is now finished. Make it the successor |
| 725 | // block. |
| 726 | if (Block) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 727 | Succ = Block; |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 728 | if (!FinishBlock(Block)) |
| 729 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 730 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 731 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 732 | // Process the false branch. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 733 | CFGBlock* ElseBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 734 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 735 | if (Stmt* Else = I->getElse()) { |
| 736 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 737 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 738 | // NULL out Block so that the recursive call to Visit will |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 739 | // create a new basic block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 740 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 741 | ElseBlock = addStmt(Else); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 742 | |
Ted Kremenek | bbad8ce | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 743 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 744 | ElseBlock = sv.get(); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 745 | else if (Block) { |
| 746 | if (!FinishBlock(ElseBlock)) |
| 747 | return 0; |
| 748 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 749 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 750 | |
Ted Kremenek | 0bcdc98 | 2009-07-17 18:04:55 +0000 | [diff] [blame] | 751 | // Process the true branch. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 752 | CFGBlock* ThenBlock; |
| 753 | { |
| 754 | Stmt* Then = I->getThen(); |
| 755 | assert (Then); |
| 756 | SaveAndRestore<CFGBlock*> sv(Succ); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 757 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 758 | ThenBlock = addStmt(Then); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 759 | |
Ted Kremenek | 1b37951 | 2009-04-01 03:52:47 +0000 | [diff] [blame] | 760 | if (!ThenBlock) { |
| 761 | // We can reach here if the "then" body has all NullStmts. |
| 762 | // Create an empty block so we can distinguish between true and false |
| 763 | // branches in path-sensitive analyses. |
| 764 | ThenBlock = createBlock(false); |
| 765 | ThenBlock->addSuccessor(sv.get()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 766 | } else if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 767 | if (!FinishBlock(ThenBlock)) |
| 768 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 769 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 772 | // Now create a new block containing the if statement. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 773 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 774 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 775 | // Set the terminator of the new block to the If statement. |
| 776 | Block->setTerminator(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 777 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 778 | // Now add the successors. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 779 | if (KnownFalse) |
| 780 | Block->addSuccessor(0); |
| 781 | else |
| 782 | Block->addSuccessor(ThenBlock); |
| 783 | if (KnownTrue) |
| 784 | Block->addSuccessor(0); |
| 785 | else |
| 786 | Block->addSuccessor(ElseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 787 | |
| 788 | // Add the condition as the last statement in the new block. This may create |
| 789 | // new blocks as the condition may contain control-flow. Any newly created |
| 790 | // blocks will be pointed to be "Block". |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 791 | return addStmt(I->getCond()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 792 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 793 | |
| 794 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 795 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 796 | // If we were in the middle of a block we stop processing that block and |
| 797 | // reverse its statements. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 798 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 799 | // NOTE: If a "return" appears in the middle of a block, this means that the |
| 800 | // code afterwards is DEAD (unreachable). We still keep a basic block |
| 801 | // for that code; a simple "mark-and-sweep" from the entry block will be |
| 802 | // able to report such dead blocks. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 803 | if (Block) FinishBlock(Block); |
| 804 | |
| 805 | // Create the new block. |
| 806 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 807 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 808 | // The Exit block is the only successor. |
| 809 | Block->addSuccessor(&cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 810 | |
| 811 | // Add the return statement to the block. This may create new blocks if R |
| 812 | // contains control-flow (short-circuit operations). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 813 | return VisitStmt(R, true); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 817 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 818 | addStmt(L->getSubStmt()); |
Ted Kremenek | cab47bd | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 819 | CFGBlock* LabelBlock = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 820 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 821 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 822 | LabelBlock = createBlock(); // scopes that only contains NullStmts. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 824 | assert(LabelMap.find(L) == LabelMap.end() && "label already in map"); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 825 | LabelMap[ L ] = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 826 | |
| 827 | // Labels partition blocks, so this is the end of the basic block we were |
| 828 | // processing (L is the block's label). Because this is label (and we have |
| 829 | // already processed the substatement) there is no extra control-flow to worry |
| 830 | // about. |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 831 | LabelBlock->setLabel(L); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 832 | if (!FinishBlock(LabelBlock)) |
| 833 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 834 | |
| 835 | // 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] | 836 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 838 | // This block is now the implicit successor of other blocks. |
| 839 | Succ = LabelBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 840 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 841 | return LabelBlock; |
| 842 | } |
| 843 | |
| 844 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 845 | // Goto is a control-flow statement. Thus we stop processing the current |
| 846 | // block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 847 | if (Block) |
| 848 | FinishBlock(Block); |
| 849 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 850 | Block = createBlock(false); |
| 851 | Block->setTerminator(G); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 852 | |
| 853 | // 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] | 854 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
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 | if (I == LabelMap.end()) |
| 857 | // We will need to backpatch this block later. |
| 858 | BackpatchBlocks.push_back(Block); |
| 859 | else |
| 860 | Block->addSuccessor(I->second); |
| 861 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 862 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 866 | // "for" is a control-flow statement. Thus we stop processing the current |
| 867 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 868 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 869 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 870 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 871 | if (!FinishBlock(Block)) |
| 872 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 873 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 874 | } else |
| 875 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 876 | |
| 877 | // Because of short-circuit evaluation, the condition of the loop can span |
| 878 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 879 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 880 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 881 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 882 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 883 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 884 | ExitConditionBlock->setTerminator(F); |
| 885 | |
| 886 | // Now add the actual condition to the condition block. Because the condition |
| 887 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 888 | if (Stmt* C = F->getCond()) { |
| 889 | Block = ExitConditionBlock; |
| 890 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 891 | if (Block) { |
| 892 | if (!FinishBlock(EntryConditionBlock)) |
| 893 | return 0; |
| 894 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 895 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 896 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 897 | // The condition block is the implicit successor for the loop body as well as |
| 898 | // any code above the loop. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 899 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 901 | // Now create the loop body. |
| 902 | { |
| 903 | assert (F->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 905 | // Save the current values for Block, Succ, and continue and break targets |
| 906 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 907 | save_continue(ContinueTargetBlock), |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 908 | save_break(BreakTargetBlock); |
| 909 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 910 | // Create a new block to contain the (bottom) of the loop body. |
| 911 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 912 | |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 913 | if (Stmt* I = F->getInc()) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 914 | // Generate increment code in its own basic block. This is the target of |
| 915 | // continue statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 916 | Succ = addStmt(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 917 | } else { |
| 918 | // No increment code. Create a special, empty, block that is used as the |
| 919 | // target block for "looping back" to the start of the loop. |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 920 | assert(Succ == EntryConditionBlock); |
| 921 | Succ = createBlock(); |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 922 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 923 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 924 | // Finish up the increment (or empty) block if it hasn't been already. |
| 925 | if (Block) { |
| 926 | assert(Block == Succ); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 927 | if (!FinishBlock(Block)) |
| 928 | return 0; |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 929 | Block = 0; |
| 930 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 931 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 932 | ContinueTargetBlock = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 933 | |
Ted Kremenek | 902393b | 2009-04-28 00:51:56 +0000 | [diff] [blame] | 934 | // The starting block for the loop increment is the block that should |
| 935 | // represent the 'loop target' for looping back to the start of the loop. |
| 936 | ContinueTargetBlock->setLoopTarget(F); |
| 937 | |
Ted Kremenek | b0746ca | 2008-09-04 21:48:47 +0000 | [diff] [blame] | 938 | // All breaks should go to the code following the loop. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 939 | BreakTargetBlock = LoopSuccessor; |
| 940 | |
| 941 | // Now populate the body block, and in the process create new blocks as we |
| 942 | // walk the body of the loop. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 943 | CFGBlock* BodyBlock = addStmt(F->getBody()); |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 944 | |
| 945 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 946 | BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 947 | else if (Block) { |
| 948 | if (!FinishBlock(BodyBlock)) |
| 949 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 952 | // This new body block is a successor to our "exit" condition block. |
| 953 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 954 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 955 | |
| 956 | // Link up the condition block with the code that follows the loop. (the |
| 957 | // false branch). |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 958 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 959 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 960 | // If the loop contains initialization, create a new block for those |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 961 | // statements. This block can also contain statements that precede the loop. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 962 | if (Stmt* I = F->getInit()) { |
| 963 | Block = createBlock(); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 964 | return addStmt(I); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 965 | } else { |
| 966 | // There is no loop initialization. We are thus basically a while loop. |
| 967 | // NULL out Block to force lazy block construction. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 968 | Block = NULL; |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 969 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 970 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 974 | CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) { |
| 975 | // Objective-C fast enumeration 'for' statements: |
| 976 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC |
| 977 | // |
| 978 | // for ( Type newVariable in collection_expression ) { statements } |
| 979 | // |
| 980 | // becomes: |
| 981 | // |
| 982 | // prologue: |
| 983 | // 1. collection_expression |
| 984 | // T. jump to loop_entry |
| 985 | // loop_entry: |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 986 | // 1. side-effects of element expression |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 987 | // 1. ObjCForCollectionStmt [performs binding to newVariable] |
| 988 | // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil] |
| 989 | // TB: |
| 990 | // statements |
| 991 | // T. jump to loop_entry |
| 992 | // FB: |
| 993 | // what comes after |
| 994 | // |
| 995 | // and |
| 996 | // |
| 997 | // Type existingItem; |
| 998 | // for ( existingItem in expression ) { statements } |
| 999 | // |
| 1000 | // becomes: |
| 1001 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1002 | // the same with newVariable replaced with existingItem; the binding works |
| 1003 | // the same except that for one ObjCForCollectionStmt::getElement() returns |
| 1004 | // a DeclStmt and the other returns a DeclRefExpr. |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1005 | // |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1006 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1007 | CFGBlock* LoopSuccessor = 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1008 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1009 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1010 | if (!FinishBlock(Block)) |
| 1011 | return 0; |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1012 | LoopSuccessor = Block; |
| 1013 | Block = 0; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1014 | } else |
| 1015 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1016 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1017 | // Build the condition blocks. |
| 1018 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1019 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1020 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1021 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1022 | ExitConditionBlock->setTerminator(S); |
| 1023 | |
| 1024 | // The last statement in the block should be the ObjCForCollectionStmt, which |
| 1025 | // performs the actual binding to 'element' and determines if there are any |
| 1026 | // more items in the collection. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1027 | ExitConditionBlock->appendStmt(S); |
| 1028 | Block = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1029 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1030 | // 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] | 1031 | // generate new blocks as necesary. We DON'T add the statement by default to |
| 1032 | // the CFG unless it contains control-flow. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1033 | EntryConditionBlock = Visit(S->getElement(), false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1034 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1035 | if (!FinishBlock(EntryConditionBlock)) |
| 1036 | return 0; |
| 1037 | Block = 0; |
| 1038 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1039 | |
| 1040 | // The condition block is the implicit successor for the loop body as well as |
| 1041 | // any code above the loop. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1042 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1043 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1044 | // Now create the true branch. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1045 | { |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1046 | // Save the current values for Succ, continue and break targets. |
| 1047 | SaveAndRestore<CFGBlock*> save_Succ(Succ), |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1048 | save_continue(ContinueTargetBlock), save_break(BreakTargetBlock); |
| 1049 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1050 | BreakTargetBlock = LoopSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1051 | ContinueTargetBlock = EntryConditionBlock; |
| 1052 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1053 | CFGBlock* BodyBlock = addStmt(S->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1054 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1055 | if (!BodyBlock) |
| 1056 | BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1057 | else if (Block) { |
| 1058 | if (!FinishBlock(BodyBlock)) |
| 1059 | return 0; |
| 1060 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1061 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1062 | // This new body block is a successor to our "exit" condition block. |
| 1063 | ExitConditionBlock->addSuccessor(BodyBlock); |
| 1064 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1065 | |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1066 | // Link up the condition block with the code that follows the loop. |
| 1067 | // (the false branch). |
| 1068 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 1069 | |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1070 | // Now create a prologue block to contain the collection expression. |
Ted Kremenek | 5cf87ff | 2008-11-14 01:57:41 +0000 | [diff] [blame] | 1071 | Block = createBlock(); |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1072 | return addStmt(S->getCollection()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1075 | CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) { |
| 1076 | // FIXME: Add locking 'primitives' to CFG for @synchronized. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1077 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1078 | // Inline the body. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1079 | CFGBlock *SyncBlock = addStmt(S->getSynchBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1080 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1081 | // The sync body starts its own basic block. This makes it a little easier |
| 1082 | // for diagnostic clients. |
| 1083 | if (SyncBlock) { |
| 1084 | if (!FinishBlock(SyncBlock)) |
| 1085 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1086 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1087 | Block = 0; |
| 1088 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | b3c657b | 2009-05-05 23:11:51 +0000 | [diff] [blame] | 1090 | Succ = SyncBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1091 | |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1092 | // Inline the sync expression. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1093 | return addStmt(S->getSynchExpr()); |
Ted Kremenek | 4980545 | 2009-05-02 01:49:13 +0000 | [diff] [blame] | 1094 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1095 | |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1096 | CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1097 | // FIXME |
Ted Kremenek | 89be652 | 2009-04-07 04:26:02 +0000 | [diff] [blame] | 1098 | return NYS(); |
Ted Kremenek | 89cc8ea | 2009-03-30 22:29:21 +0000 | [diff] [blame] | 1099 | } |
Ted Kremenek | 9d56e64 | 2008-11-11 17:10:00 +0000 | [diff] [blame] | 1100 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1101 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1102 | // "while" is a control-flow statement. Thus we stop processing the current |
| 1103 | // block. |
| 1104 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1105 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1106 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1107 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1108 | if (!FinishBlock(Block)) |
| 1109 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1110 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1111 | } else |
| 1112 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1113 | |
| 1114 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1115 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1116 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1117 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1118 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1119 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1120 | // Set the terminator for the "exit" condition block. |
| 1121 | ExitConditionBlock->setTerminator(W); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Now add the actual condition to the condition block. Because the condition |
| 1124 | // itself may contain control-flow, new blocks may be created. Thus we update |
| 1125 | // "Succ" after adding the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1126 | if (Stmt* C = W->getCond()) { |
| 1127 | Block = ExitConditionBlock; |
| 1128 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1129 | assert(Block == EntryConditionBlock); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1130 | if (Block) { |
| 1131 | if (!FinishBlock(EntryConditionBlock)) |
| 1132 | return 0; |
| 1133 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1134 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1135 | |
| 1136 | // The condition block is the implicit successor for the loop body as well as |
| 1137 | // any code above the loop. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1138 | Succ = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1139 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1140 | // Process the loop body. |
| 1141 | { |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1142 | assert(W->getBody()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Save the current values for Block, Succ, and continue and break targets |
| 1145 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 1146 | save_continue(ContinueTargetBlock), |
| 1147 | save_break(BreakTargetBlock); |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1148 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1149 | // Create an empty block to represent the transition block for looping back |
| 1150 | // to the head of the loop. |
Ted Kremenek | 49936f7 | 2009-04-28 03:09:44 +0000 | [diff] [blame] | 1151 | Block = 0; |
| 1152 | assert(Succ == EntryConditionBlock); |
| 1153 | Succ = createBlock(); |
| 1154 | Succ->setLoopTarget(W); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1155 | ContinueTargetBlock = Succ; |
| 1156 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1157 | // All breaks should go to the code following the loop. |
| 1158 | BreakTargetBlock = LoopSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1159 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1160 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1161 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1162 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1163 | // 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] | 1164 | CFGBlock* BodyBlock = addStmt(W->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1165 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1166 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 1167 | BodyBlock = EntryConditionBlock; // can happen for "while(...) ;" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1168 | else if (Block) { |
| 1169 | if (!FinishBlock(BodyBlock)) |
| 1170 | return 0; |
| 1171 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1172 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1173 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1174 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1175 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1176 | |
| 1177 | // Link up the condition block with the code that follows the loop. (the |
| 1178 | // false branch). |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1179 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1180 | |
| 1181 | // There can be no more statements in the condition block since we loop back |
| 1182 | // 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] | 1183 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1184 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1185 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1186 | Succ = EntryConditionBlock; |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1187 | return EntryConditionBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1188 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1189 | |
| 1190 | |
| 1191 | CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { |
| 1192 | // FIXME: For now we pretend that @catch and the code it contains does not |
| 1193 | // exit. |
| 1194 | return Block; |
| 1195 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1196 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1197 | CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { |
| 1198 | // FIXME: This isn't complete. We basically treat @throw like a return |
| 1199 | // statement. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1200 | |
| 1201 | // If we were in the middle of a block we stop processing that block and |
| 1202 | // reverse its statements. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1203 | if (Block && !FinishBlock(Block)) |
| 1204 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1205 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1206 | // Create the new block. |
| 1207 | Block = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1208 | |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1209 | // The Exit block is the only successor. |
| 1210 | Block->addSuccessor(&cfg->getExit()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Add the statement to the block. This may create new blocks if S contains |
| 1213 | // control-flow (short-circuit operations). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1214 | return VisitStmt(S, true); |
Ted Kremenek | 93041ba | 2008-12-09 20:20:09 +0000 | [diff] [blame] | 1215 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1216 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1217 | CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) { |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1218 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 1219 | // current block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1220 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1221 | CFGBlock* LoopSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1222 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1223 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1224 | if (!FinishBlock(Block)) |
| 1225 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1226 | LoopSuccessor = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1227 | } else |
| 1228 | LoopSuccessor = Succ; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1229 | |
| 1230 | // Because of short-circuit evaluation, the condition of the loop can span |
| 1231 | // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that |
| 1232 | // evaluate the condition. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1233 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 1234 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1235 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1236 | // Set the terminator for the "exit" condition block. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1237 | ExitConditionBlock->setTerminator(D); |
| 1238 | |
| 1239 | // Now add the actual condition to the condition block. Because the condition |
| 1240 | // itself may contain control-flow, new blocks may be created. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1241 | if (Stmt* C = D->getCond()) { |
| 1242 | Block = ExitConditionBlock; |
| 1243 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1244 | if (Block) { |
| 1245 | if (!FinishBlock(EntryConditionBlock)) |
| 1246 | return 0; |
| 1247 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1248 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1249 | |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1250 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1251 | Succ = EntryConditionBlock; |
| 1252 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1253 | // Process the loop body. |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1254 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1255 | { |
| 1256 | assert (D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1257 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1258 | // Save the current values for Block, Succ, and continue and break targets |
| 1259 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 1260 | save_continue(ContinueTargetBlock), |
| 1261 | save_break(BreakTargetBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1262 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1263 | // All continues within this loop should go to the condition block |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1264 | ContinueTargetBlock = EntryConditionBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1265 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1266 | // All breaks should go to the code following the loop. |
| 1267 | BreakTargetBlock = LoopSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1268 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1269 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 1270 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1271 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1272 | // 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] | 1273 | BodyBlock = addStmt(D->getBody()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1274 | |
Ted Kremenek | e961050 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 1275 | if (!BodyBlock) |
Ted Kremenek | 39321aa | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 1276 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1277 | else if (Block) { |
| 1278 | if (!FinishBlock(BodyBlock)) |
| 1279 | return 0; |
| 1280 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1281 | |
Ted Kremenek | cb37bb0 | 2009-04-28 04:22:00 +0000 | [diff] [blame] | 1282 | // Add an intermediate block between the BodyBlock and the |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1283 | // ExitConditionBlock to represent the "loop back" transition. Create an |
| 1284 | // empty block to represent the transition block for looping back to the |
| 1285 | // head of the loop. |
Ted Kremenek | cb37bb0 | 2009-04-28 04:22:00 +0000 | [diff] [blame] | 1286 | // FIXME: Can we do this more efficiently without adding another block? |
| 1287 | Block = NULL; |
| 1288 | Succ = BodyBlock; |
| 1289 | CFGBlock *LoopBackBlock = createBlock(); |
| 1290 | LoopBackBlock->setLoopTarget(D); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1291 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1292 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | cb37bb0 | 2009-04-28 04:22:00 +0000 | [diff] [blame] | 1293 | ExitConditionBlock->addSuccessor(LoopBackBlock); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1294 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1295 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1296 | // Link up the condition block with the code that follows the loop. |
| 1297 | // (the false branch). |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1298 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1299 | |
| 1300 | // There can be no more statements in the body block(s) since we loop back to |
| 1301 | // the body. NULL out Block to force lazy creation of another block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1302 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1303 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1304 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | a1523a3 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 1305 | Succ = BodyBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1306 | return BodyBlock; |
| 1307 | } |
| 1308 | |
| 1309 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 1310 | // "continue" is a control-flow statement. Thus we stop processing the |
| 1311 | // current block. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1312 | if (Block && !FinishBlock(Block)) |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1313 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1314 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1315 | // Now create a new block that ends with the continue statement. |
| 1316 | Block = createBlock(false); |
| 1317 | Block->setTerminator(C); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1318 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1319 | // 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] | 1320 | // incomplete AST. This means the CFG cannot be constructed. |
| 1321 | if (ContinueTargetBlock) |
| 1322 | Block->addSuccessor(ContinueTargetBlock); |
| 1323 | else |
| 1324 | badCFG = true; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1325 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1326 | return Block; |
| 1327 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1328 | |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1329 | CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, |
| 1330 | bool alwaysAdd) { |
| 1331 | |
| 1332 | if (alwaysAdd) { |
| 1333 | autoCreateBlock(); |
| 1334 | Block->appendStmt(E); |
| 1335 | } |
| 1336 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1337 | // VLA types have expressions that must be evaluated. |
| 1338 | if (E->isArgumentType()) { |
| 1339 | for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr()); |
| 1340 | VA != 0; VA = FindVA(VA->getElementType().getTypePtr())) |
| 1341 | addStmt(VA->getSizeExpr()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1342 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1343 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1344 | return Block; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1345 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1346 | |
| 1347 | /// VisitStmtExpr - Utility method to handle (nested) statement |
| 1348 | /// expressions (a GCC extension). |
Ted Kremenek | 0747de6 | 2009-07-18 00:47:21 +0000 | [diff] [blame] | 1349 | CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) { |
| 1350 | if (alwaysAdd) { |
| 1351 | autoCreateBlock(); |
| 1352 | Block->appendStmt(SE); |
| 1353 | } |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1354 | return VisitCompoundStmt(SE->getSubStmt()); |
| 1355 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1356 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1357 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1358 | // "switch" is a control-flow statement. Thus we stop processing the current |
| 1359 | // block. |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1360 | CFGBlock* SwitchSuccessor = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1361 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1362 | if (Block) { |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1363 | if (!FinishBlock(Block)) |
| 1364 | return 0; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1365 | SwitchSuccessor = Block; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1366 | } else SwitchSuccessor = Succ; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Save the current "switch" context. |
| 1369 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1370 | save_break(BreakTargetBlock), |
| 1371 | save_default(DefaultCaseBlock); |
| 1372 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1373 | // Set the "default" case to be the block after the switch statement. If the |
| 1374 | // switch statement contains a "default:", this value will be overwritten with |
| 1375 | // the block for that code. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1376 | DefaultCaseBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1377 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1378 | // Create a new block that will contain the switch statement. |
| 1379 | SwitchTerminatedBlock = createBlock(false); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1380 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1381 | // Now process the switch body. The code after the switch is the implicit |
| 1382 | // successor. |
| 1383 | Succ = SwitchSuccessor; |
| 1384 | BreakTargetBlock = SwitchSuccessor; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1385 | |
| 1386 | // When visiting the body, the case statements should automatically get linked |
| 1387 | // up to the switch. We also don't keep a pointer to the body, since all |
| 1388 | // control-flow from the switch goes to case/default statements. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1389 | assert (Terminator->getBody() && "switch must contain a non-NULL body"); |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1390 | Block = NULL; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1391 | CFGBlock *BodyBlock = addStmt(Terminator->getBody()); |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1392 | if (Block) { |
| 1393 | if (!FinishBlock(BodyBlock)) |
| 1394 | return 0; |
| 1395 | } |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1396 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1397 | // If we have no "default:" case, the default transition is to the code |
| 1398 | // following the switch body. |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1399 | SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1400 | |
Ted Kremenek | 81e1485 | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 1401 | // Add the terminator and condition in the switch block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1402 | SwitchTerminatedBlock->setTerminator(Terminator); |
| 1403 | assert (Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1404 | Block = SwitchTerminatedBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1405 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1406 | return addStmt(Terminator->getCond()); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1409 | CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1410 | // CaseStmts are essentially labels, so they are the first statement in a |
| 1411 | // block. |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 1412 | |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1413 | if (CS->getSubStmt()) |
| 1414 | addStmt(CS->getSubStmt()); |
| 1415 | |
Ted Kremenek | 55e91e8 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 1416 | CFGBlock* CaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1417 | if (!CaseBlock) |
| 1418 | CaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1419 | |
| 1420 | // Cases statements partition blocks, so this is the top of the basic block we |
| 1421 | // were processing (the "case XXX:" is the label). |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1422 | CaseBlock->setLabel(CS); |
| 1423 | |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1424 | if (!FinishBlock(CaseBlock)) |
| 1425 | return 0; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1426 | |
| 1427 | // Add this block to the list of successors for the block with the switch |
| 1428 | // statement. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1429 | assert(SwitchTerminatedBlock); |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1430 | SwitchTerminatedBlock->addSuccessor(CaseBlock); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1431 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1432 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1433 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1434 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1435 | // This block is now the implicit successor of other blocks. |
| 1436 | Succ = CaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1437 | |
Ted Kremenek | cab47bd | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 1438 | return CaseBlock; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1439 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1440 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1441 | CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1442 | if (Terminator->getSubStmt()) |
| 1443 | addStmt(Terminator->getSubStmt()); |
| 1444 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1445 | DefaultCaseBlock = Block; |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1446 | |
| 1447 | if (!DefaultCaseBlock) |
| 1448 | DefaultCaseBlock = createBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Default statements partition blocks, so this is the top of the basic block |
| 1451 | // we were processing (the "default:" is the label). |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1452 | DefaultCaseBlock->setLabel(Terminator); |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1453 | |
Ted Kremenek | 55957a8 | 2009-05-02 00:13:27 +0000 | [diff] [blame] | 1454 | if (!FinishBlock(DefaultCaseBlock)) |
| 1455 | return 0; |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1456 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1457 | // Unlike case statements, we don't add the default block to the successors |
| 1458 | // for the switch statement immediately. This is done when we finish |
| 1459 | // processing the switch statement. This allows for the default case |
| 1460 | // (including a fall-through to the code after the switch statement) to always |
| 1461 | // be the last successor of a switch-terminated block. |
| 1462 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1463 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1464 | Block = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1465 | |
Ted Kremenek | 654c78f | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 1466 | // This block is now the implicit successor of other blocks. |
| 1467 | Succ = DefaultCaseBlock; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1468 | |
| 1469 | return DefaultCaseBlock; |
Ted Kremenek | 9682be1 | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 1470 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1471 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1472 | CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1473 | // 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] | 1474 | CFGBlock* IBlock = cfg->getIndirectGotoBlock(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1475 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1476 | if (!IBlock) { |
| 1477 | IBlock = createBlock(false); |
| 1478 | cfg->setIndirectGotoBlock(IBlock); |
| 1479 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1480 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1481 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 1482 | // current block and create a new one. |
Ted Kremenek | 9366800 | 2009-07-17 22:18:43 +0000 | [diff] [blame] | 1483 | if (Block && !FinishBlock(Block)) |
| 1484 | return 0; |
| 1485 | |
Ted Kremenek | eda180e2 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1486 | Block = createBlock(false); |
| 1487 | Block->setTerminator(I); |
| 1488 | Block->addSuccessor(IBlock); |
| 1489 | return addStmt(I->getTarget()); |
| 1490 | } |
| 1491 | |
Ted Kremenek | 04cca64 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 1492 | } // end anonymous namespace |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1493 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1494 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has |
| 1495 | /// no successors or predecessors. If this is the first block created in the |
| 1496 | /// 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] | 1497 | CFGBlock* CFG::createBlock() { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1498 | bool first_block = begin() == end(); |
| 1499 | |
| 1500 | // Create the block. |
Ted Kremenek | 813dd67 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 1501 | Blocks.push_front(CFGBlock(NumBlockIDs++)); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1502 | |
| 1503 | // If this is the first block, set it as the Entry and Exit. |
| 1504 | if (first_block) Entry = Exit = &front(); |
| 1505 | |
| 1506 | // Return the block. |
| 1507 | return &front(); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1510 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 1511 | /// CFG is returned to the caller. |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 1512 | CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) { |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1513 | CFGBuilder Builder; |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 1514 | return Builder.buildCFG(Statement, C); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1518 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 1519 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1520 | //===----------------------------------------------------------------------===// |
| 1521 | // CFG: Queries for BlkExprs. |
| 1522 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1523 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1524 | namespace { |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1525 | typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1528 | static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) { |
| 1529 | if (!Terminator) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1530 | return; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1531 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1532 | 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] | 1533 | if (!*I) continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1535 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I)) |
| 1536 | if (B->isAssignmentOp()) Set.insert(B); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1537 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1538 | FindSubExprAssignments(*I, Set); |
| 1539 | } |
| 1540 | } |
| 1541 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1542 | static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { |
| 1543 | BlkExprMapTy* M = new BlkExprMapTy(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Look for assignments that are used as subexpressions. These are the only |
| 1546 | // assignments that we want to *possibly* register as a block-level |
| 1547 | // expression. Basically, if an assignment occurs both in a subexpression and |
| 1548 | // at the block-level, it is a block-level expression. |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1549 | llvm::SmallPtrSet<Expr*,50> SubExprAssignments; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1550 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1551 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) |
| 1552 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1553 | FindSubExprAssignments(*BI, SubExprAssignments); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1554 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1555 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Iterate over the statements again on identify the Expr* and Stmt* at the |
| 1558 | // block-level that are block-level expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1559 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1560 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1561 | if (Expr* Exp = dyn_cast<Expr>(*BI)) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1562 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1563 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1564 | // Assignment expressions that are not nested within another |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1565 | // expression are really "statements" whose value is never used by |
| 1566 | // another expression. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1567 | if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1568 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1569 | } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { |
| 1570 | // Special handling for statement expressions. The last statement in |
| 1571 | // the statement expression is also a block-level expr. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1572 | const CompoundStmt* C = Terminator->getSubStmt(); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1573 | if (!C->body_empty()) { |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1574 | unsigned x = M->size(); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1575 | (*M)[C->body_back()] = x; |
| 1576 | } |
| 1577 | } |
Ted Kremenek | 0cb1ba2 | 2008-01-25 23:22:27 +0000 | [diff] [blame] | 1578 | |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1579 | unsigned x = M->size(); |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1580 | (*M)[Exp] = x; |
Ted Kremenek | 95a123c | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1581 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1582 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1583 | // Look at terminators. The condition is a block-level expression. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1584 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1585 | Stmt* S = I->getTerminatorCondition(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1586 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1587 | if (S && M->find(S) == M->end()) { |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1588 | unsigned x = M->size(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1589 | (*M)[S] = x; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1590 | } |
| 1591 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1592 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1593 | return M; |
| 1594 | } |
| 1595 | |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1596 | CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { |
| 1597 | assert(S != NULL); |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1598 | if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1599 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1600 | BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); |
Ted Kremenek | 85be7cf | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1601 | BlkExprMapTy::iterator I = M->find(S); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1602 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1603 | if (I == M->end()) return CFG::BlkExprNumTy(); |
| 1604 | else return CFG::BlkExprNumTy(I->second); |
| 1605 | } |
| 1606 | |
| 1607 | unsigned CFG::getNumBlkExprs() { |
| 1608 | if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) |
| 1609 | return M->size(); |
| 1610 | else { |
| 1611 | // We assume callers interested in the number of BlkExprs will want |
| 1612 | // the map constructed if it doesn't already exist. |
| 1613 | BlkExprMap = (void*) PopulateBlkExprMap(*this); |
| 1614 | return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); |
| 1615 | } |
| 1616 | } |
| 1617 | |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1618 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6065ef6 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1619 | // Cleanup: CFG dstor. |
| 1620 | //===----------------------------------------------------------------------===// |
| 1621 | |
Ted Kremenek | f2d4372b | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1622 | CFG::~CFG() { |
| 1623 | delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); |
| 1624 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1625 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1626 | //===----------------------------------------------------------------------===// |
| 1627 | // CFG pretty printing |
| 1628 | //===----------------------------------------------------------------------===// |
| 1629 | |
Ted Kremenek | 7e776b1 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 1630 | namespace { |
| 1631 | |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 1632 | class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1633 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1634 | typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
| 1635 | StmtMapTy StmtMap; |
| 1636 | signed CurrentBlock; |
| 1637 | unsigned CurrentStmt; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1638 | const LangOptions &LangOpts; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1639 | public: |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1640 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1641 | StmtPrinterHelper(const CFG* cfg, const LangOptions &LO) |
| 1642 | : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1643 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 1644 | unsigned j = 1; |
| 1645 | for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ; |
| 1646 | BI != BEnd; ++BI, ++j ) |
| 1647 | StmtMap[*BI] = std::make_pair(I->getBlockID(),j); |
| 1648 | } |
| 1649 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1650 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1651 | virtual ~StmtPrinterHelper() {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1652 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1653 | const LangOptions &getLangOpts() const { return LangOpts; } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1654 | void setBlockID(signed i) { CurrentBlock = i; } |
| 1655 | void setStmtID(unsigned i) { CurrentStmt = i; } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1656 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1657 | virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1658 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1659 | StmtMapTy::iterator I = StmtMap.find(Terminator); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (I == StmtMap.end()) |
| 1662 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1663 | |
| 1664 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1665 | && I->second.second == CurrentStmt) |
| 1666 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1667 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1668 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 1669 | return true; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1670 | } |
| 1671 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1672 | } // end anonymous namespace |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1674 | |
| 1675 | namespace { |
Ted Kremenek | 83ebcef | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 1676 | class VISIBILITY_HIDDEN CFGBlockTerminatorPrint |
| 1677 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1678 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1679 | llvm::raw_ostream& OS; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1680 | StmtPrinterHelper* Helper; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1681 | PrintingPolicy Policy; |
| 1682 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1683 | public: |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1684 | CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper, |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1685 | const PrintingPolicy &Policy) |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1686 | : OS(os), Helper(helper), Policy(Policy) {} |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1687 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1688 | void VisitIfStmt(IfStmt* I) { |
| 1689 | OS << "if "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1690 | I->getCond()->printPretty(OS,Helper,Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1691 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1692 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1693 | // Default case. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1694 | void VisitStmt(Stmt* Terminator) { |
| 1695 | Terminator->printPretty(OS, Helper, Policy); |
| 1696 | } |
| 1697 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1698 | void VisitForStmt(ForStmt* F) { |
| 1699 | OS << "for (" ; |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1700 | if (F->getInit()) OS << "..."; |
| 1701 | OS << "; "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1702 | if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy); |
Ted Kremenek | fc7aafc | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1703 | OS << "; "; |
| 1704 | if (F->getInc()) OS << "..."; |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1705 | OS << ")"; |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1706 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1707 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1708 | void VisitWhileStmt(WhileStmt* W) { |
| 1709 | OS << "while " ; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1710 | if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1711 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1712 | |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1713 | void VisitDoStmt(DoStmt* D) { |
| 1714 | OS << "do ... while "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1715 | if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1716 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1717 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1718 | void VisitSwitchStmt(SwitchStmt* Terminator) { |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1719 | OS << "switch "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1720 | Terminator->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 9e24887 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1721 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1722 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1723 | void VisitConditionalOperator(ConditionalOperator* C) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1724 | C->getCond()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1725 | OS << " ? ... : ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1726 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1727 | |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1728 | void VisitChooseExpr(ChooseExpr* C) { |
| 1729 | OS << "__builtin_choose_expr( "; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1730 | C->getCond()->printPretty(OS, Helper, Policy); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1731 | OS << " )"; |
Ted Kremenek | 391f94a | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1732 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1733 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1734 | void VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 1735 | OS << "goto *"; |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1736 | I->getTarget()->printPretty(OS, Helper, Policy); |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1737 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1738 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1739 | void VisitBinaryOperator(BinaryOperator* B) { |
| 1740 | if (!B->isLogicalOp()) { |
| 1741 | VisitExpr(B); |
| 1742 | return; |
| 1743 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1744 | |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1745 | B->getLHS()->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1746 | |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1747 | switch (B->getOpcode()) { |
| 1748 | case BinaryOperator::LOr: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1749 | OS << " || ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1750 | return; |
| 1751 | case BinaryOperator::LAnd: |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1752 | OS << " && ..."; |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1753 | return; |
| 1754 | default: |
| 1755 | assert(false && "Invalid logical operator."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1756 | } |
Ted Kremenek | 7f7dd76 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1757 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1758 | |
Ted Kremenek | 12687ff | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 1759 | void VisitExpr(Expr* E) { |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 1760 | E->printPretty(OS, Helper, Policy); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1761 | } |
Ted Kremenek | 9aae513 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1762 | }; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1763 | } // end anonymous namespace |
| 1764 | |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1766 | static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper, |
| 1767 | Stmt* Terminator) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1768 | if (Helper) { |
| 1769 | // special printing for statement-expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1770 | if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1771 | CompoundStmt* Sub = SE->getSubStmt(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1772 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1773 | if (Sub->child_begin() != Sub->child_end()) { |
Ted Kremenek | cc77806 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1774 | OS << "({ ... ; "; |
Ted Kremenek | 6d845f0 | 2007-10-29 20:41:04 +0000 | [diff] [blame] | 1775 | Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
Ted Kremenek | cc77806 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1776 | OS << " })\n"; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1777 | return; |
| 1778 | } |
| 1779 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1780 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1781 | // special printing for comma expressions. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1782 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) { |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1783 | if (B->getOpcode() == BinaryOperator::Comma) { |
| 1784 | OS << "... , "; |
| 1785 | Helper->handledStmt(B->getRHS(),OS); |
| 1786 | OS << '\n'; |
| 1787 | return; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1790 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1792 | Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts())); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1793 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1794 | // Expressions need a newline. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1795 | if (isa<Expr>(Terminator)) OS << '\n'; |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1796 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1798 | static void print_block(llvm::raw_ostream& OS, const CFG* cfg, |
| 1799 | const CFGBlock& B, |
| 1800 | StmtPrinterHelper* Helper, bool print_edges) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1801 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1802 | if (Helper) Helper->setBlockID(B.getBlockID()); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1803 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1804 | // Print the header. |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1805 | OS << "\n [ B" << B.getBlockID(); |
| 1806 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1807 | if (&B == &cfg->getEntry()) |
| 1808 | OS << " (ENTRY) ]\n"; |
| 1809 | else if (&B == &cfg->getExit()) |
| 1810 | OS << " (EXIT) ]\n"; |
| 1811 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1812 | OS << " (INDIRECT GOTO DISPATCH) ]\n"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1813 | else |
| 1814 | OS << " ]\n"; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1815 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1816 | // Print the label of this block. |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1817 | if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1818 | |
| 1819 | if (print_edges) |
| 1820 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1821 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1822 | if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1823 | OS << L->getName(); |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1824 | else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) { |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1825 | OS << "case "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1826 | C->getLHS()->printPretty(OS, Helper, |
| 1827 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1828 | if (C->getRHS()) { |
| 1829 | OS << " ... "; |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1830 | C->getRHS()->printPretty(OS, Helper, |
| 1831 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1832 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1833 | } else if (isa<DefaultStmt>(Terminator)) |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1834 | OS << "default"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1835 | else |
| 1836 | assert(false && "Invalid label statement in CFGBlock."); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1837 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1838 | OS << ":\n"; |
| 1839 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1840 | |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1841 | // Iterate through the statements in the block and print them. |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1842 | unsigned j = 1; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1843 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1844 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 1845 | I != E ; ++I, ++j ) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1846 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1847 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1848 | if (print_edges) |
| 1849 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1850 | |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 1851 | OS << llvm::format("%3d", j) << ": "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1852 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1853 | if (Helper) |
| 1854 | Helper->setStmtID(j); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1855 | |
Ted Kremenek | f8b50e9 | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1856 | print_stmt(OS,Helper,*I); |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1857 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1858 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1859 | // Print the terminator of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1860 | if (B.getTerminator()) { |
| 1861 | if (print_edges) |
| 1862 | OS << " "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1863 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1864 | OS << " T: "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1865 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1866 | if (Helper) Helper->setBlockID(-1); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1868 | CFGBlockTerminatorPrint TPrinter(OS, Helper, |
| 1869 | PrintingPolicy(Helper->getLangOpts())); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1870 | TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1871 | OS << '\n'; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1872 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1873 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1874 | if (print_edges) { |
| 1875 | // Print the predecessors of this block. |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1876 | OS << " Predecessors (" << B.pred_size() << "):"; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1877 | unsigned i = 0; |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1878 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1879 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 1880 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1881 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1882 | if (i == 8 || (i-8) == 0) |
| 1883 | OS << "\n "; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1884 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1885 | OS << " B" << (*I)->getBlockID(); |
| 1886 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1887 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1888 | OS << '\n'; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1889 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1890 | // Print the successors of this block. |
| 1891 | OS << " Successors (" << B.succ_size() << "):"; |
| 1892 | i = 0; |
| 1893 | |
| 1894 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 1895 | I != E; ++I, ++i) { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1896 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1897 | if (i == 8 || (i-8) % 10 == 0) |
| 1898 | OS << "\n "; |
| 1899 | |
Mike Stump | 0d76d07 | 2009-07-20 23:24:15 +0000 | [diff] [blame^] | 1900 | if (*I) |
| 1901 | OS << " B" << (*I)->getBlockID(); |
| 1902 | else |
| 1903 | OS << " NULL"; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1904 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1905 | |
Ted Kremenek | 71eca01 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1906 | OS << '\n'; |
Ted Kremenek | 4aa1e8b | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1907 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1908 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1909 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1910 | |
| 1911 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1912 | void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1913 | |
| 1914 | /// 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] | 1915 | void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const { |
| 1916 | StmtPrinterHelper Helper(this, LO); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1917 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1918 | // Print the entry block. |
| 1919 | print_block(OS, this, getEntry(), &Helper, true); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1920 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1921 | // Iterate through the CFGBlocks and print them one by one. |
| 1922 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 1923 | // Skip the entry block, because we already printed it. |
| 1924 | if (&(*I) == &getEntry() || &(*I) == &getExit()) |
| 1925 | continue; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1926 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1927 | print_block(OS, this, *I, &Helper, true); |
| 1928 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1929 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1930 | // Print the exit block. |
| 1931 | print_block(OS, this, getExit(), &Helper, true); |
Ted Kremenek | e03879b | 2008-11-24 20:50:24 +0000 | [diff] [blame] | 1932 | OS.flush(); |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1933 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1934 | |
| 1935 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1936 | void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { |
| 1937 | print(llvm::errs(), cfg, LO); |
| 1938 | } |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1939 | |
| 1940 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 1941 | /// Generally this will only be called from CFG::print. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1942 | void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg, |
| 1943 | const LangOptions &LO) const { |
| 1944 | StmtPrinterHelper Helper(cfg, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1945 | print_block(OS, cfg, *this, &Helper, true); |
Ted Kremenek | 889073f | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1946 | } |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1947 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1948 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1949 | void CFGBlock::printTerminator(llvm::raw_ostream &OS, |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1950 | const LangOptions &LO) const { |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 1951 | CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO)); |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1952 | TPrinter.Visit(const_cast<Stmt*>(getTerminator())); |
| 1953 | } |
| 1954 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 1955 | Stmt* CFGBlock::getTerminatorCondition() { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1956 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1957 | if (!Terminator) |
| 1958 | return NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1959 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1960 | Expr* E = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1961 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1962 | switch (Terminator->getStmtClass()) { |
| 1963 | default: |
| 1964 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1965 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1966 | case Stmt::ForStmtClass: |
| 1967 | E = cast<ForStmt>(Terminator)->getCond(); |
| 1968 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1969 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1970 | case Stmt::WhileStmtClass: |
| 1971 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 1972 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1973 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1974 | case Stmt::DoStmtClass: |
| 1975 | E = cast<DoStmt>(Terminator)->getCond(); |
| 1976 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1977 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1978 | case Stmt::IfStmtClass: |
| 1979 | E = cast<IfStmt>(Terminator)->getCond(); |
| 1980 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1981 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1982 | case Stmt::ChooseExprClass: |
| 1983 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 1984 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1985 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1986 | case Stmt::IndirectGotoStmtClass: |
| 1987 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 1988 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1989 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1990 | case Stmt::SwitchStmtClass: |
| 1991 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 1992 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1993 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1994 | case Stmt::ConditionalOperatorClass: |
| 1995 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 1996 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 1997 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1998 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 1999 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2000 | break; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2001 | |
Ted Kremenek | 6d8b46e | 2008-11-12 21:11:49 +0000 | [diff] [blame] | 2002 | case Stmt::ObjCForCollectionStmtClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2003 | return Terminator; |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2004 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2005 | |
Ted Kremenek | c1f9a28 | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 2006 | return E ? E->IgnoreParens() : NULL; |
| 2007 | } |
| 2008 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2009 | bool CFGBlock::hasBinaryBranchTerminator() const { |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2010 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2011 | if (!Terminator) |
| 2012 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2013 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2014 | Expr* E = NULL; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2015 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2016 | switch (Terminator->getStmtClass()) { |
| 2017 | default: |
| 2018 | return false; |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2019 | |
| 2020 | case Stmt::ForStmtClass: |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2021 | case Stmt::WhileStmtClass: |
| 2022 | case Stmt::DoStmtClass: |
| 2023 | case Stmt::IfStmtClass: |
| 2024 | case Stmt::ChooseExprClass: |
| 2025 | case Stmt::ConditionalOperatorClass: |
| 2026 | case Stmt::BinaryOperatorClass: |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2027 | return true; |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2028 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2029 | |
Ted Kremenek | 92137a3 | 2008-05-16 16:06:00 +0000 | [diff] [blame] | 2030 | return E ? E->IgnoreParens() : NULL; |
| 2031 | } |
| 2032 | |
Ted Kremenek | 1564763 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 2033 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2034 | //===----------------------------------------------------------------------===// |
| 2035 | // CFG Graphviz Visualization |
| 2036 | //===----------------------------------------------------------------------===// |
| 2037 | |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2038 | |
| 2039 | #ifndef NDEBUG |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2040 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2041 | #endif |
| 2042 | |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2043 | void CFG::viewCFG(const LangOptions &LO) const { |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2044 | #ifndef NDEBUG |
Chris Lattner | c61089a | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 2045 | StmtPrinterHelper H(this, LO); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2046 | GraphHelper = &H; |
| 2047 | llvm::ViewGraph(this,"CFG"); |
| 2048 | GraphHelper = NULL; |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2049 | #endif |
| 2050 | } |
| 2051 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2052 | namespace llvm { |
| 2053 | template<> |
| 2054 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
Owen Anderson | 4d9e93c | 2009-06-24 17:37:55 +0000 | [diff] [blame] | 2055 | static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph, |
| 2056 | bool ShortNames) { |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2057 | |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 2058 | #ifndef NDEBUG |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2059 | std::string OutSStr; |
| 2060 | llvm::raw_string_ostream Out(OutSStr); |
Ted Kremenek | 04f3cee | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 2061 | print_block(Out,Graph, *Node, GraphHelper, false); |
Ted Kremenek | 2d470fc | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 2062 | std::string& OutStr = Out.str(); |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2063 | |
| 2064 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 2065 | |
| 2066 | // Process string output to make it nicer... |
| 2067 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 2068 | if (OutStr[i] == '\n') { // Left justify |
| 2069 | OutStr[i] = '\\'; |
| 2070 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 2071 | } |
Mike Stump | 31feda5 | 2009-07-17 01:31:16 +0000 | [diff] [blame] | 2072 | |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2073 | return OutStr; |
Hartmut Kaiser | 04bd2ef | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 2074 | #else |
| 2075 | return ""; |
| 2076 | #endif |
Ted Kremenek | 4e5f99d | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 2077 | } |
| 2078 | }; |
| 2079 | } // end namespace llvm |