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