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