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