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