blob: 2d6e2409439721cd1e5eedbffc58c5542eae31a2 [file] [log] [blame]
Ted Kremenek97f75312007-08-21 21:42:03 +00001//===--- 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 Kremenek95e854d2007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek0edd3a92007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
21#include "llvm/Config/config.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000022#include <iostream>
23#include <iomanip>
24#include <algorithm>
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000025#include <sstream>
26
Ted Kremenek97f75312007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Ted Kremenekd6e50602007-08-23 21:26:19 +000031// SaveAndRestore - A utility class that uses RIIA to save and restore
32// the value of a variable.
33template<typename T>
34struct SaveAndRestore {
35 SaveAndRestore(T& x) : X(x), old_value(x) {}
36 ~SaveAndRestore() { X = old_value; }
37
38 T& X;
39 T old_value;
40};
Ted Kremenek97f75312007-08-21 21:42:03 +000041
42/// CFGBuilder - This class is implements CFG construction from an AST.
43/// The builder is stateful: an instance of the builder should be used to only
44/// construct a single CFG.
45///
46/// Example usage:
47///
48/// CFGBuilder builder;
49/// CFG* cfg = builder.BuildAST(stmt1);
50///
Ted Kremenek95e854d2007-08-21 22:06:14 +000051/// CFG construction is done via a recursive walk of an AST.
52/// We actually parse the AST in reverse order so that the successor
53/// of a basic block is constructed prior to its predecessor. This
54/// allows us to nicely capture implicit fall-throughs without extra
55/// basic blocks.
56///
57class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000058 CFG* cfg;
59 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000060 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000061 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000062 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000063 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000064 unsigned NumBlocks;
65
Ted Kremenek0edd3a92007-08-28 19:26:49 +000066 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenekc5de2222007-08-21 23:26:17 +000067 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
68 LabelMapTy LabelMap;
69
Ted Kremenek0edd3a92007-08-28 19:26:49 +000070 // A list of blocks that end with a "goto" that must be backpatched to
71 // their resolved targets upon completion of CFG construction.
Ted Kremenekf5392b72007-08-22 15:40:58 +000072 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000073 BackpatchBlocksTy BackpatchBlocks;
74
Ted Kremenek0edd3a92007-08-28 19:26:49 +000075 // A list of labels whose address has been taken (for indirect gotos).
76 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
77 LabelSetTy AddressTakenLabels;
78
Ted Kremenek97f75312007-08-21 21:42:03 +000079public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000080 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000081 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremeneke809ebf2007-08-23 18:43:24 +000082 SwitchTerminatedBlock(NULL),
Ted Kremenek97f75312007-08-21 21:42:03 +000083 NumBlocks(0) {
84 // Create an empty CFG.
85 cfg = new CFG();
86 }
87
88 ~CFGBuilder() { delete cfg; }
Ted Kremenek97f75312007-08-21 21:42:03 +000089
Ted Kremenek73543912007-08-23 21:42:29 +000090 // buildCFG - Used by external clients to construct the CFG.
91 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +000092
Ted Kremenek73543912007-08-23 21:42:29 +000093 // Visitors to walk an AST and construct the CFG. Called by
94 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +000095
Ted Kremenek73543912007-08-23 21:42:29 +000096 CFGBlock* VisitStmt(Stmt* Statement);
97 CFGBlock* VisitNullStmt(NullStmt* Statement);
98 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
99 CFGBlock* VisitIfStmt(IfStmt* I);
100 CFGBlock* VisitReturnStmt(ReturnStmt* R);
101 CFGBlock* VisitLabelStmt(LabelStmt* L);
102 CFGBlock* VisitGotoStmt(GotoStmt* G);
103 CFGBlock* VisitForStmt(ForStmt* F);
104 CFGBlock* VisitWhileStmt(WhileStmt* W);
105 CFGBlock* VisitDoStmt(DoStmt* D);
106 CFGBlock* VisitContinueStmt(ContinueStmt* C);
107 CFGBlock* VisitBreakStmt(BreakStmt* B);
108 CFGBlock* VisitSwitchStmt(SwitchStmt* S);
109 CFGBlock* VisitSwitchCase(SwitchCase* S);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000110 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek97f75312007-08-21 21:42:03 +0000111
Ted Kremenek73543912007-08-23 21:42:29 +0000112private:
113 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000114 CFGBlock* addStmt(Stmt* S);
115 CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt);
116 CFGBlock* WalkAST_VisitChildren(Stmt* S);
Ted Kremeneke822b622007-08-28 18:14:37 +0000117 CFGBlock* WalkAST_VisitVarDecl(VarDecl* D);
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000118 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* S);
Ted Kremenek73543912007-08-23 21:42:29 +0000119 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000120
Ted Kremenek97f75312007-08-21 21:42:03 +0000121};
Ted Kremenek73543912007-08-23 21:42:29 +0000122
123/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
124/// represent an arbitrary statement. Examples include a single expression
125/// or a function body (compound statement). The ownership of the returned
126/// CFG is transferred to the caller. If CFG construction fails, this method
127/// returns NULL.
128CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000129 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000130 if (!Statement) return NULL;
131
132 // Create an empty block that will serve as the exit block for the CFG.
133 // Since this is the first block added to the CFG, it will be implicitly
134 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000135 Succ = createBlock();
136 assert (Succ == &cfg->getExit());
137 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000138
139 // Visit the statements and create the CFG.
140 if (CFGBlock* B = Visit(Statement)) {
141 // Finalize the last constructed block. This usually involves
142 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000143 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000144
145 // Backpatch the gotos whose label -> block mappings we didn't know
146 // when we encountered them.
147 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
148 E = BackpatchBlocks.end(); I != E; ++I ) {
149
150 CFGBlock* B = *I;
151 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
152 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
153
154 // If there is no target for the goto, then we are looking at an
155 // incomplete AST. Handle this by not registering a successor.
156 if (LI == LabelMap.end()) continue;
157
158 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000159 }
Ted Kremenek73543912007-08-23 21:42:29 +0000160
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000161 // Add successors to the Indirect Goto Dispatch block (if we have one).
162 if (CFGBlock* B = cfg->getIndirectGotoBlock())
163 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
164 E = AddressTakenLabels.end(); I != E; ++I ) {
165
166 // Lookup the target block.
167 LabelMapTy::iterator LI = LabelMap.find(*I);
168
169 // If there is no target block that contains label, then we are looking
170 // at an incomplete AST. Handle this by not registering a successor.
171 if (LI == LabelMap.end()) continue;
172
173 B->addSuccessor(LI->second);
174 }
175
176 // Create an empty entry block that has no predecessors.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000177 if (B->pred_size() > 0) {
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000178 Succ = B;
179 cfg->setEntry(createBlock());
180 }
181 else cfg->setEntry(B);
182
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000183 // NULL out cfg so that repeated calls to the builder will fail and that
184 // the ownership of the constructed CFG is passed to the caller.
Ted Kremenek73543912007-08-23 21:42:29 +0000185 CFG* t = cfg;
186 cfg = NULL;
187 return t;
188 }
189 else return NULL;
190}
191
192/// createBlock - Used to lazily create blocks that are connected
193/// to the current (global) succcessor.
194CFGBlock* CFGBuilder::createBlock(bool add_successor) {
195 CFGBlock* B = cfg->createBlock(NumBlocks++);
196 if (add_successor && Succ) B->addSuccessor(Succ);
197 return B;
198}
199
200/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000201/// we must reverse the statements because they have been inserted
202/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000203void CFGBuilder::FinishBlock(CFGBlock* B) {
204 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000205 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000206}
207
Ted Kremenek65cfa562007-08-27 21:27:44 +0000208/// addStmt - Used to add statements/expressions to the current CFGBlock
209/// "Block". This method calls WalkAST on the passed statement to see if it
210/// contains any short-circuit expressions. If so, it recursively creates
211/// the necessary blocks for such expressions. It returns the "topmost" block
212/// of the created blocks, or the original value of "Block" when this method
213/// was called if no additional blocks are created.
214CFGBlock* CFGBuilder::addStmt(Stmt* S) {
215 assert (Block);
216 return WalkAST(S,true);
217}
218
219/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000220/// add extra blocks for ternary operators, &&, and ||. We also
221/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek65cfa562007-08-27 21:27:44 +0000222CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) {
223 switch (S->getStmtClass()) {
224 case Stmt::ConditionalOperatorClass: {
225 ConditionalOperator* C = cast<ConditionalOperator>(S);
226
227 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
228 ConfluenceBlock->appendStmt(C);
229 FinishBlock(ConfluenceBlock);
230
231 Succ = ConfluenceBlock;
232 Block = NULL;
233 CFGBlock* LHSBlock = Visit(C->getLHS());
234
235 Succ = ConfluenceBlock;
236 Block = NULL;
237 CFGBlock* RHSBlock = Visit(C->getRHS());
238
239 Block = createBlock(false);
240 Block->addSuccessor(LHSBlock);
241 Block->addSuccessor(RHSBlock);
242 Block->setTerminator(C);
243 return addStmt(C->getCond());
244 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000245
Ted Kremeneke822b622007-08-28 18:14:37 +0000246 case Stmt::DeclStmtClass:
247 if (VarDecl* V = dyn_cast<VarDecl>(cast<DeclStmt>(S)->getDecl())) {
248 Block->appendStmt(S);
249 return WalkAST_VisitVarDecl(V);
250 }
251 else return Block;
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000252
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000253 case Stmt::AddrLabelExprClass: {
254 AddrLabelExpr* A = cast<AddrLabelExpr>(S);
255 AddressTakenLabels.insert(A->getLabel());
256
257 if (AlwaysAddStmt) Block->appendStmt(S);
258 return Block;
259 }
260
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000261 case Stmt::StmtExprClass:
262 return WalkAST_VisitStmtExpr(cast<StmtExpr>(S));
Ted Kremeneke822b622007-08-28 18:14:37 +0000263
Ted Kremenekcfaae762007-08-27 21:54:41 +0000264 case Stmt::BinaryOperatorClass: {
265 BinaryOperator* B = cast<BinaryOperator>(S);
266
267 if (B->isLogicalOp()) { // && or ||
268 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
269 ConfluenceBlock->appendStmt(B);
270 FinishBlock(ConfluenceBlock);
271
272 // create the block evaluating the LHS
273 CFGBlock* LHSBlock = createBlock(false);
274 LHSBlock->addSuccessor(ConfluenceBlock);
275 LHSBlock->setTerminator(B);
276
277 // create the block evaluating the RHS
278 Succ = ConfluenceBlock;
279 Block = NULL;
280 CFGBlock* RHSBlock = Visit(B->getRHS());
281 LHSBlock->addSuccessor(RHSBlock);
282
283 // Generate the blocks for evaluating the LHS.
284 Block = LHSBlock;
285 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000286 }
287 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
288 Block->appendStmt(B);
289 addStmt(B->getRHS());
290 return addStmt(B->getLHS());
Ted Kremenekcfaae762007-08-27 21:54:41 +0000291 }
292
293 // Fall through to the default case.
294 }
295
Ted Kremenek65cfa562007-08-27 21:27:44 +0000296 default:
297 if (AlwaysAddStmt) Block->appendStmt(S);
298 return WalkAST_VisitChildren(S);
299 };
300}
301
Ted Kremeneke822b622007-08-28 18:14:37 +0000302/// WalkAST_VisitVarDecl - Utility method to handle VarDecls contained in
303/// DeclStmts. Because the initialization code for declarations can
304/// contain arbitrary expressions, we must linearize declarations
305/// to handle arbitrary control-flow induced by those expressions.
306CFGBlock* CFGBuilder::WalkAST_VisitVarDecl(VarDecl* V) {
307 // We actually must parse the LAST declaration in a chain of
308 // declarations first, because we are building the CFG in reverse
309 // order.
310 if (Decl* D = V->getNextDeclarator())
311 if (VarDecl* Next = cast<VarDecl>(D))
312 Block = WalkAST_VisitVarDecl(Next);
313
314 if (Expr* E = V->getInit())
315 return addStmt(E);
316
317 assert (Block);
318 return Block;
319}
320
Ted Kremenek65cfa562007-08-27 21:27:44 +0000321/// WalkAST_VisitChildren - Utility method to call WalkAST on the
322/// children of a Stmt.
Ted Kremenekcfaae762007-08-27 21:54:41 +0000323CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000324 CFGBlock* B = Block;
325 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
326 I != E; ++I)
327 B = WalkAST(*I);
328
329 return B;
330}
331
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000332/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
333/// expressions (a GCC extension).
334CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* S) {
335 Block->appendStmt(S);
336 return VisitCompoundStmt(S->getSubStmt());
337}
338
Ted Kremenek73543912007-08-23 21:42:29 +0000339/// VisitStmt - Handle statements with no branching control flow.
340CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
341 // We cannot assume that we are in the middle of a basic block, since
342 // the CFG might only be constructed for this single statement. If
343 // we have no current basic block, just create one lazily.
344 if (!Block) Block = createBlock();
345
346 // Simply add the statement to the current block. We actually
347 // insert statements in reverse order; this order is reversed later
348 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000349 addStmt(Statement);
350
Ted Kremenek73543912007-08-23 21:42:29 +0000351 return Block;
352}
353
354CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
355 return Block;
356}
357
358CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
359 // The value returned from this function is the last created CFGBlock
360 // that represents the "entry" point for the translated AST node.
361 CFGBlock* LastBlock;
362
363 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
364 E = C->body_rend(); I != E; ++I )
365 // Add the statement to the current block.
366 if (!(LastBlock=Visit(*I)))
367 return NULL;
368
369 return LastBlock;
370}
371
372CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
373 // We may see an if statement in the middle of a basic block, or
374 // it may be the first statement we are processing. In either case,
375 // we create a new basic block. First, we create the blocks for
376 // the then...else statements, and then we create the block containing
377 // the if statement. If we were in the middle of a block, we
378 // stop processing that block and reverse its statements. That block
379 // is then the implicit successor for the "then" and "else" clauses.
380
381 // The block we were proccessing is now finished. Make it the
382 // successor block.
383 if (Block) {
384 Succ = Block;
385 FinishBlock(Block);
386 }
387
388 // Process the false branch. NULL out Block so that the recursive
389 // call to Visit will create a new basic block.
390 // Null out Block so that all successor
391 CFGBlock* ElseBlock = Succ;
392
393 if (Stmt* Else = I->getElse()) {
394 SaveAndRestore<CFGBlock*> sv(Succ);
395
396 // NULL out Block so that the recursive call to Visit will
397 // create a new basic block.
398 Block = NULL;
399 ElseBlock = Visit(Else);
400 if (!ElseBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000401 if (Block) FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000402 }
403
404 // Process the true branch. NULL out Block so that the recursive
405 // call to Visit will create a new basic block.
406 // Null out Block so that all successor
407 CFGBlock* ThenBlock;
408 {
409 Stmt* Then = I->getThen();
410 assert (Then);
411 SaveAndRestore<CFGBlock*> sv(Succ);
412 Block = NULL;
413 ThenBlock = Visit(Then);
414 if (!ThenBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000415 if (Block) FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000416 }
417
418 // Now create a new block containing the if statement.
419 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000420
421 // Set the terminator of the new block to the If statement.
422 Block->setTerminator(I);
423
424 // Now add the successors.
425 Block->addSuccessor(ThenBlock);
426 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000427
428 // Add the condition as the last statement in the new block. This
429 // may create new blocks as the condition may contain control-flow. Any
430 // newly created blocks will be pointed to be "Block".
431 return addStmt(I->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000432}
433
434CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
435 // If we were in the middle of a block we stop processing that block
436 // and reverse its statements.
437 //
438 // NOTE: If a "return" appears in the middle of a block, this means
439 // that the code afterwards is DEAD (unreachable). We still
440 // keep a basic block for that code; a simple "mark-and-sweep"
441 // from the entry block will be able to report such dead
442 // blocks.
443 if (Block) FinishBlock(Block);
444
445 // Create the new block.
446 Block = createBlock(false);
447
448 // The Exit block is the only successor.
449 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000450
451 // Add the return statement to the block. This may create new blocks
452 // if R contains control-flow (short-circuit operations).
453 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000454}
455
456CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
457 // Get the block of the labeled statement. Add it to our map.
458 CFGBlock* LabelBlock = Visit(L->getSubStmt());
459 assert (LabelBlock);
460
461 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
462 LabelMap[ L ] = LabelBlock;
463
464 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000465 // we were processing (the label is the first statement). Add the label
466 // the to end (really the beginning) of the block. Because this is
467 // label (and we have already processed the substatement) there is no
468 // extra control-flow to worry about.
Ted Kremenek73543912007-08-23 21:42:29 +0000469 LabelBlock->appendStmt(L);
470 FinishBlock(LabelBlock);
471
472 // We set Block to NULL to allow lazy creation of a new block
473 // (if necessary);
474 Block = NULL;
475
476 // This block is now the implicit successor of other blocks.
477 Succ = LabelBlock;
478
479 return LabelBlock;
480}
481
482CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
483 // Goto is a control-flow statement. Thus we stop processing the
484 // current block and create a new one.
485 if (Block) FinishBlock(Block);
486 Block = createBlock(false);
487 Block->setTerminator(G);
488
489 // If we already know the mapping to the label block add the
490 // successor now.
491 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
492
493 if (I == LabelMap.end())
494 // We will need to backpatch this block later.
495 BackpatchBlocks.push_back(Block);
496 else
497 Block->addSuccessor(I->second);
498
499 return Block;
500}
501
502CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
503 // "for" is a control-flow statement. Thus we stop processing the
504 // current block.
505
506 CFGBlock* LoopSuccessor = NULL;
507
508 if (Block) {
509 FinishBlock(Block);
510 LoopSuccessor = Block;
511 }
512 else LoopSuccessor = Succ;
513
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000514 // Because of short-circuit evaluation, the condition of the loop
515 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
516 // blocks that evaluate the condition.
517 CFGBlock* ExitConditionBlock = createBlock(false);
518 CFGBlock* EntryConditionBlock = ExitConditionBlock;
519
520 // Set the terminator for the "exit" condition block.
521 ExitConditionBlock->setTerminator(F);
522
523 // Now add the actual condition to the condition block. Because the
524 // condition itself may contain control-flow, new blocks may be created.
525 if (Stmt* C = F->getCond()) {
526 Block = ExitConditionBlock;
527 EntryConditionBlock = addStmt(C);
528 if (Block) FinishBlock(EntryConditionBlock);
529 }
Ted Kremenek73543912007-08-23 21:42:29 +0000530
531 // The condition block is the implicit successor for the loop body as
532 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000533 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000534
535 // Now create the loop body.
536 {
537 assert (F->getBody());
538
539 // Save the current values for Block, Succ, and continue and break targets
540 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
541 save_continue(ContinueTargetBlock),
542 save_break(BreakTargetBlock);
543
544 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000545 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000546
547 // All breaks should go to the code following the loop.
548 BreakTargetBlock = LoopSuccessor;
549
550 // Create a new block to contain the (bottom) of the loop body.
551 Block = createBlock();
552
553 // If we have increment code, insert it at the end of the body block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000554 if (Stmt* I = F->getInc()) Block = addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000555
556 // Now populate the body block, and in the process create new blocks
557 // as we walk the body of the loop.
558 CFGBlock* BodyBlock = Visit(F->getBody());
559 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000560 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000561
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000562 // This new body block is a successor to our "exit" condition block.
563 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000564 }
565
566 // Link up the condition block with the code that follows the loop.
567 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000568 ExitConditionBlock->addSuccessor(LoopSuccessor);
569
Ted Kremenek73543912007-08-23 21:42:29 +0000570 // If the loop contains initialization, create a new block for those
571 // statements. This block can also contain statements that precede
572 // the loop.
573 if (Stmt* I = F->getInit()) {
574 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000575 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000576 }
577 else {
578 // There is no loop initialization. We are thus basically a while
579 // loop. NULL out Block to force lazy block construction.
580 Block = NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000581 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000582 }
583}
584
585CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
586 // "while" is a control-flow statement. Thus we stop processing the
587 // current block.
588
589 CFGBlock* LoopSuccessor = NULL;
590
591 if (Block) {
592 FinishBlock(Block);
593 LoopSuccessor = Block;
594 }
595 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000596
597 // Because of short-circuit evaluation, the condition of the loop
598 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
599 // blocks that evaluate the condition.
600 CFGBlock* ExitConditionBlock = createBlock(false);
601 CFGBlock* EntryConditionBlock = ExitConditionBlock;
602
603 // Set the terminator for the "exit" condition block.
604 ExitConditionBlock->setTerminator(W);
605
606 // Now add the actual condition to the condition block. Because the
607 // condition itself may contain control-flow, new blocks may be created.
608 // Thus we update "Succ" after adding the condition.
609 if (Stmt* C = W->getCond()) {
610 Block = ExitConditionBlock;
611 EntryConditionBlock = addStmt(C);
612 if (Block) FinishBlock(EntryConditionBlock);
613 }
Ted Kremenek73543912007-08-23 21:42:29 +0000614
615 // The condition block is the implicit successor for the loop body as
616 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000617 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000618
619 // Process the loop body.
620 {
621 assert (W->getBody());
622
623 // Save the current values for Block, Succ, and continue and break targets
624 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
625 save_continue(ContinueTargetBlock),
626 save_break(BreakTargetBlock);
627
628 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000629 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000630
631 // All breaks should go to the code following the loop.
632 BreakTargetBlock = LoopSuccessor;
633
634 // NULL out Block to force lazy instantiation of blocks for the body.
635 Block = NULL;
636
637 // Create the body. The returned block is the entry to the loop body.
638 CFGBlock* BodyBlock = Visit(W->getBody());
639 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000640 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000641
642 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000643 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000644 }
645
646 // Link up the condition block with the code that follows the loop.
647 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000648 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000649
650 // There can be no more statements in the condition block
651 // since we loop back to this block. NULL out Block to force
652 // lazy creation of another block.
653 Block = NULL;
654
655 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000656 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000657}
658
659CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
660 // "do...while" is a control-flow statement. Thus we stop processing the
661 // current block.
662
663 CFGBlock* LoopSuccessor = NULL;
664
665 if (Block) {
666 FinishBlock(Block);
667 LoopSuccessor = Block;
668 }
669 else LoopSuccessor = Succ;
670
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000671 // Because of short-circuit evaluation, the condition of the loop
672 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
673 // blocks that evaluate the condition.
674 CFGBlock* ExitConditionBlock = createBlock(false);
675 CFGBlock* EntryConditionBlock = ExitConditionBlock;
676
677 // Set the terminator for the "exit" condition block.
678 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000679
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000680 // Now add the actual condition to the condition block. Because the
681 // condition itself may contain control-flow, new blocks may be created.
682 if (Stmt* C = D->getCond()) {
683 Block = ExitConditionBlock;
684 EntryConditionBlock = addStmt(C);
685 if (Block) FinishBlock(EntryConditionBlock);
686 }
Ted Kremenek73543912007-08-23 21:42:29 +0000687
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000688 // The condition block is the implicit successor for the loop body as
689 // well as any code above the loop.
690 Succ = EntryConditionBlock;
691
692
Ted Kremenek73543912007-08-23 21:42:29 +0000693 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000694 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000695 {
696 assert (D->getBody());
697
698 // Save the current values for Block, Succ, and continue and break targets
699 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
700 save_continue(ContinueTargetBlock),
701 save_break(BreakTargetBlock);
702
703 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000704 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000705
706 // All breaks should go to the code following the loop.
707 BreakTargetBlock = LoopSuccessor;
708
709 // NULL out Block to force lazy instantiation of blocks for the body.
710 Block = NULL;
711
712 // Create the body. The returned block is the entry to the loop body.
713 BodyBlock = Visit(D->getBody());
714 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000715 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000716
717 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000718 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000719 }
720
721 // Link up the condition block with the code that follows the loop.
722 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000723 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000724
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000725 // There can be no more statements in the body block(s)
726 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +0000727 // lazy creation of another block.
728 Block = NULL;
729
730 // Return the loop body, which is the dominating block for the loop.
731 return BodyBlock;
732}
733
734CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
735 // "continue" is a control-flow statement. Thus we stop processing the
736 // current block.
737 if (Block) FinishBlock(Block);
738
739 // Now create a new block that ends with the continue statement.
740 Block = createBlock(false);
741 Block->setTerminator(C);
742
743 // If there is no target for the continue, then we are looking at an
744 // incomplete AST. Handle this by not registering a successor.
745 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
746
747 return Block;
748}
749
750CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
751 // "break" is a control-flow statement. Thus we stop processing the
752 // current block.
753 if (Block) FinishBlock(Block);
754
755 // Now create a new block that ends with the continue statement.
756 Block = createBlock(false);
757 Block->setTerminator(B);
758
759 // If there is no target for the break, then we are looking at an
760 // incomplete AST. Handle this by not registering a successor.
761 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
762
763 return Block;
764}
765
766CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) {
767 // "switch" is a control-flow statement. Thus we stop processing the
768 // current block.
769 CFGBlock* SwitchSuccessor = NULL;
770
771 if (Block) {
772 FinishBlock(Block);
773 SwitchSuccessor = Block;
774 }
775 else SwitchSuccessor = Succ;
776
777 // Save the current "switch" context.
778 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
779 save_break(BreakTargetBlock);
780
781 // Create a new block that will contain the switch statement.
782 SwitchTerminatedBlock = createBlock(false);
783
Ted Kremenek73543912007-08-23 21:42:29 +0000784 // Now process the switch body. The code after the switch is the implicit
785 // successor.
786 Succ = SwitchSuccessor;
787 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000788
789 // When visiting the body, the case statements should automatically get
790 // linked up to the switch. We also don't keep a pointer to the body,
791 // since all control-flow from the switch goes to case/default statements.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000792 assert (S->getBody() && "switch must contain a non-NULL body");
793 Block = NULL;
794 CFGBlock *BodyBlock = Visit(S->getBody());
795 if (Block) FinishBlock(BodyBlock);
796
797 // Add the terminator and condition in the switch block.
798 SwitchTerminatedBlock->setTerminator(S);
799 assert (S->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +0000800 Block = SwitchTerminatedBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000801 return addStmt(S->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000802}
803
804CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) {
805 // A SwitchCase is either a "default" or "case" statement. We handle
806 // both in the same way. They are essentially labels, so they are the
807 // first statement in a block.
808 CFGBlock* CaseBlock = Visit(S->getSubStmt());
809 assert (CaseBlock);
810
811 // Cases/Default statements parition block, so this is the top of
812 // the basic block we were processing (the case/default is the first stmt).
813 CaseBlock->appendStmt(S);
814 FinishBlock(CaseBlock);
815
816 // Add this block to the list of successors for the block with the
817 // switch statement.
818 if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock);
819
820 // We set Block to NULL to allow lazy creation of a new block (if necessary)
821 Block = NULL;
822
823 // This block is now the implicit successor of other blocks.
824 Succ = CaseBlock;
825
826 return CaseBlock;
827}
828
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000829CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
830 // Lazily create the indirect-goto dispatch block if there isn't one
831 // already.
832 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
833
834 if (!IBlock) {
835 IBlock = createBlock(false);
836 cfg->setIndirectGotoBlock(IBlock);
837 }
838
839 // IndirectGoto is a control-flow statement. Thus we stop processing the
840 // current block and create a new one.
841 if (Block) FinishBlock(Block);
842 Block = createBlock(false);
843 Block->setTerminator(I);
844 Block->addSuccessor(IBlock);
845 return addStmt(I->getTarget());
846}
847
Ted Kremenek73543912007-08-23 21:42:29 +0000848
Ted Kremenekd6e50602007-08-23 21:26:19 +0000849} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +0000850
851/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
852/// block has no successors or predecessors. If this is the first block
853/// created in the CFG, it is automatically set to be the Entry and Exit
854/// of the CFG.
855CFGBlock* CFG::createBlock(unsigned blockID) {
856 bool first_block = begin() == end();
857
858 // Create the block.
859 Blocks.push_front(CFGBlock(blockID));
860
861 // If this is the first block, set it as the Entry and Exit.
862 if (first_block) Entry = Exit = &front();
863
864 // Return the block.
865 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +0000866}
867
Ted Kremenek4db5b452007-08-23 16:51:22 +0000868/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
869/// CFG is returned to the caller.
870CFG* CFG::buildCFG(Stmt* Statement) {
871 CFGBuilder Builder;
872 return Builder.buildCFG(Statement);
873}
874
875/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +0000876void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
877
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000878
879//===----------------------------------------------------------------------===//
880// CFG pretty printing
881//===----------------------------------------------------------------------===//
882
Ted Kremenek4db5b452007-08-23 16:51:22 +0000883/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000884void CFG::dump() const { print(std::cerr); }
Ted Kremenek97f75312007-08-21 21:42:03 +0000885
Ted Kremenek4db5b452007-08-23 16:51:22 +0000886/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000887void CFG::print(std::ostream& OS) const {
888
889 // Print the entry block.
890 getEntry().print(OS,this);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000891
Ted Kremenek4db5b452007-08-23 16:51:22 +0000892 // Iterate through the CFGBlocks and print them one by one.
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000893 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000894 // Skip the entry block, because we already printed it.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000895 if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000896 I->print(OS,this);
Ted Kremenek97f75312007-08-21 21:42:03 +0000897 }
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000898
899 // Print the exit block.
900 getExit().print(OS,this);
Ted Kremenek73543912007-08-23 21:42:29 +0000901
Ted Kremenek97f75312007-08-21 21:42:03 +0000902 OS << "\n";
903}
904
Ted Kremenekd8313202007-08-22 18:22:34 +0000905namespace {
906
Ted Kremenek73543912007-08-23 21:42:29 +0000907class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
908 void > {
909 std::ostream& OS;
910public:
911 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
912
913 void VisitIfStmt(IfStmt* I) {
914 OS << "if ";
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000915 I->getCond()->printPretty(OS);
Ted Kremenek73543912007-08-23 21:42:29 +0000916 OS << "\n";
917 }
918
919 // Default case.
920 void VisitStmt(Stmt* S) { S->printPretty(OS); }
921
922 void VisitForStmt(ForStmt* F) {
923 OS << "for (" ;
924 if (Stmt* I = F->getInit()) I->printPretty(OS);
925 OS << " ; ";
926 if (Stmt* C = F->getCond()) C->printPretty(OS);
927 OS << " ; ";
928 if (Stmt* I = F->getInc()) I->printPretty(OS);
929 OS << ")\n";
930 }
931
932 void VisitWhileStmt(WhileStmt* W) {
933 OS << "while " ;
934 if (Stmt* C = W->getCond()) C->printPretty(OS);
935 OS << "\n";
936 }
937
938 void VisitDoStmt(DoStmt* D) {
939 OS << "do ... while ";
940 if (Stmt* C = D->getCond()) C->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000941 OS << '\n';
942 }
943
944 void VisitSwitchStmt(SwitchStmt* S) {
945 OS << "switch ";
946 S->getCond()->printPretty(OS);
947 OS << '\n';
948 }
949
Ted Kremenekcfaae762007-08-27 21:54:41 +0000950 void VisitExpr(Expr* E) {
951 E->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000952 OS << '\n';
Ted Kremenekcfaae762007-08-27 21:54:41 +0000953 }
Ted Kremenek73543912007-08-23 21:42:29 +0000954};
955} // end anonymous namespace
Ted Kremenekd8313202007-08-22 18:22:34 +0000956
Ted Kremenek4db5b452007-08-23 16:51:22 +0000957/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000958void CFGBlock::dump(const CFG* cfg) const { print(std::cerr,cfg); }
Ted Kremenek97f75312007-08-21 21:42:03 +0000959
Ted Kremenek4db5b452007-08-23 16:51:22 +0000960/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
961/// Generally this will only be called from CFG::print.
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000962void CFGBlock::print(std::ostream& OS, const CFG* cfg) const {
963
964 // Print the header.
965 OS << "\n [ B" << getBlockID();
966 if (this == &cfg->getEntry()) { OS << " (ENTRY) ]\n"; }
967 else if (this == &cfg->getExit()) { OS << " (EXIT) ]\n"; }
968 else if (this == cfg->getIndirectGotoBlock()) {
969 OS << " (INDIRECT GOTO DISPATCH) ]\n";
970 }
971 else OS << " ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000972
973 // Iterate through the statements in the block and print them.
974 OS << " ------------------------\n";
975 unsigned j = 1;
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000976 for (const_iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000977 // Print the statement # in the basic block.
978 OS << " " << std::setw(3) << j << ": ";
979
980 // Print the statement/expression.
981 Stmt* S = *I;
982
983 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
984 OS << L->getName() << ": (LABEL)\n";
985 else
986 (*I)->printPretty(OS);
987
988 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000989 if (isa<Expr>(*I)) OS << '\n';
990 }
991 OS << " ------------------------\n";
992
993 // Print the predecessors of this block.
994 OS << " Predecessors (" << pred_size() << "):";
995 unsigned i = 0;
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000996 for (const_pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i) {
Ted Kremenek97f75312007-08-21 21:42:03 +0000997 if (i == 8 || (i-8) == 0) {
998 OS << "\n ";
999 }
1000 OS << " B" << (*I)->getBlockID();
1001 }
1002
1003 // Print the terminator of this block.
1004 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +00001005 if (ControlFlowStmt)
1006 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
1007 else
1008 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +00001009
1010 // Print the successors of this block.
1011 OS << " Successors (" << succ_size() << "):";
1012 i = 0;
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001013 for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i) {
Ted Kremenek97f75312007-08-21 21:42:03 +00001014 if (i == 8 || (i-8) % 10 == 0) {
1015 OS << "\n ";
1016 }
1017 OS << " B" << (*I)->getBlockID();
1018 }
1019 OS << '\n';
Ted Kremenek4db5b452007-08-23 16:51:22 +00001020}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001021
1022//===----------------------------------------------------------------------===//
1023// CFG Graphviz Visualization
1024//===----------------------------------------------------------------------===//
1025
1026namespace llvm {
1027template<>
1028struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1029 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1030
1031 std::ostringstream Out;
1032 Node->print(Out,Graph);
1033 std::string OutStr = Out.str();
1034
1035 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1036
1037 // Process string output to make it nicer...
1038 for (unsigned i = 0; i != OutStr.length(); ++i)
1039 if (OutStr[i] == '\n') { // Left justify
1040 OutStr[i] = '\\';
1041 OutStr.insert(OutStr.begin()+i+1, 'l');
1042 }
1043
1044 return OutStr;
1045 }
1046};
1047} // end namespace llvm
1048
1049void CFG::viewCFG() const {
1050#ifndef NDEBUG
1051 llvm::ViewGraph(this,"CFG");
1052#else
1053 std::cerr << "CFG::viewCFG is only available in debug builds on "
1054 << "systems with Graphviz or gv!" << std::endl;
1055#endif
1056}