blob: b58e9826d336c2f8051496d515c2351474afd486 [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 Kremeneke4ae4dc2010-11-15 22:59:22 +00001207 // Note that there can possibly only be one predecessor if one of the
1208 // subexpressions resulted in calling a noreturn function.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001209 std::reverse(ConfluenceBlock->pred_begin(),
1210 ConfluenceBlock->pred_end());
1211 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001214 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001215 Block->setTerminator(C);
1216 return addStmt(C->getCond());
1217}
1218
Ted Kremenek4f880632009-07-17 22:18:43 +00001219CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001220 if (DS->isSingleDecl())
1221 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenek4f880632009-07-17 22:18:43 +00001223 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek4f880632009-07-17 22:18:43 +00001225 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1226 typedef llvm::SmallVector<Decl*,10> BufTy;
1227 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenek4f880632009-07-17 22:18:43 +00001229 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1230 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1231 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1232 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenek4f880632009-07-17 22:18:43 +00001234 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1235 // automatically freed with the CFG.
1236 DeclGroupRef DG(*I);
1237 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001238 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001239 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremenek4f880632009-07-17 22:18:43 +00001241 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001242 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
1245 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001246}
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenek4f880632009-07-17 22:18:43 +00001248/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001249/// DeclStmts and initializers in them.
1250CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1251 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001252
Marcin Swiderski8599e762010-11-03 06:19:35 +00001253 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Marcin Swiderski8599e762010-11-03 06:19:35 +00001255 if (!VD) {
1256 autoCreateBlock();
1257 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001258 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Marcin Swiderski8599e762010-11-03 06:19:35 +00001261 bool IsReference = false;
1262 bool HasTemporaries = false;
1263
1264 // Destructors of temporaries in initialization expression should be called
1265 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001266 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001267 if (Init) {
1268 IsReference = VD->getType()->isReferenceType();
1269 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
1270
1271 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1272 // Generate destructors for temporaries in initialization expression.
1273 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
1274 IsReference);
1275 }
1276 }
1277
1278 autoCreateBlock();
1279 AppendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek4f880632009-07-17 22:18:43 +00001281 if (Init) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001282 AddStmtChoice asc = IsReference
1283 ? AddStmtChoice::AsLValueNotAlwaysAdd
1284 : AddStmtChoice::NotAlwaysAdd;
1285 if (HasTemporaries)
1286 // For expression with temporaries go directly to subexpression to omit
1287 // generating destructors for the second time.
1288 Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
1289 else
1290 Visit(Init, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Ted Kremenek4f880632009-07-17 22:18:43 +00001293 // If the type of VD is a VLA, then we must process its size expressions.
1294 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1295 VA = FindVA(VA->getElementType().getTypePtr()))
1296 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001298 // Remove variable from local scope.
1299 if (ScopePos && VD == *ScopePos)
1300 ++ScopePos;
1301
Ted Kremenek4f880632009-07-17 22:18:43 +00001302 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001303}
1304
1305CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001306 // We may see an if statement in the middle of a basic block, or it may be the
1307 // first statement we are processing. In either case, we create a new basic
1308 // block. First, we create the blocks for the then...else statements, and
1309 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001310 // middle of a block, we stop processing that block. That block is then the
1311 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001312
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001313 // Save local scope position because in case of condition variable ScopePos
1314 // won't be restored when traversing AST.
1315 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1316
1317 // Create local scope for possible condition variable.
1318 // Store scope position. Add implicit destructor.
1319 if (VarDecl* VD = I->getConditionVariable()) {
1320 LocalScope::const_iterator BeginScopePos = ScopePos;
1321 addLocalScopeForVarDecl(VD);
1322 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1323 }
1324
Mike Stump6d9828c2009-07-17 01:31:16 +00001325 // The block we were proccessing is now finished. Make it the successor
1326 // block.
1327 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001328 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001329 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001330 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001331 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001332
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001333 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001334 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001335
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001336 if (Stmt* Else = I->getElse()) {
1337 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001338
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001339 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001340 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001341 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001342
1343 // If branch is not a compound statement create implicit scope
1344 // and add destructors.
1345 if (!isa<CompoundStmt>(Else))
1346 addLocalScopeAndDtors(Else);
1347
Ted Kremenek4f880632009-07-17 22:18:43 +00001348 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001350 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1351 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001352 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001353 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001354 return 0;
1355 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001356 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001357
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001358 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001359 CFGBlock* ThenBlock;
1360 {
1361 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001362 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001363 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001364 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001365
1366 // If branch is not a compound statement create implicit scope
1367 // and add destructors.
1368 if (!isa<CompoundStmt>(Then))
1369 addLocalScopeAndDtors(Then);
1370
Ted Kremenek4f880632009-07-17 22:18:43 +00001371 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001373 if (!ThenBlock) {
1374 // We can reach here if the "then" body has all NullStmts.
1375 // Create an empty block so we can distinguish between true and false
1376 // branches in path-sensitive analyses.
1377 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001378 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001379 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001380 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001381 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001382 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001383 }
1384
Mike Stump6d9828c2009-07-17 01:31:16 +00001385 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001386 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001388 // Set the terminator of the new block to the If statement.
1389 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001390
Mike Stump00998a02009-07-23 23:25:26 +00001391 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001392 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001393
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001394 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001395 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1396 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001397
1398 // Add the condition as the last statement in the new block. This may create
1399 // new blocks as the condition may contain control-flow. Any newly created
1400 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001401 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001402
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001403 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1404 // and the condition variable initialization to the CFG.
1405 if (VarDecl *VD = I->getConditionVariable()) {
1406 if (Expr *Init = VD->getInit()) {
1407 autoCreateBlock();
1408 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1409 addStmt(Init);
1410 }
1411 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001412
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001413 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001414}
Mike Stump6d9828c2009-07-17 01:31:16 +00001415
1416
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001417CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001418 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001420 // NOTE: If a "return" appears in the middle of a block, this means that the
1421 // code afterwards is DEAD (unreachable). We still keep a basic block
1422 // for that code; a simple "mark-and-sweep" from the entry block will be
1423 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001424
1425 // Create the new block.
1426 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001428 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001429 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001430 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
1432 // Add the return statement to the block. This may create new blocks if R
1433 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001434 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001435}
1436
1437CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1438 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001439 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001440 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
Ted Kremenek4f880632009-07-17 22:18:43 +00001442 if (!LabelBlock) // This can happen when the body is empty, i.e.
1443 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Ted Kremenek4f880632009-07-17 22:18:43 +00001445 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001446 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001447
1448 // Labels partition blocks, so this is the end of the basic block we were
1449 // processing (L is the block's label). Because this is label (and we have
1450 // already processed the substatement) there is no extra control-flow to worry
1451 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001452 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001453 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001454 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001455
1456 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001457 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001458
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001459 // This block is now the implicit successor of other blocks.
1460 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001461
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001462 return LabelBlock;
1463}
1464
1465CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001466 // Goto is a control-flow statement. Thus we stop processing the current
1467 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001468
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001469 Block = createBlock(false);
1470 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001471
1472 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001473 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001474
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001475 if (I == LabelMap.end())
1476 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001477 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1478 else {
1479 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001480 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001481 AddSuccessor(Block, JT.Block);
1482 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001483
Mike Stump6d9828c2009-07-17 01:31:16 +00001484 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001485}
1486
1487CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001488 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001489
Marcin Swiderski47575f12010-10-01 01:38:14 +00001490 // Save local scope position because in case of condition variable ScopePos
1491 // won't be restored when traversing AST.
1492 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1493
1494 // Create local scope for init statement and possible condition variable.
1495 // Add destructor for init statement and condition variable.
1496 // Store scope position for continue statement.
1497 if (Stmt* Init = F->getInit())
1498 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001499 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1500
Marcin Swiderski47575f12010-10-01 01:38:14 +00001501 if (VarDecl* VD = F->getConditionVariable())
1502 addLocalScopeForVarDecl(VD);
1503 LocalScope::const_iterator ContinueScopePos = ScopePos;
1504
1505 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1506
Mike Stumpfefb9f72009-07-21 01:12:51 +00001507 // "for" is a control-flow statement. Thus we stop processing the current
1508 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001509 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001510 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001511 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001512 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001513 } else
1514 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001515
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001516 // Save the current value for the break targets.
1517 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001518 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001519 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001520
Mike Stump6d9828c2009-07-17 01:31:16 +00001521 // Because of short-circuit evaluation, the condition of the loop can span
1522 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1523 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001524 CFGBlock* ExitConditionBlock = createBlock(false);
1525 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001526
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001527 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001528 ExitConditionBlock->setTerminator(F);
1529
1530 // Now add the actual condition to the condition block. Because the condition
1531 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001532 if (Stmt* C = F->getCond()) {
1533 Block = ExitConditionBlock;
1534 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001535 assert(Block == EntryConditionBlock ||
1536 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001537
1538 // If this block contains a condition variable, add both the condition
1539 // variable and initializer to the CFG.
1540 if (VarDecl *VD = F->getConditionVariable()) {
1541 if (Expr *Init = VD->getInit()) {
1542 autoCreateBlock();
1543 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1544 EntryConditionBlock = addStmt(Init);
1545 assert(Block == EntryConditionBlock);
1546 }
1547 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001548
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001549 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001550 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001551 return 0;
1552 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001553 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001554
Mike Stump6d9828c2009-07-17 01:31:16 +00001555 // The condition block is the implicit successor for the loop body as well as
1556 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001557 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001558
Mike Stump00998a02009-07-23 23:25:26 +00001559 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001560 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Mike Stump00998a02009-07-23 23:25:26 +00001562 if (F->getCond())
1563 KnownVal = TryEvaluateBool(F->getCond());
1564
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001565 // Now create the loop body.
1566 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001567 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001568
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001569 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001570 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1571 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001572
Ted Kremenekaf603f72007-08-30 18:39:40 +00001573 // Create a new block to contain the (bottom) of the loop body.
1574 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001575
1576 // Loop body should end with destructor of Condition variable (if any).
1577 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001578
Ted Kremeneke9334502008-09-04 21:48:47 +00001579 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001580 // Generate increment code in its own basic block. This is the target of
1581 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001582 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001583 } else {
1584 // No increment code. Create a special, empty, block that is used as the
1585 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001586 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001587 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001588 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenek3575f842009-04-28 00:51:56 +00001590 // Finish up the increment (or empty) block if it hasn't been already.
1591 if (Block) {
1592 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001593 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001594 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001595 Block = 0;
1596 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001597
Marcin Swiderski47575f12010-10-01 01:38:14 +00001598 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001599
Ted Kremenek3575f842009-04-28 00:51:56 +00001600 // The starting block for the loop increment is the block that should
1601 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001602 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001603
Marcin Swiderski47575f12010-10-01 01:38:14 +00001604 // If body is not a compound statement create implicit scope
1605 // and add destructors.
1606 if (!isa<CompoundStmt>(F->getBody()))
1607 addLocalScopeAndDtors(F->getBody());
1608
Mike Stump6d9828c2009-07-17 01:31:16 +00001609 // Now populate the body block, and in the process create new blocks as we
1610 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001611 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001612
1613 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001614 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001615 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001616 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
Ted Kremenek941fde82009-07-24 04:47:11 +00001618 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001619 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001620 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001621
Ted Kremenek941fde82009-07-24 04:47:11 +00001622 // Link up the condition block with the code that follows the loop. (the
1623 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001624 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001625
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001626 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001627 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001628 if (Stmt* I = F->getInit()) {
1629 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001630 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001631 } else {
1632 // There is no loop initialization. We are thus basically a while loop.
1633 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001634 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001635 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001636 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001637 }
1638}
1639
Ted Kremenek115c1b92010-04-11 17:02:10 +00001640CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1641 if (asc.alwaysAdd()) {
1642 autoCreateBlock();
1643 AppendStmt(Block, M, asc);
1644 }
1645 return Visit(M->getBase(),
1646 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1647 : AddStmtChoice::AsLValueNotAlwaysAdd);
1648}
1649
Ted Kremenek514de5a2008-11-11 17:10:00 +00001650CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1651 // Objective-C fast enumeration 'for' statements:
1652 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1653 //
1654 // for ( Type newVariable in collection_expression ) { statements }
1655 //
1656 // becomes:
1657 //
1658 // prologue:
1659 // 1. collection_expression
1660 // T. jump to loop_entry
1661 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001662 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001663 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1664 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1665 // TB:
1666 // statements
1667 // T. jump to loop_entry
1668 // FB:
1669 // what comes after
1670 //
1671 // and
1672 //
1673 // Type existingItem;
1674 // for ( existingItem in expression ) { statements }
1675 //
1676 // becomes:
1677 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001678 // the same with newVariable replaced with existingItem; the binding works
1679 // the same except that for one ObjCForCollectionStmt::getElement() returns
1680 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001681 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001682
Ted Kremenek514de5a2008-11-11 17:10:00 +00001683 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001684
Ted Kremenek514de5a2008-11-11 17:10:00 +00001685 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001686 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001687 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001688 LoopSuccessor = Block;
1689 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001690 } else
1691 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001692
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001693 // Build the condition blocks.
1694 CFGBlock* ExitConditionBlock = createBlock(false);
1695 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001696
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001697 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001698 ExitConditionBlock->setTerminator(S);
1699
1700 // The last statement in the block should be the ObjCForCollectionStmt, which
1701 // performs the actual binding to 'element' and determines if there are any
1702 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001703 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001704 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001705
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001706 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001707 // generate new blocks as necesary. We DON'T add the statement by default to
1708 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001709 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001710 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001711 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001712 return 0;
1713 Block = 0;
1714 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001715
1716 // The condition block is the implicit successor for the loop body as well as
1717 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001718 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001719
Ted Kremenek514de5a2008-11-11 17:10:00 +00001720 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001721 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001722 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001723 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1724 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1725 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001726
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001727 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1728 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001729
Ted Kremenek4f880632009-07-17 22:18:43 +00001730 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001731
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001732 if (!BodyBlock)
1733 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001734 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001735 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001736 return 0;
1737 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001738
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001739 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001740 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001741 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001742
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001743 // Link up the condition block with the code that follows the loop.
1744 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001745 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001746
Ted Kremenek514de5a2008-11-11 17:10:00 +00001747 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001748 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001749 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001750}
1751
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001752CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1753 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001754
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001755 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001756 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001757
Ted Kremenekda5348e2009-05-05 23:11:51 +00001758 // The sync body starts its own basic block. This makes it a little easier
1759 // for diagnostic clients.
1760 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001761 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001762 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001763
Ted Kremenekda5348e2009-05-05 23:11:51 +00001764 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001765 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001766 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001768 // Add the @synchronized to the CFG.
1769 autoCreateBlock();
1770 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1771
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001772 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001773 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001774}
Mike Stump6d9828c2009-07-17 01:31:16 +00001775
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001776CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001777 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001778 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001779}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001780
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001781CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001782 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001783
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001784 // Save local scope position because in case of condition variable ScopePos
1785 // won't be restored when traversing AST.
1786 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1787
1788 // Create local scope for possible condition variable.
1789 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001790 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001791 if (VarDecl* VD = W->getConditionVariable()) {
1792 addLocalScopeForVarDecl(VD);
1793 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1794 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001795
Mike Stumpfefb9f72009-07-21 01:12:51 +00001796 // "while" is a control-flow statement. Thus we stop processing the current
1797 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001798 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001799 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001800 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001801 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001802 } else
1803 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001804
1805 // Because of short-circuit evaluation, the condition of the loop can span
1806 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1807 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001808 CFGBlock* ExitConditionBlock = createBlock(false);
1809 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001810
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001811 // Set the terminator for the "exit" condition block.
1812 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001813
1814 // Now add the actual condition to the condition block. Because the condition
1815 // itself may contain control-flow, new blocks may be created. Thus we update
1816 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001817 if (Stmt* C = W->getCond()) {
1818 Block = ExitConditionBlock;
1819 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001820 // The condition might finish the current 'Block'.
1821 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001822
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001823 // If this block contains a condition variable, add both the condition
1824 // variable and initializer to the CFG.
1825 if (VarDecl *VD = W->getConditionVariable()) {
1826 if (Expr *Init = VD->getInit()) {
1827 autoCreateBlock();
1828 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1829 EntryConditionBlock = addStmt(Init);
1830 assert(Block == EntryConditionBlock);
1831 }
1832 }
1833
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001834 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001835 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001836 return 0;
1837 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001838 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001839
1840 // The condition block is the implicit successor for the loop body as well as
1841 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001842 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001843
Mike Stump00998a02009-07-23 23:25:26 +00001844 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001845 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001846
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001847 // Process the loop body.
1848 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001849 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001850
1851 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001852 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1853 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1854 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001855
Mike Stump6d9828c2009-07-17 01:31:16 +00001856 // Create an empty block to represent the transition block for looping back
1857 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001858 Block = 0;
1859 assert(Succ == EntryConditionBlock);
1860 Succ = createBlock();
1861 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001862 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001863
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001864 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001865 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001867 // NULL out Block to force lazy instantiation of blocks for the body.
1868 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001869
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001870 // Loop body should end with destructor of Condition variable (if any).
1871 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1872
1873 // If body is not a compound statement create implicit scope
1874 // and add destructors.
1875 if (!isa<CompoundStmt>(W->getBody()))
1876 addLocalScopeAndDtors(W->getBody());
1877
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001878 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001879 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001880
Ted Kremenekaf603f72007-08-30 18:39:40 +00001881 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001882 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001883 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001884 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001885 return 0;
1886 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001887
Ted Kremenek941fde82009-07-24 04:47:11 +00001888 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001889 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001890 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001891
Ted Kremenek941fde82009-07-24 04:47:11 +00001892 // Link up the condition block with the code that follows the loop. (the
1893 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001894 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
1896 // There can be no more statements in the condition block since we loop back
1897 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001898 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001899
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001900 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001901 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001902 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001903}
Mike Stump1eb44332009-09-09 15:08:12 +00001904
1905
Ted Kremenek4f880632009-07-17 22:18:43 +00001906CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1907 // FIXME: For now we pretend that @catch and the code it contains does not
1908 // exit.
1909 return Block;
1910}
Mike Stump6d9828c2009-07-17 01:31:16 +00001911
Ted Kremenek2fda5042008-12-09 20:20:09 +00001912CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1913 // FIXME: This isn't complete. We basically treat @throw like a return
1914 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001915
Ted Kremenek6c249722009-09-24 18:45:41 +00001916 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001917 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001918 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001919
Ted Kremenek2fda5042008-12-09 20:20:09 +00001920 // Create the new block.
1921 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001922
Ted Kremenek2fda5042008-12-09 20:20:09 +00001923 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001924 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001925
1926 // Add the statement to the block. This may create new blocks if S contains
1927 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001928 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001929}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001930
Mike Stump0979d802009-07-22 22:56:04 +00001931CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001932 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001933 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001934 return 0;
1935
1936 // Create the new block.
1937 Block = createBlock(false);
1938
Mike Stump5d1d2022010-01-19 02:20:09 +00001939 if (TryTerminatedBlock)
1940 // The current try statement is the only successor.
1941 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001942 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001943 // otherwise the Exit block is the only successor.
1944 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001945
1946 // Add the statement to the block. This may create new blocks if S contains
1947 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001948 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001949}
1950
Ted Kremenek4f880632009-07-17 22:18:43 +00001951CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001952 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001953
Mike Stump8f9893a2009-07-21 01:27:50 +00001954 // "do...while" is a control-flow statement. Thus we stop processing the
1955 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001956 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001957 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001958 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001959 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001960 } else
1961 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001962
1963 // Because of short-circuit evaluation, the condition of the loop can span
1964 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1965 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001966 CFGBlock* ExitConditionBlock = createBlock(false);
1967 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001969 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001970 ExitConditionBlock->setTerminator(D);
1971
1972 // Now add the actual condition to the condition block. Because the condition
1973 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001974 if (Stmt* C = D->getCond()) {
1975 Block = ExitConditionBlock;
1976 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001977 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001978 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001979 return 0;
1980 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001981 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
Ted Kremenek54827132008-02-27 07:20:00 +00001983 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001984 Succ = EntryConditionBlock;
1985
Mike Stump00998a02009-07-23 23:25:26 +00001986 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001987 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001988
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001989 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001990 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001991 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001992 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001993
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001994 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001995 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1996 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1997 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001999 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002000 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002001
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002002 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002003 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002004
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002005 // NULL out Block to force lazy instantiation of blocks for the body.
2006 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002007
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002008 // If body is not a compound statement create implicit scope
2009 // and add destructors.
2010 if (!isa<CompoundStmt>(D->getBody()))
2011 addLocalScopeAndDtors(D->getBody());
2012
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002013 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002014 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002015
Ted Kremenekaf603f72007-08-30 18:39:40 +00002016 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002017 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002018 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002019 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002020 return 0;
2021 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002022
Ted Kremenekd173dc72010-08-17 20:59:56 +00002023 if (!KnownVal.isFalse()) {
2024 // Add an intermediate block between the BodyBlock and the
2025 // ExitConditionBlock to represent the "loop back" transition. Create an
2026 // empty block to represent the transition block for looping back to the
2027 // head of the loop.
2028 // FIXME: Can we do this more efficiently without adding another block?
2029 Block = NULL;
2030 Succ = BodyBlock;
2031 CFGBlock *LoopBackBlock = createBlock();
2032 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002033
Ted Kremenekd173dc72010-08-17 20:59:56 +00002034 // Add the loop body entry as a successor to the condition.
2035 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2036 }
2037 else
2038 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002039 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Ted Kremenek941fde82009-07-24 04:47:11 +00002041 // Link up the condition block with the code that follows the loop.
2042 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002043 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002044
2045 // There can be no more statements in the body block(s) since we loop back to
2046 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002047 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002048
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002049 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002050 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002051 return BodyBlock;
2052}
2053
2054CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2055 // "continue" is a control-flow statement. Thus we stop processing the
2056 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002057 if (badCFG)
2058 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002059
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002060 // Now create a new block that ends with the continue statement.
2061 Block = createBlock(false);
2062 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002063
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002064 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002065 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002066 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00002067 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002068 AddSuccessor(Block, ContinueJumpTarget.Block);
2069 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002070 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002071
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002072 return Block;
2073}
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002075CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002076 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002077
Ted Kremenek852274d2009-12-16 03:18:58 +00002078 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002079 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002080 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002081 }
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Ted Kremenek4f880632009-07-17 22:18:43 +00002083 // VLA types have expressions that must be evaluated.
2084 if (E->isArgumentType()) {
2085 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2086 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2087 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002088 }
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Mike Stump6d9828c2009-07-17 01:31:16 +00002090 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002091}
Mike Stump1eb44332009-09-09 15:08:12 +00002092
Ted Kremenek4f880632009-07-17 22:18:43 +00002093/// VisitStmtExpr - Utility method to handle (nested) statement
2094/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002095CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2096 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002097 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002098 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002099 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002100 return VisitCompoundStmt(SE->getSubStmt());
2101}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002102
Ted Kremenek411cdee2008-04-16 21:10:48 +00002103CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002104 // "switch" is a control-flow statement. Thus we stop processing the current
2105 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002106 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002107
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002108 // Save local scope position because in case of condition variable ScopePos
2109 // won't be restored when traversing AST.
2110 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2111
2112 // Create local scope for possible condition variable.
2113 // Store scope position. Add implicit destructor.
2114 if (VarDecl* VD = Terminator->getConditionVariable()) {
2115 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2116 addLocalScopeForVarDecl(VD);
2117 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2118 }
2119
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002120 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002121 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002122 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002123 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002124 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002125
2126 // Save the current "switch" context.
2127 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002128 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002129 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002130
Mike Stump6d9828c2009-07-17 01:31:16 +00002131 // Set the "default" case to be the block after the switch statement. If the
2132 // switch statement contains a "default:", this value will be overwritten with
2133 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002134 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002135
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002136 // Create a new block that will contain the switch statement.
2137 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002138
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002139 // Now process the switch body. The code after the switch is the implicit
2140 // successor.
2141 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002142 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002143
2144 // When visiting the body, the case statements should automatically get linked
2145 // up to the switch. We also don't keep a pointer to the body, since all
2146 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002147 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002148 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002149
2150 // If body is not a compound statement create implicit scope
2151 // and add destructors.
2152 if (!isa<CompoundStmt>(Terminator->getBody()))
2153 addLocalScopeAndDtors(Terminator->getBody());
2154
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002155 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002156 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002157 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002158 return 0;
2159 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002160
Mike Stump6d9828c2009-07-17 01:31:16 +00002161 // If we have no "default:" case, the default transition is to the code
2162 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002163 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002164
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002165 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002166 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002167 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002168 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002169 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002170
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002171 // Finally, if the SwitchStmt contains a condition variable, add both the
2172 // SwitchStmt and the condition variable initialization to the CFG.
2173 if (VarDecl *VD = Terminator->getConditionVariable()) {
2174 if (Expr *Init = VD->getInit()) {
2175 autoCreateBlock();
2176 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2177 addStmt(Init);
2178 }
2179 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002180
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002181 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002182}
2183
Ted Kremenek4f880632009-07-17 22:18:43 +00002184CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002185 // CaseStmts are essentially labels, so they are the first statement in a
2186 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002187 CFGBlock *TopBlock = 0, *LastBlock = 0;
2188
2189 if (Stmt *Sub = CS->getSubStmt()) {
2190 // For deeply nested chains of CaseStmts, instead of doing a recursion
2191 // (which can blow out the stack), manually unroll and create blocks
2192 // along the way.
2193 while (isa<CaseStmt>(Sub)) {
2194 CFGBlock *CurrentBlock = createBlock(false);
2195 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002196
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002197 if (TopBlock)
2198 AddSuccessor(LastBlock, CurrentBlock);
2199 else
2200 TopBlock = CurrentBlock;
2201
2202 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2203 LastBlock = CurrentBlock;
2204
2205 CS = cast<CaseStmt>(Sub);
2206 Sub = CS->getSubStmt();
2207 }
2208
2209 addStmt(Sub);
2210 }
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002212 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002213 if (!CaseBlock)
2214 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
2216 // Cases statements partition blocks, so this is the top of the basic block we
2217 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002218 CaseBlock->setLabel(CS);
2219
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002220 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002221 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002222
2223 // Add this block to the list of successors for the block with the switch
2224 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002225 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002226 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002227
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002228 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2229 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002230
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002231 if (TopBlock) {
2232 AddSuccessor(LastBlock, CaseBlock);
2233 Succ = TopBlock;
2234 }
2235 else {
2236 // This block is now the implicit successor of other blocks.
2237 Succ = CaseBlock;
2238 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002240 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002241}
Mike Stump6d9828c2009-07-17 01:31:16 +00002242
Ted Kremenek411cdee2008-04-16 21:10:48 +00002243CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002244 if (Terminator->getSubStmt())
2245 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002247 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002248
2249 if (!DefaultCaseBlock)
2250 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002251
2252 // Default statements partition blocks, so this is the top of the basic block
2253 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002254 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002256 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002257 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002258
Mike Stump6d9828c2009-07-17 01:31:16 +00002259 // Unlike case statements, we don't add the default block to the successors
2260 // for the switch statement immediately. This is done when we finish
2261 // processing the switch statement. This allows for the default case
2262 // (including a fall-through to the code after the switch statement) to always
2263 // be the last successor of a switch-terminated block.
2264
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002265 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2266 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002267
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002268 // This block is now the implicit successor of other blocks.
2269 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002270
2271 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002272}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002273
Mike Stump5d1d2022010-01-19 02:20:09 +00002274CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2275 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2276 // current block.
2277 CFGBlock* TrySuccessor = NULL;
2278
2279 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002280 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002281 return 0;
2282 TrySuccessor = Block;
2283 } else TrySuccessor = Succ;
2284
Mike Stumpa1f93632010-01-20 01:15:34 +00002285 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002286
2287 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002288 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002289 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002290 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002291
Mike Stumpa1f93632010-01-20 01:15:34 +00002292 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002293 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2294 // The code after the try is the implicit successor.
2295 Succ = TrySuccessor;
2296 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002297 if (CS->getExceptionDecl() == 0) {
2298 HasCatchAll = true;
2299 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002300 Block = NULL;
2301 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2302 if (CatchBlock == 0)
2303 return 0;
2304 // Add this block to the list of successors for the block with the try
2305 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002306 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002307 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002308 if (!HasCatchAll) {
2309 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002310 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002311 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002312 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002313 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002314
2315 // The code after the try is the implicit successor.
2316 Succ = TrySuccessor;
2317
Mike Stumpf00cca52010-01-20 01:30:58 +00002318 // Save the current "try" context.
2319 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2320 TryTerminatedBlock = NewTryTerminatedBlock;
2321
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002322 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002323 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002324 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002325 return Block;
2326}
2327
2328CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2329 // CXXCatchStmt are treated like labels, so they are the first statement in a
2330 // block.
2331
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002332 // Save local scope position because in case of exception variable ScopePos
2333 // won't be restored when traversing AST.
2334 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2335
2336 // Create local scope for possible exception variable.
2337 // Store scope position. Add implicit destructor.
2338 if (VarDecl* VD = CS->getExceptionDecl()) {
2339 LocalScope::const_iterator BeginScopePos = ScopePos;
2340 addLocalScopeForVarDecl(VD);
2341 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2342 }
2343
Mike Stump5d1d2022010-01-19 02:20:09 +00002344 if (CS->getHandlerBlock())
2345 addStmt(CS->getHandlerBlock());
2346
2347 CFGBlock* CatchBlock = Block;
2348 if (!CatchBlock)
2349 CatchBlock = createBlock();
2350
2351 CatchBlock->setLabel(CS);
2352
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002353 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002354 return 0;
2355
2356 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2357 Block = NULL;
2358
2359 return CatchBlock;
2360}
2361
Marcin Swiderski8599e762010-11-03 06:19:35 +00002362CFGBlock *CFGBuilder::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
2363 AddStmtChoice asc) {
2364 if (BuildOpts.AddImplicitDtors) {
2365 // If adding implicit destructors visit the full expression for adding
2366 // destructors of temporaries.
2367 VisitForTemporaryDtors(E->getSubExpr());
2368
2369 // Full expression has to be added as CFGStmt so it will be sequenced
2370 // before destructors of it's temporaries.
2371 asc = asc.asLValue()
2372 ? AddStmtChoice::AlwaysAddAsLValue
2373 : AddStmtChoice::AlwaysAdd;
2374 }
2375 return Visit(E->getSubExpr(), asc);
2376}
2377
Zhongxing Xua725ed42010-11-01 13:04:58 +00002378CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2379 AddStmtChoice asc) {
2380 if (asc.alwaysAdd()) {
2381 autoCreateBlock();
2382 AppendStmt(Block, E, asc);
2383
2384 // We do not want to propagate the AlwaysAdd property.
2385 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2386 : AddStmtChoice::NotAlwaysAdd);
2387 }
2388 return Visit(E->getSubExpr(), asc);
2389}
2390
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002391CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2392 AddStmtChoice asc) {
2393 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2394 : AddStmtChoice::AlwaysAdd;
2395 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002396 if (!C->isElidable())
2397 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002398 return VisitChildren(C);
2399}
2400
Zhongxing Xua725ed42010-11-01 13:04:58 +00002401CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2402 AddStmtChoice asc) {
2403 if (asc.alwaysAdd()) {
2404 autoCreateBlock();
2405 AppendStmt(Block, E, asc);
2406 // We do not want to propagate the AlwaysAdd property.
2407 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2408 : AddStmtChoice::NotAlwaysAdd);
2409 }
2410 return Visit(E->getSubExpr(), asc);
2411}
2412
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002413CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2414 AddStmtChoice asc) {
2415 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2416 : AddStmtChoice::AlwaysAdd;
2417 autoCreateBlock();
2418 AppendStmt(Block, C, AddStmtChoice(K));
2419 return VisitChildren(C);
2420}
2421
Ted Kremenekad5a8942010-08-02 23:46:59 +00002422CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002423 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002424 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002425 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002426 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002427 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002428 return VisitChildren(C);
2429}
2430
Zhongxing Xua725ed42010-11-01 13:04:58 +00002431CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2432 AddStmtChoice asc) {
2433 if (asc.alwaysAdd()) {
2434 autoCreateBlock();
2435 AppendStmt(Block, E, asc);
2436 // We do not want to propagate the AlwaysAdd property.
2437 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2438 : AddStmtChoice::NotAlwaysAdd);
2439 }
2440 return Visit(E->getSubExpr(), asc);
2441}
2442
Ted Kremenek19bb3562007-08-28 19:26:49 +00002443CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002444 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002445 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002446
Ted Kremenek19bb3562007-08-28 19:26:49 +00002447 if (!IBlock) {
2448 IBlock = createBlock(false);
2449 cfg->setIndirectGotoBlock(IBlock);
2450 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002451
Ted Kremenek19bb3562007-08-28 19:26:49 +00002452 // IndirectGoto is a control-flow statement. Thus we stop processing the
2453 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002454 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002455 return 0;
2456
Ted Kremenek19bb3562007-08-28 19:26:49 +00002457 Block = createBlock(false);
2458 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002459 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002460 return addStmt(I->getTarget());
2461}
2462
Marcin Swiderski8599e762010-11-03 06:19:35 +00002463CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2464tryAgain:
2465 if (!E) {
2466 badCFG = true;
2467 return NULL;
2468 }
2469 switch (E->getStmtClass()) {
2470 default:
2471 return VisitChildrenForTemporaryDtors(E);
2472
2473 case Stmt::BinaryOperatorClass:
2474 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2475
2476 case Stmt::CXXBindTemporaryExprClass:
2477 return VisitCXXBindTemporaryExprForTemporaryDtors(
2478 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2479
2480 case Stmt::ConditionalOperatorClass:
2481 return VisitConditionalOperatorForTemporaryDtors(
2482 cast<ConditionalOperator>(E), BindToTemporary);
2483
2484 case Stmt::ImplicitCastExprClass:
2485 // For implicit cast we want BindToTemporary to be passed further.
2486 E = cast<CastExpr>(E)->getSubExpr();
2487 goto tryAgain;
2488
2489 case Stmt::ParenExprClass:
2490 E = cast<ParenExpr>(E)->getSubExpr();
2491 goto tryAgain;
2492 }
2493}
2494
2495CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2496 // When visiting children for destructors we want to visit them in reverse
2497 // order. Because there's no reverse iterator for children must to reverse
2498 // them in helper vector.
2499 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2500 ChildrenVect ChildrenRev;
2501 for (Stmt::child_iterator I = E->child_begin(), L = E->child_end();
2502 I != L; ++I) {
2503 if (*I) ChildrenRev.push_back(*I);
2504 }
2505
2506 CFGBlock *B = Block;
2507 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2508 L = ChildrenRev.rend(); I != L; ++I) {
2509 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2510 B = R;
2511 }
2512 return B;
2513}
2514
2515CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2516 if (E->isLogicalOp()) {
2517 // Destructors for temporaries in LHS expression should be called after
2518 // those for RHS expression. Even if this will unnecessarily create a block,
2519 // this block will be used at least by the full expression.
2520 autoCreateBlock();
2521 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2522 if (badCFG)
2523 return NULL;
2524
2525 Succ = ConfluenceBlock;
2526 Block = NULL;
2527 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2528
2529 if (RHSBlock) {
2530 if (badCFG)
2531 return NULL;
2532
2533 // If RHS expression did produce destructors we need to connect created
2534 // blocks to CFG in same manner as for binary operator itself.
2535 CFGBlock *LHSBlock = createBlock(false);
2536 LHSBlock->setTerminator(CFGTerminator(E, true));
2537
2538 // For binary operator LHS block is before RHS in list of predecessors
2539 // of ConfluenceBlock.
2540 std::reverse(ConfluenceBlock->pred_begin(),
2541 ConfluenceBlock->pred_end());
2542
2543 // See if this is a known constant.
2544 TryResult KnownVal = TryEvaluateBool(E->getLHS());
2545 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2546 KnownVal.negate();
2547
2548 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2549 // itself.
2550 if (E->getOpcode() == BO_LOr) {
2551 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2552 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2553 } else {
2554 assert (E->getOpcode() == BO_LAnd);
2555 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2556 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2557 }
2558
2559 Block = LHSBlock;
2560 return LHSBlock;
2561 }
2562
2563 Block = ConfluenceBlock;
2564 return ConfluenceBlock;
2565 }
2566
2567 else if (E->isAssignmentOp()) {
2568 // For assignment operator (=) LHS expression is visited
2569 // before RHS expression. For destructors visit them in reverse order.
2570 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2571 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2572 return LHSBlock ? LHSBlock : RHSBlock;
2573 }
2574
2575 // For any other binary operator RHS expression is visited before
2576 // LHS expression (order of children). For destructors visit them in reverse
2577 // order.
2578 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2579 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2580 return RHSBlock ? RHSBlock : LHSBlock;
2581}
2582
2583CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2584 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2585 // First add destructors for temporaries in subexpression.
2586 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002587 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002588 // If lifetime of temporary is not prolonged (by assigning to constant
2589 // reference) add destructor for it.
2590 autoCreateBlock();
2591 appendTemporaryDtor(Block, E);
2592 B = Block;
2593 }
2594 return B;
2595}
2596
2597CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2598 ConditionalOperator *E, bool BindToTemporary) {
2599 // First add destructors for condition expression. Even if this will
2600 // unnecessarily create a block, this block will be used at least by the full
2601 // expression.
2602 autoCreateBlock();
2603 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2604 if (badCFG)
2605 return NULL;
2606
2607 // Try to add block with destructors for LHS expression.
2608 CFGBlock *LHSBlock = NULL;
2609 if (E->getLHS()) {
2610 Succ = ConfluenceBlock;
2611 Block = NULL;
2612 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2613 if (badCFG)
2614 return NULL;
2615 }
2616
2617 // Try to add block with destructors for RHS expression;
2618 Succ = ConfluenceBlock;
2619 Block = NULL;
2620 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2621 if (badCFG)
2622 return NULL;
2623
2624 if (!RHSBlock && !LHSBlock) {
2625 // If neither LHS nor RHS expression had temporaries to destroy don't create
2626 // more blocks.
2627 Block = ConfluenceBlock;
2628 return Block;
2629 }
2630
2631 Block = createBlock(false);
2632 Block->setTerminator(CFGTerminator(E, true));
2633
2634 // See if this is a known constant.
2635 const TryResult &KnownVal = TryEvaluateBool(E->getCond());
2636
2637 if (LHSBlock) {
2638 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
2639 } else if (KnownVal.isFalse()) {
2640 AddSuccessor(Block, NULL);
2641 } else {
2642 AddSuccessor(Block, ConfluenceBlock);
2643 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2644 }
2645
2646 if (!RHSBlock)
2647 RHSBlock = ConfluenceBlock;
2648 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
2649
2650 return Block;
2651}
2652
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002653} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002654
Mike Stump6d9828c2009-07-17 01:31:16 +00002655/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2656/// no successors or predecessors. If this is the first block created in the
2657/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002658CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002659 bool first_block = begin() == end();
2660
2661 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002662 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2663 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2664 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002665
2666 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002667 if (first_block)
2668 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002669
2670 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002671 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002672}
2673
Ted Kremenek026473c2007-08-23 16:51:22 +00002674/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2675/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002676CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002677 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002678 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002679 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002680}
2681
Ted Kremenek63f58872007-10-01 19:33:33 +00002682//===----------------------------------------------------------------------===//
2683// CFG: Queries for BlkExprs.
2684//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002685
Ted Kremenek63f58872007-10-01 19:33:33 +00002686namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002687 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002688}
2689
Ted Kremenek8a693662009-12-23 23:37:10 +00002690static void FindSubExprAssignments(Stmt *S,
2691 llvm::SmallPtrSet<Expr*,50>& Set) {
2692 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002693 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002694
Ted Kremenek8a693662009-12-23 23:37:10 +00002695 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002696 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002697 if (!child)
2698 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002699
Ted Kremenek8a693662009-12-23 23:37:10 +00002700 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002701 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002702
Ted Kremenek8a693662009-12-23 23:37:10 +00002703 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002704 }
2705}
2706
Ted Kremenek63f58872007-10-01 19:33:33 +00002707static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2708 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002709
2710 // Look for assignments that are used as subexpressions. These are the only
2711 // assignments that we want to *possibly* register as a block-level
2712 // expression. Basically, if an assignment occurs both in a subexpression and
2713 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002714 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002715
Ted Kremenek63f58872007-10-01 19:33:33 +00002716 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002717 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002718 if (CFGStmt S = BI->getAs<CFGStmt>())
2719 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002720
Ted Kremenek411cdee2008-04-16 21:10:48 +00002721 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002722
2723 // Iterate over the statements again on identify the Expr* and Stmt* at the
2724 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002725
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002726 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2727 CFGStmt CS = BI->getAs<CFGStmt>();
2728 if (!CS.isValid())
2729 continue;
2730 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002731
Ted Kremenek411cdee2008-04-16 21:10:48 +00002732 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002733 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002734 // expression are really "statements" whose value is never used by
2735 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002736 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002737 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002738 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2739 // Special handling for statement expressions. The last statement in
2740 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002741 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002742 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002743 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002744 (*M)[C->body_back()] = x;
2745 }
2746 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002747
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002748 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002749 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002750 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002751 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002752
Ted Kremenek411cdee2008-04-16 21:10:48 +00002753 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002754
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002755 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002756
Ted Kremenek390e48b2008-11-12 21:11:49 +00002757 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002758 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002759 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002760 }
2761 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002762
Ted Kremenek63f58872007-10-01 19:33:33 +00002763 return M;
2764}
2765
Ted Kremenek86946742008-01-17 20:48:37 +00002766CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2767 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002768 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002769
Ted Kremenek63f58872007-10-01 19:33:33 +00002770 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002771 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002772 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002773}
2774
2775unsigned CFG::getNumBlkExprs() {
2776 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2777 return M->size();
2778 else {
2779 // We assume callers interested in the number of BlkExprs will want
2780 // the map constructed if it doesn't already exist.
2781 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2782 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2783 }
2784}
2785
Ted Kremenek274f4332008-04-28 18:00:46 +00002786//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002787// Filtered walking of the CFG.
2788//===----------------------------------------------------------------------===//
2789
2790bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002791 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002792
2793 if (F.IgnoreDefaultsWithCoveredEnums) {
2794 // If the 'To' has no label or is labeled but the label isn't a
2795 // CaseStmt then filter this edge.
2796 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002797 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002798 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002799 const Stmt *L = To->getLabel();
2800 if (!L || !isa<CaseStmt>(L))
2801 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002802 }
2803 }
2804 }
2805
2806 return false;
2807}
2808
2809//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002810// Cleanup: CFG dstor.
2811//===----------------------------------------------------------------------===//
2812
Ted Kremenek63f58872007-10-01 19:33:33 +00002813CFG::~CFG() {
2814 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2815}
Mike Stump6d9828c2009-07-17 01:31:16 +00002816
Ted Kremenek7dba8602007-08-29 21:56:09 +00002817//===----------------------------------------------------------------------===//
2818// CFG pretty printing
2819//===----------------------------------------------------------------------===//
2820
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002821namespace {
2822
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002823class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002824 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002825 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002826 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002827 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002828 signed CurrentBlock;
2829 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002830 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002831public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002832
Chris Lattnere4f21422009-06-30 01:26:17 +00002833 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2834 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002835 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2836 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002837 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002838 BI != BEnd; ++BI, ++j ) {
2839 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2840 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2841 StmtMap[SE] = P;
2842
2843 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2844 DeclMap[DS->getSingleDecl()] = P;
2845
2846 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2847 if (VarDecl* VD = IS->getConditionVariable())
2848 DeclMap[VD] = P;
2849
2850 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2851 if (VarDecl* VD = FS->getConditionVariable())
2852 DeclMap[VD] = P;
2853
2854 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2855 if (VarDecl* VD = WS->getConditionVariable())
2856 DeclMap[VD] = P;
2857
2858 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2859 if (VarDecl* VD = SS->getConditionVariable())
2860 DeclMap[VD] = P;
2861
2862 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2863 if (VarDecl* VD = CS->getExceptionDecl())
2864 DeclMap[VD] = P;
2865 }
2866 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002867 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002868 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002869 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002870
Ted Kremenek42a509f2007-08-31 21:30:12 +00002871 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002872
Chris Lattnere4f21422009-06-30 01:26:17 +00002873 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002874 void setBlockID(signed i) { CurrentBlock = i; }
2875 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002876
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002877 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2878 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002879
2880 if (I == StmtMap.end())
2881 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002882
2883 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002884 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002885 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002886 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002887
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002888 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002889 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002890 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002891
2892 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2893 DeclMapTy::iterator I = DeclMap.find(D);
2894
2895 if (I == DeclMap.end())
2896 return false;
2897
2898 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2899 && I->second.second == CurrentStmt) {
2900 return false;
2901 }
2902
2903 OS << "[B" << I->second.first << "." << I->second.second << "]";
2904 return true;
2905 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002906};
Chris Lattnere4f21422009-06-30 01:26:17 +00002907} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002908
Chris Lattnere4f21422009-06-30 01:26:17 +00002909
2910namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002911class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002912 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002913
Ted Kremeneka95d3752008-09-13 05:16:45 +00002914 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002915 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002916 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002917public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002918 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002919 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002920 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002921
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002922 void VisitIfStmt(IfStmt* I) {
2923 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002924 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002925 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002926
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002927 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002928 void VisitStmt(Stmt* Terminator) {
2929 Terminator->printPretty(OS, Helper, Policy);
2930 }
2931
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002932 void VisitForStmt(ForStmt* F) {
2933 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002934 if (F->getInit())
2935 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002936 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002937 if (Stmt* C = F->getCond())
2938 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002939 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002940 if (F->getInc())
2941 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002942 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002943 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002944
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002945 void VisitWhileStmt(WhileStmt* W) {
2946 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002947 if (Stmt* C = W->getCond())
2948 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002949 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002950
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002951 void VisitDoStmt(DoStmt* D) {
2952 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002953 if (Stmt* C = D->getCond())
2954 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002955 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002956
Ted Kremenek411cdee2008-04-16 21:10:48 +00002957 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002958 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002959 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002960 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002961
Mike Stump5d1d2022010-01-19 02:20:09 +00002962 void VisitCXXTryStmt(CXXTryStmt* CS) {
2963 OS << "try ...";
2964 }
2965
Ted Kremenek805e9a82007-08-31 21:49:40 +00002966 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002967 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002968 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002969 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002970
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002971 void VisitChooseExpr(ChooseExpr* C) {
2972 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002973 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002974 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002975 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002976
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002977 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2978 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002979 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002980 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002981
Ted Kremenek805e9a82007-08-31 21:49:40 +00002982 void VisitBinaryOperator(BinaryOperator* B) {
2983 if (!B->isLogicalOp()) {
2984 VisitExpr(B);
2985 return;
2986 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002987
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002988 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002989
Ted Kremenek805e9a82007-08-31 21:49:40 +00002990 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002991 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002992 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002993 return;
John McCall2de56d12010-08-25 11:45:40 +00002994 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002995 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002996 return;
2997 default:
2998 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002999 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003000 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003001
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003002 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003003 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003004 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003005};
Chris Lattnere4f21422009-06-30 01:26:17 +00003006} // end anonymous namespace
3007
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003008static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003009 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003010 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3011 Stmt *S = CS;
3012
3013 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003014
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003015 // special printing for statement-expressions.
3016 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3017 CompoundStmt* Sub = SE->getSubStmt();
3018
3019 if (Sub->child_begin() != Sub->child_end()) {
3020 OS << "({ ... ; ";
3021 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3022 OS << " })\n";
3023 return;
3024 }
3025 }
3026 // special printing for comma expressions.
3027 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3028 if (B->getOpcode() == BO_Comma) {
3029 OS << "... , ";
3030 Helper->handledStmt(B->getRHS(),OS);
3031 OS << '\n';
3032 return;
3033 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003034 }
3035 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003036 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003037
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003038 if (isa<CXXOperatorCallExpr>(S)) {
3039 OS << " (OperatorCall)";
Mike Stump6d9828c2009-07-17 01:31:16 +00003040 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003041 else if (isa<CXXBindTemporaryExpr>(S)) {
3042 OS << " (BindTemporary)";
3043 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003044
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003045 // Expressions need a newline.
3046 if (isa<Expr>(S))
3047 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003048
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003049 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3050 CXXBaseOrMemberInitializer* I = IE;
3051 if (I->isBaseInitializer())
3052 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
3053 else OS << I->getMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003054
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003055 OS << "(";
3056 if (Expr* IE = I->getInit())
3057 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3058 OS << ")";
3059
3060 if (I->isBaseInitializer())
3061 OS << " (Base initializer)\n";
3062 else OS << " (Member initializer)\n";
3063
3064 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3065 VarDecl* VD = DE.getVarDecl();
3066 Helper->handleDecl(VD, OS);
3067
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003068 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003069 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3070 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003071 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3072 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003073
3074 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3075 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003076
3077 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3078 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3079 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003080 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003081
3082 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3083 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003084
3085 const Type *T = FD->getType().getTypePtr();
3086 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3087 T = ET;
3088
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003089 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003090 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003091 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003092
3093 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3094 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3095 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3096 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003097 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003098}
Mike Stump6d9828c2009-07-17 01:31:16 +00003099
Chris Lattnere4f21422009-06-30 01:26:17 +00003100static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3101 const CFGBlock& B,
3102 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003103
Ted Kremenek42a509f2007-08-31 21:30:12 +00003104 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003105
Ted Kremenek7dba8602007-08-29 21:56:09 +00003106 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003107 OS << "\n [ B" << B.getBlockID();
3108
Ted Kremenek42a509f2007-08-31 21:30:12 +00003109 if (&B == &cfg->getEntry())
3110 OS << " (ENTRY) ]\n";
3111 else if (&B == &cfg->getExit())
3112 OS << " (EXIT) ]\n";
3113 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003114 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003115 else
3116 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003117
Ted Kremenek9cffe732007-08-29 23:20:49 +00003118 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003119 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003120
3121 if (print_edges)
3122 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003123
Mike Stump079bd722010-01-19 22:00:14 +00003124 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003125 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003126 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003127 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003128 C->getLHS()->printPretty(OS, Helper,
3129 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003130 if (C->getRHS()) {
3131 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003132 C->getRHS()->printPretty(OS, Helper,
3133 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003134 }
Mike Stump079bd722010-01-19 22:00:14 +00003135 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003136 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003137 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003138 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003139 if (CS->getExceptionDecl())
3140 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3141 0);
3142 else
3143 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003144 OS << ")";
3145
3146 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003147 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003148
Ted Kremenek9cffe732007-08-29 23:20:49 +00003149 OS << ":\n";
3150 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003151
Ted Kremenekfddd5182007-08-21 21:42:03 +00003152 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003153 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003154
Ted Kremenek42a509f2007-08-31 21:30:12 +00003155 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3156 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003157
Ted Kremenek9cffe732007-08-29 23:20:49 +00003158 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003159 if (print_edges)
3160 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003161
Ted Kremeneka95d3752008-09-13 05:16:45 +00003162 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003163
Ted Kremenek42a509f2007-08-31 21:30:12 +00003164 if (Helper)
3165 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003166
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003167 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003168 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003169
Ted Kremenek9cffe732007-08-29 23:20:49 +00003170 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003171 if (B.getTerminator()) {
3172 if (print_edges)
3173 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003174
Ted Kremenek9cffe732007-08-29 23:20:49 +00003175 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003176
Ted Kremenek42a509f2007-08-31 21:30:12 +00003177 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003178
Chris Lattnere4f21422009-06-30 01:26:17 +00003179 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3180 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003181 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003182 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003183 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003184
Ted Kremenek9cffe732007-08-29 23:20:49 +00003185 if (print_edges) {
3186 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003187 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003188 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003189
Ted Kremenek42a509f2007-08-31 21:30:12 +00003190 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3191 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003192
Ted Kremenek42a509f2007-08-31 21:30:12 +00003193 if (i == 8 || (i-8) == 0)
3194 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003195
Ted Kremenek9cffe732007-08-29 23:20:49 +00003196 OS << " B" << (*I)->getBlockID();
3197 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003198
Ted Kremenek42a509f2007-08-31 21:30:12 +00003199 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003200
Ted Kremenek42a509f2007-08-31 21:30:12 +00003201 // Print the successors of this block.
3202 OS << " Successors (" << B.succ_size() << "):";
3203 i = 0;
3204
3205 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3206 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003207
Ted Kremenek42a509f2007-08-31 21:30:12 +00003208 if (i == 8 || (i-8) % 10 == 0)
3209 OS << "\n ";
3210
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003211 if (*I)
3212 OS << " B" << (*I)->getBlockID();
3213 else
3214 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003215 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003216
Ted Kremenek9cffe732007-08-29 23:20:49 +00003217 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003218 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003219}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003220
Ted Kremenek42a509f2007-08-31 21:30:12 +00003221
3222/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003223void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003224
3225/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003226void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3227 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003228
Ted Kremenek42a509f2007-08-31 21:30:12 +00003229 // Print the entry block.
3230 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003231
Ted Kremenek42a509f2007-08-31 21:30:12 +00003232 // Iterate through the CFGBlocks and print them one by one.
3233 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3234 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003235 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003236 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003237
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003238 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003239 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003240
Ted Kremenek42a509f2007-08-31 21:30:12 +00003241 // Print the exit block.
3242 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003243 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003244}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003245
3246/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003247void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3248 print(llvm::errs(), cfg, LO);
3249}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003250
3251/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3252/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003253void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3254 const LangOptions &LO) const {
3255 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003256 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003257}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003258
Ted Kremeneka2925852008-01-30 23:02:42 +00003259/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003260void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003261 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003262 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003263 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003264}
3265
Ted Kremenek390e48b2008-11-12 21:11:49 +00003266Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003267 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003268 if (!Terminator)
3269 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003270
Ted Kremenek411cdee2008-04-16 21:10:48 +00003271 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003272
Ted Kremenek411cdee2008-04-16 21:10:48 +00003273 switch (Terminator->getStmtClass()) {
3274 default:
3275 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003276
Ted Kremenek411cdee2008-04-16 21:10:48 +00003277 case Stmt::ForStmtClass:
3278 E = cast<ForStmt>(Terminator)->getCond();
3279 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003280
Ted Kremenek411cdee2008-04-16 21:10:48 +00003281 case Stmt::WhileStmtClass:
3282 E = cast<WhileStmt>(Terminator)->getCond();
3283 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003284
Ted Kremenek411cdee2008-04-16 21:10:48 +00003285 case Stmt::DoStmtClass:
3286 E = cast<DoStmt>(Terminator)->getCond();
3287 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003288
Ted Kremenek411cdee2008-04-16 21:10:48 +00003289 case Stmt::IfStmtClass:
3290 E = cast<IfStmt>(Terminator)->getCond();
3291 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003292
Ted Kremenek411cdee2008-04-16 21:10:48 +00003293 case Stmt::ChooseExprClass:
3294 E = cast<ChooseExpr>(Terminator)->getCond();
3295 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003296
Ted Kremenek411cdee2008-04-16 21:10:48 +00003297 case Stmt::IndirectGotoStmtClass:
3298 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3299 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003300
Ted Kremenek411cdee2008-04-16 21:10:48 +00003301 case Stmt::SwitchStmtClass:
3302 E = cast<SwitchStmt>(Terminator)->getCond();
3303 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003304
Ted Kremenek411cdee2008-04-16 21:10:48 +00003305 case Stmt::ConditionalOperatorClass:
3306 E = cast<ConditionalOperator>(Terminator)->getCond();
3307 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003308
Ted Kremenek411cdee2008-04-16 21:10:48 +00003309 case Stmt::BinaryOperatorClass: // '&&' and '||'
3310 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003311 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003312
Ted Kremenek390e48b2008-11-12 21:11:49 +00003313 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003314 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003315 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003316
Ted Kremenek411cdee2008-04-16 21:10:48 +00003317 return E ? E->IgnoreParens() : NULL;
3318}
3319
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003320bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003321 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003322 if (!Terminator)
3323 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003324
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003325 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003326
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003327 switch (Terminator->getStmtClass()) {
3328 default:
3329 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003330
3331 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003332 case Stmt::WhileStmtClass:
3333 case Stmt::DoStmtClass:
3334 case Stmt::IfStmtClass:
3335 case Stmt::ChooseExprClass:
3336 case Stmt::ConditionalOperatorClass:
3337 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003338 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003339 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003340
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003341 return E ? E->IgnoreParens() : NULL;
3342}
3343
Ted Kremeneka2925852008-01-30 23:02:42 +00003344
Ted Kremenek7dba8602007-08-29 21:56:09 +00003345//===----------------------------------------------------------------------===//
3346// CFG Graphviz Visualization
3347//===----------------------------------------------------------------------===//
3348
Ted Kremenek42a509f2007-08-31 21:30:12 +00003349
3350#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003351static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003352#endif
3353
Chris Lattnere4f21422009-06-30 01:26:17 +00003354void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003355#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003356 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003357 GraphHelper = &H;
3358 llvm::ViewGraph(this,"CFG");
3359 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003360#endif
3361}
3362
Ted Kremenek7dba8602007-08-29 21:56:09 +00003363namespace llvm {
3364template<>
3365struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003366
3367 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3368
3369 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003370
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003371#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003372 std::string OutSStr;
3373 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003374 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003375 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003376
3377 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3378
3379 // Process string output to make it nicer...
3380 for (unsigned i = 0; i != OutStr.length(); ++i)
3381 if (OutStr[i] == '\n') { // Left justify
3382 OutStr[i] = '\\';
3383 OutStr.insert(OutStr.begin()+i+1, 'l');
3384 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003385
Ted Kremenek7dba8602007-08-29 21:56:09 +00003386 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003387#else
3388 return "";
3389#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003390 }
3391};
3392} // end namespace llvm