blob: b84ead763f77682a8bfc9d1e645c6f803f265ea3 [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
Ted Kremenekc7eb9032008-08-06 23:20:50 +000045static SourceLocation GetEndLoc(ScopedDecl* D) {
46 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);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000153 CFGBlock* WalkAST_VisitDeclSubExpr(ScopedDecl* 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 {
366 typedef llvm::SmallVector<ScopedDecl*,10> BufTy;
367 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
382 // get automatically freed with the CFG. Note that even though
383 // we are using a DeclGroupOwningRef that wraps a singe Decl*,
384 // that Decl* will not get deallocated because the destroy method
385 // of DG is never called.
386 DeclGroupOwningRef DG(*I);
387 ScopedDecl* D = *I;
388 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
389
390 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(),
391 GetEndLoc(D));
392
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000393 // Append the fake DeclStmt to block.
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000394 Block->appendStmt(DS);
395 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000396 }
397 return B;
398 }
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000399 }
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000400
Ted Kremenek19bb3562007-08-28 19:26:49 +0000401 case Stmt::AddrLabelExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000402 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000403 AddressTakenLabels.insert(A->getLabel());
404
Ted Kremenek411cdee2008-04-16 21:10:48 +0000405 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000406 return Block;
407 }
Ted Kremenekf50ec102007-09-11 21:29:43 +0000408
Ted Kremenek15c27a82007-08-28 18:30:10 +0000409 case Stmt::StmtExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000410 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000411
Sebastian Redl05189992008-11-11 17:56:53 +0000412 case Stmt::SizeOfAlignOfExprClass: {
413 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek610a09e2008-09-26 22:58:57 +0000414
415 // VLA types have expressions that must be evaluated.
Sebastian Redl05189992008-11-11 17:56:53 +0000416 if (E->isArgumentType()) {
417 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
418 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
419 addStmt(VA->getSizeExpr());
420 }
421 // Expressions in sizeof/alignof are not evaluated and thus have no
422 // control flow.
423 else
424 Block->appendStmt(Terminator);
Ted Kremenek610a09e2008-09-26 22:58:57 +0000425
426 return Block;
427 }
428
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000429 case Stmt::BinaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000430 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000431
432 if (B->isLogicalOp()) { // && or ||
433 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
434 ConfluenceBlock->appendStmt(B);
435 FinishBlock(ConfluenceBlock);
436
437 // create the block evaluating the LHS
438 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekafe54332007-12-21 19:49:00 +0000439 LHSBlock->setTerminator(B);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000440
441 // create the block evaluating the RHS
442 Succ = ConfluenceBlock;
443 Block = NULL;
444 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu924d9a82008-10-04 05:48:38 +0000445 FinishBlock(RHSBlock);
Ted Kremenekafe54332007-12-21 19:49:00 +0000446
447 // Now link the LHSBlock with RHSBlock.
448 if (B->getOpcode() == BinaryOperator::LOr) {
449 LHSBlock->addSuccessor(ConfluenceBlock);
450 LHSBlock->addSuccessor(RHSBlock);
451 }
452 else {
453 assert (B->getOpcode() == BinaryOperator::LAnd);
454 LHSBlock->addSuccessor(RHSBlock);
455 LHSBlock->addSuccessor(ConfluenceBlock);
456 }
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000457
458 // Generate the blocks for evaluating the LHS.
459 Block = LHSBlock;
460 return addStmt(B->getLHS());
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000461 }
462 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
463 Block->appendStmt(B);
464 addStmt(B->getRHS());
465 return addStmt(B->getLHS());
Ted Kremenek63f58872007-10-01 19:33:33 +0000466 }
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000467
468 break;
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000469 }
Ted Kremenek00c0a302008-09-26 18:17:07 +0000470
471 // Blocks: No support for blocks ... yet
472 case Stmt::BlockExprClass:
473 case Stmt::BlockDeclRefExprClass:
474 return NYS();
Ted Kremenekf4e15fc2008-02-26 02:37:08 +0000475
476 case Stmt::ParenExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000477 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000478
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000479 default:
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000480 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000481 };
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000482
Ted Kremenek411cdee2008-04-16 21:10:48 +0000483 if (AlwaysAddStmt) Block->appendStmt(Terminator);
484 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000485}
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000486
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000487/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
488/// for initializers in Decls.
489CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(ScopedDecl* D) {
490 VarDecl* VD = dyn_cast<VarDecl>(D);
491
492 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000493 return Block;
494
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000495 Expr* Init = VD->getInit();
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000496
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000497 if (Init) {
498 // Optimization: Don't create separate block-level statements for literals.
499 switch (Init->getStmtClass()) {
500 case Stmt::IntegerLiteralClass:
501 case Stmt::CharacterLiteralClass:
502 case Stmt::StringLiteralClass:
503 break;
504 default:
505 Block = addStmt(Init);
506 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000507 }
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000508
509 // If the type of VD is a VLA, then we must process its size expressions.
510 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
511 VA = FindVA(VA->getElementType().getTypePtr()))
512 Block = addStmt(VA->getSizeExpr());
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000513
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000514 return Block;
515}
516
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000517/// WalkAST_VisitChildren - Utility method to call WalkAST on the
518/// children of a Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000519CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000520 CFGBlock* B = Block;
Ted Kremenek411cdee2008-04-16 21:10:48 +0000521 for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000522 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000523 if (*I) B = WalkAST(*I);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000524
525 return B;
526}
527
Ted Kremenek15c27a82007-08-28 18:30:10 +0000528/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
529/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000530CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
531 Block->appendStmt(Terminator);
532 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000533}
534
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000535/// VisitStmt - Handle statements with no branching control flow.
536CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
537 // We cannot assume that we are in the middle of a basic block, since
538 // the CFG might only be constructed for this single statement. If
539 // we have no current basic block, just create one lazily.
540 if (!Block) Block = createBlock();
541
542 // Simply add the statement to the current block. We actually
543 // insert statements in reverse order; this order is reversed later
544 // when processing the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000545 addStmt(Statement);
546
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000547 return Block;
548}
549
550CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
551 return Block;
552}
553
554CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000555
556 CFGBlock* LastBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000557
Ted Kremenekd34066c2008-02-26 00:22:58 +0000558 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
559 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000560 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000561 }
562
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000563 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000564}
565
566CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
567 // We may see an if statement in the middle of a basic block, or
568 // it may be the first statement we are processing. In either case,
569 // we create a new basic block. First, we create the blocks for
570 // the then...else statements, and then we create the block containing
571 // the if statement. If we were in the middle of a block, we
572 // stop processing that block and reverse its statements. That block
573 // is then the implicit successor for the "then" and "else" clauses.
574
575 // The block we were proccessing is now finished. Make it the
576 // successor block.
577 if (Block) {
578 Succ = Block;
579 FinishBlock(Block);
580 }
581
582 // Process the false branch. NULL out Block so that the recursive
583 // call to Visit will create a new basic block.
584 // Null out Block so that all successor
585 CFGBlock* ElseBlock = Succ;
586
587 if (Stmt* Else = I->getElse()) {
588 SaveAndRestore<CFGBlock*> sv(Succ);
589
590 // NULL out Block so that the recursive call to Visit will
591 // create a new basic block.
592 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000593 ElseBlock = Visit(Else);
594
595 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
596 ElseBlock = sv.get();
597 else if (Block)
598 FinishBlock(ElseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000599 }
600
601 // Process the true branch. NULL out Block so that the recursive
602 // call to Visit will create a new basic block.
603 // Null out Block so that all successor
604 CFGBlock* ThenBlock;
605 {
606 Stmt* Then = I->getThen();
607 assert (Then);
608 SaveAndRestore<CFGBlock*> sv(Succ);
609 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000610 ThenBlock = Visit(Then);
611
612 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
613 ThenBlock = sv.get();
614 else if (Block)
615 FinishBlock(ThenBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000616 }
617
618 // Now create a new block containing the if statement.
619 Block = createBlock(false);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000620
621 // Set the terminator of the new block to the If statement.
622 Block->setTerminator(I);
623
624 // Now add the successors.
625 Block->addSuccessor(ThenBlock);
626 Block->addSuccessor(ElseBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000627
628 // Add the condition as the last statement in the new block. This
629 // may create new blocks as the condition may contain control-flow. Any
630 // newly created blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000631 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000632}
Ted Kremenekf50ec102007-09-11 21:29:43 +0000633
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000634
635CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
636 // If we were in the middle of a block we stop processing that block
637 // and reverse its statements.
638 //
639 // NOTE: If a "return" appears in the middle of a block, this means
640 // that the code afterwards is DEAD (unreachable). We still
641 // keep a basic block for that code; a simple "mark-and-sweep"
642 // from the entry block will be able to report such dead
643 // blocks.
644 if (Block) FinishBlock(Block);
645
646 // Create the new block.
647 Block = createBlock(false);
648
649 // The Exit block is the only successor.
650 Block->addSuccessor(&cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000651
652 // Add the return statement to the block. This may create new blocks
653 // if R contains control-flow (short-circuit operations).
654 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000655}
656
657CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
658 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000659 Visit(L->getSubStmt());
660 CFGBlock* LabelBlock = Block;
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000661
662 if (!LabelBlock) // This can happen when the body is empty, i.e.
663 LabelBlock=createBlock(); // scopes that only contains NullStmts.
664
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000665 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
666 LabelMap[ L ] = LabelBlock;
667
668 // Labels partition blocks, so this is the end of the basic block
Ted Kremenek9cffe732007-08-29 23:20:49 +0000669 // we were processing (L is the block's label). Because this is
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000670 // label (and we have already processed the substatement) there is no
671 // extra control-flow to worry about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000672 LabelBlock->setLabel(L);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000673 FinishBlock(LabelBlock);
674
675 // We set Block to NULL to allow lazy creation of a new block
676 // (if necessary);
677 Block = NULL;
678
679 // This block is now the implicit successor of other blocks.
680 Succ = LabelBlock;
681
682 return LabelBlock;
683}
684
685CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
686 // Goto is a control-flow statement. Thus we stop processing the
687 // current block and create a new one.
688 if (Block) FinishBlock(Block);
689 Block = createBlock(false);
690 Block->setTerminator(G);
691
692 // If we already know the mapping to the label block add the
693 // successor now.
694 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
695
696 if (I == LabelMap.end())
697 // We will need to backpatch this block later.
698 BackpatchBlocks.push_back(Block);
699 else
700 Block->addSuccessor(I->second);
701
702 return Block;
703}
704
705CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
706 // "for" is a control-flow statement. Thus we stop processing the
707 // current block.
708
709 CFGBlock* LoopSuccessor = NULL;
710
711 if (Block) {
712 FinishBlock(Block);
713 LoopSuccessor = Block;
714 }
715 else LoopSuccessor = Succ;
716
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000717 // Because of short-circuit evaluation, the condition of the loop
718 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
719 // blocks that evaluate the condition.
720 CFGBlock* ExitConditionBlock = createBlock(false);
721 CFGBlock* EntryConditionBlock = ExitConditionBlock;
722
723 // Set the terminator for the "exit" condition block.
724 ExitConditionBlock->setTerminator(F);
725
726 // Now add the actual condition to the condition block. Because the
727 // condition itself may contain control-flow, new blocks may be created.
728 if (Stmt* C = F->getCond()) {
729 Block = ExitConditionBlock;
730 EntryConditionBlock = addStmt(C);
731 if (Block) FinishBlock(EntryConditionBlock);
732 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000733
734 // The condition block is the implicit successor for the loop body as
735 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000736 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000737
738 // Now create the loop body.
739 {
740 assert (F->getBody());
741
742 // Save the current values for Block, Succ, and continue and break targets
743 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
744 save_continue(ContinueTargetBlock),
745 save_break(BreakTargetBlock);
Ted Kremeneke9334502008-09-04 21:48:47 +0000746
Ted Kremenekaf603f72007-08-30 18:39:40 +0000747 // Create a new block to contain the (bottom) of the loop body.
748 Block = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000749
Ted Kremeneke9334502008-09-04 21:48:47 +0000750 if (Stmt* I = F->getInc()) {
751 // Generate increment code in its own basic block. This is the target
752 // of continue statements.
Ted Kremenekd0172432008-11-24 20:50:24 +0000753 Succ = Visit(I);
754
755 // Finish up the increment block if it hasn't been already.
756 if (Block) {
757 assert (Block == Succ);
758 FinishBlock(Block);
759 Block = 0;
760 }
761
Ted Kremeneke9334502008-09-04 21:48:47 +0000762 ContinueTargetBlock = Succ;
763 }
764 else {
765 // No increment code. Continues should go the the entry condition block.
766 ContinueTargetBlock = EntryConditionBlock;
767 }
768
769 // All breaks should go to the code following the loop.
770 BreakTargetBlock = LoopSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000771
772 // Now populate the body block, and in the process create new blocks
773 // as we walk the body of the loop.
774 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000775
776 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000777 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000778 else if (Block)
779 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000780
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000781 // This new body block is a successor to our "exit" condition block.
782 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000783 }
784
785 // Link up the condition block with the code that follows the loop.
786 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000787 ExitConditionBlock->addSuccessor(LoopSuccessor);
788
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000789 // If the loop contains initialization, create a new block for those
790 // statements. This block can also contain statements that precede
791 // the loop.
792 if (Stmt* I = F->getInit()) {
793 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000794 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000795 }
796 else {
797 // There is no loop initialization. We are thus basically a while
798 // loop. NULL out Block to force lazy block construction.
799 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000800 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000801 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000802 }
803}
804
Ted Kremenek514de5a2008-11-11 17:10:00 +0000805CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
806 // Objective-C fast enumeration 'for' statements:
807 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
808 //
809 // for ( Type newVariable in collection_expression ) { statements }
810 //
811 // becomes:
812 //
813 // prologue:
814 // 1. collection_expression
815 // T. jump to loop_entry
816 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000817 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000818 // 1. ObjCForCollectionStmt [performs binding to newVariable]
819 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
820 // TB:
821 // statements
822 // T. jump to loop_entry
823 // FB:
824 // what comes after
825 //
826 // and
827 //
828 // Type existingItem;
829 // for ( existingItem in expression ) { statements }
830 //
831 // becomes:
832 //
833 // the same with newVariable replaced with existingItem; the binding
834 // works the same except that for one ObjCForCollectionStmt::getElement()
835 // returns a DeclStmt and the other returns a DeclRefExpr.
836 //
837
838 CFGBlock* LoopSuccessor = 0;
839
840 if (Block) {
841 FinishBlock(Block);
842 LoopSuccessor = Block;
843 Block = 0;
844 }
845 else LoopSuccessor = Succ;
846
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000847 // Build the condition blocks.
848 CFGBlock* ExitConditionBlock = createBlock(false);
849 CFGBlock* EntryConditionBlock = ExitConditionBlock;
850
851 // Set the terminator for the "exit" condition block.
852 ExitConditionBlock->setTerminator(S);
853
854 // The last statement in the block should be the ObjCForCollectionStmt,
855 // which performs the actual binding to 'element' and determines if there
856 // are any more items in the collection.
857 ExitConditionBlock->appendStmt(S);
858 Block = ExitConditionBlock;
859
860 // Walk the 'element' expression to see if there are any side-effects. We
861 // generate new blocks as necesary. We DON'T add the statement by default
862 // to the CFG unless it contains control-flow.
863 EntryConditionBlock = WalkAST(S->getElement(), false);
864 if (Block) { FinishBlock(EntryConditionBlock); Block = 0; }
865
866 // The condition block is the implicit successor for the loop body as
867 // well as any code above the loop.
868 Succ = EntryConditionBlock;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000869
870 // Now create the true branch.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000871 {
872 // Save the current values for Succ, continue and break targets.
873 SaveAndRestore<CFGBlock*> save_Succ(Succ),
874 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
875
876 BreakTargetBlock = LoopSuccessor;
877 ContinueTargetBlock = EntryConditionBlock;
878
879 CFGBlock* BodyBlock = Visit(S->getBody());
880
881 if (!BodyBlock)
882 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
883 else if (Block)
884 FinishBlock(BodyBlock);
885
886 // This new body block is a successor to our "exit" condition block.
887 ExitConditionBlock->addSuccessor(BodyBlock);
888 }
Ted Kremenekfc335522008-11-13 06:36:45 +0000889
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000890 // Link up the condition block with the code that follows the loop.
891 // (the false branch).
892 ExitConditionBlock->addSuccessor(LoopSuccessor);
893
Ted Kremenek514de5a2008-11-11 17:10:00 +0000894 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000895 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +0000896 return addStmt(S->getCollection());
897}
898
899
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000900CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
901 // "while" is a control-flow statement. Thus we stop processing the
902 // current block.
903
904 CFGBlock* LoopSuccessor = NULL;
905
906 if (Block) {
907 FinishBlock(Block);
908 LoopSuccessor = Block;
909 }
910 else LoopSuccessor = Succ;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000911
912 // Because of short-circuit evaluation, the condition of the loop
913 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
914 // blocks that evaluate the condition.
915 CFGBlock* ExitConditionBlock = createBlock(false);
916 CFGBlock* EntryConditionBlock = ExitConditionBlock;
917
918 // Set the terminator for the "exit" condition block.
919 ExitConditionBlock->setTerminator(W);
920
921 // Now add the actual condition to the condition block. Because the
922 // condition itself may contain control-flow, new blocks may be created.
923 // Thus we update "Succ" after adding the condition.
924 if (Stmt* C = W->getCond()) {
925 Block = ExitConditionBlock;
926 EntryConditionBlock = addStmt(C);
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000927 assert (Block == EntryConditionBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000928 if (Block) FinishBlock(EntryConditionBlock);
929 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000930
931 // The condition block is the implicit successor for the loop body as
932 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000933 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000934
935 // Process the loop body.
936 {
937 assert (W->getBody());
938
939 // Save the current values for Block, Succ, and continue and break targets
940 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
941 save_continue(ContinueTargetBlock),
942 save_break(BreakTargetBlock);
943
944 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000945 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000946
947 // All breaks should go to the code following the loop.
948 BreakTargetBlock = LoopSuccessor;
949
950 // NULL out Block to force lazy instantiation of blocks for the body.
951 Block = NULL;
952
953 // Create the body. The returned block is the entry to the loop body.
954 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000955
956 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000957 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000958 else if (Block)
959 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000960
961 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000962 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000963 }
964
965 // Link up the condition block with the code that follows the loop.
966 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000967 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000968
969 // There can be no more statements in the condition block
970 // since we loop back to this block. NULL out Block to force
971 // lazy creation of another block.
972 Block = NULL;
973
974 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +0000975 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000976 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000977}
Ted Kremenek2fda5042008-12-09 20:20:09 +0000978
979CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
980 // FIXME: This isn't complete. We basically treat @throw like a return
981 // statement.
982
983 // If we were in the middle of a block we stop processing that block
984 // and reverse its statements.
985 if (Block) FinishBlock(Block);
986
987 // Create the new block.
988 Block = createBlock(false);
989
990 // The Exit block is the only successor.
991 Block->addSuccessor(&cfg->getExit());
992
993 // Add the statement to the block. This may create new blocks
994 // if S contains control-flow (short-circuit operations).
995 return addStmt(S);
996}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000997
998CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
999 // "do...while" is a control-flow statement. Thus we stop processing the
1000 // current block.
1001
1002 CFGBlock* LoopSuccessor = NULL;
1003
1004 if (Block) {
1005 FinishBlock(Block);
1006 LoopSuccessor = Block;
1007 }
1008 else LoopSuccessor = Succ;
1009
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001010 // Because of short-circuit evaluation, the condition of the loop
1011 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
1012 // blocks that evaluate the condition.
1013 CFGBlock* ExitConditionBlock = createBlock(false);
1014 CFGBlock* EntryConditionBlock = ExitConditionBlock;
1015
1016 // Set the terminator for the "exit" condition block.
1017 ExitConditionBlock->setTerminator(D);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001018
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001019 // Now add the actual condition to the condition block. Because the
1020 // condition itself may contain control-flow, new blocks may be created.
1021 if (Stmt* C = D->getCond()) {
1022 Block = ExitConditionBlock;
1023 EntryConditionBlock = addStmt(C);
1024 if (Block) FinishBlock(EntryConditionBlock);
1025 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001026
Ted Kremenek54827132008-02-27 07:20:00 +00001027 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001028 Succ = EntryConditionBlock;
1029
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001030 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001031 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001032 {
1033 assert (D->getBody());
1034
1035 // Save the current values for Block, Succ, and continue and break targets
1036 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1037 save_continue(ContinueTargetBlock),
1038 save_break(BreakTargetBlock);
1039
1040 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001041 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001042
1043 // All breaks should go to the code following the loop.
1044 BreakTargetBlock = LoopSuccessor;
1045
1046 // NULL out Block to force lazy instantiation of blocks for the body.
1047 Block = NULL;
1048
1049 // Create the body. The returned block is the entry to the loop body.
1050 BodyBlock = Visit(D->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001051
Ted Kremenekaf603f72007-08-30 18:39:40 +00001052 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001053 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenekaf603f72007-08-30 18:39:40 +00001054 else if (Block)
1055 FinishBlock(BodyBlock);
1056
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001057 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001058 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001059 }
1060
1061 // Link up the condition block with the code that follows the loop.
1062 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001063 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001064
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001065 // There can be no more statements in the body block(s)
1066 // since we loop back to the body. NULL out Block to force
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001067 // lazy creation of another block.
1068 Block = NULL;
1069
1070 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001071 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001072 return BodyBlock;
1073}
1074
1075CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1076 // "continue" is a control-flow statement. Thus we stop processing the
1077 // current block.
1078 if (Block) FinishBlock(Block);
1079
1080 // Now create a new block that ends with the continue statement.
1081 Block = createBlock(false);
1082 Block->setTerminator(C);
1083
1084 // If there is no target for the continue, then we are looking at an
1085 // incomplete AST. Handle this by not registering a successor.
1086 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1087
1088 return Block;
1089}
1090
1091CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1092 // "break" is a control-flow statement. Thus we stop processing the
1093 // current block.
1094 if (Block) FinishBlock(Block);
1095
1096 // Now create a new block that ends with the continue statement.
1097 Block = createBlock(false);
1098 Block->setTerminator(B);
1099
1100 // If there is no target for the break, then we are looking at an
1101 // incomplete AST. Handle this by not registering a successor.
1102 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1103
1104 return Block;
1105}
1106
Ted Kremenek411cdee2008-04-16 21:10:48 +00001107CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001108 // "switch" is a control-flow statement. Thus we stop processing the
1109 // current block.
1110 CFGBlock* SwitchSuccessor = NULL;
1111
1112 if (Block) {
1113 FinishBlock(Block);
1114 SwitchSuccessor = Block;
1115 }
1116 else SwitchSuccessor = Succ;
1117
1118 // Save the current "switch" context.
1119 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001120 save_break(BreakTargetBlock),
1121 save_default(DefaultCaseBlock);
1122
1123 // Set the "default" case to be the block after the switch statement.
1124 // If the switch statement contains a "default:", this value will
1125 // be overwritten with the block for that code.
1126 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenek295222c2008-02-13 21:46:34 +00001127
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001128 // Create a new block that will contain the switch statement.
1129 SwitchTerminatedBlock = createBlock(false);
1130
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001131 // Now process the switch body. The code after the switch is the implicit
1132 // successor.
1133 Succ = SwitchSuccessor;
1134 BreakTargetBlock = SwitchSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001135
1136 // When visiting the body, the case statements should automatically get
1137 // linked up to the switch. We also don't keep a pointer to the body,
1138 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001139 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001140 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001141 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001142 if (Block) FinishBlock(BodyBlock);
1143
Ted Kremenek295222c2008-02-13 21:46:34 +00001144 // If we have no "default:" case, the default transition is to the
1145 // code following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001146 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenek295222c2008-02-13 21:46:34 +00001147
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001148 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001149 SwitchTerminatedBlock->setTerminator(Terminator);
1150 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001151 Block = SwitchTerminatedBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001152
Ted Kremenek411cdee2008-04-16 21:10:48 +00001153 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001154}
1155
Ted Kremenek411cdee2008-04-16 21:10:48 +00001156CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001157 // CaseStmts are essentially labels, so they are the
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001158 // first statement in a block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001159
Ted Kremenek411cdee2008-04-16 21:10:48 +00001160 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001161 CFGBlock* CaseBlock = Block;
1162 if (!CaseBlock) CaseBlock = createBlock();
1163
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001164 // Cases statements partition blocks, so this is the top of
1165 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001166 CaseBlock->setLabel(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001167 FinishBlock(CaseBlock);
1168
1169 // Add this block to the list of successors for the block with the
1170 // switch statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001171 assert (SwitchTerminatedBlock);
1172 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001173
1174 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1175 Block = NULL;
1176
1177 // This block is now the implicit successor of other blocks.
1178 Succ = CaseBlock;
1179
Ted Kremenek2677ea82008-03-15 07:45:02 +00001180 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001181}
Ted Kremenek295222c2008-02-13 21:46:34 +00001182
Ted Kremenek411cdee2008-04-16 21:10:48 +00001183CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1184 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001185 DefaultCaseBlock = Block;
1186 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1187
1188 // Default statements partition blocks, so this is the top of
1189 // the basic block we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001190 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001191 FinishBlock(DefaultCaseBlock);
1192
1193 // Unlike case statements, we don't add the default block to the
1194 // successors for the switch statement immediately. This is done
1195 // when we finish processing the switch statement. This allows for
1196 // the default case (including a fall-through to the code after the
1197 // switch statement) to always be the last successor of a switch-terminated
1198 // block.
1199
1200 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1201 Block = NULL;
1202
1203 // This block is now the implicit successor of other blocks.
1204 Succ = DefaultCaseBlock;
1205
1206 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001207}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001208
Ted Kremenek19bb3562007-08-28 19:26:49 +00001209CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1210 // Lazily create the indirect-goto dispatch block if there isn't one
1211 // already.
1212 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1213
1214 if (!IBlock) {
1215 IBlock = createBlock(false);
1216 cfg->setIndirectGotoBlock(IBlock);
1217 }
1218
1219 // IndirectGoto is a control-flow statement. Thus we stop processing the
1220 // current block and create a new one.
1221 if (Block) FinishBlock(Block);
1222 Block = createBlock(false);
1223 Block->setTerminator(I);
1224 Block->addSuccessor(IBlock);
1225 return addStmt(I->getTarget());
1226}
1227
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001228
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001229} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001230
1231/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1232/// block has no successors or predecessors. If this is the first block
1233/// created in the CFG, it is automatically set to be the Entry and Exit
1234/// of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001235CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001236 bool first_block = begin() == end();
1237
1238 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001239 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001240
1241 // If this is the first block, set it as the Entry and Exit.
1242 if (first_block) Entry = Exit = &front();
1243
1244 // Return the block.
1245 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001246}
1247
Ted Kremenek026473c2007-08-23 16:51:22 +00001248/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1249/// CFG is returned to the caller.
1250CFG* CFG::buildCFG(Stmt* Statement) {
1251 CFGBuilder Builder;
1252 return Builder.buildCFG(Statement);
1253}
1254
1255/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001256void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1257
Ted Kremenek63f58872007-10-01 19:33:33 +00001258//===----------------------------------------------------------------------===//
1259// CFG: Queries for BlkExprs.
1260//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001261
Ted Kremenek63f58872007-10-01 19:33:33 +00001262namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001263 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001264}
1265
Ted Kremenek411cdee2008-04-16 21:10:48 +00001266static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1267 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001268 return;
1269
Ted Kremenek411cdee2008-04-16 21:10:48 +00001270 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001271 if (!*I) continue;
1272
1273 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1274 if (B->isAssignmentOp()) Set.insert(B);
1275
1276 FindSubExprAssignments(*I, Set);
1277 }
1278}
1279
Ted Kremenek63f58872007-10-01 19:33:33 +00001280static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1281 BlkExprMapTy* M = new BlkExprMapTy();
1282
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001283 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek411cdee2008-04-16 21:10:48 +00001284 // only assignments that we want to *possibly* register as a block-level
1285 // expression. Basically, if an assignment occurs both in a subexpression
1286 // and at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001287 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1288
Ted Kremenek63f58872007-10-01 19:33:33 +00001289 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1290 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001291 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001292
Ted Kremenek411cdee2008-04-16 21:10:48 +00001293 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1294
1295 // Iterate over the statements again on identify the Expr* and Stmt* at
1296 // the block-level that are block-level expressions.
1297
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001298 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001299 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001300
Ted Kremenek411cdee2008-04-16 21:10:48 +00001301 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001302 // Assignment expressions that are not nested within another
1303 // expression are really "statements" whose value is never
1304 // used by another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001305 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001306 continue;
1307 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001308 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001309 // Special handling for statement expressions. The last statement
1310 // in the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001311 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001312 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001313 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001314 (*M)[C->body_back()] = x;
1315 }
1316 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001317
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001318 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001319 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001320 }
1321
Ted Kremenek411cdee2008-04-16 21:10:48 +00001322 // Look at terminators. The condition is a block-level expression.
1323
Ted Kremenek390e48b2008-11-12 21:11:49 +00001324 Stmt* S = I->getTerminatorCondition();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001325
Ted Kremenek390e48b2008-11-12 21:11:49 +00001326 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001327 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001328 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001329 }
1330 }
1331
Ted Kremenek63f58872007-10-01 19:33:33 +00001332 return M;
1333}
1334
Ted Kremenek86946742008-01-17 20:48:37 +00001335CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1336 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001337 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1338
1339 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001340 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek63f58872007-10-01 19:33:33 +00001341
1342 if (I == M->end()) return CFG::BlkExprNumTy();
1343 else return CFG::BlkExprNumTy(I->second);
1344}
1345
1346unsigned CFG::getNumBlkExprs() {
1347 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1348 return M->size();
1349 else {
1350 // We assume callers interested in the number of BlkExprs will want
1351 // the map constructed if it doesn't already exist.
1352 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1353 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1354 }
1355}
1356
Ted Kremenek274f4332008-04-28 18:00:46 +00001357//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001358// Cleanup: CFG dstor.
1359//===----------------------------------------------------------------------===//
1360
Ted Kremenek63f58872007-10-01 19:33:33 +00001361CFG::~CFG() {
1362 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1363}
1364
Ted Kremenek7dba8602007-08-29 21:56:09 +00001365//===----------------------------------------------------------------------===//
1366// CFG pretty printing
1367//===----------------------------------------------------------------------===//
1368
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001369namespace {
1370
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001371class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001372
Ted Kremenek42a509f2007-08-31 21:30:12 +00001373 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1374 StmtMapTy StmtMap;
1375 signed CurrentBlock;
1376 unsigned CurrentStmt;
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001377
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001379
Ted Kremenek42a509f2007-08-31 21:30:12 +00001380 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1381 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1382 unsigned j = 1;
1383 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1384 BI != BEnd; ++BI, ++j )
1385 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1386 }
1387 }
1388
1389 virtual ~StmtPrinterHelper() {}
1390
1391 void setBlockID(signed i) { CurrentBlock = i; }
1392 void setStmtID(unsigned i) { CurrentStmt = i; }
1393
Ted Kremeneka95d3752008-09-13 05:16:45 +00001394 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001395
Ted Kremenek411cdee2008-04-16 21:10:48 +00001396 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001397
1398 if (I == StmtMap.end())
1399 return false;
1400
1401 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1402 && I->second.second == CurrentStmt)
1403 return false;
1404
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001405 OS << "[B" << I->second.first << "." << I->second.second << "]";
1406 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001407 }
1408};
1409
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001410class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1411 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1412
Ted Kremeneka95d3752008-09-13 05:16:45 +00001413 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001414 StmtPrinterHelper* Helper;
1415public:
Ted Kremeneka95d3752008-09-13 05:16:45 +00001416 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek42a509f2007-08-31 21:30:12 +00001417 : OS(os), Helper(helper) {}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001418
1419 void VisitIfStmt(IfStmt* I) {
1420 OS << "if ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001421 I->getCond()->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001422 }
1423
1424 // Default case.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001425 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001426
1427 void VisitForStmt(ForStmt* F) {
1428 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001429 if (F->getInit()) OS << "...";
1430 OS << "; ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001431 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek535bb202007-08-30 21:28:02 +00001432 OS << "; ";
1433 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001434 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001435 }
1436
1437 void VisitWhileStmt(WhileStmt* W) {
1438 OS << "while " ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001439 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001440 }
1441
1442 void VisitDoStmt(DoStmt* D) {
1443 OS << "do ... while ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001444 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001445 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001446
Ted Kremenek411cdee2008-04-16 21:10:48 +00001447 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001448 OS << "switch ";
Ted Kremenek411cdee2008-04-16 21:10:48 +00001449 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001450 }
1451
Ted Kremenek805e9a82007-08-31 21:49:40 +00001452 void VisitConditionalOperator(ConditionalOperator* C) {
1453 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001454 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001455 }
1456
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001457 void VisitChooseExpr(ChooseExpr* C) {
1458 OS << "__builtin_choose_expr( ";
1459 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001460 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001461 }
1462
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001463 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1464 OS << "goto *";
1465 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001466 }
1467
Ted Kremenek805e9a82007-08-31 21:49:40 +00001468 void VisitBinaryOperator(BinaryOperator* B) {
1469 if (!B->isLogicalOp()) {
1470 VisitExpr(B);
1471 return;
1472 }
1473
1474 B->getLHS()->printPretty(OS,Helper);
1475
1476 switch (B->getOpcode()) {
1477 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001478 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001479 return;
1480 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001481 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001482 return;
1483 default:
1484 assert(false && "Invalid logical operator.");
1485 }
1486 }
1487
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001488 void VisitExpr(Expr* E) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001489 E->printPretty(OS,Helper);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001490 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001491};
Ted Kremenek42a509f2007-08-31 21:30:12 +00001492
1493
Ted Kremeneka95d3752008-09-13 05:16:45 +00001494void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001495 if (Helper) {
1496 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001497 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001498 CompoundStmt* Sub = SE->getSubStmt();
1499
1500 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001501 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001502 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001503 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001504 return;
1505 }
1506 }
1507
1508 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001509 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001510 if (B->getOpcode() == BinaryOperator::Comma) {
1511 OS << "... , ";
1512 Helper->handledStmt(B->getRHS(),OS);
1513 OS << '\n';
1514 return;
1515 }
1516 }
1517 }
1518
Ted Kremenek411cdee2008-04-16 21:10:48 +00001519 Terminator->printPretty(OS, Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001520
1521 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001522 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001523}
1524
Ted Kremeneka95d3752008-09-13 05:16:45 +00001525void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek42a509f2007-08-31 21:30:12 +00001526 StmtPrinterHelper* Helper, bool print_edges) {
1527
1528 if (Helper) Helper->setBlockID(B.getBlockID());
1529
Ted Kremenek7dba8602007-08-29 21:56:09 +00001530 // Print the header.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001531 OS << "\n [ B" << B.getBlockID();
1532
1533 if (&B == &cfg->getEntry())
1534 OS << " (ENTRY) ]\n";
1535 else if (&B == &cfg->getExit())
1536 OS << " (EXIT) ]\n";
1537 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001538 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001539 else
1540 OS << " ]\n";
1541
Ted Kremenek9cffe732007-08-29 23:20:49 +00001542 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001543 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001544
1545 if (print_edges)
1546 OS << " ";
1547
Ted Kremenek411cdee2008-04-16 21:10:48 +00001548 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001549 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001550 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001551 OS << "case ";
1552 C->getLHS()->printPretty(OS);
1553 if (C->getRHS()) {
1554 OS << " ... ";
1555 C->getRHS()->printPretty(OS);
1556 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001557 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001558 else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001559 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001560 else
1561 assert(false && "Invalid label statement in CFGBlock.");
1562
Ted Kremenek9cffe732007-08-29 23:20:49 +00001563 OS << ":\n";
1564 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001565
Ted Kremenekfddd5182007-08-21 21:42:03 +00001566 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001567 unsigned j = 1;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001568
1569 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1570 I != E ; ++I, ++j ) {
1571
Ted Kremenek9cffe732007-08-29 23:20:49 +00001572 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001573 if (print_edges)
1574 OS << " ";
1575
Ted Kremeneka95d3752008-09-13 05:16:45 +00001576 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001577
1578 if (Helper)
1579 Helper->setStmtID(j);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001580
1581 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001582 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001583
Ted Kremenek9cffe732007-08-29 23:20:49 +00001584 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001585 if (B.getTerminator()) {
1586 if (print_edges)
1587 OS << " ";
1588
Ted Kremenek9cffe732007-08-29 23:20:49 +00001589 OS << " T: ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001590
1591 if (Helper) Helper->setBlockID(-1);
1592
1593 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1594 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001595 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001596 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001597
Ted Kremenek9cffe732007-08-29 23:20:49 +00001598 if (print_edges) {
1599 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001600 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001601 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001602
Ted Kremenek42a509f2007-08-31 21:30:12 +00001603 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1604 I != E; ++I, ++i) {
1605
1606 if (i == 8 || (i-8) == 0)
1607 OS << "\n ";
1608
Ted Kremenek9cffe732007-08-29 23:20:49 +00001609 OS << " B" << (*I)->getBlockID();
1610 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001611
1612 OS << '\n';
1613
1614 // Print the successors of this block.
1615 OS << " Successors (" << B.succ_size() << "):";
1616 i = 0;
1617
1618 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1619 I != E; ++I, ++i) {
1620
1621 if (i == 8 || (i-8) % 10 == 0)
1622 OS << "\n ";
1623
1624 OS << " B" << (*I)->getBlockID();
1625 }
1626
Ted Kremenek9cffe732007-08-29 23:20:49 +00001627 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001628 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001629}
1630
1631} // end anonymous namespace
1632
1633/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001634void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001635
1636/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001637void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001638
1639 StmtPrinterHelper Helper(this);
1640
1641 // Print the entry block.
1642 print_block(OS, this, getEntry(), &Helper, true);
1643
1644 // Iterate through the CFGBlocks and print them one by one.
1645 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1646 // Skip the entry block, because we already printed it.
1647 if (&(*I) == &getEntry() || &(*I) == &getExit())
1648 continue;
1649
1650 print_block(OS, this, *I, &Helper, true);
1651 }
1652
1653 // Print the exit block.
1654 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001655 OS.flush();
Ted Kremenek42a509f2007-08-31 21:30:12 +00001656}
1657
1658/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001659void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001660
1661/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1662/// Generally this will only be called from CFG::print.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001663void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001664 StmtPrinterHelper Helper(cfg);
1665 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001666}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001667
Ted Kremeneka2925852008-01-30 23:02:42 +00001668/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001669void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremeneka2925852008-01-30 23:02:42 +00001670 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1671 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1672}
1673
Ted Kremenek390e48b2008-11-12 21:11:49 +00001674Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001675
1676 if (!Terminator)
1677 return NULL;
1678
1679 Expr* E = NULL;
1680
1681 switch (Terminator->getStmtClass()) {
1682 default:
1683 break;
1684
1685 case Stmt::ForStmtClass:
1686 E = cast<ForStmt>(Terminator)->getCond();
1687 break;
1688
1689 case Stmt::WhileStmtClass:
1690 E = cast<WhileStmt>(Terminator)->getCond();
1691 break;
1692
1693 case Stmt::DoStmtClass:
1694 E = cast<DoStmt>(Terminator)->getCond();
1695 break;
1696
1697 case Stmt::IfStmtClass:
1698 E = cast<IfStmt>(Terminator)->getCond();
1699 break;
1700
1701 case Stmt::ChooseExprClass:
1702 E = cast<ChooseExpr>(Terminator)->getCond();
1703 break;
1704
1705 case Stmt::IndirectGotoStmtClass:
1706 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1707 break;
1708
1709 case Stmt::SwitchStmtClass:
1710 E = cast<SwitchStmt>(Terminator)->getCond();
1711 break;
1712
1713 case Stmt::ConditionalOperatorClass:
1714 E = cast<ConditionalOperator>(Terminator)->getCond();
1715 break;
1716
1717 case Stmt::BinaryOperatorClass: // '&&' and '||'
1718 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001719 break;
1720
1721 case Stmt::ObjCForCollectionStmtClass:
1722 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001723 }
1724
1725 return E ? E->IgnoreParens() : NULL;
1726}
1727
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001728bool CFGBlock::hasBinaryBranchTerminator() const {
1729
1730 if (!Terminator)
1731 return false;
1732
1733 Expr* E = NULL;
1734
1735 switch (Terminator->getStmtClass()) {
1736 default:
1737 return false;
1738
1739 case Stmt::ForStmtClass:
1740 case Stmt::WhileStmtClass:
1741 case Stmt::DoStmtClass:
1742 case Stmt::IfStmtClass:
1743 case Stmt::ChooseExprClass:
1744 case Stmt::ConditionalOperatorClass:
1745 case Stmt::BinaryOperatorClass:
1746 return true;
1747 }
1748
1749 return E ? E->IgnoreParens() : NULL;
1750}
1751
Ted Kremeneka2925852008-01-30 23:02:42 +00001752
Ted Kremenek7dba8602007-08-29 21:56:09 +00001753//===----------------------------------------------------------------------===//
1754// CFG Graphviz Visualization
1755//===----------------------------------------------------------------------===//
1756
Ted Kremenek42a509f2007-08-31 21:30:12 +00001757
1758#ifndef NDEBUG
Chris Lattner00123512007-09-17 06:16:32 +00001759static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001760#endif
1761
1762void CFG::viewCFG() const {
1763#ifndef NDEBUG
1764 StmtPrinterHelper H(this);
1765 GraphHelper = &H;
1766 llvm::ViewGraph(this,"CFG");
1767 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001768#endif
1769}
1770
Ted Kremenek7dba8602007-08-29 21:56:09 +00001771namespace llvm {
1772template<>
1773struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1774 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1775
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001776#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001777 std::string OutSStr;
1778 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001779 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001780 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001781
1782 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1783
1784 // Process string output to make it nicer...
1785 for (unsigned i = 0; i != OutStr.length(); ++i)
1786 if (OutStr[i] == '\n') { // Left justify
1787 OutStr[i] = '\\';
1788 OutStr.insert(OutStr.begin()+i+1, 'l');
1789 }
1790
1791 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001792#else
1793 return "";
1794#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001795 }
1796};
1797} // end namespace llvm