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