blob: 527d0eb870e69e798af58224888e2e0c477c3aa3 [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 Swiderski87b1bb62010-10-04 03:38:22 +0000307 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000308 void addAutomaticObjDtors(LocalScope::const_iterator B,
309 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000310 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000311
Marcin Swiderski5e415732010-09-30 23:05:00 +0000312 // Local scopes creation.
313 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
314
Zhongxing Xu81714f22010-10-01 03:00:16 +0000315 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000316 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
317 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
318
319 void addLocalScopeAndDtors(Stmt* S);
320
321 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000322 void AppendStmt(CFGBlock *B, Stmt *S,
323 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
324 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000325 }
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000326 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
327 B->appendInitializer(I, cfg->getBumpVectorContext());
328 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000329 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
330 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
331 }
332 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
333 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
334 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000335
Marcin Swiderski321a7072010-09-30 22:54:37 +0000336 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
337 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
338 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
339 LocalScope::const_iterator E, Stmt* S);
340 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
341 LocalScope::const_iterator B, LocalScope::const_iterator E);
342
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000343 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
344 B->addSuccessor(S, cfg->getBumpVectorContext());
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek963cc312009-07-24 06:55:42 +0000347 /// TryResult - a class representing a variant over the values
348 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
349 /// and is used by the CFGBuilder to decide if a branch condition
350 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000351 class TryResult {
352 int X;
353 public:
354 TryResult(bool b) : X(b ? 1 : 0) {}
355 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000356
Ted Kremenek30754282009-07-24 04:47:11 +0000357 bool isTrue() const { return X == 1; }
358 bool isFalse() const { return X == 0; }
359 bool isKnown() const { return X >= 0; }
360 void negate() {
361 assert(isKnown());
362 X ^= 0x1;
363 }
364 };
Mike Stump11289f42009-09-09 15:08:12 +0000365
Mike Stump773582d2009-07-23 23:25:26 +0000366 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
367 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000368 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000369 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000370 return TryResult();
371
Mike Stump773582d2009-07-23 23:25:26 +0000372 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000373 if (!S->isTypeDependent() && !S->isValueDependent() &&
374 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000375 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000376
377 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000378 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000379};
Mike Stump31feda52009-07-17 01:31:16 +0000380
Douglas Gregor4619e432008-12-05 23:32:09 +0000381// FIXME: Add support for dependent-sized array types in C++?
382// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000383static VariableArrayType* FindVA(Type* t) {
384 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
385 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
386 if (vat->getSizeExpr())
387 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000388
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000389 t = vt->getElementType().getTypePtr();
390 }
Mike Stump31feda52009-07-17 01:31:16 +0000391
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000392 return 0;
393}
Mike Stump31feda52009-07-17 01:31:16 +0000394
395/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
396/// arbitrary statement. Examples include a single expression or a function
397/// body (compound statement). The ownership of the returned CFG is
398/// transferred to the caller. If CFG construction fails, this method returns
399/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000400CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000401 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000402
Mike Stump0d76d072009-07-20 23:24:15 +0000403 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000404 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000405 if (!Statement)
406 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000407
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000408 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000409
410 // Create an empty block that will serve as the exit block for the CFG. Since
411 // this is the first block added to the CFG, it will be implicitly registered
412 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000413 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000414 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000415 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000416
Marcin Swiderski20b88732010-10-05 05:37:00 +0000417 if (BuildOpts.AddImplicitDtors)
418 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
419 addImplicitDtorsForDestructor(DD);
420
Ted Kremenek9aae5132007-08-23 21:42:29 +0000421 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000422 CFGBlock *B = addStmt(Statement);
423
424 if (badCFG)
425 return NULL;
426
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000427 // For C++ constructor add initializers to CFG.
428 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
429 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
430 E = CD->init_rend(); I != E; ++I) {
431 B = addInitializer(*I);
432 if (badCFG)
433 return NULL;
434 }
435 }
436
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000437 if (B)
438 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000439
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000440 // Backpatch the gotos whose label -> block mappings we didn't know when we
441 // encountered them.
442 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
443 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000444
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000445 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000446 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
447 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000448
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000449 // If there is no target for the goto, then we are looking at an
450 // incomplete AST. Handle this by not registering a successor.
451 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000452
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000453 JumpTarget JT = LI->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000454 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000455 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000456 }
457
458 // Add successors to the Indirect Goto Dispatch block (if we have one).
459 if (CFGBlock* B = cfg->getIndirectGotoBlock())
460 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
461 E = AddressTakenLabels.end(); I != E; ++I ) {
462
463 // Lookup the target block.
464 LabelMapTy::iterator LI = LabelMap.find(*I);
465
466 // If there is no target block that contains label, then we are looking
467 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000468 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000469
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000470 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000471 }
Mike Stump31feda52009-07-17 01:31:16 +0000472
Mike Stump31feda52009-07-17 01:31:16 +0000473 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000474 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000475
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000476 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000477}
Mike Stump31feda52009-07-17 01:31:16 +0000478
Ted Kremenek9aae5132007-08-23 21:42:29 +0000479/// createBlock - Used to lazily create blocks that are connected
480/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000481CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000482 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000483 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000484 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000485 return B;
486}
Mike Stump31feda52009-07-17 01:31:16 +0000487
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000488/// addInitializer - Add C++ base or member initializer element to CFG.
489CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
490 if (!BuildOpts.AddInitializers)
491 return Block;
492
493 autoCreateBlock();
494 appendInitializer(Block, I);
495
496 if (Expr *Init = I->getInit()) {
497 AddStmtChoice::Kind K = AddStmtChoice::NotAlwaysAdd;
498 if (FieldDecl *FD = I->getMember())
499 if (FD->getType()->isReferenceType())
500 K = AddStmtChoice::AsLValueNotAlwaysAdd;
501
502 return Visit(Init, AddStmtChoice(K));
503 }
504
505 return Block;
506}
507
Marcin Swiderski5e415732010-09-30 23:05:00 +0000508/// addAutomaticObjDtors - Add to current block automatic objects destructors
509/// for objects in range of local scope positions. Use S as trigger statement
510/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000511void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
512 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000513 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000514 return;
515
Marcin Swiderski5e415732010-09-30 23:05:00 +0000516 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000517 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000518
519 autoCreateBlock();
520 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000521}
522
Marcin Swiderski20b88732010-10-05 05:37:00 +0000523/// addImplicitDtorsForDestructor - Add implicit destructors generated for
524/// base and member objects in destructor.
525void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
526 assert (BuildOpts.AddImplicitDtors
527 && "Can be called only when dtors should be added");
528 const CXXRecordDecl *RD = DD->getParent();
529
530 // At the end destroy virtual base objects.
531 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
532 VE = RD->vbases_end(); VI != VE; ++VI) {
533 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
534 if (!CD->hasTrivialDestructor()) {
535 autoCreateBlock();
536 appendBaseDtor(Block, VI);
537 }
538 }
539
540 // Before virtual bases destroy direct base objects.
541 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
542 BE = RD->bases_end(); BI != BE; ++BI) {
543 if (!BI->isVirtual()) {
544 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
545 if (!CD->hasTrivialDestructor()) {
546 autoCreateBlock();
547 appendBaseDtor(Block, BI);
548 }
549 }
550 }
551
552 // First destroy member objects.
553 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
554 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000555 // Check for constant size array. Set type to array element type.
556 QualType QT = FI->getType();
557 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
558 if (AT->getSize() == 0)
559 continue;
560 QT = AT->getElementType();
561 }
562
563 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000564 if (!CD->hasTrivialDestructor()) {
565 autoCreateBlock();
566 appendMemberDtor(Block, *FI);
567 }
568 }
569}
570
Marcin Swiderski5e415732010-09-30 23:05:00 +0000571/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
572/// way return valid LocalScope object.
573LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
574 if (!Scope) {
575 Scope = cfg->getAllocator().Allocate<LocalScope>();
576 new (Scope) LocalScope(ScopePos);
577 }
578 return Scope;
579}
580
581/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000582/// that should create implicit scope (e.g. if/else substatements).
583void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000584 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000585 return;
586
587 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000588
589 // For compound statement we will be creating explicit scope.
590 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
591 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
592 ; BI != BE; ++BI) {
593 Stmt* SI = *BI;
594 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
595 SI = LS->getSubStmt();
596 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
597 Scope = addLocalScopeForDeclStmt(DS, Scope);
598 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000599 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000600 }
601
602 // For any other statement scope will be implicit and as such will be
603 // interesting only for DeclStmt.
604 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
605 S = LS->getSubStmt();
606 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000607 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000608}
609
610/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
611/// reuse Scope if not NULL.
612LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000613 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000614 if (!BuildOpts.AddImplicitDtors)
615 return Scope;
616
617 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
618 ; DI != DE; ++DI) {
619 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
620 Scope = addLocalScopeForVarDecl(VD, Scope);
621 }
622 return Scope;
623}
624
625/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
626/// create add scope for automatic objects and temporary objects bound to
627/// const reference. Will reuse Scope if not NULL.
628LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000629 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000630 if (!BuildOpts.AddImplicitDtors)
631 return Scope;
632
633 // Check if variable is local.
634 switch (VD->getStorageClass()) {
635 case SC_None:
636 case SC_Auto:
637 case SC_Register:
638 break;
639 default: return Scope;
640 }
641
642 // Check for const references bound to temporary. Set type to pointee.
643 QualType QT = VD->getType();
644 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
645 QT = RT->getPointeeType();
646 if (!QT.isConstQualified())
647 return Scope;
648 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
649 return Scope;
650 }
651
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000652 // Check for constant size array. Set type to array element type.
653 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
654 if (AT->getSize() == 0)
655 return Scope;
656 QT = AT->getElementType();
657 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000658
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000659 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000660 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
661 if (!CD->hasTrivialDestructor()) {
662 // Add the variable to scope
663 Scope = createOrReuseLocalScope(Scope);
664 Scope->addVar(VD);
665 ScopePos = Scope->begin();
666 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000667 return Scope;
668}
669
670/// addLocalScopeAndDtors - For given statement add local scope for it and
671/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
672void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
673 if (!BuildOpts.AddImplicitDtors)
674 return;
675
676 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000677 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000678 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
679}
680
Marcin Swiderski321a7072010-09-30 22:54:37 +0000681/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
682/// automatic storage duration to CFGBlock's elements vector. Insertion will be
683/// performed in place specified with iterator.
684void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
685 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
686 BumpVectorContext& C = cfg->getBumpVectorContext();
687 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
688 while (B != E)
689 I = Blk->insertAutomaticObjDtor(I, *B++, S);
690}
691
692/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
693/// automatic storage duration to CFGBlock's elements vector. Elements will be
694/// appended to physical end of the vector which happens to be logical
695/// beginning.
696void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
697 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
698 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
699}
700
701/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
702/// variables with automatic storage duration to CFGBlock's elements vector.
703/// Elements will be prepended to physical beginning of the vector which
704/// happens to be logical end. Use blocks terminator as statement that specifies
705/// destructors call site.
706void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
707 LocalScope::const_iterator B, LocalScope::const_iterator E) {
708 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
709}
710
Ted Kremenek93668002009-07-17 22:18:43 +0000711/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000712/// blocks for ternary operators, &&, and ||. We also process "," and
713/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000714CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000715tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000716 if (!S) {
717 badCFG = true;
718 return 0;
719 }
Ted Kremenek93668002009-07-17 22:18:43 +0000720 switch (S->getStmtClass()) {
721 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000722 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000723
724 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000725 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Ted Kremenek93668002009-07-17 22:18:43 +0000727 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000728 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000729
Ted Kremenek93668002009-07-17 22:18:43 +0000730 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000731 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000732
Ted Kremenek93668002009-07-17 22:18:43 +0000733 case Stmt::BreakStmtClass:
734 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000735
Ted Kremenek93668002009-07-17 22:18:43 +0000736 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000737 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000738 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Ted Kremenek93668002009-07-17 22:18:43 +0000740 case Stmt::CaseStmtClass:
741 return VisitCaseStmt(cast<CaseStmt>(S));
742
743 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000744 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000745
Ted Kremenek93668002009-07-17 22:18:43 +0000746 case Stmt::CompoundStmtClass:
747 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000748
Ted Kremenek93668002009-07-17 22:18:43 +0000749 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000750 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000751
Ted Kremenek93668002009-07-17 22:18:43 +0000752 case Stmt::ContinueStmtClass:
753 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000754
Ted Kremenekb27378c2010-01-19 20:40:33 +0000755 case Stmt::CXXCatchStmtClass:
756 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
757
Ted Kremenek82bfc862010-08-28 00:19:02 +0000758 case Stmt::CXXExprWithTemporariesClass: {
759 // FIXME: Handle temporaries. For now, just visit the subexpression
760 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000761 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000762 }
763
Zhongxing Xu7e612172010-04-13 09:38:01 +0000764 case Stmt::CXXMemberCallExprClass:
765 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
766
Ted Kremenekb27378c2010-01-19 20:40:33 +0000767 case Stmt::CXXThrowExprClass:
768 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000769
Ted Kremenekb27378c2010-01-19 20:40:33 +0000770 case Stmt::CXXTryStmtClass:
771 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000772
Ted Kremenek93668002009-07-17 22:18:43 +0000773 case Stmt::DeclStmtClass:
774 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ted Kremenek93668002009-07-17 22:18:43 +0000776 case Stmt::DefaultStmtClass:
777 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000778
Ted Kremenek93668002009-07-17 22:18:43 +0000779 case Stmt::DoStmtClass:
780 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000781
Ted Kremenek93668002009-07-17 22:18:43 +0000782 case Stmt::ForStmtClass:
783 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000784
Ted Kremenek93668002009-07-17 22:18:43 +0000785 case Stmt::GotoStmtClass:
786 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000787
Ted Kremenek93668002009-07-17 22:18:43 +0000788 case Stmt::IfStmtClass:
789 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 case Stmt::IndirectGotoStmtClass:
792 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ted Kremenek93668002009-07-17 22:18:43 +0000794 case Stmt::LabelStmtClass:
795 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000796
Ted Kremenek5868ec62010-04-11 17:02:10 +0000797 case Stmt::MemberExprClass:
798 return VisitMemberExpr(cast<MemberExpr>(S), asc);
799
Ted Kremenek93668002009-07-17 22:18:43 +0000800 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000801 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
802
Ted Kremenek93668002009-07-17 22:18:43 +0000803 case Stmt::ObjCAtSynchronizedStmtClass:
804 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000805
Ted Kremenek93668002009-07-17 22:18:43 +0000806 case Stmt::ObjCAtThrowStmtClass:
807 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000808
Ted Kremenek93668002009-07-17 22:18:43 +0000809 case Stmt::ObjCAtTryStmtClass:
810 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000811
Ted Kremenek93668002009-07-17 22:18:43 +0000812 case Stmt::ObjCForCollectionStmtClass:
813 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek93668002009-07-17 22:18:43 +0000815 case Stmt::ParenExprClass:
816 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000817 goto tryAgain;
818
Ted Kremenek93668002009-07-17 22:18:43 +0000819 case Stmt::NullStmtClass:
820 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000821
Ted Kremenek93668002009-07-17 22:18:43 +0000822 case Stmt::ReturnStmtClass:
823 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek93668002009-07-17 22:18:43 +0000825 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000826 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000827
Ted Kremenek93668002009-07-17 22:18:43 +0000828 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000829 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000830
Ted Kremenek93668002009-07-17 22:18:43 +0000831 case Stmt::SwitchStmtClass:
832 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000833
Ted Kremenek93668002009-07-17 22:18:43 +0000834 case Stmt::WhileStmtClass:
835 return VisitWhileStmt(cast<WhileStmt>(S));
836 }
837}
Mike Stump11289f42009-09-09 15:08:12 +0000838
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000839CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
840 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000841 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000842 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000843 }
Mike Stump11289f42009-09-09 15:08:12 +0000844
Ted Kremenek93668002009-07-17 22:18:43 +0000845 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000846}
Mike Stump31feda52009-07-17 01:31:16 +0000847
Ted Kremenek93668002009-07-17 22:18:43 +0000848/// VisitChildren - Visit the children of a Stmt.
849CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
850 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000851 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000852 E = Terminator->child_end(); I != E; ++I) {
853 if (*I) B = Visit(*I);
854 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000855 return B;
856}
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000858CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
859 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000860 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000861
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000862 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000863 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000864 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000865 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000866
Ted Kremenek9aae5132007-08-23 21:42:29 +0000867 return Block;
868}
Mike Stump11289f42009-09-09 15:08:12 +0000869
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000870CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
871 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000872 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000873 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000874 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000875
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000876 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000877 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Ted Kremenek93668002009-07-17 22:18:43 +0000879 // create the block evaluating the LHS
880 CFGBlock* LHSBlock = createBlock(false);
881 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000882
Ted Kremenek93668002009-07-17 22:18:43 +0000883 // create the block evaluating the RHS
884 Succ = ConfluenceBlock;
885 Block = NULL;
886 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000887
888 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000889 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000890 return 0;
891 }
892 else {
893 // Create an empty block for cases where the RHS doesn't require
894 // any explicit statements in the CFG.
895 RHSBlock = createBlock();
896 }
Mike Stump11289f42009-09-09 15:08:12 +0000897
Mike Stump773582d2009-07-23 23:25:26 +0000898 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000899 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000900 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000901 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000902
Ted Kremenek93668002009-07-17 22:18:43 +0000903 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000904 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000905 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
906 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000907 } else {
John McCalle3027922010-08-25 11:45:40 +0000908 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000909 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
910 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
Ted Kremenek93668002009-07-17 22:18:43 +0000913 // Generate the blocks for evaluating the LHS.
914 Block = LHSBlock;
915 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000916 }
John McCalle3027922010-08-25 11:45:40 +0000917 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000918 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000919 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000920 addStmt(B->getRHS());
921 return addStmt(B->getLHS());
922 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000923 else if (B->isAssignmentOp()) {
924 if (asc.alwaysAdd()) {
925 autoCreateBlock();
926 AppendStmt(Block, B, asc);
927 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000928
Marcin Swiderski77232492010-10-24 08:21:40 +0000929 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
930 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
Marcin Swiderski77232492010-10-24 08:21:40 +0000933 if (asc.alwaysAdd()) {
934 autoCreateBlock();
935 AppendStmt(Block, B, asc);
936 }
937
Zhongxing Xud95ccd52010-10-27 03:23:10 +0000938 CFGBlock *RBlock = Visit(B->getRHS());
939 CFGBlock *LBlock = Visit(B->getLHS());
940 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
941 // containing a DoStmt, and the LHS doesn't create a new block, then we should
942 // return RBlock. Otherwise we'll incorrectly return NULL.
943 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000944}
945
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000946CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
947 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000948 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000949 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000950 }
951 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000952}
953
Ted Kremenek93668002009-07-17 22:18:43 +0000954CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
955 // "break" is a control-flow statement. Thus we stop processing the current
956 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000957 if (badCFG)
958 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000959
Ted Kremenek93668002009-07-17 22:18:43 +0000960 // Now create a new block that ends with the break statement.
961 Block = createBlock(false);
962 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000963
Ted Kremenek93668002009-07-17 22:18:43 +0000964 // If there is no target for the break, then we are looking at an incomplete
965 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000966 if (BreakJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000967 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000968 AddSuccessor(Block, BreakJumpTarget.Block);
969 } else
Ted Kremenek93668002009-07-17 22:18:43 +0000970 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000971
972
Ted Kremenek9aae5132007-08-23 21:42:29 +0000973 return Block;
974}
Mike Stump11289f42009-09-09 15:08:12 +0000975
Mike Stump04c68512010-01-21 15:20:48 +0000976static bool CanThrow(Expr *E) {
977 QualType Ty = E->getType();
978 if (Ty->isFunctionPointerType())
979 Ty = Ty->getAs<PointerType>()->getPointeeType();
980 else if (Ty->isBlockPointerType())
981 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000982
Mike Stump04c68512010-01-21 15:20:48 +0000983 const FunctionType *FT = Ty->getAs<FunctionType>();
984 if (FT) {
985 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
986 if (Proto->hasEmptyExceptionSpec())
987 return false;
988 }
989 return true;
990}
991
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000992CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000993 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000994 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000995 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000996 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000997 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000998
Mike Stump04c68512010-01-21 15:20:48 +0000999 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001000
1001 // Languages without exceptions are assumed to not throw.
1002 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001003 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001004 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001005 }
1006
1007 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001008 if (FD->hasAttr<NoReturnAttr>())
1009 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001010 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001011 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001012 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001013
Mike Stump04c68512010-01-21 15:20:48 +00001014 if (!CanThrow(C->getCallee()))
1015 AddEHEdge = false;
1016
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001017 if (!NoReturn && !AddEHEdge) {
1018 if (asc.asLValue())
1019 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1020 else
1021 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Mike Stump92244b02010-01-19 22:00:14 +00001024 if (Block) {
1025 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001026 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001027 return 0;
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Mike Stump92244b02010-01-19 22:00:14 +00001030 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001031 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001032
Mike Stump92244b02010-01-19 22:00:14 +00001033 if (NoReturn) {
1034 // Wire this to the exit block directly.
1035 AddSuccessor(Block, &cfg->getExit());
1036 }
Mike Stump04c68512010-01-21 15:20:48 +00001037 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001038 // Add exceptional edges.
1039 if (TryTerminatedBlock)
1040 AddSuccessor(Block, TryTerminatedBlock);
1041 else
1042 AddSuccessor(Block, &cfg->getExit());
1043 }
Mike Stump11289f42009-09-09 15:08:12 +00001044
Mike Stump8c5d7992009-07-25 21:26:53 +00001045 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001046}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001047
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001048CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1049 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001050 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001051 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001052 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001053 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001054
Ted Kremenek5868ec62010-04-11 17:02:10 +00001055 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1056 : AddStmtChoice::AlwaysAdd;
1057
Ted Kremenek21822592009-07-17 18:20:32 +00001058 Succ = ConfluenceBlock;
1059 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001060 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001061 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001062 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001063
Ted Kremenek21822592009-07-17 18:20:32 +00001064 Succ = ConfluenceBlock;
1065 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001066 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001067 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001068 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001069
Ted Kremenek21822592009-07-17 18:20:32 +00001070 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001071 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001072 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001073 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1074 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001075 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001076 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001077}
Mike Stump11289f42009-09-09 15:08:12 +00001078
1079
1080CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001081 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001082 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001083
1084 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1085 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001086 // If we hit a segment of code just containing ';' (NullStmts), we can
1087 // get a null block back. In such cases, just use the LastBlock
1088 if (CFGBlock *newBlock = addStmt(*I))
1089 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001090
Ted Kremenekce499c22009-08-27 23:16:26 +00001091 if (badCFG)
1092 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001093 }
Mike Stump92244b02010-01-19 22:00:14 +00001094
Ted Kremenek93668002009-07-17 22:18:43 +00001095 return LastBlock;
1096}
Mike Stump11289f42009-09-09 15:08:12 +00001097
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001098CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1099 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001100 // Create the confluence block that will "merge" the results of the ternary
1101 // expression.
1102 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001103 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001104 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001105 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001106
Ted Kremenek5868ec62010-04-11 17:02:10 +00001107 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1108 : AddStmtChoice::AlwaysAdd;
1109
Ted Kremenek51d40b02009-07-17 18:15:54 +00001110 // Create a block for the LHS expression if there is an LHS expression. A
1111 // GCC extension allows LHS to be NULL, causing the condition to be the
1112 // value that is returned instead.
1113 // e.g: x ?: y is shorthand for: x ? x : y;
1114 Succ = ConfluenceBlock;
1115 Block = NULL;
1116 CFGBlock* LHSBlock = NULL;
1117 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001118 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001119 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001120 return 0;
1121 Block = NULL;
1122 }
Mike Stump11289f42009-09-09 15:08:12 +00001123
Ted Kremenek51d40b02009-07-17 18:15:54 +00001124 // Create the block for the RHS expression.
1125 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001126 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001127 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001128 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001129
Ted Kremenek51d40b02009-07-17 18:15:54 +00001130 // Create the block that will contain the condition.
1131 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001132
Mike Stump773582d2009-07-23 23:25:26 +00001133 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001134 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001135 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001136 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001137 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001138 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001139 // If we know the condition is false, add NULL as the successor for
1140 // the block containing the condition. In this case, the confluence
1141 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001142 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001143 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001144 } else {
1145 // If we have no LHS expression, add the ConfluenceBlock as a direct
1146 // successor for the block containing the condition. Moreover, we need to
1147 // reverse the order of the predecessors in the ConfluenceBlock because
1148 // the RHSBlock will have been added to the succcessors already, and we
1149 // want the first predecessor to the the block containing the expression
1150 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001151 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +00001152 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +00001153 std::reverse(ConfluenceBlock->pred_begin(),
1154 ConfluenceBlock->pred_end());
1155 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001158 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001159 Block->setTerminator(C);
1160 return addStmt(C->getCond());
1161}
1162
Ted Kremenek93668002009-07-17 22:18:43 +00001163CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
1164 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001165
Ted Kremenek93668002009-07-17 22:18:43 +00001166 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001167 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001168 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +00001169 }
Mike Stump11289f42009-09-09 15:08:12 +00001170
Ted Kremenek93668002009-07-17 22:18:43 +00001171 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001172
Ted Kremenek93668002009-07-17 22:18:43 +00001173 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1174 typedef llvm::SmallVector<Decl*,10> BufTy;
1175 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001176
Ted Kremenek93668002009-07-17 22:18:43 +00001177 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1178 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1179 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1180 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001181
Ted Kremenek93668002009-07-17 22:18:43 +00001182 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1183 // automatically freed with the CFG.
1184 DeclGroupRef DG(*I);
1185 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001186 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001187 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001188
Ted Kremenek93668002009-07-17 22:18:43 +00001189 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001190 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001191 B = VisitDeclSubExpr(D);
1192 }
Mike Stump11289f42009-09-09 15:08:12 +00001193
1194 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001195}
Mike Stump11289f42009-09-09 15:08:12 +00001196
Ted Kremenek93668002009-07-17 22:18:43 +00001197/// VisitDeclSubExpr - Utility method to add block-level expressions for
1198/// initializers in Decls.
1199CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
1200 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +00001201
Ted Kremenek93668002009-07-17 22:18:43 +00001202 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +00001203
Ted Kremenek93668002009-07-17 22:18:43 +00001204 if (!VD)
1205 return Block;
Mike Stump11289f42009-09-09 15:08:12 +00001206
Ted Kremenek93668002009-07-17 22:18:43 +00001207 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +00001208
Ted Kremenek93668002009-07-17 22:18:43 +00001209 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +00001210 AddStmtChoice::Kind k =
1211 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
1212 : AddStmtChoice::NotAlwaysAdd;
1213 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +00001214 }
Mike Stump11289f42009-09-09 15:08:12 +00001215
Ted Kremenek93668002009-07-17 22:18:43 +00001216 // If the type of VD is a VLA, then we must process its size expressions.
1217 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1218 VA = FindVA(VA->getElementType().getTypePtr()))
1219 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001220
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001221 // Remove variable from local scope.
1222 if (ScopePos && VD == *ScopePos)
1223 ++ScopePos;
1224
Ted Kremenek93668002009-07-17 22:18:43 +00001225 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001226}
1227
1228CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001229 // We may see an if statement in the middle of a basic block, or it may be the
1230 // first statement we are processing. In either case, we create a new basic
1231 // block. First, we create the blocks for the then...else statements, and
1232 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001233 // middle of a block, we stop processing that block. That block is then the
1234 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001235
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001236 // Save local scope position because in case of condition variable ScopePos
1237 // won't be restored when traversing AST.
1238 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1239
1240 // Create local scope for possible condition variable.
1241 // Store scope position. Add implicit destructor.
1242 if (VarDecl* VD = I->getConditionVariable()) {
1243 LocalScope::const_iterator BeginScopePos = ScopePos;
1244 addLocalScopeForVarDecl(VD);
1245 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1246 }
1247
Mike Stump31feda52009-07-17 01:31:16 +00001248 // The block we were proccessing is now finished. Make it the successor
1249 // block.
1250 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001251 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001252 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001253 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001254 }
Mike Stump31feda52009-07-17 01:31:16 +00001255
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001256 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001257 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001258
Ted Kremenek9aae5132007-08-23 21:42:29 +00001259 if (Stmt* Else = I->getElse()) {
1260 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001261
Ted Kremenek9aae5132007-08-23 21:42:29 +00001262 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001263 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001264 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001265
1266 // If branch is not a compound statement create implicit scope
1267 // and add destructors.
1268 if (!isa<CompoundStmt>(Else))
1269 addLocalScopeAndDtors(Else);
1270
Ted Kremenek93668002009-07-17 22:18:43 +00001271 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001272
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001273 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1274 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001275 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001276 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001277 return 0;
1278 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001279 }
Mike Stump31feda52009-07-17 01:31:16 +00001280
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001281 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001282 CFGBlock* ThenBlock;
1283 {
1284 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001285 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001287 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001288
1289 // If branch is not a compound statement create implicit scope
1290 // and add destructors.
1291 if (!isa<CompoundStmt>(Then))
1292 addLocalScopeAndDtors(Then);
1293
Ted Kremenek93668002009-07-17 22:18:43 +00001294 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001295
Ted Kremenek1b379512009-04-01 03:52:47 +00001296 if (!ThenBlock) {
1297 // We can reach here if the "then" body has all NullStmts.
1298 // Create an empty block so we can distinguish between true and false
1299 // branches in path-sensitive analyses.
1300 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001301 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001302 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001303 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001304 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001305 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001306 }
1307
Mike Stump31feda52009-07-17 01:31:16 +00001308 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001309 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001310
Ted Kremenek9aae5132007-08-23 21:42:29 +00001311 // Set the terminator of the new block to the If statement.
1312 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001313
Mike Stump773582d2009-07-23 23:25:26 +00001314 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001315 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001316
Ted Kremenek9aae5132007-08-23 21:42:29 +00001317 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001318 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1319 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001320
1321 // Add the condition as the last statement in the new block. This may create
1322 // new blocks as the condition may contain control-flow. Any newly created
1323 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001324 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001325
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001326 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1327 // and the condition variable initialization to the CFG.
1328 if (VarDecl *VD = I->getConditionVariable()) {
1329 if (Expr *Init = VD->getInit()) {
1330 autoCreateBlock();
1331 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1332 addStmt(Init);
1333 }
1334 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001335
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001336 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001337}
Mike Stump31feda52009-07-17 01:31:16 +00001338
1339
Ted Kremenek9aae5132007-08-23 21:42:29 +00001340CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001341 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342 //
Mike Stump31feda52009-07-17 01:31:16 +00001343 // NOTE: If a "return" appears in the middle of a block, this means that the
1344 // code afterwards is DEAD (unreachable). We still keep a basic block
1345 // for that code; a simple "mark-and-sweep" from the entry block will be
1346 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001347
1348 // Create the new block.
1349 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001350
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001352 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001353 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001354
1355 // Add the return statement to the block. This may create new blocks if R
1356 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001357 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001358}
1359
1360CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1361 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001362 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001363 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001364
Ted Kremenek93668002009-07-17 22:18:43 +00001365 if (!LabelBlock) // This can happen when the body is empty, i.e.
1366 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001367
Ted Kremenek93668002009-07-17 22:18:43 +00001368 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001369 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001370
1371 // Labels partition blocks, so this is the end of the basic block we were
1372 // processing (L is the block's label). Because this is label (and we have
1373 // already processed the substatement) there is no extra control-flow to worry
1374 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001375 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001376 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001377 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001378
1379 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001381
Ted Kremenek9aae5132007-08-23 21:42:29 +00001382 // This block is now the implicit successor of other blocks.
1383 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001384
Ted Kremenek9aae5132007-08-23 21:42:29 +00001385 return LabelBlock;
1386}
1387
1388CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001389 // Goto is a control-flow statement. Thus we stop processing the current
1390 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001391
Ted Kremenek9aae5132007-08-23 21:42:29 +00001392 Block = createBlock(false);
1393 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001394
1395 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001396 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001397
Ted Kremenek9aae5132007-08-23 21:42:29 +00001398 if (I == LabelMap.end())
1399 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001400 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1401 else {
1402 JumpTarget JT = I->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001403 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001404 AddSuccessor(Block, JT.Block);
1405 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406
Mike Stump31feda52009-07-17 01:31:16 +00001407 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001408}
1409
1410CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001411 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001412
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001413 // Save local scope position because in case of condition variable ScopePos
1414 // won't be restored when traversing AST.
1415 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1416
1417 // Create local scope for init statement and possible condition variable.
1418 // Add destructor for init statement and condition variable.
1419 // Store scope position for continue statement.
1420 if (Stmt* Init = F->getInit())
1421 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001422 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1423
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001424 if (VarDecl* VD = F->getConditionVariable())
1425 addLocalScopeForVarDecl(VD);
1426 LocalScope::const_iterator ContinueScopePos = ScopePos;
1427
1428 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1429
Mike Stump014b3ea2009-07-21 01:12:51 +00001430 // "for" is a control-flow statement. Thus we stop processing the current
1431 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001432 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001433 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001434 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001435 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001436 } else
1437 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001438
Ted Kremenek304a9532010-05-21 20:30:15 +00001439 // Save the current value for the break targets.
1440 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001441 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001442 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001443
Mike Stump31feda52009-07-17 01:31:16 +00001444 // Because of short-circuit evaluation, the condition of the loop can span
1445 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1446 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001447 CFGBlock* ExitConditionBlock = createBlock(false);
1448 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001449
Ted Kremenek81e14852007-08-27 19:46:09 +00001450 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001451 ExitConditionBlock->setTerminator(F);
1452
1453 // Now add the actual condition to the condition block. Because the condition
1454 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001455 if (Stmt* C = F->getCond()) {
1456 Block = ExitConditionBlock;
1457 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001458 assert(Block == EntryConditionBlock ||
1459 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001460
1461 // If this block contains a condition variable, add both the condition
1462 // variable and initializer to the CFG.
1463 if (VarDecl *VD = F->getConditionVariable()) {
1464 if (Expr *Init = VD->getInit()) {
1465 autoCreateBlock();
1466 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1467 EntryConditionBlock = addStmt(Init);
1468 assert(Block == EntryConditionBlock);
1469 }
1470 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001471
Ted Kremenek55957a82009-05-02 00:13:27 +00001472 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001473 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001474 return 0;
1475 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001476 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001477
Mike Stump31feda52009-07-17 01:31:16 +00001478 // The condition block is the implicit successor for the loop body as well as
1479 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001480 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001481
Mike Stump773582d2009-07-23 23:25:26 +00001482 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001483 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001484
Mike Stump773582d2009-07-23 23:25:26 +00001485 if (F->getCond())
1486 KnownVal = TryEvaluateBool(F->getCond());
1487
Ted Kremenek9aae5132007-08-23 21:42:29 +00001488 // Now create the loop body.
1489 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001490 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenek304a9532010-05-21 20:30:15 +00001492 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001493 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1494 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001495
Ted Kremeneke9610502007-08-30 18:39:40 +00001496 // Create a new block to contain the (bottom) of the loop body.
1497 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001498
1499 // Loop body should end with destructor of Condition variable (if any).
1500 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001501
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001502 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001503 // Generate increment code in its own basic block. This is the target of
1504 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001505 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001506 } else {
1507 // No increment code. Create a special, empty, block that is used as the
1508 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001509 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001510 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001511 }
Mike Stump31feda52009-07-17 01:31:16 +00001512
Ted Kremenek902393b2009-04-28 00:51:56 +00001513 // Finish up the increment (or empty) block if it hasn't been already.
1514 if (Block) {
1515 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001516 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001517 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001518 Block = 0;
1519 }
Mike Stump31feda52009-07-17 01:31:16 +00001520
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001521 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001522
Ted Kremenek902393b2009-04-28 00:51:56 +00001523 // The starting block for the loop increment is the block that should
1524 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001525 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001526
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001527 // If body is not a compound statement create implicit scope
1528 // and add destructors.
1529 if (!isa<CompoundStmt>(F->getBody()))
1530 addLocalScopeAndDtors(F->getBody());
1531
Mike Stump31feda52009-07-17 01:31:16 +00001532 // Now populate the body block, and in the process create new blocks as we
1533 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001534 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001535
1536 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001537 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001538 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001539 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001540
Ted Kremenek30754282009-07-24 04:47:11 +00001541 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001542 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001543 }
Mike Stump31feda52009-07-17 01:31:16 +00001544
Ted Kremenek30754282009-07-24 04:47:11 +00001545 // Link up the condition block with the code that follows the loop. (the
1546 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001547 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001548
Ted Kremenek9aae5132007-08-23 21:42:29 +00001549 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001550 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001551 if (Stmt* I = F->getInit()) {
1552 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001553 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001554 } else {
1555 // There is no loop initialization. We are thus basically a while loop.
1556 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001557 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001558 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001559 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001560 }
1561}
1562
Ted Kremenek5868ec62010-04-11 17:02:10 +00001563CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1564 if (asc.alwaysAdd()) {
1565 autoCreateBlock();
1566 AppendStmt(Block, M, asc);
1567 }
1568 return Visit(M->getBase(),
1569 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1570 : AddStmtChoice::AsLValueNotAlwaysAdd);
1571}
1572
Ted Kremenek9d56e642008-11-11 17:10:00 +00001573CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1574 // Objective-C fast enumeration 'for' statements:
1575 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1576 //
1577 // for ( Type newVariable in collection_expression ) { statements }
1578 //
1579 // becomes:
1580 //
1581 // prologue:
1582 // 1. collection_expression
1583 // T. jump to loop_entry
1584 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001585 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001586 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1587 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1588 // TB:
1589 // statements
1590 // T. jump to loop_entry
1591 // FB:
1592 // what comes after
1593 //
1594 // and
1595 //
1596 // Type existingItem;
1597 // for ( existingItem in expression ) { statements }
1598 //
1599 // becomes:
1600 //
Mike Stump31feda52009-07-17 01:31:16 +00001601 // the same with newVariable replaced with existingItem; the binding works
1602 // the same except that for one ObjCForCollectionStmt::getElement() returns
1603 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001604 //
Mike Stump31feda52009-07-17 01:31:16 +00001605
Ted Kremenek9d56e642008-11-11 17:10:00 +00001606 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001607
Ted Kremenek9d56e642008-11-11 17:10:00 +00001608 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001609 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001610 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001611 LoopSuccessor = Block;
1612 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001613 } else
1614 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001615
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001616 // Build the condition blocks.
1617 CFGBlock* ExitConditionBlock = createBlock(false);
1618 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001619
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001620 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001621 ExitConditionBlock->setTerminator(S);
1622
1623 // The last statement in the block should be the ObjCForCollectionStmt, which
1624 // performs the actual binding to 'element' and determines if there are any
1625 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001626 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001627 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001628
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001629 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001630 // generate new blocks as necesary. We DON'T add the statement by default to
1631 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001632 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001633 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001634 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001635 return 0;
1636 Block = 0;
1637 }
Mike Stump31feda52009-07-17 01:31:16 +00001638
1639 // The condition block is the implicit successor for the loop body as well as
1640 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001641 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001642
Ted Kremenek9d56e642008-11-11 17:10:00 +00001643 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001644 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001645 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001646 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1647 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1648 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001649
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001650 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1651 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001652
Ted Kremenek93668002009-07-17 22:18:43 +00001653 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001654
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001655 if (!BodyBlock)
1656 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001657 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001658 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001659 return 0;
1660 }
Mike Stump31feda52009-07-17 01:31:16 +00001661
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001662 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001663 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001664 }
Mike Stump31feda52009-07-17 01:31:16 +00001665
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001666 // Link up the condition block with the code that follows the loop.
1667 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001668 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001669
Ted Kremenek9d56e642008-11-11 17:10:00 +00001670 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001671 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001672 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001673}
1674
Ted Kremenek49805452009-05-02 01:49:13 +00001675CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1676 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001677
Ted Kremenek49805452009-05-02 01:49:13 +00001678 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001679 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001680
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001681 // The sync body starts its own basic block. This makes it a little easier
1682 // for diagnostic clients.
1683 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001684 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001685 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001686
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001687 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001688 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001689 }
Mike Stump31feda52009-07-17 01:31:16 +00001690
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001691 // Add the @synchronized to the CFG.
1692 autoCreateBlock();
1693 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1694
Ted Kremenek49805452009-05-02 01:49:13 +00001695 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001696 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001697}
Mike Stump31feda52009-07-17 01:31:16 +00001698
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001699CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001700 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001701 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001702}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001703
Ted Kremenek9aae5132007-08-23 21:42:29 +00001704CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001705 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001706
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001707 // Save local scope position because in case of condition variable ScopePos
1708 // won't be restored when traversing AST.
1709 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1710
1711 // Create local scope for possible condition variable.
1712 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001713 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001714 if (VarDecl* VD = W->getConditionVariable()) {
1715 addLocalScopeForVarDecl(VD);
1716 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1717 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001718
Mike Stump014b3ea2009-07-21 01:12:51 +00001719 // "while" is a control-flow statement. Thus we stop processing the current
1720 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001721 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001722 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001723 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001724 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001725 } else
1726 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001727
1728 // Because of short-circuit evaluation, the condition of the loop can span
1729 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1730 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001731 CFGBlock* ExitConditionBlock = createBlock(false);
1732 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001733
Ted Kremenek81e14852007-08-27 19:46:09 +00001734 // Set the terminator for the "exit" condition block.
1735 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001736
1737 // Now add the actual condition to the condition block. Because the condition
1738 // itself may contain control-flow, new blocks may be created. Thus we update
1739 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001740 if (Stmt* C = W->getCond()) {
1741 Block = ExitConditionBlock;
1742 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001743 // The condition might finish the current 'Block'.
1744 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001745
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001746 // If this block contains a condition variable, add both the condition
1747 // variable and initializer to the CFG.
1748 if (VarDecl *VD = W->getConditionVariable()) {
1749 if (Expr *Init = VD->getInit()) {
1750 autoCreateBlock();
1751 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1752 EntryConditionBlock = addStmt(Init);
1753 assert(Block == EntryConditionBlock);
1754 }
1755 }
1756
Ted Kremenek55957a82009-05-02 00:13:27 +00001757 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001758 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001759 return 0;
1760 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001761 }
Mike Stump31feda52009-07-17 01:31:16 +00001762
1763 // The condition block is the implicit successor for the loop body as well as
1764 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001765 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001766
Mike Stump773582d2009-07-23 23:25:26 +00001767 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001768 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001769
Ted Kremenek9aae5132007-08-23 21:42:29 +00001770 // Process the loop body.
1771 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001772 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001773
1774 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001775 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1776 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1777 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001778
Mike Stump31feda52009-07-17 01:31:16 +00001779 // Create an empty block to represent the transition block for looping back
1780 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001781 Block = 0;
1782 assert(Succ == EntryConditionBlock);
1783 Succ = createBlock();
1784 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001785 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001786
Ted Kremenek9aae5132007-08-23 21:42:29 +00001787 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001788 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001789
Ted Kremenek9aae5132007-08-23 21:42:29 +00001790 // NULL out Block to force lazy instantiation of blocks for the body.
1791 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001792
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001793 // Loop body should end with destructor of Condition variable (if any).
1794 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1795
1796 // If body is not a compound statement create implicit scope
1797 // and add destructors.
1798 if (!isa<CompoundStmt>(W->getBody()))
1799 addLocalScopeAndDtors(W->getBody());
1800
Ted Kremenek9aae5132007-08-23 21:42:29 +00001801 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001802 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001803
Ted Kremeneke9610502007-08-30 18:39:40 +00001804 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001805 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001806 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001807 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001808 return 0;
1809 }
Mike Stump31feda52009-07-17 01:31:16 +00001810
Ted Kremenek30754282009-07-24 04:47:11 +00001811 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001812 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001813 }
Mike Stump31feda52009-07-17 01:31:16 +00001814
Ted Kremenek30754282009-07-24 04:47:11 +00001815 // Link up the condition block with the code that follows the loop. (the
1816 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001817 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001818
1819 // There can be no more statements in the condition block since we loop back
1820 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001821 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001822
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001823 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001824 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001825 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001826}
Mike Stump11289f42009-09-09 15:08:12 +00001827
1828
Ted Kremenek93668002009-07-17 22:18:43 +00001829CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1830 // FIXME: For now we pretend that @catch and the code it contains does not
1831 // exit.
1832 return Block;
1833}
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenek93041ba2008-12-09 20:20:09 +00001835CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1836 // FIXME: This isn't complete. We basically treat @throw like a return
1837 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001838
Ted Kremenek0868eea2009-09-24 18:45:41 +00001839 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001840 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001841 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001842
Ted Kremenek93041ba2008-12-09 20:20:09 +00001843 // Create the new block.
1844 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001845
Ted Kremenek93041ba2008-12-09 20:20:09 +00001846 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001847 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001848
1849 // Add the statement to the block. This may create new blocks if S contains
1850 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001851 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001852}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001853
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001854CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001855 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001856 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001857 return 0;
1858
1859 // Create the new block.
1860 Block = createBlock(false);
1861
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001862 if (TryTerminatedBlock)
1863 // The current try statement is the only successor.
1864 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001865 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001866 // otherwise the Exit block is the only successor.
1867 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001868
1869 // Add the statement to the block. This may create new blocks if S contains
1870 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001871 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001872}
1873
Ted Kremenek93668002009-07-17 22:18:43 +00001874CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001875 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001876
Mike Stump8d50b6a2009-07-21 01:27:50 +00001877 // "do...while" is a control-flow statement. Thus we stop processing the
1878 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001879 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001880 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001881 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001882 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001883 } else
1884 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001885
1886 // Because of short-circuit evaluation, the condition of the loop can span
1887 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1888 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001889 CFGBlock* ExitConditionBlock = createBlock(false);
1890 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001891
Ted Kremenek81e14852007-08-27 19:46:09 +00001892 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001893 ExitConditionBlock->setTerminator(D);
1894
1895 // Now add the actual condition to the condition block. Because the condition
1896 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001897 if (Stmt* C = D->getCond()) {
1898 Block = ExitConditionBlock;
1899 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001900 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001901 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001902 return 0;
1903 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001904 }
Mike Stump31feda52009-07-17 01:31:16 +00001905
Ted Kremeneka1523a32008-02-27 07:20:00 +00001906 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001907 Succ = EntryConditionBlock;
1908
Mike Stump773582d2009-07-23 23:25:26 +00001909 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001910 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001911
Ted Kremenek9aae5132007-08-23 21:42:29 +00001912 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001913 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001914 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001915 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001916
Ted Kremenek9aae5132007-08-23 21:42:29 +00001917 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001918 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1919 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1920 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001921
Ted Kremenek9aae5132007-08-23 21:42:29 +00001922 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001923 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek9aae5132007-08-23 21:42:29 +00001925 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001926 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001927
Ted Kremenek9aae5132007-08-23 21:42:29 +00001928 // NULL out Block to force lazy instantiation of blocks for the body.
1929 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001930
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001931 // If body is not a compound statement create implicit scope
1932 // and add destructors.
1933 if (!isa<CompoundStmt>(D->getBody()))
1934 addLocalScopeAndDtors(D->getBody());
1935
Ted Kremenek9aae5132007-08-23 21:42:29 +00001936 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001937 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001938
Ted Kremeneke9610502007-08-30 18:39:40 +00001939 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001940 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001941 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001942 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001943 return 0;
1944 }
Mike Stump31feda52009-07-17 01:31:16 +00001945
Ted Kremenek110974d2010-08-17 20:59:56 +00001946 if (!KnownVal.isFalse()) {
1947 // Add an intermediate block between the BodyBlock and the
1948 // ExitConditionBlock to represent the "loop back" transition. Create an
1949 // empty block to represent the transition block for looping back to the
1950 // head of the loop.
1951 // FIXME: Can we do this more efficiently without adding another block?
1952 Block = NULL;
1953 Succ = BodyBlock;
1954 CFGBlock *LoopBackBlock = createBlock();
1955 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001956
Ted Kremenek110974d2010-08-17 20:59:56 +00001957 // Add the loop body entry as a successor to the condition.
1958 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1959 }
1960 else
1961 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001962 }
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenek30754282009-07-24 04:47:11 +00001964 // Link up the condition block with the code that follows the loop.
1965 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001966 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001967
1968 // There can be no more statements in the body block(s) since we loop back to
1969 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001970 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001971
Ted Kremenek9aae5132007-08-23 21:42:29 +00001972 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001973 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001974 return BodyBlock;
1975}
1976
1977CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1978 // "continue" is a control-flow statement. Thus we stop processing the
1979 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001980 if (badCFG)
1981 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001982
Ted Kremenek9aae5132007-08-23 21:42:29 +00001983 // Now create a new block that ends with the continue statement.
1984 Block = createBlock(false);
1985 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenek9aae5132007-08-23 21:42:29 +00001987 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001988 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001989 if (ContinueJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001990 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001991 AddSuccessor(Block, ContinueJumpTarget.Block);
1992 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00001993 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001994
Ted Kremenek9aae5132007-08-23 21:42:29 +00001995 return Block;
1996}
Mike Stump11289f42009-09-09 15:08:12 +00001997
Ted Kremenek0747de62009-07-18 00:47:21 +00001998CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001999 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002000
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002001 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002002 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002003 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
Ted Kremenek93668002009-07-17 22:18:43 +00002006 // VLA types have expressions that must be evaluated.
2007 if (E->isArgumentType()) {
2008 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2009 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2010 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002011 }
Mike Stump11289f42009-09-09 15:08:12 +00002012
Mike Stump31feda52009-07-17 01:31:16 +00002013 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002014}
Mike Stump11289f42009-09-09 15:08:12 +00002015
Ted Kremenek93668002009-07-17 22:18:43 +00002016/// VisitStmtExpr - Utility method to handle (nested) statement
2017/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002018CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2019 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002020 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002021 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002022 }
Ted Kremenek93668002009-07-17 22:18:43 +00002023 return VisitCompoundStmt(SE->getSubStmt());
2024}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002025
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002026CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002027 // "switch" is a control-flow statement. Thus we stop processing the current
2028 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002029 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002030
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002031 // Save local scope position because in case of condition variable ScopePos
2032 // won't be restored when traversing AST.
2033 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2034
2035 // Create local scope for possible condition variable.
2036 // Store scope position. Add implicit destructor.
2037 if (VarDecl* VD = Terminator->getConditionVariable()) {
2038 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2039 addLocalScopeForVarDecl(VD);
2040 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2041 }
2042
Ted Kremenek9aae5132007-08-23 21:42:29 +00002043 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002044 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002045 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002046 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002047 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002048
2049 // Save the current "switch" context.
2050 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002051 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002052 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002053
Mike Stump31feda52009-07-17 01:31:16 +00002054 // Set the "default" case to be the block after the switch statement. If the
2055 // switch statement contains a "default:", this value will be overwritten with
2056 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002057 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002058
Ted Kremenek9aae5132007-08-23 21:42:29 +00002059 // Create a new block that will contain the switch statement.
2060 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002061
Ted Kremenek9aae5132007-08-23 21:42:29 +00002062 // Now process the switch body. The code after the switch is the implicit
2063 // successor.
2064 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002065 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002066
2067 // When visiting the body, the case statements should automatically get linked
2068 // up to the switch. We also don't keep a pointer to the body, since all
2069 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002070 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002071 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002072
2073 // If body is not a compound statement create implicit scope
2074 // and add destructors.
2075 if (!isa<CompoundStmt>(Terminator->getBody()))
2076 addLocalScopeAndDtors(Terminator->getBody());
2077
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002078 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002079 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002080 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002081 return 0;
2082 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002083
Mike Stump31feda52009-07-17 01:31:16 +00002084 // If we have no "default:" case, the default transition is to the code
2085 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002086 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002087
Ted Kremenek81e14852007-08-27 19:46:09 +00002088 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002089 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002090 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002091 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002092 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002093
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002094 // Finally, if the SwitchStmt contains a condition variable, add both the
2095 // SwitchStmt and the condition variable initialization to the CFG.
2096 if (VarDecl *VD = Terminator->getConditionVariable()) {
2097 if (Expr *Init = VD->getInit()) {
2098 autoCreateBlock();
2099 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2100 addStmt(Init);
2101 }
2102 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002103
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002104 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002105}
2106
Ted Kremenek93668002009-07-17 22:18:43 +00002107CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002108 // CaseStmts are essentially labels, so they are the first statement in a
2109 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002110 CFGBlock *TopBlock = 0, *LastBlock = 0;
2111
2112 if (Stmt *Sub = CS->getSubStmt()) {
2113 // For deeply nested chains of CaseStmts, instead of doing a recursion
2114 // (which can blow out the stack), manually unroll and create blocks
2115 // along the way.
2116 while (isa<CaseStmt>(Sub)) {
2117 CFGBlock *CurrentBlock = createBlock(false);
2118 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002119
Ted Kremenek60fa6572010-08-04 23:54:30 +00002120 if (TopBlock)
2121 AddSuccessor(LastBlock, CurrentBlock);
2122 else
2123 TopBlock = CurrentBlock;
2124
2125 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2126 LastBlock = CurrentBlock;
2127
2128 CS = cast<CaseStmt>(Sub);
2129 Sub = CS->getSubStmt();
2130 }
2131
2132 addStmt(Sub);
2133 }
Mike Stump11289f42009-09-09 15:08:12 +00002134
Ted Kremenek55e91e82007-08-30 18:48:11 +00002135 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002136 if (!CaseBlock)
2137 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002138
2139 // Cases statements partition blocks, so this is the top of the basic block we
2140 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002141 CaseBlock->setLabel(CS);
2142
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002143 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002144 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002145
2146 // Add this block to the list of successors for the block with the switch
2147 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002148 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002149 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002150
Ted Kremenek9aae5132007-08-23 21:42:29 +00002151 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2152 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002153
Ted Kremenek60fa6572010-08-04 23:54:30 +00002154 if (TopBlock) {
2155 AddSuccessor(LastBlock, CaseBlock);
2156 Succ = TopBlock;
2157 }
2158 else {
2159 // This block is now the implicit successor of other blocks.
2160 Succ = CaseBlock;
2161 }
Mike Stump31feda52009-07-17 01:31:16 +00002162
Ted Kremenek60fa6572010-08-04 23:54:30 +00002163 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002164}
Mike Stump31feda52009-07-17 01:31:16 +00002165
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002166CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002167 if (Terminator->getSubStmt())
2168 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002169
Ted Kremenek654c78f2008-02-13 22:05:39 +00002170 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002171
2172 if (!DefaultCaseBlock)
2173 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002174
2175 // Default statements partition blocks, so this is the top of the basic block
2176 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002177 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002178
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002179 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002180 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002181
Mike Stump31feda52009-07-17 01:31:16 +00002182 // Unlike case statements, we don't add the default block to the successors
2183 // for the switch statement immediately. This is done when we finish
2184 // processing the switch statement. This allows for the default case
2185 // (including a fall-through to the code after the switch statement) to always
2186 // be the last successor of a switch-terminated block.
2187
Ted Kremenek654c78f2008-02-13 22:05:39 +00002188 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2189 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002190
Ted Kremenek654c78f2008-02-13 22:05:39 +00002191 // This block is now the implicit successor of other blocks.
2192 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002193
2194 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002195}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002196
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002197CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2198 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2199 // current block.
2200 CFGBlock* TrySuccessor = NULL;
2201
2202 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002203 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002204 return 0;
2205 TrySuccessor = Block;
2206 } else TrySuccessor = Succ;
2207
Mike Stump0bdba6c2010-01-20 01:15:34 +00002208 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002209
2210 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002211 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002212 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002213 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002214
Mike Stump0bdba6c2010-01-20 01:15:34 +00002215 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002216 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2217 // The code after the try is the implicit successor.
2218 Succ = TrySuccessor;
2219 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002220 if (CS->getExceptionDecl() == 0) {
2221 HasCatchAll = true;
2222 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002223 Block = NULL;
2224 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2225 if (CatchBlock == 0)
2226 return 0;
2227 // Add this block to the list of successors for the block with the try
2228 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00002229 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002230 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002231 if (!HasCatchAll) {
2232 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00002233 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002234 else
Mike Stump845384a2010-01-20 01:30:58 +00002235 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002236 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002237
2238 // The code after the try is the implicit successor.
2239 Succ = TrySuccessor;
2240
Mike Stump845384a2010-01-20 01:30:58 +00002241 // Save the current "try" context.
2242 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2243 TryTerminatedBlock = NewTryTerminatedBlock;
2244
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002245 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002246 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002247 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002248 return Block;
2249}
2250
2251CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2252 // CXXCatchStmt are treated like labels, so they are the first statement in a
2253 // block.
2254
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002255 // Save local scope position because in case of exception variable ScopePos
2256 // won't be restored when traversing AST.
2257 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2258
2259 // Create local scope for possible exception variable.
2260 // Store scope position. Add implicit destructor.
2261 if (VarDecl* VD = CS->getExceptionDecl()) {
2262 LocalScope::const_iterator BeginScopePos = ScopePos;
2263 addLocalScopeForVarDecl(VD);
2264 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2265 }
2266
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002267 if (CS->getHandlerBlock())
2268 addStmt(CS->getHandlerBlock());
2269
2270 CFGBlock* CatchBlock = Block;
2271 if (!CatchBlock)
2272 CatchBlock = createBlock();
2273
2274 CatchBlock->setLabel(CS);
2275
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002276 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002277 return 0;
2278
2279 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2280 Block = NULL;
2281
2282 return CatchBlock;
2283}
2284
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002285CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002286 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002287 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002288 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00002289 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002290 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002291 return VisitChildren(C);
2292}
2293
Ted Kremenekeda180e22007-08-28 19:26:49 +00002294CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002295 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002296 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002297
Ted Kremenekeda180e22007-08-28 19:26:49 +00002298 if (!IBlock) {
2299 IBlock = createBlock(false);
2300 cfg->setIndirectGotoBlock(IBlock);
2301 }
Mike Stump31feda52009-07-17 01:31:16 +00002302
Ted Kremenekeda180e22007-08-28 19:26:49 +00002303 // IndirectGoto is a control-flow statement. Thus we stop processing the
2304 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002305 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002306 return 0;
2307
Ted Kremenekeda180e22007-08-28 19:26:49 +00002308 Block = createBlock(false);
2309 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002310 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002311 return addStmt(I->getTarget());
2312}
2313
Ted Kremenek04cca642007-08-23 21:26:19 +00002314} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002315
Mike Stump31feda52009-07-17 01:31:16 +00002316/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2317/// no successors or predecessors. If this is the first block created in the
2318/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002319CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002320 bool first_block = begin() == end();
2321
2322 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002323 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2324 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2325 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002326
2327 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002328 if (first_block)
2329 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002330
2331 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002332 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002333}
2334
Ted Kremenek889073f2007-08-23 16:51:22 +00002335/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2336/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002337CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002338 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002339 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002340 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002341}
2342
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002343//===----------------------------------------------------------------------===//
2344// CFG: Queries for BlkExprs.
2345//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002346
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002347namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002348 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002349}
2350
Ted Kremenekbff98442009-12-23 23:37:10 +00002351static void FindSubExprAssignments(Stmt *S,
2352 llvm::SmallPtrSet<Expr*,50>& Set) {
2353 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002354 return;
Mike Stump31feda52009-07-17 01:31:16 +00002355
Ted Kremenekbff98442009-12-23 23:37:10 +00002356 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002357 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002358 if (!child)
2359 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002360
Ted Kremenekbff98442009-12-23 23:37:10 +00002361 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002362 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002363
Ted Kremenekbff98442009-12-23 23:37:10 +00002364 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002365 }
2366}
2367
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002368static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2369 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002370
2371 // Look for assignments that are used as subexpressions. These are the only
2372 // assignments that we want to *possibly* register as a block-level
2373 // expression. Basically, if an assignment occurs both in a subexpression and
2374 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002375 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002376
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002377 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002378 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002379 if (CFGStmt S = BI->getAs<CFGStmt>())
2380 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002381
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002382 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002383
2384 // Iterate over the statements again on identify the Expr* and Stmt* at the
2385 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002386
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002387 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2388 CFGStmt CS = BI->getAs<CFGStmt>();
2389 if (!CS.isValid())
2390 continue;
2391 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002392
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002393 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002394 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002395 // expression are really "statements" whose value is never used by
2396 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002397 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002398 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002399 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2400 // Special handling for statement expressions. The last statement in
2401 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002402 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002403 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002404 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002405 (*M)[C->body_back()] = x;
2406 }
2407 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002408
Ted Kremenek95a123c2008-01-26 00:03:27 +00002409 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002410 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002411 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002412 }
Mike Stump31feda52009-07-17 01:31:16 +00002413
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002414 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002415
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002416 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002417
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002418 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002419 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002420 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002421 }
2422 }
Mike Stump31feda52009-07-17 01:31:16 +00002423
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002424 return M;
2425}
2426
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002427CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2428 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002429 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002430
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002431 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002432 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002433 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002434}
2435
2436unsigned CFG::getNumBlkExprs() {
2437 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2438 return M->size();
2439 else {
2440 // We assume callers interested in the number of BlkExprs will want
2441 // the map constructed if it doesn't already exist.
2442 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2443 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2444 }
2445}
2446
Ted Kremenek6065ef62008-04-28 18:00:46 +00002447//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002448// Filtered walking of the CFG.
2449//===----------------------------------------------------------------------===//
2450
2451bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002452 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002453
2454 if (F.IgnoreDefaultsWithCoveredEnums) {
2455 // If the 'To' has no label or is labeled but the label isn't a
2456 // CaseStmt then filter this edge.
2457 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002458 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002459 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002460 const Stmt *L = To->getLabel();
2461 if (!L || !isa<CaseStmt>(L))
2462 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002463 }
2464 }
2465 }
2466
2467 return false;
2468}
2469
2470//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002471// Cleanup: CFG dstor.
2472//===----------------------------------------------------------------------===//
2473
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002474CFG::~CFG() {
2475 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2476}
Mike Stump31feda52009-07-17 01:31:16 +00002477
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002478//===----------------------------------------------------------------------===//
2479// CFG pretty printing
2480//===----------------------------------------------------------------------===//
2481
Ted Kremenek7e776b12007-08-22 18:22:34 +00002482namespace {
2483
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002484class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002485 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002486 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002487 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002488 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002489 signed CurrentBlock;
2490 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002491 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002492public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002493
Chris Lattnerc61089a2009-06-30 01:26:17 +00002494 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2495 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002496 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2497 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002498 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002499 BI != BEnd; ++BI, ++j ) {
2500 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2501 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2502 StmtMap[SE] = P;
2503
2504 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2505 DeclMap[DS->getSingleDecl()] = P;
2506
2507 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2508 if (VarDecl* VD = IS->getConditionVariable())
2509 DeclMap[VD] = P;
2510
2511 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2512 if (VarDecl* VD = FS->getConditionVariable())
2513 DeclMap[VD] = P;
2514
2515 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2516 if (VarDecl* VD = WS->getConditionVariable())
2517 DeclMap[VD] = P;
2518
2519 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2520 if (VarDecl* VD = SS->getConditionVariable())
2521 DeclMap[VD] = P;
2522
2523 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2524 if (VarDecl* VD = CS->getExceptionDecl())
2525 DeclMap[VD] = P;
2526 }
2527 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002528 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002529 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002530 }
Mike Stump31feda52009-07-17 01:31:16 +00002531
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002532 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002533
Chris Lattnerc61089a2009-06-30 01:26:17 +00002534 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002535 void setBlockID(signed i) { CurrentBlock = i; }
2536 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002537
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002538 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2539 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002540
2541 if (I == StmtMap.end())
2542 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002543
2544 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002545 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002546 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002547 }
Mike Stump31feda52009-07-17 01:31:16 +00002548
Ted Kremenek60983dc2010-01-19 20:52:05 +00002549 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002550 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002551 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002552
2553 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2554 DeclMapTy::iterator I = DeclMap.find(D);
2555
2556 if (I == DeclMap.end())
2557 return false;
2558
2559 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2560 && I->second.second == CurrentStmt) {
2561 return false;
2562 }
2563
2564 OS << "[B" << I->second.first << "." << I->second.second << "]";
2565 return true;
2566 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002567};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002568} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002569
Chris Lattnerc61089a2009-06-30 01:26:17 +00002570
2571namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002572class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002573 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002574
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002575 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002576 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002577 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002578public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002579 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002580 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002581 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002582
Ted Kremenek9aae5132007-08-23 21:42:29 +00002583 void VisitIfStmt(IfStmt* I) {
2584 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002585 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002586 }
Mike Stump31feda52009-07-17 01:31:16 +00002587
Ted Kremenek9aae5132007-08-23 21:42:29 +00002588 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002589 void VisitStmt(Stmt* Terminator) {
2590 Terminator->printPretty(OS, Helper, Policy);
2591 }
2592
Ted Kremenek9aae5132007-08-23 21:42:29 +00002593 void VisitForStmt(ForStmt* F) {
2594 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002595 if (F->getInit())
2596 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002597 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002598 if (Stmt* C = F->getCond())
2599 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002600 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002601 if (F->getInc())
2602 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002603 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002604 }
Mike Stump31feda52009-07-17 01:31:16 +00002605
Ted Kremenek9aae5132007-08-23 21:42:29 +00002606 void VisitWhileStmt(WhileStmt* W) {
2607 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002608 if (Stmt* C = W->getCond())
2609 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002610 }
Mike Stump31feda52009-07-17 01:31:16 +00002611
Ted Kremenek9aae5132007-08-23 21:42:29 +00002612 void VisitDoStmt(DoStmt* D) {
2613 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002614 if (Stmt* C = D->getCond())
2615 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002616 }
Mike Stump31feda52009-07-17 01:31:16 +00002617
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002618 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002619 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002620 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002621 }
Mike Stump31feda52009-07-17 01:31:16 +00002622
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002623 void VisitCXXTryStmt(CXXTryStmt* CS) {
2624 OS << "try ...";
2625 }
2626
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002627 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002628 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002629 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002630 }
Mike Stump31feda52009-07-17 01:31:16 +00002631
Ted Kremenek391f94a2007-08-31 22:29:13 +00002632 void VisitChooseExpr(ChooseExpr* C) {
2633 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002634 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002635 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002636 }
Mike Stump31feda52009-07-17 01:31:16 +00002637
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002638 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2639 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002640 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002641 }
Mike Stump31feda52009-07-17 01:31:16 +00002642
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002643 void VisitBinaryOperator(BinaryOperator* B) {
2644 if (!B->isLogicalOp()) {
2645 VisitExpr(B);
2646 return;
2647 }
Mike Stump31feda52009-07-17 01:31:16 +00002648
Douglas Gregor7de59662009-05-29 20:38:28 +00002649 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002650
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002651 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002652 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002653 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002654 return;
John McCalle3027922010-08-25 11:45:40 +00002655 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002656 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002657 return;
2658 default:
2659 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002660 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002661 }
Mike Stump31feda52009-07-17 01:31:16 +00002662
Ted Kremenek12687ff2007-08-27 21:54:41 +00002663 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002664 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002665 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002666};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002667} // end anonymous namespace
2668
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002669static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002670 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002671 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2672 Stmt *S = CS;
2673
2674 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002675
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002676 // special printing for statement-expressions.
2677 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2678 CompoundStmt* Sub = SE->getSubStmt();
2679
2680 if (Sub->child_begin() != Sub->child_end()) {
2681 OS << "({ ... ; ";
2682 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2683 OS << " })\n";
2684 return;
2685 }
2686 }
2687 // special printing for comma expressions.
2688 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2689 if (B->getOpcode() == BO_Comma) {
2690 OS << "... , ";
2691 Helper->handledStmt(B->getRHS(),OS);
2692 OS << '\n';
2693 return;
2694 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002695 }
2696 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002697 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002698
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002699 if (isa<CXXOperatorCallExpr>(S)) {
2700 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002701 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002702 else if (isa<CXXBindTemporaryExpr>(S)) {
2703 OS << " (BindTemporary)";
2704 }
Mike Stump31feda52009-07-17 01:31:16 +00002705
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002706 // Expressions need a newline.
2707 if (isa<Expr>(S))
2708 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002709
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002710 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2711 CXXBaseOrMemberInitializer* I = IE;
2712 if (I->isBaseInitializer())
2713 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2714 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002715
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002716 OS << "(";
2717 if (Expr* IE = I->getInit())
2718 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2719 OS << ")";
2720
2721 if (I->isBaseInitializer())
2722 OS << " (Base initializer)\n";
2723 else OS << " (Member initializer)\n";
2724
2725 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2726 VarDecl* VD = DE.getVarDecl();
2727 Helper->handleDecl(VD, OS);
2728
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00002729 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002730 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2731 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00002732 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
2733 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002734
2735 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2736 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00002737
2738 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
2739 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
2740 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00002741 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00002742
2743 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
2744 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00002745
2746 const Type *T = FD->getType().getTypePtr();
2747 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
2748 T = ET;
2749
Marcin Swiderski20b88732010-10-05 05:37:00 +00002750 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00002751 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00002752 OS << " (Member object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002753 }
2754 }
Mike Stump31feda52009-07-17 01:31:16 +00002755
Chris Lattnerc61089a2009-06-30 01:26:17 +00002756static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2757 const CFGBlock& B,
2758 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002759
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002760 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002761
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002762 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002763 OS << "\n [ B" << B.getBlockID();
2764
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002765 if (&B == &cfg->getEntry())
2766 OS << " (ENTRY) ]\n";
2767 else if (&B == &cfg->getExit())
2768 OS << " (EXIT) ]\n";
2769 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002770 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002771 else
2772 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002773
Ted Kremenek71eca012007-08-29 23:20:49 +00002774 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002775 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002776
2777 if (print_edges)
2778 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002779
Mike Stump92244b02010-01-19 22:00:14 +00002780 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002781 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002782 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002783 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002784 C->getLHS()->printPretty(OS, Helper,
2785 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002786 if (C->getRHS()) {
2787 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002788 C->getRHS()->printPretty(OS, Helper,
2789 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002790 }
Mike Stump92244b02010-01-19 22:00:14 +00002791 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002792 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002793 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002794 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002795 if (CS->getExceptionDecl())
2796 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2797 0);
2798 else
2799 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002800 OS << ")";
2801
2802 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002803 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002804
Ted Kremenek71eca012007-08-29 23:20:49 +00002805 OS << ":\n";
2806 }
Mike Stump31feda52009-07-17 01:31:16 +00002807
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002808 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002809 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002810
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002811 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2812 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002813
Ted Kremenek71eca012007-08-29 23:20:49 +00002814 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002815 if (print_edges)
2816 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002817
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002818 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002819
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002820 if (Helper)
2821 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002822
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002823 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002824 }
Mike Stump31feda52009-07-17 01:31:16 +00002825
Ted Kremenek71eca012007-08-29 23:20:49 +00002826 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002827 if (B.getTerminator()) {
2828 if (print_edges)
2829 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002830
Ted Kremenek71eca012007-08-29 23:20:49 +00002831 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002832
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002833 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002834
Chris Lattnerc61089a2009-06-30 01:26:17 +00002835 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2836 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002837 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00002838 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002839 }
Mike Stump31feda52009-07-17 01:31:16 +00002840
Ted Kremenek71eca012007-08-29 23:20:49 +00002841 if (print_edges) {
2842 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002843 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002844 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002845
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002846 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2847 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002848
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002849 if (i == 8 || (i-8) == 0)
2850 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002851
Ted Kremenek71eca012007-08-29 23:20:49 +00002852 OS << " B" << (*I)->getBlockID();
2853 }
Mike Stump31feda52009-07-17 01:31:16 +00002854
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002855 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002856
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002857 // Print the successors of this block.
2858 OS << " Successors (" << B.succ_size() << "):";
2859 i = 0;
2860
2861 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2862 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002863
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002864 if (i == 8 || (i-8) % 10 == 0)
2865 OS << "\n ";
2866
Mike Stump0d76d072009-07-20 23:24:15 +00002867 if (*I)
2868 OS << " B" << (*I)->getBlockID();
2869 else
2870 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002871 }
Mike Stump31feda52009-07-17 01:31:16 +00002872
Ted Kremenek71eca012007-08-29 23:20:49 +00002873 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002874 }
Mike Stump31feda52009-07-17 01:31:16 +00002875}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002876
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002877
2878/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002879void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002880
2881/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002882void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2883 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002884
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002885 // Print the entry block.
2886 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002887
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002888 // Iterate through the CFGBlocks and print them one by one.
2889 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2890 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002891 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002892 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002893
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002894 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002895 }
Mike Stump31feda52009-07-17 01:31:16 +00002896
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002897 // Print the exit block.
2898 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002899 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002900}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002901
2902/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002903void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2904 print(llvm::errs(), cfg, LO);
2905}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002906
2907/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2908/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002909void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2910 const LangOptions &LO) const {
2911 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002912 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002913}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002914
Ted Kremenek15647632008-01-30 23:02:42 +00002915/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002916void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002917 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002918 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002919 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00002920}
2921
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002922Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002923 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002924 if (!Terminator)
2925 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002926
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002927 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002928
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002929 switch (Terminator->getStmtClass()) {
2930 default:
2931 break;
Mike Stump31feda52009-07-17 01:31:16 +00002932
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002933 case Stmt::ForStmtClass:
2934 E = cast<ForStmt>(Terminator)->getCond();
2935 break;
Mike Stump31feda52009-07-17 01:31:16 +00002936
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002937 case Stmt::WhileStmtClass:
2938 E = cast<WhileStmt>(Terminator)->getCond();
2939 break;
Mike Stump31feda52009-07-17 01:31:16 +00002940
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002941 case Stmt::DoStmtClass:
2942 E = cast<DoStmt>(Terminator)->getCond();
2943 break;
Mike Stump31feda52009-07-17 01:31:16 +00002944
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002945 case Stmt::IfStmtClass:
2946 E = cast<IfStmt>(Terminator)->getCond();
2947 break;
Mike Stump31feda52009-07-17 01:31:16 +00002948
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002949 case Stmt::ChooseExprClass:
2950 E = cast<ChooseExpr>(Terminator)->getCond();
2951 break;
Mike Stump31feda52009-07-17 01:31:16 +00002952
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002953 case Stmt::IndirectGotoStmtClass:
2954 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2955 break;
Mike Stump31feda52009-07-17 01:31:16 +00002956
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002957 case Stmt::SwitchStmtClass:
2958 E = cast<SwitchStmt>(Terminator)->getCond();
2959 break;
Mike Stump31feda52009-07-17 01:31:16 +00002960
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002961 case Stmt::ConditionalOperatorClass:
2962 E = cast<ConditionalOperator>(Terminator)->getCond();
2963 break;
Mike Stump31feda52009-07-17 01:31:16 +00002964
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002965 case Stmt::BinaryOperatorClass: // '&&' and '||'
2966 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002967 break;
Mike Stump31feda52009-07-17 01:31:16 +00002968
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002969 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002970 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002971 }
Mike Stump31feda52009-07-17 01:31:16 +00002972
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002973 return E ? E->IgnoreParens() : NULL;
2974}
2975
Ted Kremenek92137a32008-05-16 16:06:00 +00002976bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002977 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00002978 if (!Terminator)
2979 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002980
Ted Kremenek92137a32008-05-16 16:06:00 +00002981 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002982
Ted Kremenek92137a32008-05-16 16:06:00 +00002983 switch (Terminator->getStmtClass()) {
2984 default:
2985 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002986
2987 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002988 case Stmt::WhileStmtClass:
2989 case Stmt::DoStmtClass:
2990 case Stmt::IfStmtClass:
2991 case Stmt::ChooseExprClass:
2992 case Stmt::ConditionalOperatorClass:
2993 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002994 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002995 }
Mike Stump31feda52009-07-17 01:31:16 +00002996
Ted Kremenek92137a32008-05-16 16:06:00 +00002997 return E ? E->IgnoreParens() : NULL;
2998}
2999
Ted Kremenek15647632008-01-30 23:02:42 +00003000
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003001//===----------------------------------------------------------------------===//
3002// CFG Graphviz Visualization
3003//===----------------------------------------------------------------------===//
3004
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003005
3006#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003007static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003008#endif
3009
Chris Lattnerc61089a2009-06-30 01:26:17 +00003010void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003011#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003012 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003013 GraphHelper = &H;
3014 llvm::ViewGraph(this,"CFG");
3015 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003016#endif
3017}
3018
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003019namespace llvm {
3020template<>
3021struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003022
3023 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3024
3025 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003026
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003027#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003028 std::string OutSStr;
3029 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003030 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003031 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003032
3033 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3034
3035 // Process string output to make it nicer...
3036 for (unsigned i = 0; i != OutStr.length(); ++i)
3037 if (OutStr[i] == '\n') { // Left justify
3038 OutStr[i] = '\\';
3039 OutStr.insert(OutStr.begin()+i+1, 'l');
3040 }
Mike Stump31feda52009-07-17 01:31:16 +00003041
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003042 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003043#else
3044 return "";
3045#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003046 }
3047};
3048} // end namespace llvm