blob: fb5046266032fd447839e67222cdf1cb5cb5936a [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
Ted Kremenekd6e50602007-08-23 21:26:19 +000026// SaveAndRestore - A utility class that uses RIIA to save and restore
27// the value of a variable.
28template<typename T>
29struct 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 Kremenek97f75312007-08-21 21:42:03 +000036
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 Kremenek95e854d2007-08-21 22:06:14 +000046/// 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///
52class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000053 CFG* cfg;
54 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000055 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000056 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000057 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000058 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000059 unsigned NumBlocks;
60
Ted Kremenekc5de2222007-08-21 23:26:17 +000061 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
62 LabelMapTy LabelMap;
63
Ted Kremenekf5392b72007-08-22 15:40:58 +000064 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000065 BackpatchBlocksTy BackpatchBlocks;
66
Ted Kremenek97f75312007-08-21 21:42:03 +000067public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000068 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000069 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremeneke809ebf2007-08-23 18:43:24 +000070 SwitchTerminatedBlock(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 Kremenek97f75312007-08-21 21:42:03 +000077
Ted Kremenek73543912007-08-23 21:42:29 +000078 // buildCFG - Used by external clients to construct the CFG.
79 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +000080
Ted Kremenek73543912007-08-23 21:42:29 +000081 // Visitors to walk an AST and construct the CFG. Called by
82 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +000083
Ted Kremenek73543912007-08-23 21:42:29 +000084 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 Kremenek97f75312007-08-21 21:42:03 +000098
Ted Kremenek73543912007-08-23 21:42:29 +000099private:
100 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000101 CFGBlock* addStmt(Stmt* S);
102 CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt);
103 CFGBlock* WalkAST_VisitChildren(Stmt* S);
Ted Kremeneke822b622007-08-28 18:14:37 +0000104 CFGBlock* WalkAST_VisitVarDecl(VarDecl* D);
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000105 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* S);
Ted Kremenek73543912007-08-23 21:42:29 +0000106 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000107
Ted Kremenek97f75312007-08-21 21:42:03 +0000108};
Ted Kremenek73543912007-08-23 21:42:29 +0000109
110/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
111/// represent an arbitrary statement. Examples include a single expression
112/// or a function body (compound statement). The ownership of the returned
113/// CFG is transferred to the caller. If CFG construction fails, this method
114/// returns NULL.
115CFG* CFGBuilder::buildCFG(Stmt* Statement) {
116 if (!Statement) return NULL;
117
118 // Create an empty block that will serve as the exit block for the CFG.
119 // Since this is the first block added to the CFG, it will be implicitly
120 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000121 Succ = createBlock();
122 assert (Succ == &cfg->getExit());
123 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000124
125 // Visit the statements and create the CFG.
126 if (CFGBlock* B = Visit(Statement)) {
127 // Finalize the last constructed block. This usually involves
128 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000129 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000130
131 // Backpatch the gotos whose label -> block mappings we didn't know
132 // when we encountered them.
133 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
134 E = BackpatchBlocks.end(); I != E; ++I ) {
135
136 CFGBlock* B = *I;
137 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
138 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
139
140 // If there is no target for the goto, then we are looking at an
141 // incomplete AST. Handle this by not registering a successor.
142 if (LI == LabelMap.end()) continue;
143
144 B->addSuccessor(LI->second);
145 }
146
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000147 if (B->pred_size() > 0) {
148 // create an empty entry block that has no predecessors.
149 Succ = B;
150 cfg->setEntry(createBlock());
151 }
152 else cfg->setEntry(B);
153
Ted Kremenek73543912007-08-23 21:42:29 +0000154 // NULL out cfg so that repeated calls
155 CFG* t = cfg;
156 cfg = NULL;
157 return t;
158 }
159 else return NULL;
160}
161
162/// createBlock - Used to lazily create blocks that are connected
163/// to the current (global) succcessor.
164CFGBlock* CFGBuilder::createBlock(bool add_successor) {
165 CFGBlock* B = cfg->createBlock(NumBlocks++);
166 if (add_successor && Succ) B->addSuccessor(Succ);
167 return B;
168}
169
170/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000171/// we must reverse the statements because they have been inserted
172/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000173void CFGBuilder::FinishBlock(CFGBlock* B) {
174 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000175 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000176}
177
Ted Kremenek65cfa562007-08-27 21:27:44 +0000178/// addStmt - Used to add statements/expressions to the current CFGBlock
179/// "Block". This method calls WalkAST on the passed statement to see if it
180/// contains any short-circuit expressions. If so, it recursively creates
181/// the necessary blocks for such expressions. It returns the "topmost" block
182/// of the created blocks, or the original value of "Block" when this method
183/// was called if no additional blocks are created.
184CFGBlock* CFGBuilder::addStmt(Stmt* S) {
185 assert (Block);
186 return WalkAST(S,true);
187}
188
189/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000190/// add extra blocks for ternary operators, &&, and ||. We also
191/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek65cfa562007-08-27 21:27:44 +0000192CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) {
193 switch (S->getStmtClass()) {
194 case Stmt::ConditionalOperatorClass: {
195 ConditionalOperator* C = cast<ConditionalOperator>(S);
196
197 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
198 ConfluenceBlock->appendStmt(C);
199 FinishBlock(ConfluenceBlock);
200
201 Succ = ConfluenceBlock;
202 Block = NULL;
203 CFGBlock* LHSBlock = Visit(C->getLHS());
204
205 Succ = ConfluenceBlock;
206 Block = NULL;
207 CFGBlock* RHSBlock = Visit(C->getRHS());
208
209 Block = createBlock(false);
210 Block->addSuccessor(LHSBlock);
211 Block->addSuccessor(RHSBlock);
212 Block->setTerminator(C);
213 return addStmt(C->getCond());
214 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000215
Ted Kremeneke822b622007-08-28 18:14:37 +0000216 case Stmt::DeclStmtClass:
217 if (VarDecl* V = dyn_cast<VarDecl>(cast<DeclStmt>(S)->getDecl())) {
218 Block->appendStmt(S);
219 return WalkAST_VisitVarDecl(V);
220 }
221 else return Block;
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000222
223 case Stmt::StmtExprClass:
224 return WalkAST_VisitStmtExpr(cast<StmtExpr>(S));
Ted Kremeneke822b622007-08-28 18:14:37 +0000225
Ted Kremenekcfaae762007-08-27 21:54:41 +0000226 case Stmt::BinaryOperatorClass: {
227 BinaryOperator* B = cast<BinaryOperator>(S);
228
229 if (B->isLogicalOp()) { // && or ||
230 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
231 ConfluenceBlock->appendStmt(B);
232 FinishBlock(ConfluenceBlock);
233
234 // create the block evaluating the LHS
235 CFGBlock* LHSBlock = createBlock(false);
236 LHSBlock->addSuccessor(ConfluenceBlock);
237 LHSBlock->setTerminator(B);
238
239 // create the block evaluating the RHS
240 Succ = ConfluenceBlock;
241 Block = NULL;
242 CFGBlock* RHSBlock = Visit(B->getRHS());
243 LHSBlock->addSuccessor(RHSBlock);
244
245 // Generate the blocks for evaluating the LHS.
246 Block = LHSBlock;
247 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000248 }
249 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
250 Block->appendStmt(B);
251 addStmt(B->getRHS());
252 return addStmt(B->getLHS());
Ted Kremenekcfaae762007-08-27 21:54:41 +0000253 }
254
255 // Fall through to the default case.
256 }
257
Ted Kremenek65cfa562007-08-27 21:27:44 +0000258 default:
259 if (AlwaysAddStmt) Block->appendStmt(S);
260 return WalkAST_VisitChildren(S);
261 };
262}
263
Ted Kremeneke822b622007-08-28 18:14:37 +0000264/// WalkAST_VisitVarDecl - Utility method to handle VarDecls contained in
265/// DeclStmts. Because the initialization code for declarations can
266/// contain arbitrary expressions, we must linearize declarations
267/// to handle arbitrary control-flow induced by those expressions.
268CFGBlock* CFGBuilder::WalkAST_VisitVarDecl(VarDecl* V) {
269 // We actually must parse the LAST declaration in a chain of
270 // declarations first, because we are building the CFG in reverse
271 // order.
272 if (Decl* D = V->getNextDeclarator())
273 if (VarDecl* Next = cast<VarDecl>(D))
274 Block = WalkAST_VisitVarDecl(Next);
275
276 if (Expr* E = V->getInit())
277 return addStmt(E);
278
279 assert (Block);
280 return Block;
281}
282
Ted Kremenek65cfa562007-08-27 21:27:44 +0000283/// WalkAST_VisitChildren - Utility method to call WalkAST on the
284/// children of a Stmt.
Ted Kremenekcfaae762007-08-27 21:54:41 +0000285CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000286 CFGBlock* B = Block;
287 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
288 I != E; ++I)
289 B = WalkAST(*I);
290
291 return B;
292}
293
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000294/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
295/// expressions (a GCC extension).
296CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* S) {
297 Block->appendStmt(S);
298 return VisitCompoundStmt(S->getSubStmt());
299}
300
Ted Kremenek73543912007-08-23 21:42:29 +0000301/// VisitStmt - Handle statements with no branching control flow.
302CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
303 // We cannot assume that we are in the middle of a basic block, since
304 // the CFG might only be constructed for this single statement. If
305 // we have no current basic block, just create one lazily.
306 if (!Block) Block = createBlock();
307
308 // Simply add the statement to the current block. We actually
309 // insert statements in reverse order; this order is reversed later
310 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000311 addStmt(Statement);
312
Ted Kremenek73543912007-08-23 21:42:29 +0000313 return Block;
314}
315
316CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
317 return Block;
318}
319
320CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
321 // The value returned from this function is the last created CFGBlock
322 // that represents the "entry" point for the translated AST node.
323 CFGBlock* LastBlock;
324
325 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
326 E = C->body_rend(); I != E; ++I )
327 // Add the statement to the current block.
328 if (!(LastBlock=Visit(*I)))
329 return NULL;
330
331 return LastBlock;
332}
333
334CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
335 // We may see an if statement in the middle of a basic block, or
336 // it may be the first statement we are processing. In either case,
337 // we create a new basic block. First, we create the blocks for
338 // the then...else statements, and then we create the block containing
339 // the if statement. If we were in the middle of a block, we
340 // stop processing that block and reverse its statements. That block
341 // is then the implicit successor for the "then" and "else" clauses.
342
343 // The block we were proccessing is now finished. Make it the
344 // successor block.
345 if (Block) {
346 Succ = Block;
347 FinishBlock(Block);
348 }
349
350 // Process the false branch. NULL out Block so that the recursive
351 // call to Visit will create a new basic block.
352 // Null out Block so that all successor
353 CFGBlock* ElseBlock = Succ;
354
355 if (Stmt* Else = I->getElse()) {
356 SaveAndRestore<CFGBlock*> sv(Succ);
357
358 // NULL out Block so that the recursive call to Visit will
359 // create a new basic block.
360 Block = NULL;
361 ElseBlock = Visit(Else);
362 if (!ElseBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000363 if (Block) FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000364 }
365
366 // Process the true branch. NULL out Block so that the recursive
367 // call to Visit will create a new basic block.
368 // Null out Block so that all successor
369 CFGBlock* ThenBlock;
370 {
371 Stmt* Then = I->getThen();
372 assert (Then);
373 SaveAndRestore<CFGBlock*> sv(Succ);
374 Block = NULL;
375 ThenBlock = Visit(Then);
376 if (!ThenBlock) return NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000377 if (Block) FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000378 }
379
380 // Now create a new block containing the if statement.
381 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000382
383 // Set the terminator of the new block to the If statement.
384 Block->setTerminator(I);
385
386 // Now add the successors.
387 Block->addSuccessor(ThenBlock);
388 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000389
390 // Add the condition as the last statement in the new block. This
391 // may create new blocks as the condition may contain control-flow. Any
392 // newly created blocks will be pointed to be "Block".
393 return addStmt(I->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000394}
395
396CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
397 // If we were in the middle of a block we stop processing that block
398 // and reverse its statements.
399 //
400 // NOTE: If a "return" appears in the middle of a block, this means
401 // that the code afterwards is DEAD (unreachable). We still
402 // keep a basic block for that code; a simple "mark-and-sweep"
403 // from the entry block will be able to report such dead
404 // blocks.
405 if (Block) FinishBlock(Block);
406
407 // Create the new block.
408 Block = createBlock(false);
409
410 // The Exit block is the only successor.
411 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000412
413 // Add the return statement to the block. This may create new blocks
414 // if R contains control-flow (short-circuit operations).
415 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000416}
417
418CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
419 // Get the block of the labeled statement. Add it to our map.
420 CFGBlock* LabelBlock = Visit(L->getSubStmt());
421 assert (LabelBlock);
422
423 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
424 LabelMap[ L ] = LabelBlock;
425
426 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000427 // we were processing (the label is the first statement). Add the label
428 // the to end (really the beginning) of the block. Because this is
429 // label (and we have already processed the substatement) there is no
430 // extra control-flow to worry about.
Ted Kremenek73543912007-08-23 21:42:29 +0000431 LabelBlock->appendStmt(L);
432 FinishBlock(LabelBlock);
433
434 // We set Block to NULL to allow lazy creation of a new block
435 // (if necessary);
436 Block = NULL;
437
438 // This block is now the implicit successor of other blocks.
439 Succ = LabelBlock;
440
441 return LabelBlock;
442}
443
444CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
445 // Goto is a control-flow statement. Thus we stop processing the
446 // current block and create a new one.
447 if (Block) FinishBlock(Block);
448 Block = createBlock(false);
449 Block->setTerminator(G);
450
451 // If we already know the mapping to the label block add the
452 // successor now.
453 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
454
455 if (I == LabelMap.end())
456 // We will need to backpatch this block later.
457 BackpatchBlocks.push_back(Block);
458 else
459 Block->addSuccessor(I->second);
460
461 return Block;
462}
463
464CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
465 // "for" is a control-flow statement. Thus we stop processing the
466 // current block.
467
468 CFGBlock* LoopSuccessor = NULL;
469
470 if (Block) {
471 FinishBlock(Block);
472 LoopSuccessor = Block;
473 }
474 else LoopSuccessor = Succ;
475
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000476 // Because of short-circuit evaluation, the condition of the loop
477 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
478 // blocks that evaluate the condition.
479 CFGBlock* ExitConditionBlock = createBlock(false);
480 CFGBlock* EntryConditionBlock = ExitConditionBlock;
481
482 // Set the terminator for the "exit" condition block.
483 ExitConditionBlock->setTerminator(F);
484
485 // Now add the actual condition to the condition block. Because the
486 // condition itself may contain control-flow, new blocks may be created.
487 if (Stmt* C = F->getCond()) {
488 Block = ExitConditionBlock;
489 EntryConditionBlock = addStmt(C);
490 if (Block) FinishBlock(EntryConditionBlock);
491 }
Ted Kremenek73543912007-08-23 21:42:29 +0000492
493 // The condition block is the implicit successor for the loop body as
494 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000495 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000496
497 // Now create the loop body.
498 {
499 assert (F->getBody());
500
501 // Save the current values for Block, Succ, and continue and break targets
502 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
503 save_continue(ContinueTargetBlock),
504 save_break(BreakTargetBlock);
505
506 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000507 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000508
509 // All breaks should go to the code following the loop.
510 BreakTargetBlock = LoopSuccessor;
511
512 // Create a new block to contain the (bottom) of the loop body.
513 Block = createBlock();
514
515 // If we have increment code, insert it at the end of the body block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000516 if (Stmt* I = F->getInc()) Block = addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000517
518 // Now populate the body block, and in the process create new blocks
519 // as we walk the body of the loop.
520 CFGBlock* BodyBlock = Visit(F->getBody());
521 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000522 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000523
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000524 // This new body block is a successor to our "exit" condition block.
525 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000526 }
527
528 // Link up the condition block with the code that follows the loop.
529 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000530 ExitConditionBlock->addSuccessor(LoopSuccessor);
531
Ted Kremenek73543912007-08-23 21:42:29 +0000532 // If the loop contains initialization, create a new block for those
533 // statements. This block can also contain statements that precede
534 // the loop.
535 if (Stmt* I = F->getInit()) {
536 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000537 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000538 }
539 else {
540 // There is no loop initialization. We are thus basically a while
541 // loop. NULL out Block to force lazy block construction.
542 Block = NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000543 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000544 }
545}
546
547CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
548 // "while" is a control-flow statement. Thus we stop processing the
549 // current block.
550
551 CFGBlock* LoopSuccessor = NULL;
552
553 if (Block) {
554 FinishBlock(Block);
555 LoopSuccessor = Block;
556 }
557 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000558
559 // Because of short-circuit evaluation, the condition of the loop
560 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
561 // blocks that evaluate the condition.
562 CFGBlock* ExitConditionBlock = createBlock(false);
563 CFGBlock* EntryConditionBlock = ExitConditionBlock;
564
565 // Set the terminator for the "exit" condition block.
566 ExitConditionBlock->setTerminator(W);
567
568 // Now add the actual condition to the condition block. Because the
569 // condition itself may contain control-flow, new blocks may be created.
570 // Thus we update "Succ" after adding the condition.
571 if (Stmt* C = W->getCond()) {
572 Block = ExitConditionBlock;
573 EntryConditionBlock = addStmt(C);
574 if (Block) FinishBlock(EntryConditionBlock);
575 }
Ted Kremenek73543912007-08-23 21:42:29 +0000576
577 // The condition block is the implicit successor for the loop body as
578 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000579 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000580
581 // Process the loop body.
582 {
583 assert (W->getBody());
584
585 // Save the current values for Block, Succ, and continue and break targets
586 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
587 save_continue(ContinueTargetBlock),
588 save_break(BreakTargetBlock);
589
590 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000591 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000592
593 // All breaks should go to the code following the loop.
594 BreakTargetBlock = LoopSuccessor;
595
596 // NULL out Block to force lazy instantiation of blocks for the body.
597 Block = NULL;
598
599 // Create the body. The returned block is the entry to the loop body.
600 CFGBlock* BodyBlock = Visit(W->getBody());
601 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000602 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000603
604 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000605 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000606 }
607
608 // Link up the condition block with the code that follows the loop.
609 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000610 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000611
612 // There can be no more statements in the condition block
613 // since we loop back to this block. NULL out Block to force
614 // lazy creation of another block.
615 Block = NULL;
616
617 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000618 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000619}
620
621CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
622 // "do...while" is a control-flow statement. Thus we stop processing the
623 // current block.
624
625 CFGBlock* LoopSuccessor = NULL;
626
627 if (Block) {
628 FinishBlock(Block);
629 LoopSuccessor = Block;
630 }
631 else LoopSuccessor = Succ;
632
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000633 // Because of short-circuit evaluation, the condition of the loop
634 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
635 // blocks that evaluate the condition.
636 CFGBlock* ExitConditionBlock = createBlock(false);
637 CFGBlock* EntryConditionBlock = ExitConditionBlock;
638
639 // Set the terminator for the "exit" condition block.
640 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000641
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000642 // Now add the actual condition to the condition block. Because the
643 // condition itself may contain control-flow, new blocks may be created.
644 if (Stmt* C = D->getCond()) {
645 Block = ExitConditionBlock;
646 EntryConditionBlock = addStmt(C);
647 if (Block) FinishBlock(EntryConditionBlock);
648 }
Ted Kremenek73543912007-08-23 21:42:29 +0000649
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000650 // The condition block is the implicit successor for the loop body as
651 // well as any code above the loop.
652 Succ = EntryConditionBlock;
653
654
Ted Kremenek73543912007-08-23 21:42:29 +0000655 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000656 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000657 {
658 assert (D->getBody());
659
660 // Save the current values for Block, Succ, and continue and break targets
661 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
662 save_continue(ContinueTargetBlock),
663 save_break(BreakTargetBlock);
664
665 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000666 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000667
668 // All breaks should go to the code following the loop.
669 BreakTargetBlock = LoopSuccessor;
670
671 // NULL out Block to force lazy instantiation of blocks for the body.
672 Block = NULL;
673
674 // Create the body. The returned block is the entry to the loop body.
675 BodyBlock = Visit(D->getBody());
676 assert (BodyBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000677 if (Block) FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000678
679 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000680 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000681 }
682
683 // Link up the condition block with the code that follows the loop.
684 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000685 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000686
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000687 // There can be no more statements in the body block(s)
688 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +0000689 // lazy creation of another block.
690 Block = NULL;
691
692 // Return the loop body, which is the dominating block for the loop.
693 return BodyBlock;
694}
695
696CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
697 // "continue" is a control-flow statement. Thus we stop processing the
698 // current block.
699 if (Block) FinishBlock(Block);
700
701 // Now create a new block that ends with the continue statement.
702 Block = createBlock(false);
703 Block->setTerminator(C);
704
705 // If there is no target for the continue, then we are looking at an
706 // incomplete AST. Handle this by not registering a successor.
707 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
708
709 return Block;
710}
711
712CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
713 // "break" is a control-flow statement. Thus we stop processing the
714 // current block.
715 if (Block) FinishBlock(Block);
716
717 // Now create a new block that ends with the continue statement.
718 Block = createBlock(false);
719 Block->setTerminator(B);
720
721 // If there is no target for the break, then we are looking at an
722 // incomplete AST. Handle this by not registering a successor.
723 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
724
725 return Block;
726}
727
728CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) {
729 // "switch" is a control-flow statement. Thus we stop processing the
730 // current block.
731 CFGBlock* SwitchSuccessor = NULL;
732
733 if (Block) {
734 FinishBlock(Block);
735 SwitchSuccessor = Block;
736 }
737 else SwitchSuccessor = Succ;
738
739 // Save the current "switch" context.
740 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
741 save_break(BreakTargetBlock);
742
743 // Create a new block that will contain the switch statement.
744 SwitchTerminatedBlock = createBlock(false);
745
Ted Kremenek73543912007-08-23 21:42:29 +0000746 // Now process the switch body. The code after the switch is the implicit
747 // successor.
748 Succ = SwitchSuccessor;
749 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000750
751 // When visiting the body, the case statements should automatically get
752 // linked up to the switch. We also don't keep a pointer to the body,
753 // since all control-flow from the switch goes to case/default statements.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000754 assert (S->getBody() && "switch must contain a non-NULL body");
755 Block = NULL;
756 CFGBlock *BodyBlock = Visit(S->getBody());
757 if (Block) FinishBlock(BodyBlock);
758
759 // Add the terminator and condition in the switch block.
760 SwitchTerminatedBlock->setTerminator(S);
761 assert (S->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +0000762 Block = SwitchTerminatedBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000763 return addStmt(S->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000764}
765
766CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) {
767 // A SwitchCase is either a "default" or "case" statement. We handle
768 // both in the same way. They are essentially labels, so they are the
769 // first statement in a block.
770 CFGBlock* CaseBlock = Visit(S->getSubStmt());
771 assert (CaseBlock);
772
773 // Cases/Default statements parition block, so this is the top of
774 // the basic block we were processing (the case/default is the first stmt).
775 CaseBlock->appendStmt(S);
776 FinishBlock(CaseBlock);
777
778 // Add this block to the list of successors for the block with the
779 // switch statement.
780 if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock);
781
782 // We set Block to NULL to allow lazy creation of a new block (if necessary)
783 Block = NULL;
784
785 // This block is now the implicit successor of other blocks.
786 Succ = CaseBlock;
787
788 return CaseBlock;
789}
790
791
Ted Kremenekd6e50602007-08-23 21:26:19 +0000792} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +0000793
794/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
795/// block has no successors or predecessors. If this is the first block
796/// created in the CFG, it is automatically set to be the Entry and Exit
797/// of the CFG.
798CFGBlock* CFG::createBlock(unsigned blockID) {
799 bool first_block = begin() == end();
800
801 // Create the block.
802 Blocks.push_front(CFGBlock(blockID));
803
804 // If this is the first block, set it as the Entry and Exit.
805 if (first_block) Entry = Exit = &front();
806
807 // Return the block.
808 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +0000809}
810
Ted Kremenek4db5b452007-08-23 16:51:22 +0000811/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
812/// CFG is returned to the caller.
813CFG* CFG::buildCFG(Stmt* Statement) {
814 CFGBuilder Builder;
815 return Builder.buildCFG(Statement);
816}
817
818/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +0000819void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
820
Ted Kremenek4db5b452007-08-23 16:51:22 +0000821/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000822void CFG::dump() { print(std::cerr); }
823
Ted Kremenek4db5b452007-08-23 16:51:22 +0000824/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek97f75312007-08-21 21:42:03 +0000825void CFG::print(std::ostream& OS) {
Ted Kremenek4db5b452007-08-23 16:51:22 +0000826 // Print the Entry block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000827 if (begin() != end()) {
828 CFGBlock& Entry = getEntry();
829 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
830 Entry.print(OS);
831 }
832
Ted Kremenek4db5b452007-08-23 16:51:22 +0000833 // Iterate through the CFGBlocks and print them one by one.
Ted Kremenek97f75312007-08-21 21:42:03 +0000834 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000835 // Skip the entry block, because we already printed it.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000836 if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000837
Ted Kremenek4db5b452007-08-23 16:51:22 +0000838 OS << "\n [ B" << I->getBlockID() << " ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000839 I->print(OS);
840 }
Ted Kremenek73543912007-08-23 21:42:29 +0000841
Ted Kremenek4db5b452007-08-23 16:51:22 +0000842 // Print the Exit Block.
843 if (begin() != end()) {
844 CFGBlock& Exit = getExit();
845 OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n";
846 Exit.print(OS);
847 }
Ted Kremenek73543912007-08-23 21:42:29 +0000848
Ted Kremenek97f75312007-08-21 21:42:03 +0000849 OS << "\n";
850}
851
Ted Kremenekd8313202007-08-22 18:22:34 +0000852
853namespace {
854
Ted Kremenek73543912007-08-23 21:42:29 +0000855class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
856 void > {
857 std::ostream& OS;
858public:
859 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
860
861 void VisitIfStmt(IfStmt* I) {
862 OS << "if ";
863 I->getCond()->printPretty(std::cerr);
864 OS << "\n";
865 }
866
867 // Default case.
868 void VisitStmt(Stmt* S) { S->printPretty(OS); }
869
870 void VisitForStmt(ForStmt* F) {
871 OS << "for (" ;
872 if (Stmt* I = F->getInit()) I->printPretty(OS);
873 OS << " ; ";
874 if (Stmt* C = F->getCond()) C->printPretty(OS);
875 OS << " ; ";
876 if (Stmt* I = F->getInc()) I->printPretty(OS);
877 OS << ")\n";
878 }
879
880 void VisitWhileStmt(WhileStmt* W) {
881 OS << "while " ;
882 if (Stmt* C = W->getCond()) C->printPretty(OS);
883 OS << "\n";
884 }
885
886 void VisitDoStmt(DoStmt* D) {
887 OS << "do ... while ";
888 if (Stmt* C = D->getCond()) C->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000889 OS << '\n';
890 }
891
892 void VisitSwitchStmt(SwitchStmt* S) {
893 OS << "switch ";
894 S->getCond()->printPretty(OS);
895 OS << '\n';
896 }
897
Ted Kremenekcfaae762007-08-27 21:54:41 +0000898 void VisitExpr(Expr* E) {
899 E->printPretty(OS);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000900 OS << '\n';
Ted Kremenekcfaae762007-08-27 21:54:41 +0000901 }
Ted Kremenek73543912007-08-23 21:42:29 +0000902};
903} // end anonymous namespace
Ted Kremenekd8313202007-08-22 18:22:34 +0000904
Ted Kremenek4db5b452007-08-23 16:51:22 +0000905/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000906void CFGBlock::dump() { print(std::cerr); }
907
Ted Kremenek4db5b452007-08-23 16:51:22 +0000908/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
909/// Generally this will only be called from CFG::print.
Ted Kremenek97f75312007-08-21 21:42:03 +0000910void CFGBlock::print(std::ostream& OS) {
911
912 // Iterate through the statements in the block and print them.
913 OS << " ------------------------\n";
914 unsigned j = 1;
915 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000916 // Print the statement # in the basic block.
917 OS << " " << std::setw(3) << j << ": ";
918
919 // Print the statement/expression.
920 Stmt* S = *I;
921
922 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
923 OS << L->getName() << ": (LABEL)\n";
924 else
925 (*I)->printPretty(OS);
926
927 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000928 if (isa<Expr>(*I)) OS << '\n';
929 }
930 OS << " ------------------------\n";
931
932 // Print the predecessors of this block.
933 OS << " Predecessors (" << pred_size() << "):";
934 unsigned i = 0;
935 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
936 if (i == 8 || (i-8) == 0) {
937 OS << "\n ";
938 }
939 OS << " B" << (*I)->getBlockID();
940 }
941
942 // Print the terminator of this block.
943 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000944 if (ControlFlowStmt)
945 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
946 else
947 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000948
949 // Print the successors of this block.
950 OS << " Successors (" << succ_size() << "):";
951 i = 0;
952 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
953 if (i == 8 || (i-8) % 10 == 0) {
954 OS << "\n ";
955 }
956 OS << " B" << (*I)->getBlockID();
957 }
958 OS << '\n';
Ted Kremenek4db5b452007-08-23 16:51:22 +0000959}