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