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