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