blob: 3bfac7fbdcdae4e9f9aad214ae0f2669821a6d84 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenekfddd5182007-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 Kremenekc310e932007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7dba8602007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
Ted Kremenek7e3a89d2007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek6fa9b882008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek274f4332008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremeneka95d3752008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremenekfddd5182007-08-21 21:42:03 +000025#include <iomanip>
26#include <algorithm>
Ted Kremenek7dba8602007-08-29 21:56:09 +000027#include <sstream>
Ted Kremenek83c01da2008-01-11 00:40:29 +000028
Ted Kremenekfddd5182007-08-21 21:42:03 +000029using namespace clang;
30
31namespace {
32
Ted Kremenekbefef2f2007-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 Kremenek6fa9b882008-01-08 18:15:10 +000036struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekbefef2f2007-08-23 21:26:19 +000037 SaveAndRestore(T& x) : X(x), old_value(x) {}
38 ~SaveAndRestore() { X = old_value; }
Ted Kremenekb6f7b722007-08-30 18:13:31 +000039 T get() { return old_value; }
40
Ted Kremenekbefef2f2007-08-23 21:26:19 +000041 T& X;
42 T old_value;
43};
Ted Kremenekfddd5182007-08-21 21:42:03 +000044
Douglas Gregor4afa39d2009-01-20 01:17:11 +000045static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-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 Kremeneka34ea072008-08-04 22:51:42 +000053/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-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 Kremenekc310e932007-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 Kremenek6fa9b882008-01-08 18:15:10 +000068class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenekfddd5182007-08-21 21:42:03 +000069 CFG* cfg;
70 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000071 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000072 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000073 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000074 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000075 CFGBlock* DefaultCaseBlock;
Ted Kremenekfddd5182007-08-21 21:42:03 +000076
Ted Kremenek19bb3562007-08-28 19:26:49 +000077 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000078 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
79 LabelMapTy LabelMap;
80
Ted Kremenek19bb3562007-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 Kremenek4a2b8a12007-08-22 15:40:58 +000083 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000084 BackpatchBlocksTy BackpatchBlocks;
85
Ted Kremenek19bb3562007-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 Kremenekfddd5182007-08-21 21:42:03 +000090public:
Ted Kremenek026473c2007-08-23 16:51:22 +000091 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000092 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000093 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +000094 // Create an empty CFG.
95 cfg = new CFG();
96 }
97
98 ~CFGBuilder() { delete cfg; }
Ted Kremenekfddd5182007-08-21 21:42:03 +000099
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000100 // buildCFG - Used by external clients to construct the CFG.
101 CFG* buildCFG(Stmt* Statement);
Ted Kremenekc310e932007-08-21 22:06:14 +0000102
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000103 // Visitors to walk an AST and construct the CFG. Called by
104 // buildCFG. Do not call directly!
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000105
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000106 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000107 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000108 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
109 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenek295222c2008-02-13 21:46:34 +0000110 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek514de5a2008-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 Kremenek19bb3562007-08-28 19:26:49 +0000115 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek514de5a2008-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 Kremenekfddd5182007-08-21 21:42:03 +0000123
Ted Kremenek4102af92008-03-13 03:04:22 +0000124 // FIXME: Add support for ObjC-specific control-flow structures.
125
Ted Kremenek274f4332008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
131
Ted Kremenek274f4332008-04-28 18:00:46 +0000132 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S) { return NYS(); }
133 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { return NYS(); }
134 CFGBlock* VisitObjCAtFinallyStmt(ObjCAtFinallyStmt* S) { return NYS(); }
Ted Kremenek2fda5042008-12-09 20:20:09 +0000135
136 // FIXME: This is not completely supported. We basically @throw like
137 // a 'return'.
138 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenek274f4332008-04-28 18:00:46 +0000139
140 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
141 return NYS();
Ted Kremenek4102af92008-03-13 03:04:22 +0000142 }
143
Ted Kremenek00c0a302008-09-26 18:17:07 +0000144 // Blocks.
145 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
146 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
147
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000148private:
149 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000150 CFGBlock* addStmt(Stmt* Terminator);
151 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
152 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000153 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000154 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000155 void FinishBlock(CFGBlock* B);
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000156
Ted Kremenek4102af92008-03-13 03:04:22 +0000157 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000158};
Ted Kremenek610a09e2008-09-26 22:58:57 +0000159
Douglas Gregor898574e2008-12-05 23:32:09 +0000160// FIXME: Add support for dependent-sized array types in C++?
161// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000162static VariableArrayType* FindVA(Type* t) {
163 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
164 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
165 if (vat->getSizeExpr())
166 return vat;
167
168 t = vt->getElementType().getTypePtr();
169 }
170
171 return 0;
172}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000173
174/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
175/// represent an arbitrary statement. Examples include a single expression
176/// or a function body (compound statement). The ownership of the returned
177/// CFG is transferred to the caller. If CFG construction fails, this method
178/// returns NULL.
179CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek19bb3562007-08-28 19:26:49 +0000180 assert (cfg);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000181 if (!Statement) return NULL;
182
Ted Kremenek4102af92008-03-13 03:04:22 +0000183 badCFG = false;
184
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000185 // Create an empty block that will serve as the exit block for the CFG.
186 // Since this is the first block added to the CFG, it will be implicitly
187 // registered as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000188 Succ = createBlock();
189 assert (Succ == &cfg->getExit());
190 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000191
192 // Visit the statements and create the CFG.
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000193 CFGBlock* B = Visit(Statement);
194 if (!B) B = Succ;
195
196 if (B) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000197 // Finalize the last constructed block. This usually involves
198 // reversing the order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000199 if (Block) FinishBlock(B);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000200
201 // Backpatch the gotos whose label -> block mappings we didn't know
202 // when we encountered them.
203 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
204 E = BackpatchBlocks.end(); I != E; ++I ) {
205
206 CFGBlock* B = *I;
207 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
208 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
209
210 // If there is no target for the goto, then we are looking at an
211 // incomplete AST. Handle this by not registering a successor.
212 if (LI == LabelMap.end()) continue;
213
214 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000215 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000216
Ted Kremenek19bb3562007-08-28 19:26:49 +0000217 // Add successors to the Indirect Goto Dispatch block (if we have one).
218 if (CFGBlock* B = cfg->getIndirectGotoBlock())
219 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
220 E = AddressTakenLabels.end(); I != E; ++I ) {
221
222 // Lookup the target block.
223 LabelMapTy::iterator LI = LabelMap.find(*I);
224
225 // If there is no target block that contains label, then we are looking
226 // at an incomplete AST. Handle this by not registering a successor.
227 if (LI == LabelMap.end()) continue;
228
229 B->addSuccessor(LI->second);
230 }
Ted Kremenek322f58d2007-09-26 21:23:31 +0000231
Ted Kremenek94b33162007-09-17 16:18:02 +0000232 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000233 }
234
235 // Create an empty entry block that has no predecessors.
236 cfg->setEntry(createBlock());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000237
Ted Kremenek4102af92008-03-13 03:04:22 +0000238 if (badCFG) {
239 delete cfg;
240 cfg = NULL;
241 return NULL;
242 }
243
Ted Kremenek322f58d2007-09-26 21:23:31 +0000244 // NULL out cfg so that repeated calls to the builder will fail and that
245 // the ownership of the constructed CFG is passed to the caller.
246 CFG* t = cfg;
247 cfg = NULL;
248 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000249}
250
251/// createBlock - Used to lazily create blocks that are connected
252/// to the current (global) succcessor.
253CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000254 CFGBlock* B = cfg->createBlock();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000255 if (add_successor && Succ) B->addSuccessor(Succ);
256 return B;
257}
258
259/// FinishBlock - When the last statement has been added to the block,
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000260/// we must reverse the statements because they have been inserted
261/// in reverse order.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262void CFGBuilder::FinishBlock(CFGBlock* B) {
263 assert (B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000264 B->reverseStmts();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000265}
266
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000267/// addStmt - Used to add statements/expressions to the current CFGBlock
268/// "Block". This method calls WalkAST on the passed statement to see if it
269/// contains any short-circuit expressions. If so, it recursively creates
270/// the necessary blocks for such expressions. It returns the "topmost" block
271/// of the created blocks, or the original value of "Block" when this method
272/// was called if no additional blocks are created.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000273CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenekaf603f72007-08-30 18:39:40 +0000274 if (!Block) Block = createBlock();
Ted Kremenek411cdee2008-04-16 21:10:48 +0000275 return WalkAST(Terminator,true);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000276}
277
278/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000279/// add extra blocks for ternary operators, &&, and ||. We also
280/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000281CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
282 switch (Terminator->getStmtClass()) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000283 case Stmt::ConditionalOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000284 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000285
286 // Create the confluence block that will "merge" the results
287 // of the ternary expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000288 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
289 ConfluenceBlock->appendStmt(C);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000290 FinishBlock(ConfluenceBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000291
292 // Create a block for the LHS expression if there is an LHS expression.
293 // A GCC extension allows LHS to be NULL, causing the condition to
294 // be the value that is returned instead.
295 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000296 Succ = ConfluenceBlock;
297 Block = NULL;
Ted Kremenekecc04c92007-11-26 18:20:26 +0000298 CFGBlock* LHSBlock = NULL;
299 if (C->getLHS()) {
300 LHSBlock = Visit(C->getLHS());
301 FinishBlock(LHSBlock);
302 Block = NULL;
303 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000304
Ted Kremenekecc04c92007-11-26 18:20:26 +0000305 // Create the block for the RHS expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000306 Succ = ConfluenceBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000307 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000308 FinishBlock(RHSBlock);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000309
Ted Kremenekecc04c92007-11-26 18:20:26 +0000310 // Create the block that will contain the condition.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000311 Block = createBlock(false);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000312
313 if (LHSBlock)
314 Block->addSuccessor(LHSBlock);
315 else {
316 // If we have no LHS expression, add the ConfluenceBlock as a direct
317 // successor for the block containing the condition. Moreover,
318 // we need to reverse the order of the predecessors in the
319 // ConfluenceBlock because the RHSBlock will have been added to
320 // the succcessors already, and we want the first predecessor to the
321 // the block containing the expression for the case when the ternary
322 // expression evaluates to true.
323 Block->addSuccessor(ConfluenceBlock);
324 assert (ConfluenceBlock->pred_size() == 2);
325 std::reverse(ConfluenceBlock->pred_begin(),
326 ConfluenceBlock->pred_end());
327 }
328
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000329 Block->addSuccessor(RHSBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000330
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000331 Block->setTerminator(C);
332 return addStmt(C->getCond());
333 }
Ted Kremenek49a436d2007-08-31 17:03:41 +0000334
335 case Stmt::ChooseExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000336 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000337
338 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
339 ConfluenceBlock->appendStmt(C);
340 FinishBlock(ConfluenceBlock);
341
342 Succ = ConfluenceBlock;
343 Block = NULL;
344 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000345 FinishBlock(LHSBlock);
346
Ted Kremenek49a436d2007-08-31 17:03:41 +0000347 Succ = ConfluenceBlock;
348 Block = NULL;
349 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000350 FinishBlock(RHSBlock);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000351
352 Block = createBlock(false);
353 Block->addSuccessor(LHSBlock);
354 Block->addSuccessor(RHSBlock);
355 Block->setTerminator(C);
356 return addStmt(C->getCond());
357 }
Ted Kremenek7926f7c2007-08-28 16:18:58 +0000358
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000359 case Stmt::DeclStmtClass: {
Ted Kremenek53061c82008-10-06 20:56:19 +0000360 DeclStmt *DS = cast<DeclStmt>(Terminator);
361 if (DS->hasSolitaryDecl()) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000362 Block->appendStmt(Terminator);
Ted Kremenek53061c82008-10-06 20:56:19 +0000363 return WalkAST_VisitDeclSubExpr(DS->getSolitaryDecl());
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000364 }
365 else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000366 typedef llvm::SmallVector<Decl*,10> BufTy;
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000367 BufTy Buf;
368 CFGBlock* B = 0;
Ted Kremenek53061c82008-10-06 20:56:19 +0000369
370 // FIXME: Add a reverse iterator for DeclStmt to avoid this
371 // extra copy.
372 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
373 DI != DE; ++DI)
374 Buf.push_back(*DI);
375
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000376 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000377 // 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 Kremenekc7eb9032008-08-06 23:20:50 +0000380
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000381 // Allocate the DeclStmt using the BumpPtrAllocator. It will
Douglas Gregor9653db72009-02-13 19:06:18 +0000382 // get automatically freed with the CFG.
383 DeclGroupRef DG(*I);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000384 Decl* D = *I;
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000385 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
386
387 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(),
388 GetEndLoc(D));
389
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000390 // Append the fake DeclStmt to block.
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000391 Block->appendStmt(DS);
392 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000393 }
394 return B;
395 }
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000396 }
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000397
Ted Kremenek19bb3562007-08-28 19:26:49 +0000398 case Stmt::AddrLabelExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000399 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000400 AddressTakenLabels.insert(A->getLabel());
401
Ted Kremenek411cdee2008-04-16 21:10:48 +0000402 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000403 return Block;
404 }
Ted Kremenekf50ec102007-09-11 21:29:43 +0000405
Ted Kremenek15c27a82007-08-28 18:30:10 +0000406 case Stmt::StmtExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000407 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000408
Sebastian Redl05189992008-11-11 17:56:53 +0000409 case Stmt::SizeOfAlignOfExprClass: {
410 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek610a09e2008-09-26 22:58:57 +0000411
412 // VLA types have expressions that must be evaluated.
Sebastian Redl05189992008-11-11 17:56:53 +0000413 if (E->isArgumentType()) {
414 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
415 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
416 addStmt(VA->getSizeExpr());
417 }
418 // Expressions in sizeof/alignof are not evaluated and thus have no
419 // control flow.
420 else
421 Block->appendStmt(Terminator);
Ted Kremenek610a09e2008-09-26 22:58:57 +0000422
423 return Block;
424 }
425
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000426 case Stmt::BinaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000427 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000428
429 if (B->isLogicalOp()) { // && or ||
430 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
431 ConfluenceBlock->appendStmt(B);
432 FinishBlock(ConfluenceBlock);
433
434 // create the block evaluating the LHS
435 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekafe54332007-12-21 19:49:00 +0000436 LHSBlock->setTerminator(B);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000437
438 // create the block evaluating the RHS
439 Succ = ConfluenceBlock;
440 Block = NULL;
441 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu924d9a82008-10-04 05:48:38 +0000442 FinishBlock(RHSBlock);
Ted Kremenekafe54332007-12-21 19:49:00 +0000443
444 // Now link the LHSBlock with RHSBlock.
445 if (B->getOpcode() == BinaryOperator::LOr) {
446 LHSBlock->addSuccessor(ConfluenceBlock);
447 LHSBlock->addSuccessor(RHSBlock);
448 }
449 else {
450 assert (B->getOpcode() == BinaryOperator::LAnd);
451 LHSBlock->addSuccessor(RHSBlock);
452 LHSBlock->addSuccessor(ConfluenceBlock);
453 }
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000454
455 // Generate the blocks for evaluating the LHS.
456 Block = LHSBlock;
457 return addStmt(B->getLHS());
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000458 }
459 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
460 Block->appendStmt(B);
461 addStmt(B->getRHS());
462 return addStmt(B->getLHS());
Ted Kremenek63f58872007-10-01 19:33:33 +0000463 }
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000464
465 break;
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000466 }
Ted Kremenek00c0a302008-09-26 18:17:07 +0000467
468 // Blocks: No support for blocks ... yet
469 case Stmt::BlockExprClass:
470 case Stmt::BlockDeclRefExprClass:
471 return NYS();
Ted Kremenekf4e15fc2008-02-26 02:37:08 +0000472
473 case Stmt::ParenExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000474 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000475
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000476 default:
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000477 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000478 };
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000479
Ted Kremenek411cdee2008-04-16 21:10:48 +0000480 if (AlwaysAddStmt) Block->appendStmt(Terminator);
481 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000482}
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000483
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000484/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
485/// for initializers in Decls.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000486CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000487 VarDecl* VD = dyn_cast<VarDecl>(D);
488
489 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000490 return Block;
491
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000492 Expr* Init = VD->getInit();
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000493
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000494 if (Init) {
Mike Stump54cc43f2009-02-26 08:00:25 +0000495 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000496 switch (Init->getStmtClass()) {
497 case Stmt::IntegerLiteralClass:
498 case Stmt::CharacterLiteralClass:
499 case Stmt::StringLiteralClass:
500 break;
501 default:
502 Block = addStmt(Init);
503 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000504 }
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000505
506 // If the type of VD is a VLA, then we must process its size expressions.
507 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
508 VA = FindVA(VA->getElementType().getTypePtr()))
509 Block = addStmt(VA->getSizeExpr());
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000510
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000511 return Block;
512}
513
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000514/// WalkAST_VisitChildren - Utility method to call WalkAST on the
515/// children of a Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000516CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000517 CFGBlock* B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000518 for (Stmt::child_iterator I = Terminator->child_begin(),
519 E = Terminator->child_end();
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000520 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000521 if (*I) B = WalkAST(*I);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000522
523 return B;
524}
525
Ted Kremenek15c27a82007-08-28 18:30:10 +0000526/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
527/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000528CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
529 Block->appendStmt(Terminator);
530 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000531}
532
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000533/// VisitStmt - Handle statements with no branching control flow.
534CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
535 // We cannot assume that we are in the middle of a basic block, since
536 // the CFG might only be constructed for this single statement. If
537 // we have no current basic block, just create one lazily.
538 if (!Block) Block = createBlock();
539
540 // Simply add the statement to the current block. We actually
541 // insert statements in reverse order; this order is reversed later
542 // when processing the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000543 addStmt(Statement);
544
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000545 return Block;
546}
547
548CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
549 return Block;
550}
551
552CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000553
554 CFGBlock* LastBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000555
Ted Kremenekd34066c2008-02-26 00:22:58 +0000556 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
557 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000558 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000559 }
560
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000561 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000562}
563
564CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
565 // We may see an if statement in the middle of a basic block, or
566 // it may be the first statement we are processing. In either case,
567 // we create a new basic block. First, we create the blocks for
568 // the then...else statements, and then we create the block containing
569 // the if statement. If we were in the middle of a block, we
570 // stop processing that block and reverse its statements. That block
571 // is then the implicit successor for the "then" and "else" clauses.
572
573 // The block we were proccessing is now finished. Make it the
574 // successor block.
575 if (Block) {
576 Succ = Block;
577 FinishBlock(Block);
578 }
579
580 // Process the false branch. NULL out Block so that the recursive
581 // call to Visit will create a new basic block.
582 // Null out Block so that all successor
583 CFGBlock* ElseBlock = Succ;
584
585 if (Stmt* Else = I->getElse()) {
586 SaveAndRestore<CFGBlock*> sv(Succ);
587
588 // NULL out Block so that the recursive call to Visit will
589 // create a new basic block.
590 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000591 ElseBlock = Visit(Else);
592
593 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
594 ElseBlock = sv.get();
595 else if (Block)
596 FinishBlock(ElseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000597 }
598
599 // Process the true branch. NULL out Block so that the recursive
600 // call to Visit will create a new basic block.
601 // Null out Block so that all successor
602 CFGBlock* ThenBlock;
603 {
604 Stmt* Then = I->getThen();
605 assert (Then);
606 SaveAndRestore<CFGBlock*> sv(Succ);
607 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000608 ThenBlock = Visit(Then);
609
610 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
611 ThenBlock = sv.get();
612 else if (Block)
613 FinishBlock(ThenBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000614 }
615
616 // Now create a new block containing the if statement.
617 Block = createBlock(false);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000618
619 // Set the terminator of the new block to the If statement.
620 Block->setTerminator(I);
621
622 // Now add the successors.
623 Block->addSuccessor(ThenBlock);
624 Block->addSuccessor(ElseBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000625
626 // Add the condition as the last statement in the new block. This
627 // may create new blocks as the condition may contain control-flow. Any
628 // newly created blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000629 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000630}
Ted Kremenekf50ec102007-09-11 21:29:43 +0000631
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000632
633CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
634 // If we were in the middle of a block we stop processing that block
635 // and reverse its statements.
636 //
637 // NOTE: If a "return" appears in the middle of a block, this means
638 // that the code afterwards is DEAD (unreachable). We still
639 // keep a basic block for that code; a simple "mark-and-sweep"
640 // from the entry block will be able to report such dead
641 // blocks.
642 if (Block) FinishBlock(Block);
643
644 // Create the new block.
645 Block = createBlock(false);
646
647 // The Exit block is the only successor.
648 Block->addSuccessor(&cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000649
650 // Add the return statement to the block. This may create new blocks
651 // if R contains control-flow (short-circuit operations).
652 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000653}
654
655CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
656 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000657 Visit(L->getSubStmt());
658 CFGBlock* LabelBlock = Block;
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000659
660 if (!LabelBlock) // This can happen when the body is empty, i.e.
661 LabelBlock=createBlock(); // scopes that only contains NullStmts.
662
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000663 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
664 LabelMap[ L ] = LabelBlock;
665
666 // Labels partition blocks, so this is the end of the basic block
Ted Kremenek9cffe732007-08-29 23:20:49 +0000667 // we were processing (L is the block's label). Because this is
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000668 // label (and we have already processed the substatement) there is no
669 // extra control-flow to worry about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000670 LabelBlock->setLabel(L);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000671 FinishBlock(LabelBlock);
672
673 // We set Block to NULL to allow lazy creation of a new block
674 // (if necessary);
675 Block = NULL;
676
677 // This block is now the implicit successor of other blocks.
678 Succ = LabelBlock;
679
680 return LabelBlock;
681}
682
683CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
684 // Goto is a control-flow statement. Thus we stop processing the
685 // current block and create a new one.
686 if (Block) FinishBlock(Block);
687 Block = createBlock(false);
688 Block->setTerminator(G);
689
690 // If we already know the mapping to the label block add the
691 // successor now.
692 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
693
694 if (I == LabelMap.end())
695 // We will need to backpatch this block later.
696 BackpatchBlocks.push_back(Block);
697 else
698 Block->addSuccessor(I->second);
699
700 return Block;
701}
702
703CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
704 // "for" is a control-flow statement. Thus we stop processing the
705 // current block.
706
707 CFGBlock* LoopSuccessor = NULL;
708
709 if (Block) {
710 FinishBlock(Block);
711 LoopSuccessor = Block;
712 }
713 else LoopSuccessor = Succ;
714
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000715 // Because of short-circuit evaluation, the condition of the loop
716 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
717 // blocks that evaluate the condition.
718 CFGBlock* ExitConditionBlock = createBlock(false);
719 CFGBlock* EntryConditionBlock = ExitConditionBlock;
720
721 // Set the terminator for the "exit" condition block.
722 ExitConditionBlock->setTerminator(F);
723
724 // Now add the actual condition to the condition block. Because the
725 // condition itself may contain control-flow, new blocks may be created.
726 if (Stmt* C = F->getCond()) {
727 Block = ExitConditionBlock;
728 EntryConditionBlock = addStmt(C);
729 if (Block) FinishBlock(EntryConditionBlock);
730 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000731
732 // The condition block is the implicit successor for the loop body as
733 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000734 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000735
736 // Now create the loop body.
737 {
738 assert (F->getBody());
739
740 // Save the current values for Block, Succ, and continue and break targets
741 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
742 save_continue(ContinueTargetBlock),
743 save_break(BreakTargetBlock);
Ted Kremeneke9334502008-09-04 21:48:47 +0000744
Ted Kremenekaf603f72007-08-30 18:39:40 +0000745 // Create a new block to contain the (bottom) of the loop body.
746 Block = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000747
Ted Kremeneke9334502008-09-04 21:48:47 +0000748 if (Stmt* I = F->getInc()) {
749 // Generate increment code in its own basic block. This is the target
750 // of continue statements.
Ted Kremenekd0172432008-11-24 20:50:24 +0000751 Succ = Visit(I);
752
753 // Finish up the increment block if it hasn't been already.
754 if (Block) {
755 assert (Block == Succ);
756 FinishBlock(Block);
757 Block = 0;
758 }
759
Ted Kremeneke9334502008-09-04 21:48:47 +0000760 ContinueTargetBlock = Succ;
761 }
762 else {
763 // No increment code. Continues should go the the entry condition block.
764 ContinueTargetBlock = EntryConditionBlock;
765 }
766
767 // All breaks should go to the code following the loop.
768 BreakTargetBlock = LoopSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000769
770 // Now populate the body block, and in the process create new blocks
771 // as we walk the body of the loop.
772 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000773
774 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000775 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000776 else if (Block)
777 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000778
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000779 // This new body block is a successor to our "exit" condition block.
780 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000781 }
782
783 // Link up the condition block with the code that follows the loop.
784 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000785 ExitConditionBlock->addSuccessor(LoopSuccessor);
786
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000787 // If the loop contains initialization, create a new block for those
788 // statements. This block can also contain statements that precede
789 // the loop.
790 if (Stmt* I = F->getInit()) {
791 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000792 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000793 }
794 else {
795 // There is no loop initialization. We are thus basically a while
796 // loop. NULL out Block to force lazy block construction.
797 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000798 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000799 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000800 }
801}
802
Ted Kremenek514de5a2008-11-11 17:10:00 +0000803CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
804 // Objective-C fast enumeration 'for' statements:
805 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
806 //
807 // for ( Type newVariable in collection_expression ) { statements }
808 //
809 // becomes:
810 //
811 // prologue:
812 // 1. collection_expression
813 // T. jump to loop_entry
814 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000815 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000816 // 1. ObjCForCollectionStmt [performs binding to newVariable]
817 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
818 // TB:
819 // statements
820 // T. jump to loop_entry
821 // FB:
822 // what comes after
823 //
824 // and
825 //
826 // Type existingItem;
827 // for ( existingItem in expression ) { statements }
828 //
829 // becomes:
830 //
831 // the same with newVariable replaced with existingItem; the binding
832 // works the same except that for one ObjCForCollectionStmt::getElement()
833 // returns a DeclStmt and the other returns a DeclRefExpr.
834 //
835
836 CFGBlock* LoopSuccessor = 0;
837
838 if (Block) {
839 FinishBlock(Block);
840 LoopSuccessor = Block;
841 Block = 0;
842 }
843 else LoopSuccessor = Succ;
844
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000845 // Build the condition blocks.
846 CFGBlock* ExitConditionBlock = createBlock(false);
847 CFGBlock* EntryConditionBlock = ExitConditionBlock;
848
849 // Set the terminator for the "exit" condition block.
850 ExitConditionBlock->setTerminator(S);
851
852 // The last statement in the block should be the ObjCForCollectionStmt,
853 // which performs the actual binding to 'element' and determines if there
854 // are any more items in the collection.
855 ExitConditionBlock->appendStmt(S);
856 Block = ExitConditionBlock;
857
858 // Walk the 'element' expression to see if there are any side-effects. We
859 // generate new blocks as necesary. We DON'T add the statement by default
860 // to the CFG unless it contains control-flow.
861 EntryConditionBlock = WalkAST(S->getElement(), false);
862 if (Block) { FinishBlock(EntryConditionBlock); Block = 0; }
863
864 // The condition block is the implicit successor for the loop body as
865 // well as any code above the loop.
866 Succ = EntryConditionBlock;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000867
868 // Now create the true branch.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000869 {
870 // Save the current values for Succ, continue and break targets.
871 SaveAndRestore<CFGBlock*> save_Succ(Succ),
872 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
873
874 BreakTargetBlock = LoopSuccessor;
875 ContinueTargetBlock = EntryConditionBlock;
876
877 CFGBlock* BodyBlock = Visit(S->getBody());
878
879 if (!BodyBlock)
880 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
881 else if (Block)
882 FinishBlock(BodyBlock);
883
884 // This new body block is a successor to our "exit" condition block.
885 ExitConditionBlock->addSuccessor(BodyBlock);
886 }
Ted Kremenekfc335522008-11-13 06:36:45 +0000887
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000888 // Link up the condition block with the code that follows the loop.
889 // (the false branch).
890 ExitConditionBlock->addSuccessor(LoopSuccessor);
891
Ted Kremenek514de5a2008-11-11 17:10:00 +0000892 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000893 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +0000894 return addStmt(S->getCollection());
895}
896
897
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000898CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
899 // "while" is a control-flow statement. Thus we stop processing the
900 // current block.
901
902 CFGBlock* LoopSuccessor = NULL;
903
904 if (Block) {
905 FinishBlock(Block);
906 LoopSuccessor = Block;
907 }
908 else LoopSuccessor = Succ;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000909
910 // Because of short-circuit evaluation, the condition of the loop
911 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
912 // blocks that evaluate the condition.
913 CFGBlock* ExitConditionBlock = createBlock(false);
914 CFGBlock* EntryConditionBlock = ExitConditionBlock;
915
916 // Set the terminator for the "exit" condition block.
917 ExitConditionBlock->setTerminator(W);
918
919 // Now add the actual condition to the condition block. Because the
920 // condition itself may contain control-flow, new blocks may be created.
921 // Thus we update "Succ" after adding the condition.
922 if (Stmt* C = W->getCond()) {
923 Block = ExitConditionBlock;
924 EntryConditionBlock = addStmt(C);
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000925 assert (Block == EntryConditionBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000926 if (Block) FinishBlock(EntryConditionBlock);
927 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000928
929 // The condition block is the implicit successor for the loop body as
930 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000931 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000932
933 // Process the loop body.
934 {
935 assert (W->getBody());
936
937 // Save the current values for Block, Succ, and continue and break targets
938 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
939 save_continue(ContinueTargetBlock),
940 save_break(BreakTargetBlock);
941
942 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000943 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000944
945 // All breaks should go to the code following the loop.
946 BreakTargetBlock = LoopSuccessor;
947
948 // NULL out Block to force lazy instantiation of blocks for the body.
949 Block = NULL;
950
951 // Create the body. The returned block is the entry to the loop body.
952 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000953
954 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000955 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000956 else if (Block)
957 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000958
959 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000960 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000961 }
962
963 // Link up the condition block with the code that follows the loop.
964 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000965 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000966
967 // There can be no more statements in the condition block
968 // since we loop back to this block. NULL out Block to force
969 // lazy creation of another block.
970 Block = NULL;
971
972 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +0000973 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000974 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000975}
Ted Kremenek2fda5042008-12-09 20:20:09 +0000976
977CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
978 // FIXME: This isn't complete. We basically treat @throw like a return
979 // statement.
980
981 // If we were in the middle of a block we stop processing that block
982 // and reverse its statements.
983 if (Block) FinishBlock(Block);
984
985 // Create the new block.
986 Block = createBlock(false);
987
988 // The Exit block is the only successor.
989 Block->addSuccessor(&cfg->getExit());
990
991 // Add the statement to the block. This may create new blocks
992 // if S contains control-flow (short-circuit operations).
993 return addStmt(S);
994}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000995
996CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
997 // "do...while" is a control-flow statement. Thus we stop processing the
998 // current block.
999
1000 CFGBlock* LoopSuccessor = NULL;
1001
1002 if (Block) {
1003 FinishBlock(Block);
1004 LoopSuccessor = Block;
1005 }
1006 else LoopSuccessor = Succ;
1007
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001008 // Because of short-circuit evaluation, the condition of the loop
1009 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
1010 // blocks that evaluate the condition.
1011 CFGBlock* ExitConditionBlock = createBlock(false);
1012 CFGBlock* EntryConditionBlock = ExitConditionBlock;
1013
1014 // Set the terminator for the "exit" condition block.
1015 ExitConditionBlock->setTerminator(D);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001016
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001017 // Now add the actual condition to the condition block. Because the
1018 // condition itself may contain control-flow, new blocks may be created.
1019 if (Stmt* C = D->getCond()) {
1020 Block = ExitConditionBlock;
1021 EntryConditionBlock = addStmt(C);
1022 if (Block) FinishBlock(EntryConditionBlock);
1023 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001024
Ted Kremenek54827132008-02-27 07:20:00 +00001025 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001026 Succ = EntryConditionBlock;
1027
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001028 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001029 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001030 {
1031 assert (D->getBody());
1032
1033 // Save the current values for Block, Succ, and continue and break targets
1034 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1035 save_continue(ContinueTargetBlock),
1036 save_break(BreakTargetBlock);
1037
1038 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001039 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001040
1041 // All breaks should go to the code following the loop.
1042 BreakTargetBlock = LoopSuccessor;
1043
1044 // NULL out Block to force lazy instantiation of blocks for the body.
1045 Block = NULL;
1046
1047 // Create the body. The returned block is the entry to the loop body.
1048 BodyBlock = Visit(D->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001049
Ted Kremenekaf603f72007-08-30 18:39:40 +00001050 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001051 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenekaf603f72007-08-30 18:39:40 +00001052 else if (Block)
1053 FinishBlock(BodyBlock);
1054
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001055 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001056 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001057 }
1058
1059 // Link up the condition block with the code that follows the loop.
1060 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001061 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001062
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001063 // There can be no more statements in the body block(s)
1064 // since we loop back to the body. NULL out Block to force
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001065 // lazy creation of another block.
1066 Block = NULL;
1067
1068 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001069 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001070 return BodyBlock;
1071}
1072
1073CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1074 // "continue" is a control-flow statement. Thus we stop processing the
1075 // current block.
1076 if (Block) FinishBlock(Block);
1077
1078 // Now create a new block that ends with the continue statement.
1079 Block = createBlock(false);
1080 Block->setTerminator(C);
1081
1082 // If there is no target for the continue, then we are looking at an
1083 // incomplete AST. Handle this by not registering a successor.
1084 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1085
1086 return Block;
1087}
1088
1089CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1090 // "break" is a control-flow statement. Thus we stop processing the
1091 // current block.
1092 if (Block) FinishBlock(Block);
1093
1094 // Now create a new block that ends with the continue statement.
1095 Block = createBlock(false);
1096 Block->setTerminator(B);
1097
1098 // If there is no target for the break, then we are looking at an
1099 // incomplete AST. Handle this by not registering a successor.
1100 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1101
1102 return Block;
1103}
1104
Ted Kremenek411cdee2008-04-16 21:10:48 +00001105CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001106 // "switch" is a control-flow statement. Thus we stop processing the
1107 // current block.
1108 CFGBlock* SwitchSuccessor = NULL;
1109
1110 if (Block) {
1111 FinishBlock(Block);
1112 SwitchSuccessor = Block;
1113 }
1114 else SwitchSuccessor = Succ;
1115
1116 // Save the current "switch" context.
1117 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001118 save_break(BreakTargetBlock),
1119 save_default(DefaultCaseBlock);
1120
1121 // Set the "default" case to be the block after the switch statement.
1122 // If the switch statement contains a "default:", this value will
1123 // be overwritten with the block for that code.
1124 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenek295222c2008-02-13 21:46:34 +00001125
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001126 // Create a new block that will contain the switch statement.
1127 SwitchTerminatedBlock = createBlock(false);
1128
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001129 // Now process the switch body. The code after the switch is the implicit
1130 // successor.
1131 Succ = SwitchSuccessor;
1132 BreakTargetBlock = SwitchSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001133
1134 // When visiting the body, the case statements should automatically get
1135 // linked up to the switch. We also don't keep a pointer to the body,
1136 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001137 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001138 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001139 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001140 if (Block) FinishBlock(BodyBlock);
1141
Ted Kremenek295222c2008-02-13 21:46:34 +00001142 // If we have no "default:" case, the default transition is to the
1143 // code following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001144 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenek295222c2008-02-13 21:46:34 +00001145
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001146 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001147 SwitchTerminatedBlock->setTerminator(Terminator);
1148 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001149 Block = SwitchTerminatedBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001150
Ted Kremenek411cdee2008-04-16 21:10:48 +00001151 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001152}
1153
Ted Kremenek411cdee2008-04-16 21:10:48 +00001154CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001155 // CaseStmts are essentially labels, so they are the
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001156 // first statement in a block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001157
Ted Kremenek411cdee2008-04-16 21:10:48 +00001158 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001159 CFGBlock* CaseBlock = Block;
1160 if (!CaseBlock) CaseBlock = createBlock();
1161
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001162 // Cases statements partition blocks, so this is the top of
1163 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001164 CaseBlock->setLabel(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001165 FinishBlock(CaseBlock);
1166
1167 // Add this block to the list of successors for the block with the
1168 // switch statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001169 assert (SwitchTerminatedBlock);
1170 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001171
1172 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1173 Block = NULL;
1174
1175 // This block is now the implicit successor of other blocks.
1176 Succ = CaseBlock;
1177
Ted Kremenek2677ea82008-03-15 07:45:02 +00001178 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001179}
Ted Kremenek295222c2008-02-13 21:46:34 +00001180
Ted Kremenek411cdee2008-04-16 21:10:48 +00001181CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1182 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001183 DefaultCaseBlock = Block;
1184 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1185
1186 // Default statements partition blocks, so this is the top of
1187 // the basic block we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001188 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001189 FinishBlock(DefaultCaseBlock);
1190
1191 // Unlike case statements, we don't add the default block to the
1192 // successors for the switch statement immediately. This is done
1193 // when we finish processing the switch statement. This allows for
1194 // the default case (including a fall-through to the code after the
1195 // switch statement) to always be the last successor of a switch-terminated
1196 // block.
1197
1198 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1199 Block = NULL;
1200
1201 // This block is now the implicit successor of other blocks.
1202 Succ = DefaultCaseBlock;
1203
1204 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001205}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001206
Ted Kremenek19bb3562007-08-28 19:26:49 +00001207CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1208 // Lazily create the indirect-goto dispatch block if there isn't one
1209 // already.
1210 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1211
1212 if (!IBlock) {
1213 IBlock = createBlock(false);
1214 cfg->setIndirectGotoBlock(IBlock);
1215 }
1216
1217 // IndirectGoto is a control-flow statement. Thus we stop processing the
1218 // current block and create a new one.
1219 if (Block) FinishBlock(Block);
1220 Block = createBlock(false);
1221 Block->setTerminator(I);
1222 Block->addSuccessor(IBlock);
1223 return addStmt(I->getTarget());
1224}
1225
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001226
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001227} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001228
1229/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1230/// block has no successors or predecessors. If this is the first block
1231/// created in the CFG, it is automatically set to be the Entry and Exit
1232/// of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001233CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001234 bool first_block = begin() == end();
1235
1236 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001237 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001238
1239 // If this is the first block, set it as the Entry and Exit.
1240 if (first_block) Entry = Exit = &front();
1241
1242 // Return the block.
1243 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001244}
1245
Ted Kremenek026473c2007-08-23 16:51:22 +00001246/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1247/// CFG is returned to the caller.
1248CFG* CFG::buildCFG(Stmt* Statement) {
1249 CFGBuilder Builder;
1250 return Builder.buildCFG(Statement);
1251}
1252
1253/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001254void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1255
Ted Kremenek63f58872007-10-01 19:33:33 +00001256//===----------------------------------------------------------------------===//
1257// CFG: Queries for BlkExprs.
1258//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001259
Ted Kremenek63f58872007-10-01 19:33:33 +00001260namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001261 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001262}
1263
Ted Kremenek411cdee2008-04-16 21:10:48 +00001264static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1265 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001266 return;
1267
Ted Kremenek411cdee2008-04-16 21:10:48 +00001268 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001269 if (!*I) continue;
1270
1271 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1272 if (B->isAssignmentOp()) Set.insert(B);
1273
1274 FindSubExprAssignments(*I, Set);
1275 }
1276}
1277
Ted Kremenek63f58872007-10-01 19:33:33 +00001278static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1279 BlkExprMapTy* M = new BlkExprMapTy();
1280
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001281 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek411cdee2008-04-16 21:10:48 +00001282 // only assignments that we want to *possibly* register as a block-level
1283 // expression. Basically, if an assignment occurs both in a subexpression
1284 // and at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001285 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1286
Ted Kremenek63f58872007-10-01 19:33:33 +00001287 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1288 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001289 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001290
Ted Kremenek411cdee2008-04-16 21:10:48 +00001291 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1292
1293 // Iterate over the statements again on identify the Expr* and Stmt* at
1294 // the block-level that are block-level expressions.
1295
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001296 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001297 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001298
Ted Kremenek411cdee2008-04-16 21:10:48 +00001299 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001300 // Assignment expressions that are not nested within another
1301 // expression are really "statements" whose value is never
1302 // used by another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001303 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001304 continue;
1305 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001306 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001307 // Special handling for statement expressions. The last statement
1308 // in the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001309 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001310 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001311 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001312 (*M)[C->body_back()] = x;
1313 }
1314 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001315
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001316 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001317 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001318 }
1319
Ted Kremenek411cdee2008-04-16 21:10:48 +00001320 // Look at terminators. The condition is a block-level expression.
1321
Ted Kremenek390e48b2008-11-12 21:11:49 +00001322 Stmt* S = I->getTerminatorCondition();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001323
Ted Kremenek390e48b2008-11-12 21:11:49 +00001324 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001325 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001326 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001327 }
1328 }
1329
Ted Kremenek63f58872007-10-01 19:33:33 +00001330 return M;
1331}
1332
Ted Kremenek86946742008-01-17 20:48:37 +00001333CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1334 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001335 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1336
1337 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001338 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek63f58872007-10-01 19:33:33 +00001339
1340 if (I == M->end()) return CFG::BlkExprNumTy();
1341 else return CFG::BlkExprNumTy(I->second);
1342}
1343
1344unsigned CFG::getNumBlkExprs() {
1345 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1346 return M->size();
1347 else {
1348 // We assume callers interested in the number of BlkExprs will want
1349 // the map constructed if it doesn't already exist.
1350 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1351 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1352 }
1353}
1354
Ted Kremenek274f4332008-04-28 18:00:46 +00001355//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001356// Cleanup: CFG dstor.
1357//===----------------------------------------------------------------------===//
1358
Ted Kremenek63f58872007-10-01 19:33:33 +00001359CFG::~CFG() {
1360 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1361}
1362
Ted Kremenek7dba8602007-08-29 21:56:09 +00001363//===----------------------------------------------------------------------===//
1364// CFG pretty printing
1365//===----------------------------------------------------------------------===//
1366
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001367namespace {
1368
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001369class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001370
Ted Kremenek42a509f2007-08-31 21:30:12 +00001371 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1372 StmtMapTy StmtMap;
1373 signed CurrentBlock;
1374 unsigned CurrentStmt;
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001375
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001376public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001377
Ted Kremenek42a509f2007-08-31 21:30:12 +00001378 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1379 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1380 unsigned j = 1;
1381 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1382 BI != BEnd; ++BI, ++j )
1383 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1384 }
1385 }
1386
1387 virtual ~StmtPrinterHelper() {}
1388
1389 void setBlockID(signed i) { CurrentBlock = i; }
1390 void setStmtID(unsigned i) { CurrentStmt = i; }
1391
Ted Kremeneka95d3752008-09-13 05:16:45 +00001392 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001393
Ted Kremenek411cdee2008-04-16 21:10:48 +00001394 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001395
1396 if (I == StmtMap.end())
1397 return false;
1398
1399 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1400 && I->second.second == CurrentStmt)
1401 return false;
1402
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001403 OS << "[B" << I->second.first << "." << I->second.second << "]";
1404 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001405 }
1406};
1407
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001408class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1409 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1410
Ted Kremeneka95d3752008-09-13 05:16:45 +00001411 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001412 StmtPrinterHelper* Helper;
1413public:
Ted Kremeneka95d3752008-09-13 05:16:45 +00001414 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek42a509f2007-08-31 21:30:12 +00001415 : OS(os), Helper(helper) {}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001416
1417 void VisitIfStmt(IfStmt* I) {
1418 OS << "if ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001419 I->getCond()->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001420 }
1421
1422 // Default case.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001423 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001424
1425 void VisitForStmt(ForStmt* F) {
1426 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001427 if (F->getInit()) OS << "...";
1428 OS << "; ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001429 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek535bb202007-08-30 21:28:02 +00001430 OS << "; ";
1431 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001432 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 }
1434
1435 void VisitWhileStmt(WhileStmt* W) {
1436 OS << "while " ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001437 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001438 }
1439
1440 void VisitDoStmt(DoStmt* D) {
1441 OS << "do ... while ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001442 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001443 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001444
Ted Kremenek411cdee2008-04-16 21:10:48 +00001445 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001446 OS << "switch ";
Ted Kremenek411cdee2008-04-16 21:10:48 +00001447 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001448 }
1449
Ted Kremenek805e9a82007-08-31 21:49:40 +00001450 void VisitConditionalOperator(ConditionalOperator* C) {
1451 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001452 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001453 }
1454
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001455 void VisitChooseExpr(ChooseExpr* C) {
1456 OS << "__builtin_choose_expr( ";
1457 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001458 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001459 }
1460
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001461 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1462 OS << "goto *";
1463 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001464 }
1465
Ted Kremenek805e9a82007-08-31 21:49:40 +00001466 void VisitBinaryOperator(BinaryOperator* B) {
1467 if (!B->isLogicalOp()) {
1468 VisitExpr(B);
1469 return;
1470 }
1471
1472 B->getLHS()->printPretty(OS,Helper);
1473
1474 switch (B->getOpcode()) {
1475 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001476 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001477 return;
1478 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001479 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001480 return;
1481 default:
1482 assert(false && "Invalid logical operator.");
1483 }
1484 }
1485
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001486 void VisitExpr(Expr* E) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001487 E->printPretty(OS,Helper);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001488 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489};
Ted Kremenek42a509f2007-08-31 21:30:12 +00001490
1491
Ted Kremeneka95d3752008-09-13 05:16:45 +00001492void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001493 if (Helper) {
1494 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001495 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001496 CompoundStmt* Sub = SE->getSubStmt();
1497
1498 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001499 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001500 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001501 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001502 return;
1503 }
1504 }
1505
1506 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001507 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001508 if (B->getOpcode() == BinaryOperator::Comma) {
1509 OS << "... , ";
1510 Helper->handledStmt(B->getRHS(),OS);
1511 OS << '\n';
1512 return;
1513 }
1514 }
1515 }
1516
Ted Kremenek411cdee2008-04-16 21:10:48 +00001517 Terminator->printPretty(OS, Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001518
1519 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001520 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001521}
1522
Ted Kremeneka95d3752008-09-13 05:16:45 +00001523void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek42a509f2007-08-31 21:30:12 +00001524 StmtPrinterHelper* Helper, bool print_edges) {
1525
1526 if (Helper) Helper->setBlockID(B.getBlockID());
1527
Ted Kremenek7dba8602007-08-29 21:56:09 +00001528 // Print the header.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001529 OS << "\n [ B" << B.getBlockID();
1530
1531 if (&B == &cfg->getEntry())
1532 OS << " (ENTRY) ]\n";
1533 else if (&B == &cfg->getExit())
1534 OS << " (EXIT) ]\n";
1535 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001536 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001537 else
1538 OS << " ]\n";
1539
Ted Kremenek9cffe732007-08-29 23:20:49 +00001540 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001541 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001542
1543 if (print_edges)
1544 OS << " ";
1545
Ted Kremenek411cdee2008-04-16 21:10:48 +00001546 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001547 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001548 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001549 OS << "case ";
1550 C->getLHS()->printPretty(OS);
1551 if (C->getRHS()) {
1552 OS << " ... ";
1553 C->getRHS()->printPretty(OS);
1554 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001555 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001556 else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001557 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001558 else
1559 assert(false && "Invalid label statement in CFGBlock.");
1560
Ted Kremenek9cffe732007-08-29 23:20:49 +00001561 OS << ":\n";
1562 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001563
Ted Kremenekfddd5182007-08-21 21:42:03 +00001564 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001565 unsigned j = 1;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001566
1567 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1568 I != E ; ++I, ++j ) {
1569
Ted Kremenek9cffe732007-08-29 23:20:49 +00001570 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001571 if (print_edges)
1572 OS << " ";
1573
Ted Kremeneka95d3752008-09-13 05:16:45 +00001574 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001575
1576 if (Helper)
1577 Helper->setStmtID(j);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001578
1579 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001580 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001581
Ted Kremenek9cffe732007-08-29 23:20:49 +00001582 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001583 if (B.getTerminator()) {
1584 if (print_edges)
1585 OS << " ";
1586
Ted Kremenek9cffe732007-08-29 23:20:49 +00001587 OS << " T: ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001588
1589 if (Helper) Helper->setBlockID(-1);
1590
1591 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1592 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001593 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001594 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001595
Ted Kremenek9cffe732007-08-29 23:20:49 +00001596 if (print_edges) {
1597 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001598 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001599 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001600
Ted Kremenek42a509f2007-08-31 21:30:12 +00001601 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1602 I != E; ++I, ++i) {
1603
1604 if (i == 8 || (i-8) == 0)
1605 OS << "\n ";
1606
Ted Kremenek9cffe732007-08-29 23:20:49 +00001607 OS << " B" << (*I)->getBlockID();
1608 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001609
1610 OS << '\n';
1611
1612 // Print the successors of this block.
1613 OS << " Successors (" << B.succ_size() << "):";
1614 i = 0;
1615
1616 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1617 I != E; ++I, ++i) {
1618
1619 if (i == 8 || (i-8) % 10 == 0)
1620 OS << "\n ";
1621
1622 OS << " B" << (*I)->getBlockID();
1623 }
1624
Ted Kremenek9cffe732007-08-29 23:20:49 +00001625 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001626 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001627}
1628
1629} // end anonymous namespace
1630
1631/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001632void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001633
1634/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001635void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001636
1637 StmtPrinterHelper Helper(this);
1638
1639 // Print the entry block.
1640 print_block(OS, this, getEntry(), &Helper, true);
1641
1642 // Iterate through the CFGBlocks and print them one by one.
1643 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1644 // Skip the entry block, because we already printed it.
1645 if (&(*I) == &getEntry() || &(*I) == &getExit())
1646 continue;
1647
1648 print_block(OS, this, *I, &Helper, true);
1649 }
1650
1651 // Print the exit block.
1652 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001653 OS.flush();
Ted Kremenek42a509f2007-08-31 21:30:12 +00001654}
1655
1656/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001657void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001658
1659/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1660/// Generally this will only be called from CFG::print.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001661void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001662 StmtPrinterHelper Helper(cfg);
1663 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001664}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001665
Ted Kremeneka2925852008-01-30 23:02:42 +00001666/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001667void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremeneka2925852008-01-30 23:02:42 +00001668 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1669 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1670}
1671
Ted Kremenek390e48b2008-11-12 21:11:49 +00001672Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001673
1674 if (!Terminator)
1675 return NULL;
1676
1677 Expr* E = NULL;
1678
1679 switch (Terminator->getStmtClass()) {
1680 default:
1681 break;
1682
1683 case Stmt::ForStmtClass:
1684 E = cast<ForStmt>(Terminator)->getCond();
1685 break;
1686
1687 case Stmt::WhileStmtClass:
1688 E = cast<WhileStmt>(Terminator)->getCond();
1689 break;
1690
1691 case Stmt::DoStmtClass:
1692 E = cast<DoStmt>(Terminator)->getCond();
1693 break;
1694
1695 case Stmt::IfStmtClass:
1696 E = cast<IfStmt>(Terminator)->getCond();
1697 break;
1698
1699 case Stmt::ChooseExprClass:
1700 E = cast<ChooseExpr>(Terminator)->getCond();
1701 break;
1702
1703 case Stmt::IndirectGotoStmtClass:
1704 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1705 break;
1706
1707 case Stmt::SwitchStmtClass:
1708 E = cast<SwitchStmt>(Terminator)->getCond();
1709 break;
1710
1711 case Stmt::ConditionalOperatorClass:
1712 E = cast<ConditionalOperator>(Terminator)->getCond();
1713 break;
1714
1715 case Stmt::BinaryOperatorClass: // '&&' and '||'
1716 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001717 break;
1718
1719 case Stmt::ObjCForCollectionStmtClass:
1720 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001721 }
1722
1723 return E ? E->IgnoreParens() : NULL;
1724}
1725
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001726bool CFGBlock::hasBinaryBranchTerminator() const {
1727
1728 if (!Terminator)
1729 return false;
1730
1731 Expr* E = NULL;
1732
1733 switch (Terminator->getStmtClass()) {
1734 default:
1735 return false;
1736
1737 case Stmt::ForStmtClass:
1738 case Stmt::WhileStmtClass:
1739 case Stmt::DoStmtClass:
1740 case Stmt::IfStmtClass:
1741 case Stmt::ChooseExprClass:
1742 case Stmt::ConditionalOperatorClass:
1743 case Stmt::BinaryOperatorClass:
1744 return true;
1745 }
1746
1747 return E ? E->IgnoreParens() : NULL;
1748}
1749
Ted Kremeneka2925852008-01-30 23:02:42 +00001750
Ted Kremenek7dba8602007-08-29 21:56:09 +00001751//===----------------------------------------------------------------------===//
1752// CFG Graphviz Visualization
1753//===----------------------------------------------------------------------===//
1754
Ted Kremenek42a509f2007-08-31 21:30:12 +00001755
1756#ifndef NDEBUG
Chris Lattner00123512007-09-17 06:16:32 +00001757static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001758#endif
1759
1760void CFG::viewCFG() const {
1761#ifndef NDEBUG
1762 StmtPrinterHelper H(this);
1763 GraphHelper = &H;
1764 llvm::ViewGraph(this,"CFG");
1765 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001766#endif
1767}
1768
Ted Kremenek7dba8602007-08-29 21:56:09 +00001769namespace llvm {
1770template<>
1771struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1772 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1773
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001774#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001775 std::string OutSStr;
1776 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001777 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001778 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001779
1780 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1781
1782 // Process string output to make it nicer...
1783 for (unsigned i = 0; i != OutStr.length(); ++i)
1784 if (OutStr[i] == '\n') { // Left justify
1785 OutStr[i] = '\\';
1786 OutStr.insert(OutStr.begin()+i+1, 'l');
1787 }
1788
1789 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001790#else
1791 return "";
1792#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001793 }
1794};
1795} // end namespace llvm