blob: c5ac453d4dd70994c09f726be75f96e8b97ded4a [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);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000261 CFGBlock *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
262 AddStmtChoice asc);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000263 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
264 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000265 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
266 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000267 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000268 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
269 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000270 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
271 AddStmtChoice asc);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000272 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000273 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000274 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000275 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000276 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000277 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000278 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000279 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000280 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000281 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
282 CFGBlock *VisitDoStmt(DoStmt *D);
283 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000284 CFGBlock *VisitGotoStmt(GotoStmt* G);
285 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000286 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000287 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
288 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000289 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000290 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
291 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
292 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
293 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
294 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
295 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000296 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
297 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000298 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000299 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000300 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000301
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000302 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
303 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000304 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000305
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000306 // Visitors to walk an AST and generate destructors of temporaries in
307 // full expression.
308 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
309 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
310 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
311 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
312 bool BindToTemporary);
313 CFGBlock *VisitConditionalOperatorForTemporaryDtors(ConditionalOperator *E,
314 bool BindToTemporary);
315
Ted Kremenek6065ef62008-04-28 18:00:46 +0000316 // NYS == Not Yet Supported
317 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000318 badCFG = true;
319 return Block;
320 }
Mike Stump31feda52009-07-17 01:31:16 +0000321
Ted Kremenek93668002009-07-17 22:18:43 +0000322 void autoCreateBlock() { if (!Block) Block = createBlock(); }
323 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000324
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000325 CFGBlock *addStmt(Stmt *S) {
326 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000327 }
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000328 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000329 void addAutomaticObjDtors(LocalScope::const_iterator B,
330 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000331 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000332
Marcin Swiderski5e415732010-09-30 23:05:00 +0000333 // Local scopes creation.
334 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
335
Zhongxing Xu81714f22010-10-01 03:00:16 +0000336 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000337 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
338 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
339
340 void addLocalScopeAndDtors(Stmt* S);
341
342 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000343 void AppendStmt(CFGBlock *B, Stmt *S,
344 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
345 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000346 }
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000347 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
348 B->appendInitializer(I, cfg->getBumpVectorContext());
349 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000350 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
351 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
352 }
353 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
354 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
355 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000356 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
357 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
358 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000359
Marcin Swiderski321a7072010-09-30 22:54:37 +0000360 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
361 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
362 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
363 LocalScope::const_iterator E, Stmt* S);
364 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
365 LocalScope::const_iterator B, LocalScope::const_iterator E);
366
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000367 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
368 B->addSuccessor(S, cfg->getBumpVectorContext());
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek963cc312009-07-24 06:55:42 +0000371 /// TryResult - a class representing a variant over the values
372 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
373 /// and is used by the CFGBuilder to decide if a branch condition
374 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000375 class TryResult {
376 int X;
377 public:
378 TryResult(bool b) : X(b ? 1 : 0) {}
379 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek30754282009-07-24 04:47:11 +0000381 bool isTrue() const { return X == 1; }
382 bool isFalse() const { return X == 0; }
383 bool isKnown() const { return X >= 0; }
384 void negate() {
385 assert(isKnown());
386 X ^= 0x1;
387 }
388 };
Mike Stump11289f42009-09-09 15:08:12 +0000389
Mike Stump773582d2009-07-23 23:25:26 +0000390 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
391 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000392 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000393 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000394 return TryResult();
395
Mike Stump773582d2009-07-23 23:25:26 +0000396 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000397 if (!S->isTypeDependent() && !S->isValueDependent() &&
398 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000399 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000400
401 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000402 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000403};
Mike Stump31feda52009-07-17 01:31:16 +0000404
Douglas Gregor4619e432008-12-05 23:32:09 +0000405// FIXME: Add support for dependent-sized array types in C++?
406// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000407static VariableArrayType* FindVA(Type* t) {
408 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
409 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
410 if (vat->getSizeExpr())
411 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000412
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000413 t = vt->getElementType().getTypePtr();
414 }
Mike Stump31feda52009-07-17 01:31:16 +0000415
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000416 return 0;
417}
Mike Stump31feda52009-07-17 01:31:16 +0000418
419/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
420/// arbitrary statement. Examples include a single expression or a function
421/// body (compound statement). The ownership of the returned CFG is
422/// transferred to the caller. If CFG construction fails, this method returns
423/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000424CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000425 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000426
Mike Stump0d76d072009-07-20 23:24:15 +0000427 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000428 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000429 if (!Statement)
430 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000431
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000432 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000433
434 // Create an empty block that will serve as the exit block for the CFG. Since
435 // this is the first block added to the CFG, it will be implicitly registered
436 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000437 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000438 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000439 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000440
Marcin Swiderski20b88732010-10-05 05:37:00 +0000441 if (BuildOpts.AddImplicitDtors)
442 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
443 addImplicitDtorsForDestructor(DD);
444
Ted Kremenek9aae5132007-08-23 21:42:29 +0000445 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000446 CFGBlock *B = addStmt(Statement);
447
448 if (badCFG)
449 return NULL;
450
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000451 // For C++ constructor add initializers to CFG.
452 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
453 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
454 E = CD->init_rend(); I != E; ++I) {
455 B = addInitializer(*I);
456 if (badCFG)
457 return NULL;
458 }
459 }
460
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000461 if (B)
462 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000463
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000464 // Backpatch the gotos whose label -> block mappings we didn't know when we
465 // encountered them.
466 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
467 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000468
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000469 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000470 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
471 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000472
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000473 // If there is no target for the goto, then we are looking at an
474 // incomplete AST. Handle this by not registering a successor.
475 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000476
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000477 JumpTarget JT = LI->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +0000478 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000479 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000480 }
481
482 // Add successors to the Indirect Goto Dispatch block (if we have one).
483 if (CFGBlock* B = cfg->getIndirectGotoBlock())
484 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
485 E = AddressTakenLabels.end(); I != E; ++I ) {
486
487 // Lookup the target block.
488 LabelMapTy::iterator LI = LabelMap.find(*I);
489
490 // If there is no target block that contains label, then we are looking
491 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000492 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000493
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000494 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000495 }
Mike Stump31feda52009-07-17 01:31:16 +0000496
Mike Stump31feda52009-07-17 01:31:16 +0000497 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000498 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000499
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000500 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000501}
Mike Stump31feda52009-07-17 01:31:16 +0000502
Ted Kremenek9aae5132007-08-23 21:42:29 +0000503/// createBlock - Used to lazily create blocks that are connected
504/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000505CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000506 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000507 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000508 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000509 return B;
510}
Mike Stump31feda52009-07-17 01:31:16 +0000511
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000512/// addInitializer - Add C++ base or member initializer element to CFG.
513CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
514 if (!BuildOpts.AddInitializers)
515 return Block;
516
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000517 bool IsReference = false;
518 bool HasTemporaries = false;
519
520 // Destructors of temporaries in initialization expression should be called
521 // after initialization finishes.
522 Expr *Init = I->getInit();
523 if (Init) {
524 if (FieldDecl *FD = I->getMember())
525 IsReference = FD->getType()->isReferenceType();
526 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
527
528 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
529 // Generate destructors for temporaries in initialization expression.
530 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
531 IsReference);
532 }
533 }
534
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000535 autoCreateBlock();
536 appendInitializer(Block, I);
537
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000538 if (Init) {
539 AddStmtChoice asc = IsReference
540 ? AddStmtChoice::AsLValueNotAlwaysAdd
541 : AddStmtChoice::NotAlwaysAdd;
542 if (HasTemporaries)
543 // For expression with temporaries go directly to subexpression to omit
544 // generating destructors for the second time.
545 return Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
546 return Visit(Init, asc);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000547 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000548
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000549 return Block;
550}
551
Marcin Swiderski5e415732010-09-30 23:05:00 +0000552/// addAutomaticObjDtors - Add to current block automatic objects destructors
553/// for objects in range of local scope positions. Use S as trigger statement
554/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000555void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
556 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000557 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000558 return;
559
Marcin Swiderski5e415732010-09-30 23:05:00 +0000560 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000561 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000562
563 autoCreateBlock();
564 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000565}
566
Marcin Swiderski20b88732010-10-05 05:37:00 +0000567/// addImplicitDtorsForDestructor - Add implicit destructors generated for
568/// base and member objects in destructor.
569void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
570 assert (BuildOpts.AddImplicitDtors
571 && "Can be called only when dtors should be added");
572 const CXXRecordDecl *RD = DD->getParent();
573
574 // At the end destroy virtual base objects.
575 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
576 VE = RD->vbases_end(); VI != VE; ++VI) {
577 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
578 if (!CD->hasTrivialDestructor()) {
579 autoCreateBlock();
580 appendBaseDtor(Block, VI);
581 }
582 }
583
584 // Before virtual bases destroy direct base objects.
585 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
586 BE = RD->bases_end(); BI != BE; ++BI) {
587 if (!BI->isVirtual()) {
588 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
589 if (!CD->hasTrivialDestructor()) {
590 autoCreateBlock();
591 appendBaseDtor(Block, BI);
592 }
593 }
594 }
595
596 // First destroy member objects.
597 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
598 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000599 // Check for constant size array. Set type to array element type.
600 QualType QT = FI->getType();
601 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
602 if (AT->getSize() == 0)
603 continue;
604 QT = AT->getElementType();
605 }
606
607 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000608 if (!CD->hasTrivialDestructor()) {
609 autoCreateBlock();
610 appendMemberDtor(Block, *FI);
611 }
612 }
613}
614
Marcin Swiderski5e415732010-09-30 23:05:00 +0000615/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
616/// way return valid LocalScope object.
617LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
618 if (!Scope) {
619 Scope = cfg->getAllocator().Allocate<LocalScope>();
620 new (Scope) LocalScope(ScopePos);
621 }
622 return Scope;
623}
624
625/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000626/// that should create implicit scope (e.g. if/else substatements).
627void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000628 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000629 return;
630
631 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000632
633 // For compound statement we will be creating explicit scope.
634 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
635 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
636 ; BI != BE; ++BI) {
637 Stmt* SI = *BI;
638 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
639 SI = LS->getSubStmt();
640 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
641 Scope = addLocalScopeForDeclStmt(DS, Scope);
642 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000643 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000644 }
645
646 // For any other statement scope will be implicit and as such will be
647 // interesting only for DeclStmt.
648 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
649 S = LS->getSubStmt();
650 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000651 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000652}
653
654/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
655/// reuse Scope if not NULL.
656LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000657 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000658 if (!BuildOpts.AddImplicitDtors)
659 return Scope;
660
661 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
662 ; DI != DE; ++DI) {
663 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
664 Scope = addLocalScopeForVarDecl(VD, Scope);
665 }
666 return Scope;
667}
668
669/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
670/// create add scope for automatic objects and temporary objects bound to
671/// const reference. Will reuse Scope if not NULL.
672LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000673 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000674 if (!BuildOpts.AddImplicitDtors)
675 return Scope;
676
677 // Check if variable is local.
678 switch (VD->getStorageClass()) {
679 case SC_None:
680 case SC_Auto:
681 case SC_Register:
682 break;
683 default: return Scope;
684 }
685
686 // Check for const references bound to temporary. Set type to pointee.
687 QualType QT = VD->getType();
688 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
689 QT = RT->getPointeeType();
690 if (!QT.isConstQualified())
691 return Scope;
692 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
693 return Scope;
694 }
695
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000696 // Check for constant size array. Set type to array element type.
697 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
698 if (AT->getSize() == 0)
699 return Scope;
700 QT = AT->getElementType();
701 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000702
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000703 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000704 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
705 if (!CD->hasTrivialDestructor()) {
706 // Add the variable to scope
707 Scope = createOrReuseLocalScope(Scope);
708 Scope->addVar(VD);
709 ScopePos = Scope->begin();
710 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000711 return Scope;
712}
713
714/// addLocalScopeAndDtors - For given statement add local scope for it and
715/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
716void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
717 if (!BuildOpts.AddImplicitDtors)
718 return;
719
720 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000721 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000722 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
723}
724
Marcin Swiderski321a7072010-09-30 22:54:37 +0000725/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
726/// automatic storage duration to CFGBlock's elements vector. Insertion will be
727/// performed in place specified with iterator.
728void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
729 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
730 BumpVectorContext& C = cfg->getBumpVectorContext();
731 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
732 while (B != E)
733 I = Blk->insertAutomaticObjDtor(I, *B++, S);
734}
735
736/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
737/// automatic storage duration to CFGBlock's elements vector. Elements will be
738/// appended to physical end of the vector which happens to be logical
739/// beginning.
740void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
741 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
742 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
743}
744
745/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
746/// variables with automatic storage duration to CFGBlock's elements vector.
747/// Elements will be prepended to physical beginning of the vector which
748/// happens to be logical end. Use blocks terminator as statement that specifies
749/// destructors call site.
750void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
751 LocalScope::const_iterator B, LocalScope::const_iterator E) {
752 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
753}
754
Ted Kremenek93668002009-07-17 22:18:43 +0000755/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000756/// blocks for ternary operators, &&, and ||. We also process "," and
757/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000758CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000759tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000760 if (!S) {
761 badCFG = true;
762 return 0;
763 }
Ted Kremenek93668002009-07-17 22:18:43 +0000764 switch (S->getStmtClass()) {
765 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000766 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000767
768 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000769 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000770
Ted Kremenek93668002009-07-17 22:18:43 +0000771 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000772 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000773
Ted Kremenek93668002009-07-17 22:18:43 +0000774 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000775 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000776
Ted Kremenek93668002009-07-17 22:18:43 +0000777 case Stmt::BreakStmtClass:
778 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000779
Ted Kremenek93668002009-07-17 22:18:43 +0000780 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000781 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000782 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Ted Kremenek93668002009-07-17 22:18:43 +0000784 case Stmt::CaseStmtClass:
785 return VisitCaseStmt(cast<CaseStmt>(S));
786
787 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000788 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000789
Ted Kremenek93668002009-07-17 22:18:43 +0000790 case Stmt::CompoundStmtClass:
791 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000792
Ted Kremenek93668002009-07-17 22:18:43 +0000793 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000794 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Ted Kremenek93668002009-07-17 22:18:43 +0000796 case Stmt::ContinueStmtClass:
797 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000798
Ted Kremenekb27378c2010-01-19 20:40:33 +0000799 case Stmt::CXXCatchStmtClass:
800 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
801
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000802 case Stmt::CXXExprWithTemporariesClass:
803 return VisitCXXExprWithTemporaries(cast<CXXExprWithTemporaries>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000804
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000805 case Stmt::CXXBindTemporaryExprClass:
806 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
807
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000808 case Stmt::CXXConstructExprClass:
809 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
810
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000811 case Stmt::CXXFunctionalCastExprClass:
812 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
813
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000814 case Stmt::CXXTemporaryObjectExprClass:
815 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
816
Zhongxing Xu7e612172010-04-13 09:38:01 +0000817 case Stmt::CXXMemberCallExprClass:
818 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
819
Ted Kremenekb27378c2010-01-19 20:40:33 +0000820 case Stmt::CXXThrowExprClass:
821 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000822
Ted Kremenekb27378c2010-01-19 20:40:33 +0000823 case Stmt::CXXTryStmtClass:
824 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000825
Ted Kremenek93668002009-07-17 22:18:43 +0000826 case Stmt::DeclStmtClass:
827 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000828
Ted Kremenek93668002009-07-17 22:18:43 +0000829 case Stmt::DefaultStmtClass:
830 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000831
Ted Kremenek93668002009-07-17 22:18:43 +0000832 case Stmt::DoStmtClass:
833 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000834
Ted Kremenek93668002009-07-17 22:18:43 +0000835 case Stmt::ForStmtClass:
836 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000837
Ted Kremenek93668002009-07-17 22:18:43 +0000838 case Stmt::GotoStmtClass:
839 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000840
Ted Kremenek93668002009-07-17 22:18:43 +0000841 case Stmt::IfStmtClass:
842 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000843
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000844 case Stmt::ImplicitCastExprClass:
845 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
846
Ted Kremenek93668002009-07-17 22:18:43 +0000847 case Stmt::IndirectGotoStmtClass:
848 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000849
Ted Kremenek93668002009-07-17 22:18:43 +0000850 case Stmt::LabelStmtClass:
851 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000852
Ted Kremenek5868ec62010-04-11 17:02:10 +0000853 case Stmt::MemberExprClass:
854 return VisitMemberExpr(cast<MemberExpr>(S), asc);
855
Ted Kremenek93668002009-07-17 22:18:43 +0000856 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000857 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
858
Ted Kremenek93668002009-07-17 22:18:43 +0000859 case Stmt::ObjCAtSynchronizedStmtClass:
860 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000861
Ted Kremenek93668002009-07-17 22:18:43 +0000862 case Stmt::ObjCAtThrowStmtClass:
863 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000864
Ted Kremenek93668002009-07-17 22:18:43 +0000865 case Stmt::ObjCAtTryStmtClass:
866 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000867
Ted Kremenek93668002009-07-17 22:18:43 +0000868 case Stmt::ObjCForCollectionStmtClass:
869 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenek93668002009-07-17 22:18:43 +0000871 case Stmt::ParenExprClass:
872 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000873 goto tryAgain;
874
Ted Kremenek93668002009-07-17 22:18:43 +0000875 case Stmt::NullStmtClass:
876 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000877
Ted Kremenek93668002009-07-17 22:18:43 +0000878 case Stmt::ReturnStmtClass:
879 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000880
Ted Kremenek93668002009-07-17 22:18:43 +0000881 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000882 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000883
Ted Kremenek93668002009-07-17 22:18:43 +0000884 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000885 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000886
Ted Kremenek93668002009-07-17 22:18:43 +0000887 case Stmt::SwitchStmtClass:
888 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000889
Zhanyong Wan6dace612010-11-22 08:45:56 +0000890 case Stmt::UnaryOperatorClass:
891 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
892
Ted Kremenek93668002009-07-17 22:18:43 +0000893 case Stmt::WhileStmtClass:
894 return VisitWhileStmt(cast<WhileStmt>(S));
895 }
896}
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000898CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
899 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000900 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000901 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Ted Kremenek93668002009-07-17 22:18:43 +0000904 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000905}
Mike Stump31feda52009-07-17 01:31:16 +0000906
Ted Kremenek93668002009-07-17 22:18:43 +0000907/// VisitChildren - Visit the children of a Stmt.
908CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
909 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000910 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000911 E = Terminator->child_end(); I != E; ++I) {
912 if (*I) B = Visit(*I);
913 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000914 return B;
915}
Mike Stump11289f42009-09-09 15:08:12 +0000916
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000917CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
918 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000919 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000920
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000921 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000922 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000923 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000924 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000925
Ted Kremenek9aae5132007-08-23 21:42:29 +0000926 return Block;
927}
Mike Stump11289f42009-09-09 15:08:12 +0000928
Zhanyong Wan6dace612010-11-22 08:45:56 +0000929CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
930 AddStmtChoice asc) {
931 if (asc.alwaysAdd()) {
932 autoCreateBlock();
933 AppendStmt(Block, U, asc);
934 }
935
936 bool asLVal = U->isIncrementDecrementOp();
937 return Visit(U->getSubExpr(),
938 asLVal ? AddStmtChoice::AsLValueNotAlwaysAdd :
939 AddStmtChoice::NotAlwaysAdd);
940}
941
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000942CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
943 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000944 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000945 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000946 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000947
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000948 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000949 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000950
Ted Kremenek93668002009-07-17 22:18:43 +0000951 // create the block evaluating the LHS
952 CFGBlock* LHSBlock = createBlock(false);
953 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000954
Ted Kremenek93668002009-07-17 22:18:43 +0000955 // create the block evaluating the RHS
956 Succ = ConfluenceBlock;
957 Block = NULL;
958 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000959
960 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000961 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000962 return 0;
963 }
964 else {
965 // Create an empty block for cases where the RHS doesn't require
966 // any explicit statements in the CFG.
967 RHSBlock = createBlock();
968 }
Mike Stump11289f42009-09-09 15:08:12 +0000969
Mike Stump773582d2009-07-23 23:25:26 +0000970 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000971 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000972 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000973 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000974
Ted Kremenek93668002009-07-17 22:18:43 +0000975 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000976 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000977 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
978 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000979 } else {
John McCalle3027922010-08-25 11:45:40 +0000980 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000981 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
982 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
Ted Kremenek93668002009-07-17 22:18:43 +0000985 // Generate the blocks for evaluating the LHS.
986 Block = LHSBlock;
987 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000988 }
John McCalle3027922010-08-25 11:45:40 +0000989 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000990 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000991 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000992 addStmt(B->getRHS());
993 return addStmt(B->getLHS());
994 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000995 else if (B->isAssignmentOp()) {
996 if (asc.alwaysAdd()) {
997 autoCreateBlock();
998 AppendStmt(Block, B, asc);
999 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001000
Marcin Swiderski77232492010-10-24 08:21:40 +00001001 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
1002 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001003 }
Mike Stump11289f42009-09-09 15:08:12 +00001004
Marcin Swiderski77232492010-10-24 08:21:40 +00001005 if (asc.alwaysAdd()) {
1006 autoCreateBlock();
1007 AppendStmt(Block, B, asc);
1008 }
1009
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001010 CFGBlock *RBlock = Visit(B->getRHS());
1011 CFGBlock *LBlock = Visit(B->getLHS());
1012 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1013 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1014 // return RBlock. Otherwise we'll incorrectly return NULL.
1015 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001016}
1017
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001018CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1019 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001020 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001021 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001022 }
1023 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001024}
1025
Ted Kremenek93668002009-07-17 22:18:43 +00001026CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1027 // "break" is a control-flow statement. Thus we stop processing the current
1028 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001029 if (badCFG)
1030 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001031
Ted Kremenek93668002009-07-17 22:18:43 +00001032 // Now create a new block that ends with the break statement.
1033 Block = createBlock(false);
1034 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001035
Ted Kremenek93668002009-07-17 22:18:43 +00001036 // If there is no target for the break, then we are looking at an incomplete
1037 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001038 if (BreakJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001039 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001040 AddSuccessor(Block, BreakJumpTarget.Block);
1041 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001042 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001043
1044
Ted Kremenek9aae5132007-08-23 21:42:29 +00001045 return Block;
1046}
Mike Stump11289f42009-09-09 15:08:12 +00001047
Mike Stump04c68512010-01-21 15:20:48 +00001048static bool CanThrow(Expr *E) {
1049 QualType Ty = E->getType();
1050 if (Ty->isFunctionPointerType())
1051 Ty = Ty->getAs<PointerType>()->getPointeeType();
1052 else if (Ty->isBlockPointerType())
1053 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001054
Mike Stump04c68512010-01-21 15:20:48 +00001055 const FunctionType *FT = Ty->getAs<FunctionType>();
1056 if (FT) {
1057 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1058 if (Proto->hasEmptyExceptionSpec())
1059 return false;
1060 }
1061 return true;
1062}
1063
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001064CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001065 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001066 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001067 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001068 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001069 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001070
Mike Stump04c68512010-01-21 15:20:48 +00001071 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001072
1073 // Languages without exceptions are assumed to not throw.
1074 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001075 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001076 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001077 }
1078
1079 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001080 if (FD->hasAttr<NoReturnAttr>())
1081 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001082 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001083 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001084 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001085
Mike Stump04c68512010-01-21 15:20:48 +00001086 if (!CanThrow(C->getCallee()))
1087 AddEHEdge = false;
1088
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001089 if (!NoReturn && !AddEHEdge) {
1090 if (asc.asLValue())
1091 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1092 else
1093 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Mike Stump92244b02010-01-19 22:00:14 +00001096 if (Block) {
1097 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001098 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001099 return 0;
1100 }
Mike Stump11289f42009-09-09 15:08:12 +00001101
Mike Stump92244b02010-01-19 22:00:14 +00001102 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001103 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001104
Mike Stump92244b02010-01-19 22:00:14 +00001105 if (NoReturn) {
1106 // Wire this to the exit block directly.
1107 AddSuccessor(Block, &cfg->getExit());
1108 }
Mike Stump04c68512010-01-21 15:20:48 +00001109 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001110 // Add exceptional edges.
1111 if (TryTerminatedBlock)
1112 AddSuccessor(Block, TryTerminatedBlock);
1113 else
1114 AddSuccessor(Block, &cfg->getExit());
1115 }
Mike Stump11289f42009-09-09 15:08:12 +00001116
Mike Stump8c5d7992009-07-25 21:26:53 +00001117 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001118}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001119
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001120CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1121 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001122 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001123 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001124 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001125 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001126
Ted Kremenek5868ec62010-04-11 17:02:10 +00001127 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1128 : AddStmtChoice::AlwaysAdd;
1129
Ted Kremenek21822592009-07-17 18:20:32 +00001130 Succ = ConfluenceBlock;
1131 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001132 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001133 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001134 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Ted Kremenek21822592009-07-17 18:20:32 +00001136 Succ = ConfluenceBlock;
1137 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001138 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001139 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001140 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001141
Ted Kremenek21822592009-07-17 18:20:32 +00001142 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001143 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001144 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001145 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1146 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001147 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001148 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001149}
Mike Stump11289f42009-09-09 15:08:12 +00001150
1151
1152CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001153 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001154 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001155
1156 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1157 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001158 // If we hit a segment of code just containing ';' (NullStmts), we can
1159 // get a null block back. In such cases, just use the LastBlock
1160 if (CFGBlock *newBlock = addStmt(*I))
1161 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001162
Ted Kremenekce499c22009-08-27 23:16:26 +00001163 if (badCFG)
1164 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001165 }
Mike Stump92244b02010-01-19 22:00:14 +00001166
Ted Kremenek93668002009-07-17 22:18:43 +00001167 return LastBlock;
1168}
Mike Stump11289f42009-09-09 15:08:12 +00001169
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001170CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1171 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001172 // Create the confluence block that will "merge" the results of the ternary
1173 // expression.
1174 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001175 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001176 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001177 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001178
Ted Kremenek5868ec62010-04-11 17:02:10 +00001179 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1180 : AddStmtChoice::AlwaysAdd;
1181
Ted Kremenek51d40b02009-07-17 18:15:54 +00001182 // Create a block for the LHS expression if there is an LHS expression. A
1183 // GCC extension allows LHS to be NULL, causing the condition to be the
1184 // value that is returned instead.
1185 // e.g: x ?: y is shorthand for: x ? x : y;
1186 Succ = ConfluenceBlock;
1187 Block = NULL;
1188 CFGBlock* LHSBlock = NULL;
1189 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001190 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001191 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001192 return 0;
1193 Block = NULL;
1194 }
Mike Stump11289f42009-09-09 15:08:12 +00001195
Ted Kremenek51d40b02009-07-17 18:15:54 +00001196 // Create the block for the RHS expression.
1197 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001198 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001199 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001200 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001201
Ted Kremenek51d40b02009-07-17 18:15:54 +00001202 // Create the block that will contain the condition.
1203 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001204
Mike Stump773582d2009-07-23 23:25:26 +00001205 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001206 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001207 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001208 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001209 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001210 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001211 // If we know the condition is false, add NULL as the successor for
1212 // the block containing the condition. In this case, the confluence
1213 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001214 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001215 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001216 } else {
1217 // If we have no LHS expression, add the ConfluenceBlock as a direct
1218 // successor for the block containing the condition. Moreover, we need to
1219 // reverse the order of the predecessors in the ConfluenceBlock because
1220 // the RHSBlock will have been added to the succcessors already, and we
1221 // want the first predecessor to the the block containing the expression
1222 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001223 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek18fb1662010-11-15 22:59:22 +00001224 // Note that there can possibly only be one predecessor if one of the
1225 // subexpressions resulted in calling a noreturn function.
Mike Stump0d76d072009-07-20 23:24:15 +00001226 std::reverse(ConfluenceBlock->pred_begin(),
1227 ConfluenceBlock->pred_end());
1228 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001231 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001232 Block->setTerminator(C);
1233 return addStmt(C->getCond());
1234}
1235
Ted Kremenek93668002009-07-17 22:18:43 +00001236CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001237 if (DS->isSingleDecl())
1238 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001239
Ted Kremenek93668002009-07-17 22:18:43 +00001240 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001241
Ted Kremenek93668002009-07-17 22:18:43 +00001242 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1243 typedef llvm::SmallVector<Decl*,10> BufTy;
1244 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001245
Ted Kremenek93668002009-07-17 22:18:43 +00001246 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1247 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1248 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1249 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001250
Ted Kremenek93668002009-07-17 22:18:43 +00001251 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1252 // automatically freed with the CFG.
1253 DeclGroupRef DG(*I);
1254 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001255 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001256 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001257
Ted Kremenek93668002009-07-17 22:18:43 +00001258 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001259 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001260 }
Mike Stump11289f42009-09-09 15:08:12 +00001261
1262 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001263}
Mike Stump11289f42009-09-09 15:08:12 +00001264
Ted Kremenek93668002009-07-17 22:18:43 +00001265/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001266/// DeclStmts and initializers in them.
1267CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1268 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001269
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001270 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001271
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001272 if (!VD) {
1273 autoCreateBlock();
1274 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001275 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001278 bool IsReference = false;
1279 bool HasTemporaries = false;
1280
1281 // Destructors of temporaries in initialization expression should be called
1282 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001283 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001284 if (Init) {
1285 IsReference = VD->getType()->isReferenceType();
1286 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
1287
1288 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1289 // Generate destructors for temporaries in initialization expression.
1290 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
1291 IsReference);
1292 }
1293 }
1294
1295 autoCreateBlock();
1296 AppendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001297
Ted Kremenek93668002009-07-17 22:18:43 +00001298 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001299 AddStmtChoice asc = IsReference
1300 ? AddStmtChoice::AsLValueNotAlwaysAdd
1301 : AddStmtChoice::NotAlwaysAdd;
1302 if (HasTemporaries)
1303 // For expression with temporaries go directly to subexpression to omit
1304 // generating destructors for the second time.
1305 Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
1306 else
1307 Visit(Init, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
Ted Kremenek93668002009-07-17 22:18:43 +00001310 // If the type of VD is a VLA, then we must process its size expressions.
1311 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1312 VA = FindVA(VA->getElementType().getTypePtr()))
1313 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001314
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001315 // Remove variable from local scope.
1316 if (ScopePos && VD == *ScopePos)
1317 ++ScopePos;
1318
Ted Kremenek93668002009-07-17 22:18:43 +00001319 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001320}
1321
1322CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001323 // We may see an if statement in the middle of a basic block, or it may be the
1324 // first statement we are processing. In either case, we create a new basic
1325 // block. First, we create the blocks for the then...else statements, and
1326 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001327 // middle of a block, we stop processing that block. That block is then the
1328 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001329
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001330 // Save local scope position because in case of condition variable ScopePos
1331 // won't be restored when traversing AST.
1332 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1333
1334 // Create local scope for possible condition variable.
1335 // Store scope position. Add implicit destructor.
1336 if (VarDecl* VD = I->getConditionVariable()) {
1337 LocalScope::const_iterator BeginScopePos = ScopePos;
1338 addLocalScopeForVarDecl(VD);
1339 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1340 }
1341
Mike Stump31feda52009-07-17 01:31:16 +00001342 // The block we were proccessing is now finished. Make it the successor
1343 // block.
1344 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001345 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001346 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001347 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001348 }
Mike Stump31feda52009-07-17 01:31:16 +00001349
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001350 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001352
Ted Kremenek9aae5132007-08-23 21:42:29 +00001353 if (Stmt* Else = I->getElse()) {
1354 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek9aae5132007-08-23 21:42:29 +00001356 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001357 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001358 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001359
1360 // If branch is not a compound statement create implicit scope
1361 // and add destructors.
1362 if (!isa<CompoundStmt>(Else))
1363 addLocalScopeAndDtors(Else);
1364
Ted Kremenek93668002009-07-17 22:18:43 +00001365 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001366
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001367 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1368 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001369 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001370 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001371 return 0;
1372 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001373 }
Mike Stump31feda52009-07-17 01:31:16 +00001374
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001375 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001376 CFGBlock* ThenBlock;
1377 {
1378 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001379 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001381 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001382
1383 // If branch is not a compound statement create implicit scope
1384 // and add destructors.
1385 if (!isa<CompoundStmt>(Then))
1386 addLocalScopeAndDtors(Then);
1387
Ted Kremenek93668002009-07-17 22:18:43 +00001388 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001389
Ted Kremenek1b379512009-04-01 03:52:47 +00001390 if (!ThenBlock) {
1391 // We can reach here if the "then" body has all NullStmts.
1392 // Create an empty block so we can distinguish between true and false
1393 // branches in path-sensitive analyses.
1394 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001395 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001396 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001397 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001398 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001399 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001400 }
1401
Mike Stump31feda52009-07-17 01:31:16 +00001402 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001403 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001404
Ted Kremenek9aae5132007-08-23 21:42:29 +00001405 // Set the terminator of the new block to the If statement.
1406 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001407
Mike Stump773582d2009-07-23 23:25:26 +00001408 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001409 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001410
Ted Kremenek9aae5132007-08-23 21:42:29 +00001411 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001412 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1413 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001414
1415 // Add the condition as the last statement in the new block. This may create
1416 // new blocks as the condition may contain control-flow. Any newly created
1417 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001418 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001419
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001420 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1421 // and the condition variable initialization to the CFG.
1422 if (VarDecl *VD = I->getConditionVariable()) {
1423 if (Expr *Init = VD->getInit()) {
1424 autoCreateBlock();
1425 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1426 addStmt(Init);
1427 }
1428 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001429
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001430 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001431}
Mike Stump31feda52009-07-17 01:31:16 +00001432
1433
Ted Kremenek9aae5132007-08-23 21:42:29 +00001434CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001435 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001436 //
Mike Stump31feda52009-07-17 01:31:16 +00001437 // NOTE: If a "return" appears in the middle of a block, this means that the
1438 // code afterwards is DEAD (unreachable). We still keep a basic block
1439 // for that code; a simple "mark-and-sweep" from the entry block will be
1440 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001441
1442 // Create the new block.
1443 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001444
Ted Kremenek9aae5132007-08-23 21:42:29 +00001445 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001446 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001447 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001448
1449 // Add the return statement to the block. This may create new blocks if R
1450 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001451 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001452}
1453
1454CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1455 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001456 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001457 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001458
Ted Kremenek93668002009-07-17 22:18:43 +00001459 if (!LabelBlock) // This can happen when the body is empty, i.e.
1460 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001461
Ted Kremenek93668002009-07-17 22:18:43 +00001462 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001463 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001464
1465 // Labels partition blocks, so this is the end of the basic block we were
1466 // processing (L is the block's label). Because this is label (and we have
1467 // already processed the substatement) there is no extra control-flow to worry
1468 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001469 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001470 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001471 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001472
1473 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001474 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001475
Ted Kremenek9aae5132007-08-23 21:42:29 +00001476 // This block is now the implicit successor of other blocks.
1477 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenek9aae5132007-08-23 21:42:29 +00001479 return LabelBlock;
1480}
1481
1482CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001483 // Goto is a control-flow statement. Thus we stop processing the current
1484 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001485
Ted Kremenek9aae5132007-08-23 21:42:29 +00001486 Block = createBlock(false);
1487 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001488
1489 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001490 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenek9aae5132007-08-23 21:42:29 +00001492 if (I == LabelMap.end())
1493 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001494 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1495 else {
1496 JumpTarget JT = I->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001497 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001498 AddSuccessor(Block, JT.Block);
1499 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001500
Mike Stump31feda52009-07-17 01:31:16 +00001501 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001502}
1503
1504CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001505 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001506
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001507 // Save local scope position because in case of condition variable ScopePos
1508 // won't be restored when traversing AST.
1509 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1510
1511 // Create local scope for init statement and possible condition variable.
1512 // Add destructor for init statement and condition variable.
1513 // Store scope position for continue statement.
1514 if (Stmt* Init = F->getInit())
1515 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001516 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1517
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001518 if (VarDecl* VD = F->getConditionVariable())
1519 addLocalScopeForVarDecl(VD);
1520 LocalScope::const_iterator ContinueScopePos = ScopePos;
1521
1522 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1523
Mike Stump014b3ea2009-07-21 01:12:51 +00001524 // "for" is a control-flow statement. Thus we stop processing the current
1525 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001526 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001527 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001528 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001529 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001530 } else
1531 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001532
Ted Kremenek304a9532010-05-21 20:30:15 +00001533 // Save the current value for the break targets.
1534 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001535 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001536 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001537
Mike Stump31feda52009-07-17 01:31:16 +00001538 // Because of short-circuit evaluation, the condition of the loop can span
1539 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1540 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001541 CFGBlock* ExitConditionBlock = createBlock(false);
1542 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001543
Ted Kremenek81e14852007-08-27 19:46:09 +00001544 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001545 ExitConditionBlock->setTerminator(F);
1546
1547 // Now add the actual condition to the condition block. Because the condition
1548 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001549 if (Stmt* C = F->getCond()) {
1550 Block = ExitConditionBlock;
1551 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001552 assert(Block == EntryConditionBlock ||
1553 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001554
1555 // If this block contains a condition variable, add both the condition
1556 // variable and initializer to the CFG.
1557 if (VarDecl *VD = F->getConditionVariable()) {
1558 if (Expr *Init = VD->getInit()) {
1559 autoCreateBlock();
1560 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1561 EntryConditionBlock = addStmt(Init);
1562 assert(Block == EntryConditionBlock);
1563 }
1564 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001565
Ted Kremenek55957a82009-05-02 00:13:27 +00001566 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001567 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001568 return 0;
1569 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001570 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001571
Mike Stump31feda52009-07-17 01:31:16 +00001572 // The condition block is the implicit successor for the loop body as well as
1573 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001574 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001575
Mike Stump773582d2009-07-23 23:25:26 +00001576 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001577 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001578
Mike Stump773582d2009-07-23 23:25:26 +00001579 if (F->getCond())
1580 KnownVal = TryEvaluateBool(F->getCond());
1581
Ted Kremenek9aae5132007-08-23 21:42:29 +00001582 // Now create the loop body.
1583 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001584 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001585
Ted Kremenek304a9532010-05-21 20:30:15 +00001586 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001587 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1588 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001589
Ted Kremeneke9610502007-08-30 18:39:40 +00001590 // Create a new block to contain the (bottom) of the loop body.
1591 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001592
1593 // Loop body should end with destructor of Condition variable (if any).
1594 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001595
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001596 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001597 // Generate increment code in its own basic block. This is the target of
1598 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001599 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001600 } else {
1601 // No increment code. Create a special, empty, block that is used as the
1602 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001603 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001604 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001605 }
Mike Stump31feda52009-07-17 01:31:16 +00001606
Ted Kremenek902393b2009-04-28 00:51:56 +00001607 // Finish up the increment (or empty) block if it hasn't been already.
1608 if (Block) {
1609 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001610 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001611 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001612 Block = 0;
1613 }
Mike Stump31feda52009-07-17 01:31:16 +00001614
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001615 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001616
Ted Kremenek902393b2009-04-28 00:51:56 +00001617 // The starting block for the loop increment is the block that should
1618 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001619 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001620
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001621 // If body is not a compound statement create implicit scope
1622 // and add destructors.
1623 if (!isa<CompoundStmt>(F->getBody()))
1624 addLocalScopeAndDtors(F->getBody());
1625
Mike Stump31feda52009-07-17 01:31:16 +00001626 // Now populate the body block, and in the process create new blocks as we
1627 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001628 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001629
1630 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001631 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001632 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001633 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001634
Ted Kremenek30754282009-07-24 04:47:11 +00001635 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001636 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001637 }
Mike Stump31feda52009-07-17 01:31:16 +00001638
Ted Kremenek30754282009-07-24 04:47:11 +00001639 // Link up the condition block with the code that follows the loop. (the
1640 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001641 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001642
Ted Kremenek9aae5132007-08-23 21:42:29 +00001643 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001644 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001645 if (Stmt* I = F->getInit()) {
1646 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001647 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001648 } else {
1649 // There is no loop initialization. We are thus basically a while loop.
1650 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001651 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001652 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001653 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001654 }
1655}
1656
Ted Kremenek5868ec62010-04-11 17:02:10 +00001657CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1658 if (asc.alwaysAdd()) {
1659 autoCreateBlock();
1660 AppendStmt(Block, M, asc);
1661 }
1662 return Visit(M->getBase(),
1663 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1664 : AddStmtChoice::AsLValueNotAlwaysAdd);
1665}
1666
Ted Kremenek9d56e642008-11-11 17:10:00 +00001667CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1668 // Objective-C fast enumeration 'for' statements:
1669 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1670 //
1671 // for ( Type newVariable in collection_expression ) { statements }
1672 //
1673 // becomes:
1674 //
1675 // prologue:
1676 // 1. collection_expression
1677 // T. jump to loop_entry
1678 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001679 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001680 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1681 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1682 // TB:
1683 // statements
1684 // T. jump to loop_entry
1685 // FB:
1686 // what comes after
1687 //
1688 // and
1689 //
1690 // Type existingItem;
1691 // for ( existingItem in expression ) { statements }
1692 //
1693 // becomes:
1694 //
Mike Stump31feda52009-07-17 01:31:16 +00001695 // the same with newVariable replaced with existingItem; the binding works
1696 // the same except that for one ObjCForCollectionStmt::getElement() returns
1697 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001698 //
Mike Stump31feda52009-07-17 01:31:16 +00001699
Ted Kremenek9d56e642008-11-11 17:10:00 +00001700 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001701
Ted Kremenek9d56e642008-11-11 17:10:00 +00001702 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001703 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001704 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001705 LoopSuccessor = Block;
1706 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001707 } else
1708 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001709
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001710 // Build the condition blocks.
1711 CFGBlock* ExitConditionBlock = createBlock(false);
1712 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001713
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001714 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001715 ExitConditionBlock->setTerminator(S);
1716
1717 // The last statement in the block should be the ObjCForCollectionStmt, which
1718 // performs the actual binding to 'element' and determines if there are any
1719 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001720 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001721 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001723 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001724 // generate new blocks as necesary. We DON'T add the statement by default to
1725 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001726 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001727 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001728 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001729 return 0;
1730 Block = 0;
1731 }
Mike Stump31feda52009-07-17 01:31:16 +00001732
1733 // The condition block is the implicit successor for the loop body as well as
1734 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001735 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001736
Ted Kremenek9d56e642008-11-11 17:10:00 +00001737 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001738 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001739 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001740 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1741 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1742 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001743
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001744 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1745 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001746
Ted Kremenek93668002009-07-17 22:18:43 +00001747 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001748
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001749 if (!BodyBlock)
1750 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001751 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001752 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001753 return 0;
1754 }
Mike Stump31feda52009-07-17 01:31:16 +00001755
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001756 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001757 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001758 }
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001760 // Link up the condition block with the code that follows the loop.
1761 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001762 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001763
Ted Kremenek9d56e642008-11-11 17:10:00 +00001764 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001765 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001766 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001767}
1768
Ted Kremenek49805452009-05-02 01:49:13 +00001769CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1770 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001771
Ted Kremenek49805452009-05-02 01:49:13 +00001772 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001773 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001774
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001775 // The sync body starts its own basic block. This makes it a little easier
1776 // for diagnostic clients.
1777 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001778 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001779 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001780
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001781 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001782 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001783 }
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001785 // Add the @synchronized to the CFG.
1786 autoCreateBlock();
1787 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1788
Ted Kremenek49805452009-05-02 01:49:13 +00001789 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001790 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001791}
Mike Stump31feda52009-07-17 01:31:16 +00001792
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001793CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001794 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001795 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001796}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001797
Ted Kremenek9aae5132007-08-23 21:42:29 +00001798CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001799 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001800
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001801 // Save local scope position because in case of condition variable ScopePos
1802 // won't be restored when traversing AST.
1803 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1804
1805 // Create local scope for possible condition variable.
1806 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001807 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001808 if (VarDecl* VD = W->getConditionVariable()) {
1809 addLocalScopeForVarDecl(VD);
1810 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1811 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001812
Mike Stump014b3ea2009-07-21 01:12:51 +00001813 // "while" is a control-flow statement. Thus we stop processing the current
1814 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001815 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001816 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001817 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001818 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001819 } else
1820 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001821
1822 // Because of short-circuit evaluation, the condition of the loop can span
1823 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1824 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001825 CFGBlock* ExitConditionBlock = createBlock(false);
1826 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001827
Ted Kremenek81e14852007-08-27 19:46:09 +00001828 // Set the terminator for the "exit" condition block.
1829 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001830
1831 // Now add the actual condition to the condition block. Because the condition
1832 // itself may contain control-flow, new blocks may be created. Thus we update
1833 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001834 if (Stmt* C = W->getCond()) {
1835 Block = ExitConditionBlock;
1836 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001837 // The condition might finish the current 'Block'.
1838 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001839
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001840 // If this block contains a condition variable, add both the condition
1841 // variable and initializer to the CFG.
1842 if (VarDecl *VD = W->getConditionVariable()) {
1843 if (Expr *Init = VD->getInit()) {
1844 autoCreateBlock();
1845 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1846 EntryConditionBlock = addStmt(Init);
1847 assert(Block == EntryConditionBlock);
1848 }
1849 }
1850
Ted Kremenek55957a82009-05-02 00:13:27 +00001851 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001852 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001853 return 0;
1854 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001855 }
Mike Stump31feda52009-07-17 01:31:16 +00001856
1857 // The condition block is the implicit successor for the loop body as well as
1858 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001859 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001860
Mike Stump773582d2009-07-23 23:25:26 +00001861 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001862 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001863
Ted Kremenek9aae5132007-08-23 21:42:29 +00001864 // Process the loop body.
1865 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001866 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001867
1868 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001869 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1870 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1871 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001872
Mike Stump31feda52009-07-17 01:31:16 +00001873 // Create an empty block to represent the transition block for looping back
1874 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001875 Block = 0;
1876 assert(Succ == EntryConditionBlock);
1877 Succ = createBlock();
1878 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001879 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001880
Ted Kremenek9aae5132007-08-23 21:42:29 +00001881 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001882 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001883
Ted Kremenek9aae5132007-08-23 21:42:29 +00001884 // NULL out Block to force lazy instantiation of blocks for the body.
1885 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001886
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001887 // Loop body should end with destructor of Condition variable (if any).
1888 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1889
1890 // If body is not a compound statement create implicit scope
1891 // and add destructors.
1892 if (!isa<CompoundStmt>(W->getBody()))
1893 addLocalScopeAndDtors(W->getBody());
1894
Ted Kremenek9aae5132007-08-23 21:42:29 +00001895 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001896 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001897
Ted Kremeneke9610502007-08-30 18:39:40 +00001898 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001899 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001900 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001901 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001902 return 0;
1903 }
Mike Stump31feda52009-07-17 01:31:16 +00001904
Ted Kremenek30754282009-07-24 04:47:11 +00001905 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001906 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001907 }
Mike Stump31feda52009-07-17 01:31:16 +00001908
Ted Kremenek30754282009-07-24 04:47:11 +00001909 // Link up the condition block with the code that follows the loop. (the
1910 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001911 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001912
1913 // There can be no more statements in the condition block since we loop back
1914 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001915 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001916
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001917 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001918 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001919 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001920}
Mike Stump11289f42009-09-09 15:08:12 +00001921
1922
Ted Kremenek93668002009-07-17 22:18:43 +00001923CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1924 // FIXME: For now we pretend that @catch and the code it contains does not
1925 // exit.
1926 return Block;
1927}
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenek93041ba2008-12-09 20:20:09 +00001929CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1930 // FIXME: This isn't complete. We basically treat @throw like a return
1931 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001932
Ted Kremenek0868eea2009-09-24 18:45:41 +00001933 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001934 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001935 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek93041ba2008-12-09 20:20:09 +00001937 // Create the new block.
1938 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001939
Ted Kremenek93041ba2008-12-09 20:20:09 +00001940 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001941 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001942
1943 // Add the statement to the block. This may create new blocks if S contains
1944 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001945 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001946}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001947
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001948CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001949 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001950 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001951 return 0;
1952
1953 // Create the new block.
1954 Block = createBlock(false);
1955
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001956 if (TryTerminatedBlock)
1957 // The current try statement is the only successor.
1958 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001959 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001960 // otherwise the Exit block is the only successor.
1961 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001962
1963 // Add the statement to the block. This may create new blocks if S contains
1964 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001965 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001966}
1967
Ted Kremenek93668002009-07-17 22:18:43 +00001968CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001969 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001970
Mike Stump8d50b6a2009-07-21 01:27:50 +00001971 // "do...while" is a control-flow statement. Thus we stop processing the
1972 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001973 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001974 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001975 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001976 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001977 } else
1978 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001979
1980 // Because of short-circuit evaluation, the condition of the loop can span
1981 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1982 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001983 CFGBlock* ExitConditionBlock = createBlock(false);
1984 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001985
Ted Kremenek81e14852007-08-27 19:46:09 +00001986 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001987 ExitConditionBlock->setTerminator(D);
1988
1989 // Now add the actual condition to the condition block. Because the condition
1990 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001991 if (Stmt* C = D->getCond()) {
1992 Block = ExitConditionBlock;
1993 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001994 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001995 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001996 return 0;
1997 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001998 }
Mike Stump31feda52009-07-17 01:31:16 +00001999
Ted Kremeneka1523a32008-02-27 07:20:00 +00002000 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002001 Succ = EntryConditionBlock;
2002
Mike Stump773582d2009-07-23 23:25:26 +00002003 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00002004 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002005
Ted Kremenek9aae5132007-08-23 21:42:29 +00002006 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002007 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002008 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002009 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenek9aae5132007-08-23 21:42:29 +00002011 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002012 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2013 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2014 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenek9aae5132007-08-23 21:42:29 +00002016 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002017 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenek9aae5132007-08-23 21:42:29 +00002019 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002020 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002021
Ted Kremenek9aae5132007-08-23 21:42:29 +00002022 // NULL out Block to force lazy instantiation of blocks for the body.
2023 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002024
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002025 // If body is not a compound statement create implicit scope
2026 // and add destructors.
2027 if (!isa<CompoundStmt>(D->getBody()))
2028 addLocalScopeAndDtors(D->getBody());
2029
Ted Kremenek9aae5132007-08-23 21:42:29 +00002030 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002031 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002032
Ted Kremeneke9610502007-08-30 18:39:40 +00002033 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002034 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002035 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002036 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002037 return 0;
2038 }
Mike Stump31feda52009-07-17 01:31:16 +00002039
Ted Kremenek110974d2010-08-17 20:59:56 +00002040 if (!KnownVal.isFalse()) {
2041 // Add an intermediate block between the BodyBlock and the
2042 // ExitConditionBlock to represent the "loop back" transition. Create an
2043 // empty block to represent the transition block for looping back to the
2044 // head of the loop.
2045 // FIXME: Can we do this more efficiently without adding another block?
2046 Block = NULL;
2047 Succ = BodyBlock;
2048 CFGBlock *LoopBackBlock = createBlock();
2049 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002050
Ted Kremenek110974d2010-08-17 20:59:56 +00002051 // Add the loop body entry as a successor to the condition.
2052 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2053 }
2054 else
2055 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002056 }
Mike Stump31feda52009-07-17 01:31:16 +00002057
Ted Kremenek30754282009-07-24 04:47:11 +00002058 // Link up the condition block with the code that follows the loop.
2059 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002060 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002061
2062 // There can be no more statements in the body block(s) since we loop back to
2063 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002064 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002065
Ted Kremenek9aae5132007-08-23 21:42:29 +00002066 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002067 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002068 return BodyBlock;
2069}
2070
2071CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2072 // "continue" is a control-flow statement. Thus we stop processing the
2073 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002074 if (badCFG)
2075 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002076
Ted Kremenek9aae5132007-08-23 21:42:29 +00002077 // Now create a new block that ends with the continue statement.
2078 Block = createBlock(false);
2079 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002080
Ted Kremenek9aae5132007-08-23 21:42:29 +00002081 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002082 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002083 if (ContinueJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00002084 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002085 AddSuccessor(Block, ContinueJumpTarget.Block);
2086 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002087 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002088
Ted Kremenek9aae5132007-08-23 21:42:29 +00002089 return Block;
2090}
Mike Stump11289f42009-09-09 15:08:12 +00002091
Ted Kremenek0747de62009-07-18 00:47:21 +00002092CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002093 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002094
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002095 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002096 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002097 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002098 }
Mike Stump11289f42009-09-09 15:08:12 +00002099
Ted Kremenek93668002009-07-17 22:18:43 +00002100 // VLA types have expressions that must be evaluated.
2101 if (E->isArgumentType()) {
2102 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2103 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2104 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002105 }
Mike Stump11289f42009-09-09 15:08:12 +00002106
Mike Stump31feda52009-07-17 01:31:16 +00002107 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002108}
Mike Stump11289f42009-09-09 15:08:12 +00002109
Ted Kremenek93668002009-07-17 22:18:43 +00002110/// VisitStmtExpr - Utility method to handle (nested) statement
2111/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002112CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2113 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002114 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002115 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002116 }
Ted Kremenek93668002009-07-17 22:18:43 +00002117 return VisitCompoundStmt(SE->getSubStmt());
2118}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002119
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002120CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002121 // "switch" is a control-flow statement. Thus we stop processing the current
2122 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002123 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002124
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002125 // Save local scope position because in case of condition variable ScopePos
2126 // won't be restored when traversing AST.
2127 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2128
2129 // Create local scope for possible condition variable.
2130 // Store scope position. Add implicit destructor.
2131 if (VarDecl* VD = Terminator->getConditionVariable()) {
2132 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2133 addLocalScopeForVarDecl(VD);
2134 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2135 }
2136
Ted Kremenek9aae5132007-08-23 21:42:29 +00002137 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002138 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002139 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002140 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002141 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002142
2143 // Save the current "switch" context.
2144 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002145 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002146 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002147
Mike Stump31feda52009-07-17 01:31:16 +00002148 // Set the "default" case to be the block after the switch statement. If the
2149 // switch statement contains a "default:", this value will be overwritten with
2150 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002151 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002152
Ted Kremenek9aae5132007-08-23 21:42:29 +00002153 // Create a new block that will contain the switch statement.
2154 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002155
Ted Kremenek9aae5132007-08-23 21:42:29 +00002156 // Now process the switch body. The code after the switch is the implicit
2157 // successor.
2158 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002159 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002160
2161 // When visiting the body, the case statements should automatically get linked
2162 // up to the switch. We also don't keep a pointer to the body, since all
2163 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002164 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002165 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002166
2167 // If body is not a compound statement create implicit scope
2168 // and add destructors.
2169 if (!isa<CompoundStmt>(Terminator->getBody()))
2170 addLocalScopeAndDtors(Terminator->getBody());
2171
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002172 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002173 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002174 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002175 return 0;
2176 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002177
Mike Stump31feda52009-07-17 01:31:16 +00002178 // If we have no "default:" case, the default transition is to the code
2179 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002180 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002181
Ted Kremenek81e14852007-08-27 19:46:09 +00002182 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002183 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002184 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002185 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002186 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002187
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002188 // Finally, if the SwitchStmt contains a condition variable, add both the
2189 // SwitchStmt and the condition variable initialization to the CFG.
2190 if (VarDecl *VD = Terminator->getConditionVariable()) {
2191 if (Expr *Init = VD->getInit()) {
2192 autoCreateBlock();
2193 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2194 addStmt(Init);
2195 }
2196 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002197
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002198 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002199}
2200
Ted Kremenek93668002009-07-17 22:18:43 +00002201CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002202 // CaseStmts are essentially labels, so they are the first statement in a
2203 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002204 CFGBlock *TopBlock = 0, *LastBlock = 0;
2205
2206 if (Stmt *Sub = CS->getSubStmt()) {
2207 // For deeply nested chains of CaseStmts, instead of doing a recursion
2208 // (which can blow out the stack), manually unroll and create blocks
2209 // along the way.
2210 while (isa<CaseStmt>(Sub)) {
2211 CFGBlock *CurrentBlock = createBlock(false);
2212 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002213
Ted Kremenek60fa6572010-08-04 23:54:30 +00002214 if (TopBlock)
2215 AddSuccessor(LastBlock, CurrentBlock);
2216 else
2217 TopBlock = CurrentBlock;
2218
2219 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2220 LastBlock = CurrentBlock;
2221
2222 CS = cast<CaseStmt>(Sub);
2223 Sub = CS->getSubStmt();
2224 }
2225
2226 addStmt(Sub);
2227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228
Ted Kremenek55e91e82007-08-30 18:48:11 +00002229 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002230 if (!CaseBlock)
2231 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002232
2233 // Cases statements partition blocks, so this is the top of the basic block we
2234 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002235 CaseBlock->setLabel(CS);
2236
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002237 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002238 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002239
2240 // Add this block to the list of successors for the block with the switch
2241 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002242 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002243 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002244
Ted Kremenek9aae5132007-08-23 21:42:29 +00002245 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2246 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002247
Ted Kremenek60fa6572010-08-04 23:54:30 +00002248 if (TopBlock) {
2249 AddSuccessor(LastBlock, CaseBlock);
2250 Succ = TopBlock;
2251 }
2252 else {
2253 // This block is now the implicit successor of other blocks.
2254 Succ = CaseBlock;
2255 }
Mike Stump31feda52009-07-17 01:31:16 +00002256
Ted Kremenek60fa6572010-08-04 23:54:30 +00002257 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002258}
Mike Stump31feda52009-07-17 01:31:16 +00002259
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002260CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002261 if (Terminator->getSubStmt())
2262 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002263
Ted Kremenek654c78f2008-02-13 22:05:39 +00002264 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002265
2266 if (!DefaultCaseBlock)
2267 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002268
2269 // Default statements partition blocks, so this is the top of the basic block
2270 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002271 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002272
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002273 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002274 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002275
Mike Stump31feda52009-07-17 01:31:16 +00002276 // Unlike case statements, we don't add the default block to the successors
2277 // for the switch statement immediately. This is done when we finish
2278 // processing the switch statement. This allows for the default case
2279 // (including a fall-through to the code after the switch statement) to always
2280 // be the last successor of a switch-terminated block.
2281
Ted Kremenek654c78f2008-02-13 22:05:39 +00002282 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2283 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002284
Ted Kremenek654c78f2008-02-13 22:05:39 +00002285 // This block is now the implicit successor of other blocks.
2286 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002287
2288 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002289}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002290
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002291CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2292 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2293 // current block.
2294 CFGBlock* TrySuccessor = NULL;
2295
2296 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002297 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002298 return 0;
2299 TrySuccessor = Block;
2300 } else TrySuccessor = Succ;
2301
Mike Stump0bdba6c2010-01-20 01:15:34 +00002302 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002303
2304 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002305 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002306 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002307 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002308
Mike Stump0bdba6c2010-01-20 01:15:34 +00002309 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002310 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2311 // The code after the try is the implicit successor.
2312 Succ = TrySuccessor;
2313 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002314 if (CS->getExceptionDecl() == 0) {
2315 HasCatchAll = true;
2316 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002317 Block = NULL;
2318 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2319 if (CatchBlock == 0)
2320 return 0;
2321 // Add this block to the list of successors for the block with the try
2322 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00002323 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002324 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002325 if (!HasCatchAll) {
2326 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00002327 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002328 else
Mike Stump845384a2010-01-20 01:30:58 +00002329 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002330 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002331
2332 // The code after the try is the implicit successor.
2333 Succ = TrySuccessor;
2334
Mike Stump845384a2010-01-20 01:30:58 +00002335 // Save the current "try" context.
2336 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2337 TryTerminatedBlock = NewTryTerminatedBlock;
2338
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002339 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002340 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002341 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002342 return Block;
2343}
2344
2345CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2346 // CXXCatchStmt are treated like labels, so they are the first statement in a
2347 // block.
2348
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002349 // Save local scope position because in case of exception variable ScopePos
2350 // won't be restored when traversing AST.
2351 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2352
2353 // Create local scope for possible exception variable.
2354 // Store scope position. Add implicit destructor.
2355 if (VarDecl* VD = CS->getExceptionDecl()) {
2356 LocalScope::const_iterator BeginScopePos = ScopePos;
2357 addLocalScopeForVarDecl(VD);
2358 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2359 }
2360
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002361 if (CS->getHandlerBlock())
2362 addStmt(CS->getHandlerBlock());
2363
2364 CFGBlock* CatchBlock = Block;
2365 if (!CatchBlock)
2366 CatchBlock = createBlock();
2367
2368 CatchBlock->setLabel(CS);
2369
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002370 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002371 return 0;
2372
2373 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2374 Block = NULL;
2375
2376 return CatchBlock;
2377}
2378
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002379CFGBlock *CFGBuilder::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
2380 AddStmtChoice asc) {
2381 if (BuildOpts.AddImplicitDtors) {
2382 // If adding implicit destructors visit the full expression for adding
2383 // destructors of temporaries.
2384 VisitForTemporaryDtors(E->getSubExpr());
2385
2386 // Full expression has to be added as CFGStmt so it will be sequenced
2387 // before destructors of it's temporaries.
2388 asc = asc.asLValue()
2389 ? AddStmtChoice::AlwaysAddAsLValue
2390 : AddStmtChoice::AlwaysAdd;
2391 }
2392 return Visit(E->getSubExpr(), asc);
2393}
2394
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002395CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2396 AddStmtChoice asc) {
2397 if (asc.alwaysAdd()) {
2398 autoCreateBlock();
2399 AppendStmt(Block, E, asc);
2400
2401 // We do not want to propagate the AlwaysAdd property.
2402 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2403 : AddStmtChoice::NotAlwaysAdd);
2404 }
2405 return Visit(E->getSubExpr(), asc);
2406}
2407
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002408CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2409 AddStmtChoice asc) {
2410 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2411 : AddStmtChoice::AlwaysAdd;
2412 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002413 if (!C->isElidable())
2414 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002415 return VisitChildren(C);
2416}
2417
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002418CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2419 AddStmtChoice asc) {
2420 if (asc.alwaysAdd()) {
2421 autoCreateBlock();
2422 AppendStmt(Block, E, asc);
2423 // We do not want to propagate the AlwaysAdd property.
2424 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2425 : AddStmtChoice::NotAlwaysAdd);
2426 }
2427 return Visit(E->getSubExpr(), asc);
2428}
2429
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002430CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2431 AddStmtChoice asc) {
2432 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2433 : AddStmtChoice::AlwaysAdd;
2434 autoCreateBlock();
2435 AppendStmt(Block, C, AddStmtChoice(K));
2436 return VisitChildren(C);
2437}
2438
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002439CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002440 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002441 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002442 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00002443 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00002444 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002445 return VisitChildren(C);
2446}
2447
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002448CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2449 AddStmtChoice asc) {
2450 if (asc.alwaysAdd()) {
2451 autoCreateBlock();
2452 AppendStmt(Block, E, asc);
2453 // We do not want to propagate the AlwaysAdd property.
2454 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2455 : AddStmtChoice::NotAlwaysAdd);
2456 }
2457 return Visit(E->getSubExpr(), asc);
2458}
2459
Ted Kremenekeda180e22007-08-28 19:26:49 +00002460CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002461 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002462 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002463
Ted Kremenekeda180e22007-08-28 19:26:49 +00002464 if (!IBlock) {
2465 IBlock = createBlock(false);
2466 cfg->setIndirectGotoBlock(IBlock);
2467 }
Mike Stump31feda52009-07-17 01:31:16 +00002468
Ted Kremenekeda180e22007-08-28 19:26:49 +00002469 // IndirectGoto is a control-flow statement. Thus we stop processing the
2470 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002471 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002472 return 0;
2473
Ted Kremenekeda180e22007-08-28 19:26:49 +00002474 Block = createBlock(false);
2475 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002476 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002477 return addStmt(I->getTarget());
2478}
2479
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002480CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2481tryAgain:
2482 if (!E) {
2483 badCFG = true;
2484 return NULL;
2485 }
2486 switch (E->getStmtClass()) {
2487 default:
2488 return VisitChildrenForTemporaryDtors(E);
2489
2490 case Stmt::BinaryOperatorClass:
2491 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2492
2493 case Stmt::CXXBindTemporaryExprClass:
2494 return VisitCXXBindTemporaryExprForTemporaryDtors(
2495 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2496
2497 case Stmt::ConditionalOperatorClass:
2498 return VisitConditionalOperatorForTemporaryDtors(
2499 cast<ConditionalOperator>(E), BindToTemporary);
2500
2501 case Stmt::ImplicitCastExprClass:
2502 // For implicit cast we want BindToTemporary to be passed further.
2503 E = cast<CastExpr>(E)->getSubExpr();
2504 goto tryAgain;
2505
2506 case Stmt::ParenExprClass:
2507 E = cast<ParenExpr>(E)->getSubExpr();
2508 goto tryAgain;
2509 }
2510}
2511
2512CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2513 // When visiting children for destructors we want to visit them in reverse
2514 // order. Because there's no reverse iterator for children must to reverse
2515 // them in helper vector.
2516 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2517 ChildrenVect ChildrenRev;
2518 for (Stmt::child_iterator I = E->child_begin(), L = E->child_end();
2519 I != L; ++I) {
2520 if (*I) ChildrenRev.push_back(*I);
2521 }
2522
2523 CFGBlock *B = Block;
2524 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2525 L = ChildrenRev.rend(); I != L; ++I) {
2526 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2527 B = R;
2528 }
2529 return B;
2530}
2531
2532CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2533 if (E->isLogicalOp()) {
2534 // Destructors for temporaries in LHS expression should be called after
2535 // those for RHS expression. Even if this will unnecessarily create a block,
2536 // this block will be used at least by the full expression.
2537 autoCreateBlock();
2538 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2539 if (badCFG)
2540 return NULL;
2541
2542 Succ = ConfluenceBlock;
2543 Block = NULL;
2544 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2545
2546 if (RHSBlock) {
2547 if (badCFG)
2548 return NULL;
2549
2550 // If RHS expression did produce destructors we need to connect created
2551 // blocks to CFG in same manner as for binary operator itself.
2552 CFGBlock *LHSBlock = createBlock(false);
2553 LHSBlock->setTerminator(CFGTerminator(E, true));
2554
2555 // For binary operator LHS block is before RHS in list of predecessors
2556 // of ConfluenceBlock.
2557 std::reverse(ConfluenceBlock->pred_begin(),
2558 ConfluenceBlock->pred_end());
2559
2560 // See if this is a known constant.
2561 TryResult KnownVal = TryEvaluateBool(E->getLHS());
2562 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2563 KnownVal.negate();
2564
2565 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2566 // itself.
2567 if (E->getOpcode() == BO_LOr) {
2568 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2569 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2570 } else {
2571 assert (E->getOpcode() == BO_LAnd);
2572 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2573 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2574 }
2575
2576 Block = LHSBlock;
2577 return LHSBlock;
2578 }
2579
2580 Block = ConfluenceBlock;
2581 return ConfluenceBlock;
2582 }
2583
2584 else if (E->isAssignmentOp()) {
2585 // For assignment operator (=) LHS expression is visited
2586 // before RHS expression. For destructors visit them in reverse order.
2587 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2588 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2589 return LHSBlock ? LHSBlock : RHSBlock;
2590 }
2591
2592 // For any other binary operator RHS expression is visited before
2593 // LHS expression (order of children). For destructors visit them in reverse
2594 // order.
2595 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2596 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2597 return RHSBlock ? RHSBlock : LHSBlock;
2598}
2599
2600CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2601 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2602 // First add destructors for temporaries in subexpression.
2603 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002604 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002605 // If lifetime of temporary is not prolonged (by assigning to constant
2606 // reference) add destructor for it.
2607 autoCreateBlock();
2608 appendTemporaryDtor(Block, E);
2609 B = Block;
2610 }
2611 return B;
2612}
2613
2614CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2615 ConditionalOperator *E, bool BindToTemporary) {
2616 // First add destructors for condition expression. Even if this will
2617 // unnecessarily create a block, this block will be used at least by the full
2618 // expression.
2619 autoCreateBlock();
2620 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2621 if (badCFG)
2622 return NULL;
2623
2624 // Try to add block with destructors for LHS expression.
2625 CFGBlock *LHSBlock = NULL;
2626 if (E->getLHS()) {
2627 Succ = ConfluenceBlock;
2628 Block = NULL;
2629 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2630 if (badCFG)
2631 return NULL;
2632 }
2633
2634 // Try to add block with destructors for RHS expression;
2635 Succ = ConfluenceBlock;
2636 Block = NULL;
2637 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2638 if (badCFG)
2639 return NULL;
2640
2641 if (!RHSBlock && !LHSBlock) {
2642 // If neither LHS nor RHS expression had temporaries to destroy don't create
2643 // more blocks.
2644 Block = ConfluenceBlock;
2645 return Block;
2646 }
2647
2648 Block = createBlock(false);
2649 Block->setTerminator(CFGTerminator(E, true));
2650
2651 // See if this is a known constant.
2652 const TryResult &KnownVal = TryEvaluateBool(E->getCond());
2653
2654 if (LHSBlock) {
2655 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
2656 } else if (KnownVal.isFalse()) {
2657 AddSuccessor(Block, NULL);
2658 } else {
2659 AddSuccessor(Block, ConfluenceBlock);
2660 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2661 }
2662
2663 if (!RHSBlock)
2664 RHSBlock = ConfluenceBlock;
2665 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
2666
2667 return Block;
2668}
2669
Ted Kremenek04cca642007-08-23 21:26:19 +00002670} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002671
Mike Stump31feda52009-07-17 01:31:16 +00002672/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2673/// no successors or predecessors. If this is the first block created in the
2674/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002675CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002676 bool first_block = begin() == end();
2677
2678 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002679 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2680 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2681 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002682
2683 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002684 if (first_block)
2685 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002686
2687 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002688 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002689}
2690
Ted Kremenek889073f2007-08-23 16:51:22 +00002691/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2692/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002693CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002694 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002695 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002696 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002697}
2698
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002699//===----------------------------------------------------------------------===//
2700// CFG: Queries for BlkExprs.
2701//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002702
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002703namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002704 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002705}
2706
Ted Kremenekbff98442009-12-23 23:37:10 +00002707static void FindSubExprAssignments(Stmt *S,
2708 llvm::SmallPtrSet<Expr*,50>& Set) {
2709 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002710 return;
Mike Stump31feda52009-07-17 01:31:16 +00002711
Ted Kremenekbff98442009-12-23 23:37:10 +00002712 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002713 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002714 if (!child)
2715 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002716
Ted Kremenekbff98442009-12-23 23:37:10 +00002717 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002718 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002719
Ted Kremenekbff98442009-12-23 23:37:10 +00002720 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002721 }
2722}
2723
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002724static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2725 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002726
2727 // Look for assignments that are used as subexpressions. These are the only
2728 // assignments that we want to *possibly* register as a block-level
2729 // expression. Basically, if an assignment occurs both in a subexpression and
2730 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002731 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002732
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002733 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002734 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002735 if (CFGStmt S = BI->getAs<CFGStmt>())
2736 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002737
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002738 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002739
2740 // Iterate over the statements again on identify the Expr* and Stmt* at the
2741 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002742
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002743 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2744 CFGStmt CS = BI->getAs<CFGStmt>();
2745 if (!CS.isValid())
2746 continue;
2747 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002748
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002749 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002750 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002751 // expression are really "statements" whose value is never used by
2752 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002753 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002754 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002755 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2756 // Special handling for statement expressions. The last statement in
2757 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002758 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002759 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002760 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002761 (*M)[C->body_back()] = x;
2762 }
2763 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002764
Ted Kremenek95a123c2008-01-26 00:03:27 +00002765 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002766 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002767 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002768 }
Mike Stump31feda52009-07-17 01:31:16 +00002769
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002770 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002771
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002772 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002773
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002774 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002775 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002776 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002777 }
2778 }
Mike Stump31feda52009-07-17 01:31:16 +00002779
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002780 return M;
2781}
2782
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002783CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2784 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002785 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002786
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002787 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002788 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002789 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002790}
2791
2792unsigned CFG::getNumBlkExprs() {
2793 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2794 return M->size();
2795 else {
2796 // We assume callers interested in the number of BlkExprs will want
2797 // the map constructed if it doesn't already exist.
2798 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2799 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2800 }
2801}
2802
Ted Kremenek6065ef62008-04-28 18:00:46 +00002803//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002804// Filtered walking of the CFG.
2805//===----------------------------------------------------------------------===//
2806
2807bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002808 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002809
2810 if (F.IgnoreDefaultsWithCoveredEnums) {
2811 // If the 'To' has no label or is labeled but the label isn't a
2812 // CaseStmt then filter this edge.
2813 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002814 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002815 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002816 const Stmt *L = To->getLabel();
2817 if (!L || !isa<CaseStmt>(L))
2818 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002819 }
2820 }
2821 }
2822
2823 return false;
2824}
2825
2826//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002827// Cleanup: CFG dstor.
2828//===----------------------------------------------------------------------===//
2829
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002830CFG::~CFG() {
2831 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2832}
Mike Stump31feda52009-07-17 01:31:16 +00002833
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002834//===----------------------------------------------------------------------===//
2835// CFG pretty printing
2836//===----------------------------------------------------------------------===//
2837
Ted Kremenek7e776b12007-08-22 18:22:34 +00002838namespace {
2839
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002840class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002841 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002842 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002843 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002844 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002845 signed CurrentBlock;
2846 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002847 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002848public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002849
Chris Lattnerc61089a2009-06-30 01:26:17 +00002850 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2851 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002852 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2853 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002854 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002855 BI != BEnd; ++BI, ++j ) {
2856 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2857 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2858 StmtMap[SE] = P;
2859
2860 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2861 DeclMap[DS->getSingleDecl()] = P;
2862
2863 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2864 if (VarDecl* VD = IS->getConditionVariable())
2865 DeclMap[VD] = P;
2866
2867 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2868 if (VarDecl* VD = FS->getConditionVariable())
2869 DeclMap[VD] = P;
2870
2871 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2872 if (VarDecl* VD = WS->getConditionVariable())
2873 DeclMap[VD] = P;
2874
2875 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2876 if (VarDecl* VD = SS->getConditionVariable())
2877 DeclMap[VD] = P;
2878
2879 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2880 if (VarDecl* VD = CS->getExceptionDecl())
2881 DeclMap[VD] = P;
2882 }
2883 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002884 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002885 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002886 }
Mike Stump31feda52009-07-17 01:31:16 +00002887
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002888 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002889
Chris Lattnerc61089a2009-06-30 01:26:17 +00002890 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002891 void setBlockID(signed i) { CurrentBlock = i; }
2892 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002893
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002894 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2895 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002896
2897 if (I == StmtMap.end())
2898 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002899
2900 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002901 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002902 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002903 }
Mike Stump31feda52009-07-17 01:31:16 +00002904
Ted Kremenek60983dc2010-01-19 20:52:05 +00002905 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002906 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002907 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002908
2909 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2910 DeclMapTy::iterator I = DeclMap.find(D);
2911
2912 if (I == DeclMap.end())
2913 return false;
2914
2915 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2916 && I->second.second == CurrentStmt) {
2917 return false;
2918 }
2919
2920 OS << "[B" << I->second.first << "." << I->second.second << "]";
2921 return true;
2922 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002923};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002924} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002925
Chris Lattnerc61089a2009-06-30 01:26:17 +00002926
2927namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002928class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002929 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002930
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002931 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002932 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002933 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002934public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002935 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002936 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002937 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002938
Ted Kremenek9aae5132007-08-23 21:42:29 +00002939 void VisitIfStmt(IfStmt* I) {
2940 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002941 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002942 }
Mike Stump31feda52009-07-17 01:31:16 +00002943
Ted Kremenek9aae5132007-08-23 21:42:29 +00002944 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002945 void VisitStmt(Stmt* Terminator) {
2946 Terminator->printPretty(OS, Helper, Policy);
2947 }
2948
Ted Kremenek9aae5132007-08-23 21:42:29 +00002949 void VisitForStmt(ForStmt* F) {
2950 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002951 if (F->getInit())
2952 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002953 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002954 if (Stmt* C = F->getCond())
2955 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002956 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002957 if (F->getInc())
2958 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002959 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002960 }
Mike Stump31feda52009-07-17 01:31:16 +00002961
Ted Kremenek9aae5132007-08-23 21:42:29 +00002962 void VisitWhileStmt(WhileStmt* W) {
2963 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002964 if (Stmt* C = W->getCond())
2965 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002966 }
Mike Stump31feda52009-07-17 01:31:16 +00002967
Ted Kremenek9aae5132007-08-23 21:42:29 +00002968 void VisitDoStmt(DoStmt* D) {
2969 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002970 if (Stmt* C = D->getCond())
2971 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002972 }
Mike Stump31feda52009-07-17 01:31:16 +00002973
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002974 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002975 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002976 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002977 }
Mike Stump31feda52009-07-17 01:31:16 +00002978
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002979 void VisitCXXTryStmt(CXXTryStmt* CS) {
2980 OS << "try ...";
2981 }
2982
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002983 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002984 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002985 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002986 }
Mike Stump31feda52009-07-17 01:31:16 +00002987
Ted Kremenek391f94a2007-08-31 22:29:13 +00002988 void VisitChooseExpr(ChooseExpr* C) {
2989 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002990 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002991 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002992 }
Mike Stump31feda52009-07-17 01:31:16 +00002993
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002994 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2995 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002996 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002997 }
Mike Stump31feda52009-07-17 01:31:16 +00002998
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002999 void VisitBinaryOperator(BinaryOperator* B) {
3000 if (!B->isLogicalOp()) {
3001 VisitExpr(B);
3002 return;
3003 }
Mike Stump31feda52009-07-17 01:31:16 +00003004
Douglas Gregor7de59662009-05-29 20:38:28 +00003005 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003006
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003007 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003008 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003009 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003010 return;
John McCalle3027922010-08-25 11:45:40 +00003011 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003012 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003013 return;
3014 default:
3015 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003016 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003017 }
Mike Stump31feda52009-07-17 01:31:16 +00003018
Ted Kremenek12687ff2007-08-27 21:54:41 +00003019 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003020 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003021 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003022};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003023} // end anonymous namespace
3024
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003025static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003026 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003027 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3028 Stmt *S = CS;
3029
3030 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003031
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003032 // special printing for statement-expressions.
3033 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3034 CompoundStmt* Sub = SE->getSubStmt();
3035
3036 if (Sub->child_begin() != Sub->child_end()) {
3037 OS << "({ ... ; ";
3038 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3039 OS << " })\n";
3040 return;
3041 }
3042 }
3043 // special printing for comma expressions.
3044 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3045 if (B->getOpcode() == BO_Comma) {
3046 OS << "... , ";
3047 Helper->handledStmt(B->getRHS(),OS);
3048 OS << '\n';
3049 return;
3050 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003051 }
3052 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003053 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003054
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003055 if (isa<CXXOperatorCallExpr>(S)) {
3056 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00003057 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003058 else if (isa<CXXBindTemporaryExpr>(S)) {
3059 OS << " (BindTemporary)";
3060 }
Mike Stump31feda52009-07-17 01:31:16 +00003061
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003062 // Expressions need a newline.
3063 if (isa<Expr>(S))
3064 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003065
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003066 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3067 CXXBaseOrMemberInitializer* I = IE;
3068 if (I->isBaseInitializer())
3069 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
3070 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003071
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003072 OS << "(";
3073 if (Expr* IE = I->getInit())
3074 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3075 OS << ")";
3076
3077 if (I->isBaseInitializer())
3078 OS << " (Base initializer)\n";
3079 else OS << " (Member initializer)\n";
3080
3081 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3082 VarDecl* VD = DE.getVarDecl();
3083 Helper->handleDecl(VD, OS);
3084
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003085 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003086 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3087 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003088 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3089 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003090
3091 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3092 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003093
3094 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3095 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3096 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003097 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003098
3099 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3100 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003101
3102 const Type *T = FD->getType().getTypePtr();
3103 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3104 T = ET;
3105
Marcin Swiderski20b88732010-10-05 05:37:00 +00003106 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003107 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003108 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003109
3110 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3111 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3112 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3113 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003114 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003115}
Mike Stump31feda52009-07-17 01:31:16 +00003116
Chris Lattnerc61089a2009-06-30 01:26:17 +00003117static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3118 const CFGBlock& B,
3119 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003120
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003121 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003122
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003123 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003124 OS << "\n [ B" << B.getBlockID();
3125
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003126 if (&B == &cfg->getEntry())
3127 OS << " (ENTRY) ]\n";
3128 else if (&B == &cfg->getExit())
3129 OS << " (EXIT) ]\n";
3130 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003131 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003132 else
3133 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003134
Ted Kremenek71eca012007-08-29 23:20:49 +00003135 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003136 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003137
3138 if (print_edges)
3139 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003140
Mike Stump92244b02010-01-19 22:00:14 +00003141 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003142 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003143 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003144 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003145 C->getLHS()->printPretty(OS, Helper,
3146 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003147 if (C->getRHS()) {
3148 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003149 C->getRHS()->printPretty(OS, Helper,
3150 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003151 }
Mike Stump92244b02010-01-19 22:00:14 +00003152 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003153 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003154 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003155 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003156 if (CS->getExceptionDecl())
3157 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3158 0);
3159 else
3160 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003161 OS << ")";
3162
3163 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003164 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003165
Ted Kremenek71eca012007-08-29 23:20:49 +00003166 OS << ":\n";
3167 }
Mike Stump31feda52009-07-17 01:31:16 +00003168
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003169 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003170 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003171
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003172 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3173 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003174
Ted Kremenek71eca012007-08-29 23:20:49 +00003175 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003176 if (print_edges)
3177 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003178
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003179 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003180
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003181 if (Helper)
3182 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003183
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003184 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003185 }
Mike Stump31feda52009-07-17 01:31:16 +00003186
Ted Kremenek71eca012007-08-29 23:20:49 +00003187 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003188 if (B.getTerminator()) {
3189 if (print_edges)
3190 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003191
Ted Kremenek71eca012007-08-29 23:20:49 +00003192 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003193
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003194 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003195
Chris Lattnerc61089a2009-06-30 01:26:17 +00003196 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3197 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003198 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003199 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003200 }
Mike Stump31feda52009-07-17 01:31:16 +00003201
Ted Kremenek71eca012007-08-29 23:20:49 +00003202 if (print_edges) {
3203 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003204 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003205 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003206
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003207 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3208 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003209
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003210 if (i == 8 || (i-8) == 0)
3211 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003212
Ted Kremenek71eca012007-08-29 23:20:49 +00003213 OS << " B" << (*I)->getBlockID();
3214 }
Mike Stump31feda52009-07-17 01:31:16 +00003215
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003216 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003217
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003218 // Print the successors of this block.
3219 OS << " Successors (" << B.succ_size() << "):";
3220 i = 0;
3221
3222 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3223 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003224
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003225 if (i == 8 || (i-8) % 10 == 0)
3226 OS << "\n ";
3227
Mike Stump0d76d072009-07-20 23:24:15 +00003228 if (*I)
3229 OS << " B" << (*I)->getBlockID();
3230 else
3231 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003232 }
Mike Stump31feda52009-07-17 01:31:16 +00003233
Ted Kremenek71eca012007-08-29 23:20:49 +00003234 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003235 }
Mike Stump31feda52009-07-17 01:31:16 +00003236}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003237
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003238
3239/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003240void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003241
3242/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003243void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3244 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003245
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003246 // Print the entry block.
3247 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003248
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003249 // Iterate through the CFGBlocks and print them one by one.
3250 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3251 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003252 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003253 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003254
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003255 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003256 }
Mike Stump31feda52009-07-17 01:31:16 +00003257
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003258 // Print the exit block.
3259 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003260 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003261}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003262
3263/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003264void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3265 print(llvm::errs(), cfg, LO);
3266}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003267
3268/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3269/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003270void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3271 const LangOptions &LO) const {
3272 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003273 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003274}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003275
Ted Kremenek15647632008-01-30 23:02:42 +00003276/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003277void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003278 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003279 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003280 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003281}
3282
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003283Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003284 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003285 if (!Terminator)
3286 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003287
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003288 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003289
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003290 switch (Terminator->getStmtClass()) {
3291 default:
3292 break;
Mike Stump31feda52009-07-17 01:31:16 +00003293
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003294 case Stmt::ForStmtClass:
3295 E = cast<ForStmt>(Terminator)->getCond();
3296 break;
Mike Stump31feda52009-07-17 01:31:16 +00003297
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003298 case Stmt::WhileStmtClass:
3299 E = cast<WhileStmt>(Terminator)->getCond();
3300 break;
Mike Stump31feda52009-07-17 01:31:16 +00003301
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003302 case Stmt::DoStmtClass:
3303 E = cast<DoStmt>(Terminator)->getCond();
3304 break;
Mike Stump31feda52009-07-17 01:31:16 +00003305
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003306 case Stmt::IfStmtClass:
3307 E = cast<IfStmt>(Terminator)->getCond();
3308 break;
Mike Stump31feda52009-07-17 01:31:16 +00003309
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003310 case Stmt::ChooseExprClass:
3311 E = cast<ChooseExpr>(Terminator)->getCond();
3312 break;
Mike Stump31feda52009-07-17 01:31:16 +00003313
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003314 case Stmt::IndirectGotoStmtClass:
3315 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3316 break;
Mike Stump31feda52009-07-17 01:31:16 +00003317
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003318 case Stmt::SwitchStmtClass:
3319 E = cast<SwitchStmt>(Terminator)->getCond();
3320 break;
Mike Stump31feda52009-07-17 01:31:16 +00003321
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003322 case Stmt::ConditionalOperatorClass:
3323 E = cast<ConditionalOperator>(Terminator)->getCond();
3324 break;
Mike Stump31feda52009-07-17 01:31:16 +00003325
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003326 case Stmt::BinaryOperatorClass: // '&&' and '||'
3327 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003328 break;
Mike Stump31feda52009-07-17 01:31:16 +00003329
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003330 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003331 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003332 }
Mike Stump31feda52009-07-17 01:31:16 +00003333
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003334 return E ? E->IgnoreParens() : NULL;
3335}
3336
Ted Kremenek92137a32008-05-16 16:06:00 +00003337bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003338 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003339 if (!Terminator)
3340 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003341
Ted Kremenek92137a32008-05-16 16:06:00 +00003342 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003343
Ted Kremenek92137a32008-05-16 16:06:00 +00003344 switch (Terminator->getStmtClass()) {
3345 default:
3346 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003347
3348 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003349 case Stmt::WhileStmtClass:
3350 case Stmt::DoStmtClass:
3351 case Stmt::IfStmtClass:
3352 case Stmt::ChooseExprClass:
3353 case Stmt::ConditionalOperatorClass:
3354 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003355 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003356 }
Mike Stump31feda52009-07-17 01:31:16 +00003357
Ted Kremenek92137a32008-05-16 16:06:00 +00003358 return E ? E->IgnoreParens() : NULL;
3359}
3360
Ted Kremenek15647632008-01-30 23:02:42 +00003361
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003362//===----------------------------------------------------------------------===//
3363// CFG Graphviz Visualization
3364//===----------------------------------------------------------------------===//
3365
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003366
3367#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003368static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003369#endif
3370
Chris Lattnerc61089a2009-06-30 01:26:17 +00003371void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003372#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003373 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003374 GraphHelper = &H;
3375 llvm::ViewGraph(this,"CFG");
3376 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003377#endif
3378}
3379
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003380namespace llvm {
3381template<>
3382struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003383
3384 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3385
3386 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003387
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003388#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003389 std::string OutSStr;
3390 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003391 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003392 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003393
3394 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3395
3396 // Process string output to make it nicer...
3397 for (unsigned i = 0; i != OutStr.length(); ++i)
3398 if (OutStr[i] == '\n') { // Left justify
3399 OutStr[i] = '\\';
3400 OutStr.insert(OutStr.begin()+i+1, 'l');
3401 }
Mike Stump31feda52009-07-17 01:31:16 +00003402
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003403 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003404#else
3405 return "";
3406#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003407 }
3408};
3409} // end namespace llvm