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