blob: 4b89a54023a41f3f7c7c1a2f9005c616f49ec854 [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;
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000963 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +0000964 // Create an empty block for cases where the RHS doesn't require
965 // any explicit statements in the CFG.
966 RHSBlock = createBlock();
967 }
Mike Stump11289f42009-09-09 15:08:12 +0000968
Mike Stump773582d2009-07-23 23:25:26 +0000969 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000970 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000971 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000972 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000973
Ted Kremenek93668002009-07-17 22:18:43 +0000974 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000975 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000976 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
977 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000978 } else {
John McCalle3027922010-08-25 11:45:40 +0000979 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000980 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
981 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000982 }
Mike Stump11289f42009-09-09 15:08:12 +0000983
Ted Kremenek93668002009-07-17 22:18:43 +0000984 // Generate the blocks for evaluating the LHS.
985 Block = LHSBlock;
986 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000987 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000988
989 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 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000995
996 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000997 if (asc.alwaysAdd()) {
998 autoCreateBlock();
999 AppendStmt(Block, B, asc);
1000 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001001
Marcin Swiderski77232492010-10-24 08:21:40 +00001002 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
1003 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Marcin Swiderski77232492010-10-24 08:21:40 +00001006 if (asc.alwaysAdd()) {
1007 autoCreateBlock();
1008 AppendStmt(Block, B, asc);
1009 }
1010
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001011 CFGBlock *RBlock = Visit(B->getRHS());
1012 CFGBlock *LBlock = Visit(B->getLHS());
1013 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1014 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1015 // return RBlock. Otherwise we'll incorrectly return NULL.
1016 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001017}
1018
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001019CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1020 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001021 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001022 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001023 }
1024 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001025}
1026
Ted Kremenek93668002009-07-17 22:18:43 +00001027CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1028 // "break" is a control-flow statement. Thus we stop processing the current
1029 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001030 if (badCFG)
1031 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001032
Ted Kremenek93668002009-07-17 22:18:43 +00001033 // Now create a new block that ends with the break statement.
1034 Block = createBlock(false);
1035 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001036
Ted Kremenek93668002009-07-17 22:18:43 +00001037 // If there is no target for the break, then we are looking at an incomplete
1038 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001039 if (BreakJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001040 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001041 AddSuccessor(Block, BreakJumpTarget.Block);
1042 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001043 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001044
1045
Ted Kremenek9aae5132007-08-23 21:42:29 +00001046 return Block;
1047}
Mike Stump11289f42009-09-09 15:08:12 +00001048
Mike Stump04c68512010-01-21 15:20:48 +00001049static bool CanThrow(Expr *E) {
1050 QualType Ty = E->getType();
1051 if (Ty->isFunctionPointerType())
1052 Ty = Ty->getAs<PointerType>()->getPointeeType();
1053 else if (Ty->isBlockPointerType())
1054 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001055
Mike Stump04c68512010-01-21 15:20:48 +00001056 const FunctionType *FT = Ty->getAs<FunctionType>();
1057 if (FT) {
1058 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1059 if (Proto->hasEmptyExceptionSpec())
1060 return false;
1061 }
1062 return true;
1063}
1064
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001065CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001066 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001067 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001068 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001069 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001070 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001071
Mike Stump04c68512010-01-21 15:20:48 +00001072 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001073
1074 // Languages without exceptions are assumed to not throw.
1075 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001076 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001077 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001078 }
1079
1080 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001081 if (FD->hasAttr<NoReturnAttr>())
1082 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001083 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001084 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001085 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001086
Mike Stump04c68512010-01-21 15:20:48 +00001087 if (!CanThrow(C->getCallee()))
1088 AddEHEdge = false;
1089
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001090 if (!NoReturn && !AddEHEdge) {
1091 if (asc.asLValue())
1092 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1093 else
1094 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
Mike Stump92244b02010-01-19 22:00:14 +00001097 if (Block) {
1098 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001099 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001100 return 0;
1101 }
Mike Stump11289f42009-09-09 15:08:12 +00001102
Mike Stump92244b02010-01-19 22:00:14 +00001103 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001104 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001105
Mike Stump92244b02010-01-19 22:00:14 +00001106 if (NoReturn) {
1107 // Wire this to the exit block directly.
1108 AddSuccessor(Block, &cfg->getExit());
1109 }
Mike Stump04c68512010-01-21 15:20:48 +00001110 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001111 // Add exceptional edges.
1112 if (TryTerminatedBlock)
1113 AddSuccessor(Block, TryTerminatedBlock);
1114 else
1115 AddSuccessor(Block, &cfg->getExit());
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Mike Stump8c5d7992009-07-25 21:26:53 +00001118 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001119}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001120
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001121CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1122 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001123 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001124 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001125 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001126 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001127
Ted Kremenek5868ec62010-04-11 17:02:10 +00001128 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1129 : AddStmtChoice::AlwaysAdd;
1130
Ted Kremenek21822592009-07-17 18:20:32 +00001131 Succ = ConfluenceBlock;
1132 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001133 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001134 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001135 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001136
Ted Kremenek21822592009-07-17 18:20:32 +00001137 Succ = ConfluenceBlock;
1138 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001139 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001140 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001141 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001142
Ted Kremenek21822592009-07-17 18:20:32 +00001143 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001144 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001145 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001146 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1147 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001148 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001149 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001150}
Mike Stump11289f42009-09-09 15:08:12 +00001151
1152
1153CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001154 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001155 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001156
1157 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1158 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001159 // If we hit a segment of code just containing ';' (NullStmts), we can
1160 // get a null block back. In such cases, just use the LastBlock
1161 if (CFGBlock *newBlock = addStmt(*I))
1162 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001163
Ted Kremenekce499c22009-08-27 23:16:26 +00001164 if (badCFG)
1165 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001166 }
Mike Stump92244b02010-01-19 22:00:14 +00001167
Ted Kremenek93668002009-07-17 22:18:43 +00001168 return LastBlock;
1169}
Mike Stump11289f42009-09-09 15:08:12 +00001170
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001171CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1172 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001173 // Create the confluence block that will "merge" the results of the ternary
1174 // expression.
1175 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001176 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001177 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001178 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001179
Ted Kremenek5868ec62010-04-11 17:02:10 +00001180 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1181 : AddStmtChoice::AlwaysAdd;
1182
Ted Kremenek51d40b02009-07-17 18:15:54 +00001183 // Create a block for the LHS expression if there is an LHS expression. A
1184 // GCC extension allows LHS to be NULL, causing the condition to be the
1185 // value that is returned instead.
1186 // e.g: x ?: y is shorthand for: x ? x : y;
1187 Succ = ConfluenceBlock;
1188 Block = NULL;
1189 CFGBlock* LHSBlock = NULL;
1190 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001191 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001192 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001193 return 0;
1194 Block = NULL;
1195 }
Mike Stump11289f42009-09-09 15:08:12 +00001196
Ted Kremenek51d40b02009-07-17 18:15:54 +00001197 // Create the block for the RHS expression.
1198 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +00001199 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001200 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001201 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenek51d40b02009-07-17 18:15:54 +00001203 // Create the block that will contain the condition.
1204 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001205
Mike Stump773582d2009-07-23 23:25:26 +00001206 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001207 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001208 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001209 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001210 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001211 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001212 // If we know the condition is false, add NULL as the successor for
1213 // the block containing the condition. In this case, the confluence
1214 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001215 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001216 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001217 } else {
1218 // If we have no LHS expression, add the ConfluenceBlock as a direct
1219 // successor for the block containing the condition. Moreover, we need to
1220 // reverse the order of the predecessors in the ConfluenceBlock because
1221 // the RHSBlock will have been added to the succcessors already, and we
1222 // want the first predecessor to the the block containing the expression
1223 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001224 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek18fb1662010-11-15 22:59:22 +00001225 // Note that there can possibly only be one predecessor if one of the
1226 // subexpressions resulted in calling a noreturn function.
Mike Stump0d76d072009-07-20 23:24:15 +00001227 std::reverse(ConfluenceBlock->pred_begin(),
1228 ConfluenceBlock->pred_end());
1229 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001230 }
Mike Stump11289f42009-09-09 15:08:12 +00001231
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001232 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001233 Block->setTerminator(C);
1234 return addStmt(C->getCond());
1235}
1236
Ted Kremenek93668002009-07-17 22:18:43 +00001237CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001238 if (DS->isSingleDecl())
1239 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001240
Ted Kremenek93668002009-07-17 22:18:43 +00001241 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001242
Ted Kremenek93668002009-07-17 22:18:43 +00001243 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1244 typedef llvm::SmallVector<Decl*,10> BufTy;
1245 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001246
Ted Kremenek93668002009-07-17 22:18:43 +00001247 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1248 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1249 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1250 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001251
Ted Kremenek93668002009-07-17 22:18:43 +00001252 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1253 // automatically freed with the CFG.
1254 DeclGroupRef DG(*I);
1255 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001256 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001257 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001258
Ted Kremenek93668002009-07-17 22:18:43 +00001259 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001260 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
1263 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001264}
Mike Stump11289f42009-09-09 15:08:12 +00001265
Ted Kremenek93668002009-07-17 22:18:43 +00001266/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001267/// DeclStmts and initializers in them.
1268CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1269 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001270
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001271 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001272
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001273 if (!VD) {
1274 autoCreateBlock();
1275 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001276 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001277 }
Mike Stump11289f42009-09-09 15:08:12 +00001278
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001279 bool IsReference = false;
1280 bool HasTemporaries = false;
1281
1282 // Destructors of temporaries in initialization expression should be called
1283 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001284 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001285 if (Init) {
1286 IsReference = VD->getType()->isReferenceType();
1287 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
1288
1289 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1290 // Generate destructors for temporaries in initialization expression.
1291 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
1292 IsReference);
1293 }
1294 }
1295
1296 autoCreateBlock();
1297 AppendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001298
Ted Kremenek93668002009-07-17 22:18:43 +00001299 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001300 AddStmtChoice asc = IsReference
1301 ? AddStmtChoice::AsLValueNotAlwaysAdd
1302 : AddStmtChoice::NotAlwaysAdd;
1303 if (HasTemporaries)
1304 // For expression with temporaries go directly to subexpression to omit
1305 // generating destructors for the second time.
1306 Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
1307 else
1308 Visit(Init, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001309 }
Mike Stump11289f42009-09-09 15:08:12 +00001310
Ted Kremenek93668002009-07-17 22:18:43 +00001311 // If the type of VD is a VLA, then we must process its size expressions.
1312 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1313 VA = FindVA(VA->getElementType().getTypePtr()))
1314 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001315
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001316 // Remove variable from local scope.
1317 if (ScopePos && VD == *ScopePos)
1318 ++ScopePos;
1319
Ted Kremenek93668002009-07-17 22:18:43 +00001320 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001321}
1322
1323CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001324 // We may see an if statement in the middle of a basic block, or it may be the
1325 // first statement we are processing. In either case, we create a new basic
1326 // block. First, we create the blocks for the then...else statements, and
1327 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001328 // middle of a block, we stop processing that block. That block is then the
1329 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001330
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001331 // Save local scope position because in case of condition variable ScopePos
1332 // won't be restored when traversing AST.
1333 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1334
1335 // Create local scope for possible condition variable.
1336 // Store scope position. Add implicit destructor.
1337 if (VarDecl* VD = I->getConditionVariable()) {
1338 LocalScope::const_iterator BeginScopePos = ScopePos;
1339 addLocalScopeForVarDecl(VD);
1340 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1341 }
1342
Mike Stump31feda52009-07-17 01:31:16 +00001343 // The block we were proccessing is now finished. Make it the successor
1344 // block.
1345 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001346 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001347 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001348 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001349 }
Mike Stump31feda52009-07-17 01:31:16 +00001350
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001351 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001353
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 if (Stmt* Else = I->getElse()) {
1355 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001356
Ted Kremenek9aae5132007-08-23 21:42:29 +00001357 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001358 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001359 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001360
1361 // If branch is not a compound statement create implicit scope
1362 // and add destructors.
1363 if (!isa<CompoundStmt>(Else))
1364 addLocalScopeAndDtors(Else);
1365
Ted Kremenek93668002009-07-17 22:18:43 +00001366 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001367
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001368 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1369 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001370 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001371 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001372 return 0;
1373 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001374 }
Mike Stump31feda52009-07-17 01:31:16 +00001375
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001376 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001377 CFGBlock* ThenBlock;
1378 {
1379 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001380 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001381 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001382 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001383
1384 // If branch is not a compound statement create implicit scope
1385 // and add destructors.
1386 if (!isa<CompoundStmt>(Then))
1387 addLocalScopeAndDtors(Then);
1388
Ted Kremenek93668002009-07-17 22:18:43 +00001389 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001390
Ted Kremenek1b379512009-04-01 03:52:47 +00001391 if (!ThenBlock) {
1392 // We can reach here if the "then" body has all NullStmts.
1393 // Create an empty block so we can distinguish between true and false
1394 // branches in path-sensitive analyses.
1395 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001396 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001397 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001398 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001399 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001400 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001401 }
1402
Mike Stump31feda52009-07-17 01:31:16 +00001403 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001405
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406 // Set the terminator of the new block to the If statement.
1407 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001408
Mike Stump773582d2009-07-23 23:25:26 +00001409 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001410 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001411
Ted Kremenek9aae5132007-08-23 21:42:29 +00001412 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001413 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1414 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001415
1416 // Add the condition as the last statement in the new block. This may create
1417 // new blocks as the condition may contain control-flow. Any newly created
1418 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001419 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001420
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001421 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1422 // and the condition variable initialization to the CFG.
1423 if (VarDecl *VD = I->getConditionVariable()) {
1424 if (Expr *Init = VD->getInit()) {
1425 autoCreateBlock();
1426 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1427 addStmt(Init);
1428 }
1429 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001430
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001431 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001432}
Mike Stump31feda52009-07-17 01:31:16 +00001433
1434
Ted Kremenek9aae5132007-08-23 21:42:29 +00001435CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001436 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001437 //
Mike Stump31feda52009-07-17 01:31:16 +00001438 // NOTE: If a "return" appears in the middle of a block, this means that the
1439 // code afterwards is DEAD (unreachable). We still keep a basic block
1440 // for that code; a simple "mark-and-sweep" from the entry block will be
1441 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001442
1443 // Create the new block.
1444 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001445
Ted Kremenek9aae5132007-08-23 21:42:29 +00001446 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001447 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001448 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001449
1450 // Add the return statement to the block. This may create new blocks if R
1451 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001452 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453}
1454
1455CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1456 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001457 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001458 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001459
Ted Kremenek93668002009-07-17 22:18:43 +00001460 if (!LabelBlock) // This can happen when the body is empty, i.e.
1461 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek93668002009-07-17 22:18:43 +00001463 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001464 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001465
1466 // Labels partition blocks, so this is the end of the basic block we were
1467 // processing (L is the block's label). Because this is label (and we have
1468 // already processed the substatement) there is no extra control-flow to worry
1469 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001470 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001471 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001472 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001473
1474 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001475 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001476
Ted Kremenek9aae5132007-08-23 21:42:29 +00001477 // This block is now the implicit successor of other blocks.
1478 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001479
Ted Kremenek9aae5132007-08-23 21:42:29 +00001480 return LabelBlock;
1481}
1482
1483CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001484 // Goto is a control-flow statement. Thus we stop processing the current
1485 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001486
Ted Kremenek9aae5132007-08-23 21:42:29 +00001487 Block = createBlock(false);
1488 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001489
1490 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001491 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001492
Ted Kremenek9aae5132007-08-23 21:42:29 +00001493 if (I == LabelMap.end())
1494 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001495 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1496 else {
1497 JumpTarget JT = I->second;
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001498 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001499 AddSuccessor(Block, JT.Block);
1500 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001501
Mike Stump31feda52009-07-17 01:31:16 +00001502 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001503}
1504
1505CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001506 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001507
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001508 // Save local scope position because in case of condition variable ScopePos
1509 // won't be restored when traversing AST.
1510 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1511
1512 // Create local scope for init statement and possible condition variable.
1513 // Add destructor for init statement and condition variable.
1514 // Store scope position for continue statement.
1515 if (Stmt* Init = F->getInit())
1516 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001517 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1518
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001519 if (VarDecl* VD = F->getConditionVariable())
1520 addLocalScopeForVarDecl(VD);
1521 LocalScope::const_iterator ContinueScopePos = ScopePos;
1522
1523 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1524
Mike Stump014b3ea2009-07-21 01:12:51 +00001525 // "for" is a control-flow statement. Thus we stop processing the current
1526 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001527 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001528 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001529 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001530 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001531 } else
1532 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001533
Ted Kremenek304a9532010-05-21 20:30:15 +00001534 // Save the current value for the break targets.
1535 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001536 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001537 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001538
Mike Stump31feda52009-07-17 01:31:16 +00001539 // Because of short-circuit evaluation, the condition of the loop can span
1540 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1541 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001542 CFGBlock* ExitConditionBlock = createBlock(false);
1543 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001544
Ted Kremenek81e14852007-08-27 19:46:09 +00001545 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001546 ExitConditionBlock->setTerminator(F);
1547
1548 // Now add the actual condition to the condition block. Because the condition
1549 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001550 if (Stmt* C = F->getCond()) {
1551 Block = ExitConditionBlock;
1552 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001553 assert(Block == EntryConditionBlock ||
1554 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001555
1556 // If this block contains a condition variable, add both the condition
1557 // variable and initializer to the CFG.
1558 if (VarDecl *VD = F->getConditionVariable()) {
1559 if (Expr *Init = VD->getInit()) {
1560 autoCreateBlock();
1561 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1562 EntryConditionBlock = addStmt(Init);
1563 assert(Block == EntryConditionBlock);
1564 }
1565 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001566
Ted Kremenek55957a82009-05-02 00:13:27 +00001567 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001568 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001569 return 0;
1570 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001571 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001572
Mike Stump31feda52009-07-17 01:31:16 +00001573 // The condition block is the implicit successor for the loop body as well as
1574 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001575 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001576
Mike Stump773582d2009-07-23 23:25:26 +00001577 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001578 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001579
Mike Stump773582d2009-07-23 23:25:26 +00001580 if (F->getCond())
1581 KnownVal = TryEvaluateBool(F->getCond());
1582
Ted Kremenek9aae5132007-08-23 21:42:29 +00001583 // Now create the loop body.
1584 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001585 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001586
Ted Kremenek304a9532010-05-21 20:30:15 +00001587 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001588 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1589 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001590
Ted Kremeneke9610502007-08-30 18:39:40 +00001591 // Create a new block to contain the (bottom) of the loop body.
1592 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001593
1594 // Loop body should end with destructor of Condition variable (if any).
1595 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001596
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001597 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001598 // Generate increment code in its own basic block. This is the target of
1599 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001600 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001601 } else {
1602 // No increment code. Create a special, empty, block that is used as the
1603 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001604 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001605 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001606 }
Mike Stump31feda52009-07-17 01:31:16 +00001607
Ted Kremenek902393b2009-04-28 00:51:56 +00001608 // Finish up the increment (or empty) block if it hasn't been already.
1609 if (Block) {
1610 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001611 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001612 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001613 Block = 0;
1614 }
Mike Stump31feda52009-07-17 01:31:16 +00001615
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001616 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001617
Ted Kremenek902393b2009-04-28 00:51:56 +00001618 // The starting block for the loop increment is the block that should
1619 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001620 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001621
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001622 // If body is not a compound statement create implicit scope
1623 // and add destructors.
1624 if (!isa<CompoundStmt>(F->getBody()))
1625 addLocalScopeAndDtors(F->getBody());
1626
Mike Stump31feda52009-07-17 01:31:16 +00001627 // Now populate the body block, and in the process create new blocks as we
1628 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001629 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001630
1631 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001632 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001633 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001634 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001635
Ted Kremenek30754282009-07-24 04:47:11 +00001636 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001637 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001638 }
Mike Stump31feda52009-07-17 01:31:16 +00001639
Ted Kremenek30754282009-07-24 04:47:11 +00001640 // Link up the condition block with the code that follows the loop. (the
1641 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001642 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001643
Ted Kremenek9aae5132007-08-23 21:42:29 +00001644 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001645 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001646 if (Stmt* I = F->getInit()) {
1647 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001648 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001649 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001650
1651 // There is no loop initialization. We are thus basically a while loop.
1652 // NULL out Block to force lazy block construction.
1653 Block = NULL;
1654 Succ = EntryConditionBlock;
1655 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001656}
1657
Ted Kremenek5868ec62010-04-11 17:02:10 +00001658CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1659 if (asc.alwaysAdd()) {
1660 autoCreateBlock();
1661 AppendStmt(Block, M, asc);
1662 }
1663 return Visit(M->getBase(),
1664 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1665 : AddStmtChoice::AsLValueNotAlwaysAdd);
1666}
1667
Ted Kremenek9d56e642008-11-11 17:10:00 +00001668CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1669 // Objective-C fast enumeration 'for' statements:
1670 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1671 //
1672 // for ( Type newVariable in collection_expression ) { statements }
1673 //
1674 // becomes:
1675 //
1676 // prologue:
1677 // 1. collection_expression
1678 // T. jump to loop_entry
1679 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001680 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001681 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1682 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1683 // TB:
1684 // statements
1685 // T. jump to loop_entry
1686 // FB:
1687 // what comes after
1688 //
1689 // and
1690 //
1691 // Type existingItem;
1692 // for ( existingItem in expression ) { statements }
1693 //
1694 // becomes:
1695 //
Mike Stump31feda52009-07-17 01:31:16 +00001696 // the same with newVariable replaced with existingItem; the binding works
1697 // the same except that for one ObjCForCollectionStmt::getElement() returns
1698 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001699 //
Mike Stump31feda52009-07-17 01:31:16 +00001700
Ted Kremenek9d56e642008-11-11 17:10:00 +00001701 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001702
Ted Kremenek9d56e642008-11-11 17:10:00 +00001703 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001704 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001705 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001706 LoopSuccessor = Block;
1707 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001708 } else
1709 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001710
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001711 // Build the condition blocks.
1712 CFGBlock* ExitConditionBlock = createBlock(false);
1713 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001715 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001716 ExitConditionBlock->setTerminator(S);
1717
1718 // The last statement in the block should be the ObjCForCollectionStmt, which
1719 // performs the actual binding to 'element' and determines if there are any
1720 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001721 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001722 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001723
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001724 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001725 // generate new blocks as necesary. We DON'T add the statement by default to
1726 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001727 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001728 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001729 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001730 return 0;
1731 Block = 0;
1732 }
Mike Stump31feda52009-07-17 01:31:16 +00001733
1734 // The condition block is the implicit successor for the loop body as well as
1735 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001736 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001737
Ted Kremenek9d56e642008-11-11 17:10:00 +00001738 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001739 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001740 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001741 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1742 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1743 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001744
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001745 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1746 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001747
Ted Kremenek93668002009-07-17 22:18:43 +00001748 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001749
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001750 if (!BodyBlock)
1751 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001752 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001753 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001754 return 0;
1755 }
Mike Stump31feda52009-07-17 01:31:16 +00001756
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001757 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001758 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001759 }
Mike Stump31feda52009-07-17 01:31:16 +00001760
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001761 // Link up the condition block with the code that follows the loop.
1762 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001763 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001764
Ted Kremenek9d56e642008-11-11 17:10:00 +00001765 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001766 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001767 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001768}
1769
Ted Kremenek49805452009-05-02 01:49:13 +00001770CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1771 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001772
Ted Kremenek49805452009-05-02 01:49:13 +00001773 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001774 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001775
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001776 // The sync body starts its own basic block. This makes it a little easier
1777 // for diagnostic clients.
1778 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001779 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001780 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001781
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001782 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001783 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001784 }
Mike Stump31feda52009-07-17 01:31:16 +00001785
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001786 // Add the @synchronized to the CFG.
1787 autoCreateBlock();
1788 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1789
Ted Kremenek49805452009-05-02 01:49:13 +00001790 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001791 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001792}
Mike Stump31feda52009-07-17 01:31:16 +00001793
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001794CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001795 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001796 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001797}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001798
Ted Kremenek9aae5132007-08-23 21:42:29 +00001799CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001800 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001801
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001802 // Save local scope position because in case of condition variable ScopePos
1803 // won't be restored when traversing AST.
1804 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1805
1806 // Create local scope for possible condition variable.
1807 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001808 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001809 if (VarDecl* VD = W->getConditionVariable()) {
1810 addLocalScopeForVarDecl(VD);
1811 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1812 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001813
Mike Stump014b3ea2009-07-21 01:12:51 +00001814 // "while" is a control-flow statement. Thus we stop processing the current
1815 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001816 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001817 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001818 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001819 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001820 } else
1821 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001822
1823 // Because of short-circuit evaluation, the condition of the loop can span
1824 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1825 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001826 CFGBlock* ExitConditionBlock = createBlock(false);
1827 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001828
Ted Kremenek81e14852007-08-27 19:46:09 +00001829 // Set the terminator for the "exit" condition block.
1830 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001831
1832 // Now add the actual condition to the condition block. Because the condition
1833 // itself may contain control-flow, new blocks may be created. Thus we update
1834 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001835 if (Stmt* C = W->getCond()) {
1836 Block = ExitConditionBlock;
1837 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001838 // The condition might finish the current 'Block'.
1839 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001840
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001841 // If this block contains a condition variable, add both the condition
1842 // variable and initializer to the CFG.
1843 if (VarDecl *VD = W->getConditionVariable()) {
1844 if (Expr *Init = VD->getInit()) {
1845 autoCreateBlock();
1846 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1847 EntryConditionBlock = addStmt(Init);
1848 assert(Block == EntryConditionBlock);
1849 }
1850 }
1851
Ted Kremenek55957a82009-05-02 00:13:27 +00001852 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001853 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001854 return 0;
1855 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001856 }
Mike Stump31feda52009-07-17 01:31:16 +00001857
1858 // The condition block is the implicit successor for the loop body as well as
1859 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001860 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001861
Mike Stump773582d2009-07-23 23:25:26 +00001862 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001863 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001864
Ted Kremenek9aae5132007-08-23 21:42:29 +00001865 // Process the loop body.
1866 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001867 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001868
1869 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001870 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1871 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1872 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001873
Mike Stump31feda52009-07-17 01:31:16 +00001874 // Create an empty block to represent the transition block for looping back
1875 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001876 Block = 0;
1877 assert(Succ == EntryConditionBlock);
1878 Succ = createBlock();
1879 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001880 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenek9aae5132007-08-23 21:42:29 +00001882 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001883 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek9aae5132007-08-23 21:42:29 +00001885 // NULL out Block to force lazy instantiation of blocks for the body.
1886 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001887
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001888 // Loop body should end with destructor of Condition variable (if any).
1889 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1890
1891 // If body is not a compound statement create implicit scope
1892 // and add destructors.
1893 if (!isa<CompoundStmt>(W->getBody()))
1894 addLocalScopeAndDtors(W->getBody());
1895
Ted Kremenek9aae5132007-08-23 21:42:29 +00001896 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001897 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001898
Ted Kremeneke9610502007-08-30 18:39:40 +00001899 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001900 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001901 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001902 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001903 return 0;
1904 }
Mike Stump31feda52009-07-17 01:31:16 +00001905
Ted Kremenek30754282009-07-24 04:47:11 +00001906 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001907 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001908 }
Mike Stump31feda52009-07-17 01:31:16 +00001909
Ted Kremenek30754282009-07-24 04:47:11 +00001910 // Link up the condition block with the code that follows the loop. (the
1911 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001912 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001913
1914 // There can be no more statements in the condition block since we loop back
1915 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001916 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001917
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001918 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001919 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001920 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001921}
Mike Stump11289f42009-09-09 15:08:12 +00001922
1923
Ted Kremenek93668002009-07-17 22:18:43 +00001924CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1925 // FIXME: For now we pretend that @catch and the code it contains does not
1926 // exit.
1927 return Block;
1928}
Mike Stump31feda52009-07-17 01:31:16 +00001929
Ted Kremenek93041ba2008-12-09 20:20:09 +00001930CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1931 // FIXME: This isn't complete. We basically treat @throw like a return
1932 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001933
Ted Kremenek0868eea2009-09-24 18:45:41 +00001934 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001935 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001936 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001937
Ted Kremenek93041ba2008-12-09 20:20:09 +00001938 // Create the new block.
1939 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001940
Ted Kremenek93041ba2008-12-09 20:20:09 +00001941 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001942 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001943
1944 // Add the statement to the block. This may create new blocks if S contains
1945 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001946 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001947}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001948
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001949CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001950 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001951 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001952 return 0;
1953
1954 // Create the new block.
1955 Block = createBlock(false);
1956
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001957 if (TryTerminatedBlock)
1958 // The current try statement is the only successor.
1959 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001960 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001961 // otherwise the Exit block is the only successor.
1962 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001963
1964 // Add the statement to the block. This may create new blocks if S contains
1965 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001966 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001967}
1968
Ted Kremenek93668002009-07-17 22:18:43 +00001969CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001970 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001971
Mike Stump8d50b6a2009-07-21 01:27:50 +00001972 // "do...while" is a control-flow statement. Thus we stop processing the
1973 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001974 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001975 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001976 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001977 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001978 } else
1979 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001980
1981 // Because of short-circuit evaluation, the condition of the loop can span
1982 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1983 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001984 CFGBlock* ExitConditionBlock = createBlock(false);
1985 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenek81e14852007-08-27 19:46:09 +00001987 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001988 ExitConditionBlock->setTerminator(D);
1989
1990 // Now add the actual condition to the condition block. Because the condition
1991 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001992 if (Stmt* C = D->getCond()) {
1993 Block = ExitConditionBlock;
1994 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001995 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001996 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001997 return 0;
1998 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001999 }
Mike Stump31feda52009-07-17 01:31:16 +00002000
Ted Kremeneka1523a32008-02-27 07:20:00 +00002001 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002002 Succ = EntryConditionBlock;
2003
Mike Stump773582d2009-07-23 23:25:26 +00002004 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00002005 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002006
Ted Kremenek9aae5132007-08-23 21:42:29 +00002007 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002008 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002009 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002010 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002011
Ted Kremenek9aae5132007-08-23 21:42:29 +00002012 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002013 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2014 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2015 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002016
Ted Kremenek9aae5132007-08-23 21:42:29 +00002017 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002018 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002019
Ted Kremenek9aae5132007-08-23 21:42:29 +00002020 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002021 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenek9aae5132007-08-23 21:42:29 +00002023 // NULL out Block to force lazy instantiation of blocks for the body.
2024 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002025
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002026 // If body is not a compound statement create implicit scope
2027 // and add destructors.
2028 if (!isa<CompoundStmt>(D->getBody()))
2029 addLocalScopeAndDtors(D->getBody());
2030
Ted Kremenek9aae5132007-08-23 21:42:29 +00002031 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002032 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002033
Ted Kremeneke9610502007-08-30 18:39:40 +00002034 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002035 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002036 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002037 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002038 return 0;
2039 }
Mike Stump31feda52009-07-17 01:31:16 +00002040
Ted Kremenek110974d2010-08-17 20:59:56 +00002041 if (!KnownVal.isFalse()) {
2042 // Add an intermediate block between the BodyBlock and the
2043 // ExitConditionBlock to represent the "loop back" transition. Create an
2044 // empty block to represent the transition block for looping back to the
2045 // head of the loop.
2046 // FIXME: Can we do this more efficiently without adding another block?
2047 Block = NULL;
2048 Succ = BodyBlock;
2049 CFGBlock *LoopBackBlock = createBlock();
2050 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002051
Ted Kremenek110974d2010-08-17 20:59:56 +00002052 // Add the loop body entry as a successor to the condition.
2053 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2054 }
2055 else
2056 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002057 }
Mike Stump31feda52009-07-17 01:31:16 +00002058
Ted Kremenek30754282009-07-24 04:47:11 +00002059 // Link up the condition block with the code that follows the loop.
2060 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002061 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002062
2063 // There can be no more statements in the body block(s) since we loop back to
2064 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002065 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002066
Ted Kremenek9aae5132007-08-23 21:42:29 +00002067 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002068 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002069 return BodyBlock;
2070}
2071
2072CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2073 // "continue" is a control-flow statement. Thus we stop processing the
2074 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002075 if (badCFG)
2076 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002077
Ted Kremenek9aae5132007-08-23 21:42:29 +00002078 // Now create a new block that ends with the continue statement.
2079 Block = createBlock(false);
2080 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002081
Ted Kremenek9aae5132007-08-23 21:42:29 +00002082 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002083 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002084 if (ContinueJumpTarget.Block) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00002085 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002086 AddSuccessor(Block, ContinueJumpTarget.Block);
2087 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002088 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002089
Ted Kremenek9aae5132007-08-23 21:42:29 +00002090 return Block;
2091}
Mike Stump11289f42009-09-09 15:08:12 +00002092
Ted Kremenek0747de62009-07-18 00:47:21 +00002093CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002094 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002095
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002096 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002097 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002098 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002099 }
Mike Stump11289f42009-09-09 15:08:12 +00002100
Ted Kremenek93668002009-07-17 22:18:43 +00002101 // VLA types have expressions that must be evaluated.
2102 if (E->isArgumentType()) {
2103 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2104 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2105 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002106 }
Mike Stump11289f42009-09-09 15:08:12 +00002107
Mike Stump31feda52009-07-17 01:31:16 +00002108 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002109}
Mike Stump11289f42009-09-09 15:08:12 +00002110
Ted Kremenek93668002009-07-17 22:18:43 +00002111/// VisitStmtExpr - Utility method to handle (nested) statement
2112/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002113CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2114 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002115 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002116 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002117 }
Ted Kremenek93668002009-07-17 22:18:43 +00002118 return VisitCompoundStmt(SE->getSubStmt());
2119}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002120
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002121CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002122 // "switch" is a control-flow statement. Thus we stop processing the current
2123 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002124 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002125
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002126 // Save local scope position because in case of condition variable ScopePos
2127 // won't be restored when traversing AST.
2128 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2129
2130 // Create local scope for possible condition variable.
2131 // Store scope position. Add implicit destructor.
2132 if (VarDecl* VD = Terminator->getConditionVariable()) {
2133 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2134 addLocalScopeForVarDecl(VD);
2135 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2136 }
2137
Ted Kremenek9aae5132007-08-23 21:42:29 +00002138 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002139 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002140 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002141 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002142 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002143
2144 // Save the current "switch" context.
2145 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002146 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002147 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002148
Mike Stump31feda52009-07-17 01:31:16 +00002149 // Set the "default" case to be the block after the switch statement. If the
2150 // switch statement contains a "default:", this value will be overwritten with
2151 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002152 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002153
Ted Kremenek9aae5132007-08-23 21:42:29 +00002154 // Create a new block that will contain the switch statement.
2155 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002156
Ted Kremenek9aae5132007-08-23 21:42:29 +00002157 // Now process the switch body. The code after the switch is the implicit
2158 // successor.
2159 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002160 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002161
2162 // When visiting the body, the case statements should automatically get linked
2163 // up to the switch. We also don't keep a pointer to the body, since all
2164 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002165 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002166 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002167
2168 // If body is not a compound statement create implicit scope
2169 // and add destructors.
2170 if (!isa<CompoundStmt>(Terminator->getBody()))
2171 addLocalScopeAndDtors(Terminator->getBody());
2172
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002173 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002174 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002175 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002176 return 0;
2177 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002178
Mike Stump31feda52009-07-17 01:31:16 +00002179 // If we have no "default:" case, the default transition is to the code
2180 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002181 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002182
Ted Kremenek81e14852007-08-27 19:46:09 +00002183 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002184 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002185 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002186 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002187 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002188
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002189 // Finally, if the SwitchStmt contains a condition variable, add both the
2190 // SwitchStmt and the condition variable initialization to the CFG.
2191 if (VarDecl *VD = Terminator->getConditionVariable()) {
2192 if (Expr *Init = VD->getInit()) {
2193 autoCreateBlock();
2194 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2195 addStmt(Init);
2196 }
2197 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002198
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002199 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002200}
2201
Ted Kremenek93668002009-07-17 22:18:43 +00002202CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002203 // CaseStmts are essentially labels, so they are the first statement in a
2204 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002205 CFGBlock *TopBlock = 0, *LastBlock = 0;
2206
2207 if (Stmt *Sub = CS->getSubStmt()) {
2208 // For deeply nested chains of CaseStmts, instead of doing a recursion
2209 // (which can blow out the stack), manually unroll and create blocks
2210 // along the way.
2211 while (isa<CaseStmt>(Sub)) {
2212 CFGBlock *CurrentBlock = createBlock(false);
2213 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002214
Ted Kremenek60fa6572010-08-04 23:54:30 +00002215 if (TopBlock)
2216 AddSuccessor(LastBlock, CurrentBlock);
2217 else
2218 TopBlock = CurrentBlock;
2219
2220 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2221 LastBlock = CurrentBlock;
2222
2223 CS = cast<CaseStmt>(Sub);
2224 Sub = CS->getSubStmt();
2225 }
2226
2227 addStmt(Sub);
2228 }
Mike Stump11289f42009-09-09 15:08:12 +00002229
Ted Kremenek55e91e82007-08-30 18:48:11 +00002230 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002231 if (!CaseBlock)
2232 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002233
2234 // Cases statements partition blocks, so this is the top of the basic block we
2235 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002236 CaseBlock->setLabel(CS);
2237
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002238 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002239 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002240
2241 // Add this block to the list of successors for the block with the switch
2242 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002243 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002244 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002245
Ted Kremenek9aae5132007-08-23 21:42:29 +00002246 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2247 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002248
Ted Kremenek60fa6572010-08-04 23:54:30 +00002249 if (TopBlock) {
2250 AddSuccessor(LastBlock, CaseBlock);
2251 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002252 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002253 // 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
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002584 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002585 // 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();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002795
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();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002800}
2801
Ted Kremenek6065ef62008-04-28 18:00:46 +00002802//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002803// Filtered walking of the CFG.
2804//===----------------------------------------------------------------------===//
2805
2806bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002807 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002808
2809 if (F.IgnoreDefaultsWithCoveredEnums) {
2810 // If the 'To' has no label or is labeled but the label isn't a
2811 // CaseStmt then filter this edge.
2812 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002813 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002814 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002815 const Stmt *L = To->getLabel();
2816 if (!L || !isa<CaseStmt>(L))
2817 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002818 }
2819 }
2820 }
2821
2822 return false;
2823}
2824
2825//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002826// Cleanup: CFG dstor.
2827//===----------------------------------------------------------------------===//
2828
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002829CFG::~CFG() {
2830 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2831}
Mike Stump31feda52009-07-17 01:31:16 +00002832
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002833//===----------------------------------------------------------------------===//
2834// CFG pretty printing
2835//===----------------------------------------------------------------------===//
2836
Ted Kremenek7e776b12007-08-22 18:22:34 +00002837namespace {
2838
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002839class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002840 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002841 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002842 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002843 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002844 signed CurrentBlock;
2845 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002846 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002847public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002848
Chris Lattnerc61089a2009-06-30 01:26:17 +00002849 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2850 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002851 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2852 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002853 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002854 BI != BEnd; ++BI, ++j ) {
2855 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2856 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2857 StmtMap[SE] = P;
2858
2859 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2860 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002861
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002862 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2863 if (VarDecl* VD = IS->getConditionVariable())
2864 DeclMap[VD] = P;
2865
2866 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2867 if (VarDecl* VD = FS->getConditionVariable())
2868 DeclMap[VD] = P;
2869
2870 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2871 if (VarDecl* VD = WS->getConditionVariable())
2872 DeclMap[VD] = P;
2873
2874 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2875 if (VarDecl* VD = SS->getConditionVariable())
2876 DeclMap[VD] = P;
2877
2878 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2879 if (VarDecl* VD = CS->getExceptionDecl())
2880 DeclMap[VD] = P;
2881 }
2882 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002883 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002884 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002885 }
Mike Stump31feda52009-07-17 01:31:16 +00002886
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002887 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002888
Chris Lattnerc61089a2009-06-30 01:26:17 +00002889 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002890 void setBlockID(signed i) { CurrentBlock = i; }
2891 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002892
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002893 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2894 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002895
2896 if (I == StmtMap.end())
2897 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002898
2899 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002900 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002901 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002902 }
Mike Stump31feda52009-07-17 01:31:16 +00002903
Ted Kremenek60983dc2010-01-19 20:52:05 +00002904 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002905 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002906 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002907
2908 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2909 DeclMapTy::iterator I = DeclMap.find(D);
2910
2911 if (I == DeclMap.end())
2912 return false;
2913
2914 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2915 && I->second.second == CurrentStmt) {
2916 return false;
2917 }
2918
2919 OS << "[B" << I->second.first << "." << I->second.second << "]";
2920 return true;
2921 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002922};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002923} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002924
Chris Lattnerc61089a2009-06-30 01:26:17 +00002925
2926namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002927class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002928 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002929
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002930 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002931 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002932 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002933public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002934 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002935 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002936 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002937
Ted Kremenek9aae5132007-08-23 21:42:29 +00002938 void VisitIfStmt(IfStmt* I) {
2939 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002940 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002941 }
Mike Stump31feda52009-07-17 01:31:16 +00002942
Ted Kremenek9aae5132007-08-23 21:42:29 +00002943 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002944 void VisitStmt(Stmt* Terminator) {
2945 Terminator->printPretty(OS, Helper, Policy);
2946 }
2947
Ted Kremenek9aae5132007-08-23 21:42:29 +00002948 void VisitForStmt(ForStmt* F) {
2949 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002950 if (F->getInit())
2951 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002952 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002953 if (Stmt* C = F->getCond())
2954 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002955 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002956 if (F->getInc())
2957 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002958 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002959 }
Mike Stump31feda52009-07-17 01:31:16 +00002960
Ted Kremenek9aae5132007-08-23 21:42:29 +00002961 void VisitWhileStmt(WhileStmt* W) {
2962 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002963 if (Stmt* C = W->getCond())
2964 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002965 }
Mike Stump31feda52009-07-17 01:31:16 +00002966
Ted Kremenek9aae5132007-08-23 21:42:29 +00002967 void VisitDoStmt(DoStmt* D) {
2968 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002969 if (Stmt* C = D->getCond())
2970 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002971 }
Mike Stump31feda52009-07-17 01:31:16 +00002972
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002973 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002974 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002975 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002976 }
Mike Stump31feda52009-07-17 01:31:16 +00002977
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002978 void VisitCXXTryStmt(CXXTryStmt* CS) {
2979 OS << "try ...";
2980 }
2981
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002982 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002983 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002984 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002985 }
Mike Stump31feda52009-07-17 01:31:16 +00002986
Ted Kremenek391f94a2007-08-31 22:29:13 +00002987 void VisitChooseExpr(ChooseExpr* C) {
2988 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002989 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002990 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002991 }
Mike Stump31feda52009-07-17 01:31:16 +00002992
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002993 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2994 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002995 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002996 }
Mike Stump31feda52009-07-17 01:31:16 +00002997
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002998 void VisitBinaryOperator(BinaryOperator* B) {
2999 if (!B->isLogicalOp()) {
3000 VisitExpr(B);
3001 return;
3002 }
Mike Stump31feda52009-07-17 01:31:16 +00003003
Douglas Gregor7de59662009-05-29 20:38:28 +00003004 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003005
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003006 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003007 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003008 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003009 return;
John McCalle3027922010-08-25 11:45:40 +00003010 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003011 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003012 return;
3013 default:
3014 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003015 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003016 }
Mike Stump31feda52009-07-17 01:31:16 +00003017
Ted Kremenek12687ff2007-08-27 21:54:41 +00003018 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003019 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003020 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003021};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003022} // end anonymous namespace
3023
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003024static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003025 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003026 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3027 Stmt *S = CS;
3028
3029 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003030
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003031 // special printing for statement-expressions.
3032 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3033 CompoundStmt* Sub = SE->getSubStmt();
3034
3035 if (Sub->child_begin() != Sub->child_end()) {
3036 OS << "({ ... ; ";
3037 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3038 OS << " })\n";
3039 return;
3040 }
3041 }
3042 // special printing for comma expressions.
3043 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3044 if (B->getOpcode() == BO_Comma) {
3045 OS << "... , ";
3046 Helper->handledStmt(B->getRHS(),OS);
3047 OS << '\n';
3048 return;
3049 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003050 }
3051 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003052 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003053
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003054 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003055 OS << " (OperatorCall)";
3056 } else if (isa<CXXBindTemporaryExpr>(S)) {
3057 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003058 }
Mike Stump31feda52009-07-17 01:31:16 +00003059
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003060 // Expressions need a newline.
3061 if (isa<Expr>(S))
3062 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003063
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003064 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3065 CXXBaseOrMemberInitializer* I = IE;
3066 if (I->isBaseInitializer())
3067 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
3068 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003069
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003070 OS << "(";
3071 if (Expr* IE = I->getInit())
3072 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3073 OS << ")";
3074
3075 if (I->isBaseInitializer())
3076 OS << " (Base initializer)\n";
3077 else OS << " (Member initializer)\n";
3078
3079 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3080 VarDecl* VD = DE.getVarDecl();
3081 Helper->handleDecl(VD, OS);
3082
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003083 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003084 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3085 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003086 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3087 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003088
3089 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3090 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003091
3092 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3093 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3094 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003095 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003096
3097 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3098 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003099
3100 const Type *T = FD->getType().getTypePtr();
3101 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3102 T = ET;
3103
Marcin Swiderski20b88732010-10-05 05:37:00 +00003104 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003105 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003106 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003107
3108 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3109 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3110 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3111 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003112 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003113}
Mike Stump31feda52009-07-17 01:31:16 +00003114
Chris Lattnerc61089a2009-06-30 01:26:17 +00003115static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3116 const CFGBlock& B,
3117 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003118
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003119 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003120
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003121 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003122 OS << "\n [ B" << B.getBlockID();
3123
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003124 if (&B == &cfg->getEntry())
3125 OS << " (ENTRY) ]\n";
3126 else if (&B == &cfg->getExit())
3127 OS << " (EXIT) ]\n";
3128 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003129 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003130 else
3131 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003132
Ted Kremenek71eca012007-08-29 23:20:49 +00003133 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003134 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003135
3136 if (print_edges)
3137 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003138
Mike Stump92244b02010-01-19 22:00:14 +00003139 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003140 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003141 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003142 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003143 C->getLHS()->printPretty(OS, Helper,
3144 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003145 if (C->getRHS()) {
3146 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003147 C->getRHS()->printPretty(OS, Helper,
3148 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003149 }
Mike Stump92244b02010-01-19 22:00:14 +00003150 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003151 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003152 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003153 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003154 if (CS->getExceptionDecl())
3155 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3156 0);
3157 else
3158 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003159 OS << ")";
3160
3161 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003162 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003163
Ted Kremenek71eca012007-08-29 23:20:49 +00003164 OS << ":\n";
3165 }
Mike Stump31feda52009-07-17 01:31:16 +00003166
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003167 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003168 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003169
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003170 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3171 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003172
Ted Kremenek71eca012007-08-29 23:20:49 +00003173 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003174 if (print_edges)
3175 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003176
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003177 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003178
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003179 if (Helper)
3180 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003181
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003182 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003183 }
Mike Stump31feda52009-07-17 01:31:16 +00003184
Ted Kremenek71eca012007-08-29 23:20:49 +00003185 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003186 if (B.getTerminator()) {
3187 if (print_edges)
3188 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003189
Ted Kremenek71eca012007-08-29 23:20:49 +00003190 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003191
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003192 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003193
Chris Lattnerc61089a2009-06-30 01:26:17 +00003194 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3195 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003196 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003197 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003198 }
Mike Stump31feda52009-07-17 01:31:16 +00003199
Ted Kremenek71eca012007-08-29 23:20:49 +00003200 if (print_edges) {
3201 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003202 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003203 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003204
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003205 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3206 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003207
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003208 if (i == 8 || (i-8) == 0)
3209 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003210
Ted Kremenek71eca012007-08-29 23:20:49 +00003211 OS << " B" << (*I)->getBlockID();
3212 }
Mike Stump31feda52009-07-17 01:31:16 +00003213
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003214 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003215
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003216 // Print the successors of this block.
3217 OS << " Successors (" << B.succ_size() << "):";
3218 i = 0;
3219
3220 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3221 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003222
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003223 if (i == 8 || (i-8) % 10 == 0)
3224 OS << "\n ";
3225
Mike Stump0d76d072009-07-20 23:24:15 +00003226 if (*I)
3227 OS << " B" << (*I)->getBlockID();
3228 else
3229 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003230 }
Mike Stump31feda52009-07-17 01:31:16 +00003231
Ted Kremenek71eca012007-08-29 23:20:49 +00003232 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003233 }
Mike Stump31feda52009-07-17 01:31:16 +00003234}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003235
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003236
3237/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003238void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003239
3240/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003241void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3242 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003243
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003244 // Print the entry block.
3245 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003246
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003247 // Iterate through the CFGBlocks and print them one by one.
3248 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3249 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003250 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003251 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003252
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003253 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003254 }
Mike Stump31feda52009-07-17 01:31:16 +00003255
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003256 // Print the exit block.
3257 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003258 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003259}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003260
3261/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003262void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3263 print(llvm::errs(), cfg, LO);
3264}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003265
3266/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3267/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003268void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3269 const LangOptions &LO) const {
3270 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003271 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003272}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003273
Ted Kremenek15647632008-01-30 23:02:42 +00003274/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003275void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003276 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003277 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003278 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003279}
3280
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003281Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003282 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003283 if (!Terminator)
3284 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003285
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003286 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003287
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003288 switch (Terminator->getStmtClass()) {
3289 default:
3290 break;
Mike Stump31feda52009-07-17 01:31:16 +00003291
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003292 case Stmt::ForStmtClass:
3293 E = cast<ForStmt>(Terminator)->getCond();
3294 break;
Mike Stump31feda52009-07-17 01:31:16 +00003295
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003296 case Stmt::WhileStmtClass:
3297 E = cast<WhileStmt>(Terminator)->getCond();
3298 break;
Mike Stump31feda52009-07-17 01:31:16 +00003299
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003300 case Stmt::DoStmtClass:
3301 E = cast<DoStmt>(Terminator)->getCond();
3302 break;
Mike Stump31feda52009-07-17 01:31:16 +00003303
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003304 case Stmt::IfStmtClass:
3305 E = cast<IfStmt>(Terminator)->getCond();
3306 break;
Mike Stump31feda52009-07-17 01:31:16 +00003307
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003308 case Stmt::ChooseExprClass:
3309 E = cast<ChooseExpr>(Terminator)->getCond();
3310 break;
Mike Stump31feda52009-07-17 01:31:16 +00003311
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003312 case Stmt::IndirectGotoStmtClass:
3313 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3314 break;
Mike Stump31feda52009-07-17 01:31:16 +00003315
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003316 case Stmt::SwitchStmtClass:
3317 E = cast<SwitchStmt>(Terminator)->getCond();
3318 break;
Mike Stump31feda52009-07-17 01:31:16 +00003319
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003320 case Stmt::ConditionalOperatorClass:
3321 E = cast<ConditionalOperator>(Terminator)->getCond();
3322 break;
Mike Stump31feda52009-07-17 01:31:16 +00003323
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003324 case Stmt::BinaryOperatorClass: // '&&' and '||'
3325 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003326 break;
Mike Stump31feda52009-07-17 01:31:16 +00003327
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003328 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003329 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003330 }
Mike Stump31feda52009-07-17 01:31:16 +00003331
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003332 return E ? E->IgnoreParens() : NULL;
3333}
3334
Ted Kremenek92137a32008-05-16 16:06:00 +00003335bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003336 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003337 if (!Terminator)
3338 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003339
Ted Kremenek92137a32008-05-16 16:06:00 +00003340 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003341
Ted Kremenek92137a32008-05-16 16:06:00 +00003342 switch (Terminator->getStmtClass()) {
3343 default:
3344 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003345
3346 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003347 case Stmt::WhileStmtClass:
3348 case Stmt::DoStmtClass:
3349 case Stmt::IfStmtClass:
3350 case Stmt::ChooseExprClass:
3351 case Stmt::ConditionalOperatorClass:
3352 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003353 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003354 }
Mike Stump31feda52009-07-17 01:31:16 +00003355
Ted Kremenek92137a32008-05-16 16:06:00 +00003356 return E ? E->IgnoreParens() : NULL;
3357}
3358
Ted Kremenek15647632008-01-30 23:02:42 +00003359
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003360//===----------------------------------------------------------------------===//
3361// CFG Graphviz Visualization
3362//===----------------------------------------------------------------------===//
3363
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003364
3365#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003366static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003367#endif
3368
Chris Lattnerc61089a2009-06-30 01:26:17 +00003369void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003370#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003371 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003372 GraphHelper = &H;
3373 llvm::ViewGraph(this,"CFG");
3374 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003375#endif
3376}
3377
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003378namespace llvm {
3379template<>
3380struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003381
3382 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3383
3384 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003385
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003386#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003387 std::string OutSStr;
3388 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003389 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003390 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003391
3392 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3393
3394 // Process string output to make it nicer...
3395 for (unsigned i = 0; i != OutStr.length(); ++i)
3396 if (OutStr[i] == '\n') { // Left justify
3397 OutStr[i] = '\\';
3398 OutStr.insert(OutStr.begin()+i+1, 'l');
3399 }
Mike Stump31feda52009-07-17 01:31:16 +00003400
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003401 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003402#else
3403 return "";
3404#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003405 }
3406};
3407} // end namespace llvm