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