blob: ef67d3b9576bb7575127361ef6cf9f3773325530 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenekfddd5182007-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 Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-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 Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Ted Kremenek852274d2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramer792bea92010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000047
Benjamin Kramer792bea92010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek431ac2d2010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5ba290a2010-03-02 21:43:54 +000050
Ted Kremenek852274d2009-12-16 03:18:58 +000051private:
Benjamin Kramer792bea92010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek852274d2009-12-16 03:18:58 +000053};
Mike Stump6d9828c2009-07-17 01:31:16 +000054
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +000065/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +000081 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000125 const_iterator operator++(int) {
126 const_iterator P = *this;
127 ++*this;
128 return P;
129 }
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000137
138 operator bool() const {
139 return *this != const_iterator();
140 }
141
142 int distance(const_iterator L);
Marcin Swiderskif1308c72010-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 Swiderski35387a02010-09-30 22:42:32 +0000162
163 void addVar(VarDecl* VD) {
164 Vars.push_back(VD);
165 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000166};
167
Marcin Swiderski35387a02010-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 Swiderskif1308c72010-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 Kremeneka34ea072008-08-04 22:51:42 +0000196/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-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 Stump6d9828c2009-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 Kremenekc310e932007-08-21 22:06:14 +0000209///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000210class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000211 typedef BlockScopePosPair JumpTarget;
212 typedef BlockScopePosPair JumpSource;
213
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000214 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000215 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000216
Ted Kremenekfddd5182007-08-21 21:42:03 +0000217 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000218 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000219 JumpTarget ContinueJumpTarget;
220 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000221 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000222 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000223 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Marcin Swiderskif1308c72010-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 Kremenek0cebe3e2007-08-21 23:26:17 +0000230 LabelMapTy LabelMap;
Mike Stump6d9828c2009-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 Swiderskif1308c72010-09-25 11:05:21 +0000234 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000235 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000236
Ted Kremenek19bb3562007-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 Stump6d9828c2009-07-17 01:31:16 +0000240
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000241 bool badCFG;
242 CFG::BuildOptions BuildOpts;
243
Mike Stump6d9828c2009-07-17 01:31:16 +0000244public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000245 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
246 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000247 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000248 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000249
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000250 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000251 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000252 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000253
Ted Kremenek4f880632009-07-17 22:18:43 +0000254private:
255 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-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 Kremenek4f880632009-07-17 22:18:43 +0000259 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000260 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000261 CFGBlock *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
262 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000263 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
264 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000265 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
266 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000267 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000268 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
269 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000270 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
271 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000272 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000273 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000274 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000275 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000276 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000277 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000278 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000279 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000280 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000281 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
282 CFGBlock *VisitDoStmt(DoStmt *D);
283 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000284 CFGBlock *VisitGotoStmt(GotoStmt* G);
285 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000286 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000287 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
288 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000289 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-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 Kremenek852274d2009-12-16 03:18:58 +0000296 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
297 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000298 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
299 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000300
Ted Kremenek852274d2009-12-16 03:18:58 +0000301 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
302 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000303 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000304
Marcin Swiderski8599e762010-11-03 06:19:35 +0000305 // Visitors to walk an AST and generate destructors of temporaries in
306 // full expression.
307 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
308 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
309 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
310 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
311 bool BindToTemporary);
312 CFGBlock *VisitConditionalOperatorForTemporaryDtors(ConditionalOperator *E,
313 bool BindToTemporary);
314
Ted Kremenek274f4332008-04-28 18:00:46 +0000315 // NYS == Not Yet Supported
316 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000317 badCFG = true;
318 return Block;
319 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000320
Ted Kremenek4f880632009-07-17 22:18:43 +0000321 void autoCreateBlock() { if (!Block) Block = createBlock(); }
322 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000323
Zhongxing Xudf119892010-06-03 06:43:23 +0000324 CFGBlock *addStmt(Stmt *S) {
325 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000326 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000327 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000328 void addAutomaticObjDtors(LocalScope::const_iterator B,
329 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000330 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000331
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000332 // Local scopes creation.
333 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
334
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000335 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000336 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
337 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
338
339 void addLocalScopeAndDtors(Stmt* S);
340
341 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek852274d2009-12-16 03:18:58 +0000342 void AppendStmt(CFGBlock *B, Stmt *S,
343 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
344 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000345 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000346 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
347 B->appendInitializer(I, cfg->getBumpVectorContext());
348 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000349 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
350 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
351 }
352 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
353 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
354 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000355 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
356 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
357 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000358
Marcin Swiderski53de1342010-09-30 22:54:37 +0000359 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
360 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
361 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
362 LocalScope::const_iterator E, Stmt* S);
363 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
364 LocalScope::const_iterator B, LocalScope::const_iterator E);
365
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000366 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
367 B->addSuccessor(S, cfg->getBumpVectorContext());
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000370 /// TryResult - a class representing a variant over the values
371 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
372 /// and is used by the CFGBuilder to decide if a branch condition
373 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000374 class TryResult {
375 int X;
376 public:
377 TryResult(bool b) : X(b ? 1 : 0) {}
378 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Ted Kremenek941fde82009-07-24 04:47:11 +0000380 bool isTrue() const { return X == 1; }
381 bool isFalse() const { return X == 0; }
382 bool isKnown() const { return X >= 0; }
383 void negate() {
384 assert(isKnown());
385 X ^= 0x1;
386 }
387 };
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Mike Stump00998a02009-07-23 23:25:26 +0000389 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
390 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000391 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000392 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000393 return TryResult();
394
Mike Stump00998a02009-07-23 23:25:26 +0000395 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000396 if (!S->isTypeDependent() && !S->isValueDependent() &&
397 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000398 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000399
400 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000401 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000402};
Mike Stump6d9828c2009-07-17 01:31:16 +0000403
Douglas Gregor898574e2008-12-05 23:32:09 +0000404// FIXME: Add support for dependent-sized array types in C++?
405// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000406static VariableArrayType* FindVA(Type* t) {
407 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
408 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
409 if (vat->getSizeExpr())
410 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000411
Ted Kremenek610a09e2008-09-26 22:58:57 +0000412 t = vt->getElementType().getTypePtr();
413 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000414
Ted Kremenek610a09e2008-09-26 22:58:57 +0000415 return 0;
416}
Mike Stump6d9828c2009-07-17 01:31:16 +0000417
418/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
419/// arbitrary statement. Examples include a single expression or a function
420/// body (compound statement). The ownership of the returned CFG is
421/// transferred to the caller. If CFG construction fails, this method returns
422/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000423CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000424 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000425
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000426 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000427 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000428 if (!Statement)
429 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000430
Ted Kremenek6c52c782010-09-14 23:41:16 +0000431 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000432
433 // Create an empty block that will serve as the exit block for the CFG. Since
434 // this is the first block added to the CFG, it will be implicitly registered
435 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000436 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000437 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000438 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000439
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000440 if (BuildOpts.AddImplicitDtors)
441 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
442 addImplicitDtorsForDestructor(DD);
443
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000444 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000445 CFGBlock *B = addStmt(Statement);
446
447 if (badCFG)
448 return NULL;
449
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000450 // For C++ constructor add initializers to CFG.
451 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
452 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
453 E = CD->init_rend(); I != E; ++I) {
454 B = addInitializer(*I);
455 if (badCFG)
456 return NULL;
457 }
458 }
459
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000460 if (B)
461 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000462
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000463 // Backpatch the gotos whose label -> block mappings we didn't know when we
464 // encountered them.
465 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
466 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000467
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000468 CFGBlock* B = I->Block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000469 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
470 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000471
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000472 // If there is no target for the goto, then we are looking at an
473 // incomplete AST. Handle this by not registering a successor.
474 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000475
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000476 JumpTarget JT = LI->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000477 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000478 AddSuccessor(B, JT.Block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000479 }
480
481 // Add successors to the Indirect Goto Dispatch block (if we have one).
482 if (CFGBlock* B = cfg->getIndirectGotoBlock())
483 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
484 E = AddressTakenLabels.end(); I != E; ++I ) {
485
486 // Lookup the target block.
487 LabelMapTy::iterator LI = LabelMap.find(*I);
488
489 // If there is no target block that contains label, then we are looking
490 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000491 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000492
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000493 AddSuccessor(B, LI->second.Block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000494 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000495
Mike Stump6d9828c2009-07-17 01:31:16 +0000496 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000497 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000498
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000499 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000500}
Mike Stump6d9828c2009-07-17 01:31:16 +0000501
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000502/// createBlock - Used to lazily create blocks that are connected
503/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000504CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000505 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000506 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000507 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000508 return B;
509}
Mike Stump6d9828c2009-07-17 01:31:16 +0000510
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000511/// addInitializer - Add C++ base or member initializer element to CFG.
512CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
513 if (!BuildOpts.AddInitializers)
514 return Block;
515
Marcin Swiderski8599e762010-11-03 06:19:35 +0000516 bool IsReference = false;
517 bool HasTemporaries = false;
518
519 // Destructors of temporaries in initialization expression should be called
520 // after initialization finishes.
521 Expr *Init = I->getInit();
522 if (Init) {
523 if (FieldDecl *FD = I->getMember())
524 IsReference = FD->getType()->isReferenceType();
525 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
526
527 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
528 // Generate destructors for temporaries in initialization expression.
529 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
530 IsReference);
531 }
532 }
533
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000534 autoCreateBlock();
535 appendInitializer(Block, I);
536
Marcin Swiderski8599e762010-11-03 06:19:35 +0000537 if (Init) {
538 AddStmtChoice asc = IsReference
539 ? AddStmtChoice::AsLValueNotAlwaysAdd
540 : AddStmtChoice::NotAlwaysAdd;
541 if (HasTemporaries)
542 // For expression with temporaries go directly to subexpression to omit
543 // generating destructors for the second time.
544 return Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
545 return Visit(Init, asc);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000546 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000547
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000548 return Block;
549}
550
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000551/// addAutomaticObjDtors - Add to current block automatic objects destructors
552/// for objects in range of local scope positions. Use S as trigger statement
553/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000554void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
555 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000556 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000557 return;
558
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000559 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000560 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000561
562 autoCreateBlock();
563 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000564}
565
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000566/// addImplicitDtorsForDestructor - Add implicit destructors generated for
567/// base and member objects in destructor.
568void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
569 assert (BuildOpts.AddImplicitDtors
570 && "Can be called only when dtors should be added");
571 const CXXRecordDecl *RD = DD->getParent();
572
573 // At the end destroy virtual base objects.
574 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
575 VE = RD->vbases_end(); VI != VE; ++VI) {
576 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
577 if (!CD->hasTrivialDestructor()) {
578 autoCreateBlock();
579 appendBaseDtor(Block, VI);
580 }
581 }
582
583 // Before virtual bases destroy direct base objects.
584 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
585 BE = RD->bases_end(); BI != BE; ++BI) {
586 if (!BI->isVirtual()) {
587 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
588 if (!CD->hasTrivialDestructor()) {
589 autoCreateBlock();
590 appendBaseDtor(Block, BI);
591 }
592 }
593 }
594
595 // First destroy member objects.
596 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
597 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000598 // Check for constant size array. Set type to array element type.
599 QualType QT = FI->getType();
600 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
601 if (AT->getSize() == 0)
602 continue;
603 QT = AT->getElementType();
604 }
605
606 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000607 if (!CD->hasTrivialDestructor()) {
608 autoCreateBlock();
609 appendMemberDtor(Block, *FI);
610 }
611 }
612}
613
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000614/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
615/// way return valid LocalScope object.
616LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
617 if (!Scope) {
618 Scope = cfg->getAllocator().Allocate<LocalScope>();
619 new (Scope) LocalScope(ScopePos);
620 }
621 return Scope;
622}
623
624/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000625/// that should create implicit scope (e.g. if/else substatements).
626void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000627 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000628 return;
629
630 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000631
632 // For compound statement we will be creating explicit scope.
633 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
634 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
635 ; BI != BE; ++BI) {
636 Stmt* SI = *BI;
637 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
638 SI = LS->getSubStmt();
639 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
640 Scope = addLocalScopeForDeclStmt(DS, Scope);
641 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000642 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000643 }
644
645 // For any other statement scope will be implicit and as such will be
646 // interesting only for DeclStmt.
647 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
648 S = LS->getSubStmt();
649 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000650 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000651}
652
653/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
654/// reuse Scope if not NULL.
655LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000656 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000657 if (!BuildOpts.AddImplicitDtors)
658 return Scope;
659
660 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
661 ; DI != DE; ++DI) {
662 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
663 Scope = addLocalScopeForVarDecl(VD, Scope);
664 }
665 return Scope;
666}
667
668/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
669/// create add scope for automatic objects and temporary objects bound to
670/// const reference. Will reuse Scope if not NULL.
671LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000672 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000673 if (!BuildOpts.AddImplicitDtors)
674 return Scope;
675
676 // Check if variable is local.
677 switch (VD->getStorageClass()) {
678 case SC_None:
679 case SC_Auto:
680 case SC_Register:
681 break;
682 default: return Scope;
683 }
684
685 // Check for const references bound to temporary. Set type to pointee.
686 QualType QT = VD->getType();
687 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
688 QT = RT->getPointeeType();
689 if (!QT.isConstQualified())
690 return Scope;
691 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
692 return Scope;
693 }
694
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000695 // Check for constant size array. Set type to array element type.
696 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
697 if (AT->getSize() == 0)
698 return Scope;
699 QT = AT->getElementType();
700 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000701
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000702 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000703 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
704 if (!CD->hasTrivialDestructor()) {
705 // Add the variable to scope
706 Scope = createOrReuseLocalScope(Scope);
707 Scope->addVar(VD);
708 ScopePos = Scope->begin();
709 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000710 return Scope;
711}
712
713/// addLocalScopeAndDtors - For given statement add local scope for it and
714/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
715void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
716 if (!BuildOpts.AddImplicitDtors)
717 return;
718
719 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000720 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000721 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
722}
723
Marcin Swiderski53de1342010-09-30 22:54:37 +0000724/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
725/// automatic storage duration to CFGBlock's elements vector. Insertion will be
726/// performed in place specified with iterator.
727void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
728 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
729 BumpVectorContext& C = cfg->getBumpVectorContext();
730 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
731 while (B != E)
732 I = Blk->insertAutomaticObjDtor(I, *B++, S);
733}
734
735/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
736/// automatic storage duration to CFGBlock's elements vector. Elements will be
737/// appended to physical end of the vector which happens to be logical
738/// beginning.
739void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
740 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
741 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
742}
743
744/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
745/// variables with automatic storage duration to CFGBlock's elements vector.
746/// Elements will be prepended to physical beginning of the vector which
747/// happens to be logical end. Use blocks terminator as statement that specifies
748/// destructors call site.
749void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
750 LocalScope::const_iterator B, LocalScope::const_iterator E) {
751 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
752}
753
Ted Kremenek4f880632009-07-17 22:18:43 +0000754/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000755/// blocks for ternary operators, &&, and ||. We also process "," and
756/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000757CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000758tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000759 if (!S) {
760 badCFG = true;
761 return 0;
762 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000763 switch (S->getStmtClass()) {
764 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000765 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000766
767 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000768 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek4f880632009-07-17 22:18:43 +0000770 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000771 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek4f880632009-07-17 22:18:43 +0000773 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000774 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000775
Ted Kremenek4f880632009-07-17 22:18:43 +0000776 case Stmt::BreakStmtClass:
777 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek4f880632009-07-17 22:18:43 +0000779 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000780 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000781 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek4f880632009-07-17 22:18:43 +0000783 case Stmt::CaseStmtClass:
784 return VisitCaseStmt(cast<CaseStmt>(S));
785
786 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000787 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 case Stmt::CompoundStmtClass:
790 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenek4f880632009-07-17 22:18:43 +0000792 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000793 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek4f880632009-07-17 22:18:43 +0000795 case Stmt::ContinueStmtClass:
796 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek021c8af2010-01-19 20:40:33 +0000798 case Stmt::CXXCatchStmtClass:
799 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
800
Marcin Swiderski8599e762010-11-03 06:19:35 +0000801 case Stmt::CXXExprWithTemporariesClass:
802 return VisitCXXExprWithTemporaries(cast<CXXExprWithTemporaries>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000803
Zhongxing Xua725ed42010-11-01 13:04:58 +0000804 case Stmt::CXXBindTemporaryExprClass:
805 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
806
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000807 case Stmt::CXXConstructExprClass:
808 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
809
Zhongxing Xua725ed42010-11-01 13:04:58 +0000810 case Stmt::CXXFunctionalCastExprClass:
811 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
812
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000813 case Stmt::CXXTemporaryObjectExprClass:
814 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
815
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000816 case Stmt::CXXMemberCallExprClass:
817 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
818
Ted Kremenek021c8af2010-01-19 20:40:33 +0000819 case Stmt::CXXThrowExprClass:
820 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000821
Ted Kremenek021c8af2010-01-19 20:40:33 +0000822 case Stmt::CXXTryStmtClass:
823 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000824
Ted Kremenek4f880632009-07-17 22:18:43 +0000825 case Stmt::DeclStmtClass:
826 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek4f880632009-07-17 22:18:43 +0000828 case Stmt::DefaultStmtClass:
829 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek4f880632009-07-17 22:18:43 +0000831 case Stmt::DoStmtClass:
832 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenek4f880632009-07-17 22:18:43 +0000834 case Stmt::ForStmtClass:
835 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek4f880632009-07-17 22:18:43 +0000837 case Stmt::GotoStmtClass:
838 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Ted Kremenek4f880632009-07-17 22:18:43 +0000840 case Stmt::IfStmtClass:
841 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Zhongxing Xua725ed42010-11-01 13:04:58 +0000843 case Stmt::ImplicitCastExprClass:
844 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
845
Ted Kremenek4f880632009-07-17 22:18:43 +0000846 case Stmt::IndirectGotoStmtClass:
847 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek4f880632009-07-17 22:18:43 +0000849 case Stmt::LabelStmtClass:
850 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremenek115c1b92010-04-11 17:02:10 +0000852 case Stmt::MemberExprClass:
853 return VisitMemberExpr(cast<MemberExpr>(S), asc);
854
Ted Kremenek4f880632009-07-17 22:18:43 +0000855 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000856 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
857
Ted Kremenek4f880632009-07-17 22:18:43 +0000858 case Stmt::ObjCAtSynchronizedStmtClass:
859 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenek4f880632009-07-17 22:18:43 +0000861 case Stmt::ObjCAtThrowStmtClass:
862 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek4f880632009-07-17 22:18:43 +0000864 case Stmt::ObjCAtTryStmtClass:
865 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Ted Kremenek4f880632009-07-17 22:18:43 +0000867 case Stmt::ObjCForCollectionStmtClass:
868 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek4f880632009-07-17 22:18:43 +0000870 case Stmt::ParenExprClass:
871 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000872 goto tryAgain;
873
Ted Kremenek4f880632009-07-17 22:18:43 +0000874 case Stmt::NullStmtClass:
875 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Ted Kremenek4f880632009-07-17 22:18:43 +0000877 case Stmt::ReturnStmtClass:
878 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek4f880632009-07-17 22:18:43 +0000880 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000881 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek4f880632009-07-17 22:18:43 +0000883 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000884 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Ted Kremenek4f880632009-07-17 22:18:43 +0000886 case Stmt::SwitchStmtClass:
887 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek4f880632009-07-17 22:18:43 +0000889 case Stmt::WhileStmtClass:
890 return VisitWhileStmt(cast<WhileStmt>(S));
891 }
892}
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek852274d2009-12-16 03:18:58 +0000894CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
895 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000896 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000897 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek4f880632009-07-17 22:18:43 +0000900 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000901}
Mike Stump6d9828c2009-07-17 01:31:16 +0000902
Ted Kremenek4f880632009-07-17 22:18:43 +0000903/// VisitChildren - Visit the children of a Stmt.
904CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
905 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000906 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000907 E = Terminator->child_end(); I != E; ++I) {
908 if (*I) B = Visit(*I);
909 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000910 return B;
911}
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Ted Kremenek852274d2009-12-16 03:18:58 +0000913CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
914 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000915 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000916
Ted Kremenek852274d2009-12-16 03:18:58 +0000917 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000918 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000919 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000920 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000921
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000922 return Block;
923}
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek852274d2009-12-16 03:18:58 +0000925CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
926 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000927 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000928 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000929 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000931 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000932 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek4f880632009-07-17 22:18:43 +0000934 // create the block evaluating the LHS
935 CFGBlock* LHSBlock = createBlock(false);
936 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremenek4f880632009-07-17 22:18:43 +0000938 // create the block evaluating the RHS
939 Succ = ConfluenceBlock;
940 Block = NULL;
941 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000942
943 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000944 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000945 return 0;
946 }
947 else {
948 // Create an empty block for cases where the RHS doesn't require
949 // any explicit statements in the CFG.
950 RHSBlock = createBlock();
951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Mike Stump00998a02009-07-23 23:25:26 +0000953 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000954 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000955 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000956 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000957
Ted Kremenek4f880632009-07-17 22:18:43 +0000958 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000959 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000960 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
961 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000962 } else {
John McCall2de56d12010-08-25 11:45:40 +0000963 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000964 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
965 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Ted Kremenek4f880632009-07-17 22:18:43 +0000968 // Generate the blocks for evaluating the LHS.
969 Block = LHSBlock;
970 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000971 }
John McCall2de56d12010-08-25 11:45:40 +0000972 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000973 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000974 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000975 addStmt(B->getRHS());
976 return addStmt(B->getLHS());
977 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000978 else if (B->isAssignmentOp()) {
979 if (asc.alwaysAdd()) {
980 autoCreateBlock();
981 AppendStmt(Block, B, asc);
982 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000983
Marcin Swiderskie1667192010-10-24 08:21:40 +0000984 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
985 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +0000986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Marcin Swiderskie1667192010-10-24 08:21:40 +0000988 if (asc.alwaysAdd()) {
989 autoCreateBlock();
990 AppendStmt(Block, B, asc);
991 }
992
Zhongxing Xua1898dd2010-10-27 03:23:10 +0000993 CFGBlock *RBlock = Visit(B->getRHS());
994 CFGBlock *LBlock = Visit(B->getLHS());
995 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
996 // containing a DoStmt, and the LHS doesn't create a new block, then we should
997 // return RBlock. Otherwise we'll incorrectly return NULL.
998 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000999}
1000
Ted Kremenek852274d2009-12-16 03:18:58 +00001001CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1002 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001003 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001004 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001005 }
1006 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001007}
1008
Ted Kremenek4f880632009-07-17 22:18:43 +00001009CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1010 // "break" is a control-flow statement. Thus we stop processing the current
1011 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001012 if (badCFG)
1013 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Ted Kremenek4f880632009-07-17 22:18:43 +00001015 // Now create a new block that ends with the break statement.
1016 Block = createBlock(false);
1017 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Ted Kremenek4f880632009-07-17 22:18:43 +00001019 // If there is no target for the break, then we are looking at an incomplete
1020 // AST. This means that the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001021 if (BreakJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001022 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001023 AddSuccessor(Block, BreakJumpTarget.Block);
1024 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001025 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001026
1027
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001028 return Block;
1029}
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Mike Stump4c45aa12010-01-21 15:20:48 +00001031static bool CanThrow(Expr *E) {
1032 QualType Ty = E->getType();
1033 if (Ty->isFunctionPointerType())
1034 Ty = Ty->getAs<PointerType>()->getPointeeType();
1035 else if (Ty->isBlockPointerType())
1036 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001037
Mike Stump4c45aa12010-01-21 15:20:48 +00001038 const FunctionType *FT = Ty->getAs<FunctionType>();
1039 if (FT) {
1040 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1041 if (Proto->hasEmptyExceptionSpec())
1042 return false;
1043 }
1044 return true;
1045}
1046
Ted Kremenek852274d2009-12-16 03:18:58 +00001047CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001048 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001049 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001050 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001051 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001052 }
Mike Stump24556362009-07-25 21:26:53 +00001053
Mike Stump4c45aa12010-01-21 15:20:48 +00001054 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001055
1056 // Languages without exceptions are assumed to not throw.
1057 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001058 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001059 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001060 }
1061
1062 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001063 if (FD->hasAttr<NoReturnAttr>())
1064 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001065 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001066 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001067 }
Mike Stump24556362009-07-25 21:26:53 +00001068
Mike Stump4c45aa12010-01-21 15:20:48 +00001069 if (!CanThrow(C->getCallee()))
1070 AddEHEdge = false;
1071
Zhongxing Xufc61d942010-06-03 06:23:18 +00001072 if (!NoReturn && !AddEHEdge) {
1073 if (asc.asLValue())
1074 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1075 else
1076 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Mike Stump079bd722010-01-19 22:00:14 +00001079 if (Block) {
1080 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001081 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001082 return 0;
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Mike Stump079bd722010-01-19 22:00:14 +00001085 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +00001086 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001087
Mike Stump079bd722010-01-19 22:00:14 +00001088 if (NoReturn) {
1089 // Wire this to the exit block directly.
1090 AddSuccessor(Block, &cfg->getExit());
1091 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001092 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001093 // Add exceptional edges.
1094 if (TryTerminatedBlock)
1095 AddSuccessor(Block, TryTerminatedBlock);
1096 else
1097 AddSuccessor(Block, &cfg->getExit());
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Mike Stump24556362009-07-25 21:26:53 +00001100 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001101}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001102
Ted Kremenek852274d2009-12-16 03:18:58 +00001103CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1104 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001105 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001106 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001107 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001108 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Ted Kremenek115c1b92010-04-11 17:02:10 +00001110 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1111 : AddStmtChoice::AlwaysAdd;
1112
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001113 Succ = ConfluenceBlock;
1114 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +00001115 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001116 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001117 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001119 Succ = ConfluenceBlock;
1120 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +00001121 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001122 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001123 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001125 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001126 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001127 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001128 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1129 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001130 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001131 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001132}
Mike Stump1eb44332009-09-09 15:08:12 +00001133
1134
1135CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001136 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001137 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001138
1139 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1140 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001141 // If we hit a segment of code just containing ';' (NullStmts), we can
1142 // get a null block back. In such cases, just use the LastBlock
1143 if (CFGBlock *newBlock = addStmt(*I))
1144 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001146 if (badCFG)
1147 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001148 }
Mike Stump079bd722010-01-19 22:00:14 +00001149
Ted Kremenek4f880632009-07-17 22:18:43 +00001150 return LastBlock;
1151}
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Ted Kremenek852274d2009-12-16 03:18:58 +00001153CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1154 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001155 // Create the confluence block that will "merge" the results of the ternary
1156 // expression.
1157 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001158 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001159 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001160 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenek115c1b92010-04-11 17:02:10 +00001162 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1163 : AddStmtChoice::AlwaysAdd;
1164
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001165 // Create a block for the LHS expression if there is an LHS expression. A
1166 // GCC extension allows LHS to be NULL, causing the condition to be the
1167 // value that is returned instead.
1168 // e.g: x ?: y is shorthand for: x ? x : y;
1169 Succ = ConfluenceBlock;
1170 Block = NULL;
1171 CFGBlock* LHSBlock = NULL;
1172 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +00001173 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001174 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001175 return 0;
1176 Block = NULL;
1177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001179 // Create the block for the RHS expression.
1180 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +00001181 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001182 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001183 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001185 // Create the block that will contain the condition.
1186 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Mike Stump00998a02009-07-23 23:25:26 +00001188 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001189 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001190 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001191 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001192 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +00001193 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001194 // If we know the condition is false, add NULL as the successor for
1195 // the block containing the condition. In this case, the confluence
1196 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001197 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +00001198 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001199 } else {
1200 // If we have no LHS expression, add the ConfluenceBlock as a direct
1201 // successor for the block containing the condition. Moreover, we need to
1202 // reverse the order of the predecessors in the ConfluenceBlock because
1203 // the RHSBlock will have been added to the succcessors already, and we
1204 // want the first predecessor to the the block containing the expression
1205 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001206 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +00001207 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001208 std::reverse(ConfluenceBlock->pred_begin(),
1209 ConfluenceBlock->pred_end());
1210 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001213 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001214 Block->setTerminator(C);
1215 return addStmt(C->getCond());
1216}
1217
Ted Kremenek4f880632009-07-17 22:18:43 +00001218CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001219 if (DS->isSingleDecl())
1220 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek4f880632009-07-17 22:18:43 +00001222 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Ted Kremenek4f880632009-07-17 22:18:43 +00001224 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1225 typedef llvm::SmallVector<Decl*,10> BufTy;
1226 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Ted Kremenek4f880632009-07-17 22:18:43 +00001228 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1229 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1230 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1231 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Ted Kremenek4f880632009-07-17 22:18:43 +00001233 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1234 // automatically freed with the CFG.
1235 DeclGroupRef DG(*I);
1236 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001237 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001238 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Ted Kremenek4f880632009-07-17 22:18:43 +00001240 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001241 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
1244 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001245}
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenek4f880632009-07-17 22:18:43 +00001247/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001248/// DeclStmts and initializers in them.
1249CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1250 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001251
Marcin Swiderski8599e762010-11-03 06:19:35 +00001252 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Marcin Swiderski8599e762010-11-03 06:19:35 +00001254 if (!VD) {
1255 autoCreateBlock();
1256 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001257 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001258 }
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Marcin Swiderski8599e762010-11-03 06:19:35 +00001260 bool IsReference = false;
1261 bool HasTemporaries = false;
1262
1263 // Destructors of temporaries in initialization expression should be called
1264 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001265 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001266 if (Init) {
1267 IsReference = VD->getType()->isReferenceType();
1268 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
1269
1270 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1271 // Generate destructors for temporaries in initialization expression.
1272 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
1273 IsReference);
1274 }
1275 }
1276
1277 autoCreateBlock();
1278 AppendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek4f880632009-07-17 22:18:43 +00001280 if (Init) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001281 AddStmtChoice asc = IsReference
1282 ? AddStmtChoice::AsLValueNotAlwaysAdd
1283 : AddStmtChoice::NotAlwaysAdd;
1284 if (HasTemporaries)
1285 // For expression with temporaries go directly to subexpression to omit
1286 // generating destructors for the second time.
1287 Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
1288 else
1289 Visit(Init, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek4f880632009-07-17 22:18:43 +00001292 // If the type of VD is a VLA, then we must process its size expressions.
1293 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1294 VA = FindVA(VA->getElementType().getTypePtr()))
1295 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001297 // Remove variable from local scope.
1298 if (ScopePos && VD == *ScopePos)
1299 ++ScopePos;
1300
Ted Kremenek4f880632009-07-17 22:18:43 +00001301 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001302}
1303
1304CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001305 // We may see an if statement in the middle of a basic block, or it may be the
1306 // first statement we are processing. In either case, we create a new basic
1307 // block. First, we create the blocks for the then...else statements, and
1308 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001309 // middle of a block, we stop processing that block. That block is then the
1310 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001311
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001312 // Save local scope position because in case of condition variable ScopePos
1313 // won't be restored when traversing AST.
1314 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1315
1316 // Create local scope for possible condition variable.
1317 // Store scope position. Add implicit destructor.
1318 if (VarDecl* VD = I->getConditionVariable()) {
1319 LocalScope::const_iterator BeginScopePos = ScopePos;
1320 addLocalScopeForVarDecl(VD);
1321 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1322 }
1323
Mike Stump6d9828c2009-07-17 01:31:16 +00001324 // The block we were proccessing is now finished. Make it the successor
1325 // block.
1326 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001327 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001328 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001329 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001330 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001331
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001332 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001333 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001334
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001335 if (Stmt* Else = I->getElse()) {
1336 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001337
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001338 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001339 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001340 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001341
1342 // If branch is not a compound statement create implicit scope
1343 // and add destructors.
1344 if (!isa<CompoundStmt>(Else))
1345 addLocalScopeAndDtors(Else);
1346
Ted Kremenek4f880632009-07-17 22:18:43 +00001347 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001348
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001349 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1350 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001351 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001352 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001353 return 0;
1354 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001355 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001356
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001357 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001358 CFGBlock* ThenBlock;
1359 {
1360 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001361 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001362 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001363 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001364
1365 // If branch is not a compound statement create implicit scope
1366 // and add destructors.
1367 if (!isa<CompoundStmt>(Then))
1368 addLocalScopeAndDtors(Then);
1369
Ted Kremenek4f880632009-07-17 22:18:43 +00001370 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001371
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001372 if (!ThenBlock) {
1373 // We can reach here if the "then" body has all NullStmts.
1374 // Create an empty block so we can distinguish between true and false
1375 // branches in path-sensitive analyses.
1376 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001377 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001378 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001379 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001380 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001381 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001382 }
1383
Mike Stump6d9828c2009-07-17 01:31:16 +00001384 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001385 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001386
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001387 // Set the terminator of the new block to the If statement.
1388 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
Mike Stump00998a02009-07-23 23:25:26 +00001390 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001391 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001392
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001394 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1395 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001396
1397 // Add the condition as the last statement in the new block. This may create
1398 // new blocks as the condition may contain control-flow. Any newly created
1399 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001400 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001401
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001402 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1403 // and the condition variable initialization to the CFG.
1404 if (VarDecl *VD = I->getConditionVariable()) {
1405 if (Expr *Init = VD->getInit()) {
1406 autoCreateBlock();
1407 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1408 addStmt(Init);
1409 }
1410 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001411
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001412 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001413}
Mike Stump6d9828c2009-07-17 01:31:16 +00001414
1415
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001416CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001417 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001418 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001419 // NOTE: If a "return" appears in the middle of a block, this means that the
1420 // code afterwards is DEAD (unreachable). We still keep a basic block
1421 // for that code; a simple "mark-and-sweep" from the entry block will be
1422 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001423
1424 // Create the new block.
1425 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001426
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001427 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001428 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001429 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001430
1431 // Add the return statement to the block. This may create new blocks if R
1432 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001433 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001434}
1435
1436CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1437 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001438 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001439 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001440
Ted Kremenek4f880632009-07-17 22:18:43 +00001441 if (!LabelBlock) // This can happen when the body is empty, i.e.
1442 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001443
Ted Kremenek4f880632009-07-17 22:18:43 +00001444 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001445 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001446
1447 // Labels partition blocks, so this is the end of the basic block we were
1448 // processing (L is the block's label). Because this is label (and we have
1449 // already processed the substatement) there is no extra control-flow to worry
1450 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001451 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001452 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001453 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001454
1455 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001456 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001457
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001458 // This block is now the implicit successor of other blocks.
1459 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001461 return LabelBlock;
1462}
1463
1464CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001465 // Goto is a control-flow statement. Thus we stop processing the current
1466 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001467
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001468 Block = createBlock(false);
1469 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001470
1471 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001473
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001474 if (I == LabelMap.end())
1475 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001476 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1477 else {
1478 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001479 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001480 AddSuccessor(Block, JT.Block);
1481 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482
Mike Stump6d9828c2009-07-17 01:31:16 +00001483 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001484}
1485
1486CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001487 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001488
Marcin Swiderski47575f12010-10-01 01:38:14 +00001489 // Save local scope position because in case of condition variable ScopePos
1490 // won't be restored when traversing AST.
1491 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1492
1493 // Create local scope for init statement and possible condition variable.
1494 // Add destructor for init statement and condition variable.
1495 // Store scope position for continue statement.
1496 if (Stmt* Init = F->getInit())
1497 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001498 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1499
Marcin Swiderski47575f12010-10-01 01:38:14 +00001500 if (VarDecl* VD = F->getConditionVariable())
1501 addLocalScopeForVarDecl(VD);
1502 LocalScope::const_iterator ContinueScopePos = ScopePos;
1503
1504 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1505
Mike Stumpfefb9f72009-07-21 01:12:51 +00001506 // "for" is a control-flow statement. Thus we stop processing the current
1507 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001508 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001509 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001510 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001511 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001512 } else
1513 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001514
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001515 // Save the current value for the break targets.
1516 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001517 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001518 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001519
Mike Stump6d9828c2009-07-17 01:31:16 +00001520 // Because of short-circuit evaluation, the condition of the loop can span
1521 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1522 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001523 CFGBlock* ExitConditionBlock = createBlock(false);
1524 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001525
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001526 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001527 ExitConditionBlock->setTerminator(F);
1528
1529 // Now add the actual condition to the condition block. Because the condition
1530 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001531 if (Stmt* C = F->getCond()) {
1532 Block = ExitConditionBlock;
1533 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001534 assert(Block == EntryConditionBlock ||
1535 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001536
1537 // If this block contains a condition variable, add both the condition
1538 // variable and initializer to the CFG.
1539 if (VarDecl *VD = F->getConditionVariable()) {
1540 if (Expr *Init = VD->getInit()) {
1541 autoCreateBlock();
1542 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1543 EntryConditionBlock = addStmt(Init);
1544 assert(Block == EntryConditionBlock);
1545 }
1546 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001547
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001548 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001549 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001550 return 0;
1551 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001552 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001553
Mike Stump6d9828c2009-07-17 01:31:16 +00001554 // The condition block is the implicit successor for the loop body as well as
1555 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001556 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001557
Mike Stump00998a02009-07-23 23:25:26 +00001558 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001559 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Mike Stump00998a02009-07-23 23:25:26 +00001561 if (F->getCond())
1562 KnownVal = TryEvaluateBool(F->getCond());
1563
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001564 // Now create the loop body.
1565 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001566 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001568 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001569 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1570 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
Ted Kremenekaf603f72007-08-30 18:39:40 +00001572 // Create a new block to contain the (bottom) of the loop body.
1573 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001574
1575 // Loop body should end with destructor of Condition variable (if any).
1576 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001577
Ted Kremeneke9334502008-09-04 21:48:47 +00001578 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001579 // Generate increment code in its own basic block. This is the target of
1580 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001581 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001582 } else {
1583 // No increment code. Create a special, empty, block that is used as the
1584 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001585 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001586 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001587 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001588
Ted Kremenek3575f842009-04-28 00:51:56 +00001589 // Finish up the increment (or empty) block if it hasn't been already.
1590 if (Block) {
1591 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001592 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001593 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001594 Block = 0;
1595 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001596
Marcin Swiderski47575f12010-10-01 01:38:14 +00001597 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001598
Ted Kremenek3575f842009-04-28 00:51:56 +00001599 // The starting block for the loop increment is the block that should
1600 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001601 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001602
Marcin Swiderski47575f12010-10-01 01:38:14 +00001603 // If body is not a compound statement create implicit scope
1604 // and add destructors.
1605 if (!isa<CompoundStmt>(F->getBody()))
1606 addLocalScopeAndDtors(F->getBody());
1607
Mike Stump6d9828c2009-07-17 01:31:16 +00001608 // Now populate the body block, and in the process create new blocks as we
1609 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001610 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001611
1612 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001613 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001614 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001615 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001616
Ted Kremenek941fde82009-07-24 04:47:11 +00001617 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001618 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001619 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
Ted Kremenek941fde82009-07-24 04:47:11 +00001621 // Link up the condition block with the code that follows the loop. (the
1622 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001623 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001624
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001625 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001626 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001627 if (Stmt* I = F->getInit()) {
1628 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001629 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001630 } else {
1631 // There is no loop initialization. We are thus basically a while loop.
1632 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001633 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001634 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001635 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001636 }
1637}
1638
Ted Kremenek115c1b92010-04-11 17:02:10 +00001639CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1640 if (asc.alwaysAdd()) {
1641 autoCreateBlock();
1642 AppendStmt(Block, M, asc);
1643 }
1644 return Visit(M->getBase(),
1645 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1646 : AddStmtChoice::AsLValueNotAlwaysAdd);
1647}
1648
Ted Kremenek514de5a2008-11-11 17:10:00 +00001649CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1650 // Objective-C fast enumeration 'for' statements:
1651 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1652 //
1653 // for ( Type newVariable in collection_expression ) { statements }
1654 //
1655 // becomes:
1656 //
1657 // prologue:
1658 // 1. collection_expression
1659 // T. jump to loop_entry
1660 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001661 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001662 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1663 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1664 // TB:
1665 // statements
1666 // T. jump to loop_entry
1667 // FB:
1668 // what comes after
1669 //
1670 // and
1671 //
1672 // Type existingItem;
1673 // for ( existingItem in expression ) { statements }
1674 //
1675 // becomes:
1676 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001677 // the same with newVariable replaced with existingItem; the binding works
1678 // the same except that for one ObjCForCollectionStmt::getElement() returns
1679 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001680 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001681
Ted Kremenek514de5a2008-11-11 17:10:00 +00001682 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001683
Ted Kremenek514de5a2008-11-11 17:10:00 +00001684 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001685 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001686 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001687 LoopSuccessor = Block;
1688 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001689 } else
1690 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001691
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001692 // Build the condition blocks.
1693 CFGBlock* ExitConditionBlock = createBlock(false);
1694 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001695
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001696 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001697 ExitConditionBlock->setTerminator(S);
1698
1699 // The last statement in the block should be the ObjCForCollectionStmt, which
1700 // performs the actual binding to 'element' and determines if there are any
1701 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001702 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001703 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001704
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001705 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001706 // generate new blocks as necesary. We DON'T add the statement by default to
1707 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001708 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001709 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001710 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001711 return 0;
1712 Block = 0;
1713 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001714
1715 // The condition block is the implicit successor for the loop body as well as
1716 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001717 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
Ted Kremenek514de5a2008-11-11 17:10:00 +00001719 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001720 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001721 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001722 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1723 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1724 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001725
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001726 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1727 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenek4f880632009-07-17 22:18:43 +00001729 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001730
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001731 if (!BodyBlock)
1732 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001733 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001734 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001735 return 0;
1736 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001737
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001738 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001739 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001740 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001742 // Link up the condition block with the code that follows the loop.
1743 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001744 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001745
Ted Kremenek514de5a2008-11-11 17:10:00 +00001746 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001747 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001748 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001749}
1750
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001751CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1752 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001753
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001754 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001755 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001756
Ted Kremenekda5348e2009-05-05 23:11:51 +00001757 // The sync body starts its own basic block. This makes it a little easier
1758 // for diagnostic clients.
1759 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001760 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001761 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001762
Ted Kremenekda5348e2009-05-05 23:11:51 +00001763 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001764 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001765 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001766
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001767 // Add the @synchronized to the CFG.
1768 autoCreateBlock();
1769 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1770
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001771 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001772 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001773}
Mike Stump6d9828c2009-07-17 01:31:16 +00001774
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001775CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001776 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001777 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001778}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001779
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001780CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001781 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001782
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001783 // Save local scope position because in case of condition variable ScopePos
1784 // won't be restored when traversing AST.
1785 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1786
1787 // Create local scope for possible condition variable.
1788 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001789 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001790 if (VarDecl* VD = W->getConditionVariable()) {
1791 addLocalScopeForVarDecl(VD);
1792 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1793 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001794
Mike Stumpfefb9f72009-07-21 01:12:51 +00001795 // "while" is a control-flow statement. Thus we stop processing the current
1796 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001797 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001798 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001799 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001800 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001801 } else
1802 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001803
1804 // Because of short-circuit evaluation, the condition of the loop can span
1805 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1806 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001807 CFGBlock* ExitConditionBlock = createBlock(false);
1808 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001809
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001810 // Set the terminator for the "exit" condition block.
1811 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
1813 // Now add the actual condition to the condition block. Because the condition
1814 // itself may contain control-flow, new blocks may be created. Thus we update
1815 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001816 if (Stmt* C = W->getCond()) {
1817 Block = ExitConditionBlock;
1818 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001819 // The condition might finish the current 'Block'.
1820 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001821
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001822 // If this block contains a condition variable, add both the condition
1823 // variable and initializer to the CFG.
1824 if (VarDecl *VD = W->getConditionVariable()) {
1825 if (Expr *Init = VD->getInit()) {
1826 autoCreateBlock();
1827 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1828 EntryConditionBlock = addStmt(Init);
1829 assert(Block == EntryConditionBlock);
1830 }
1831 }
1832
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001833 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001834 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001835 return 0;
1836 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001837 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001838
1839 // The condition block is the implicit successor for the loop body as well as
1840 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001841 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001842
Mike Stump00998a02009-07-23 23:25:26 +00001843 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001844 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001845
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001846 // Process the loop body.
1847 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001848 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001849
1850 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001851 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1852 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1853 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001854
Mike Stump6d9828c2009-07-17 01:31:16 +00001855 // Create an empty block to represent the transition block for looping back
1856 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001857 Block = 0;
1858 assert(Succ == EntryConditionBlock);
1859 Succ = createBlock();
1860 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001861 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001862
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001863 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001864 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001865
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001866 // NULL out Block to force lazy instantiation of blocks for the body.
1867 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001869 // Loop body should end with destructor of Condition variable (if any).
1870 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1871
1872 // If body is not a compound statement create implicit scope
1873 // and add destructors.
1874 if (!isa<CompoundStmt>(W->getBody()))
1875 addLocalScopeAndDtors(W->getBody());
1876
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001877 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001878 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001879
Ted Kremenekaf603f72007-08-30 18:39:40 +00001880 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001881 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001882 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001883 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001884 return 0;
1885 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001886
Ted Kremenek941fde82009-07-24 04:47:11 +00001887 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001888 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001889 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001890
Ted Kremenek941fde82009-07-24 04:47:11 +00001891 // Link up the condition block with the code that follows the loop. (the
1892 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001893 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001894
1895 // There can be no more statements in the condition block since we loop back
1896 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001897 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001898
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001899 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001900 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001901 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001902}
Mike Stump1eb44332009-09-09 15:08:12 +00001903
1904
Ted Kremenek4f880632009-07-17 22:18:43 +00001905CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1906 // FIXME: For now we pretend that @catch and the code it contains does not
1907 // exit.
1908 return Block;
1909}
Mike Stump6d9828c2009-07-17 01:31:16 +00001910
Ted Kremenek2fda5042008-12-09 20:20:09 +00001911CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1912 // FIXME: This isn't complete. We basically treat @throw like a return
1913 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001914
Ted Kremenek6c249722009-09-24 18:45:41 +00001915 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001916 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001917 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001918
Ted Kremenek2fda5042008-12-09 20:20:09 +00001919 // Create the new block.
1920 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
Ted Kremenek2fda5042008-12-09 20:20:09 +00001922 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001923 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001924
1925 // Add the statement to the block. This may create new blocks if S contains
1926 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001927 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001928}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001929
Mike Stump0979d802009-07-22 22:56:04 +00001930CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001931 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001932 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001933 return 0;
1934
1935 // Create the new block.
1936 Block = createBlock(false);
1937
Mike Stump5d1d2022010-01-19 02:20:09 +00001938 if (TryTerminatedBlock)
1939 // The current try statement is the only successor.
1940 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001941 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001942 // otherwise the Exit block is the only successor.
1943 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001944
1945 // Add the statement to the block. This may create new blocks if S contains
1946 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001947 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001948}
1949
Ted Kremenek4f880632009-07-17 22:18:43 +00001950CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001951 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001952
Mike Stump8f9893a2009-07-21 01:27:50 +00001953 // "do...while" is a control-flow statement. Thus we stop processing the
1954 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001955 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001956 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001957 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001958 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001959 } else
1960 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
1962 // Because of short-circuit evaluation, the condition of the loop can span
1963 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1964 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001965 CFGBlock* ExitConditionBlock = createBlock(false);
1966 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001967
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001968 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001969 ExitConditionBlock->setTerminator(D);
1970
1971 // Now add the actual condition to the condition block. Because the condition
1972 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001973 if (Stmt* C = D->getCond()) {
1974 Block = ExitConditionBlock;
1975 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001976 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001977 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001978 return 0;
1979 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001980 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001981
Ted Kremenek54827132008-02-27 07:20:00 +00001982 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001983 Succ = EntryConditionBlock;
1984
Mike Stump00998a02009-07-23 23:25:26 +00001985 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001986 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001987
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001988 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001989 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001990 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001991 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001993 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001994 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1995 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1996 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001997
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001998 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001999 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002001 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002002 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002003
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002004 // NULL out Block to force lazy instantiation of blocks for the body.
2005 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002007 // If body is not a compound statement create implicit scope
2008 // and add destructors.
2009 if (!isa<CompoundStmt>(D->getBody()))
2010 addLocalScopeAndDtors(D->getBody());
2011
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002012 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002013 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002014
Ted Kremenekaf603f72007-08-30 18:39:40 +00002015 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002016 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002017 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002018 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002019 return 0;
2020 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002021
Ted Kremenekd173dc72010-08-17 20:59:56 +00002022 if (!KnownVal.isFalse()) {
2023 // Add an intermediate block between the BodyBlock and the
2024 // ExitConditionBlock to represent the "loop back" transition. Create an
2025 // empty block to represent the transition block for looping back to the
2026 // head of the loop.
2027 // FIXME: Can we do this more efficiently without adding another block?
2028 Block = NULL;
2029 Succ = BodyBlock;
2030 CFGBlock *LoopBackBlock = createBlock();
2031 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002032
Ted Kremenekd173dc72010-08-17 20:59:56 +00002033 // Add the loop body entry as a successor to the condition.
2034 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2035 }
2036 else
2037 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002038 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002039
Ted Kremenek941fde82009-07-24 04:47:11 +00002040 // Link up the condition block with the code that follows the loop.
2041 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002042 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002043
2044 // There can be no more statements in the body block(s) since we loop back to
2045 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002046 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002047
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002048 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002049 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002050 return BodyBlock;
2051}
2052
2053CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2054 // "continue" is a control-flow statement. Thus we stop processing the
2055 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002056 if (badCFG)
2057 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002059 // Now create a new block that ends with the continue statement.
2060 Block = createBlock(false);
2061 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002062
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002063 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002064 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002065 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00002066 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002067 AddSuccessor(Block, ContinueJumpTarget.Block);
2068 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002069 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002070
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002071 return Block;
2072}
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002074CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002075 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002076
Ted Kremenek852274d2009-12-16 03:18:58 +00002077 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002078 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002079 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002080 }
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Ted Kremenek4f880632009-07-17 22:18:43 +00002082 // VLA types have expressions that must be evaluated.
2083 if (E->isArgumentType()) {
2084 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2085 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2086 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002087 }
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Mike Stump6d9828c2009-07-17 01:31:16 +00002089 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002090}
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Ted Kremenek4f880632009-07-17 22:18:43 +00002092/// VisitStmtExpr - Utility method to handle (nested) statement
2093/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002094CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2095 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002096 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002097 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002098 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002099 return VisitCompoundStmt(SE->getSubStmt());
2100}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002101
Ted Kremenek411cdee2008-04-16 21:10:48 +00002102CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002103 // "switch" is a control-flow statement. Thus we stop processing the current
2104 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002105 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002106
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002107 // Save local scope position because in case of condition variable ScopePos
2108 // won't be restored when traversing AST.
2109 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2110
2111 // Create local scope for possible condition variable.
2112 // Store scope position. Add implicit destructor.
2113 if (VarDecl* VD = Terminator->getConditionVariable()) {
2114 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2115 addLocalScopeForVarDecl(VD);
2116 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2117 }
2118
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002119 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002120 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002121 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002122 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002123 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002124
2125 // Save the current "switch" context.
2126 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002127 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002128 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002129
Mike Stump6d9828c2009-07-17 01:31:16 +00002130 // Set the "default" case to be the block after the switch statement. If the
2131 // switch statement contains a "default:", this value will be overwritten with
2132 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002133 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002134
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002135 // Create a new block that will contain the switch statement.
2136 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002137
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002138 // Now process the switch body. The code after the switch is the implicit
2139 // successor.
2140 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002141 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002142
2143 // When visiting the body, the case statements should automatically get linked
2144 // up to the switch. We also don't keep a pointer to the body, since all
2145 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002146 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002147 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002148
2149 // If body is not a compound statement create implicit scope
2150 // and add destructors.
2151 if (!isa<CompoundStmt>(Terminator->getBody()))
2152 addLocalScopeAndDtors(Terminator->getBody());
2153
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002154 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002155 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002156 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002157 return 0;
2158 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002159
Mike Stump6d9828c2009-07-17 01:31:16 +00002160 // If we have no "default:" case, the default transition is to the code
2161 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002162 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002163
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002164 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002165 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002166 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002167 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002168 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002169
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002170 // Finally, if the SwitchStmt contains a condition variable, add both the
2171 // SwitchStmt and the condition variable initialization to the CFG.
2172 if (VarDecl *VD = Terminator->getConditionVariable()) {
2173 if (Expr *Init = VD->getInit()) {
2174 autoCreateBlock();
2175 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2176 addStmt(Init);
2177 }
2178 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002179
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002180 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002181}
2182
Ted Kremenek4f880632009-07-17 22:18:43 +00002183CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002184 // CaseStmts are essentially labels, so they are the first statement in a
2185 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002186 CFGBlock *TopBlock = 0, *LastBlock = 0;
2187
2188 if (Stmt *Sub = CS->getSubStmt()) {
2189 // For deeply nested chains of CaseStmts, instead of doing a recursion
2190 // (which can blow out the stack), manually unroll and create blocks
2191 // along the way.
2192 while (isa<CaseStmt>(Sub)) {
2193 CFGBlock *CurrentBlock = createBlock(false);
2194 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002195
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002196 if (TopBlock)
2197 AddSuccessor(LastBlock, CurrentBlock);
2198 else
2199 TopBlock = CurrentBlock;
2200
2201 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2202 LastBlock = CurrentBlock;
2203
2204 CS = cast<CaseStmt>(Sub);
2205 Sub = CS->getSubStmt();
2206 }
2207
2208 addStmt(Sub);
2209 }
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002211 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002212 if (!CaseBlock)
2213 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002214
2215 // Cases statements partition blocks, so this is the top of the basic block we
2216 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002217 CaseBlock->setLabel(CS);
2218
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002219 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002220 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002221
2222 // Add this block to the list of successors for the block with the switch
2223 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002224 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002225 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002226
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002227 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2228 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002229
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002230 if (TopBlock) {
2231 AddSuccessor(LastBlock, CaseBlock);
2232 Succ = TopBlock;
2233 }
2234 else {
2235 // This block is now the implicit successor of other blocks.
2236 Succ = CaseBlock;
2237 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002238
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002239 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002240}
Mike Stump6d9828c2009-07-17 01:31:16 +00002241
Ted Kremenek411cdee2008-04-16 21:10:48 +00002242CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002243 if (Terminator->getSubStmt())
2244 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002246 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002247
2248 if (!DefaultCaseBlock)
2249 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002250
2251 // Default statements partition blocks, so this is the top of the basic block
2252 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002253 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002255 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002256 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002257
Mike Stump6d9828c2009-07-17 01:31:16 +00002258 // Unlike case statements, we don't add the default block to the successors
2259 // for the switch statement immediately. This is done when we finish
2260 // processing the switch statement. This allows for the default case
2261 // (including a fall-through to the code after the switch statement) to always
2262 // be the last successor of a switch-terminated block.
2263
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002264 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2265 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002266
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002267 // This block is now the implicit successor of other blocks.
2268 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002269
2270 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002271}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002272
Mike Stump5d1d2022010-01-19 02:20:09 +00002273CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2274 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2275 // current block.
2276 CFGBlock* TrySuccessor = NULL;
2277
2278 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002279 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002280 return 0;
2281 TrySuccessor = Block;
2282 } else TrySuccessor = Succ;
2283
Mike Stumpa1f93632010-01-20 01:15:34 +00002284 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002285
2286 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002287 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002288 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002289 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002290
Mike Stumpa1f93632010-01-20 01:15:34 +00002291 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002292 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2293 // The code after the try is the implicit successor.
2294 Succ = TrySuccessor;
2295 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002296 if (CS->getExceptionDecl() == 0) {
2297 HasCatchAll = true;
2298 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002299 Block = NULL;
2300 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2301 if (CatchBlock == 0)
2302 return 0;
2303 // Add this block to the list of successors for the block with the try
2304 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002305 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002306 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002307 if (!HasCatchAll) {
2308 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002309 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002310 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002311 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002312 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002313
2314 // The code after the try is the implicit successor.
2315 Succ = TrySuccessor;
2316
Mike Stumpf00cca52010-01-20 01:30:58 +00002317 // Save the current "try" context.
2318 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2319 TryTerminatedBlock = NewTryTerminatedBlock;
2320
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002321 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002322 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002323 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002324 return Block;
2325}
2326
2327CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2328 // CXXCatchStmt are treated like labels, so they are the first statement in a
2329 // block.
2330
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002331 // Save local scope position because in case of exception variable ScopePos
2332 // won't be restored when traversing AST.
2333 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2334
2335 // Create local scope for possible exception variable.
2336 // Store scope position. Add implicit destructor.
2337 if (VarDecl* VD = CS->getExceptionDecl()) {
2338 LocalScope::const_iterator BeginScopePos = ScopePos;
2339 addLocalScopeForVarDecl(VD);
2340 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2341 }
2342
Mike Stump5d1d2022010-01-19 02:20:09 +00002343 if (CS->getHandlerBlock())
2344 addStmt(CS->getHandlerBlock());
2345
2346 CFGBlock* CatchBlock = Block;
2347 if (!CatchBlock)
2348 CatchBlock = createBlock();
2349
2350 CatchBlock->setLabel(CS);
2351
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002352 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002353 return 0;
2354
2355 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2356 Block = NULL;
2357
2358 return CatchBlock;
2359}
2360
Marcin Swiderski8599e762010-11-03 06:19:35 +00002361CFGBlock *CFGBuilder::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
2362 AddStmtChoice asc) {
2363 if (BuildOpts.AddImplicitDtors) {
2364 // If adding implicit destructors visit the full expression for adding
2365 // destructors of temporaries.
2366 VisitForTemporaryDtors(E->getSubExpr());
2367
2368 // Full expression has to be added as CFGStmt so it will be sequenced
2369 // before destructors of it's temporaries.
2370 asc = asc.asLValue()
2371 ? AddStmtChoice::AlwaysAddAsLValue
2372 : AddStmtChoice::AlwaysAdd;
2373 }
2374 return Visit(E->getSubExpr(), asc);
2375}
2376
Zhongxing Xua725ed42010-11-01 13:04:58 +00002377CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2378 AddStmtChoice asc) {
2379 if (asc.alwaysAdd()) {
2380 autoCreateBlock();
2381 AppendStmt(Block, E, asc);
2382
2383 // We do not want to propagate the AlwaysAdd property.
2384 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2385 : AddStmtChoice::NotAlwaysAdd);
2386 }
2387 return Visit(E->getSubExpr(), asc);
2388}
2389
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002390CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2391 AddStmtChoice asc) {
2392 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2393 : AddStmtChoice::AlwaysAdd;
2394 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002395 if (!C->isElidable())
2396 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002397 return VisitChildren(C);
2398}
2399
Zhongxing Xua725ed42010-11-01 13:04:58 +00002400CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2401 AddStmtChoice asc) {
2402 if (asc.alwaysAdd()) {
2403 autoCreateBlock();
2404 AppendStmt(Block, E, asc);
2405 // We do not want to propagate the AlwaysAdd property.
2406 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2407 : AddStmtChoice::NotAlwaysAdd);
2408 }
2409 return Visit(E->getSubExpr(), asc);
2410}
2411
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002412CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2413 AddStmtChoice asc) {
2414 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2415 : AddStmtChoice::AlwaysAdd;
2416 autoCreateBlock();
2417 AppendStmt(Block, C, AddStmtChoice(K));
2418 return VisitChildren(C);
2419}
2420
Ted Kremenekad5a8942010-08-02 23:46:59 +00002421CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002422 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002423 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002424 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002425 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002426 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002427 return VisitChildren(C);
2428}
2429
Zhongxing Xua725ed42010-11-01 13:04:58 +00002430CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2431 AddStmtChoice asc) {
2432 if (asc.alwaysAdd()) {
2433 autoCreateBlock();
2434 AppendStmt(Block, E, asc);
2435 // We do not want to propagate the AlwaysAdd property.
2436 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2437 : AddStmtChoice::NotAlwaysAdd);
2438 }
2439 return Visit(E->getSubExpr(), asc);
2440}
2441
Ted Kremenek19bb3562007-08-28 19:26:49 +00002442CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002443 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002444 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002445
Ted Kremenek19bb3562007-08-28 19:26:49 +00002446 if (!IBlock) {
2447 IBlock = createBlock(false);
2448 cfg->setIndirectGotoBlock(IBlock);
2449 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002450
Ted Kremenek19bb3562007-08-28 19:26:49 +00002451 // IndirectGoto is a control-flow statement. Thus we stop processing the
2452 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002453 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002454 return 0;
2455
Ted Kremenek19bb3562007-08-28 19:26:49 +00002456 Block = createBlock(false);
2457 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002458 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002459 return addStmt(I->getTarget());
2460}
2461
Marcin Swiderski8599e762010-11-03 06:19:35 +00002462CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2463tryAgain:
2464 if (!E) {
2465 badCFG = true;
2466 return NULL;
2467 }
2468 switch (E->getStmtClass()) {
2469 default:
2470 return VisitChildrenForTemporaryDtors(E);
2471
2472 case Stmt::BinaryOperatorClass:
2473 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2474
2475 case Stmt::CXXBindTemporaryExprClass:
2476 return VisitCXXBindTemporaryExprForTemporaryDtors(
2477 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2478
2479 case Stmt::ConditionalOperatorClass:
2480 return VisitConditionalOperatorForTemporaryDtors(
2481 cast<ConditionalOperator>(E), BindToTemporary);
2482
2483 case Stmt::ImplicitCastExprClass:
2484 // For implicit cast we want BindToTemporary to be passed further.
2485 E = cast<CastExpr>(E)->getSubExpr();
2486 goto tryAgain;
2487
2488 case Stmt::ParenExprClass:
2489 E = cast<ParenExpr>(E)->getSubExpr();
2490 goto tryAgain;
2491 }
2492}
2493
2494CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2495 // When visiting children for destructors we want to visit them in reverse
2496 // order. Because there's no reverse iterator for children must to reverse
2497 // them in helper vector.
2498 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2499 ChildrenVect ChildrenRev;
2500 for (Stmt::child_iterator I = E->child_begin(), L = E->child_end();
2501 I != L; ++I) {
2502 if (*I) ChildrenRev.push_back(*I);
2503 }
2504
2505 CFGBlock *B = Block;
2506 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2507 L = ChildrenRev.rend(); I != L; ++I) {
2508 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2509 B = R;
2510 }
2511 return B;
2512}
2513
2514CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2515 if (E->isLogicalOp()) {
2516 // Destructors for temporaries in LHS expression should be called after
2517 // those for RHS expression. Even if this will unnecessarily create a block,
2518 // this block will be used at least by the full expression.
2519 autoCreateBlock();
2520 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2521 if (badCFG)
2522 return NULL;
2523
2524 Succ = ConfluenceBlock;
2525 Block = NULL;
2526 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2527
2528 if (RHSBlock) {
2529 if (badCFG)
2530 return NULL;
2531
2532 // If RHS expression did produce destructors we need to connect created
2533 // blocks to CFG in same manner as for binary operator itself.
2534 CFGBlock *LHSBlock = createBlock(false);
2535 LHSBlock->setTerminator(CFGTerminator(E, true));
2536
2537 // For binary operator LHS block is before RHS in list of predecessors
2538 // of ConfluenceBlock.
2539 std::reverse(ConfluenceBlock->pred_begin(),
2540 ConfluenceBlock->pred_end());
2541
2542 // See if this is a known constant.
2543 TryResult KnownVal = TryEvaluateBool(E->getLHS());
2544 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2545 KnownVal.negate();
2546
2547 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2548 // itself.
2549 if (E->getOpcode() == BO_LOr) {
2550 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2551 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2552 } else {
2553 assert (E->getOpcode() == BO_LAnd);
2554 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2555 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2556 }
2557
2558 Block = LHSBlock;
2559 return LHSBlock;
2560 }
2561
2562 Block = ConfluenceBlock;
2563 return ConfluenceBlock;
2564 }
2565
2566 else if (E->isAssignmentOp()) {
2567 // For assignment operator (=) LHS expression is visited
2568 // before RHS expression. For destructors visit them in reverse order.
2569 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2570 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2571 return LHSBlock ? LHSBlock : RHSBlock;
2572 }
2573
2574 // For any other binary operator RHS expression is visited before
2575 // LHS expression (order of children). For destructors visit them in reverse
2576 // order.
2577 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2578 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2579 return RHSBlock ? RHSBlock : LHSBlock;
2580}
2581
2582CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2583 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2584 // First add destructors for temporaries in subexpression.
2585 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
2586 if (!BindToTemporary) {
2587 // If lifetime of temporary is not prolonged (by assigning to constant
2588 // reference) add destructor for it.
2589 autoCreateBlock();
2590 appendTemporaryDtor(Block, E);
2591 B = Block;
2592 }
2593 return B;
2594}
2595
2596CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2597 ConditionalOperator *E, bool BindToTemporary) {
2598 // First add destructors for condition expression. Even if this will
2599 // unnecessarily create a block, this block will be used at least by the full
2600 // expression.
2601 autoCreateBlock();
2602 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2603 if (badCFG)
2604 return NULL;
2605
2606 // Try to add block with destructors for LHS expression.
2607 CFGBlock *LHSBlock = NULL;
2608 if (E->getLHS()) {
2609 Succ = ConfluenceBlock;
2610 Block = NULL;
2611 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2612 if (badCFG)
2613 return NULL;
2614 }
2615
2616 // Try to add block with destructors for RHS expression;
2617 Succ = ConfluenceBlock;
2618 Block = NULL;
2619 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2620 if (badCFG)
2621 return NULL;
2622
2623 if (!RHSBlock && !LHSBlock) {
2624 // If neither LHS nor RHS expression had temporaries to destroy don't create
2625 // more blocks.
2626 Block = ConfluenceBlock;
2627 return Block;
2628 }
2629
2630 Block = createBlock(false);
2631 Block->setTerminator(CFGTerminator(E, true));
2632
2633 // See if this is a known constant.
2634 const TryResult &KnownVal = TryEvaluateBool(E->getCond());
2635
2636 if (LHSBlock) {
2637 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
2638 } else if (KnownVal.isFalse()) {
2639 AddSuccessor(Block, NULL);
2640 } else {
2641 AddSuccessor(Block, ConfluenceBlock);
2642 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2643 }
2644
2645 if (!RHSBlock)
2646 RHSBlock = ConfluenceBlock;
2647 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
2648
2649 return Block;
2650}
2651
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002652} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002653
Mike Stump6d9828c2009-07-17 01:31:16 +00002654/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2655/// no successors or predecessors. If this is the first block created in the
2656/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002657CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002658 bool first_block = begin() == end();
2659
2660 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002661 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2662 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2663 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002664
2665 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002666 if (first_block)
2667 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002668
2669 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002670 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002671}
2672
Ted Kremenek026473c2007-08-23 16:51:22 +00002673/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2674/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002675CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002676 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002677 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002678 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002679}
2680
Ted Kremenek63f58872007-10-01 19:33:33 +00002681//===----------------------------------------------------------------------===//
2682// CFG: Queries for BlkExprs.
2683//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002684
Ted Kremenek63f58872007-10-01 19:33:33 +00002685namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002686 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002687}
2688
Ted Kremenek8a693662009-12-23 23:37:10 +00002689static void FindSubExprAssignments(Stmt *S,
2690 llvm::SmallPtrSet<Expr*,50>& Set) {
2691 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002692 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002693
Ted Kremenek8a693662009-12-23 23:37:10 +00002694 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002695 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002696 if (!child)
2697 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002698
Ted Kremenek8a693662009-12-23 23:37:10 +00002699 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002700 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002701
Ted Kremenek8a693662009-12-23 23:37:10 +00002702 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002703 }
2704}
2705
Ted Kremenek63f58872007-10-01 19:33:33 +00002706static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2707 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002708
2709 // Look for assignments that are used as subexpressions. These are the only
2710 // assignments that we want to *possibly* register as a block-level
2711 // expression. Basically, if an assignment occurs both in a subexpression and
2712 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002713 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002714
Ted Kremenek63f58872007-10-01 19:33:33 +00002715 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002716 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002717 if (CFGStmt S = BI->getAs<CFGStmt>())
2718 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002719
Ted Kremenek411cdee2008-04-16 21:10:48 +00002720 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002721
2722 // Iterate over the statements again on identify the Expr* and Stmt* at the
2723 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002724
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002725 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2726 CFGStmt CS = BI->getAs<CFGStmt>();
2727 if (!CS.isValid())
2728 continue;
2729 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002730
Ted Kremenek411cdee2008-04-16 21:10:48 +00002731 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002732 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002733 // expression are really "statements" whose value is never used by
2734 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002735 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002736 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002737 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2738 // Special handling for statement expressions. The last statement in
2739 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002740 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002741 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002742 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002743 (*M)[C->body_back()] = x;
2744 }
2745 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002746
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002747 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002748 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002749 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002750 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002751
Ted Kremenek411cdee2008-04-16 21:10:48 +00002752 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002753
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002754 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002755
Ted Kremenek390e48b2008-11-12 21:11:49 +00002756 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002757 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002758 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002759 }
2760 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002761
Ted Kremenek63f58872007-10-01 19:33:33 +00002762 return M;
2763}
2764
Ted Kremenek86946742008-01-17 20:48:37 +00002765CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2766 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002767 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002768
Ted Kremenek63f58872007-10-01 19:33:33 +00002769 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002770 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002771 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002772}
2773
2774unsigned CFG::getNumBlkExprs() {
2775 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2776 return M->size();
2777 else {
2778 // We assume callers interested in the number of BlkExprs will want
2779 // the map constructed if it doesn't already exist.
2780 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2781 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2782 }
2783}
2784
Ted Kremenek274f4332008-04-28 18:00:46 +00002785//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002786// Filtered walking of the CFG.
2787//===----------------------------------------------------------------------===//
2788
2789bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002790 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002791
2792 if (F.IgnoreDefaultsWithCoveredEnums) {
2793 // If the 'To' has no label or is labeled but the label isn't a
2794 // CaseStmt then filter this edge.
2795 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002796 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002797 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002798 const Stmt *L = To->getLabel();
2799 if (!L || !isa<CaseStmt>(L))
2800 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002801 }
2802 }
2803 }
2804
2805 return false;
2806}
2807
2808//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002809// Cleanup: CFG dstor.
2810//===----------------------------------------------------------------------===//
2811
Ted Kremenek63f58872007-10-01 19:33:33 +00002812CFG::~CFG() {
2813 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2814}
Mike Stump6d9828c2009-07-17 01:31:16 +00002815
Ted Kremenek7dba8602007-08-29 21:56:09 +00002816//===----------------------------------------------------------------------===//
2817// CFG pretty printing
2818//===----------------------------------------------------------------------===//
2819
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002820namespace {
2821
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002822class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002823 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002824 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002825 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002826 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002827 signed CurrentBlock;
2828 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002829 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002830public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002831
Chris Lattnere4f21422009-06-30 01:26:17 +00002832 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2833 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002834 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2835 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002836 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002837 BI != BEnd; ++BI, ++j ) {
2838 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2839 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2840 StmtMap[SE] = P;
2841
2842 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2843 DeclMap[DS->getSingleDecl()] = P;
2844
2845 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2846 if (VarDecl* VD = IS->getConditionVariable())
2847 DeclMap[VD] = P;
2848
2849 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2850 if (VarDecl* VD = FS->getConditionVariable())
2851 DeclMap[VD] = P;
2852
2853 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2854 if (VarDecl* VD = WS->getConditionVariable())
2855 DeclMap[VD] = P;
2856
2857 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2858 if (VarDecl* VD = SS->getConditionVariable())
2859 DeclMap[VD] = P;
2860
2861 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2862 if (VarDecl* VD = CS->getExceptionDecl())
2863 DeclMap[VD] = P;
2864 }
2865 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002866 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002867 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002868 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002869
Ted Kremenek42a509f2007-08-31 21:30:12 +00002870 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002871
Chris Lattnere4f21422009-06-30 01:26:17 +00002872 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002873 void setBlockID(signed i) { CurrentBlock = i; }
2874 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002875
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002876 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2877 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002878
2879 if (I == StmtMap.end())
2880 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002881
2882 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002883 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002884 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002885 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002886
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002887 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002888 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002889 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002890
2891 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2892 DeclMapTy::iterator I = DeclMap.find(D);
2893
2894 if (I == DeclMap.end())
2895 return false;
2896
2897 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2898 && I->second.second == CurrentStmt) {
2899 return false;
2900 }
2901
2902 OS << "[B" << I->second.first << "." << I->second.second << "]";
2903 return true;
2904 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002905};
Chris Lattnere4f21422009-06-30 01:26:17 +00002906} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002907
Chris Lattnere4f21422009-06-30 01:26:17 +00002908
2909namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002910class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002911 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002912
Ted Kremeneka95d3752008-09-13 05:16:45 +00002913 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002914 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002915 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002916public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002917 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002918 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002919 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002920
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002921 void VisitIfStmt(IfStmt* I) {
2922 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002923 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002924 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002925
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002926 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002927 void VisitStmt(Stmt* Terminator) {
2928 Terminator->printPretty(OS, Helper, Policy);
2929 }
2930
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002931 void VisitForStmt(ForStmt* F) {
2932 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002933 if (F->getInit())
2934 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002935 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002936 if (Stmt* C = F->getCond())
2937 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002938 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002939 if (F->getInc())
2940 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002941 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002942 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002943
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002944 void VisitWhileStmt(WhileStmt* W) {
2945 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002946 if (Stmt* C = W->getCond())
2947 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002948 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002949
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002950 void VisitDoStmt(DoStmt* D) {
2951 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002952 if (Stmt* C = D->getCond())
2953 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002954 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002955
Ted Kremenek411cdee2008-04-16 21:10:48 +00002956 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002957 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002958 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002959 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002960
Mike Stump5d1d2022010-01-19 02:20:09 +00002961 void VisitCXXTryStmt(CXXTryStmt* CS) {
2962 OS << "try ...";
2963 }
2964
Ted Kremenek805e9a82007-08-31 21:49:40 +00002965 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002966 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002967 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002968 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002969
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002970 void VisitChooseExpr(ChooseExpr* C) {
2971 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002972 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002973 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002974 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002975
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002976 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2977 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002978 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002979 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002980
Ted Kremenek805e9a82007-08-31 21:49:40 +00002981 void VisitBinaryOperator(BinaryOperator* B) {
2982 if (!B->isLogicalOp()) {
2983 VisitExpr(B);
2984 return;
2985 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002986
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002987 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002988
Ted Kremenek805e9a82007-08-31 21:49:40 +00002989 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002990 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002991 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002992 return;
John McCall2de56d12010-08-25 11:45:40 +00002993 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002994 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002995 return;
2996 default:
2997 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002998 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002999 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003000
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003001 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003002 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003003 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003004};
Chris Lattnere4f21422009-06-30 01:26:17 +00003005} // end anonymous namespace
3006
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003007static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003008 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003009 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3010 Stmt *S = CS;
3011
3012 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003013
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003014 // special printing for statement-expressions.
3015 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3016 CompoundStmt* Sub = SE->getSubStmt();
3017
3018 if (Sub->child_begin() != Sub->child_end()) {
3019 OS << "({ ... ; ";
3020 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3021 OS << " })\n";
3022 return;
3023 }
3024 }
3025 // special printing for comma expressions.
3026 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3027 if (B->getOpcode() == BO_Comma) {
3028 OS << "... , ";
3029 Helper->handledStmt(B->getRHS(),OS);
3030 OS << '\n';
3031 return;
3032 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003033 }
3034 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003035 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003036
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003037 if (isa<CXXOperatorCallExpr>(S)) {
3038 OS << " (OperatorCall)";
Mike Stump6d9828c2009-07-17 01:31:16 +00003039 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003040 else if (isa<CXXBindTemporaryExpr>(S)) {
3041 OS << " (BindTemporary)";
3042 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003043
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003044 // Expressions need a newline.
3045 if (isa<Expr>(S))
3046 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003047
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003048 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3049 CXXBaseOrMemberInitializer* I = IE;
3050 if (I->isBaseInitializer())
3051 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
3052 else OS << I->getMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003053
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003054 OS << "(";
3055 if (Expr* IE = I->getInit())
3056 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3057 OS << ")";
3058
3059 if (I->isBaseInitializer())
3060 OS << " (Base initializer)\n";
3061 else OS << " (Member initializer)\n";
3062
3063 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3064 VarDecl* VD = DE.getVarDecl();
3065 Helper->handleDecl(VD, OS);
3066
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003067 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003068 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3069 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003070 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3071 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003072
3073 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3074 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003075
3076 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3077 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3078 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003079 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003080
3081 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3082 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003083
3084 const Type *T = FD->getType().getTypePtr();
3085 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3086 T = ET;
3087
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003088 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003089 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003090 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003091
3092 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3093 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3094 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3095 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003096 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003097}
Mike Stump6d9828c2009-07-17 01:31:16 +00003098
Chris Lattnere4f21422009-06-30 01:26:17 +00003099static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3100 const CFGBlock& B,
3101 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003102
Ted Kremenek42a509f2007-08-31 21:30:12 +00003103 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003104
Ted Kremenek7dba8602007-08-29 21:56:09 +00003105 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003106 OS << "\n [ B" << B.getBlockID();
3107
Ted Kremenek42a509f2007-08-31 21:30:12 +00003108 if (&B == &cfg->getEntry())
3109 OS << " (ENTRY) ]\n";
3110 else if (&B == &cfg->getExit())
3111 OS << " (EXIT) ]\n";
3112 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003113 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003114 else
3115 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003116
Ted Kremenek9cffe732007-08-29 23:20:49 +00003117 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003118 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003119
3120 if (print_edges)
3121 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003122
Mike Stump079bd722010-01-19 22:00:14 +00003123 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003124 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003125 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003126 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003127 C->getLHS()->printPretty(OS, Helper,
3128 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003129 if (C->getRHS()) {
3130 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003131 C->getRHS()->printPretty(OS, Helper,
3132 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003133 }
Mike Stump079bd722010-01-19 22:00:14 +00003134 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003135 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003136 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003137 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003138 if (CS->getExceptionDecl())
3139 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3140 0);
3141 else
3142 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003143 OS << ")";
3144
3145 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003146 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003147
Ted Kremenek9cffe732007-08-29 23:20:49 +00003148 OS << ":\n";
3149 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003150
Ted Kremenekfddd5182007-08-21 21:42:03 +00003151 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003152 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003153
Ted Kremenek42a509f2007-08-31 21:30:12 +00003154 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3155 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003156
Ted Kremenek9cffe732007-08-29 23:20:49 +00003157 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003158 if (print_edges)
3159 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003160
Ted Kremeneka95d3752008-09-13 05:16:45 +00003161 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003162
Ted Kremenek42a509f2007-08-31 21:30:12 +00003163 if (Helper)
3164 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003165
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003166 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003167 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003168
Ted Kremenek9cffe732007-08-29 23:20:49 +00003169 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003170 if (B.getTerminator()) {
3171 if (print_edges)
3172 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003173
Ted Kremenek9cffe732007-08-29 23:20:49 +00003174 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003175
Ted Kremenek42a509f2007-08-31 21:30:12 +00003176 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003177
Chris Lattnere4f21422009-06-30 01:26:17 +00003178 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3179 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003180 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003181 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003182 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003183
Ted Kremenek9cffe732007-08-29 23:20:49 +00003184 if (print_edges) {
3185 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003186 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003187 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003188
Ted Kremenek42a509f2007-08-31 21:30:12 +00003189 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3190 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003191
Ted Kremenek42a509f2007-08-31 21:30:12 +00003192 if (i == 8 || (i-8) == 0)
3193 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003194
Ted Kremenek9cffe732007-08-29 23:20:49 +00003195 OS << " B" << (*I)->getBlockID();
3196 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003197
Ted Kremenek42a509f2007-08-31 21:30:12 +00003198 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003199
Ted Kremenek42a509f2007-08-31 21:30:12 +00003200 // Print the successors of this block.
3201 OS << " Successors (" << B.succ_size() << "):";
3202 i = 0;
3203
3204 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3205 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003206
Ted Kremenek42a509f2007-08-31 21:30:12 +00003207 if (i == 8 || (i-8) % 10 == 0)
3208 OS << "\n ";
3209
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003210 if (*I)
3211 OS << " B" << (*I)->getBlockID();
3212 else
3213 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003214 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003215
Ted Kremenek9cffe732007-08-29 23:20:49 +00003216 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003217 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003218}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003219
Ted Kremenek42a509f2007-08-31 21:30:12 +00003220
3221/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003222void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003223
3224/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003225void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3226 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003227
Ted Kremenek42a509f2007-08-31 21:30:12 +00003228 // Print the entry block.
3229 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003230
Ted Kremenek42a509f2007-08-31 21:30:12 +00003231 // Iterate through the CFGBlocks and print them one by one.
3232 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3233 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003234 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003235 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003236
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003237 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003238 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003239
Ted Kremenek42a509f2007-08-31 21:30:12 +00003240 // Print the exit block.
3241 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003242 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003243}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003244
3245/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003246void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3247 print(llvm::errs(), cfg, LO);
3248}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003249
3250/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3251/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003252void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3253 const LangOptions &LO) const {
3254 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003255 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003256}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003257
Ted Kremeneka2925852008-01-30 23:02:42 +00003258/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003259void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003260 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003261 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003262 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003263}
3264
Ted Kremenek390e48b2008-11-12 21:11:49 +00003265Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003266 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003267 if (!Terminator)
3268 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003269
Ted Kremenek411cdee2008-04-16 21:10:48 +00003270 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003271
Ted Kremenek411cdee2008-04-16 21:10:48 +00003272 switch (Terminator->getStmtClass()) {
3273 default:
3274 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003275
Ted Kremenek411cdee2008-04-16 21:10:48 +00003276 case Stmt::ForStmtClass:
3277 E = cast<ForStmt>(Terminator)->getCond();
3278 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003279
Ted Kremenek411cdee2008-04-16 21:10:48 +00003280 case Stmt::WhileStmtClass:
3281 E = cast<WhileStmt>(Terminator)->getCond();
3282 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003283
Ted Kremenek411cdee2008-04-16 21:10:48 +00003284 case Stmt::DoStmtClass:
3285 E = cast<DoStmt>(Terminator)->getCond();
3286 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003287
Ted Kremenek411cdee2008-04-16 21:10:48 +00003288 case Stmt::IfStmtClass:
3289 E = cast<IfStmt>(Terminator)->getCond();
3290 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003291
Ted Kremenek411cdee2008-04-16 21:10:48 +00003292 case Stmt::ChooseExprClass:
3293 E = cast<ChooseExpr>(Terminator)->getCond();
3294 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003295
Ted Kremenek411cdee2008-04-16 21:10:48 +00003296 case Stmt::IndirectGotoStmtClass:
3297 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3298 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003299
Ted Kremenek411cdee2008-04-16 21:10:48 +00003300 case Stmt::SwitchStmtClass:
3301 E = cast<SwitchStmt>(Terminator)->getCond();
3302 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003303
Ted Kremenek411cdee2008-04-16 21:10:48 +00003304 case Stmt::ConditionalOperatorClass:
3305 E = cast<ConditionalOperator>(Terminator)->getCond();
3306 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003307
Ted Kremenek411cdee2008-04-16 21:10:48 +00003308 case Stmt::BinaryOperatorClass: // '&&' and '||'
3309 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003310 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003311
Ted Kremenek390e48b2008-11-12 21:11:49 +00003312 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003313 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003314 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003315
Ted Kremenek411cdee2008-04-16 21:10:48 +00003316 return E ? E->IgnoreParens() : NULL;
3317}
3318
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003319bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003320 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003321 if (!Terminator)
3322 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003323
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003324 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003325
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003326 switch (Terminator->getStmtClass()) {
3327 default:
3328 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003329
3330 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003331 case Stmt::WhileStmtClass:
3332 case Stmt::DoStmtClass:
3333 case Stmt::IfStmtClass:
3334 case Stmt::ChooseExprClass:
3335 case Stmt::ConditionalOperatorClass:
3336 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003337 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003338 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003339
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003340 return E ? E->IgnoreParens() : NULL;
3341}
3342
Ted Kremeneka2925852008-01-30 23:02:42 +00003343
Ted Kremenek7dba8602007-08-29 21:56:09 +00003344//===----------------------------------------------------------------------===//
3345// CFG Graphviz Visualization
3346//===----------------------------------------------------------------------===//
3347
Ted Kremenek42a509f2007-08-31 21:30:12 +00003348
3349#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003350static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003351#endif
3352
Chris Lattnere4f21422009-06-30 01:26:17 +00003353void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003354#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003355 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003356 GraphHelper = &H;
3357 llvm::ViewGraph(this,"CFG");
3358 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003359#endif
3360}
3361
Ted Kremenek7dba8602007-08-29 21:56:09 +00003362namespace llvm {
3363template<>
3364struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003365
3366 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3367
3368 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003369
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003370#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003371 std::string OutSStr;
3372 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003373 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003374 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003375
3376 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3377
3378 // Process string output to make it nicer...
3379 for (unsigned i = 0; i != OutStr.length(); ++i)
3380 if (OutStr[i] == '\n') { // Left justify
3381 OutStr[i] = '\\';
3382 OutStr.insert(OutStr.begin()+i+1, 'l');
3383 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003384
Ted Kremenek7dba8602007-08-29 21:56:09 +00003385 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003386#else
3387 return "";
3388#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003389 }
3390};
3391} // end namespace llvm