blob: a4e24f8b9f9116874f040ee5ee92e856211ca3c5 [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"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#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 Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramera3b13412010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000047
Benjamin Kramera3b13412010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek66de6032010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000050
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051private:
Benjamin Kramera3b13412010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000053};
Mike Stump31feda52009-07-17 01:31:16 +000054
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000055/// LocalScope - Node in tree of local scopes created for C++ implicit
56/// destructor calls generation. It contains list of automatic variables
57/// declared in the scope and link to position in previous scope this scope
58/// began in.
59///
60/// The process of creating local scopes is as follows:
61/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
62/// - Before processing statements in scope (e.g. CompoundStmt) create
63/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
64/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000065/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000066/// at this VarDecl,
67/// - For every normal (without jump) end of scope add to CFGBlock destructors
68/// for objects in the current scope,
69/// - For every jump add to CFGBlock destructors for objects
70/// between CFGBuilder::ScopePos and local scope position saved for jump
71/// target. Thanks to C++ restrictions on goto jumps we can be sure that
72/// jump target position will be on the path to root from CFGBuilder::ScopePos
73/// (adding any variable that doesn't need constructor to be called to
74/// LocalScope can break this assumption),
75///
76class LocalScope {
77public:
78 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
79
80 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000081 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000082 class const_iterator {
83 const LocalScope* Scope;
84
85 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
86 /// Invalid iterator (with null Scope) has VarIter equal to 0.
87 unsigned VarIter;
88
89 public:
90 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
91 /// Incrementing invalid iterator is allowed and will result in invalid
92 /// iterator.
93 const_iterator()
94 : Scope(NULL), VarIter(0) {}
95
96 /// Create valid iterator. In case when S.Prev is an invalid iterator and
97 /// I is equal to 0, this will create invalid iterator.
98 const_iterator(const LocalScope& S, unsigned I)
99 : Scope(&S), VarIter(I) {
100 // Iterator to "end" of scope is not allowed. Handle it by going up
101 // in scopes tree possibly up to invalid iterator in the root.
102 if (VarIter == 0 && Scope)
103 *this = Scope->Prev;
104 }
105
106 VarDecl* const* operator->() const {
107 assert (Scope && "Dereferencing invalid iterator is not allowed");
108 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
109 return &Scope->Vars[VarIter - 1];
110 }
111 VarDecl* operator*() const {
112 return *this->operator->();
113 }
114
115 const_iterator& operator++() {
116 if (!Scope)
117 return *this;
118
119 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
120 --VarIter;
121 if (VarIter == 0)
122 *this = Scope->Prev;
123 return *this;
124 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000125 const_iterator operator++(int) {
126 const_iterator P = *this;
127 ++*this;
128 return P;
129 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000130
131 bool operator==(const const_iterator& rhs) const {
132 return Scope == rhs.Scope && VarIter == rhs.VarIter;
133 }
134 bool operator!=(const const_iterator& rhs) const {
135 return !(*this == rhs);
136 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000137
138 operator bool() const {
139 return *this != const_iterator();
140 }
141
142 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000143 };
144
145 friend class const_iterator;
146
147private:
148 /// Automatic variables in order of declaration.
149 AutomaticVarsTy Vars;
150 /// Iterator to variable in previous scope that was declared just before
151 /// begin of this scope.
152 const_iterator Prev;
153
154public:
155 /// Constructs empty scope linked to previous scope in specified place.
156 LocalScope(const_iterator P)
157 : Vars()
158 , Prev(P) {}
159
160 /// Begin of scope in direction of CFG building (backwards).
161 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000162
163 void addVar(VarDecl* VD) {
164 Vars.push_back(VD);
165 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000166};
167
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000168/// distance - Calculates distance from this to L. L must be reachable from this
169/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
170/// number of scopes between this and L.
171int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
172 int D = 0;
173 const_iterator F = *this;
174 while (F.Scope != L.Scope) {
175 assert (F != const_iterator()
176 && "L iterator is not reachable from F iterator.");
177 D += F.VarIter;
178 F = F.Scope->Prev;
179 }
180 D += F.VarIter - L.VarIter;
181 return D;
182}
183
184/// BlockScopePosPair - Structure for specifying position in CFG during its
185/// build process. It consists of CFGBlock that specifies position in CFG graph
186/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000187struct BlockScopePosPair {
188 BlockScopePosPair() {}
189 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
190 : Block(B), ScopePos(S) {}
191
192 CFGBlock* Block;
193 LocalScope::const_iterator ScopePos;
194};
195
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000196/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000197/// The builder is stateful: an instance of the builder should be used to only
198/// construct a single CFG.
199///
200/// Example usage:
201///
202/// CFGBuilder builder;
203/// CFG* cfg = builder.BuildAST(stmt1);
204///
Mike Stump31feda52009-07-17 01:31:16 +0000205/// CFG construction is done via a recursive walk of an AST. We actually parse
206/// the AST in reverse order so that the successor of a basic block is
207/// constructed prior to its predecessor. This allows us to nicely capture
208/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000209///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000210class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000211 typedef BlockScopePosPair JumpTarget;
212 typedef BlockScopePosPair JumpSource;
213
Mike Stump0d76d072009-07-20 23:24:15 +0000214 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000215 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000216
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000217 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000218 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000219 JumpTarget ContinueJumpTarget;
220 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000221 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000222 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000223 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000224
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000225 // Current position in local scope.
226 LocalScope::const_iterator ScopePos;
227
228 // LabelMap records the mapping from Label expressions to their jump targets.
229 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000230 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000231
232 // A list of blocks that end with a "goto" that must be backpatched to their
233 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000234 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000235 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000236
Ted Kremenekeda180e22007-08-28 19:26:49 +0000237 // A list of labels whose address has been taken (for indirect gotos).
238 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
239 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000240
Zhongxing Xud38fb842010-09-16 03:28:18 +0000241 bool badCFG;
242 CFG::BuildOptions BuildOpts;
243
Mike Stump31feda52009-07-17 01:31:16 +0000244public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000245 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
246 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000247 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000248 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenek9aae5132007-08-23 21:42:29 +0000250 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000251 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000252 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000253
Ted Kremenek93668002009-07-17 22:18:43 +0000254private:
255 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000256 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
257 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
258 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000259 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000260 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
261 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
262 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000263 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000264 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000265 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000266 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000267 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000268 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000269 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000270 CFGBlock *VisitDeclStmt(DeclStmt *DS);
271 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000272 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
273 CFGBlock *VisitDoStmt(DoStmt *D);
274 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000275 CFGBlock *VisitGotoStmt(GotoStmt* G);
276 CFGBlock *VisitIfStmt(IfStmt *I);
277 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
278 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000279 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000280 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
281 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
282 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
283 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
284 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
285 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000286 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
287 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000288 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
289 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000290
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000291 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
292 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000293 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000294
Ted Kremenek6065ef62008-04-28 18:00:46 +0000295 // NYS == Not Yet Supported
296 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000297 badCFG = true;
298 return Block;
299 }
Mike Stump31feda52009-07-17 01:31:16 +0000300
Ted Kremenek93668002009-07-17 22:18:43 +0000301 void autoCreateBlock() { if (!Block) Block = createBlock(); }
302 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000303
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000304 CFGBlock *addStmt(Stmt *S) {
305 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000307 CFGBlock *addAutomaticObjDtors(LocalScope::const_iterator B,
308 LocalScope::const_iterator E, Stmt* S);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000309
Marcin Swiderski5e415732010-09-30 23:05:00 +0000310 // Local scopes creation.
311 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
312
313 LocalScope* addLocalScopeForStmt(Stmt* S, LocalScope* Scope = NULL);
314 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
315 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
316
317 void addLocalScopeAndDtors(Stmt* S);
318
319 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000320 void AppendStmt(CFGBlock *B, Stmt *S,
321 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
322 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000323 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000324
Marcin Swiderski321a7072010-09-30 22:54:37 +0000325 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
326 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
327 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
328 LocalScope::const_iterator E, Stmt* S);
329 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
330 LocalScope::const_iterator B, LocalScope::const_iterator E);
331
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000332 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
333 B->addSuccessor(S, cfg->getBumpVectorContext());
334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenek963cc312009-07-24 06:55:42 +0000336 /// TryResult - a class representing a variant over the values
337 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
338 /// and is used by the CFGBuilder to decide if a branch condition
339 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000340 class TryResult {
341 int X;
342 public:
343 TryResult(bool b) : X(b ? 1 : 0) {}
344 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000345
Ted Kremenek30754282009-07-24 04:47:11 +0000346 bool isTrue() const { return X == 1; }
347 bool isFalse() const { return X == 0; }
348 bool isKnown() const { return X >= 0; }
349 void negate() {
350 assert(isKnown());
351 X ^= 0x1;
352 }
353 };
Mike Stump11289f42009-09-09 15:08:12 +0000354
Mike Stump773582d2009-07-23 23:25:26 +0000355 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
356 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000357 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000358 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000359 return TryResult();
360
Mike Stump773582d2009-07-23 23:25:26 +0000361 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000362 if (!S->isTypeDependent() && !S->isValueDependent() &&
363 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000364 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000365
366 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000367 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000368};
Mike Stump31feda52009-07-17 01:31:16 +0000369
Douglas Gregor4619e432008-12-05 23:32:09 +0000370// FIXME: Add support for dependent-sized array types in C++?
371// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000372static VariableArrayType* FindVA(Type* t) {
373 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
374 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
375 if (vat->getSizeExpr())
376 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000377
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000378 t = vt->getElementType().getTypePtr();
379 }
Mike Stump31feda52009-07-17 01:31:16 +0000380
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000381 return 0;
382}
Mike Stump31feda52009-07-17 01:31:16 +0000383
384/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
385/// arbitrary statement. Examples include a single expression or a function
386/// body (compound statement). The ownership of the returned CFG is
387/// transferred to the caller. If CFG construction fails, this method returns
388/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000389CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000390 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000391
Mike Stump0d76d072009-07-20 23:24:15 +0000392 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000393 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000394 if (!Statement)
395 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000396
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000397 BuildOpts = BO;
398 if (!C->getLangOptions().CPlusPlus)
399 BuildOpts.AddImplicitDtors = false;
Mike Stump31feda52009-07-17 01:31:16 +0000400
401 // Create an empty block that will serve as the exit block for the CFG. Since
402 // this is the first block added to the CFG, it will be implicitly registered
403 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000404 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000405 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000406 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000407
Ted Kremenek9aae5132007-08-23 21:42:29 +0000408 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000409 CFGBlock *B = addStmt(Statement);
410
411 if (badCFG)
412 return NULL;
413
414 if (B)
415 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000416
417 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
418 // FIXME: Add code for base initializers and member initializers.
419 (void)CD;
420 }
Mike Stump31feda52009-07-17 01:31:16 +0000421
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000422 // Backpatch the gotos whose label -> block mappings we didn't know when we
423 // encountered them.
424 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
425 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000426
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000427 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000428 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
429 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000430
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000431 // If there is no target for the goto, then we are looking at an
432 // incomplete AST. Handle this by not registering a successor.
433 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000434
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000435 JumpTarget JT = LI->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000436 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000437 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000438 }
439
440 // Add successors to the Indirect Goto Dispatch block (if we have one).
441 if (CFGBlock* B = cfg->getIndirectGotoBlock())
442 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
443 E = AddressTakenLabels.end(); I != E; ++I ) {
444
445 // Lookup the target block.
446 LabelMapTy::iterator LI = LabelMap.find(*I);
447
448 // If there is no target block that contains label, then we are looking
449 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000450 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000451
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000452 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000453 }
Mike Stump31feda52009-07-17 01:31:16 +0000454
Mike Stump31feda52009-07-17 01:31:16 +0000455 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000456 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000457
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000458 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000459}
Mike Stump31feda52009-07-17 01:31:16 +0000460
Ted Kremenek9aae5132007-08-23 21:42:29 +0000461/// createBlock - Used to lazily create blocks that are connected
462/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000463CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000464 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000465 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000466 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000467 return B;
468}
Mike Stump31feda52009-07-17 01:31:16 +0000469
Marcin Swiderski5e415732010-09-30 23:05:00 +0000470/// addAutomaticObjDtors - Add to current block automatic objects destructors
471/// for objects in range of local scope positions. Use S as trigger statement
472/// for destructors.
473CFGBlock* CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
474 LocalScope::const_iterator E, Stmt* S) {
475 if (!BuildOpts.AddImplicitDtors)
476 return Block;
477 if (B == E)
478 return Block;
479
480 autoCreateBlock();
481 appendAutomaticObjDtors(Block, B, E, S);
482 return Block;
483}
484
485/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
486/// way return valid LocalScope object.
487LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
488 if (!Scope) {
489 Scope = cfg->getAllocator().Allocate<LocalScope>();
490 new (Scope) LocalScope(ScopePos);
491 }
492 return Scope;
493}
494
495/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
496/// that should create implicit scope (e.g. if/else substatements). Will reuse
497/// Scope if not NULL.
498LocalScope* CFGBuilder::addLocalScopeForStmt(Stmt* S, LocalScope* Scope) {
499 if (!BuildOpts.AddImplicitDtors)
500 return Scope;
501
502 // For compound statement we will be creating explicit scope.
503 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
504 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
505 ; BI != BE; ++BI) {
506 Stmt* SI = *BI;
507 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
508 SI = LS->getSubStmt();
509 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
510 Scope = addLocalScopeForDeclStmt(DS, Scope);
511 }
512 return Scope;
513 }
514
515 // For any other statement scope will be implicit and as such will be
516 // interesting only for DeclStmt.
517 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
518 S = LS->getSubStmt();
519 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
520 Scope = addLocalScopeForDeclStmt(DS, Scope);
521 return Scope;
522}
523
524/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
525/// reuse Scope if not NULL.
526LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
527 LocalScope* Scope) {
528 if (!BuildOpts.AddImplicitDtors)
529 return Scope;
530
531 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
532 ; DI != DE; ++DI) {
533 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
534 Scope = addLocalScopeForVarDecl(VD, Scope);
535 }
536 return Scope;
537}
538
539/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
540/// create add scope for automatic objects and temporary objects bound to
541/// const reference. Will reuse Scope if not NULL.
542LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
543 LocalScope* Scope) {
544 if (!BuildOpts.AddImplicitDtors)
545 return Scope;
546
547 // Check if variable is local.
548 switch (VD->getStorageClass()) {
549 case SC_None:
550 case SC_Auto:
551 case SC_Register:
552 break;
553 default: return Scope;
554 }
555
556 // Check for const references bound to temporary. Set type to pointee.
557 QualType QT = VD->getType();
558 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
559 QT = RT->getPointeeType();
560 if (!QT.isConstQualified())
561 return Scope;
562 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
563 return Scope;
564 }
565
566 // Check if type is a C++ class with non-trivial destructor.
567 const RecordType* RT = QT.getTypePtr()->getAs<RecordType>();
568 if (!RT || cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor())
569 return Scope;
570
571 // Add the variable to scope
572 Scope = createOrReuseLocalScope(Scope);
573 Scope->addVar(VD);
574 ScopePos = Scope->begin();
575 return Scope;
576}
577
578/// addLocalScopeAndDtors - For given statement add local scope for it and
579/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
580void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
581 if (!BuildOpts.AddImplicitDtors)
582 return;
583
584 LocalScope::const_iterator scopeBeginPos = ScopePos;
585 addLocalScopeForStmt(S, NULL);
586 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
587}
588
Marcin Swiderski321a7072010-09-30 22:54:37 +0000589/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
590/// automatic storage duration to CFGBlock's elements vector. Insertion will be
591/// performed in place specified with iterator.
592void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
593 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
594 BumpVectorContext& C = cfg->getBumpVectorContext();
595 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
596 while (B != E)
597 I = Blk->insertAutomaticObjDtor(I, *B++, S);
598}
599
600/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
601/// automatic storage duration to CFGBlock's elements vector. Elements will be
602/// appended to physical end of the vector which happens to be logical
603/// beginning.
604void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
605 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
606 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
607}
608
609/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
610/// variables with automatic storage duration to CFGBlock's elements vector.
611/// Elements will be prepended to physical beginning of the vector which
612/// happens to be logical end. Use blocks terminator as statement that specifies
613/// destructors call site.
614void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
615 LocalScope::const_iterator B, LocalScope::const_iterator E) {
616 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
617}
618
Ted Kremenek93668002009-07-17 22:18:43 +0000619/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000620/// blocks for ternary operators, &&, and ||. We also process "," and
621/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000622CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000623tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000624 if (!S) {
625 badCFG = true;
626 return 0;
627 }
Ted Kremenek93668002009-07-17 22:18:43 +0000628 switch (S->getStmtClass()) {
629 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000630 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000631
632 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000633 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000634
Ted Kremenek93668002009-07-17 22:18:43 +0000635 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000636 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000637
Ted Kremenek93668002009-07-17 22:18:43 +0000638 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000639 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000640
Ted Kremenek93668002009-07-17 22:18:43 +0000641 case Stmt::BreakStmtClass:
642 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000643
Ted Kremenek93668002009-07-17 22:18:43 +0000644 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000645 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000646 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000647
Ted Kremenek93668002009-07-17 22:18:43 +0000648 case Stmt::CaseStmtClass:
649 return VisitCaseStmt(cast<CaseStmt>(S));
650
651 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000652 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenek93668002009-07-17 22:18:43 +0000654 case Stmt::CompoundStmtClass:
655 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenek93668002009-07-17 22:18:43 +0000657 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000658 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000659
Ted Kremenek93668002009-07-17 22:18:43 +0000660 case Stmt::ContinueStmtClass:
661 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenekb27378c2010-01-19 20:40:33 +0000663 case Stmt::CXXCatchStmtClass:
664 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
665
Ted Kremenek82bfc862010-08-28 00:19:02 +0000666 case Stmt::CXXExprWithTemporariesClass: {
667 // FIXME: Handle temporaries. For now, just visit the subexpression
668 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000669 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000670 }
671
Zhongxing Xu7e612172010-04-13 09:38:01 +0000672 case Stmt::CXXMemberCallExprClass:
673 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
674
Ted Kremenekb27378c2010-01-19 20:40:33 +0000675 case Stmt::CXXThrowExprClass:
676 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000677
Ted Kremenekb27378c2010-01-19 20:40:33 +0000678 case Stmt::CXXTryStmtClass:
679 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000680
Ted Kremenek93668002009-07-17 22:18:43 +0000681 case Stmt::DeclStmtClass:
682 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek93668002009-07-17 22:18:43 +0000684 case Stmt::DefaultStmtClass:
685 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000686
Ted Kremenek93668002009-07-17 22:18:43 +0000687 case Stmt::DoStmtClass:
688 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenek93668002009-07-17 22:18:43 +0000690 case Stmt::ForStmtClass:
691 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenek93668002009-07-17 22:18:43 +0000693 case Stmt::GotoStmtClass:
694 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000695
Ted Kremenek93668002009-07-17 22:18:43 +0000696 case Stmt::IfStmtClass:
697 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenek93668002009-07-17 22:18:43 +0000699 case Stmt::IndirectGotoStmtClass:
700 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000701
Ted Kremenek93668002009-07-17 22:18:43 +0000702 case Stmt::LabelStmtClass:
703 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000704
Ted Kremenek5868ec62010-04-11 17:02:10 +0000705 case Stmt::MemberExprClass:
706 return VisitMemberExpr(cast<MemberExpr>(S), asc);
707
Ted Kremenek93668002009-07-17 22:18:43 +0000708 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000709 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
710
Ted Kremenek93668002009-07-17 22:18:43 +0000711 case Stmt::ObjCAtSynchronizedStmtClass:
712 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000713
Ted Kremenek93668002009-07-17 22:18:43 +0000714 case Stmt::ObjCAtThrowStmtClass:
715 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000716
Ted Kremenek93668002009-07-17 22:18:43 +0000717 case Stmt::ObjCAtTryStmtClass:
718 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000719
Ted Kremenek93668002009-07-17 22:18:43 +0000720 case Stmt::ObjCForCollectionStmtClass:
721 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000722
Ted Kremenek93668002009-07-17 22:18:43 +0000723 case Stmt::ParenExprClass:
724 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000725 goto tryAgain;
726
Ted Kremenek93668002009-07-17 22:18:43 +0000727 case Stmt::NullStmtClass:
728 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000729
Ted Kremenek93668002009-07-17 22:18:43 +0000730 case Stmt::ReturnStmtClass:
731 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000732
Ted Kremenek93668002009-07-17 22:18:43 +0000733 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000734 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000735
Ted Kremenek93668002009-07-17 22:18:43 +0000736 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000737 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000738
Ted Kremenek93668002009-07-17 22:18:43 +0000739 case Stmt::SwitchStmtClass:
740 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000741
Ted Kremenek93668002009-07-17 22:18:43 +0000742 case Stmt::WhileStmtClass:
743 return VisitWhileStmt(cast<WhileStmt>(S));
744 }
745}
Mike Stump11289f42009-09-09 15:08:12 +0000746
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000747CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
748 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000749 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000750 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000751 }
Mike Stump11289f42009-09-09 15:08:12 +0000752
Ted Kremenek93668002009-07-17 22:18:43 +0000753 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000754}
Mike Stump31feda52009-07-17 01:31:16 +0000755
Ted Kremenek93668002009-07-17 22:18:43 +0000756/// VisitChildren - Visit the children of a Stmt.
757CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
758 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000759 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000760 E = Terminator->child_end(); I != E; ++I) {
761 if (*I) B = Visit(*I);
762 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000763 return B;
764}
Mike Stump11289f42009-09-09 15:08:12 +0000765
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000766CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
767 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000768 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000769
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000770 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000771 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000772 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000773 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000774
Ted Kremenek9aae5132007-08-23 21:42:29 +0000775 return Block;
776}
Mike Stump11289f42009-09-09 15:08:12 +0000777
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000778CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
779 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000780 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000781 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000782 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000784 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000785 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Ted Kremenek93668002009-07-17 22:18:43 +0000787 // create the block evaluating the LHS
788 CFGBlock* LHSBlock = createBlock(false);
789 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 // create the block evaluating the RHS
792 Succ = ConfluenceBlock;
793 Block = NULL;
794 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000795
796 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000797 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000798 return 0;
799 }
800 else {
801 // Create an empty block for cases where the RHS doesn't require
802 // any explicit statements in the CFG.
803 RHSBlock = createBlock();
804 }
Mike Stump11289f42009-09-09 15:08:12 +0000805
Mike Stump773582d2009-07-23 23:25:26 +0000806 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000807 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000808 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000809 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000810
Ted Kremenek93668002009-07-17 22:18:43 +0000811 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000812 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000813 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
814 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000815 } else {
John McCalle3027922010-08-25 11:45:40 +0000816 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000817 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
818 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Ted Kremenek93668002009-07-17 22:18:43 +0000821 // Generate the blocks for evaluating the LHS.
822 Block = LHSBlock;
823 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000824 }
John McCalle3027922010-08-25 11:45:40 +0000825 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000826 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000827 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000828 addStmt(B->getRHS());
829 return addStmt(B->getLHS());
830 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000831 else if (B->isAssignmentOp()) {
832 if (asc.alwaysAdd()) {
833 autoCreateBlock();
834 AppendStmt(Block, B, asc);
835 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000836
Ted Kremenek8abff772010-09-14 01:13:32 +0000837 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
838 // create a new block, then we should return RBlock. Otherwise
839 // we'll incorrectly return NULL.
840 CFGBlock *RBlock = Visit(B->getRHS());
841 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
842 return LBlock ? LBlock : RBlock;
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000843 }
Mike Stump11289f42009-09-09 15:08:12 +0000844
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000845 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000846}
847
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000848CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
849 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000850 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000851 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000852 }
853 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000854}
855
Ted Kremenek93668002009-07-17 22:18:43 +0000856CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
857 // "break" is a control-flow statement. Thus we stop processing the current
858 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000859 if (badCFG)
860 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000861
Ted Kremenek93668002009-07-17 22:18:43 +0000862 // Now create a new block that ends with the break statement.
863 Block = createBlock(false);
864 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000865
Ted Kremenek93668002009-07-17 22:18:43 +0000866 // If there is no target for the break, then we are looking at an incomplete
867 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000868 if (BreakJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000869 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000870 AddSuccessor(Block, BreakJumpTarget.Block);
871 } else
Ted Kremenek93668002009-07-17 22:18:43 +0000872 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000873
874
Ted Kremenek9aae5132007-08-23 21:42:29 +0000875 return Block;
876}
Mike Stump11289f42009-09-09 15:08:12 +0000877
Mike Stump04c68512010-01-21 15:20:48 +0000878static bool CanThrow(Expr *E) {
879 QualType Ty = E->getType();
880 if (Ty->isFunctionPointerType())
881 Ty = Ty->getAs<PointerType>()->getPointeeType();
882 else if (Ty->isBlockPointerType())
883 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000884
Mike Stump04c68512010-01-21 15:20:48 +0000885 const FunctionType *FT = Ty->getAs<FunctionType>();
886 if (FT) {
887 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
888 if (Proto->hasEmptyExceptionSpec())
889 return false;
890 }
891 return true;
892}
893
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000894CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000895 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000896 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000897 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000898 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000899 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000900
Mike Stump04c68512010-01-21 15:20:48 +0000901 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000902
903 // Languages without exceptions are assumed to not throw.
904 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000905 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +0000906 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000907 }
908
909 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000910 if (FD->hasAttr<NoReturnAttr>())
911 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000912 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000913 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000914 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000915
Mike Stump04c68512010-01-21 15:20:48 +0000916 if (!CanThrow(C->getCallee()))
917 AddEHEdge = false;
918
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000919 if (!NoReturn && !AddEHEdge) {
920 if (asc.asLValue())
921 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
922 else
923 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Mike Stump92244b02010-01-19 22:00:14 +0000926 if (Block) {
927 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000928 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +0000929 return 0;
930 }
Mike Stump11289f42009-09-09 15:08:12 +0000931
Mike Stump92244b02010-01-19 22:00:14 +0000932 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000933 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000934
Mike Stump92244b02010-01-19 22:00:14 +0000935 if (NoReturn) {
936 // Wire this to the exit block directly.
937 AddSuccessor(Block, &cfg->getExit());
938 }
Mike Stump04c68512010-01-21 15:20:48 +0000939 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000940 // Add exceptional edges.
941 if (TryTerminatedBlock)
942 AddSuccessor(Block, TryTerminatedBlock);
943 else
944 AddSuccessor(Block, &cfg->getExit());
945 }
Mike Stump11289f42009-09-09 15:08:12 +0000946
Mike Stump8c5d7992009-07-25 21:26:53 +0000947 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000948}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000950CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
951 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000952 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000953 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000954 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000955 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000956
Ted Kremenek5868ec62010-04-11 17:02:10 +0000957 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
958 : AddStmtChoice::AlwaysAdd;
959
Ted Kremenek21822592009-07-17 18:20:32 +0000960 Succ = ConfluenceBlock;
961 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000962 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000963 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000964 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000965
Ted Kremenek21822592009-07-17 18:20:32 +0000966 Succ = ConfluenceBlock;
967 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000968 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000969 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000970 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000971
Ted Kremenek21822592009-07-17 18:20:32 +0000972 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000973 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000974 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000975 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
976 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000977 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000978 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000979}
Mike Stump11289f42009-09-09 15:08:12 +0000980
981
982CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000983 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +0000984 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000985
986 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
987 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000988 // If we hit a segment of code just containing ';' (NullStmts), we can
989 // get a null block back. In such cases, just use the LastBlock
990 if (CFGBlock *newBlock = addStmt(*I))
991 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000992
Ted Kremenekce499c22009-08-27 23:16:26 +0000993 if (badCFG)
994 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000995 }
Mike Stump92244b02010-01-19 22:00:14 +0000996
Ted Kremenek93668002009-07-17 22:18:43 +0000997 return LastBlock;
998}
Mike Stump11289f42009-09-09 15:08:12 +0000999
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001000CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1001 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001002 // Create the confluence block that will "merge" the results of the ternary
1003 // expression.
1004 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001005 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001006 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001007 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001008
Ted Kremenek5868ec62010-04-11 17:02:10 +00001009 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1010 : AddStmtChoice::AlwaysAdd;
1011
Ted Kremenek51d40b02009-07-17 18:15:54 +00001012 // Create a block for the LHS expression if there is an LHS expression. A
1013 // GCC extension allows LHS to be NULL, causing the condition to be the
1014 // value that is returned instead.
1015 // e.g: x ?: y is shorthand for: x ? x : y;
1016 Succ = ConfluenceBlock;
1017 Block = NULL;
1018 CFGBlock* LHSBlock = NULL;
1019 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001020 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001021 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001022 return 0;
1023 Block = NULL;
1024 }
Mike Stump11289f42009-09-09 15:08:12 +00001025
Ted Kremenek51d40b02009-07-17 18:15:54 +00001026 // Create the block for the RHS expression.
1027 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001028 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001029 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001030 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001031
Ted Kremenek51d40b02009-07-17 18:15:54 +00001032 // Create the block that will contain the condition.
1033 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001034
Mike Stump773582d2009-07-23 23:25:26 +00001035 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001036 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001037 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001038 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001039 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001040 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001041 // If we know the condition is false, add NULL as the successor for
1042 // the block containing the condition. In this case, the confluence
1043 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001044 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001045 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001046 } else {
1047 // If we have no LHS expression, add the ConfluenceBlock as a direct
1048 // successor for the block containing the condition. Moreover, we need to
1049 // reverse the order of the predecessors in the ConfluenceBlock because
1050 // the RHSBlock will have been added to the succcessors already, and we
1051 // want the first predecessor to the the block containing the expression
1052 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001053 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +00001054 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +00001055 std::reverse(ConfluenceBlock->pred_begin(),
1056 ConfluenceBlock->pred_end());
1057 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001060 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001061 Block->setTerminator(C);
1062 return addStmt(C->getCond());
1063}
1064
Ted Kremenek93668002009-07-17 22:18:43 +00001065CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
1066 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001067
Ted Kremenek93668002009-07-17 22:18:43 +00001068 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001069 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001070 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +00001071 }
Mike Stump11289f42009-09-09 15:08:12 +00001072
Ted Kremenek93668002009-07-17 22:18:43 +00001073 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001074
Ted Kremenek93668002009-07-17 22:18:43 +00001075 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1076 typedef llvm::SmallVector<Decl*,10> BufTy;
1077 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001078
Ted Kremenek93668002009-07-17 22:18:43 +00001079 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1080 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1081 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1082 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001083
Ted Kremenek93668002009-07-17 22:18:43 +00001084 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1085 // automatically freed with the CFG.
1086 DeclGroupRef DG(*I);
1087 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001088 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001089 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001090
Ted Kremenek93668002009-07-17 22:18:43 +00001091 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001092 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001093 B = VisitDeclSubExpr(D);
1094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
1096 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001097}
Mike Stump11289f42009-09-09 15:08:12 +00001098
Ted Kremenek93668002009-07-17 22:18:43 +00001099/// VisitDeclSubExpr - Utility method to add block-level expressions for
1100/// initializers in Decls.
1101CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
1102 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +00001103
Ted Kremenek93668002009-07-17 22:18:43 +00001104 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Ted Kremenek93668002009-07-17 22:18:43 +00001106 if (!VD)
1107 return Block;
Mike Stump11289f42009-09-09 15:08:12 +00001108
Ted Kremenek93668002009-07-17 22:18:43 +00001109 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +00001110
Ted Kremenek93668002009-07-17 22:18:43 +00001111 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +00001112 AddStmtChoice::Kind k =
1113 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
1114 : AddStmtChoice::NotAlwaysAdd;
1115 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +00001116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Ted Kremenek93668002009-07-17 22:18:43 +00001118 // If the type of VD is a VLA, then we must process its size expressions.
1119 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1120 VA = FindVA(VA->getElementType().getTypePtr()))
1121 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001122
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001123 // Remove variable from local scope.
1124 if (ScopePos && VD == *ScopePos)
1125 ++ScopePos;
1126
Ted Kremenek93668002009-07-17 22:18:43 +00001127 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001128}
1129
1130CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001131 // We may see an if statement in the middle of a basic block, or it may be the
1132 // first statement we are processing. In either case, we create a new basic
1133 // block. First, we create the blocks for the then...else statements, and
1134 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001135 // middle of a block, we stop processing that block. That block is then the
1136 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001137
1138 // The block we were proccessing is now finished. Make it the successor
1139 // block.
1140 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001141 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001142 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001143 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001144 }
Mike Stump31feda52009-07-17 01:31:16 +00001145
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001146 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001147 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001148
Ted Kremenek9aae5132007-08-23 21:42:29 +00001149 if (Stmt* Else = I->getElse()) {
1150 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001151
Ted Kremenek9aae5132007-08-23 21:42:29 +00001152 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001153 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001154 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001155 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001156
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001157 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1158 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001159 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001160 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001161 return 0;
1162 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001163 }
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001165 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001166 CFGBlock* ThenBlock;
1167 {
1168 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001169 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001170 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001171 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001172 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001173
Ted Kremenek1b379512009-04-01 03:52:47 +00001174 if (!ThenBlock) {
1175 // We can reach here if the "then" body has all NullStmts.
1176 // Create an empty block so we can distinguish between true and false
1177 // branches in path-sensitive analyses.
1178 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001179 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001180 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001181 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001182 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001183 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001184 }
1185
Mike Stump31feda52009-07-17 01:31:16 +00001186 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001187 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001188
Ted Kremenek9aae5132007-08-23 21:42:29 +00001189 // Set the terminator of the new block to the If statement.
1190 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001191
Mike Stump773582d2009-07-23 23:25:26 +00001192 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001193 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001194
Ted Kremenek9aae5132007-08-23 21:42:29 +00001195 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001196 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1197 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001198
1199 // Add the condition as the last statement in the new block. This may create
1200 // new blocks as the condition may contain control-flow. Any newly created
1201 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001202 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001203
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001204 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1205 // and the condition variable initialization to the CFG.
1206 if (VarDecl *VD = I->getConditionVariable()) {
1207 if (Expr *Init = VD->getInit()) {
1208 autoCreateBlock();
1209 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1210 addStmt(Init);
1211 }
1212 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001213
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001214 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001215}
Mike Stump31feda52009-07-17 01:31:16 +00001216
1217
Ted Kremenek9aae5132007-08-23 21:42:29 +00001218CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001219 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001220 //
Mike Stump31feda52009-07-17 01:31:16 +00001221 // NOTE: If a "return" appears in the middle of a block, this means that the
1222 // code afterwards is DEAD (unreachable). We still keep a basic block
1223 // for that code; a simple "mark-and-sweep" from the entry block will be
1224 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001225
1226 // Create the new block.
1227 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001228
Ted Kremenek9aae5132007-08-23 21:42:29 +00001229 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001230 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001231 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001232
1233 // Add the return statement to the block. This may create new blocks if R
1234 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001235 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001236}
1237
1238CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1239 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001240 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001241 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001242
Ted Kremenek93668002009-07-17 22:18:43 +00001243 if (!LabelBlock) // This can happen when the body is empty, i.e.
1244 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001245
Ted Kremenek93668002009-07-17 22:18:43 +00001246 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001247 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001248
1249 // Labels partition blocks, so this is the end of the basic block we were
1250 // processing (L is the block's label). Because this is label (and we have
1251 // already processed the substatement) there is no extra control-flow to worry
1252 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001253 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001254 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001255 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001256
1257 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001258 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001259
Ted Kremenek9aae5132007-08-23 21:42:29 +00001260 // This block is now the implicit successor of other blocks.
1261 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001262
Ted Kremenek9aae5132007-08-23 21:42:29 +00001263 return LabelBlock;
1264}
1265
1266CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001267 // Goto is a control-flow statement. Thus we stop processing the current
1268 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001269
Ted Kremenek9aae5132007-08-23 21:42:29 +00001270 Block = createBlock(false);
1271 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001272
1273 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001274 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001275
Ted Kremenek9aae5132007-08-23 21:42:29 +00001276 if (I == LabelMap.end())
1277 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001278 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1279 else {
1280 JumpTarget JT = I->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001281 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001282 AddSuccessor(Block, JT.Block);
1283 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001284
Mike Stump31feda52009-07-17 01:31:16 +00001285 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286}
1287
1288CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001289 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001290
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001291 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1292
Mike Stump014b3ea2009-07-21 01:12:51 +00001293 // "for" is a control-flow statement. Thus we stop processing the current
1294 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001295 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001296 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001297 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001298 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001299 } else
1300 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001301
Ted Kremenek304a9532010-05-21 20:30:15 +00001302 // Save the current value for the break targets.
1303 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001304 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
1305 BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001306
Mike Stump31feda52009-07-17 01:31:16 +00001307 // Because of short-circuit evaluation, the condition of the loop can span
1308 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1309 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001310 CFGBlock* ExitConditionBlock = createBlock(false);
1311 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001312
Ted Kremenek81e14852007-08-27 19:46:09 +00001313 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001314 ExitConditionBlock->setTerminator(F);
1315
1316 // Now add the actual condition to the condition block. Because the condition
1317 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001318 if (Stmt* C = F->getCond()) {
1319 Block = ExitConditionBlock;
1320 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001321 assert(Block == EntryConditionBlock ||
1322 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001323
1324 // If this block contains a condition variable, add both the condition
1325 // variable and initializer to the CFG.
1326 if (VarDecl *VD = F->getConditionVariable()) {
1327 if (Expr *Init = VD->getInit()) {
1328 autoCreateBlock();
1329 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1330 EntryConditionBlock = addStmt(Init);
1331 assert(Block == EntryConditionBlock);
1332 }
1333 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001334
Ted Kremenek55957a82009-05-02 00:13:27 +00001335 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001336 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001337 return 0;
1338 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001339 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001340
Mike Stump31feda52009-07-17 01:31:16 +00001341 // The condition block is the implicit successor for the loop body as well as
1342 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001343 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001344
Mike Stump773582d2009-07-23 23:25:26 +00001345 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001346 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001347
Mike Stump773582d2009-07-23 23:25:26 +00001348 if (F->getCond())
1349 KnownVal = TryEvaluateBool(F->getCond());
1350
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351 // Now create the loop body.
1352 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001353 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001354
Ted Kremenek304a9532010-05-21 20:30:15 +00001355 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001356 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1357 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremeneke9610502007-08-30 18:39:40 +00001359 // Create a new block to contain the (bottom) of the loop body.
1360 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001361
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001362 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001363 // Generate increment code in its own basic block. This is the target of
1364 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001365 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001366 } else {
1367 // No increment code. Create a special, empty, block that is used as the
1368 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001369 assert(Succ == EntryConditionBlock);
1370 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001371 }
Mike Stump31feda52009-07-17 01:31:16 +00001372
Ted Kremenek902393b2009-04-28 00:51:56 +00001373 // Finish up the increment (or empty) block if it hasn't been already.
1374 if (Block) {
1375 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001376 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001377 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001378 Block = 0;
1379 }
Mike Stump31feda52009-07-17 01:31:16 +00001380
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001381 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001382
Ted Kremenek902393b2009-04-28 00:51:56 +00001383 // The starting block for the loop increment is the block that should
1384 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001385 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001386
Mike Stump31feda52009-07-17 01:31:16 +00001387 // Now populate the body block, and in the process create new blocks as we
1388 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001389 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001390
1391 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001392 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001393 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001394 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001395
Ted Kremenek30754282009-07-24 04:47:11 +00001396 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001397 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001398 }
Mike Stump31feda52009-07-17 01:31:16 +00001399
Ted Kremenek30754282009-07-24 04:47:11 +00001400 // Link up the condition block with the code that follows the loop. (the
1401 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001402 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001403
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001405 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406 if (Stmt* I = F->getInit()) {
1407 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001408 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001409 } else {
1410 // There is no loop initialization. We are thus basically a while loop.
1411 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001412 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001413 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001414 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415 }
1416}
1417
Ted Kremenek5868ec62010-04-11 17:02:10 +00001418CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1419 if (asc.alwaysAdd()) {
1420 autoCreateBlock();
1421 AppendStmt(Block, M, asc);
1422 }
1423 return Visit(M->getBase(),
1424 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1425 : AddStmtChoice::AsLValueNotAlwaysAdd);
1426}
1427
Ted Kremenek9d56e642008-11-11 17:10:00 +00001428CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1429 // Objective-C fast enumeration 'for' statements:
1430 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1431 //
1432 // for ( Type newVariable in collection_expression ) { statements }
1433 //
1434 // becomes:
1435 //
1436 // prologue:
1437 // 1. collection_expression
1438 // T. jump to loop_entry
1439 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001440 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001441 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1442 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1443 // TB:
1444 // statements
1445 // T. jump to loop_entry
1446 // FB:
1447 // what comes after
1448 //
1449 // and
1450 //
1451 // Type existingItem;
1452 // for ( existingItem in expression ) { statements }
1453 //
1454 // becomes:
1455 //
Mike Stump31feda52009-07-17 01:31:16 +00001456 // the same with newVariable replaced with existingItem; the binding works
1457 // the same except that for one ObjCForCollectionStmt::getElement() returns
1458 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001459 //
Mike Stump31feda52009-07-17 01:31:16 +00001460
Ted Kremenek9d56e642008-11-11 17:10:00 +00001461 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek9d56e642008-11-11 17:10:00 +00001463 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001464 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001465 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001466 LoopSuccessor = Block;
1467 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001468 } else
1469 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001470
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001471 // Build the condition blocks.
1472 CFGBlock* ExitConditionBlock = createBlock(false);
1473 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001474
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001475 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001476 ExitConditionBlock->setTerminator(S);
1477
1478 // The last statement in the block should be the ObjCForCollectionStmt, which
1479 // performs the actual binding to 'element' and determines if there are any
1480 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001481 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001482 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001483
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001484 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001485 // generate new blocks as necesary. We DON'T add the statement by default to
1486 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001487 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001488 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001489 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001490 return 0;
1491 Block = 0;
1492 }
Mike Stump31feda52009-07-17 01:31:16 +00001493
1494 // The condition block is the implicit successor for the loop body as well as
1495 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001496 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001497
Ted Kremenek9d56e642008-11-11 17:10:00 +00001498 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001499 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001500 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001501 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1502 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1503 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001504
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001505 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1506 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001507
Ted Kremenek93668002009-07-17 22:18:43 +00001508 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001509
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001510 if (!BodyBlock)
1511 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001512 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001513 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001514 return 0;
1515 }
Mike Stump31feda52009-07-17 01:31:16 +00001516
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001517 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001518 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001519 }
Mike Stump31feda52009-07-17 01:31:16 +00001520
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001521 // Link up the condition block with the code that follows the loop.
1522 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001523 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001524
Ted Kremenek9d56e642008-11-11 17:10:00 +00001525 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001526 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001527 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001528}
1529
Ted Kremenek49805452009-05-02 01:49:13 +00001530CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1531 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001532
Ted Kremenek49805452009-05-02 01:49:13 +00001533 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001534 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001535
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001536 // The sync body starts its own basic block. This makes it a little easier
1537 // for diagnostic clients.
1538 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001539 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001540 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001541
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001542 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001543 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001544 }
Mike Stump31feda52009-07-17 01:31:16 +00001545
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001546 // Add the @synchronized to the CFG.
1547 autoCreateBlock();
1548 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1549
Ted Kremenek49805452009-05-02 01:49:13 +00001550 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001551 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001552}
Mike Stump31feda52009-07-17 01:31:16 +00001553
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001554CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001555 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001556 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001557}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001558
Ted Kremenek9aae5132007-08-23 21:42:29 +00001559CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001560 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001561
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001562 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1563
Mike Stump014b3ea2009-07-21 01:12:51 +00001564 // "while" is a control-flow statement. Thus we stop processing the current
1565 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001566 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001567 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001568 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001569 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001570 } else
1571 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001572
1573 // Because of short-circuit evaluation, the condition of the loop can span
1574 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1575 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001576 CFGBlock* ExitConditionBlock = createBlock(false);
1577 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001578
Ted Kremenek81e14852007-08-27 19:46:09 +00001579 // Set the terminator for the "exit" condition block.
1580 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001581
1582 // Now add the actual condition to the condition block. Because the condition
1583 // itself may contain control-flow, new blocks may be created. Thus we update
1584 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001585 if (Stmt* C = W->getCond()) {
1586 Block = ExitConditionBlock;
1587 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001588 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001589
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001590 // If this block contains a condition variable, add both the condition
1591 // variable and initializer to the CFG.
1592 if (VarDecl *VD = W->getConditionVariable()) {
1593 if (Expr *Init = VD->getInit()) {
1594 autoCreateBlock();
1595 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1596 EntryConditionBlock = addStmt(Init);
1597 assert(Block == EntryConditionBlock);
1598 }
1599 }
1600
Ted Kremenek55957a82009-05-02 00:13:27 +00001601 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001602 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001603 return 0;
1604 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001605 }
Mike Stump31feda52009-07-17 01:31:16 +00001606
1607 // The condition block is the implicit successor for the loop body as well as
1608 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001609 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001610
Mike Stump773582d2009-07-23 23:25:26 +00001611 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001612 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001613
Ted Kremenek9aae5132007-08-23 21:42:29 +00001614 // Process the loop body.
1615 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001616 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001617
1618 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001619 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1620 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1621 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001622
Mike Stump31feda52009-07-17 01:31:16 +00001623 // Create an empty block to represent the transition block for looping back
1624 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001625 Block = 0;
1626 assert(Succ == EntryConditionBlock);
1627 Succ = createBlock();
1628 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001629 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001630
Ted Kremenek9aae5132007-08-23 21:42:29 +00001631 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001632 BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001633
Ted Kremenek9aae5132007-08-23 21:42:29 +00001634 // NULL out Block to force lazy instantiation of blocks for the body.
1635 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001636
Ted Kremenek9aae5132007-08-23 21:42:29 +00001637 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001638 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001639
Ted Kremeneke9610502007-08-30 18:39:40 +00001640 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001641 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001642 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001643 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001644 return 0;
1645 }
Mike Stump31feda52009-07-17 01:31:16 +00001646
Ted Kremenek30754282009-07-24 04:47:11 +00001647 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001648 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001649 }
Mike Stump31feda52009-07-17 01:31:16 +00001650
Ted Kremenek30754282009-07-24 04:47:11 +00001651 // Link up the condition block with the code that follows the loop. (the
1652 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001653 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001654
1655 // There can be no more statements in the condition block since we loop back
1656 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001657 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001658
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001659 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001660 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001661 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001662}
Mike Stump11289f42009-09-09 15:08:12 +00001663
1664
Ted Kremenek93668002009-07-17 22:18:43 +00001665CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1666 // FIXME: For now we pretend that @catch and the code it contains does not
1667 // exit.
1668 return Block;
1669}
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenek93041ba2008-12-09 20:20:09 +00001671CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1672 // FIXME: This isn't complete. We basically treat @throw like a return
1673 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001674
Ted Kremenek0868eea2009-09-24 18:45:41 +00001675 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001676 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001677 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001678
Ted Kremenek93041ba2008-12-09 20:20:09 +00001679 // Create the new block.
1680 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001681
Ted Kremenek93041ba2008-12-09 20:20:09 +00001682 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001683 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001684
1685 // Add the statement to the block. This may create new blocks if S contains
1686 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001687 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001688}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001689
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001690CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001691 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001692 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001693 return 0;
1694
1695 // Create the new block.
1696 Block = createBlock(false);
1697
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001698 if (TryTerminatedBlock)
1699 // The current try statement is the only successor.
1700 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001701 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001702 // otherwise the Exit block is the only successor.
1703 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001704
1705 // Add the statement to the block. This may create new blocks if S contains
1706 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001707 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001708}
1709
Ted Kremenek93668002009-07-17 22:18:43 +00001710CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001711 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001712
Mike Stump8d50b6a2009-07-21 01:27:50 +00001713 // "do...while" is a control-flow statement. Thus we stop processing the
1714 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001715 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001716 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001717 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001718 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001719 } else
1720 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001721
1722 // Because of short-circuit evaluation, the condition of the loop can span
1723 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1724 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001725 CFGBlock* ExitConditionBlock = createBlock(false);
1726 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001727
Ted Kremenek81e14852007-08-27 19:46:09 +00001728 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001729 ExitConditionBlock->setTerminator(D);
1730
1731 // Now add the actual condition to the condition block. Because the condition
1732 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001733 if (Stmt* C = D->getCond()) {
1734 Block = ExitConditionBlock;
1735 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001736 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001737 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001738 return 0;
1739 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001740 }
Mike Stump31feda52009-07-17 01:31:16 +00001741
Ted Kremeneka1523a32008-02-27 07:20:00 +00001742 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001743 Succ = EntryConditionBlock;
1744
Mike Stump773582d2009-07-23 23:25:26 +00001745 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001746 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001747
Ted Kremenek9aae5132007-08-23 21:42:29 +00001748 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001749 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001750 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001751 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001752
Ted Kremenek9aae5132007-08-23 21:42:29 +00001753 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001754 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1755 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1756 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001757
Ted Kremenek9aae5132007-08-23 21:42:29 +00001758 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001759 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001760
Ted Kremenek9aae5132007-08-23 21:42:29 +00001761 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001762 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001763
Ted Kremenek9aae5132007-08-23 21:42:29 +00001764 // NULL out Block to force lazy instantiation of blocks for the body.
1765 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenek9aae5132007-08-23 21:42:29 +00001767 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001768 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremeneke9610502007-08-30 18:39:40 +00001770 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001771 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001772 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001773 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001774 return 0;
1775 }
Mike Stump31feda52009-07-17 01:31:16 +00001776
Ted Kremenek110974d2010-08-17 20:59:56 +00001777 if (!KnownVal.isFalse()) {
1778 // Add an intermediate block between the BodyBlock and the
1779 // ExitConditionBlock to represent the "loop back" transition. Create an
1780 // empty block to represent the transition block for looping back to the
1781 // head of the loop.
1782 // FIXME: Can we do this more efficiently without adding another block?
1783 Block = NULL;
1784 Succ = BodyBlock;
1785 CFGBlock *LoopBackBlock = createBlock();
1786 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001787
Ted Kremenek110974d2010-08-17 20:59:56 +00001788 // Add the loop body entry as a successor to the condition.
1789 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1790 }
1791 else
1792 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001793 }
Mike Stump31feda52009-07-17 01:31:16 +00001794
Ted Kremenek30754282009-07-24 04:47:11 +00001795 // Link up the condition block with the code that follows the loop.
1796 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001797 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001798
1799 // There can be no more statements in the body block(s) since we loop back to
1800 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001801 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001802
Ted Kremenek9aae5132007-08-23 21:42:29 +00001803 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001804 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001805 return BodyBlock;
1806}
1807
1808CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1809 // "continue" is a control-flow statement. Thus we stop processing the
1810 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001811 if (badCFG)
1812 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001813
Ted Kremenek9aae5132007-08-23 21:42:29 +00001814 // Now create a new block that ends with the continue statement.
1815 Block = createBlock(false);
1816 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001817
Ted Kremenek9aae5132007-08-23 21:42:29 +00001818 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001819 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001820 if (ContinueJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001821 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001822 AddSuccessor(Block, ContinueJumpTarget.Block);
1823 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00001824 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001825
Ted Kremenek9aae5132007-08-23 21:42:29 +00001826 return Block;
1827}
Mike Stump11289f42009-09-09 15:08:12 +00001828
Ted Kremenek0747de62009-07-18 00:47:21 +00001829CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001830 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001831
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001832 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001833 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001834 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001835 }
Mike Stump11289f42009-09-09 15:08:12 +00001836
Ted Kremenek93668002009-07-17 22:18:43 +00001837 // VLA types have expressions that must be evaluated.
1838 if (E->isArgumentType()) {
1839 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1840 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1841 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001842 }
Mike Stump11289f42009-09-09 15:08:12 +00001843
Mike Stump31feda52009-07-17 01:31:16 +00001844 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001845}
Mike Stump11289f42009-09-09 15:08:12 +00001846
Ted Kremenek93668002009-07-17 22:18:43 +00001847/// VisitStmtExpr - Utility method to handle (nested) statement
1848/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001849CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1850 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001851 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001852 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001853 }
Ted Kremenek93668002009-07-17 22:18:43 +00001854 return VisitCompoundStmt(SE->getSubStmt());
1855}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001856
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001857CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001858 // "switch" is a control-flow statement. Thus we stop processing the current
1859 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001860 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001861
Ted Kremenek9aae5132007-08-23 21:42:29 +00001862 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001863 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001864 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001865 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001866 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001867
1868 // Save the current "switch" context.
1869 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001870 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001871 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001872
Mike Stump31feda52009-07-17 01:31:16 +00001873 // Set the "default" case to be the block after the switch statement. If the
1874 // switch statement contains a "default:", this value will be overwritten with
1875 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001876 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001877
Ted Kremenek9aae5132007-08-23 21:42:29 +00001878 // Create a new block that will contain the switch statement.
1879 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001880
Ted Kremenek9aae5132007-08-23 21:42:29 +00001881 // Now process the switch body. The code after the switch is the implicit
1882 // successor.
1883 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001884 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001885
1886 // When visiting the body, the case statements should automatically get linked
1887 // up to the switch. We also don't keep a pointer to the body, since all
1888 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001889 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001890 Block = NULL;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001891 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001892 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001893 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001894 return 0;
1895 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001896
Mike Stump31feda52009-07-17 01:31:16 +00001897 // If we have no "default:" case, the default transition is to the code
1898 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001899 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001900
Ted Kremenek81e14852007-08-27 19:46:09 +00001901 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001902 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001903 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001904 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001905 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001906
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001907 // Finally, if the SwitchStmt contains a condition variable, add both the
1908 // SwitchStmt and the condition variable initialization to the CFG.
1909 if (VarDecl *VD = Terminator->getConditionVariable()) {
1910 if (Expr *Init = VD->getInit()) {
1911 autoCreateBlock();
1912 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1913 addStmt(Init);
1914 }
1915 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001916
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001917 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001918}
1919
Ted Kremenek93668002009-07-17 22:18:43 +00001920CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001921 // CaseStmts are essentially labels, so they are the first statement in a
1922 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001923 CFGBlock *TopBlock = 0, *LastBlock = 0;
1924
1925 if (Stmt *Sub = CS->getSubStmt()) {
1926 // For deeply nested chains of CaseStmts, instead of doing a recursion
1927 // (which can blow out the stack), manually unroll and create blocks
1928 // along the way.
1929 while (isa<CaseStmt>(Sub)) {
1930 CFGBlock *CurrentBlock = createBlock(false);
1931 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001932
Ted Kremenek60fa6572010-08-04 23:54:30 +00001933 if (TopBlock)
1934 AddSuccessor(LastBlock, CurrentBlock);
1935 else
1936 TopBlock = CurrentBlock;
1937
1938 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1939 LastBlock = CurrentBlock;
1940
1941 CS = cast<CaseStmt>(Sub);
1942 Sub = CS->getSubStmt();
1943 }
1944
1945 addStmt(Sub);
1946 }
Mike Stump11289f42009-09-09 15:08:12 +00001947
Ted Kremenek55e91e82007-08-30 18:48:11 +00001948 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001949 if (!CaseBlock)
1950 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001951
1952 // Cases statements partition blocks, so this is the top of the basic block we
1953 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001954 CaseBlock->setLabel(CS);
1955
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001956 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001957 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001958
1959 // Add this block to the list of successors for the block with the switch
1960 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001961 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001962 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenek9aae5132007-08-23 21:42:29 +00001964 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1965 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001966
Ted Kremenek60fa6572010-08-04 23:54:30 +00001967 if (TopBlock) {
1968 AddSuccessor(LastBlock, CaseBlock);
1969 Succ = TopBlock;
1970 }
1971 else {
1972 // This block is now the implicit successor of other blocks.
1973 Succ = CaseBlock;
1974 }
Mike Stump31feda52009-07-17 01:31:16 +00001975
Ted Kremenek60fa6572010-08-04 23:54:30 +00001976 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001977}
Mike Stump31feda52009-07-17 01:31:16 +00001978
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001979CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001980 if (Terminator->getSubStmt())
1981 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001982
Ted Kremenek654c78f2008-02-13 22:05:39 +00001983 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001984
1985 if (!DefaultCaseBlock)
1986 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001987
1988 // Default statements partition blocks, so this is the top of the basic block
1989 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001990 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001991
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001992 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001993 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001994
Mike Stump31feda52009-07-17 01:31:16 +00001995 // Unlike case statements, we don't add the default block to the successors
1996 // for the switch statement immediately. This is done when we finish
1997 // processing the switch statement. This allows for the default case
1998 // (including a fall-through to the code after the switch statement) to always
1999 // be the last successor of a switch-terminated block.
2000
Ted Kremenek654c78f2008-02-13 22:05:39 +00002001 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2002 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002003
Ted Kremenek654c78f2008-02-13 22:05:39 +00002004 // This block is now the implicit successor of other blocks.
2005 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002006
2007 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002008}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002009
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002010CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2011 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2012 // current block.
2013 CFGBlock* TrySuccessor = NULL;
2014
2015 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002016 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002017 return 0;
2018 TrySuccessor = Block;
2019 } else TrySuccessor = Succ;
2020
Mike Stump0bdba6c2010-01-20 01:15:34 +00002021 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002022
2023 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002024 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002025 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002026 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002027
Mike Stump0bdba6c2010-01-20 01:15:34 +00002028 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002029 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2030 // The code after the try is the implicit successor.
2031 Succ = TrySuccessor;
2032 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002033 if (CS->getExceptionDecl() == 0) {
2034 HasCatchAll = true;
2035 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002036 Block = NULL;
2037 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2038 if (CatchBlock == 0)
2039 return 0;
2040 // Add this block to the list of successors for the block with the try
2041 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00002042 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002043 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002044 if (!HasCatchAll) {
2045 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00002046 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002047 else
Mike Stump845384a2010-01-20 01:30:58 +00002048 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002049 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002050
2051 // The code after the try is the implicit successor.
2052 Succ = TrySuccessor;
2053
Mike Stump845384a2010-01-20 01:30:58 +00002054 // Save the current "try" context.
2055 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2056 TryTerminatedBlock = NewTryTerminatedBlock;
2057
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002058 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002059 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002060 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002061 return Block;
2062}
2063
2064CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2065 // CXXCatchStmt are treated like labels, so they are the first statement in a
2066 // block.
2067
2068 if (CS->getHandlerBlock())
2069 addStmt(CS->getHandlerBlock());
2070
2071 CFGBlock* CatchBlock = Block;
2072 if (!CatchBlock)
2073 CatchBlock = createBlock();
2074
2075 CatchBlock->setLabel(CS);
2076
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002077 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002078 return 0;
2079
2080 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2081 Block = NULL;
2082
2083 return CatchBlock;
2084}
2085
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002086CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002087 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002088 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002089 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00002090 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002091 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002092 return VisitChildren(C);
2093}
2094
Ted Kremenekeda180e22007-08-28 19:26:49 +00002095CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002096 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002097 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002098
Ted Kremenekeda180e22007-08-28 19:26:49 +00002099 if (!IBlock) {
2100 IBlock = createBlock(false);
2101 cfg->setIndirectGotoBlock(IBlock);
2102 }
Mike Stump31feda52009-07-17 01:31:16 +00002103
Ted Kremenekeda180e22007-08-28 19:26:49 +00002104 // IndirectGoto is a control-flow statement. Thus we stop processing the
2105 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002106 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002107 return 0;
2108
Ted Kremenekeda180e22007-08-28 19:26:49 +00002109 Block = createBlock(false);
2110 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002111 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002112 return addStmt(I->getTarget());
2113}
2114
Ted Kremenek04cca642007-08-23 21:26:19 +00002115} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002116
Mike Stump31feda52009-07-17 01:31:16 +00002117/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2118/// no successors or predecessors. If this is the first block created in the
2119/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002120CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002121 bool first_block = begin() == end();
2122
2123 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002124 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2125 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2126 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002127
2128 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002129 if (first_block)
2130 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002131
2132 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002133 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002134}
2135
Ted Kremenek889073f2007-08-23 16:51:22 +00002136/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2137/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002138CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002139 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002140 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002141 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002142}
2143
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002144//===----------------------------------------------------------------------===//
2145// CFG: Queries for BlkExprs.
2146//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002147
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002148namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002149 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002150}
2151
Ted Kremenekbff98442009-12-23 23:37:10 +00002152static void FindSubExprAssignments(Stmt *S,
2153 llvm::SmallPtrSet<Expr*,50>& Set) {
2154 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002155 return;
Mike Stump31feda52009-07-17 01:31:16 +00002156
Ted Kremenekbff98442009-12-23 23:37:10 +00002157 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002158 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002159 if (!child)
2160 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002161
Ted Kremenekbff98442009-12-23 23:37:10 +00002162 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002163 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002164
Ted Kremenekbff98442009-12-23 23:37:10 +00002165 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002166 }
2167}
2168
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002169static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2170 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002171
2172 // Look for assignments that are used as subexpressions. These are the only
2173 // assignments that we want to *possibly* register as a block-level
2174 // expression. Basically, if an assignment occurs both in a subexpression and
2175 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002176 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002177
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002178 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002179 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002180 if (CFGStmt S = BI->getAs<CFGStmt>())
2181 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002182
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002183 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002184
2185 // Iterate over the statements again on identify the Expr* and Stmt* at the
2186 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002187
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002188 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2189 CFGStmt CS = BI->getAs<CFGStmt>();
2190 if (!CS.isValid())
2191 continue;
2192 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002193
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002194 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002195 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002196 // expression are really "statements" whose value is never used by
2197 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002198 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002199 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002200 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2201 // Special handling for statement expressions. The last statement in
2202 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002203 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002204 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002205 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002206 (*M)[C->body_back()] = x;
2207 }
2208 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002209
Ted Kremenek95a123c2008-01-26 00:03:27 +00002210 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002211 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002212 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002213 }
Mike Stump31feda52009-07-17 01:31:16 +00002214
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002215 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002216
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002217 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002218
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002219 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002220 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002221 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002222 }
2223 }
Mike Stump31feda52009-07-17 01:31:16 +00002224
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002225 return M;
2226}
2227
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002228CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2229 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002230 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002231
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002232 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002233 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002234 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002235}
2236
2237unsigned CFG::getNumBlkExprs() {
2238 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2239 return M->size();
2240 else {
2241 // We assume callers interested in the number of BlkExprs will want
2242 // the map constructed if it doesn't already exist.
2243 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2244 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2245 }
2246}
2247
Ted Kremenek6065ef62008-04-28 18:00:46 +00002248//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002249// Filtered walking of the CFG.
2250//===----------------------------------------------------------------------===//
2251
2252bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002253 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002254
2255 if (F.IgnoreDefaultsWithCoveredEnums) {
2256 // If the 'To' has no label or is labeled but the label isn't a
2257 // CaseStmt then filter this edge.
2258 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00002259 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002260 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002261 const Stmt *L = To->getLabel();
2262 if (!L || !isa<CaseStmt>(L))
2263 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002264 }
2265 }
2266 }
2267
2268 return false;
2269}
2270
2271//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002272// Cleanup: CFG dstor.
2273//===----------------------------------------------------------------------===//
2274
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002275CFG::~CFG() {
2276 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2277}
Mike Stump31feda52009-07-17 01:31:16 +00002278
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002279//===----------------------------------------------------------------------===//
2280// CFG pretty printing
2281//===----------------------------------------------------------------------===//
2282
Ted Kremenek7e776b12007-08-22 18:22:34 +00002283namespace {
2284
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002285class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002286 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002287 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002288 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002289 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002290 signed CurrentBlock;
2291 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002292 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002293public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002294
Chris Lattnerc61089a2009-06-30 01:26:17 +00002295 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2296 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002297 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2298 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002299 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002300 BI != BEnd; ++BI, ++j ) {
2301 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2302 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2303 StmtMap[SE] = P;
2304
2305 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2306 DeclMap[DS->getSingleDecl()] = P;
2307
2308 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2309 if (VarDecl* VD = IS->getConditionVariable())
2310 DeclMap[VD] = P;
2311
2312 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2313 if (VarDecl* VD = FS->getConditionVariable())
2314 DeclMap[VD] = P;
2315
2316 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2317 if (VarDecl* VD = WS->getConditionVariable())
2318 DeclMap[VD] = P;
2319
2320 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2321 if (VarDecl* VD = SS->getConditionVariable())
2322 DeclMap[VD] = P;
2323
2324 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2325 if (VarDecl* VD = CS->getExceptionDecl())
2326 DeclMap[VD] = P;
2327 }
2328 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002329 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002330 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002331 }
Mike Stump31feda52009-07-17 01:31:16 +00002332
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002333 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002334
Chris Lattnerc61089a2009-06-30 01:26:17 +00002335 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002336 void setBlockID(signed i) { CurrentBlock = i; }
2337 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002338
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002339 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2340 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002341
2342 if (I == StmtMap.end())
2343 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002344
2345 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002346 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002347 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002348 }
Mike Stump31feda52009-07-17 01:31:16 +00002349
Ted Kremenek60983dc2010-01-19 20:52:05 +00002350 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002351 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002352 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002353
2354 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2355 DeclMapTy::iterator I = DeclMap.find(D);
2356
2357 if (I == DeclMap.end())
2358 return false;
2359
2360 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2361 && I->second.second == CurrentStmt) {
2362 return false;
2363 }
2364
2365 OS << "[B" << I->second.first << "." << I->second.second << "]";
2366 return true;
2367 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002368};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002369} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002370
Chris Lattnerc61089a2009-06-30 01:26:17 +00002371
2372namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002373class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002374 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002375
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002376 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002377 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002378 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002379public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002380 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002381 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002382 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002383
Ted Kremenek9aae5132007-08-23 21:42:29 +00002384 void VisitIfStmt(IfStmt* I) {
2385 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002386 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002387 }
Mike Stump31feda52009-07-17 01:31:16 +00002388
Ted Kremenek9aae5132007-08-23 21:42:29 +00002389 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002390 void VisitStmt(Stmt* Terminator) {
2391 Terminator->printPretty(OS, Helper, Policy);
2392 }
2393
Ted Kremenek9aae5132007-08-23 21:42:29 +00002394 void VisitForStmt(ForStmt* F) {
2395 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002396 if (F->getInit())
2397 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002398 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002399 if (Stmt* C = F->getCond())
2400 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002401 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002402 if (F->getInc())
2403 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002404 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002405 }
Mike Stump31feda52009-07-17 01:31:16 +00002406
Ted Kremenek9aae5132007-08-23 21:42:29 +00002407 void VisitWhileStmt(WhileStmt* W) {
2408 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002409 if (Stmt* C = W->getCond())
2410 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002411 }
Mike Stump31feda52009-07-17 01:31:16 +00002412
Ted Kremenek9aae5132007-08-23 21:42:29 +00002413 void VisitDoStmt(DoStmt* D) {
2414 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002415 if (Stmt* C = D->getCond())
2416 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002417 }
Mike Stump31feda52009-07-17 01:31:16 +00002418
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002419 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002420 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002421 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002422 }
Mike Stump31feda52009-07-17 01:31:16 +00002423
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002424 void VisitCXXTryStmt(CXXTryStmt* CS) {
2425 OS << "try ...";
2426 }
2427
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002428 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002429 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002430 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002431 }
Mike Stump31feda52009-07-17 01:31:16 +00002432
Ted Kremenek391f94a2007-08-31 22:29:13 +00002433 void VisitChooseExpr(ChooseExpr* C) {
2434 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002435 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002436 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002437 }
Mike Stump31feda52009-07-17 01:31:16 +00002438
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002439 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2440 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002441 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002442 }
Mike Stump31feda52009-07-17 01:31:16 +00002443
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002444 void VisitBinaryOperator(BinaryOperator* B) {
2445 if (!B->isLogicalOp()) {
2446 VisitExpr(B);
2447 return;
2448 }
Mike Stump31feda52009-07-17 01:31:16 +00002449
Douglas Gregor7de59662009-05-29 20:38:28 +00002450 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002451
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002452 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002453 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002454 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002455 return;
John McCalle3027922010-08-25 11:45:40 +00002456 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002457 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002458 return;
2459 default:
2460 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002461 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002462 }
Mike Stump31feda52009-07-17 01:31:16 +00002463
Ted Kremenek12687ff2007-08-27 21:54:41 +00002464 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002465 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002466 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002467};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002468} // end anonymous namespace
2469
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002470static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002471 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002472 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2473 Stmt *S = CS;
2474
2475 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002476
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002477 // special printing for statement-expressions.
2478 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2479 CompoundStmt* Sub = SE->getSubStmt();
2480
2481 if (Sub->child_begin() != Sub->child_end()) {
2482 OS << "({ ... ; ";
2483 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2484 OS << " })\n";
2485 return;
2486 }
2487 }
2488 // special printing for comma expressions.
2489 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2490 if (B->getOpcode() == BO_Comma) {
2491 OS << "... , ";
2492 Helper->handledStmt(B->getRHS(),OS);
2493 OS << '\n';
2494 return;
2495 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002496 }
2497 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002498 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002499
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002500 if (isa<CXXOperatorCallExpr>(S)) {
2501 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002502 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002503 else if (isa<CXXBindTemporaryExpr>(S)) {
2504 OS << " (BindTemporary)";
2505 }
Mike Stump31feda52009-07-17 01:31:16 +00002506
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002507 // Expressions need a newline.
2508 if (isa<Expr>(S))
2509 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002510
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002511 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2512 CXXBaseOrMemberInitializer* I = IE;
2513 if (I->isBaseInitializer())
2514 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2515 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002516
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002517 OS << "(";
2518 if (Expr* IE = I->getInit())
2519 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2520 OS << ")";
2521
2522 if (I->isBaseInitializer())
2523 OS << " (Base initializer)\n";
2524 else OS << " (Member initializer)\n";
2525
2526 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2527 VarDecl* VD = DE.getVarDecl();
2528 Helper->handleDecl(VD, OS);
2529
2530 Type* T = VD->getType().getTypePtr();
2531 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2532 T = RT->getPointeeType().getTypePtr();
2533
2534 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2535 OS << " (Implicit destructor)\n";
2536 }
2537 }
Mike Stump31feda52009-07-17 01:31:16 +00002538
Chris Lattnerc61089a2009-06-30 01:26:17 +00002539static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2540 const CFGBlock& B,
2541 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002542
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002543 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002544
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002545 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002546 OS << "\n [ B" << B.getBlockID();
2547
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002548 if (&B == &cfg->getEntry())
2549 OS << " (ENTRY) ]\n";
2550 else if (&B == &cfg->getExit())
2551 OS << " (EXIT) ]\n";
2552 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002553 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002554 else
2555 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002556
Ted Kremenek71eca012007-08-29 23:20:49 +00002557 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002558 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002559
2560 if (print_edges)
2561 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002562
Mike Stump92244b02010-01-19 22:00:14 +00002563 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002564 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002565 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002566 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002567 C->getLHS()->printPretty(OS, Helper,
2568 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002569 if (C->getRHS()) {
2570 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002571 C->getRHS()->printPretty(OS, Helper,
2572 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002573 }
Mike Stump92244b02010-01-19 22:00:14 +00002574 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002575 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002576 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002577 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002578 if (CS->getExceptionDecl())
2579 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2580 0);
2581 else
2582 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002583 OS << ")";
2584
2585 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002586 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002587
Ted Kremenek71eca012007-08-29 23:20:49 +00002588 OS << ":\n";
2589 }
Mike Stump31feda52009-07-17 01:31:16 +00002590
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002591 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002592 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002593
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002594 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2595 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002596
Ted Kremenek71eca012007-08-29 23:20:49 +00002597 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002598 if (print_edges)
2599 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002600
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002601 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002602
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002603 if (Helper)
2604 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002605
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002606 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002607 }
Mike Stump31feda52009-07-17 01:31:16 +00002608
Ted Kremenek71eca012007-08-29 23:20:49 +00002609 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002610 if (B.getTerminator()) {
2611 if (print_edges)
2612 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002613
Ted Kremenek71eca012007-08-29 23:20:49 +00002614 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002615
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002616 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002617
Chris Lattnerc61089a2009-06-30 01:26:17 +00002618 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2619 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002620 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002621 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002622 }
Mike Stump31feda52009-07-17 01:31:16 +00002623
Ted Kremenek71eca012007-08-29 23:20:49 +00002624 if (print_edges) {
2625 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002626 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002627 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002628
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002629 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2630 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002631
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002632 if (i == 8 || (i-8) == 0)
2633 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002634
Ted Kremenek71eca012007-08-29 23:20:49 +00002635 OS << " B" << (*I)->getBlockID();
2636 }
Mike Stump31feda52009-07-17 01:31:16 +00002637
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002638 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002639
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002640 // Print the successors of this block.
2641 OS << " Successors (" << B.succ_size() << "):";
2642 i = 0;
2643
2644 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2645 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002646
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002647 if (i == 8 || (i-8) % 10 == 0)
2648 OS << "\n ";
2649
Mike Stump0d76d072009-07-20 23:24:15 +00002650 if (*I)
2651 OS << " B" << (*I)->getBlockID();
2652 else
2653 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002654 }
Mike Stump31feda52009-07-17 01:31:16 +00002655
Ted Kremenek71eca012007-08-29 23:20:49 +00002656 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002657 }
Mike Stump31feda52009-07-17 01:31:16 +00002658}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002659
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002660
2661/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002662void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002663
2664/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002665void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2666 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002667
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002668 // Print the entry block.
2669 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002670
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002671 // Iterate through the CFGBlocks and print them one by one.
2672 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2673 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002674 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002675 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002676
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002677 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002678 }
Mike Stump31feda52009-07-17 01:31:16 +00002679
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002680 // Print the exit block.
2681 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002682 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002683}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002684
2685/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002686void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2687 print(llvm::errs(), cfg, LO);
2688}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002689
2690/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2691/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002692void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2693 const LangOptions &LO) const {
2694 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002695 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002696}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002697
Ted Kremenek15647632008-01-30 23:02:42 +00002698/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002699void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002700 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002701 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002702 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2703}
2704
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002705Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002706
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002707 if (!Terminator)
2708 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002709
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002710 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002711
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002712 switch (Terminator->getStmtClass()) {
2713 default:
2714 break;
Mike Stump31feda52009-07-17 01:31:16 +00002715
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002716 case Stmt::ForStmtClass:
2717 E = cast<ForStmt>(Terminator)->getCond();
2718 break;
Mike Stump31feda52009-07-17 01:31:16 +00002719
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002720 case Stmt::WhileStmtClass:
2721 E = cast<WhileStmt>(Terminator)->getCond();
2722 break;
Mike Stump31feda52009-07-17 01:31:16 +00002723
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002724 case Stmt::DoStmtClass:
2725 E = cast<DoStmt>(Terminator)->getCond();
2726 break;
Mike Stump31feda52009-07-17 01:31:16 +00002727
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002728 case Stmt::IfStmtClass:
2729 E = cast<IfStmt>(Terminator)->getCond();
2730 break;
Mike Stump31feda52009-07-17 01:31:16 +00002731
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002732 case Stmt::ChooseExprClass:
2733 E = cast<ChooseExpr>(Terminator)->getCond();
2734 break;
Mike Stump31feda52009-07-17 01:31:16 +00002735
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002736 case Stmt::IndirectGotoStmtClass:
2737 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2738 break;
Mike Stump31feda52009-07-17 01:31:16 +00002739
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002740 case Stmt::SwitchStmtClass:
2741 E = cast<SwitchStmt>(Terminator)->getCond();
2742 break;
Mike Stump31feda52009-07-17 01:31:16 +00002743
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002744 case Stmt::ConditionalOperatorClass:
2745 E = cast<ConditionalOperator>(Terminator)->getCond();
2746 break;
Mike Stump31feda52009-07-17 01:31:16 +00002747
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002748 case Stmt::BinaryOperatorClass: // '&&' and '||'
2749 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002750 break;
Mike Stump31feda52009-07-17 01:31:16 +00002751
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002752 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002753 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002754 }
Mike Stump31feda52009-07-17 01:31:16 +00002755
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002756 return E ? E->IgnoreParens() : NULL;
2757}
2758
Ted Kremenek92137a32008-05-16 16:06:00 +00002759bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002760
Ted Kremenek92137a32008-05-16 16:06:00 +00002761 if (!Terminator)
2762 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002763
Ted Kremenek92137a32008-05-16 16:06:00 +00002764 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002765
Ted Kremenek92137a32008-05-16 16:06:00 +00002766 switch (Terminator->getStmtClass()) {
2767 default:
2768 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002769
2770 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002771 case Stmt::WhileStmtClass:
2772 case Stmt::DoStmtClass:
2773 case Stmt::IfStmtClass:
2774 case Stmt::ChooseExprClass:
2775 case Stmt::ConditionalOperatorClass:
2776 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002777 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002778 }
Mike Stump31feda52009-07-17 01:31:16 +00002779
Ted Kremenek92137a32008-05-16 16:06:00 +00002780 return E ? E->IgnoreParens() : NULL;
2781}
2782
Ted Kremenek15647632008-01-30 23:02:42 +00002783
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002784//===----------------------------------------------------------------------===//
2785// CFG Graphviz Visualization
2786//===----------------------------------------------------------------------===//
2787
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002788
2789#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002790static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002791#endif
2792
Chris Lattnerc61089a2009-06-30 01:26:17 +00002793void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002794#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002795 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002796 GraphHelper = &H;
2797 llvm::ViewGraph(this,"CFG");
2798 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002799#endif
2800}
2801
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002802namespace llvm {
2803template<>
2804struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002805
2806 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2807
2808 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002809
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002810#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002811 std::string OutSStr;
2812 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002813 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002814 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002815
2816 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2817
2818 // Process string output to make it nicer...
2819 for (unsigned i = 0; i != OutStr.length(); ++i)
2820 if (OutStr[i] == '\n') { // Left justify
2821 OutStr[i] = '\\';
2822 OutStr.insert(OutStr.begin()+i+1, 'l');
2823 }
Mike Stump31feda52009-07-17 01:31:16 +00002824
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002825 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002826#else
2827 return "";
2828#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002829 }
2830};
2831} // end namespace llvm