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