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