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 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 16 | #include "clang/AST/Expr.h" |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <algorithm> |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
Ted Kremenek | d6e5060 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 26 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 27 | // the value of a variable. |
| 28 | template<typename T> |
| 29 | struct SaveAndRestore { |
| 30 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 31 | ~SaveAndRestore() { X = old_value; } |
| 32 | |
| 33 | T& X; |
| 34 | T old_value; |
| 35 | }; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 36 | |
| 37 | /// CFGBuilder - This class is implements CFG construction from an AST. |
| 38 | /// The builder is stateful: an instance of the builder should be used to only |
| 39 | /// construct a single CFG. |
| 40 | /// |
| 41 | /// Example usage: |
| 42 | /// |
| 43 | /// CFGBuilder builder; |
| 44 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 45 | /// |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 46 | /// CFG construction is done via a recursive walk of an AST. |
| 47 | /// We actually parse the AST in reverse order so that the successor |
| 48 | /// of a basic block is constructed prior to its predecessor. This |
| 49 | /// allows us to nicely capture implicit fall-throughs without extra |
| 50 | /// basic blocks. |
| 51 | /// |
| 52 | class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 53 | CFG* cfg; |
| 54 | CFGBlock* Block; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 55 | CFGBlock* Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 56 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 57 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 58 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 59 | unsigned NumBlocks; |
| 60 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 61 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 62 | LabelMapTy LabelMap; |
| 63 | |
Ted Kremenek | f5392b7 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 64 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 65 | BackpatchBlocksTy BackpatchBlocks; |
| 66 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 67 | public: |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 68 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 69 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 70 | SwitchTerminatedBlock(NULL), |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 71 | NumBlocks(0) { |
| 72 | // Create an empty CFG. |
| 73 | cfg = new CFG(); |
| 74 | } |
| 75 | |
| 76 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 78 | // buildCFG - Used by external clients to construct the CFG. |
| 79 | CFG* buildCFG(Stmt* Statement); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 80 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 81 | // Visitors to walk an AST and construct the CFG. Called by |
| 82 | // buildCFG. Do not call directly! |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 84 | CFGBlock* VisitStmt(Stmt* Statement); |
| 85 | CFGBlock* VisitNullStmt(NullStmt* Statement); |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame^] | 86 | CFGBlock* VisitParenExpr(ParenExpr* Statement); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 87 | CFGBlock* VisitCompoundStmt(CompoundStmt* C); |
| 88 | CFGBlock* VisitIfStmt(IfStmt* I); |
| 89 | CFGBlock* VisitReturnStmt(ReturnStmt* R); |
| 90 | CFGBlock* VisitLabelStmt(LabelStmt* L); |
| 91 | CFGBlock* VisitGotoStmt(GotoStmt* G); |
| 92 | CFGBlock* VisitForStmt(ForStmt* F); |
| 93 | CFGBlock* VisitWhileStmt(WhileStmt* W); |
| 94 | CFGBlock* VisitDoStmt(DoStmt* D); |
| 95 | CFGBlock* VisitContinueStmt(ContinueStmt* C); |
| 96 | CFGBlock* VisitBreakStmt(BreakStmt* B); |
| 97 | CFGBlock* VisitSwitchStmt(SwitchStmt* S); |
| 98 | CFGBlock* VisitSwitchCase(SwitchCase* S); |
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 | private: |
| 101 | CFGBlock* createBlock(bool add_successor = true); |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame^] | 102 | CFGBlock* addStmt(Stmt* S); |
| 103 | CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt); |
| 104 | CFGBlock* WalkAST_VisitChildren(Stmt* S); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 105 | void FinishBlock(CFGBlock* B); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 107 | }; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 108 | |
| 109 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
| 110 | /// represent an arbitrary statement. Examples include a single expression |
| 111 | /// or a function body (compound statement). The ownership of the returned |
| 112 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 113 | /// returns NULL. |
| 114 | CFG* CFGBuilder::buildCFG(Stmt* Statement) { |
| 115 | if (!Statement) return NULL; |
| 116 | |
| 117 | // Create an empty block that will serve as the exit block for the CFG. |
| 118 | // Since this is the first block added to the CFG, it will be implicitly |
| 119 | // registered as the exit block. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 120 | Succ = createBlock(); |
| 121 | assert (Succ == &cfg->getExit()); |
| 122 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 123 | |
| 124 | // Visit the statements and create the CFG. |
| 125 | if (CFGBlock* B = Visit(Statement)) { |
| 126 | // Finalize the last constructed block. This usually involves |
| 127 | // reversing the order of the statements in the block. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 128 | if (Block) FinishBlock(B); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 129 | |
| 130 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 131 | // when we encountered them. |
| 132 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 133 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 134 | |
| 135 | CFGBlock* B = *I; |
| 136 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 137 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 138 | |
| 139 | // If there is no target for the goto, then we are looking at an |
| 140 | // incomplete AST. Handle this by not registering a successor. |
| 141 | if (LI == LabelMap.end()) continue; |
| 142 | |
| 143 | B->addSuccessor(LI->second); |
| 144 | } |
| 145 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 146 | if (B->pred_size() > 0) { |
| 147 | // create an empty entry block that has no predecessors. |
| 148 | Succ = B; |
| 149 | cfg->setEntry(createBlock()); |
| 150 | } |
| 151 | else cfg->setEntry(B); |
| 152 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 153 | // NULL out cfg so that repeated calls |
| 154 | CFG* t = cfg; |
| 155 | cfg = NULL; |
| 156 | return t; |
| 157 | } |
| 158 | else return NULL; |
| 159 | } |
| 160 | |
| 161 | /// createBlock - Used to lazily create blocks that are connected |
| 162 | /// to the current (global) succcessor. |
| 163 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
| 164 | CFGBlock* B = cfg->createBlock(NumBlocks++); |
| 165 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 166 | return B; |
| 167 | } |
| 168 | |
| 169 | /// FinishBlock - When the last statement has been added to the block, |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 170 | /// we must reverse the statements because they have been inserted |
| 171 | /// in reverse order. |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 172 | void CFGBuilder::FinishBlock(CFGBlock* B) { |
| 173 | assert (B); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 174 | B->reverseStmts(); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame^] | 177 | /// addStmt - Used to add statements/expressions to the current CFGBlock |
| 178 | /// "Block". This method calls WalkAST on the passed statement to see if it |
| 179 | /// contains any short-circuit expressions. If so, it recursively creates |
| 180 | /// the necessary blocks for such expressions. It returns the "topmost" block |
| 181 | /// of the created blocks, or the original value of "Block" when this method |
| 182 | /// was called if no additional blocks are created. |
| 183 | CFGBlock* CFGBuilder::addStmt(Stmt* S) { |
| 184 | assert (Block); |
| 185 | return WalkAST(S,true); |
| 186 | } |
| 187 | |
| 188 | /// WalkAST - Used by addStmt to walk the subtree of a statement and |
| 189 | /// add extra blocks for ternary operators, &&, and ||. |
| 190 | CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) { |
| 191 | switch (S->getStmtClass()) { |
| 192 | case Stmt::ConditionalOperatorClass: { |
| 193 | ConditionalOperator* C = cast<ConditionalOperator>(S); |
| 194 | |
| 195 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 196 | ConfluenceBlock->appendStmt(C); |
| 197 | FinishBlock(ConfluenceBlock); |
| 198 | |
| 199 | Succ = ConfluenceBlock; |
| 200 | Block = NULL; |
| 201 | CFGBlock* LHSBlock = Visit(C->getLHS()); |
| 202 | |
| 203 | Succ = ConfluenceBlock; |
| 204 | Block = NULL; |
| 205 | CFGBlock* RHSBlock = Visit(C->getRHS()); |
| 206 | |
| 207 | Block = createBlock(false); |
| 208 | Block->addSuccessor(LHSBlock); |
| 209 | Block->addSuccessor(RHSBlock); |
| 210 | Block->setTerminator(C); |
| 211 | return addStmt(C->getCond()); |
| 212 | } |
| 213 | default: |
| 214 | if (AlwaysAddStmt) Block->appendStmt(S); |
| 215 | return WalkAST_VisitChildren(S); |
| 216 | }; |
| 217 | } |
| 218 | |
| 219 | /// WalkAST_VisitChildren - Utility method to call WalkAST on the |
| 220 | /// children of a Stmt. |
| 221 | CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) |
| 222 | { |
| 223 | CFGBlock* B = Block; |
| 224 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ; |
| 225 | I != E; ++I) |
| 226 | B = WalkAST(*I); |
| 227 | |
| 228 | return B; |
| 229 | } |
| 230 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 231 | /// VisitStmt - Handle statements with no branching control flow. |
| 232 | CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) { |
| 233 | // We cannot assume that we are in the middle of a basic block, since |
| 234 | // the CFG might only be constructed for this single statement. If |
| 235 | // we have no current basic block, just create one lazily. |
| 236 | if (!Block) Block = createBlock(); |
| 237 | |
| 238 | // Simply add the statement to the current block. We actually |
| 239 | // insert statements in reverse order; this order is reversed later |
| 240 | // when processing the containing element in the AST. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 241 | addStmt(Statement); |
| 242 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 243 | return Block; |
| 244 | } |
| 245 | |
| 246 | CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) { |
| 247 | return Block; |
| 248 | } |
| 249 | |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame^] | 250 | CFGBlock* CFGBuilder::VisitParenExpr(ParenExpr* Statement) { |
| 251 | return Visit(Statement->getSubExpr()); |
| 252 | } |
| 253 | |
| 254 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 255 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
| 256 | // The value returned from this function is the last created CFGBlock |
| 257 | // that represents the "entry" point for the translated AST node. |
| 258 | CFGBlock* LastBlock; |
| 259 | |
| 260 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
| 261 | E = C->body_rend(); I != E; ++I ) |
| 262 | // Add the statement to the current block. |
| 263 | if (!(LastBlock=Visit(*I))) |
| 264 | return NULL; |
| 265 | |
| 266 | return LastBlock; |
| 267 | } |
| 268 | |
| 269 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
| 270 | // We may see an if statement in the middle of a basic block, or |
| 271 | // it may be the first statement we are processing. In either case, |
| 272 | // we create a new basic block. First, we create the blocks for |
| 273 | // the then...else statements, and then we create the block containing |
| 274 | // the if statement. If we were in the middle of a block, we |
| 275 | // stop processing that block and reverse its statements. That block |
| 276 | // is then the implicit successor for the "then" and "else" clauses. |
| 277 | |
| 278 | // The block we were proccessing is now finished. Make it the |
| 279 | // successor block. |
| 280 | if (Block) { |
| 281 | Succ = Block; |
| 282 | FinishBlock(Block); |
| 283 | } |
| 284 | |
| 285 | // Process the false branch. NULL out Block so that the recursive |
| 286 | // call to Visit will create a new basic block. |
| 287 | // Null out Block so that all successor |
| 288 | CFGBlock* ElseBlock = Succ; |
| 289 | |
| 290 | if (Stmt* Else = I->getElse()) { |
| 291 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 292 | |
| 293 | // NULL out Block so that the recursive call to Visit will |
| 294 | // create a new basic block. |
| 295 | Block = NULL; |
| 296 | ElseBlock = Visit(Else); |
| 297 | if (!ElseBlock) return NULL; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 298 | if (Block) FinishBlock(ElseBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Process the true branch. NULL out Block so that the recursive |
| 302 | // call to Visit will create a new basic block. |
| 303 | // Null out Block so that all successor |
| 304 | CFGBlock* ThenBlock; |
| 305 | { |
| 306 | Stmt* Then = I->getThen(); |
| 307 | assert (Then); |
| 308 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 309 | Block = NULL; |
| 310 | ThenBlock = Visit(Then); |
| 311 | if (!ThenBlock) return NULL; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 312 | if (Block) FinishBlock(ThenBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Now create a new block containing the if statement. |
| 316 | Block = createBlock(false); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 317 | |
| 318 | // Set the terminator of the new block to the If statement. |
| 319 | Block->setTerminator(I); |
| 320 | |
| 321 | // Now add the successors. |
| 322 | Block->addSuccessor(ThenBlock); |
| 323 | Block->addSuccessor(ElseBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 324 | |
| 325 | // Add the condition as the last statement in the new block. This |
| 326 | // may create new blocks as the condition may contain control-flow. Any |
| 327 | // newly created blocks will be pointed to be "Block". |
| 328 | return addStmt(I->getCond()); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
| 332 | // If we were in the middle of a block we stop processing that block |
| 333 | // and reverse its statements. |
| 334 | // |
| 335 | // NOTE: If a "return" appears in the middle of a block, this means |
| 336 | // that the code afterwards is DEAD (unreachable). We still |
| 337 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 338 | // from the entry block will be able to report such dead |
| 339 | // blocks. |
| 340 | if (Block) FinishBlock(Block); |
| 341 | |
| 342 | // Create the new block. |
| 343 | Block = createBlock(false); |
| 344 | |
| 345 | // The Exit block is the only successor. |
| 346 | Block->addSuccessor(&cfg->getExit()); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 348 | // Also add the return statement as the terminator. |
| 349 | Block->setTerminator(R); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 350 | |
| 351 | // Add the return statement to the block. This may create new blocks |
| 352 | // if R contains control-flow (short-circuit operations). |
| 353 | return addStmt(R); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 357 | // Get the block of the labeled statement. Add it to our map. |
| 358 | CFGBlock* LabelBlock = Visit(L->getSubStmt()); |
| 359 | assert (LabelBlock); |
| 360 | |
| 361 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 362 | LabelMap[ L ] = LabelBlock; |
| 363 | |
| 364 | // Labels partition blocks, so this is the end of the basic block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 365 | // we were processing (the label is the first statement). Add the label |
| 366 | // the to end (really the beginning) of the block. Because this is |
| 367 | // label (and we have already processed the substatement) there is no |
| 368 | // extra control-flow to worry about. |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 369 | LabelBlock->appendStmt(L); |
| 370 | FinishBlock(LabelBlock); |
| 371 | |
| 372 | // We set Block to NULL to allow lazy creation of a new block |
| 373 | // (if necessary); |
| 374 | Block = NULL; |
| 375 | |
| 376 | // This block is now the implicit successor of other blocks. |
| 377 | Succ = LabelBlock; |
| 378 | |
| 379 | return LabelBlock; |
| 380 | } |
| 381 | |
| 382 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
| 383 | // Goto is a control-flow statement. Thus we stop processing the |
| 384 | // current block and create a new one. |
| 385 | if (Block) FinishBlock(Block); |
| 386 | Block = createBlock(false); |
| 387 | Block->setTerminator(G); |
| 388 | |
| 389 | // If we already know the mapping to the label block add the |
| 390 | // successor now. |
| 391 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 392 | |
| 393 | if (I == LabelMap.end()) |
| 394 | // We will need to backpatch this block later. |
| 395 | BackpatchBlocks.push_back(Block); |
| 396 | else |
| 397 | Block->addSuccessor(I->second); |
| 398 | |
| 399 | return Block; |
| 400 | } |
| 401 | |
| 402 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
| 403 | // "for" is a control-flow statement. Thus we stop processing the |
| 404 | // current block. |
| 405 | |
| 406 | CFGBlock* LoopSuccessor = NULL; |
| 407 | |
| 408 | if (Block) { |
| 409 | FinishBlock(Block); |
| 410 | LoopSuccessor = Block; |
| 411 | } |
| 412 | else LoopSuccessor = Succ; |
| 413 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 414 | // Because of short-circuit evaluation, the condition of the loop |
| 415 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 416 | // blocks that evaluate the condition. |
| 417 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 418 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 419 | |
| 420 | // Set the terminator for the "exit" condition block. |
| 421 | ExitConditionBlock->setTerminator(F); |
| 422 | |
| 423 | // Now add the actual condition to the condition block. Because the |
| 424 | // condition itself may contain control-flow, new blocks may be created. |
| 425 | if (Stmt* C = F->getCond()) { |
| 426 | Block = ExitConditionBlock; |
| 427 | EntryConditionBlock = addStmt(C); |
| 428 | if (Block) FinishBlock(EntryConditionBlock); |
| 429 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 430 | |
| 431 | // The condition block is the implicit successor for the loop body as |
| 432 | // well as any code above the loop. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 433 | Succ = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 434 | |
| 435 | // Now create the loop body. |
| 436 | { |
| 437 | assert (F->getBody()); |
| 438 | |
| 439 | // Save the current values for Block, Succ, and continue and break targets |
| 440 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 441 | save_continue(ContinueTargetBlock), |
| 442 | save_break(BreakTargetBlock); |
| 443 | |
| 444 | // All continues within this loop should go to the condition block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 445 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 446 | |
| 447 | // All breaks should go to the code following the loop. |
| 448 | BreakTargetBlock = LoopSuccessor; |
| 449 | |
| 450 | // Create a new block to contain the (bottom) of the loop body. |
| 451 | Block = createBlock(); |
| 452 | |
| 453 | // If we have increment code, insert it at the end of the body block. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 454 | if (Stmt* I = F->getInc()) Block = addStmt(I); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 455 | |
| 456 | // Now populate the body block, and in the process create new blocks |
| 457 | // as we walk the body of the loop. |
| 458 | CFGBlock* BodyBlock = Visit(F->getBody()); |
| 459 | assert (BodyBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 460 | if (Block) FinishBlock(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 461 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 462 | // This new body block is a successor to our "exit" condition block. |
| 463 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // Link up the condition block with the code that follows the loop. |
| 467 | // (the false branch). |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 468 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 469 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 470 | // If the loop contains initialization, create a new block for those |
| 471 | // statements. This block can also contain statements that precede |
| 472 | // the loop. |
| 473 | if (Stmt* I = F->getInit()) { |
| 474 | Block = createBlock(); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 475 | return addStmt(I); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 476 | } |
| 477 | else { |
| 478 | // There is no loop initialization. We are thus basically a while |
| 479 | // loop. NULL out Block to force lazy block construction. |
| 480 | Block = NULL; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 481 | return EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
| 486 | // "while" is a control-flow statement. Thus we stop processing the |
| 487 | // current block. |
| 488 | |
| 489 | CFGBlock* LoopSuccessor = NULL; |
| 490 | |
| 491 | if (Block) { |
| 492 | FinishBlock(Block); |
| 493 | LoopSuccessor = Block; |
| 494 | } |
| 495 | else LoopSuccessor = Succ; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 496 | |
| 497 | // Because of short-circuit evaluation, the condition of the loop |
| 498 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 499 | // blocks that evaluate the condition. |
| 500 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 501 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 502 | |
| 503 | // Set the terminator for the "exit" condition block. |
| 504 | ExitConditionBlock->setTerminator(W); |
| 505 | |
| 506 | // Now add the actual condition to the condition block. Because the |
| 507 | // condition itself may contain control-flow, new blocks may be created. |
| 508 | // Thus we update "Succ" after adding the condition. |
| 509 | if (Stmt* C = W->getCond()) { |
| 510 | Block = ExitConditionBlock; |
| 511 | EntryConditionBlock = addStmt(C); |
| 512 | if (Block) FinishBlock(EntryConditionBlock); |
| 513 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 514 | |
| 515 | // The condition block is the implicit successor for the loop body as |
| 516 | // well as any code above the loop. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 517 | Succ = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 518 | |
| 519 | // Process the loop body. |
| 520 | { |
| 521 | assert (W->getBody()); |
| 522 | |
| 523 | // Save the current values for Block, Succ, and continue and break targets |
| 524 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 525 | save_continue(ContinueTargetBlock), |
| 526 | save_break(BreakTargetBlock); |
| 527 | |
| 528 | // All continues within this loop should go to the condition block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 529 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 530 | |
| 531 | // All breaks should go to the code following the loop. |
| 532 | BreakTargetBlock = LoopSuccessor; |
| 533 | |
| 534 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 535 | Block = NULL; |
| 536 | |
| 537 | // Create the body. The returned block is the entry to the loop body. |
| 538 | CFGBlock* BodyBlock = Visit(W->getBody()); |
| 539 | assert (BodyBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 540 | if (Block) FinishBlock(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 541 | |
| 542 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 543 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // Link up the condition block with the code that follows the loop. |
| 547 | // (the false branch). |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 548 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 549 | |
| 550 | // There can be no more statements in the condition block |
| 551 | // since we loop back to this block. NULL out Block to force |
| 552 | // lazy creation of another block. |
| 553 | Block = NULL; |
| 554 | |
| 555 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 556 | return EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) { |
| 560 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 561 | // current block. |
| 562 | |
| 563 | CFGBlock* LoopSuccessor = NULL; |
| 564 | |
| 565 | if (Block) { |
| 566 | FinishBlock(Block); |
| 567 | LoopSuccessor = Block; |
| 568 | } |
| 569 | else LoopSuccessor = Succ; |
| 570 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 571 | // Because of short-circuit evaluation, the condition of the loop |
| 572 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 573 | // blocks that evaluate the condition. |
| 574 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 575 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 576 | |
| 577 | // Set the terminator for the "exit" condition block. |
| 578 | ExitConditionBlock->setTerminator(D); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 579 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 580 | // Now add the actual condition to the condition block. Because the |
| 581 | // condition itself may contain control-flow, new blocks may be created. |
| 582 | if (Stmt* C = D->getCond()) { |
| 583 | Block = ExitConditionBlock; |
| 584 | EntryConditionBlock = addStmt(C); |
| 585 | if (Block) FinishBlock(EntryConditionBlock); |
| 586 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 588 | // The condition block is the implicit successor for the loop body as |
| 589 | // well as any code above the loop. |
| 590 | Succ = EntryConditionBlock; |
| 591 | |
| 592 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 593 | // Process the loop body. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 594 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 595 | { |
| 596 | assert (D->getBody()); |
| 597 | |
| 598 | // Save the current values for Block, Succ, and continue and break targets |
| 599 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 600 | save_continue(ContinueTargetBlock), |
| 601 | save_break(BreakTargetBlock); |
| 602 | |
| 603 | // All continues within this loop should go to the condition block |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 604 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 605 | |
| 606 | // All breaks should go to the code following the loop. |
| 607 | BreakTargetBlock = LoopSuccessor; |
| 608 | |
| 609 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 610 | Block = NULL; |
| 611 | |
| 612 | // Create the body. The returned block is the entry to the loop body. |
| 613 | BodyBlock = Visit(D->getBody()); |
| 614 | assert (BodyBlock); |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 615 | if (Block) FinishBlock(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 616 | |
| 617 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 618 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // Link up the condition block with the code that follows the loop. |
| 622 | // (the false branch). |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 623 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 625 | // There can be no more statements in the body block(s) |
| 626 | // since we loop back to the body. NULL out Block to force |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 627 | // lazy creation of another block. |
| 628 | Block = NULL; |
| 629 | |
| 630 | // Return the loop body, which is the dominating block for the loop. |
| 631 | return BodyBlock; |
| 632 | } |
| 633 | |
| 634 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 635 | // "continue" is a control-flow statement. Thus we stop processing the |
| 636 | // current block. |
| 637 | if (Block) FinishBlock(Block); |
| 638 | |
| 639 | // Now create a new block that ends with the continue statement. |
| 640 | Block = createBlock(false); |
| 641 | Block->setTerminator(C); |
| 642 | |
| 643 | // If there is no target for the continue, then we are looking at an |
| 644 | // incomplete AST. Handle this by not registering a successor. |
| 645 | if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock); |
| 646 | |
| 647 | return Block; |
| 648 | } |
| 649 | |
| 650 | CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) { |
| 651 | // "break" is a control-flow statement. Thus we stop processing the |
| 652 | // current block. |
| 653 | if (Block) FinishBlock(Block); |
| 654 | |
| 655 | // Now create a new block that ends with the continue statement. |
| 656 | Block = createBlock(false); |
| 657 | Block->setTerminator(B); |
| 658 | |
| 659 | // If there is no target for the break, then we are looking at an |
| 660 | // incomplete AST. Handle this by not registering a successor. |
| 661 | if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock); |
| 662 | |
| 663 | return Block; |
| 664 | } |
| 665 | |
| 666 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) { |
| 667 | // "switch" is a control-flow statement. Thus we stop processing the |
| 668 | // current block. |
| 669 | CFGBlock* SwitchSuccessor = NULL; |
| 670 | |
| 671 | if (Block) { |
| 672 | FinishBlock(Block); |
| 673 | SwitchSuccessor = Block; |
| 674 | } |
| 675 | else SwitchSuccessor = Succ; |
| 676 | |
| 677 | // Save the current "switch" context. |
| 678 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
| 679 | save_break(BreakTargetBlock); |
| 680 | |
| 681 | // Create a new block that will contain the switch statement. |
| 682 | SwitchTerminatedBlock = createBlock(false); |
| 683 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 684 | // Now process the switch body. The code after the switch is the implicit |
| 685 | // successor. |
| 686 | Succ = SwitchSuccessor; |
| 687 | BreakTargetBlock = SwitchSuccessor; |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 688 | |
| 689 | // When visiting the body, the case statements should automatically get |
| 690 | // linked up to the switch. We also don't keep a pointer to the body, |
| 691 | // since all control-flow from the switch goes to case/default statements. |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 692 | assert (S->getBody() && "switch must contain a non-NULL body"); |
| 693 | Block = NULL; |
| 694 | CFGBlock *BodyBlock = Visit(S->getBody()); |
| 695 | if (Block) FinishBlock(BodyBlock); |
| 696 | |
| 697 | // Add the terminator and condition in the switch block. |
| 698 | SwitchTerminatedBlock->setTerminator(S); |
| 699 | assert (S->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 700 | Block = SwitchTerminatedBlock; |
Ted Kremenek | cfee50c | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 701 | return addStmt(S->getCond()); |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) { |
| 705 | // A SwitchCase is either a "default" or "case" statement. We handle |
| 706 | // both in the same way. They are essentially labels, so they are the |
| 707 | // first statement in a block. |
| 708 | CFGBlock* CaseBlock = Visit(S->getSubStmt()); |
| 709 | assert (CaseBlock); |
| 710 | |
| 711 | // Cases/Default statements parition block, so this is the top of |
| 712 | // the basic block we were processing (the case/default is the first stmt). |
| 713 | CaseBlock->appendStmt(S); |
| 714 | FinishBlock(CaseBlock); |
| 715 | |
| 716 | // Add this block to the list of successors for the block with the |
| 717 | // switch statement. |
| 718 | if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock); |
| 719 | |
| 720 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 721 | Block = NULL; |
| 722 | |
| 723 | // This block is now the implicit successor of other blocks. |
| 724 | Succ = CaseBlock; |
| 725 | |
| 726 | return CaseBlock; |
| 727 | } |
| 728 | |
| 729 | |
Ted Kremenek | d6e5060 | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 730 | } // end anonymous namespace |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 731 | |
| 732 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The |
| 733 | /// block has no successors or predecessors. If this is the first block |
| 734 | /// created in the CFG, it is automatically set to be the Entry and Exit |
| 735 | /// of the CFG. |
| 736 | CFGBlock* CFG::createBlock(unsigned blockID) { |
| 737 | bool first_block = begin() == end(); |
| 738 | |
| 739 | // Create the block. |
| 740 | Blocks.push_front(CFGBlock(blockID)); |
| 741 | |
| 742 | // If this is the first block, set it as the Entry and Exit. |
| 743 | if (first_block) Entry = Exit = &front(); |
| 744 | |
| 745 | // Return the block. |
| 746 | return &front(); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 749 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 750 | /// CFG is returned to the caller. |
| 751 | CFG* CFG::buildCFG(Stmt* Statement) { |
| 752 | CFGBuilder Builder; |
| 753 | return Builder.buildCFG(Statement); |
| 754 | } |
| 755 | |
| 756 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 757 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 758 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 759 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 760 | void CFG::dump() { print(std::cerr); } |
| 761 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 762 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 763 | void CFG::print(std::ostream& OS) { |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 764 | // Print the Entry block. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 765 | if (begin() != end()) { |
| 766 | CFGBlock& Entry = getEntry(); |
| 767 | OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n"; |
| 768 | Entry.print(OS); |
| 769 | } |
| 770 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 771 | // Iterate through the CFGBlocks and print them one by one. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 772 | for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 773 | // Skip the entry block, because we already printed it. |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 774 | if (&(*I) == &getEntry() || &(*I) == &getExit()) continue; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 775 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 776 | OS << "\n [ B" << I->getBlockID() << " ]\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 777 | I->print(OS); |
| 778 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 779 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 780 | // Print the Exit Block. |
| 781 | if (begin() != end()) { |
| 782 | CFGBlock& Exit = getExit(); |
| 783 | OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n"; |
| 784 | Exit.print(OS); |
| 785 | } |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 787 | OS << "\n"; |
| 788 | } |
| 789 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 790 | |
| 791 | namespace { |
| 792 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 793 | class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint, |
| 794 | void > { |
| 795 | std::ostream& OS; |
| 796 | public: |
| 797 | CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {} |
| 798 | |
| 799 | void VisitIfStmt(IfStmt* I) { |
| 800 | OS << "if "; |
| 801 | I->getCond()->printPretty(std::cerr); |
| 802 | OS << "\n"; |
| 803 | } |
| 804 | |
| 805 | // Default case. |
| 806 | void VisitStmt(Stmt* S) { S->printPretty(OS); } |
| 807 | |
| 808 | void VisitForStmt(ForStmt* F) { |
| 809 | OS << "for (" ; |
| 810 | if (Stmt* I = F->getInit()) I->printPretty(OS); |
| 811 | OS << " ; "; |
| 812 | if (Stmt* C = F->getCond()) C->printPretty(OS); |
| 813 | OS << " ; "; |
| 814 | if (Stmt* I = F->getInc()) I->printPretty(OS); |
| 815 | OS << ")\n"; |
| 816 | } |
| 817 | |
| 818 | void VisitWhileStmt(WhileStmt* W) { |
| 819 | OS << "while " ; |
| 820 | if (Stmt* C = W->getCond()) C->printPretty(OS); |
| 821 | OS << "\n"; |
| 822 | } |
| 823 | |
| 824 | void VisitDoStmt(DoStmt* D) { |
| 825 | OS << "do ... while "; |
| 826 | if (Stmt* C = D->getCond()) C->printPretty(OS); |
Ted Kremenek | 65cfa56 | 2007-08-27 21:27:44 +0000 | [diff] [blame^] | 827 | OS << '\n'; |
| 828 | } |
| 829 | |
| 830 | void VisitSwitchStmt(SwitchStmt* S) { |
| 831 | OS << "switch "; |
| 832 | S->getCond()->printPretty(OS); |
| 833 | OS << '\n'; |
| 834 | } |
| 835 | |
| 836 | void VisitConditionalOperator(ConditionalOperator* C) { |
| 837 | C->printPretty(OS); |
| 838 | OS << '\n'; |
| 839 | } |
| 840 | |
Ted Kremenek | 7354391 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 841 | }; |
| 842 | } // end anonymous namespace |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 843 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 844 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 845 | void CFGBlock::dump() { print(std::cerr); } |
| 846 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 847 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 848 | /// Generally this will only be called from CFG::print. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 849 | void CFGBlock::print(std::ostream& OS) { |
| 850 | |
| 851 | // Iterate through the statements in the block and print them. |
| 852 | OS << " ------------------------\n"; |
| 853 | unsigned j = 1; |
| 854 | for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) { |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 855 | // Print the statement # in the basic block. |
| 856 | OS << " " << std::setw(3) << j << ": "; |
| 857 | |
| 858 | // Print the statement/expression. |
| 859 | Stmt* S = *I; |
| 860 | |
| 861 | if (LabelStmt* L = dyn_cast<LabelStmt>(S)) |
| 862 | OS << L->getName() << ": (LABEL)\n"; |
| 863 | else |
| 864 | (*I)->printPretty(OS); |
| 865 | |
| 866 | // Expressions need a newline. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 867 | if (isa<Expr>(*I)) OS << '\n'; |
| 868 | } |
| 869 | OS << " ------------------------\n"; |
| 870 | |
| 871 | // Print the predecessors of this block. |
| 872 | OS << " Predecessors (" << pred_size() << "):"; |
| 873 | unsigned i = 0; |
| 874 | for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) { |
| 875 | if (i == 8 || (i-8) == 0) { |
| 876 | OS << "\n "; |
| 877 | } |
| 878 | OS << " B" << (*I)->getBlockID(); |
| 879 | } |
| 880 | |
| 881 | // Print the terminator of this block. |
| 882 | OS << "\n Terminator: "; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 883 | if (ControlFlowStmt) |
| 884 | CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt); |
| 885 | else |
| 886 | OS << "<NULL>\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 887 | |
| 888 | // Print the successors of this block. |
| 889 | OS << " Successors (" << succ_size() << "):"; |
| 890 | i = 0; |
| 891 | for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) { |
| 892 | if (i == 8 || (i-8) % 10 == 0) { |
| 893 | OS << "\n "; |
| 894 | } |
| 895 | OS << " B" << (*I)->getBlockID(); |
| 896 | } |
| 897 | OS << '\n'; |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 898 | } |