blob: d5fde0a8198fb9038b8139bc0feccbc47145c8a9 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenek4aa1e8b2007-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
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor6e6ad602009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Mike Stump31feda52009-07-17 01:31:16 +000037
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000038/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000039/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;
45/// CFG* cfg = builder.BuildAST(stmt1);
46///
Mike Stump31feda52009-07-17 01:31:16 +000047/// CFG construction is done via a recursive walk of an AST. We actually parse
48/// the AST in reverse order so that the successor of a basic block is
49/// constructed prior to its predecessor. This allows us to nicely capture
50/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000051///
Ted Kremenek93668002009-07-17 22:18:43 +000052class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000053 ASTContext *Context;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000054 CFG* cfg;
55 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000057 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000058 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000059 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000060 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000061
Ted Kremenekeda180e22007-08-28 19:26:49 +000062 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000063 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
64 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000065
66 // A list of blocks that end with a "goto" that must be backpatched to their
67 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000068 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000069 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000070
Ted Kremenekeda180e22007-08-28 19:26:49 +000071 // A list of labels whose address has been taken (for indirect gotos).
72 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
73 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000074
75public:
Ted Kremenek889073f2007-08-23 16:51:22 +000076 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000077 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000078 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000079 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000080 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000081 }
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000083 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000084
Ted Kremenek9aae5132007-08-23 21:42:29 +000085 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000086 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000087
Ted Kremenek93668002009-07-17 22:18:43 +000088private:
89 // Visitors to walk an AST and construct the CFG.
90 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
91 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000092 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
93 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000094 CFGBlock *VisitBreakStmt(BreakStmt *B);
95 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
96 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000097 CFGBlock *VisitChooseExpr(ChooseExpr *C);
98 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
99 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
100 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000101 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +0000102 CFGBlock *VisitDeclStmt(DeclStmt *DS);
103 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000104 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
105 CFGBlock *VisitDoStmt(DoStmt *D);
106 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000107 CFGBlock *VisitGotoStmt(GotoStmt* G);
108 CFGBlock *VisitIfStmt(IfStmt *I);
109 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
110 CFGBlock *VisitLabelStmt(LabelStmt *L);
111 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
112 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
113 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
114 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
115 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
116 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000117 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
118 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000119 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
120 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000121
Ted Kremenek93668002009-07-17 22:18:43 +0000122 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
123 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
124 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000125
Ted Kremenek6065ef62008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
Mike Stump31feda52009-07-17 01:31:16 +0000131
Ted Kremenek93668002009-07-17 22:18:43 +0000132 void autoCreateBlock() { if (!Block) Block = createBlock(); }
133 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000134 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000135 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
Mike Stump11289f42009-09-09 15:08:12 +0000136
137
Ted Kremenek963cc312009-07-24 06:55:42 +0000138 /// TryResult - a class representing a variant over the values
139 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
140 /// and is used by the CFGBuilder to decide if a branch condition
141 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000142 class TryResult {
143 int X;
144 public:
145 TryResult(bool b) : X(b ? 1 : 0) {}
146 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000147
Ted Kremenek30754282009-07-24 04:47:11 +0000148 bool isTrue() const { return X == 1; }
149 bool isFalse() const { return X == 0; }
150 bool isKnown() const { return X >= 0; }
151 void negate() {
152 assert(isKnown());
153 X ^= 0x1;
154 }
155 };
Mike Stump11289f42009-09-09 15:08:12 +0000156
Mike Stump773582d2009-07-23 23:25:26 +0000157 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
158 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000159 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000160 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000161 if (!S->isTypeDependent() && !S->isValueDependent() &&
162 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000163 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000164
165 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000166 }
167
Ted Kremenekb64d1832008-03-13 03:04:22 +0000168 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000169};
Mike Stump31feda52009-07-17 01:31:16 +0000170
Douglas Gregor4619e432008-12-05 23:32:09 +0000171// FIXME: Add support for dependent-sized array types in C++?
172// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000173static VariableArrayType* FindVA(Type* t) {
174 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
175 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
176 if (vat->getSizeExpr())
177 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000178
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000179 t = vt->getElementType().getTypePtr();
180 }
Mike Stump31feda52009-07-17 01:31:16 +0000181
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000182 return 0;
183}
Mike Stump31feda52009-07-17 01:31:16 +0000184
185/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
186/// arbitrary statement. Examples include a single expression or a function
187/// body (compound statement). The ownership of the returned CFG is
188/// transferred to the caller. If CFG construction fails, this method returns
189/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000190CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
191 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000192 assert(cfg);
193 if (!Statement)
194 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000195
Ted Kremenekb64d1832008-03-13 03:04:22 +0000196 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000197
198 // Create an empty block that will serve as the exit block for the CFG. Since
199 // this is the first block added to the CFG, it will be implicitly registered
200 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000201 Succ = createBlock();
202 assert (Succ == &cfg->getExit());
203 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000204
Ted Kremenek9aae5132007-08-23 21:42:29 +0000205 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000206 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000207 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000208
Ted Kremenek40d87632008-02-27 17:33:02 +0000209 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000210 // Finalize the last constructed block. This usually involves reversing the
211 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000212 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000213
214 // Backpatch the gotos whose label -> block mappings we didn't know when we
215 // encountered them.
216 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000217 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000218
Ted Kremenek9aae5132007-08-23 21:42:29 +0000219 CFGBlock* B = *I;
220 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
221 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
222
223 // If there is no target for the goto, then we are looking at an
224 // incomplete AST. Handle this by not registering a successor.
225 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000226
227 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000228 }
Mike Stump31feda52009-07-17 01:31:16 +0000229
Ted Kremenekeda180e22007-08-28 19:26:49 +0000230 // Add successors to the Indirect Goto Dispatch block (if we have one).
231 if (CFGBlock* B = cfg->getIndirectGotoBlock())
232 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
233 E = AddressTakenLabels.end(); I != E; ++I ) {
234
235 // Lookup the target block.
236 LabelMapTy::iterator LI = LabelMap.find(*I);
237
238 // If there is no target block that contains label, then we are looking
239 // at an incomplete AST. Handle this by not registering a successor.
240 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000241
242 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000243 }
Mike Stump31feda52009-07-17 01:31:16 +0000244
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000245 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000246 }
Mike Stump31feda52009-07-17 01:31:16 +0000247
248 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000249 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000250
Ted Kremenekb64d1832008-03-13 03:04:22 +0000251 if (badCFG) {
252 delete cfg;
253 cfg = NULL;
254 return NULL;
255 }
Mike Stump31feda52009-07-17 01:31:16 +0000256
257 // NULL out cfg so that repeated calls to the builder will fail and that the
258 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000259 CFG* t = cfg;
260 cfg = NULL;
261 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000262}
Mike Stump31feda52009-07-17 01:31:16 +0000263
Ted Kremenek9aae5132007-08-23 21:42:29 +0000264/// createBlock - Used to lazily create blocks that are connected
265/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000266CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000267 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000268 if (add_successor && Succ)
269 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000270 return B;
271}
Mike Stump31feda52009-07-17 01:31:16 +0000272
273/// FinishBlock - When the last statement has been added to the block, we must
274/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000275bool CFGBuilder::FinishBlock(CFGBlock* B) {
276 if (badCFG)
277 return false;
278
Ted Kremenek93668002009-07-17 22:18:43 +0000279 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000280 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000281 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000282}
283
Ted Kremenek93668002009-07-17 22:18:43 +0000284/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000285/// blocks for ternary operators, &&, and ||. We also process "," and
286/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000287CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
288tryAgain:
289 switch (S->getStmtClass()) {
290 default:
291 return VisitStmt(S, alwaysAdd);
292
293 case Stmt::AddrLabelExprClass:
294 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenek93668002009-07-17 22:18:43 +0000296 case Stmt::BinaryOperatorClass:
297 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000298
Ted Kremenek93668002009-07-17 22:18:43 +0000299 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000300 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000301
302 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000303 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek93668002009-07-17 22:18:43 +0000305 case Stmt::BreakStmtClass:
306 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenek93668002009-07-17 22:18:43 +0000308 case Stmt::CallExprClass:
309 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000310
Ted Kremenek93668002009-07-17 22:18:43 +0000311 case Stmt::CaseStmtClass:
312 return VisitCaseStmt(cast<CaseStmt>(S));
313
314 case Stmt::ChooseExprClass:
315 return VisitChooseExpr(cast<ChooseExpr>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek93668002009-07-17 22:18:43 +0000317 case Stmt::CompoundStmtClass:
318 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremenek93668002009-07-17 22:18:43 +0000320 case Stmt::ConditionalOperatorClass:
321 return VisitConditionalOperator(cast<ConditionalOperator>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek93668002009-07-17 22:18:43 +0000323 case Stmt::ContinueStmtClass:
324 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek93668002009-07-17 22:18:43 +0000326 case Stmt::DeclStmtClass:
327 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek93668002009-07-17 22:18:43 +0000329 case Stmt::DefaultStmtClass:
330 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 case Stmt::DoStmtClass:
333 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek93668002009-07-17 22:18:43 +0000335 case Stmt::ForStmtClass:
336 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 case Stmt::GotoStmtClass:
339 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 case Stmt::IfStmtClass:
342 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek93668002009-07-17 22:18:43 +0000344 case Stmt::IndirectGotoStmtClass:
345 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek93668002009-07-17 22:18:43 +0000347 case Stmt::LabelStmtClass:
348 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek93668002009-07-17 22:18:43 +0000350 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000351 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
352
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000353 case Stmt::CXXThrowExprClass:
354 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
355
Ted Kremenek93668002009-07-17 22:18:43 +0000356 case Stmt::ObjCAtSynchronizedStmtClass:
357 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek93668002009-07-17 22:18:43 +0000359 case Stmt::ObjCAtThrowStmtClass:
360 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000361
Ted Kremenek93668002009-07-17 22:18:43 +0000362 case Stmt::ObjCAtTryStmtClass:
363 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenek93668002009-07-17 22:18:43 +0000365 case Stmt::ObjCForCollectionStmtClass:
366 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek93668002009-07-17 22:18:43 +0000368 case Stmt::ParenExprClass:
369 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000370 goto tryAgain;
371
Ted Kremenek93668002009-07-17 22:18:43 +0000372 case Stmt::NullStmtClass:
373 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000374
Ted Kremenek93668002009-07-17 22:18:43 +0000375 case Stmt::ReturnStmtClass:
376 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenek93668002009-07-17 22:18:43 +0000378 case Stmt::SizeOfAlignOfExprClass:
Mike Stump11289f42009-09-09 15:08:12 +0000379 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
380
Ted Kremenek93668002009-07-17 22:18:43 +0000381 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000382 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek93668002009-07-17 22:18:43 +0000384 case Stmt::SwitchStmtClass:
385 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek93668002009-07-17 22:18:43 +0000387 case Stmt::WhileStmtClass:
388 return VisitWhileStmt(cast<WhileStmt>(S));
389 }
390}
Mike Stump11289f42009-09-09 15:08:12 +0000391
Ted Kremenek93668002009-07-17 22:18:43 +0000392CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
393 if (alwaysAdd) {
394 autoCreateBlock();
395 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek93668002009-07-17 22:18:43 +0000398 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000399}
Mike Stump31feda52009-07-17 01:31:16 +0000400
Ted Kremenek93668002009-07-17 22:18:43 +0000401/// VisitChildren - Visit the children of a Stmt.
402CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
403 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000404 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000405 E = Terminator->child_end(); I != E; ++I) {
406 if (*I) B = Visit(*I);
407 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000408 return B;
409}
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenek93668002009-07-17 22:18:43 +0000411CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
412 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000413
Ted Kremenek93668002009-07-17 22:18:43 +0000414 if (alwaysAdd) {
415 autoCreateBlock();
416 Block->appendStmt(A);
417 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000418
Ted Kremenek9aae5132007-08-23 21:42:29 +0000419 return Block;
420}
Mike Stump11289f42009-09-09 15:08:12 +0000421
Ted Kremenek93668002009-07-17 22:18:43 +0000422CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
423 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000424 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
425 ConfluenceBlock->appendStmt(B);
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenek93668002009-07-17 22:18:43 +0000427 if (!FinishBlock(ConfluenceBlock))
428 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenek93668002009-07-17 22:18:43 +0000430 // create the block evaluating the LHS
431 CFGBlock* LHSBlock = createBlock(false);
432 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000433
Ted Kremenek93668002009-07-17 22:18:43 +0000434 // create the block evaluating the RHS
435 Succ = ConfluenceBlock;
436 Block = NULL;
437 CFGBlock* RHSBlock = addStmt(B->getRHS());
438 if (!FinishBlock(RHSBlock))
439 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Mike Stump773582d2009-07-23 23:25:26 +0000441 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000442 TryResult KnownVal = TryEvaluateBool(B->getLHS());
443 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
444 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000445
Ted Kremenek93668002009-07-17 22:18:43 +0000446 // Now link the LHSBlock with RHSBlock.
447 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek30754282009-07-24 04:47:11 +0000448 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
449 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000450 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000451 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek30754282009-07-24 04:47:11 +0000452 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
453 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Ted Kremenek93668002009-07-17 22:18:43 +0000456 // Generate the blocks for evaluating the LHS.
457 Block = LHSBlock;
458 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000459 }
Ted Kremenek93668002009-07-17 22:18:43 +0000460 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000461 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000462 Block->appendStmt(B);
463 addStmt(B->getRHS());
464 return addStmt(B->getLHS());
465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Ted Kremenek93668002009-07-17 22:18:43 +0000467 return VisitStmt(B, alwaysAdd);
468}
469
Ted Kremenek0747de62009-07-18 00:47:21 +0000470CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000471 // FIXME
472 return NYS();
473}
474
Ted Kremenek0747de62009-07-18 00:47:21 +0000475CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
476 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000477 // FIXME
478 return NYS();
479}
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremenek93668002009-07-17 22:18:43 +0000481CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
482 // "break" is a control-flow statement. Thus we stop processing the current
483 // block.
484 if (Block && !FinishBlock(Block))
485 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek93668002009-07-17 22:18:43 +0000487 // Now create a new block that ends with the break statement.
488 Block = createBlock(false);
489 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenek93668002009-07-17 22:18:43 +0000491 // If there is no target for the break, then we are looking at an incomplete
492 // AST. This means that the CFG cannot be constructed.
493 if (BreakTargetBlock)
494 Block->addSuccessor(BreakTargetBlock);
495 else
496 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000497
498
Ted Kremenek9aae5132007-08-23 21:42:29 +0000499 return Block;
500}
Mike Stump11289f42009-09-09 15:08:12 +0000501
Ted Kremenek93668002009-07-17 22:18:43 +0000502CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
503 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000504 bool NoReturn = false;
505 if (C->getCallee()->getType().getNoReturnAttr()) {
506 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000507 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000508
509 if (FunctionDecl *FD = C->getDirectCallee())
510 if (FD->hasAttr<NoReturnAttr>())
511 NoReturn = true;
512
513 if (!NoReturn)
514 return VisitStmt(C, alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000515
Mike Stump8c5d7992009-07-25 21:26:53 +0000516 if (Block && !FinishBlock(Block))
517 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000518
Mike Stump8c5d7992009-07-25 21:26:53 +0000519 // Create new block with no successor for the remaining pieces.
520 Block = createBlock(false);
521 Block->appendStmt(C);
522
523 // Wire this to the exit block directly.
524 Block->addSuccessor(&cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000525
Mike Stump8c5d7992009-07-25 21:26:53 +0000526 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000527}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000528
Ted Kremenek21822592009-07-17 18:20:32 +0000529CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
530 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
531 ConfluenceBlock->appendStmt(C);
532 if (!FinishBlock(ConfluenceBlock))
533 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Ted Kremenek21822592009-07-17 18:20:32 +0000535 Succ = ConfluenceBlock;
536 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000537 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000538 if (!FinishBlock(LHSBlock))
539 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenek21822592009-07-17 18:20:32 +0000541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000543 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000544 if (!FinishBlock(RHSBlock))
545 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenek21822592009-07-17 18:20:32 +0000547 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000548 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000549 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
550 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
551 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000552 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000553 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000554}
Mike Stump11289f42009-09-09 15:08:12 +0000555
556
557CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
558 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000559
560 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
561 I != E; ++I ) {
562 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000563
Ted Kremenekce499c22009-08-27 23:16:26 +0000564 if (badCFG)
565 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000566 }
Ted Kremenek93668002009-07-17 22:18:43 +0000567 return LastBlock;
568}
Mike Stump11289f42009-09-09 15:08:12 +0000569
Ted Kremenek51d40b02009-07-17 18:15:54 +0000570CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
571 // Create the confluence block that will "merge" the results of the ternary
572 // expression.
573 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
574 ConfluenceBlock->appendStmt(C);
575 if (!FinishBlock(ConfluenceBlock))
576 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek51d40b02009-07-17 18:15:54 +0000578 // Create a block for the LHS expression if there is an LHS expression. A
579 // GCC extension allows LHS to be NULL, causing the condition to be the
580 // value that is returned instead.
581 // e.g: x ?: y is shorthand for: x ? x : y;
582 Succ = ConfluenceBlock;
583 Block = NULL;
584 CFGBlock* LHSBlock = NULL;
585 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000586 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000587 if (!FinishBlock(LHSBlock))
588 return 0;
589 Block = NULL;
590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Ted Kremenek51d40b02009-07-17 18:15:54 +0000592 // Create the block for the RHS expression.
593 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000594 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000595 if (!FinishBlock(RHSBlock))
596 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Ted Kremenek51d40b02009-07-17 18:15:54 +0000598 // Create the block that will contain the condition.
599 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000600
Mike Stump773582d2009-07-23 23:25:26 +0000601 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000602 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000603 if (LHSBlock) {
Ted Kremenek30754282009-07-24 04:47:11 +0000604 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000605 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000606 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000607 // If we know the condition is false, add NULL as the successor for
608 // the block containing the condition. In this case, the confluence
609 // block will have just one predecessor.
610 Block->addSuccessor(0);
Ted Kremenek30754282009-07-24 04:47:11 +0000611 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000612 } else {
613 // If we have no LHS expression, add the ConfluenceBlock as a direct
614 // successor for the block containing the condition. Moreover, we need to
615 // reverse the order of the predecessors in the ConfluenceBlock because
616 // the RHSBlock will have been added to the succcessors already, and we
617 // want the first predecessor to the the block containing the expression
618 // for the case when the ternary expression evaluates to true.
619 Block->addSuccessor(ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000620 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000621 std::reverse(ConfluenceBlock->pred_begin(),
622 ConfluenceBlock->pred_end());
623 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000624 }
Mike Stump11289f42009-09-09 15:08:12 +0000625
626 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000627 Block->setTerminator(C);
628 return addStmt(C->getCond());
629}
630
Ted Kremenek93668002009-07-17 22:18:43 +0000631CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
632 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000633
Ted Kremenek93668002009-07-17 22:18:43 +0000634 if (DS->isSingleDecl()) {
635 Block->appendStmt(DS);
636 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000637 }
Mike Stump11289f42009-09-09 15:08:12 +0000638
Ted Kremenek93668002009-07-17 22:18:43 +0000639 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000640
Ted Kremenek93668002009-07-17 22:18:43 +0000641 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
642 typedef llvm::SmallVector<Decl*,10> BufTy;
643 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000644
Ted Kremenek93668002009-07-17 22:18:43 +0000645 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
646 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
647 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
648 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000649
Ted Kremenek93668002009-07-17 22:18:43 +0000650 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
651 // automatically freed with the CFG.
652 DeclGroupRef DG(*I);
653 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000654 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000655 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenek93668002009-07-17 22:18:43 +0000657 // Append the fake DeclStmt to block.
658 Block->appendStmt(DSNew);
659 B = VisitDeclSubExpr(D);
660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
662 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000663}
Mike Stump11289f42009-09-09 15:08:12 +0000664
Ted Kremenek93668002009-07-17 22:18:43 +0000665/// VisitDeclSubExpr - Utility method to add block-level expressions for
666/// initializers in Decls.
667CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
668 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000669
Ted Kremenek93668002009-07-17 22:18:43 +0000670 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672 if (!VD)
673 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Ted Kremenek93668002009-07-17 22:18:43 +0000675 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenek93668002009-07-17 22:18:43 +0000677 if (Init) {
678 // Optimization: Don't create separate block-level statements for literals.
679 switch (Init->getStmtClass()) {
680 case Stmt::IntegerLiteralClass:
681 case Stmt::CharacterLiteralClass:
682 case Stmt::StringLiteralClass:
683 break;
684 default:
685 Block = addStmt(Init);
686 }
687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688
Ted Kremenek93668002009-07-17 22:18:43 +0000689 // If the type of VD is a VLA, then we must process its size expressions.
690 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
691 VA = FindVA(VA->getElementType().getTypePtr()))
692 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000693
Ted Kremenek93668002009-07-17 22:18:43 +0000694 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000695}
696
697CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000698 // We may see an if statement in the middle of a basic block, or it may be the
699 // first statement we are processing. In either case, we create a new basic
700 // block. First, we create the blocks for the then...else statements, and
701 // then we create the block containing the if statement. If we were in the
702 // middle of a block, we stop processing that block and reverse its
703 // statements. That block is then the implicit successor for the "then" and
704 // "else" clauses.
705
706 // The block we were proccessing is now finished. Make it the successor
707 // block.
708 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000709 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000710 if (!FinishBlock(Block))
711 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000712 }
Mike Stump31feda52009-07-17 01:31:16 +0000713
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000714 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000715 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000716
Ted Kremenek9aae5132007-08-23 21:42:29 +0000717 if (Stmt* Else = I->getElse()) {
718 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000719
Ted Kremenek9aae5132007-08-23 21:42:29 +0000720 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000721 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000722 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000723 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000724
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000725 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
726 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000727 else if (Block) {
728 if (!FinishBlock(ElseBlock))
729 return 0;
730 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000731 }
Mike Stump31feda52009-07-17 01:31:16 +0000732
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000733 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000734 CFGBlock* ThenBlock;
735 {
736 Stmt* Then = I->getThen();
737 assert (Then);
738 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000739 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000740 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000741
Ted Kremenek1b379512009-04-01 03:52:47 +0000742 if (!ThenBlock) {
743 // We can reach here if the "then" body has all NullStmts.
744 // Create an empty block so we can distinguish between true and false
745 // branches in path-sensitive analyses.
746 ThenBlock = createBlock(false);
747 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000748 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000749 if (!FinishBlock(ThenBlock))
750 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000751 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000752 }
753
Mike Stump31feda52009-07-17 01:31:16 +0000754 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000755 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000756
Ted Kremenek9aae5132007-08-23 21:42:29 +0000757 // Set the terminator of the new block to the If statement.
758 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000759
Mike Stump773582d2009-07-23 23:25:26 +0000760 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000761 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000762
Ted Kremenek9aae5132007-08-23 21:42:29 +0000763 // Now add the successors.
Ted Kremenek30754282009-07-24 04:47:11 +0000764 Block->addSuccessor(KnownVal.isFalse() ? NULL : ThenBlock);
765 Block->addSuccessor(KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000766
767 // Add the condition as the last statement in the new block. This may create
768 // new blocks as the condition may contain control-flow. Any newly created
769 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000770 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000771}
Mike Stump31feda52009-07-17 01:31:16 +0000772
773
Ted Kremenek9aae5132007-08-23 21:42:29 +0000774CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000775 // If we were in the middle of a block we stop processing that block and
776 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000777 //
Mike Stump31feda52009-07-17 01:31:16 +0000778 // NOTE: If a "return" appears in the middle of a block, this means that the
779 // code afterwards is DEAD (unreachable). We still keep a basic block
780 // for that code; a simple "mark-and-sweep" from the entry block will be
781 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000782 if (Block) FinishBlock(Block);
783
784 // Create the new block.
785 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000786
Ted Kremenek9aae5132007-08-23 21:42:29 +0000787 // The Exit block is the only successor.
788 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000789
790 // Add the return statement to the block. This may create new blocks if R
791 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000792 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000793}
794
795CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
796 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000797 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000798 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000799
Ted Kremenek93668002009-07-17 22:18:43 +0000800 if (!LabelBlock) // This can happen when the body is empty, i.e.
801 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000802
Ted Kremenek93668002009-07-17 22:18:43 +0000803 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000804 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000805
806 // Labels partition blocks, so this is the end of the basic block we were
807 // processing (L is the block's label). Because this is label (and we have
808 // already processed the substatement) there is no extra control-flow to worry
809 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000810 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000811 if (!FinishBlock(LabelBlock))
812 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000813
814 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000815 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000816
Ted Kremenek9aae5132007-08-23 21:42:29 +0000817 // This block is now the implicit successor of other blocks.
818 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000819
Ted Kremenek9aae5132007-08-23 21:42:29 +0000820 return LabelBlock;
821}
822
823CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000824 // Goto is a control-flow statement. Thus we stop processing the current
825 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000826 if (Block)
827 FinishBlock(Block);
828
Ted Kremenek9aae5132007-08-23 21:42:29 +0000829 Block = createBlock(false);
830 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000831
832 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000833 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000834
Ted Kremenek9aae5132007-08-23 21:42:29 +0000835 if (I == LabelMap.end())
836 // We will need to backpatch this block later.
837 BackpatchBlocks.push_back(Block);
838 else
839 Block->addSuccessor(I->second);
840
Mike Stump31feda52009-07-17 01:31:16 +0000841 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000842}
843
844CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000845 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000846
Mike Stump014b3ea2009-07-21 01:12:51 +0000847 // "for" is a control-flow statement. Thus we stop processing the current
848 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000849 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000850 if (!FinishBlock(Block))
851 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000852 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000853 } else
854 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000855
856 // Because of short-circuit evaluation, the condition of the loop can span
857 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
858 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000859 CFGBlock* ExitConditionBlock = createBlock(false);
860 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000861
Ted Kremenek81e14852007-08-27 19:46:09 +0000862 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000863 ExitConditionBlock->setTerminator(F);
864
865 // Now add the actual condition to the condition block. Because the condition
866 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000867 if (Stmt* C = F->getCond()) {
868 Block = ExitConditionBlock;
869 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000870 if (Block) {
871 if (!FinishBlock(EntryConditionBlock))
872 return 0;
873 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000874 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000875
Mike Stump31feda52009-07-17 01:31:16 +0000876 // The condition block is the implicit successor for the loop body as well as
877 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000878 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000879
Mike Stump773582d2009-07-23 23:25:26 +0000880 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000881 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000882
Mike Stump773582d2009-07-23 23:25:26 +0000883 if (F->getCond())
884 KnownVal = TryEvaluateBool(F->getCond());
885
Ted Kremenek9aae5132007-08-23 21:42:29 +0000886 // Now create the loop body.
887 {
888 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000889
Ted Kremenek9aae5132007-08-23 21:42:29 +0000890 // Save the current values for Block, Succ, and continue and break targets
891 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
892 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000893 save_break(BreakTargetBlock);
894
Ted Kremeneke9610502007-08-30 18:39:40 +0000895 // Create a new block to contain the (bottom) of the loop body.
896 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000897
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000898 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000899 // Generate increment code in its own basic block. This is the target of
900 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000901 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000902 } else {
903 // No increment code. Create a special, empty, block that is used as the
904 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000905 assert(Succ == EntryConditionBlock);
906 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000907 }
Mike Stump31feda52009-07-17 01:31:16 +0000908
Ted Kremenek902393b2009-04-28 00:51:56 +0000909 // Finish up the increment (or empty) block if it hasn't been already.
910 if (Block) {
911 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000912 if (!FinishBlock(Block))
913 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000914 Block = 0;
915 }
Mike Stump31feda52009-07-17 01:31:16 +0000916
Ted Kremenek902393b2009-04-28 00:51:56 +0000917 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000918
Ted Kremenek902393b2009-04-28 00:51:56 +0000919 // The starting block for the loop increment is the block that should
920 // represent the 'loop target' for looping back to the start of the loop.
921 ContinueTargetBlock->setLoopTarget(F);
922
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000923 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000924 BreakTargetBlock = LoopSuccessor;
925
926 // Now populate the body block, and in the process create new blocks as we
927 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000928 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000929
930 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000931 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000932 else if (Block && !FinishBlock(BodyBlock))
933 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000934
Ted Kremenek30754282009-07-24 04:47:11 +0000935 // This new body block is a successor to our "exit" condition block.
936 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000937 }
Mike Stump31feda52009-07-17 01:31:16 +0000938
Ted Kremenek30754282009-07-24 04:47:11 +0000939 // Link up the condition block with the code that follows the loop. (the
940 // false branch).
941 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000942
Ted Kremenek9aae5132007-08-23 21:42:29 +0000943 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000944 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000945 if (Stmt* I = F->getInit()) {
946 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000947 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000948 } else {
949 // There is no loop initialization. We are thus basically a while loop.
950 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000951 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000952 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000953 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000954 }
955}
956
Ted Kremenek9d56e642008-11-11 17:10:00 +0000957CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
958 // Objective-C fast enumeration 'for' statements:
959 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
960 //
961 // for ( Type newVariable in collection_expression ) { statements }
962 //
963 // becomes:
964 //
965 // prologue:
966 // 1. collection_expression
967 // T. jump to loop_entry
968 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000969 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000970 // 1. ObjCForCollectionStmt [performs binding to newVariable]
971 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
972 // TB:
973 // statements
974 // T. jump to loop_entry
975 // FB:
976 // what comes after
977 //
978 // and
979 //
980 // Type existingItem;
981 // for ( existingItem in expression ) { statements }
982 //
983 // becomes:
984 //
Mike Stump31feda52009-07-17 01:31:16 +0000985 // the same with newVariable replaced with existingItem; the binding works
986 // the same except that for one ObjCForCollectionStmt::getElement() returns
987 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000988 //
Mike Stump31feda52009-07-17 01:31:16 +0000989
Ted Kremenek9d56e642008-11-11 17:10:00 +0000990 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000991
Ted Kremenek9d56e642008-11-11 17:10:00 +0000992 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000993 if (!FinishBlock(Block))
994 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000995 LoopSuccessor = Block;
996 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000997 } else
998 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000999
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001000 // Build the condition blocks.
1001 CFGBlock* ExitConditionBlock = createBlock(false);
1002 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001003
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001004 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001005 ExitConditionBlock->setTerminator(S);
1006
1007 // The last statement in the block should be the ObjCForCollectionStmt, which
1008 // performs the actual binding to 'element' and determines if there are any
1009 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001010 ExitConditionBlock->appendStmt(S);
1011 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001012
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001013 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001014 // generate new blocks as necesary. We DON'T add the statement by default to
1015 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001016 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001017 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001018 if (!FinishBlock(EntryConditionBlock))
1019 return 0;
1020 Block = 0;
1021 }
Mike Stump31feda52009-07-17 01:31:16 +00001022
1023 // The condition block is the implicit successor for the loop body as well as
1024 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001025 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001026
Ted Kremenek9d56e642008-11-11 17:10:00 +00001027 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001028 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001029 // Save the current values for Succ, continue and break targets.
1030 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001031 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1032
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001033 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001034 ContinueTargetBlock = EntryConditionBlock;
1035
Ted Kremenek93668002009-07-17 22:18:43 +00001036 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001037
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001038 if (!BodyBlock)
1039 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001040 else if (Block) {
1041 if (!FinishBlock(BodyBlock))
1042 return 0;
1043 }
Mike Stump31feda52009-07-17 01:31:16 +00001044
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001045 // This new body block is a successor to our "exit" condition block.
1046 ExitConditionBlock->addSuccessor(BodyBlock);
1047 }
Mike Stump31feda52009-07-17 01:31:16 +00001048
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001049 // Link up the condition block with the code that follows the loop.
1050 // (the false branch).
1051 ExitConditionBlock->addSuccessor(LoopSuccessor);
1052
Ted Kremenek9d56e642008-11-11 17:10:00 +00001053 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001054 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001055 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001056}
1057
Ted Kremenek49805452009-05-02 01:49:13 +00001058CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1059 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001060
Ted Kremenek49805452009-05-02 01:49:13 +00001061 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001062 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001063
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001064 // The sync body starts its own basic block. This makes it a little easier
1065 // for diagnostic clients.
1066 if (SyncBlock) {
1067 if (!FinishBlock(SyncBlock))
1068 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001069
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001070 Block = 0;
1071 }
Mike Stump31feda52009-07-17 01:31:16 +00001072
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001073 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001074
Ted Kremenek49805452009-05-02 01:49:13 +00001075 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001076 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001077}
Mike Stump31feda52009-07-17 01:31:16 +00001078
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001079CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001080 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001081 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001082}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001083
Ted Kremenek9aae5132007-08-23 21:42:29 +00001084CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001085 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001086
Mike Stump014b3ea2009-07-21 01:12:51 +00001087 // "while" is a control-flow statement. Thus we stop processing the current
1088 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001089 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001090 if (!FinishBlock(Block))
1091 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001092 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001093 } else
1094 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001095
1096 // Because of short-circuit evaluation, the condition of the loop can span
1097 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1098 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001099 CFGBlock* ExitConditionBlock = createBlock(false);
1100 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001101
Ted Kremenek81e14852007-08-27 19:46:09 +00001102 // Set the terminator for the "exit" condition block.
1103 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001104
1105 // Now add the actual condition to the condition block. Because the condition
1106 // itself may contain control-flow, new blocks may be created. Thus we update
1107 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001108 if (Stmt* C = W->getCond()) {
1109 Block = ExitConditionBlock;
1110 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001111 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001112 if (Block) {
1113 if (!FinishBlock(EntryConditionBlock))
1114 return 0;
1115 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001116 }
Mike Stump31feda52009-07-17 01:31:16 +00001117
1118 // The condition block is the implicit successor for the loop body as well as
1119 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001120 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001121
Mike Stump773582d2009-07-23 23:25:26 +00001122 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001123 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001124
Ted Kremenek9aae5132007-08-23 21:42:29 +00001125 // Process the loop body.
1126 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001127 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001128
1129 // Save the current values for Block, Succ, and continue and break targets
1130 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1131 save_continue(ContinueTargetBlock),
1132 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001133
Mike Stump31feda52009-07-17 01:31:16 +00001134 // Create an empty block to represent the transition block for looping back
1135 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001136 Block = 0;
1137 assert(Succ == EntryConditionBlock);
1138 Succ = createBlock();
1139 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001140 ContinueTargetBlock = Succ;
1141
Ted Kremenek9aae5132007-08-23 21:42:29 +00001142 // All breaks should go to the code following the loop.
1143 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001144
Ted Kremenek9aae5132007-08-23 21:42:29 +00001145 // NULL out Block to force lazy instantiation of blocks for the body.
1146 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001147
Ted Kremenek9aae5132007-08-23 21:42:29 +00001148 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001149 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001150
Ted Kremeneke9610502007-08-30 18:39:40 +00001151 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001152 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001153 else if (Block) {
1154 if (!FinishBlock(BodyBlock))
1155 return 0;
1156 }
Mike Stump31feda52009-07-17 01:31:16 +00001157
Ted Kremenek30754282009-07-24 04:47:11 +00001158 // Add the loop body entry as a successor to the condition.
1159 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001160 }
Mike Stump31feda52009-07-17 01:31:16 +00001161
Ted Kremenek30754282009-07-24 04:47:11 +00001162 // Link up the condition block with the code that follows the loop. (the
1163 // false branch).
1164 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001165
1166 // There can be no more statements in the condition block since we loop back
1167 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001168 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001169
Ted Kremenek9aae5132007-08-23 21:42:29 +00001170 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001171 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001172 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001173}
Mike Stump11289f42009-09-09 15:08:12 +00001174
1175
Ted Kremenek93668002009-07-17 22:18:43 +00001176CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1177 // FIXME: For now we pretend that @catch and the code it contains does not
1178 // exit.
1179 return Block;
1180}
Mike Stump31feda52009-07-17 01:31:16 +00001181
Ted Kremenek93041ba2008-12-09 20:20:09 +00001182CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1183 // FIXME: This isn't complete. We basically treat @throw like a return
1184 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001185
1186 // If we were in the middle of a block we stop processing that block and
1187 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001188 if (Block && !FinishBlock(Block))
1189 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001190
Ted Kremenek93041ba2008-12-09 20:20:09 +00001191 // Create the new block.
1192 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001193
Ted Kremenek93041ba2008-12-09 20:20:09 +00001194 // The Exit block is the only successor.
1195 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001196
1197 // Add the statement to the block. This may create new blocks if S contains
1198 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001199 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001200}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001201
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001202CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1203 // If we were in the middle of a block we stop processing that block and
1204 // reverse its statements.
1205 if (Block && !FinishBlock(Block))
1206 return 0;
1207
1208 // Create the new block.
1209 Block = createBlock(false);
1210
1211 // The Exit block is the only successor.
1212 Block->addSuccessor(&cfg->getExit());
1213
1214 // Add the statement to the block. This may create new blocks if S contains
1215 // control-flow (short-circuit operations).
1216 return VisitStmt(T, true);
1217}
1218
Ted Kremenek93668002009-07-17 22:18:43 +00001219CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001220 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001221
Mike Stump8d50b6a2009-07-21 01:27:50 +00001222 // "do...while" is a control-flow statement. Thus we stop processing the
1223 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001224 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001225 if (!FinishBlock(Block))
1226 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001227 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001228 } else
1229 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001230
1231 // Because of short-circuit evaluation, the condition of the loop can span
1232 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1233 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001234 CFGBlock* ExitConditionBlock = createBlock(false);
1235 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001236
Ted Kremenek81e14852007-08-27 19:46:09 +00001237 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001238 ExitConditionBlock->setTerminator(D);
1239
1240 // Now add the actual condition to the condition block. Because the condition
1241 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001242 if (Stmt* C = D->getCond()) {
1243 Block = ExitConditionBlock;
1244 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001245 if (Block) {
1246 if (!FinishBlock(EntryConditionBlock))
1247 return 0;
1248 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001249 }
Mike Stump31feda52009-07-17 01:31:16 +00001250
Ted Kremeneka1523a32008-02-27 07:20:00 +00001251 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001252 Succ = EntryConditionBlock;
1253
Mike Stump773582d2009-07-23 23:25:26 +00001254 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001255 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001256
Ted Kremenek9aae5132007-08-23 21:42:29 +00001257 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001258 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001259 {
1260 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001261
Ted Kremenek9aae5132007-08-23 21:42:29 +00001262 // Save the current values for Block, Succ, and continue and break targets
1263 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1264 save_continue(ContinueTargetBlock),
1265 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001266
Ted Kremenek9aae5132007-08-23 21:42:29 +00001267 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001268 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001269
Ted Kremenek9aae5132007-08-23 21:42:29 +00001270 // All breaks should go to the code following the loop.
1271 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001272
Ted Kremenek9aae5132007-08-23 21:42:29 +00001273 // NULL out Block to force lazy instantiation of blocks for the body.
1274 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001275
Ted Kremenek9aae5132007-08-23 21:42:29 +00001276 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001277 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001278
Ted Kremeneke9610502007-08-30 18:39:40 +00001279 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001280 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001281 else if (Block) {
1282 if (!FinishBlock(BodyBlock))
1283 return 0;
1284 }
Mike Stump31feda52009-07-17 01:31:16 +00001285
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001286 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001287 // ExitConditionBlock to represent the "loop back" transition. Create an
1288 // empty block to represent the transition block for looping back to the
1289 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001290 // FIXME: Can we do this more efficiently without adding another block?
1291 Block = NULL;
1292 Succ = BodyBlock;
1293 CFGBlock *LoopBackBlock = createBlock();
1294 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001295
Ted Kremenek30754282009-07-24 04:47:11 +00001296 // Add the loop body entry as a successor to the condition.
1297 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001298 }
Mike Stump31feda52009-07-17 01:31:16 +00001299
Ted Kremenek30754282009-07-24 04:47:11 +00001300 // Link up the condition block with the code that follows the loop.
1301 // (the false branch).
1302 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001303
1304 // There can be no more statements in the body block(s) since we loop back to
1305 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001306 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001307
Ted Kremenek9aae5132007-08-23 21:42:29 +00001308 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001309 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001310 return BodyBlock;
1311}
1312
1313CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1314 // "continue" is a control-flow statement. Thus we stop processing the
1315 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001316 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001317 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001318
Ted Kremenek9aae5132007-08-23 21:42:29 +00001319 // Now create a new block that ends with the continue statement.
1320 Block = createBlock(false);
1321 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001322
Ted Kremenek9aae5132007-08-23 21:42:29 +00001323 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001324 // incomplete AST. This means the CFG cannot be constructed.
1325 if (ContinueTargetBlock)
1326 Block->addSuccessor(ContinueTargetBlock);
1327 else
1328 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001329
Ted Kremenek9aae5132007-08-23 21:42:29 +00001330 return Block;
1331}
Mike Stump11289f42009-09-09 15:08:12 +00001332
Ted Kremenek0747de62009-07-18 00:47:21 +00001333CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1334 bool alwaysAdd) {
1335
1336 if (alwaysAdd) {
1337 autoCreateBlock();
1338 Block->appendStmt(E);
1339 }
Mike Stump11289f42009-09-09 15:08:12 +00001340
Ted Kremenek93668002009-07-17 22:18:43 +00001341 // VLA types have expressions that must be evaluated.
1342 if (E->isArgumentType()) {
1343 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1344 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1345 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001346 }
Mike Stump11289f42009-09-09 15:08:12 +00001347
Mike Stump31feda52009-07-17 01:31:16 +00001348 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001349}
Mike Stump11289f42009-09-09 15:08:12 +00001350
Ted Kremenek93668002009-07-17 22:18:43 +00001351/// VisitStmtExpr - Utility method to handle (nested) statement
1352/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001353CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1354 if (alwaysAdd) {
1355 autoCreateBlock();
1356 Block->appendStmt(SE);
1357 }
Ted Kremenek93668002009-07-17 22:18:43 +00001358 return VisitCompoundStmt(SE->getSubStmt());
1359}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001360
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001361CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001362 // "switch" is a control-flow statement. Thus we stop processing the current
1363 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001364 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001365
Ted Kremenek9aae5132007-08-23 21:42:29 +00001366 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001367 if (!FinishBlock(Block))
1368 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001369 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001370 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001371
1372 // Save the current "switch" context.
1373 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001374 save_break(BreakTargetBlock),
1375 save_default(DefaultCaseBlock);
1376
Mike Stump31feda52009-07-17 01:31:16 +00001377 // Set the "default" case to be the block after the switch statement. If the
1378 // switch statement contains a "default:", this value will be overwritten with
1379 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001380 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001381
Ted Kremenek9aae5132007-08-23 21:42:29 +00001382 // Create a new block that will contain the switch statement.
1383 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001384
Ted Kremenek9aae5132007-08-23 21:42:29 +00001385 // Now process the switch body. The code after the switch is the implicit
1386 // successor.
1387 Succ = SwitchSuccessor;
1388 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001389
1390 // When visiting the body, the case statements should automatically get linked
1391 // up to the switch. We also don't keep a pointer to the body, since all
1392 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001393 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001394 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001395 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001396 if (Block) {
1397 if (!FinishBlock(BodyBlock))
1398 return 0;
1399 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001400
Mike Stump31feda52009-07-17 01:31:16 +00001401 // If we have no "default:" case, the default transition is to the code
1402 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001403 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001404
Ted Kremenek81e14852007-08-27 19:46:09 +00001405 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001406 SwitchTerminatedBlock->setTerminator(Terminator);
1407 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001408 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001409
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001410 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001411}
1412
Ted Kremenek93668002009-07-17 22:18:43 +00001413CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001414 // CaseStmts are essentially labels, so they are the first statement in a
1415 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001416
Ted Kremenek93668002009-07-17 22:18:43 +00001417 if (CS->getSubStmt())
1418 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001419
Ted Kremenek55e91e82007-08-30 18:48:11 +00001420 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001421 if (!CaseBlock)
1422 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001423
1424 // Cases statements partition blocks, so this is the top of the basic block we
1425 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001426 CaseBlock->setLabel(CS);
1427
Ted Kremenek55957a82009-05-02 00:13:27 +00001428 if (!FinishBlock(CaseBlock))
1429 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001430
1431 // Add this block to the list of successors for the block with the switch
1432 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001433 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001434 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001435
Ted Kremenek9aae5132007-08-23 21:42:29 +00001436 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1437 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001438
Ted Kremenek9aae5132007-08-23 21:42:29 +00001439 // This block is now the implicit successor of other blocks.
1440 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001441
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001442 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001443}
Mike Stump31feda52009-07-17 01:31:16 +00001444
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001445CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001446 if (Terminator->getSubStmt())
1447 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001448
Ted Kremenek654c78f2008-02-13 22:05:39 +00001449 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001450
1451 if (!DefaultCaseBlock)
1452 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001453
1454 // Default statements partition blocks, so this is the top of the basic block
1455 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001456 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001457
Ted Kremenek55957a82009-05-02 00:13:27 +00001458 if (!FinishBlock(DefaultCaseBlock))
1459 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001460
Mike Stump31feda52009-07-17 01:31:16 +00001461 // Unlike case statements, we don't add the default block to the successors
1462 // for the switch statement immediately. This is done when we finish
1463 // processing the switch statement. This allows for the default case
1464 // (including a fall-through to the code after the switch statement) to always
1465 // be the last successor of a switch-terminated block.
1466
Ted Kremenek654c78f2008-02-13 22:05:39 +00001467 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1468 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001469
Ted Kremenek654c78f2008-02-13 22:05:39 +00001470 // This block is now the implicit successor of other blocks.
1471 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001472
1473 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001474}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001475
Ted Kremenekeda180e22007-08-28 19:26:49 +00001476CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001477 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001478 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001479
Ted Kremenekeda180e22007-08-28 19:26:49 +00001480 if (!IBlock) {
1481 IBlock = createBlock(false);
1482 cfg->setIndirectGotoBlock(IBlock);
1483 }
Mike Stump31feda52009-07-17 01:31:16 +00001484
Ted Kremenekeda180e22007-08-28 19:26:49 +00001485 // IndirectGoto is a control-flow statement. Thus we stop processing the
1486 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001487 if (Block && !FinishBlock(Block))
1488 return 0;
1489
Ted Kremenekeda180e22007-08-28 19:26:49 +00001490 Block = createBlock(false);
1491 Block->setTerminator(I);
1492 Block->addSuccessor(IBlock);
1493 return addStmt(I->getTarget());
1494}
1495
Ted Kremenek04cca642007-08-23 21:26:19 +00001496} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001497
Mike Stump31feda52009-07-17 01:31:16 +00001498/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1499/// no successors or predecessors. If this is the first block created in the
1500/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001501CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001502 bool first_block = begin() == end();
1503
1504 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001505 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001506
1507 // If this is the first block, set it as the Entry and Exit.
1508 if (first_block) Entry = Exit = &front();
1509
1510 // Return the block.
1511 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001512}
1513
Ted Kremenek889073f2007-08-23 16:51:22 +00001514/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1515/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001516CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001517 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001518 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001519}
1520
1521/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001522void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1523
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001524//===----------------------------------------------------------------------===//
1525// CFG: Queries for BlkExprs.
1526//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001527
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001528namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001529 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001530}
1531
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001532static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1533 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001534 return;
Mike Stump31feda52009-07-17 01:31:16 +00001535
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001536 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001537 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001538
Ted Kremenek95a123c2008-01-26 00:03:27 +00001539 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1540 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001541
Ted Kremenek95a123c2008-01-26 00:03:27 +00001542 FindSubExprAssignments(*I, Set);
1543 }
1544}
1545
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001546static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1547 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001548
1549 // Look for assignments that are used as subexpressions. These are the only
1550 // assignments that we want to *possibly* register as a block-level
1551 // expression. Basically, if an assignment occurs both in a subexpression and
1552 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001553 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001554
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001555 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1556 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001557 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001558
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001559 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001560
1561 // Iterate over the statements again on identify the Expr* and Stmt* at the
1562 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001563
Ted Kremenek95a123c2008-01-26 00:03:27 +00001564 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001565 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001566
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001567 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001568 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001569 // expression are really "statements" whose value is never used by
1570 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001571 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001572 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001573 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1574 // Special handling for statement expressions. The last statement in
1575 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001576 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001577 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001578 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001579 (*M)[C->body_back()] = x;
1580 }
1581 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001582
Ted Kremenek95a123c2008-01-26 00:03:27 +00001583 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001584 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001585 }
Mike Stump31feda52009-07-17 01:31:16 +00001586
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001587 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001588
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001589 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001590
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001591 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001592 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001593 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001594 }
1595 }
Mike Stump31feda52009-07-17 01:31:16 +00001596
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001597 return M;
1598}
1599
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001600CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1601 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001602 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001603
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001604 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001605 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001606
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001607 if (I == M->end()) return CFG::BlkExprNumTy();
1608 else return CFG::BlkExprNumTy(I->second);
1609}
1610
1611unsigned CFG::getNumBlkExprs() {
1612 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1613 return M->size();
1614 else {
1615 // We assume callers interested in the number of BlkExprs will want
1616 // the map constructed if it doesn't already exist.
1617 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1618 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1619 }
1620}
1621
Ted Kremenek6065ef62008-04-28 18:00:46 +00001622//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001623// Cleanup: CFG dstor.
1624//===----------------------------------------------------------------------===//
1625
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001626CFG::~CFG() {
1627 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1628}
Mike Stump31feda52009-07-17 01:31:16 +00001629
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001630//===----------------------------------------------------------------------===//
1631// CFG pretty printing
1632//===----------------------------------------------------------------------===//
1633
Ted Kremenek7e776b12007-08-22 18:22:34 +00001634namespace {
1635
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001636class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001637
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001638 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1639 StmtMapTy StmtMap;
1640 signed CurrentBlock;
1641 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001642 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001643public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001644
Chris Lattnerc61089a2009-06-30 01:26:17 +00001645 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1646 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001647 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1648 unsigned j = 1;
1649 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1650 BI != BEnd; ++BI, ++j )
1651 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1652 }
1653 }
Mike Stump31feda52009-07-17 01:31:16 +00001654
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001655 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001656
Chris Lattnerc61089a2009-06-30 01:26:17 +00001657 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001658 void setBlockID(signed i) { CurrentBlock = i; }
1659 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001660
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001661 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001663 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001664
1665 if (I == StmtMap.end())
1666 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001667
1668 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001669 && I->second.second == CurrentStmt)
1670 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001671
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001672 OS << "[B" << I->second.first << "." << I->second.second << "]";
1673 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001674 }
1675};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001676} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001677
Chris Lattnerc61089a2009-06-30 01:26:17 +00001678
1679namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001680class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1681 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001682
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001683 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001684 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001685 PrintingPolicy Policy;
1686
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001687public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001688 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001689 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001690 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001691
Ted Kremenek9aae5132007-08-23 21:42:29 +00001692 void VisitIfStmt(IfStmt* I) {
1693 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001694 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001695 }
Mike Stump31feda52009-07-17 01:31:16 +00001696
Ted Kremenek9aae5132007-08-23 21:42:29 +00001697 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001698 void VisitStmt(Stmt* Terminator) {
1699 Terminator->printPretty(OS, Helper, Policy);
1700 }
1701
Ted Kremenek9aae5132007-08-23 21:42:29 +00001702 void VisitForStmt(ForStmt* F) {
1703 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001704 if (F->getInit()) OS << "...";
1705 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001706 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001707 OS << "; ";
1708 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001709 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001710 }
Mike Stump31feda52009-07-17 01:31:16 +00001711
Ted Kremenek9aae5132007-08-23 21:42:29 +00001712 void VisitWhileStmt(WhileStmt* W) {
1713 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001714 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001715 }
Mike Stump31feda52009-07-17 01:31:16 +00001716
Ted Kremenek9aae5132007-08-23 21:42:29 +00001717 void VisitDoStmt(DoStmt* D) {
1718 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001719 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001720 }
Mike Stump31feda52009-07-17 01:31:16 +00001721
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001722 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001723 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001724 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001725 }
Mike Stump31feda52009-07-17 01:31:16 +00001726
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001727 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001728 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001729 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001730 }
Mike Stump31feda52009-07-17 01:31:16 +00001731
Ted Kremenek391f94a2007-08-31 22:29:13 +00001732 void VisitChooseExpr(ChooseExpr* C) {
1733 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001734 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001735 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001736 }
Mike Stump31feda52009-07-17 01:31:16 +00001737
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001738 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1739 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001740 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001741 }
Mike Stump31feda52009-07-17 01:31:16 +00001742
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001743 void VisitBinaryOperator(BinaryOperator* B) {
1744 if (!B->isLogicalOp()) {
1745 VisitExpr(B);
1746 return;
1747 }
Mike Stump31feda52009-07-17 01:31:16 +00001748
Douglas Gregor7de59662009-05-29 20:38:28 +00001749 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001750
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001751 switch (B->getOpcode()) {
1752 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001753 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001754 return;
1755 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001756 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001757 return;
1758 default:
1759 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001760 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001761 }
Mike Stump31feda52009-07-17 01:31:16 +00001762
Ted Kremenek12687ff2007-08-27 21:54:41 +00001763 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001764 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001765 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001766};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001767} // end anonymous namespace
1768
Mike Stump31feda52009-07-17 01:31:16 +00001769
Chris Lattnerc61089a2009-06-30 01:26:17 +00001770static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1771 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001772 if (Helper) {
1773 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001774 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001775 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001776
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001777 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001778 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001779 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001780 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001781 return;
1782 }
1783 }
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001785 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001786 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001787 if (B->getOpcode() == BinaryOperator::Comma) {
1788 OS << "... , ";
1789 Helper->handledStmt(B->getRHS(),OS);
1790 OS << '\n';
1791 return;
Mike Stump31feda52009-07-17 01:31:16 +00001792 }
1793 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001794 }
Mike Stump31feda52009-07-17 01:31:16 +00001795
Chris Lattnerc61089a2009-06-30 01:26:17 +00001796 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001798 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001799 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001800}
Mike Stump31feda52009-07-17 01:31:16 +00001801
Chris Lattnerc61089a2009-06-30 01:26:17 +00001802static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1803 const CFGBlock& B,
1804 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001805
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001806 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001808 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001809 OS << "\n [ B" << B.getBlockID();
1810
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001811 if (&B == &cfg->getEntry())
1812 OS << " (ENTRY) ]\n";
1813 else if (&B == &cfg->getExit())
1814 OS << " (EXIT) ]\n";
1815 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001816 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001817 else
1818 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001819
Ted Kremenek71eca012007-08-29 23:20:49 +00001820 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001821 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001822
1823 if (print_edges)
1824 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001825
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001826 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001827 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001828 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001829 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001830 C->getLHS()->printPretty(OS, Helper,
1831 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001832 if (C->getRHS()) {
1833 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001834 C->getRHS()->printPretty(OS, Helper,
1835 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001836 }
Mike Stump31feda52009-07-17 01:31:16 +00001837 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001838 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001839 else
1840 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001841
Ted Kremenek71eca012007-08-29 23:20:49 +00001842 OS << ":\n";
1843 }
Mike Stump31feda52009-07-17 01:31:16 +00001844
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001845 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001846 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001847
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001848 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1849 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenek71eca012007-08-29 23:20:49 +00001851 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001852 if (print_edges)
1853 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001854
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001855 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001856
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001857 if (Helper)
1858 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001859
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001860 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001861 }
Mike Stump31feda52009-07-17 01:31:16 +00001862
Ted Kremenek71eca012007-08-29 23:20:49 +00001863 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001864 if (B.getTerminator()) {
1865 if (print_edges)
1866 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001867
Ted Kremenek71eca012007-08-29 23:20:49 +00001868 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001869
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001870 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001871
Chris Lattnerc61089a2009-06-30 01:26:17 +00001872 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1873 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001874 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001875 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001876 }
Mike Stump31feda52009-07-17 01:31:16 +00001877
Ted Kremenek71eca012007-08-29 23:20:49 +00001878 if (print_edges) {
1879 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001880 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001881 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001882
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001883 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1884 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001885
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001886 if (i == 8 || (i-8) == 0)
1887 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001888
Ted Kremenek71eca012007-08-29 23:20:49 +00001889 OS << " B" << (*I)->getBlockID();
1890 }
Mike Stump31feda52009-07-17 01:31:16 +00001891
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001892 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001893
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001894 // Print the successors of this block.
1895 OS << " Successors (" << B.succ_size() << "):";
1896 i = 0;
1897
1898 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1899 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001900
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001901 if (i == 8 || (i-8) % 10 == 0)
1902 OS << "\n ";
1903
Mike Stump0d76d072009-07-20 23:24:15 +00001904 if (*I)
1905 OS << " B" << (*I)->getBlockID();
1906 else
1907 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001908 }
Mike Stump31feda52009-07-17 01:31:16 +00001909
Ted Kremenek71eca012007-08-29 23:20:49 +00001910 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001911 }
Mike Stump31feda52009-07-17 01:31:16 +00001912}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001914
1915/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001916void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001917
1918/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001919void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1920 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001921
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001922 // Print the entry block.
1923 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001925 // Iterate through the CFGBlocks and print them one by one.
1926 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1927 // Skip the entry block, because we already printed it.
1928 if (&(*I) == &getEntry() || &(*I) == &getExit())
1929 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001930
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001931 print_block(OS, this, *I, &Helper, true);
1932 }
Mike Stump31feda52009-07-17 01:31:16 +00001933
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001934 // Print the exit block.
1935 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001936 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001937}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001938
1939/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001940void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1941 print(llvm::errs(), cfg, LO);
1942}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001943
1944/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1945/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001946void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1947 const LangOptions &LO) const {
1948 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001949 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001950}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001951
Ted Kremenek15647632008-01-30 23:02:42 +00001952/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001953void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001954 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001955 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001956 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1957}
1958
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001959Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001960
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001961 if (!Terminator)
1962 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001964 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001965
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001966 switch (Terminator->getStmtClass()) {
1967 default:
1968 break;
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001970 case Stmt::ForStmtClass:
1971 E = cast<ForStmt>(Terminator)->getCond();
1972 break;
Mike Stump31feda52009-07-17 01:31:16 +00001973
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001974 case Stmt::WhileStmtClass:
1975 E = cast<WhileStmt>(Terminator)->getCond();
1976 break;
Mike Stump31feda52009-07-17 01:31:16 +00001977
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001978 case Stmt::DoStmtClass:
1979 E = cast<DoStmt>(Terminator)->getCond();
1980 break;
Mike Stump31feda52009-07-17 01:31:16 +00001981
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001982 case Stmt::IfStmtClass:
1983 E = cast<IfStmt>(Terminator)->getCond();
1984 break;
Mike Stump31feda52009-07-17 01:31:16 +00001985
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001986 case Stmt::ChooseExprClass:
1987 E = cast<ChooseExpr>(Terminator)->getCond();
1988 break;
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001990 case Stmt::IndirectGotoStmtClass:
1991 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1992 break;
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001994 case Stmt::SwitchStmtClass:
1995 E = cast<SwitchStmt>(Terminator)->getCond();
1996 break;
Mike Stump31feda52009-07-17 01:31:16 +00001997
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001998 case Stmt::ConditionalOperatorClass:
1999 E = cast<ConditionalOperator>(Terminator)->getCond();
2000 break;
Mike Stump31feda52009-07-17 01:31:16 +00002001
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002002 case Stmt::BinaryOperatorClass: // '&&' and '||'
2003 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002004 break;
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002006 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002007 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002008 }
Mike Stump31feda52009-07-17 01:31:16 +00002009
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002010 return E ? E->IgnoreParens() : NULL;
2011}
2012
Ted Kremenek92137a32008-05-16 16:06:00 +00002013bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002014
Ted Kremenek92137a32008-05-16 16:06:00 +00002015 if (!Terminator)
2016 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002017
Ted Kremenek92137a32008-05-16 16:06:00 +00002018 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002019
Ted Kremenek92137a32008-05-16 16:06:00 +00002020 switch (Terminator->getStmtClass()) {
2021 default:
2022 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002023
2024 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002025 case Stmt::WhileStmtClass:
2026 case Stmt::DoStmtClass:
2027 case Stmt::IfStmtClass:
2028 case Stmt::ChooseExprClass:
2029 case Stmt::ConditionalOperatorClass:
2030 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002031 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002032 }
Mike Stump31feda52009-07-17 01:31:16 +00002033
Ted Kremenek92137a32008-05-16 16:06:00 +00002034 return E ? E->IgnoreParens() : NULL;
2035}
2036
Ted Kremenek15647632008-01-30 23:02:42 +00002037
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002038//===----------------------------------------------------------------------===//
2039// CFG Graphviz Visualization
2040//===----------------------------------------------------------------------===//
2041
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002042
2043#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002044static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002045#endif
2046
Chris Lattnerc61089a2009-06-30 01:26:17 +00002047void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002048#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002049 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002050 GraphHelper = &H;
2051 llvm::ViewGraph(this,"CFG");
2052 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002053#endif
2054}
2055
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002056namespace llvm {
2057template<>
2058struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002059 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2060 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002061
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002062#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002063 std::string OutSStr;
2064 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002065 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002066 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002067
2068 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2069
2070 // Process string output to make it nicer...
2071 for (unsigned i = 0; i != OutStr.length(); ++i)
2072 if (OutStr[i] == '\n') { // Left justify
2073 OutStr[i] = '\\';
2074 OutStr.insert(OutStr.begin()+i+1, 'l');
2075 }
Mike Stump31feda52009-07-17 01:31:16 +00002076
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002077 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002078#else
2079 return "";
2080#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002081 }
2082};
2083} // end namespace llvm