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