blob: fc893d5b649a3c647274b94c7826e86604d621fb [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//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek97f75312007-08-21 21:42:03 +00007//
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"
Ted Kremenek95e854d2007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek08176a52007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.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"
Ted Kremenek56c939e2007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek98cee3a2008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenekd058a9c2008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremenek7b6f67b2008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremenek97f75312007-08-21 21:42:03 +000025#include <iomanip>
26#include <algorithm>
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000027#include <sstream>
Ted Kremenek5ee98a72008-01-11 00:40:29 +000028
Ted Kremenek97f75312007-08-21 21:42:03 +000029using namespace clang;
30
31namespace {
32
Ted Kremenekd6e50602007-08-23 21:26:19 +000033// SaveAndRestore - A utility class that uses RIIA to save and restore
34// the value of a variable.
35template<typename T>
Ted Kremenek98cee3a2008-01-08 18:15:10 +000036struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekd6e50602007-08-23 21:26:19 +000037 SaveAndRestore(T& x) : X(x), old_value(x) {}
38 ~SaveAndRestore() { X = old_value; }
Ted Kremenek44db7872007-08-30 18:13:31 +000039 T get() { return old_value; }
40
Ted Kremenekd6e50602007-08-23 21:26:19 +000041 T& X;
42 T old_value;
43};
Ted Kremenek97f75312007-08-21 21:42:03 +000044
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000045static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek0865a992008-08-06 23:20:50 +000046 if (VarDecl* VD = dyn_cast<VarDecl>(D))
47 if (Expr* Ex = VD->getInit())
48 return Ex->getSourceRange().getEnd();
49
50 return D->getLocation();
51}
52
Ted Kremeneka3195a32008-08-04 22:51:42 +000053/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek97f75312007-08-21 21:42:03 +000054/// The builder is stateful: an instance of the builder should be used to only
55/// construct a single CFG.
56///
57/// Example usage:
58///
59/// CFGBuilder builder;
60/// CFG* cfg = builder.BuildAST(stmt1);
61///
Ted Kremenek95e854d2007-08-21 22:06:14 +000062/// CFG construction is done via a recursive walk of an AST.
63/// We actually parse the AST in reverse order so that the successor
64/// of a basic block is constructed prior to its predecessor. This
65/// allows us to nicely capture implicit fall-throughs without extra
66/// basic blocks.
67///
Ted Kremenek98cee3a2008-01-08 18:15:10 +000068class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000069 CFG* cfg;
70 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000071 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000072 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000073 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000074 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97bc3422008-02-13 22:05:39 +000075 CFGBlock* DefaultCaseBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000076
Ted Kremenek0edd3a92007-08-28 19:26:49 +000077 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenekc5de2222007-08-21 23:26:17 +000078 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
79 LabelMapTy LabelMap;
80
Ted Kremenek0edd3a92007-08-28 19:26:49 +000081 // A list of blocks that end with a "goto" that must be backpatched to
82 // their resolved targets upon completion of CFG construction.
Ted Kremenekf5392b72007-08-22 15:40:58 +000083 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000084 BackpatchBlocksTy BackpatchBlocks;
85
Ted Kremenek0edd3a92007-08-28 19:26:49 +000086 // A list of labels whose address has been taken (for indirect gotos).
87 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
88 LabelSetTy AddressTakenLabels;
89
Ted Kremenek97f75312007-08-21 21:42:03 +000090public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000091 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000092 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek97bc3422008-02-13 22:05:39 +000093 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek97f75312007-08-21 21:42:03 +000094 // Create an empty CFG.
95 cfg = new CFG();
96 }
97
98 ~CFGBuilder() { delete cfg; }
Ted Kremenek97f75312007-08-21 21:42:03 +000099
Ted Kremenek73543912007-08-23 21:42:29 +0000100 // buildCFG - Used by external clients to construct the CFG.
101 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000102
Ted Kremenek73543912007-08-23 21:42:29 +0000103 // Visitors to walk an AST and construct the CFG. Called by
104 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +0000105
Ted Kremenek73543912007-08-23 21:42:29 +0000106 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000107 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek05335162008-11-11 17:10:00 +0000108 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
109 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenekc07a8af2008-02-13 21:46:34 +0000110 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek05335162008-11-11 17:10:00 +0000111 CFGBlock* VisitDoStmt(DoStmt* D);
112 CFGBlock* VisitForStmt(ForStmt* F);
113 CFGBlock* VisitGotoStmt(GotoStmt* G);
114 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000115 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek05335162008-11-11 17:10:00 +0000116 CFGBlock* VisitLabelStmt(LabelStmt* L);
117 CFGBlock* VisitNullStmt(NullStmt* Statement);
118 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
119 CFGBlock* VisitReturnStmt(ReturnStmt* R);
120 CFGBlock* VisitStmt(Stmt* Statement);
121 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
122 CFGBlock* VisitWhileStmt(WhileStmt* W);
Ted Kremenek97f75312007-08-21 21:42:03 +0000123
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000124 // FIXME: Add support for ObjC-specific control-flow structures.
125
Ted Kremenekd058a9c2008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
131
Ted Kremenek72037962009-03-30 22:29:21 +0000132 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S);
133 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
134 // FIXME: For now we pretend that @catch and the code it contains
135 // does not exit.
136 return Block;
137 }
138
Ted Kremenekc74ac3e2008-12-09 20:20:09 +0000139 // FIXME: This is not completely supported. We basically @throw like
140 // a 'return'.
141 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenekd058a9c2008-04-28 18:00:46 +0000142
143 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
144 return NYS();
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000145 }
146
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000147 // Blocks.
148 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
149 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
150
Ted Kremenek73543912007-08-23 21:42:29 +0000151private:
152 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000153 CFGBlock* addStmt(Stmt* Terminator);
154 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
155 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000156 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenek79f0a632008-04-16 21:10:48 +0000157 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek73543912007-08-23 21:42:29 +0000158 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000159
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000160 bool badCFG;
Ted Kremenek97f75312007-08-21 21:42:03 +0000161};
Ted Kremenek09535672008-09-26 22:58:57 +0000162
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000163// FIXME: Add support for dependent-sized array types in C++?
164// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek09535672008-09-26 22:58:57 +0000165static VariableArrayType* FindVA(Type* t) {
166 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
167 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
168 if (vat->getSizeExpr())
169 return vat;
170
171 t = vt->getElementType().getTypePtr();
172 }
173
174 return 0;
175}
Ted Kremenek73543912007-08-23 21:42:29 +0000176
177/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
178/// represent an arbitrary statement. Examples include a single expression
179/// or a function body (compound statement). The ownership of the returned
180/// CFG is transferred to the caller. If CFG construction fails, this method
181/// returns NULL.
182CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000183 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000184 if (!Statement) return NULL;
185
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000186 badCFG = false;
187
Ted Kremenek73543912007-08-23 21:42:29 +0000188 // Create an empty block that will serve as the exit block for the CFG.
189 // Since this is the first block added to the CFG, it will be implicitly
190 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000191 Succ = createBlock();
192 assert (Succ == &cfg->getExit());
193 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000194
195 // Visit the statements and create the CFG.
Ted Kremenekfa38c7a2008-02-27 17:33:02 +0000196 CFGBlock* B = Visit(Statement);
197 if (!B) B = Succ;
198
199 if (B) {
Ted Kremenek73543912007-08-23 21:42:29 +0000200 // Finalize the last constructed block. This usually involves
201 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000202 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000203
204 // Backpatch the gotos whose label -> block mappings we didn't know
205 // when we encountered them.
206 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
207 E = BackpatchBlocks.end(); I != E; ++I ) {
208
209 CFGBlock* B = *I;
210 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
211 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
212
213 // If there is no target for the goto, then we are looking at an
214 // incomplete AST. Handle this by not registering a successor.
215 if (LI == LabelMap.end()) continue;
216
217 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000218 }
Ted Kremenek73543912007-08-23 21:42:29 +0000219
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000220 // Add successors to the Indirect Goto Dispatch block (if we have one).
221 if (CFGBlock* B = cfg->getIndirectGotoBlock())
222 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
223 E = AddressTakenLabels.end(); I != E; ++I ) {
224
225 // Lookup the target block.
226 LabelMapTy::iterator LI = LabelMap.find(*I);
227
228 // If there is no target block that contains label, then we are looking
229 // at an incomplete AST. Handle this by not registering a successor.
230 if (LI == LabelMap.end()) continue;
231
232 B->addSuccessor(LI->second);
233 }
Ted Kremenek680fcb82007-09-26 21:23:31 +0000234
Ted Kremenek844cb4d2007-09-17 16:18:02 +0000235 Succ = B;
Ted Kremenek680fcb82007-09-26 21:23:31 +0000236 }
237
238 // Create an empty entry block that has no predecessors.
239 cfg->setEntry(createBlock());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000240
Ted Kremenek4c69b3a2008-03-13 03:04:22 +0000241 if (badCFG) {
242 delete cfg;
243 cfg = NULL;
244 return NULL;
245 }
246
Ted Kremenek680fcb82007-09-26 21:23:31 +0000247 // NULL out cfg so that repeated calls to the builder will fail and that
248 // the ownership of the constructed CFG is passed to the caller.
249 CFG* t = cfg;
250 cfg = NULL;
251 return t;
Ted Kremenek73543912007-08-23 21:42:29 +0000252}
253
254/// createBlock - Used to lazily create blocks that are connected
255/// to the current (global) succcessor.
256CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek14594572007-09-05 20:02:05 +0000257 CFGBlock* B = cfg->createBlock();
Ted Kremenek73543912007-08-23 21:42:29 +0000258 if (add_successor && Succ) B->addSuccessor(Succ);
259 return B;
260}
261
262/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000263/// we must reverse the statements because they have been inserted
264/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000265void CFGBuilder::FinishBlock(CFGBlock* B) {
266 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000267 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000268}
269
Ted Kremenek65cfa562007-08-27 21:27:44 +0000270/// addStmt - Used to add statements/expressions to the current CFGBlock
271/// "Block". This method calls WalkAST on the passed statement to see if it
272/// contains any short-circuit expressions. If so, it recursively creates
273/// the necessary blocks for such expressions. It returns the "topmost" block
274/// of the created blocks, or the original value of "Block" when this method
275/// was called if no additional blocks are created.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000276CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenek390b9762007-08-30 18:39:40 +0000277 if (!Block) Block = createBlock();
Ted Kremenek79f0a632008-04-16 21:10:48 +0000278 return WalkAST(Terminator,true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000279}
280
281/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000282/// add extra blocks for ternary operators, &&, and ||. We also
283/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000284CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
285 switch (Terminator->getStmtClass()) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000286 case Stmt::ConditionalOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000287 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000288
289 // Create the confluence block that will "merge" the results
290 // of the ternary expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000291 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
292 ConfluenceBlock->appendStmt(C);
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000293 FinishBlock(ConfluenceBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000294
295 // Create a block for the LHS expression if there is an LHS expression.
296 // A GCC extension allows LHS to be NULL, causing the condition to
297 // be the value that is returned instead.
298 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000299 Succ = ConfluenceBlock;
300 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000301 CFGBlock* LHSBlock = NULL;
302 if (C->getLHS()) {
303 LHSBlock = Visit(C->getLHS());
304 FinishBlock(LHSBlock);
305 Block = NULL;
306 }
Ted Kremenek65cfa562007-08-27 21:27:44 +0000307
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000308 // Create the block for the RHS expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000309 Succ = ConfluenceBlock;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000310 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000311 FinishBlock(RHSBlock);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000312
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000313 // Create the block that will contain the condition.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000314 Block = createBlock(false);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000315
316 if (LHSBlock)
317 Block->addSuccessor(LHSBlock);
318 else {
319 // If we have no LHS expression, add the ConfluenceBlock as a direct
320 // successor for the block containing the condition. Moreover,
321 // we need to reverse the order of the predecessors in the
322 // ConfluenceBlock because the RHSBlock will have been added to
323 // the succcessors already, and we want the first predecessor to the
324 // the block containing the expression for the case when the ternary
325 // expression evaluates to true.
326 Block->addSuccessor(ConfluenceBlock);
327 assert (ConfluenceBlock->pred_size() == 2);
328 std::reverse(ConfluenceBlock->pred_begin(),
329 ConfluenceBlock->pred_end());
330 }
331
Ted Kremenek65cfa562007-08-27 21:27:44 +0000332 Block->addSuccessor(RHSBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000333
Ted Kremenek65cfa562007-08-27 21:27:44 +0000334 Block->setTerminator(C);
335 return addStmt(C->getCond());
336 }
Ted Kremenek7f788422007-08-31 17:03:41 +0000337
338 case Stmt::ChooseExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000339 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek7f788422007-08-31 17:03:41 +0000340
341 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
342 ConfluenceBlock->appendStmt(C);
343 FinishBlock(ConfluenceBlock);
344
345 Succ = ConfluenceBlock;
346 Block = NULL;
347 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000348 FinishBlock(LHSBlock);
349
Ted Kremenek7f788422007-08-31 17:03:41 +0000350 Succ = ConfluenceBlock;
351 Block = NULL;
352 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000353 FinishBlock(RHSBlock);
Ted Kremenek7f788422007-08-31 17:03:41 +0000354
355 Block = createBlock(false);
356 Block->addSuccessor(LHSBlock);
357 Block->addSuccessor(RHSBlock);
358 Block->setTerminator(C);
359 return addStmt(C->getCond());
360 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000361
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000362 case Stmt::DeclStmtClass: {
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000363 DeclStmt *DS = cast<DeclStmt>(Terminator);
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000364 if (DS->isSingleDecl()) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000365 Block->appendStmt(Terminator);
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000366 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenek0865a992008-08-06 23:20:50 +0000367 }
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000368
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000369 CFGBlock* B = 0;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000370
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000371 // FIXME: Add a reverse iterator for DeclStmt to avoid this
372 // extra copy.
Chris Lattnerffae0dd2009-03-28 06:53:40 +0000373 typedef llvm::SmallVector<Decl*,10> BufTy;
374 BufTy Buf(DS->decl_begin(), DS->decl_end());
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000375
376 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
377 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
378 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
379 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenekbcc375a2008-10-06 20:56:19 +0000380
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000381 // Allocate the DeclStmt using the BumpPtrAllocator. It will
382 // get automatically freed with the CFG.
383 DeclGroupRef DG(*I);
384 Decl* D = *I;
385 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
386
Chris Lattnerffae0dd2009-03-28 06:53:40 +0000387 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000388
389 // Append the fake DeclStmt to block.
390 Block->appendStmt(DS);
391 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenek0865a992008-08-06 23:20:50 +0000392 }
Chris Lattner4a9a85e2009-03-28 06:33:19 +0000393 return B;
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000394 }
Ted Kremenek0865a992008-08-06 23:20:50 +0000395
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000396 case Stmt::AddrLabelExprClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000397 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000398 AddressTakenLabels.insert(A->getLabel());
399
Ted Kremenek79f0a632008-04-16 21:10:48 +0000400 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000401 return Block;
402 }
Ted Kremenekd11620d2007-09-11 21:29:43 +0000403
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000404 case Stmt::StmtExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000405 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremeneke822b622007-08-28 18:14:37 +0000406
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000407 case Stmt::SizeOfAlignOfExprClass: {
408 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000409
410 // VLA types have expressions that must be evaluated.
Sebastian Redl0cb7c872008-11-11 17:56:53 +0000411 if (E->isArgumentType()) {
412 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
413 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
414 addStmt(VA->getSizeExpr());
415 }
416 // Expressions in sizeof/alignof are not evaluated and thus have no
417 // control flow.
418 else
419 Block->appendStmt(Terminator);
Ted Kremenek09535672008-09-26 22:58:57 +0000420
421 return Block;
422 }
423
Ted Kremenekcfaae762007-08-27 21:54:41 +0000424 case Stmt::BinaryOperatorClass: {
Ted Kremenek79f0a632008-04-16 21:10:48 +0000425 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000426
427 if (B->isLogicalOp()) { // && or ||
428 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
429 ConfluenceBlock->appendStmt(B);
430 FinishBlock(ConfluenceBlock);
431
432 // create the block evaluating the LHS
433 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekb2348522007-12-21 19:49:00 +0000434 LHSBlock->setTerminator(B);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000435
436 // create the block evaluating the RHS
437 Succ = ConfluenceBlock;
438 Block = NULL;
439 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu7636e912008-10-04 05:48:38 +0000440 FinishBlock(RHSBlock);
Ted Kremenekb2348522007-12-21 19:49:00 +0000441
442 // Now link the LHSBlock with RHSBlock.
443 if (B->getOpcode() == BinaryOperator::LOr) {
444 LHSBlock->addSuccessor(ConfluenceBlock);
445 LHSBlock->addSuccessor(RHSBlock);
446 }
447 else {
448 assert (B->getOpcode() == BinaryOperator::LAnd);
449 LHSBlock->addSuccessor(RHSBlock);
450 LHSBlock->addSuccessor(ConfluenceBlock);
451 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000452
453 // Generate the blocks for evaluating the LHS.
454 Block = LHSBlock;
455 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000456 }
457 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
458 Block->appendStmt(B);
459 addStmt(B->getRHS());
460 return addStmt(B->getLHS());
Ted Kremenek3a819822007-10-01 19:33:33 +0000461 }
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000462
463 break;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000464 }
Ted Kremenekd68a8d32008-09-26 18:17:07 +0000465
466 // Blocks: No support for blocks ... yet
467 case Stmt::BlockExprClass:
468 case Stmt::BlockDeclRefExprClass:
469 return NYS();
Ted Kremeneka9ba5cc2008-02-26 02:37:08 +0000470
471 case Stmt::ParenExprClass:
Ted Kremenek79f0a632008-04-16 21:10:48 +0000472 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000473
Ted Kremenek65cfa562007-08-27 21:27:44 +0000474 default:
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000475 break;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000476 };
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000477
Ted Kremenek79f0a632008-04-16 21:10:48 +0000478 if (AlwaysAddStmt) Block->appendStmt(Terminator);
479 return WalkAST_VisitChildren(Terminator);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000480}
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000481
Ted Kremenek0865a992008-08-06 23:20:50 +0000482/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
483/// for initializers in Decls.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000484CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenek0865a992008-08-06 23:20:50 +0000485 VarDecl* VD = dyn_cast<VarDecl>(D);
486
487 if (!VD)
Ted Kremenekf4e35622007-11-18 20:06:01 +0000488 return Block;
489
Ted Kremenek0865a992008-08-06 23:20:50 +0000490 Expr* Init = VD->getInit();
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000491
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000492 if (Init) {
Mike Stumpd8472f22009-02-26 08:00:25 +0000493 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000494 switch (Init->getStmtClass()) {
495 case Stmt::IntegerLiteralClass:
496 case Stmt::CharacterLiteralClass:
497 case Stmt::StringLiteralClass:
498 break;
499 default:
500 Block = addStmt(Init);
501 }
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000502 }
Ted Kremenekdf8c7d72008-09-26 16:26:36 +0000503
504 // If the type of VD is a VLA, then we must process its size expressions.
505 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
506 VA = FindVA(VA->getElementType().getTypePtr()))
507 Block = addStmt(VA->getSizeExpr());
Ted Kremenek4ad64e82008-02-29 22:32:24 +0000508
Ted Kremeneke822b622007-08-28 18:14:37 +0000509 return Block;
510}
511
Ted Kremenek65cfa562007-08-27 21:27:44 +0000512/// WalkAST_VisitChildren - Utility method to call WalkAST on the
513/// children of a Stmt.
Ted Kremenek79f0a632008-04-16 21:10:48 +0000514CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000515 CFGBlock* B = Block;
Mike Stumpd8472f22009-02-26 08:00:25 +0000516 for (Stmt::child_iterator I = Terminator->child_begin(),
517 E = Terminator->child_end();
Ted Kremenek65cfa562007-08-27 21:27:44 +0000518 I != E; ++I)
Ted Kremenek680fcb82007-09-26 21:23:31 +0000519 if (*I) B = WalkAST(*I);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000520
521 return B;
522}
523
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000524/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
525/// expressions (a GCC extension).
Ted Kremenek79f0a632008-04-16 21:10:48 +0000526CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
527 Block->appendStmt(Terminator);
528 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000529}
530
Ted Kremenek73543912007-08-23 21:42:29 +0000531/// VisitStmt - Handle statements with no branching control flow.
532CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
533 // We cannot assume that we are in the middle of a basic block, since
534 // the CFG might only be constructed for this single statement. If
535 // we have no current basic block, just create one lazily.
536 if (!Block) Block = createBlock();
537
538 // Simply add the statement to the current block. We actually
539 // insert statements in reverse order; this order is reversed later
540 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000541 addStmt(Statement);
542
Ted Kremenek73543912007-08-23 21:42:29 +0000543 return Block;
544}
545
546CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
547 return Block;
548}
549
550CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000551
552 CFGBlock* LastBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000553
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000554 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
555 I != E; ++I ) {
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000556 LastBlock = Visit(*I);
Ted Kremenekfeb0e992008-02-26 00:22:58 +0000557 }
558
Ted Kremenek92e3ff92008-03-17 17:19:44 +0000559 return LastBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000560}
561
562CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
563 // We may see an if statement in the middle of a basic block, or
564 // it may be the first statement we are processing. In either case,
565 // we create a new basic block. First, we create the blocks for
566 // the then...else statements, and then we create the block containing
567 // the if statement. If we were in the middle of a block, we
568 // stop processing that block and reverse its statements. That block
569 // is then the implicit successor for the "then" and "else" clauses.
570
571 // The block we were proccessing is now finished. Make it the
572 // successor block.
573 if (Block) {
574 Succ = Block;
575 FinishBlock(Block);
576 }
577
578 // Process the false branch. NULL out Block so that the recursive
579 // call to Visit will create a new basic block.
580 // Null out Block so that all successor
581 CFGBlock* ElseBlock = Succ;
582
583 if (Stmt* Else = I->getElse()) {
584 SaveAndRestore<CFGBlock*> sv(Succ);
585
586 // NULL out Block so that the recursive call to Visit will
587 // create a new basic block.
588 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000589 ElseBlock = Visit(Else);
590
591 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
592 ElseBlock = sv.get();
593 else if (Block)
594 FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000595 }
596
597 // Process the true branch. NULL out Block so that the recursive
598 // call to Visit will create a new basic block.
599 // Null out Block so that all successor
600 CFGBlock* ThenBlock;
601 {
602 Stmt* Then = I->getThen();
603 assert (Then);
604 SaveAndRestore<CFGBlock*> sv(Succ);
605 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000606 ThenBlock = Visit(Then);
607
608 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
609 ThenBlock = sv.get();
610 else if (Block)
611 FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000612 }
613
614 // Now create a new block containing the if statement.
615 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000616
617 // Set the terminator of the new block to the If statement.
618 Block->setTerminator(I);
619
620 // Now add the successors.
621 Block->addSuccessor(ThenBlock);
622 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000623
624 // Add the condition as the last statement in the new block. This
625 // may create new blocks as the condition may contain control-flow. Any
626 // newly created blocks will be pointed to be "Block".
Ted Kremenek1eaa6712008-01-30 23:02:42 +0000627 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek73543912007-08-23 21:42:29 +0000628}
Ted Kremenekd11620d2007-09-11 21:29:43 +0000629
Ted Kremenek73543912007-08-23 21:42:29 +0000630
631CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
632 // If we were in the middle of a block we stop processing that block
633 // and reverse its statements.
634 //
635 // NOTE: If a "return" appears in the middle of a block, this means
636 // that the code afterwards is DEAD (unreachable). We still
637 // keep a basic block for that code; a simple "mark-and-sweep"
638 // from the entry block will be able to report such dead
639 // blocks.
640 if (Block) FinishBlock(Block);
641
642 // Create the new block.
643 Block = createBlock(false);
644
645 // The Exit block is the only successor.
646 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000647
648 // Add the return statement to the block. This may create new blocks
649 // if R contains control-flow (short-circuit operations).
650 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000651}
652
653CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
654 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek82e8a192008-03-15 07:45:02 +0000655 Visit(L->getSubStmt());
656 CFGBlock* LabelBlock = Block;
Ted Kremenek9b0d1b62007-08-30 18:20:57 +0000657
658 if (!LabelBlock) // This can happen when the body is empty, i.e.
659 LabelBlock=createBlock(); // scopes that only contains NullStmts.
660
Ted Kremenek73543912007-08-23 21:42:29 +0000661 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
662 LabelMap[ L ] = LabelBlock;
663
664 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekec055e12007-08-29 23:20:49 +0000665 // we were processing (L is the block's label). Because this is
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000666 // label (and we have already processed the substatement) there is no
667 // extra control-flow to worry about.
Ted Kremenekec055e12007-08-29 23:20:49 +0000668 LabelBlock->setLabel(L);
Ted Kremenek73543912007-08-23 21:42:29 +0000669 FinishBlock(LabelBlock);
670
671 // We set Block to NULL to allow lazy creation of a new block
672 // (if necessary);
673 Block = NULL;
674
675 // This block is now the implicit successor of other blocks.
676 Succ = LabelBlock;
677
678 return LabelBlock;
679}
680
681CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
682 // Goto is a control-flow statement. Thus we stop processing the
683 // current block and create a new one.
684 if (Block) FinishBlock(Block);
685 Block = createBlock(false);
686 Block->setTerminator(G);
687
688 // If we already know the mapping to the label block add the
689 // successor now.
690 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
691
692 if (I == LabelMap.end())
693 // We will need to backpatch this block later.
694 BackpatchBlocks.push_back(Block);
695 else
696 Block->addSuccessor(I->second);
697
698 return Block;
699}
700
701CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
702 // "for" is a control-flow statement. Thus we stop processing the
703 // current block.
704
705 CFGBlock* LoopSuccessor = NULL;
706
707 if (Block) {
708 FinishBlock(Block);
709 LoopSuccessor = Block;
710 }
711 else LoopSuccessor = Succ;
712
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000713 // Because of short-circuit evaluation, the condition of the loop
714 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
715 // blocks that evaluate the condition.
716 CFGBlock* ExitConditionBlock = createBlock(false);
717 CFGBlock* EntryConditionBlock = ExitConditionBlock;
718
719 // Set the terminator for the "exit" condition block.
720 ExitConditionBlock->setTerminator(F);
721
722 // Now add the actual condition to the condition block. Because the
723 // condition itself may contain control-flow, new blocks may be created.
724 if (Stmt* C = F->getCond()) {
725 Block = ExitConditionBlock;
726 EntryConditionBlock = addStmt(C);
727 if (Block) FinishBlock(EntryConditionBlock);
728 }
Ted Kremenek73543912007-08-23 21:42:29 +0000729
730 // The condition block is the implicit successor for the loop body as
731 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000732 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000733
734 // Now create the loop body.
735 {
736 assert (F->getBody());
737
738 // Save the current values for Block, Succ, and continue and break targets
739 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
740 save_continue(ContinueTargetBlock),
741 save_break(BreakTargetBlock);
Ted Kremenek77f93372008-09-04 21:48:47 +0000742
Ted Kremenek390b9762007-08-30 18:39:40 +0000743 // Create a new block to contain the (bottom) of the loop body.
744 Block = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000745
Ted Kremenek77f93372008-09-04 21:48:47 +0000746 if (Stmt* I = F->getInc()) {
747 // Generate increment code in its own basic block. This is the target
748 // of continue statements.
Ted Kremenekd19e99e2008-11-24 20:50:24 +0000749 Succ = Visit(I);
750
751 // Finish up the increment block if it hasn't been already.
752 if (Block) {
753 assert (Block == Succ);
754 FinishBlock(Block);
755 Block = 0;
756 }
757
Ted Kremenek77f93372008-09-04 21:48:47 +0000758 ContinueTargetBlock = Succ;
759 }
760 else {
761 // No increment code. Continues should go the the entry condition block.
762 ContinueTargetBlock = EntryConditionBlock;
763 }
764
765 // All breaks should go to the code following the loop.
766 BreakTargetBlock = LoopSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000767
768 // Now populate the body block, and in the process create new blocks
769 // as we walk the body of the loop.
770 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000771
772 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000773 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek390b9762007-08-30 18:39:40 +0000774 else if (Block)
775 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000776
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000777 // This new body block is a successor to our "exit" condition block.
778 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000779 }
780
781 // Link up the condition block with the code that follows the loop.
782 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000783 ExitConditionBlock->addSuccessor(LoopSuccessor);
784
Ted Kremenek73543912007-08-23 21:42:29 +0000785 // If the loop contains initialization, create a new block for those
786 // statements. This block can also contain statements that precede
787 // the loop.
788 if (Stmt* I = F->getInit()) {
789 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000790 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000791 }
792 else {
793 // There is no loop initialization. We are thus basically a while
794 // loop. NULL out Block to force lazy block construction.
795 Block = NULL;
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000796 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000797 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000798 }
799}
800
Ted Kremenek05335162008-11-11 17:10:00 +0000801CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
802 // Objective-C fast enumeration 'for' statements:
803 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
804 //
805 // for ( Type newVariable in collection_expression ) { statements }
806 //
807 // becomes:
808 //
809 // prologue:
810 // 1. collection_expression
811 // T. jump to loop_entry
812 // loop_entry:
Ted Kremenek65514842008-11-14 01:57:41 +0000813 // 1. side-effects of element expression
Ted Kremenek05335162008-11-11 17:10:00 +0000814 // 1. ObjCForCollectionStmt [performs binding to newVariable]
815 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
816 // TB:
817 // statements
818 // T. jump to loop_entry
819 // FB:
820 // what comes after
821 //
822 // and
823 //
824 // Type existingItem;
825 // for ( existingItem in expression ) { statements }
826 //
827 // becomes:
828 //
829 // the same with newVariable replaced with existingItem; the binding
830 // works the same except that for one ObjCForCollectionStmt::getElement()
831 // returns a DeclStmt and the other returns a DeclRefExpr.
832 //
833
834 CFGBlock* LoopSuccessor = 0;
835
836 if (Block) {
837 FinishBlock(Block);
838 LoopSuccessor = Block;
839 Block = 0;
840 }
841 else LoopSuccessor = Succ;
842
Ted Kremenek65514842008-11-14 01:57:41 +0000843 // Build the condition blocks.
844 CFGBlock* ExitConditionBlock = createBlock(false);
845 CFGBlock* EntryConditionBlock = ExitConditionBlock;
846
847 // Set the terminator for the "exit" condition block.
848 ExitConditionBlock->setTerminator(S);
849
850 // The last statement in the block should be the ObjCForCollectionStmt,
851 // which performs the actual binding to 'element' and determines if there
852 // are any more items in the collection.
853 ExitConditionBlock->appendStmt(S);
854 Block = ExitConditionBlock;
855
856 // Walk the 'element' expression to see if there are any side-effects. We
857 // generate new blocks as necesary. We DON'T add the statement by default
858 // to the CFG unless it contains control-flow.
859 EntryConditionBlock = WalkAST(S->getElement(), false);
860 if (Block) { FinishBlock(EntryConditionBlock); Block = 0; }
861
862 // The condition block is the implicit successor for the loop body as
863 // well as any code above the loop.
864 Succ = EntryConditionBlock;
Ted Kremenek05335162008-11-11 17:10:00 +0000865
866 // Now create the true branch.
Ted Kremenek65514842008-11-14 01:57:41 +0000867 {
868 // Save the current values for Succ, continue and break targets.
869 SaveAndRestore<CFGBlock*> save_Succ(Succ),
870 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
871
872 BreakTargetBlock = LoopSuccessor;
873 ContinueTargetBlock = EntryConditionBlock;
874
875 CFGBlock* BodyBlock = Visit(S->getBody());
876
877 if (!BodyBlock)
878 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
879 else if (Block)
880 FinishBlock(BodyBlock);
881
882 // This new body block is a successor to our "exit" condition block.
883 ExitConditionBlock->addSuccessor(BodyBlock);
884 }
Ted Kremenekf5383072008-11-13 06:36:45 +0000885
Ted Kremenek65514842008-11-14 01:57:41 +0000886 // Link up the condition block with the code that follows the loop.
887 // (the false branch).
888 ExitConditionBlock->addSuccessor(LoopSuccessor);
889
Ted Kremenek05335162008-11-11 17:10:00 +0000890 // Now create a prologue block to contain the collection expression.
Ted Kremenek65514842008-11-14 01:57:41 +0000891 Block = createBlock();
Ted Kremenek05335162008-11-11 17:10:00 +0000892 return addStmt(S->getCollection());
893}
Ted Kremenek72037962009-03-30 22:29:21 +0000894
895CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
896 // Process the statements of the @finally block.
897 if (ObjCAtFinallyStmt *FS = S->getFinallyStmt())
898 Visit(FS->getFinallyBody());
899
900 // FIXME: Handle the @catch statements.
901
902 // Process the try body
903 return Visit(S->getTryBody());
904}
Ted Kremenek05335162008-11-11 17:10:00 +0000905
Ted Kremenek73543912007-08-23 21:42:29 +0000906CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
907 // "while" is a control-flow statement. Thus we stop processing the
908 // current block.
909
910 CFGBlock* LoopSuccessor = NULL;
911
912 if (Block) {
913 FinishBlock(Block);
914 LoopSuccessor = Block;
915 }
916 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000917
918 // Because of short-circuit evaluation, the condition of the loop
919 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
920 // blocks that evaluate the condition.
921 CFGBlock* ExitConditionBlock = createBlock(false);
922 CFGBlock* EntryConditionBlock = ExitConditionBlock;
923
924 // Set the terminator for the "exit" condition block.
925 ExitConditionBlock->setTerminator(W);
926
927 // Now add the actual condition to the condition block. Because the
928 // condition itself may contain control-flow, new blocks may be created.
929 // Thus we update "Succ" after adding the condition.
930 if (Stmt* C = W->getCond()) {
931 Block = ExitConditionBlock;
932 EntryConditionBlock = addStmt(C);
Ted Kremenekd0c87602008-02-27 00:28:17 +0000933 assert (Block == EntryConditionBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000934 if (Block) FinishBlock(EntryConditionBlock);
935 }
Ted Kremenek73543912007-08-23 21:42:29 +0000936
937 // The condition block is the implicit successor for the loop body as
938 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000939 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000940
941 // Process the loop body.
942 {
943 assert (W->getBody());
944
945 // Save the current values for Block, Succ, and continue and break targets
946 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
947 save_continue(ContinueTargetBlock),
948 save_break(BreakTargetBlock);
949
950 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000951 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000952
953 // All breaks should go to the code following the loop.
954 BreakTargetBlock = LoopSuccessor;
955
956 // NULL out Block to force lazy instantiation of blocks for the body.
957 Block = NULL;
958
959 // Create the body. The returned block is the entry to the loop body.
960 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000961
962 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +0000963 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek390b9762007-08-30 18:39:40 +0000964 else if (Block)
965 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000966
967 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000968 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000969 }
970
971 // Link up the condition block with the code that follows the loop.
972 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000973 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000974
975 // There can be no more statements in the condition block
976 // since we loop back to this block. NULL out Block to force
977 // lazy creation of another block.
978 Block = NULL;
979
980 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +0000981 Succ = EntryConditionBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000982 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000983}
Ted Kremenekc74ac3e2008-12-09 20:20:09 +0000984
985CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
986 // FIXME: This isn't complete. We basically treat @throw like a return
987 // statement.
988
989 // If we were in the middle of a block we stop processing that block
990 // and reverse its statements.
991 if (Block) FinishBlock(Block);
992
993 // Create the new block.
994 Block = createBlock(false);
995
996 // The Exit block is the only successor.
997 Block->addSuccessor(&cfg->getExit());
998
999 // Add the statement to the block. This may create new blocks
1000 // if S contains control-flow (short-circuit operations).
1001 return addStmt(S);
1002}
Ted Kremenek73543912007-08-23 21:42:29 +00001003
1004CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1005 // "do...while" is a control-flow statement. Thus we stop processing the
1006 // current block.
1007
1008 CFGBlock* LoopSuccessor = NULL;
1009
1010 if (Block) {
1011 FinishBlock(Block);
1012 LoopSuccessor = Block;
1013 }
1014 else LoopSuccessor = Succ;
1015
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001016 // Because of short-circuit evaluation, the condition of the loop
1017 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
1018 // blocks that evaluate the condition.
1019 CFGBlock* ExitConditionBlock = createBlock(false);
1020 CFGBlock* EntryConditionBlock = ExitConditionBlock;
1021
1022 // Set the terminator for the "exit" condition block.
1023 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +00001024
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001025 // Now add the actual condition to the condition block. Because the
1026 // condition itself may contain control-flow, new blocks may be created.
1027 if (Stmt* C = D->getCond()) {
1028 Block = ExitConditionBlock;
1029 EntryConditionBlock = addStmt(C);
1030 if (Block) FinishBlock(EntryConditionBlock);
1031 }
Ted Kremenek73543912007-08-23 21:42:29 +00001032
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001033 // The condition block is the implicit successor for the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001034 Succ = EntryConditionBlock;
1035
Ted Kremenek73543912007-08-23 21:42:29 +00001036 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001037 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +00001038 {
1039 assert (D->getBody());
1040
1041 // Save the current values for Block, Succ, and continue and break targets
1042 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1043 save_continue(ContinueTargetBlock),
1044 save_break(BreakTargetBlock);
1045
1046 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001047 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001048
1049 // All breaks should go to the code following the loop.
1050 BreakTargetBlock = LoopSuccessor;
1051
1052 // NULL out Block to force lazy instantiation of blocks for the body.
1053 Block = NULL;
1054
1055 // Create the body. The returned block is the entry to the loop body.
1056 BodyBlock = Visit(D->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +00001057
Ted Kremenek390b9762007-08-30 18:39:40 +00001058 if (!BodyBlock)
Ted Kremenekd0c87602008-02-27 00:28:17 +00001059 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek390b9762007-08-30 18:39:40 +00001060 else if (Block)
1061 FinishBlock(BodyBlock);
1062
Ted Kremenek73543912007-08-23 21:42:29 +00001063 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001064 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001065 }
1066
1067 // Link up the condition block with the code that follows the loop.
1068 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001069 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +00001070
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001071 // There can be no more statements in the body block(s)
1072 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +00001073 // lazy creation of another block.
1074 Block = NULL;
1075
1076 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek9ff572c2008-02-27 07:20:00 +00001077 Succ = BodyBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001078 return BodyBlock;
1079}
1080
1081CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1082 // "continue" is a control-flow statement. Thus we stop processing the
1083 // current block.
1084 if (Block) FinishBlock(Block);
1085
1086 // Now create a new block that ends with the continue statement.
1087 Block = createBlock(false);
1088 Block->setTerminator(C);
1089
1090 // If there is no target for the continue, then we are looking at an
1091 // incomplete AST. Handle this by not registering a successor.
1092 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1093
1094 return Block;
1095}
1096
1097CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1098 // "break" is a control-flow statement. Thus we stop processing the
1099 // current block.
1100 if (Block) FinishBlock(Block);
1101
1102 // Now create a new block that ends with the continue statement.
1103 Block = createBlock(false);
1104 Block->setTerminator(B);
1105
1106 // If there is no target for the break, then we are looking at an
1107 // incomplete AST. Handle this by not registering a successor.
1108 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1109
1110 return Block;
1111}
1112
Ted Kremenek79f0a632008-04-16 21:10:48 +00001113CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek73543912007-08-23 21:42:29 +00001114 // "switch" is a control-flow statement. Thus we stop processing the
1115 // current block.
1116 CFGBlock* SwitchSuccessor = NULL;
1117
1118 if (Block) {
1119 FinishBlock(Block);
1120 SwitchSuccessor = Block;
1121 }
1122 else SwitchSuccessor = Succ;
1123
1124 // Save the current "switch" context.
1125 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek97bc3422008-02-13 22:05:39 +00001126 save_break(BreakTargetBlock),
1127 save_default(DefaultCaseBlock);
1128
1129 // Set the "default" case to be the block after the switch statement.
1130 // If the switch statement contains a "default:", this value will
1131 // be overwritten with the block for that code.
1132 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001133
Ted Kremenek73543912007-08-23 21:42:29 +00001134 // Create a new block that will contain the switch statement.
1135 SwitchTerminatedBlock = createBlock(false);
1136
Ted Kremenek73543912007-08-23 21:42:29 +00001137 // Now process the switch body. The code after the switch is the implicit
1138 // successor.
1139 Succ = SwitchSuccessor;
1140 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +00001141
1142 // When visiting the body, the case statements should automatically get
1143 // linked up to the switch. We also don't keep a pointer to the body,
1144 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001145 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001146 Block = NULL;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001147 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001148 if (Block) FinishBlock(BodyBlock);
1149
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001150 // If we have no "default:" case, the default transition is to the
1151 // code following the switch body.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001152 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001153
Ted Kremenekcfee50c2007-08-27 19:46:09 +00001154 // Add the terminator and condition in the switch block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001155 SwitchTerminatedBlock->setTerminator(Terminator);
1156 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +00001157 Block = SwitchTerminatedBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001158
Ted Kremenek79f0a632008-04-16 21:10:48 +00001159 return addStmt(Terminator->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +00001160}
1161
Ted Kremenek79f0a632008-04-16 21:10:48 +00001162CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenek97bc3422008-02-13 22:05:39 +00001163 // CaseStmts are essentially labels, so they are the
Ted Kremenek73543912007-08-23 21:42:29 +00001164 // first statement in a block.
Ted Kremenek44659d82007-08-30 18:48:11 +00001165
Ted Kremenek79f0a632008-04-16 21:10:48 +00001166 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek44659d82007-08-30 18:48:11 +00001167 CFGBlock* CaseBlock = Block;
1168 if (!CaseBlock) CaseBlock = createBlock();
1169
Ted Kremenek97bc3422008-02-13 22:05:39 +00001170 // Cases statements partition blocks, so this is the top of
1171 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001172 CaseBlock->setLabel(Terminator);
Ted Kremenek73543912007-08-23 21:42:29 +00001173 FinishBlock(CaseBlock);
1174
1175 // Add this block to the list of successors for the block with the
1176 // switch statement.
Ted Kremenek97bc3422008-02-13 22:05:39 +00001177 assert (SwitchTerminatedBlock);
1178 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +00001179
1180 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1181 Block = NULL;
1182
1183 // This block is now the implicit successor of other blocks.
1184 Succ = CaseBlock;
1185
Ted Kremenek82e8a192008-03-15 07:45:02 +00001186 return CaseBlock;
Ted Kremenek73543912007-08-23 21:42:29 +00001187}
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001188
Ted Kremenek79f0a632008-04-16 21:10:48 +00001189CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1190 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek97bc3422008-02-13 22:05:39 +00001191 DefaultCaseBlock = Block;
1192 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1193
1194 // Default statements partition blocks, so this is the top of
1195 // the basic block we were processing (the "default:" is the label).
Ted Kremenek79f0a632008-04-16 21:10:48 +00001196 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek97bc3422008-02-13 22:05:39 +00001197 FinishBlock(DefaultCaseBlock);
1198
1199 // Unlike case statements, we don't add the default block to the
1200 // successors for the switch statement immediately. This is done
1201 // when we finish processing the switch statement. This allows for
1202 // the default case (including a fall-through to the code after the
1203 // switch statement) to always be the last successor of a switch-terminated
1204 // block.
1205
1206 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1207 Block = NULL;
1208
1209 // This block is now the implicit successor of other blocks.
1210 Succ = DefaultCaseBlock;
1211
1212 return DefaultCaseBlock;
Ted Kremenekc07a8af2008-02-13 21:46:34 +00001213}
Ted Kremenek73543912007-08-23 21:42:29 +00001214
Ted Kremenek0edd3a92007-08-28 19:26:49 +00001215CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1216 // Lazily create the indirect-goto dispatch block if there isn't one
1217 // already.
1218 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1219
1220 if (!IBlock) {
1221 IBlock = createBlock(false);
1222 cfg->setIndirectGotoBlock(IBlock);
1223 }
1224
1225 // IndirectGoto is a control-flow statement. Thus we stop processing the
1226 // current block and create a new one.
1227 if (Block) FinishBlock(Block);
1228 Block = createBlock(false);
1229 Block->setTerminator(I);
1230 Block->addSuccessor(IBlock);
1231 return addStmt(I->getTarget());
1232}
1233
Ted Kremenek73543912007-08-23 21:42:29 +00001234
Ted Kremenekd6e50602007-08-23 21:26:19 +00001235} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +00001236
1237/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1238/// block has no successors or predecessors. If this is the first block
1239/// created in the CFG, it is automatically set to be the Entry and Exit
1240/// of the CFG.
Ted Kremenek14594572007-09-05 20:02:05 +00001241CFGBlock* CFG::createBlock() {
Ted Kremenek4db5b452007-08-23 16:51:22 +00001242 bool first_block = begin() == end();
1243
1244 // Create the block.
Ted Kremenek14594572007-09-05 20:02:05 +00001245 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek4db5b452007-08-23 16:51:22 +00001246
1247 // If this is the first block, set it as the Entry and Exit.
1248 if (first_block) Entry = Exit = &front();
1249
1250 // Return the block.
1251 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +00001252}
1253
Ted Kremenek4db5b452007-08-23 16:51:22 +00001254/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1255/// CFG is returned to the caller.
1256CFG* CFG::buildCFG(Stmt* Statement) {
1257 CFGBuilder Builder;
1258 return Builder.buildCFG(Statement);
1259}
1260
1261/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +00001262void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1263
Ted Kremenek3a819822007-10-01 19:33:33 +00001264//===----------------------------------------------------------------------===//
1265// CFG: Queries for BlkExprs.
1266//===----------------------------------------------------------------------===//
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001267
Ted Kremenek3a819822007-10-01 19:33:33 +00001268namespace {
Ted Kremenekab6c5902008-01-17 20:48:37 +00001269 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek3a819822007-10-01 19:33:33 +00001270}
1271
Ted Kremenek79f0a632008-04-16 21:10:48 +00001272static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1273 if (!Terminator)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001274 return;
1275
Ted Kremenek79f0a632008-04-16 21:10:48 +00001276 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001277 if (!*I) continue;
1278
1279 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1280 if (B->isAssignmentOp()) Set.insert(B);
1281
1282 FindSubExprAssignments(*I, Set);
1283 }
1284}
1285
Ted Kremenek3a819822007-10-01 19:33:33 +00001286static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1287 BlkExprMapTy* M = new BlkExprMapTy();
1288
Ted Kremenekc6fda602008-01-26 00:03:27 +00001289 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek79f0a632008-04-16 21:10:48 +00001290 // only assignments that we want to *possibly* register as a block-level
1291 // expression. Basically, if an assignment occurs both in a subexpression
1292 // and at the block-level, it is a block-level expression.
Ted Kremenekc6fda602008-01-26 00:03:27 +00001293 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1294
Ted Kremenek3a819822007-10-01 19:33:33 +00001295 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1296 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc6fda602008-01-26 00:03:27 +00001297 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001298
Ted Kremenek79f0a632008-04-16 21:10:48 +00001299 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1300
1301 // Iterate over the statements again on identify the Expr* and Stmt* at
1302 // the block-level that are block-level expressions.
1303
Ted Kremenekc6fda602008-01-26 00:03:27 +00001304 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek79f0a632008-04-16 21:10:48 +00001305 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001306
Ted Kremenek79f0a632008-04-16 21:10:48 +00001307 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001308 // Assignment expressions that are not nested within another
1309 // expression are really "statements" whose value is never
1310 // used by another expression.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001311 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenekc6fda602008-01-26 00:03:27 +00001312 continue;
1313 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001314 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001315 // Special handling for statement expressions. The last statement
1316 // in the statement expression is also a block-level expr.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001317 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001318 if (!C->body_empty()) {
Ted Kremenekc6fda602008-01-26 00:03:27 +00001319 unsigned x = M->size();
Ted Kremenekab6c5902008-01-17 20:48:37 +00001320 (*M)[C->body_back()] = x;
1321 }
1322 }
Ted Kremenek5b4eb172008-01-25 23:22:27 +00001323
Ted Kremenekc6fda602008-01-26 00:03:27 +00001324 unsigned x = M->size();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001325 (*M)[Exp] = x;
Ted Kremenekc6fda602008-01-26 00:03:27 +00001326 }
1327
Ted Kremenek79f0a632008-04-16 21:10:48 +00001328 // Look at terminators. The condition is a block-level expression.
1329
Ted Kremenek16516e22008-11-12 21:11:49 +00001330 Stmt* S = I->getTerminatorCondition();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001331
Ted Kremenek16516e22008-11-12 21:11:49 +00001332 if (S && M->find(S) == M->end()) {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001333 unsigned x = M->size();
Ted Kremenek16516e22008-11-12 21:11:49 +00001334 (*M)[S] = x;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001335 }
1336 }
1337
Ted Kremenek3a819822007-10-01 19:33:33 +00001338 return M;
1339}
1340
Ted Kremenekab6c5902008-01-17 20:48:37 +00001341CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1342 assert(S != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001343 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1344
1345 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenekab6c5902008-01-17 20:48:37 +00001346 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3a819822007-10-01 19:33:33 +00001347
1348 if (I == M->end()) return CFG::BlkExprNumTy();
1349 else return CFG::BlkExprNumTy(I->second);
1350}
1351
1352unsigned CFG::getNumBlkExprs() {
1353 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1354 return M->size();
1355 else {
1356 // We assume callers interested in the number of BlkExprs will want
1357 // the map constructed if it doesn't already exist.
1358 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1359 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1360 }
1361}
1362
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001363//===----------------------------------------------------------------------===//
Ted Kremenekd058a9c2008-04-28 18:00:46 +00001364// Cleanup: CFG dstor.
1365//===----------------------------------------------------------------------===//
1366
Ted Kremenek3a819822007-10-01 19:33:33 +00001367CFG::~CFG() {
1368 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1369}
1370
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001371//===----------------------------------------------------------------------===//
1372// CFG pretty printing
1373//===----------------------------------------------------------------------===//
1374
Ted Kremenekd8313202007-08-22 18:22:34 +00001375namespace {
1376
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001377class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek86afc042007-08-31 22:26:13 +00001378
Ted Kremenek08176a52007-08-31 21:30:12 +00001379 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1380 StmtMapTy StmtMap;
1381 signed CurrentBlock;
1382 unsigned CurrentStmt;
Ted Kremenek86afc042007-08-31 22:26:13 +00001383
Ted Kremenek73543912007-08-23 21:42:29 +00001384public:
Ted Kremenek86afc042007-08-31 22:26:13 +00001385
Ted Kremenek08176a52007-08-31 21:30:12 +00001386 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1387 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1388 unsigned j = 1;
1389 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1390 BI != BEnd; ++BI, ++j )
1391 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1392 }
1393 }
1394
1395 virtual ~StmtPrinterHelper() {}
1396
1397 void setBlockID(signed i) { CurrentBlock = i; }
1398 void setStmtID(unsigned i) { CurrentStmt = i; }
1399
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001400 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001401
Ted Kremenek79f0a632008-04-16 21:10:48 +00001402 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek08176a52007-08-31 21:30:12 +00001403
1404 if (I == StmtMap.end())
1405 return false;
1406
1407 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1408 && I->second.second == CurrentStmt)
1409 return false;
1410
Ted Kremenek86afc042007-08-31 22:26:13 +00001411 OS << "[B" << I->second.first << "." << I->second.second << "]";
1412 return true;
Ted Kremenek08176a52007-08-31 21:30:12 +00001413 }
1414};
1415
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001416class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1417 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1418
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001419 llvm::raw_ostream& OS;
Ted Kremenek08176a52007-08-31 21:30:12 +00001420 StmtPrinterHelper* Helper;
1421public:
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001422 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek08176a52007-08-31 21:30:12 +00001423 : OS(os), Helper(helper) {}
Ted Kremenek73543912007-08-23 21:42:29 +00001424
1425 void VisitIfStmt(IfStmt* I) {
1426 OS << "if ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001427 I->getCond()->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001428 }
1429
1430 // Default case.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001431 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenek73543912007-08-23 21:42:29 +00001432
1433 void VisitForStmt(ForStmt* F) {
1434 OS << "for (" ;
Ted Kremenek23a1d662007-08-30 21:28:02 +00001435 if (F->getInit()) OS << "...";
1436 OS << "; ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001437 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek23a1d662007-08-30 21:28:02 +00001438 OS << "; ";
1439 if (F->getInc()) OS << "...";
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001440 OS << ")";
Ted Kremenek73543912007-08-23 21:42:29 +00001441 }
1442
1443 void VisitWhileStmt(WhileStmt* W) {
1444 OS << "while " ;
Ted Kremenek08176a52007-08-31 21:30:12 +00001445 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001446 }
1447
1448 void VisitDoStmt(DoStmt* D) {
1449 OS << "do ... while ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001450 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001451 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001452
Ted Kremenek79f0a632008-04-16 21:10:48 +00001453 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek65cfa562007-08-27 21:27:44 +00001454 OS << "switch ";
Ted Kremenek79f0a632008-04-16 21:10:48 +00001455 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001456 }
1457
Ted Kremenek621e1592007-08-31 21:49:40 +00001458 void VisitConditionalOperator(ConditionalOperator* C) {
1459 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001460 OS << " ? ... : ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001461 }
1462
Ted Kremenek2025cc92007-08-31 22:29:13 +00001463 void VisitChooseExpr(ChooseExpr* C) {
1464 OS << "__builtin_choose_expr( ";
1465 C->getCond()->printPretty(OS,Helper);
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001466 OS << " )";
Ted Kremenek2025cc92007-08-31 22:29:13 +00001467 }
1468
Ted Kremenek86afc042007-08-31 22:26:13 +00001469 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1470 OS << "goto *";
1471 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001472 }
1473
Ted Kremenek621e1592007-08-31 21:49:40 +00001474 void VisitBinaryOperator(BinaryOperator* B) {
1475 if (!B->isLogicalOp()) {
1476 VisitExpr(B);
1477 return;
1478 }
1479
1480 B->getLHS()->printPretty(OS,Helper);
1481
1482 switch (B->getOpcode()) {
1483 case BinaryOperator::LOr:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001484 OS << " || ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001485 return;
1486 case BinaryOperator::LAnd:
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001487 OS << " && ...";
Ted Kremenek621e1592007-08-31 21:49:40 +00001488 return;
1489 default:
1490 assert(false && "Invalid logical operator.");
1491 }
1492 }
1493
Ted Kremenekcfaae762007-08-27 21:54:41 +00001494 void VisitExpr(Expr* E) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001495 E->printPretty(OS,Helper);
Ted Kremenekcfaae762007-08-27 21:54:41 +00001496 }
Ted Kremenek73543912007-08-23 21:42:29 +00001497};
Ted Kremenek08176a52007-08-31 21:30:12 +00001498
1499
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001500void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001501 if (Helper) {
1502 // special printing for statement-expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001503 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001504 CompoundStmt* Sub = SE->getSubStmt();
1505
1506 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001507 OS << "({ ... ; ";
Ted Kremenek256a2592007-10-29 20:41:04 +00001508 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001509 OS << " })\n";
Ted Kremenek86afc042007-08-31 22:26:13 +00001510 return;
1511 }
1512 }
1513
1514 // special printing for comma expressions.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001515 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek86afc042007-08-31 22:26:13 +00001516 if (B->getOpcode() == BinaryOperator::Comma) {
1517 OS << "... , ";
1518 Helper->handledStmt(B->getRHS(),OS);
1519 OS << '\n';
1520 return;
1521 }
1522 }
1523 }
1524
Ted Kremenek79f0a632008-04-16 21:10:48 +00001525 Terminator->printPretty(OS, Helper);
Ted Kremenek86afc042007-08-31 22:26:13 +00001526
1527 // Expressions need a newline.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001528 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek86afc042007-08-31 22:26:13 +00001529}
1530
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001531void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek08176a52007-08-31 21:30:12 +00001532 StmtPrinterHelper* Helper, bool print_edges) {
1533
1534 if (Helper) Helper->setBlockID(B.getBlockID());
1535
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001536 // Print the header.
Ted Kremenek08176a52007-08-31 21:30:12 +00001537 OS << "\n [ B" << B.getBlockID();
1538
1539 if (&B == &cfg->getEntry())
1540 OS << " (ENTRY) ]\n";
1541 else if (&B == &cfg->getExit())
1542 OS << " (EXIT) ]\n";
1543 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001544 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001545 else
1546 OS << " ]\n";
1547
Ted Kremenekec055e12007-08-29 23:20:49 +00001548 // Print the label of this block.
Ted Kremenek79f0a632008-04-16 21:10:48 +00001549 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001550
1551 if (print_edges)
1552 OS << " ";
1553
Ted Kremenek79f0a632008-04-16 21:10:48 +00001554 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001555 OS << L->getName();
Ted Kremenek79f0a632008-04-16 21:10:48 +00001556 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenekec055e12007-08-29 23:20:49 +00001557 OS << "case ";
1558 C->getLHS()->printPretty(OS);
1559 if (C->getRHS()) {
1560 OS << " ... ";
1561 C->getRHS()->printPretty(OS);
1562 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001563 }
Ted Kremenek79f0a632008-04-16 21:10:48 +00001564 else if (isa<DefaultStmt>(Terminator))
Ted Kremenekec055e12007-08-29 23:20:49 +00001565 OS << "default";
Ted Kremenek08176a52007-08-31 21:30:12 +00001566 else
1567 assert(false && "Invalid label statement in CFGBlock.");
1568
Ted Kremenekec055e12007-08-29 23:20:49 +00001569 OS << ":\n";
1570 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001571
Ted Kremenek97f75312007-08-21 21:42:03 +00001572 // Iterate through the statements in the block and print them.
Ted Kremenek97f75312007-08-21 21:42:03 +00001573 unsigned j = 1;
Ted Kremenek08176a52007-08-31 21:30:12 +00001574
1575 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1576 I != E ; ++I, ++j ) {
1577
Ted Kremenekec055e12007-08-29 23:20:49 +00001578 // Print the statement # in the basic block and the statement itself.
Ted Kremenek08176a52007-08-31 21:30:12 +00001579 if (print_edges)
1580 OS << " ";
1581
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001582 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001583
1584 if (Helper)
1585 Helper->setStmtID(j);
Ted Kremenek86afc042007-08-31 22:26:13 +00001586
1587 print_stmt(OS,Helper,*I);
Ted Kremenek97f75312007-08-21 21:42:03 +00001588 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001589
Ted Kremenekec055e12007-08-29 23:20:49 +00001590 // Print the terminator of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001591 if (B.getTerminator()) {
1592 if (print_edges)
1593 OS << " ";
1594
Ted Kremenekec055e12007-08-29 23:20:49 +00001595 OS << " T: ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001596
1597 if (Helper) Helper->setBlockID(-1);
1598
1599 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1600 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001601 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001602 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001603
Ted Kremenekec055e12007-08-29 23:20:49 +00001604 if (print_edges) {
1605 // Print the predecessors of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001606 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenekec055e12007-08-29 23:20:49 +00001607 unsigned i = 0;
Ted Kremenekec055e12007-08-29 23:20:49 +00001608
Ted Kremenek08176a52007-08-31 21:30:12 +00001609 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1610 I != E; ++I, ++i) {
1611
1612 if (i == 8 || (i-8) == 0)
1613 OS << "\n ";
1614
Ted Kremenekec055e12007-08-29 23:20:49 +00001615 OS << " B" << (*I)->getBlockID();
1616 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001617
1618 OS << '\n';
1619
1620 // Print the successors of this block.
1621 OS << " Successors (" << B.succ_size() << "):";
1622 i = 0;
1623
1624 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1625 I != E; ++I, ++i) {
1626
1627 if (i == 8 || (i-8) % 10 == 0)
1628 OS << "\n ";
1629
1630 OS << " B" << (*I)->getBlockID();
1631 }
1632
Ted Kremenekec055e12007-08-29 23:20:49 +00001633 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001634 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001635}
1636
1637} // end anonymous namespace
1638
1639/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001640void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001641
1642/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001643void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001644
1645 StmtPrinterHelper Helper(this);
1646
1647 // Print the entry block.
1648 print_block(OS, this, getEntry(), &Helper, true);
1649
1650 // Iterate through the CFGBlocks and print them one by one.
1651 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1652 // Skip the entry block, because we already printed it.
1653 if (&(*I) == &getEntry() || &(*I) == &getExit())
1654 continue;
1655
1656 print_block(OS, this, *I, &Helper, true);
1657 }
1658
1659 // Print the exit block.
1660 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd19e99e2008-11-24 20:50:24 +00001661 OS.flush();
Ted Kremenek08176a52007-08-31 21:30:12 +00001662}
1663
1664/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001665void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001666
1667/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1668/// Generally this will only be called from CFG::print.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001669void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek08176a52007-08-31 21:30:12 +00001670 StmtPrinterHelper Helper(cfg);
1671 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek4db5b452007-08-23 16:51:22 +00001672}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001673
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001674/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001675void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001676 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1677 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1678}
1679
Ted Kremenek16516e22008-11-12 21:11:49 +00001680Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek79f0a632008-04-16 21:10:48 +00001681
1682 if (!Terminator)
1683 return NULL;
1684
1685 Expr* E = NULL;
1686
1687 switch (Terminator->getStmtClass()) {
1688 default:
1689 break;
1690
1691 case Stmt::ForStmtClass:
1692 E = cast<ForStmt>(Terminator)->getCond();
1693 break;
1694
1695 case Stmt::WhileStmtClass:
1696 E = cast<WhileStmt>(Terminator)->getCond();
1697 break;
1698
1699 case Stmt::DoStmtClass:
1700 E = cast<DoStmt>(Terminator)->getCond();
1701 break;
1702
1703 case Stmt::IfStmtClass:
1704 E = cast<IfStmt>(Terminator)->getCond();
1705 break;
1706
1707 case Stmt::ChooseExprClass:
1708 E = cast<ChooseExpr>(Terminator)->getCond();
1709 break;
1710
1711 case Stmt::IndirectGotoStmtClass:
1712 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1713 break;
1714
1715 case Stmt::SwitchStmtClass:
1716 E = cast<SwitchStmt>(Terminator)->getCond();
1717 break;
1718
1719 case Stmt::ConditionalOperatorClass:
1720 E = cast<ConditionalOperator>(Terminator)->getCond();
1721 break;
1722
1723 case Stmt::BinaryOperatorClass: // '&&' and '||'
1724 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek16516e22008-11-12 21:11:49 +00001725 break;
1726
1727 case Stmt::ObjCForCollectionStmtClass:
1728 return Terminator;
Ted Kremenek79f0a632008-04-16 21:10:48 +00001729 }
1730
1731 return E ? E->IgnoreParens() : NULL;
1732}
1733
Ted Kremenekbdbd1b52008-05-16 16:06:00 +00001734bool CFGBlock::hasBinaryBranchTerminator() const {
1735
1736 if (!Terminator)
1737 return false;
1738
1739 Expr* E = NULL;
1740
1741 switch (Terminator->getStmtClass()) {
1742 default:
1743 return false;
1744
1745 case Stmt::ForStmtClass:
1746 case Stmt::WhileStmtClass:
1747 case Stmt::DoStmtClass:
1748 case Stmt::IfStmtClass:
1749 case Stmt::ChooseExprClass:
1750 case Stmt::ConditionalOperatorClass:
1751 case Stmt::BinaryOperatorClass:
1752 return true;
1753 }
1754
1755 return E ? E->IgnoreParens() : NULL;
1756}
1757
Ted Kremenek1eaa6712008-01-30 23:02:42 +00001758
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001759//===----------------------------------------------------------------------===//
1760// CFG Graphviz Visualization
1761//===----------------------------------------------------------------------===//
1762
Ted Kremenek08176a52007-08-31 21:30:12 +00001763
1764#ifndef NDEBUG
Chris Lattner26002172007-09-17 06:16:32 +00001765static StmtPrinterHelper* GraphHelper;
Ted Kremenek08176a52007-08-31 21:30:12 +00001766#endif
1767
1768void CFG::viewCFG() const {
1769#ifndef NDEBUG
1770 StmtPrinterHelper H(this);
1771 GraphHelper = &H;
1772 llvm::ViewGraph(this,"CFG");
1773 GraphHelper = NULL;
Ted Kremenek08176a52007-08-31 21:30:12 +00001774#endif
1775}
1776
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001777namespace llvm {
1778template<>
1779struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1780 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1781
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001782#ifndef NDEBUG
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001783 std::string OutSStr;
1784 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek08176a52007-08-31 21:30:12 +00001785 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek7b6f67b2008-09-13 05:16:45 +00001786 std::string& OutStr = Out.str();
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001787
1788 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1789
1790 // Process string output to make it nicer...
1791 for (unsigned i = 0; i != OutStr.length(); ++i)
1792 if (OutStr[i] == '\n') { // Left justify
1793 OutStr[i] = '\\';
1794 OutStr.insert(OutStr.begin()+i+1, 'l');
1795 }
1796
1797 return OutStr;
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001798#else
1799 return "";
1800#endif
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001801 }
1802};
1803} // end namespace llvm