blob: 3537a9a878f46a35022874fd23f3ec200819ef91 [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 Kremenek97f75312007-08-21 21:42:03 +000019#include <iostream>
20#include <iomanip>
21#include <algorithm>
22using namespace clang;
23
24namespace {
25
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 };
36}
37
38/// CFGBuilder - This class is implements CFG construction from an AST.
39/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;
45/// CFG* cfg = builder.BuildAST(stmt1);
46///
Ted Kremenek95e854d2007-08-21 22:06:14 +000047/// CFG construction is done via a recursive walk of an AST.
48/// We actually parse the AST in reverse order so that the successor
49/// of a basic block is constructed prior to its predecessor. This
50/// allows us to nicely capture implicit fall-throughs without extra
51/// basic blocks.
52///
53class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000054 CFG* cfg;
55 CFGBlock* Block;
56 CFGBlock* Exit;
57 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000060 unsigned NumBlocks;
61
Ted Kremenekc5de2222007-08-21 23:26:17 +000062 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
63 LabelMapTy LabelMap;
64
Ted Kremenekf5392b72007-08-22 15:40:58 +000065 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000066 BackpatchBlocksTy BackpatchBlocks;
67
Ted Kremenek97f75312007-08-21 21:42:03 +000068public:
Ted Kremenekf511d672007-08-22 21:36:54 +000069 explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000070 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek97f75312007-08-21 21:42:03 +000071 NumBlocks(0) {
72 // Create an empty CFG.
73 cfg = new CFG();
74 }
75
76 ~CFGBuilder() { delete cfg; }
Ted Kremenekd8313202007-08-22 18:22:34 +000077
Ted Kremenekbec06e82007-08-22 21:05:42 +000078 /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
Ted Kremenek97f75312007-08-21 21:42:03 +000079 /// represent an arbitrary statement. Examples include a single expression
80 /// or a function body (compound statement). The ownership of the returned
81 /// CFG is transferred to the caller. If CFG construction fails, this method
82 /// returns NULL.
Ted Kremenekbec06e82007-08-22 21:05:42 +000083 CFG* BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +000084 if (!Statement) return NULL;
85
Ted Kremenekc5de2222007-08-21 23:26:17 +000086 assert (!Exit && "CFGBuilder should only be used to construct one CFG");
Ted Kremenek97f75312007-08-21 21:42:03 +000087
88 // Create the exit block.
89 Block = createBlock();
90 Exit = Block;
91
92 // Visit the statements and create the CFG.
Ted Kremenek95e854d2007-08-21 22:06:14 +000093 if (CFGBlock* B = Visit(Statement)) {
Ted Kremenekd8313202007-08-22 18:22:34 +000094 // Finalize the last constructed block. This usually involves
95 // reversing the order of the statements in the block.
96 FinishBlock(B);
Ted Kremenekbec06e82007-08-22 21:05:42 +000097 cfg->setEntry(B);
Ted Kremenekc5de2222007-08-21 23:26:17 +000098
99 // Backpatch the gotos whose label -> block mappings we didn't know
100 // when we encountered them.
101 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
102 E = BackpatchBlocks.end(); I != E; ++I ) {
103
104 CFGBlock* B = *I;
105 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
106 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
107
108 if (LI == LabelMap.end())
109 return NULL; // No matching label. Bad CFG.
110
111 B->addSuccessor(LI->second);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000112 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000113
Ted Kremenek97f75312007-08-21 21:42:03 +0000114 // NULL out cfg so that repeated calls
115 CFG* t = cfg;
116 cfg = NULL;
117 return t;
118 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000119 else return NULL;
Ted Kremenek97f75312007-08-21 21:42:03 +0000120 }
Ted Kremenek95e854d2007-08-21 22:06:14 +0000121
Ted Kremenek97f75312007-08-21 21:42:03 +0000122 // createBlock - Used to lazily create blocks that are connected
123 // to the current (global) succcessor.
124 CFGBlock* createBlock( bool add_successor = true ) {
125 CFGBlock* B = cfg->createBlock(NumBlocks++);
126 if (add_successor && Succ) B->addSuccessor(Succ);
127 return B;
128 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000129
130 // FinishBlock - When the last statement has been added to the block,
131 // usually we must reverse the statements because they have been inserted
132 // in reverse order. When processing labels, however, there are cases
133 // in the recursion where we may have already reversed the statements
134 // in a block. This method safely tidies up a block: if the block
135 // has a label at the front, it has already been reversed. Otherwise,
136 // we reverse it.
137 void FinishBlock(CFGBlock* B) {
138 assert (B);
139 CFGBlock::iterator I = B->begin();
140 if (I != B->end()) {
141 Stmt* S = *I;
142 if (S->getStmtClass() != Stmt::LabelStmtClass)
143 B->reverseStmts();
144 }
145 }
Ted Kremenek97f75312007-08-21 21:42:03 +0000146
Ted Kremenek95e854d2007-08-21 22:06:14 +0000147 /// Here we handle statements with no branching control flow.
148 CFGBlock* VisitStmt(Stmt* Statement) {
149 // We cannot assume that we are in the middle of a basic block, since
150 // the CFG might only be constructed for this single statement. If
151 // we have no current basic block, just create one lazily.
152 if (!Block) Block = createBlock();
153
154 // Simply add the statement to the current block. We actually
155 // insert statements in reverse order; this order is reversed later
156 // when processing the containing element in the AST.
157 Block->appendStmt(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000158
159 return Block;
160 }
161
Ted Kremenekd8313202007-08-22 18:22:34 +0000162 CFGBlock* VisitNullStmt(NullStmt* Statement) {
163 return Block;
164 }
165
Ted Kremenek95e854d2007-08-21 22:06:14 +0000166 CFGBlock* VisitCompoundStmt(CompoundStmt* C) {
167 // The value returned from this function is the last created CFGBlock
168 // that represents the "entry" point for the translated AST node.
Ted Kremenekd8313202007-08-22 18:22:34 +0000169 CFGBlock* LastBlock;
170
Ted Kremenek95e854d2007-08-21 22:06:14 +0000171 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
Ted Kremenekd8313202007-08-22 18:22:34 +0000172 E = C->body_rend(); I != E; ++I )
Ted Kremenek95e854d2007-08-21 22:06:14 +0000173 // Add the statement to the current block.
Ted Kremenekd8313202007-08-22 18:22:34 +0000174 if (!(LastBlock=Visit(*I)))
175 return NULL;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000176
Ted Kremenekd8313202007-08-22 18:22:34 +0000177 return LastBlock;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000178 }
179
180 CFGBlock* VisitIfStmt(IfStmt* I) {
181
182 // We may see an if statement in the middle of a basic block, or
183 // it may be the first statement we are processing. In either case,
184 // we create a new basic block. First, we create the blocks for
185 // the then...else statements, and then we create the block containing
186 // the if statement. If we were in the middle of a block, we
187 // stop processing that block and reverse its statements. That block
188 // is then the implicit successor for the "then" and "else" clauses.
189
190 // The block we were proccessing is now finished. Make it the
191 // successor block.
192 if (Block) {
193 Succ = Block;
Ted Kremenekd8313202007-08-22 18:22:34 +0000194 FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000195 }
196
197 // Process the false branch. NULL out Block so that the recursive
198 // call to Visit will create a new basic block.
199 // Null out Block so that all successor
200 CFGBlock* ElseBlock = Succ;
201
202 if (Stmt* Else = I->getElse()) {
203 SaveAndRestore<CFGBlock*> sv(Succ);
204
205 // NULL out Block so that the recursive call to Visit will
206 // create a new basic block.
207 Block = NULL;
208 ElseBlock = Visit(Else);
209 if (!ElseBlock) return NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000210 FinishBlock(ElseBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000211 }
212
213 // Process the true branch. NULL out Block so that the recursive
214 // call to Visit will create a new basic block.
215 // Null out Block so that all successor
216 CFGBlock* ThenBlock;
217 {
218 Stmt* Then = I->getThen();
219 assert (Then);
220 SaveAndRestore<CFGBlock*> sv(Succ);
221 Block = NULL;
222 ThenBlock = Visit(Then);
223 if (!ThenBlock) return NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000224 FinishBlock(ThenBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000225 }
226
227 // Now create a new block containing the if statement.
228 Block = createBlock(false);
229
230 // Add the condition as the last statement in the new block.
231 Block->appendStmt(I->getCond());
232
233 // Set the terminator of the new block to the If statement.
234 Block->setTerminator(I);
235
236 // Now add the successors.
237 Block->addSuccessor(ThenBlock);
238 Block->addSuccessor(ElseBlock);
239
240 return Block;
241 }
242
243 CFGBlock* VisitReturnStmt(ReturnStmt* R) {
244 // If we were in the middle of a block we stop processing that block
245 // and reverse its statements.
246 //
247 // NOTE: If a "return" appears in the middle of a block, this means
248 // that the code afterwards is DEAD (unreachable). We still
249 // keep a basic block for that code; a simple "mark-and-sweep"
250 // from the entry block will be able to report such dead
251 // blocks.
Ted Kremenekd8313202007-08-22 18:22:34 +0000252 if (Block) FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000253
254 // Create the new block.
255 Block = createBlock(false);
256
257 // The Exit block is the only successor.
258 Block->addSuccessor(Exit);
259
260 // Add the return expression to the block.
261 Block->appendStmt(R);
262
263 // Add the return statement itself to the block.
264 if (R->getRetValue()) Block->appendStmt(R->getRetValue());
265
266 return Block;
Ted Kremenekc5de2222007-08-21 23:26:17 +0000267 }
268
269 CFGBlock* VisitLabelStmt(LabelStmt* L) {
270 // Get the block of the labeled statement. Add it to our map.
271 CFGBlock* LabelBlock = Visit(L->getSubStmt());
Ted Kremenekd8313202007-08-22 18:22:34 +0000272 assert (LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000273
274 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
275 LabelMap[ L ] = LabelBlock;
276
277 // Labels partition blocks, so this is the end of the basic block
278 // we were processing (the label is the first statement).
Ted Kremenekd8313202007-08-22 18:22:34 +0000279 LabelBlock->appendStmt(L);
280 FinishBlock(LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000281
282 // We set Block to NULL to allow lazy creation of a new block
283 // (if necessary);
284 Block = NULL;
285
286 // This block is now the implicit successor of other blocks.
287 Succ = LabelBlock;
288
289 return LabelBlock;
290 }
291
292 CFGBlock* VisitGotoStmt(GotoStmt* G) {
293 // Goto is a control-flow statement. Thus we stop processing the
294 // current block and create a new one.
Ted Kremenekd8313202007-08-22 18:22:34 +0000295 if (Block) FinishBlock(Block);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000296 Block = createBlock(false);
297 Block->setTerminator(G);
298
299 // If we already know the mapping to the label block add the
300 // successor now.
301 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
302
303 if (I == LabelMap.end())
304 // We will need to backpatch this block later.
305 BackpatchBlocks.push_back(Block);
306 else
307 Block->addSuccessor(I->second);
308
309 return Block;
310 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000311
312 CFGBlock* VisitForStmt(ForStmt* F) {
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000313 // "for" is a control-flow statement. Thus we stop processing the
Ted Kremenekd8313202007-08-22 18:22:34 +0000314 // current block.
Ted Kremenekd8313202007-08-22 18:22:34 +0000315
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000316 CFGBlock* LoopSuccessor = NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000317
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000318 if (Block) {
319 FinishBlock(Block);
320 LoopSuccessor = Block;
321 }
322 else LoopSuccessor = Succ;
323
324 // Create the condition block.
Ted Kremenekf511d672007-08-22 21:36:54 +0000325 CFGBlock* ConditionBlock = createBlock(false);
Ted Kremenekf511d672007-08-22 21:36:54 +0000326 ConditionBlock->setTerminator(F);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000327 if (Stmt* C = F->getCond()) ConditionBlock->appendStmt(C);
Ted Kremenekf511d672007-08-22 21:36:54 +0000328
329 // The condition block is the implicit successor for the loop body as
330 // well as any code above the loop.
331 Succ = ConditionBlock;
Ted Kremenekd8313202007-08-22 18:22:34 +0000332
333 // Now create the loop body.
334 {
335 assert (F->getBody());
Ted Kremenekf511d672007-08-22 21:36:54 +0000336
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000337 // Save the current values for Block, Succ, and continue and break targets
Ted Kremenekf511d672007-08-22 21:36:54 +0000338 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000339 save_continue(ContinueTargetBlock),
340 save_break(BreakTargetBlock);
341
Ted Kremenekf511d672007-08-22 21:36:54 +0000342 // All continues within this loop should go to the condition block
343 ContinueTargetBlock = ConditionBlock;
Ted Kremenekd8313202007-08-22 18:22:34 +0000344
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000345 // All breaks should go to the code following the loop.
346 BreakTargetBlock = LoopSuccessor;
Ted Kremenekf308d372007-08-22 21:51:58 +0000347
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000348 // Create a new block to contain the (bottom) of the loop body.
Ted Kremenekd8313202007-08-22 18:22:34 +0000349 Block = createBlock();
350
351 // If we have increment code, insert it at the end of the body block.
352 if (Stmt* I = F->getInc()) Block->appendStmt(I);
353
354 // Now populate the body block, and in the process create new blocks
355 // as we walk the body of the loop.
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000356 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekd8313202007-08-22 18:22:34 +0000357 assert (BodyBlock);
358 FinishBlock(BodyBlock);
359
360 // This new body block is a successor to our condition block.
Ted Kremenekf511d672007-08-22 21:36:54 +0000361 ConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd8313202007-08-22 18:22:34 +0000362 }
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000363
Ted Kremenekd8313202007-08-22 18:22:34 +0000364 // Link up the condition block with the code that follows the loop.
365 // (the false branch).
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000366 ConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd8313202007-08-22 18:22:34 +0000367
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000368 // If the loop contains initialization, create a new block for those
369 // statements. This block can also contain statements that precede
370 // the loop.
371 if (Stmt* I = F->getInit()) {
372 Block = createBlock();
373 Block->appendStmt(I);
374 return Block;
375 }
376 else {
377 // There is no loop initialization. We are thus basically a while
378 // loop. NULL out Block to force lazy block construction.
379 Block = NULL;
380 return ConditionBlock;
381 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000382 }
Ted Kremenekbec06e82007-08-22 21:05:42 +0000383
384 CFGBlock* VisitWhileStmt(WhileStmt* W) {
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000385 // "while" is a control-flow statement. Thus we stop processing the
Ted Kremenekbec06e82007-08-22 21:05:42 +0000386 // current block.
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000387
388 CFGBlock* LoopSuccessor = NULL;
389
390 if (Block) {
391 FinishBlock(Block);
392 LoopSuccessor = Block;
393 }
394 else LoopSuccessor = Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +0000395
396 // Create the condition block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000397 CFGBlock* ConditionBlock = createBlock(false);
398 ConditionBlock->setTerminator(W);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000399 if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000400
Ted Kremenekf511d672007-08-22 21:36:54 +0000401 // The condition block is the implicit successor for the loop body as
402 // well as any code above the loop.
403 Succ = ConditionBlock;
404
Ted Kremenekbec06e82007-08-22 21:05:42 +0000405 // Process the loop body.
406 {
407 assert (W->getBody());
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000408
409 // Save the current values for Block, Succ, and continue and break targets
Ted Kremenekf511d672007-08-22 21:36:54 +0000410 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Ted Kremenekf308d372007-08-22 21:51:58 +0000411 save_continue(ContinueTargetBlock),
412 save_break(BreakTargetBlock);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000413
Ted Kremenekf511d672007-08-22 21:36:54 +0000414 // All continues within this loop should go to the condition block
415 ContinueTargetBlock = ConditionBlock;
416
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000417 // All breaks should go to the code following the loop.
418 BreakTargetBlock = LoopSuccessor;
Ted Kremenekf308d372007-08-22 21:51:58 +0000419
Ted Kremenekf511d672007-08-22 21:36:54 +0000420 // NULL out Block to force lazy instantiation of blocks for the body.
421 Block = NULL;
422
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000423 // Create the body. The returned block is the entry to the loop body.
Ted Kremenekf511d672007-08-22 21:36:54 +0000424 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekbec06e82007-08-22 21:05:42 +0000425 assert (BodyBlock);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000426 FinishBlock(BodyBlock);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000427
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000428 // Add the loop body entry as a successor to the condition.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000429 ConditionBlock->addSuccessor(BodyBlock);
430 }
431
Ted Kremenekf308d372007-08-22 21:51:58 +0000432 // Link up the condition block with the code that follows the loop.
433 // (the false branch).
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000434 ConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000435
436 // There can be no more statements in the condition block
437 // since we loop back to this block. NULL out Block to force
438 // lazy creation of another block.
439 Block = NULL;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000440
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000441 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000442 return ConditionBlock;
443 }
444
Ted Kremenekf511d672007-08-22 21:36:54 +0000445 CFGBlock* VisitContinueStmt(ContinueStmt* C) {
Ted Kremenekf308d372007-08-22 21:51:58 +0000446 // "continue" is a control-flow statement. Thus we stop processing the
Ted Kremenekf511d672007-08-22 21:36:54 +0000447 // current block.
448 if (Block) FinishBlock(Block);
449
450 // Now create a new block that ends with the continue statement.
451 Block = createBlock(false);
452 Block->setTerminator(C);
453
454 // FIXME: We should gracefully handle continues without resolved targets.
455 assert (ContinueTargetBlock);
456
457 Block->addSuccessor(ContinueTargetBlock);
458 return Block;
459 }
460
Ted Kremenekf308d372007-08-22 21:51:58 +0000461 CFGBlock* VisitBreakStmt(BreakStmt* B) {
462 // "break" is a control-flow statement. Thus we stop processing the
463 // current block.
464 if (Block) FinishBlock(Block);
465
466 // Now create a new block that ends with the continue statement.
467 Block = createBlock(false);
468 Block->setTerminator(B);
469
470 // FIXME: We should gracefully handle breaks without resolved targets.
471 assert (BreakTargetBlock);
472
473 Block->addSuccessor(BreakTargetBlock);
474 return Block;
475 }
476
Ted Kremenek97f75312007-08-21 21:42:03 +0000477};
478
479// BuildCFG - A helper function that builds CFGs from ASTS.
Ted Kremenek95e854d2007-08-21 22:06:14 +0000480CFG* CFG::BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +0000481 CFGBuilder Builder;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000482 return Builder.BuildCFG(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000483}
484
485// reverseStmts - A method that reverses the order of the statements within
486// a CFGBlock.
487void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
488
489// dump - A simple pretty printer of a CFG that outputs to stderr.
490void CFG::dump() { print(std::cerr); }
491
492// print - A simple pretty printer of a CFG that outputs to an ostream.
493void CFG::print(std::ostream& OS) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000494 // First print out the Entry block, which may not be the first block
495 // in our list of blocks
496 if (begin() != end()) {
497 CFGBlock& Entry = getEntry();
498 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
499 Entry.print(OS);
500 }
501
Ted Kremenek97f75312007-08-21 21:42:03 +0000502 // Iterate through the CFGBlocks and print them one by one. Specially
503 // designate the Entry and Exit blocks.
504 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000505 // Skip the entry block, because we already printed it.
506 if (&(*I) == &getEntry())
507 continue;
508
Ted Kremenek97f75312007-08-21 21:42:03 +0000509 OS << "\n [ B" << I->getBlockID();
Ted Kremenekbec06e82007-08-22 21:05:42 +0000510
Ted Kremenekc5de2222007-08-21 23:26:17 +0000511 if (&(*I) == &getExit()) OS << " (EXIT) ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000512 else OS << " ]\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000513
Ted Kremenek97f75312007-08-21 21:42:03 +0000514 I->print(OS);
515 }
516 OS << "\n";
517}
518
Ted Kremenekd8313202007-08-22 18:22:34 +0000519
520namespace {
521
522 class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
523 void > {
524 std::ostream& OS;
525 public:
526 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
527
528 void VisitIfStmt(IfStmt* I) {
529 OS << "if ";
530 I->getCond()->printPretty(std::cerr);
531 OS << "\n";
532 }
533
534 // Default case.
535 void VisitStmt(Stmt* S) { S->printPretty(OS); }
536
537 void VisitForStmt(ForStmt* F) {
538 OS << "for (" ;
539 if (Stmt* I = F->getInit()) I->printPretty(OS);
540 OS << " ; ";
541 if (Stmt* C = F->getCond()) C->printPretty(OS);
542 OS << " ; ";
543 if (Stmt* I = F->getInc()) I->printPretty(OS);
544 OS << ")\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000545 }
546
547 void VisitWhileStmt(WhileStmt* W) {
548 OS << "while " ;
549 if (Stmt* C = W->getCond()) C->printPretty(OS);
550 OS << "\n";
551 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000552 };
553}
554
Ted Kremenek97f75312007-08-21 21:42:03 +0000555// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
556void CFGBlock::dump() { print(std::cerr); }
557
558// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
559// Generally this will only be called from CFG::print.
560void CFGBlock::print(std::ostream& OS) {
561
562 // Iterate through the statements in the block and print them.
563 OS << " ------------------------\n";
564 unsigned j = 1;
565 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000566 // Print the statement # in the basic block.
567 OS << " " << std::setw(3) << j << ": ";
568
569 // Print the statement/expression.
570 Stmt* S = *I;
571
572 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
573 OS << L->getName() << ": (LABEL)\n";
574 else
575 (*I)->printPretty(OS);
576
577 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000578 if (isa<Expr>(*I)) OS << '\n';
579 }
580 OS << " ------------------------\n";
581
582 // Print the predecessors of this block.
583 OS << " Predecessors (" << pred_size() << "):";
584 unsigned i = 0;
585 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
586 if (i == 8 || (i-8) == 0) {
587 OS << "\n ";
588 }
589 OS << " B" << (*I)->getBlockID();
590 }
591
592 // Print the terminator of this block.
593 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000594 if (ControlFlowStmt)
595 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
596 else
597 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000598
599 // Print the successors of this block.
600 OS << " Successors (" << succ_size() << "):";
601 i = 0;
602 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
603 if (i == 8 || (i-8) % 10 == 0) {
604 OS << "\n ";
605 }
606 OS << " B" << (*I)->getBlockID();
607 }
608 OS << '\n';
609}