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