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