blob: 35d93867c166c123ebaa66b3959457df3f67c7e9 [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
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000039/// The CFG builder uses a recursive algorithm to build the CFG. When
40/// we process an expression, sometimes we know that we must add the
41/// subexpressions as block-level expressions. For example:
42///
43/// exp1 || exp2
44///
45/// When processing the '||' expression, we know that exp1 and exp2
46/// need to be added as block-level expressions, even though they
47/// might not normally need to be. AddStmtChoice records this
48/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
49/// the builder has an option not to add a subexpression as a
50/// block-level expression.
51///
52/// The lvalue bit captures whether or not a subexpression needs to
53/// be processed as an lvalue. That information needs to be recorded
54/// in the CFG for block-level expressions so that analyses do the
55/// right thing when traversing the CFG (since such subexpressions
56/// will be seen before their parent expression is processed).
Ted Kremenek852274d2009-12-16 03:18:58 +000057class AddStmtChoice {
58public:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000059 enum Kind { NotAlwaysAdd = 0,
60 AlwaysAdd = 1,
61 AsLValueNotAlwaysAdd = 2,
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000062 AsLValueAlwaysAdd = 3 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000063
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000064 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000065
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000066 bool alwaysAdd() const { return kind & AlwaysAdd; }
67 bool asLValue() const { return kind >= AsLValueNotAlwaysAdd; }
68
69 /// Return a copy of this object, except with the 'always-add' bit
70 /// set as specified.
71 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
72 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
73 Kind(kind & ~AlwaysAdd));
74 }
75
76 /// Return a copy of this object, except with the 'as-lvalue' bit
77 /// set as specified.
78 AddStmtChoice withAsLValue(bool asLVal) const {
79 return AddStmtChoice(asLVal ? Kind(kind | AsLValueNotAlwaysAdd) :
80 Kind(kind & ~AsLValueNotAlwaysAdd));
81 }
Ted Kremenek5ba290a2010-03-02 21:43:54 +000082
Ted Kremenek852274d2009-12-16 03:18:58 +000083private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000084 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000085};
Mike Stump6d9828c2009-07-17 01:31:16 +000086
Marcin Swiderskif1308c72010-09-25 11:05:21 +000087/// LocalScope - Node in tree of local scopes created for C++ implicit
88/// destructor calls generation. It contains list of automatic variables
89/// declared in the scope and link to position in previous scope this scope
90/// began in.
91///
92/// The process of creating local scopes is as follows:
93/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
94/// - Before processing statements in scope (e.g. CompoundStmt) create
95/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
96/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000097/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000098/// at this VarDecl,
99/// - For every normal (without jump) end of scope add to CFGBlock destructors
100/// for objects in the current scope,
101/// - For every jump add to CFGBlock destructors for objects
102/// between CFGBuilder::ScopePos and local scope position saved for jump
103/// target. Thanks to C++ restrictions on goto jumps we can be sure that
104/// jump target position will be on the path to root from CFGBuilder::ScopePos
105/// (adding any variable that doesn't need constructor to be called to
106/// LocalScope can break this assumption),
107///
108class LocalScope {
109public:
110 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
111
112 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +0000113 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000114 class const_iterator {
115 const LocalScope* Scope;
116
117 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
118 /// Invalid iterator (with null Scope) has VarIter equal to 0.
119 unsigned VarIter;
120
121 public:
122 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
123 /// Incrementing invalid iterator is allowed and will result in invalid
124 /// iterator.
125 const_iterator()
126 : Scope(NULL), VarIter(0) {}
127
128 /// Create valid iterator. In case when S.Prev is an invalid iterator and
129 /// I is equal to 0, this will create invalid iterator.
130 const_iterator(const LocalScope& S, unsigned I)
131 : Scope(&S), VarIter(I) {
132 // Iterator to "end" of scope is not allowed. Handle it by going up
133 // in scopes tree possibly up to invalid iterator in the root.
134 if (VarIter == 0 && Scope)
135 *this = Scope->Prev;
136 }
137
138 VarDecl* const* operator->() const {
139 assert (Scope && "Dereferencing invalid iterator is not allowed");
140 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
141 return &Scope->Vars[VarIter - 1];
142 }
143 VarDecl* operator*() const {
144 return *this->operator->();
145 }
146
147 const_iterator& operator++() {
148 if (!Scope)
149 return *this;
150
151 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
152 --VarIter;
153 if (VarIter == 0)
154 *this = Scope->Prev;
155 return *this;
156 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000157 const_iterator operator++(int) {
158 const_iterator P = *this;
159 ++*this;
160 return P;
161 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000162
163 bool operator==(const const_iterator& rhs) const {
164 return Scope == rhs.Scope && VarIter == rhs.VarIter;
165 }
166 bool operator!=(const const_iterator& rhs) const {
167 return !(*this == rhs);
168 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000169
170 operator bool() const {
171 return *this != const_iterator();
172 }
173
174 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000175 };
176
177 friend class const_iterator;
178
179private:
180 /// Automatic variables in order of declaration.
181 AutomaticVarsTy Vars;
182 /// Iterator to variable in previous scope that was declared just before
183 /// begin of this scope.
184 const_iterator Prev;
185
186public:
187 /// Constructs empty scope linked to previous scope in specified place.
188 LocalScope(const_iterator P)
189 : Vars()
190 , Prev(P) {}
191
192 /// Begin of scope in direction of CFG building (backwards).
193 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000194
195 void addVar(VarDecl* VD) {
196 Vars.push_back(VD);
197 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000198};
199
Marcin Swiderski35387a02010-09-30 22:42:32 +0000200/// distance - Calculates distance from this to L. L must be reachable from this
201/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
202/// number of scopes between this and L.
203int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
204 int D = 0;
205 const_iterator F = *this;
206 while (F.Scope != L.Scope) {
207 assert (F != const_iterator()
208 && "L iterator is not reachable from F iterator.");
209 D += F.VarIter;
210 F = F.Scope->Prev;
211 }
212 D += F.VarIter - L.VarIter;
213 return D;
214}
215
216/// BlockScopePosPair - Structure for specifying position in CFG during its
217/// build process. It consists of CFGBlock that specifies position in CFG graph
218/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000219struct BlockScopePosPair {
220 BlockScopePosPair() {}
221 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
222 : Block(B), ScopePos(S) {}
223
224 CFGBlock* Block;
225 LocalScope::const_iterator ScopePos;
226};
227
Ted Kremeneka34ea072008-08-04 22:51:42 +0000228/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000229/// The builder is stateful: an instance of the builder should be used to only
230/// construct a single CFG.
231///
232/// Example usage:
233///
234/// CFGBuilder builder;
235/// CFG* cfg = builder.BuildAST(stmt1);
236///
Mike Stump6d9828c2009-07-17 01:31:16 +0000237/// CFG construction is done via a recursive walk of an AST. We actually parse
238/// the AST in reverse order so that the successor of a basic block is
239/// constructed prior to its predecessor. This allows us to nicely capture
240/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000241///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000242class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000243 typedef BlockScopePosPair JumpTarget;
244 typedef BlockScopePosPair JumpSource;
245
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000246 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000247 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000248
Ted Kremenekfddd5182007-08-21 21:42:03 +0000249 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000250 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000251 JumpTarget ContinueJumpTarget;
252 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000253 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000254 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000255 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000256
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000257 // Current position in local scope.
258 LocalScope::const_iterator ScopePos;
259
260 // LabelMap records the mapping from Label expressions to their jump targets.
261 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000262 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000263
264 // A list of blocks that end with a "goto" that must be backpatched to their
265 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000266 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000267 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000268
Ted Kremenek19bb3562007-08-28 19:26:49 +0000269 // A list of labels whose address has been taken (for indirect gotos).
270 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
271 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000272
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000273 bool badCFG;
274 CFG::BuildOptions BuildOpts;
275
Mike Stump6d9828c2009-07-17 01:31:16 +0000276public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000277 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
278 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000279 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000280 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000281
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000282 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000283 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000284 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000285
Ted Kremenek4f880632009-07-17 22:18:43 +0000286private:
287 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000288 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
289 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
290 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000291 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000292 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000293 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000294 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000295 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
296 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000297 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
298 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000299 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000300 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
301 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000302 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
303 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000304 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000305 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000306 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000307 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000308 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000309 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000310 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000311 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000312 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000313 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
314 CFGBlock *VisitDoStmt(DoStmt *D);
315 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000316 CFGBlock *VisitGotoStmt(GotoStmt* G);
317 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000318 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000319 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
320 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000321 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000322 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
323 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
324 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
325 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
326 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
327 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000328 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
329 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000330 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000331 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000332 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000333
Ted Kremenek852274d2009-12-16 03:18:58 +0000334 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
335 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000336 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000337
Marcin Swiderski8599e762010-11-03 06:19:35 +0000338 // Visitors to walk an AST and generate destructors of temporaries in
339 // full expression.
340 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
341 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
342 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
343 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
344 bool BindToTemporary);
345 CFGBlock *VisitConditionalOperatorForTemporaryDtors(ConditionalOperator *E,
346 bool BindToTemporary);
347
Ted Kremenek274f4332008-04-28 18:00:46 +0000348 // NYS == Not Yet Supported
349 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000350 badCFG = true;
351 return Block;
352 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000353
Ted Kremenek4f880632009-07-17 22:18:43 +0000354 void autoCreateBlock() { if (!Block) Block = createBlock(); }
355 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000356
Zhongxing Xudf119892010-06-03 06:43:23 +0000357 CFGBlock *addStmt(Stmt *S) {
358 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000359 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000360 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000361 void addAutomaticObjDtors(LocalScope::const_iterator B,
362 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000363 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000364
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000365 // Local scopes creation.
366 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
367
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000368 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000369 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
370 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
371
372 void addLocalScopeAndDtors(Stmt* S);
373
374 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek852274d2009-12-16 03:18:58 +0000375 void AppendStmt(CFGBlock *B, Stmt *S,
376 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
377 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000378 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000379 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
380 B->appendInitializer(I, cfg->getBumpVectorContext());
381 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000382 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
383 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
384 }
385 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
386 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
387 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000388 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
389 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
390 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000391
Marcin Swiderski53de1342010-09-30 22:54:37 +0000392 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
393 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
394 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
395 LocalScope::const_iterator E, Stmt* S);
396 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
397 LocalScope::const_iterator B, LocalScope::const_iterator E);
398
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000399 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
400 B->addSuccessor(S, cfg->getBumpVectorContext());
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000403 /// TryResult - a class representing a variant over the values
404 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
405 /// and is used by the CFGBuilder to decide if a branch condition
406 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000407 class TryResult {
408 int X;
409 public:
410 TryResult(bool b) : X(b ? 1 : 0) {}
411 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek941fde82009-07-24 04:47:11 +0000413 bool isTrue() const { return X == 1; }
414 bool isFalse() const { return X == 0; }
415 bool isKnown() const { return X >= 0; }
416 void negate() {
417 assert(isKnown());
418 X ^= 0x1;
419 }
420 };
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Mike Stump00998a02009-07-23 23:25:26 +0000422 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
423 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000424 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000425 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000426 return TryResult();
427
Mike Stump00998a02009-07-23 23:25:26 +0000428 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000429 if (!S->isTypeDependent() && !S->isValueDependent() &&
430 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000431 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000432
433 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000434 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000435};
Mike Stump6d9828c2009-07-17 01:31:16 +0000436
Douglas Gregor898574e2008-12-05 23:32:09 +0000437// FIXME: Add support for dependent-sized array types in C++?
438// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000439static VariableArrayType* FindVA(Type* t) {
440 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
441 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
442 if (vat->getSizeExpr())
443 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000444
Ted Kremenek610a09e2008-09-26 22:58:57 +0000445 t = vt->getElementType().getTypePtr();
446 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000447
Ted Kremenek610a09e2008-09-26 22:58:57 +0000448 return 0;
449}
Mike Stump6d9828c2009-07-17 01:31:16 +0000450
451/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
452/// arbitrary statement. Examples include a single expression or a function
453/// body (compound statement). The ownership of the returned CFG is
454/// transferred to the caller. If CFG construction fails, this method returns
455/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000456CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000457 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000458
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000459 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000460 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000461 if (!Statement)
462 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000463
Ted Kremenek6c52c782010-09-14 23:41:16 +0000464 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000465
466 // Create an empty block that will serve as the exit block for the CFG. Since
467 // this is the first block added to the CFG, it will be implicitly registered
468 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000469 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000470 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000471 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000472
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000473 if (BuildOpts.AddImplicitDtors)
474 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
475 addImplicitDtorsForDestructor(DD);
476
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000477 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000478 CFGBlock *B = addStmt(Statement);
479
480 if (badCFG)
481 return NULL;
482
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000483 // For C++ constructor add initializers to CFG.
484 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
485 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
486 E = CD->init_rend(); I != E; ++I) {
487 B = addInitializer(*I);
488 if (badCFG)
489 return NULL;
490 }
491 }
492
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000493 if (B)
494 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000495
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000496 // Backpatch the gotos whose label -> block mappings we didn't know when we
497 // encountered them.
498 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
499 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000500
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000501 CFGBlock* B = I->Block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000502 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
503 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000504
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000505 // If there is no target for the goto, then we are looking at an
506 // incomplete AST. Handle this by not registering a successor.
507 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000508
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000509 JumpTarget JT = LI->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000510 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000511 AddSuccessor(B, JT.Block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000512 }
513
514 // Add successors to the Indirect Goto Dispatch block (if we have one).
515 if (CFGBlock* B = cfg->getIndirectGotoBlock())
516 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
517 E = AddressTakenLabels.end(); I != E; ++I ) {
518
519 // Lookup the target block.
520 LabelMapTy::iterator LI = LabelMap.find(*I);
521
522 // If there is no target block that contains label, then we are looking
523 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000524 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000525
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000526 AddSuccessor(B, LI->second.Block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000527 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000528
Mike Stump6d9828c2009-07-17 01:31:16 +0000529 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000530 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000531
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000532 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000533}
Mike Stump6d9828c2009-07-17 01:31:16 +0000534
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000535/// createBlock - Used to lazily create blocks that are connected
536/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000537CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000538 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000539 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000540 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000541 return B;
542}
Mike Stump6d9828c2009-07-17 01:31:16 +0000543
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000544/// addInitializer - Add C++ base or member initializer element to CFG.
545CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
546 if (!BuildOpts.AddInitializers)
547 return Block;
548
Marcin Swiderski8599e762010-11-03 06:19:35 +0000549 bool IsReference = false;
550 bool HasTemporaries = false;
551
552 // Destructors of temporaries in initialization expression should be called
553 // after initialization finishes.
554 Expr *Init = I->getInit();
555 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000556 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000557 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000558 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000559
560 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
561 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000562 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000563 IsReference);
564 }
565 }
566
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000567 autoCreateBlock();
568 appendInitializer(Block, I);
569
Marcin Swiderski8599e762010-11-03 06:19:35 +0000570 if (Init) {
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +0000571 AddStmtChoice asc = AddStmtChoice().withAsLValue(IsReference);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000572 if (HasTemporaries)
573 // For expression with temporaries go directly to subexpression to omit
574 // generating destructors for the second time.
John McCall4765fa02010-12-06 08:20:24 +0000575 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr(), asc);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000576 return Visit(Init, asc);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000577 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000578
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000579 return Block;
580}
581
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000582/// addAutomaticObjDtors - Add to current block automatic objects destructors
583/// for objects in range of local scope positions. Use S as trigger statement
584/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000585void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
586 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000587 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000588 return;
589
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000590 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000591 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000592
593 autoCreateBlock();
594 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000595}
596
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000597/// addImplicitDtorsForDestructor - Add implicit destructors generated for
598/// base and member objects in destructor.
599void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
600 assert (BuildOpts.AddImplicitDtors
601 && "Can be called only when dtors should be added");
602 const CXXRecordDecl *RD = DD->getParent();
603
604 // At the end destroy virtual base objects.
605 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
606 VE = RD->vbases_end(); VI != VE; ++VI) {
607 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
608 if (!CD->hasTrivialDestructor()) {
609 autoCreateBlock();
610 appendBaseDtor(Block, VI);
611 }
612 }
613
614 // Before virtual bases destroy direct base objects.
615 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
616 BE = RD->bases_end(); BI != BE; ++BI) {
617 if (!BI->isVirtual()) {
618 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
619 if (!CD->hasTrivialDestructor()) {
620 autoCreateBlock();
621 appendBaseDtor(Block, BI);
622 }
623 }
624 }
625
626 // First destroy member objects.
627 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
628 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000629 // Check for constant size array. Set type to array element type.
630 QualType QT = FI->getType();
631 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
632 if (AT->getSize() == 0)
633 continue;
634 QT = AT->getElementType();
635 }
636
637 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000638 if (!CD->hasTrivialDestructor()) {
639 autoCreateBlock();
640 appendMemberDtor(Block, *FI);
641 }
642 }
643}
644
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000645/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
646/// way return valid LocalScope object.
647LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
648 if (!Scope) {
649 Scope = cfg->getAllocator().Allocate<LocalScope>();
650 new (Scope) LocalScope(ScopePos);
651 }
652 return Scope;
653}
654
655/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000656/// that should create implicit scope (e.g. if/else substatements).
657void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000658 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000659 return;
660
661 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000662
663 // For compound statement we will be creating explicit scope.
664 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
665 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
666 ; BI != BE; ++BI) {
667 Stmt* SI = *BI;
668 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
669 SI = LS->getSubStmt();
670 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
671 Scope = addLocalScopeForDeclStmt(DS, Scope);
672 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000673 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000674 }
675
676 // For any other statement scope will be implicit and as such will be
677 // interesting only for DeclStmt.
678 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
679 S = LS->getSubStmt();
680 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000681 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000682}
683
684/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
685/// reuse Scope if not NULL.
686LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000687 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000688 if (!BuildOpts.AddImplicitDtors)
689 return Scope;
690
691 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
692 ; DI != DE; ++DI) {
693 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
694 Scope = addLocalScopeForVarDecl(VD, Scope);
695 }
696 return Scope;
697}
698
699/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
700/// create add scope for automatic objects and temporary objects bound to
701/// const reference. Will reuse Scope if not NULL.
702LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000703 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000704 if (!BuildOpts.AddImplicitDtors)
705 return Scope;
706
707 // Check if variable is local.
708 switch (VD->getStorageClass()) {
709 case SC_None:
710 case SC_Auto:
711 case SC_Register:
712 break;
713 default: return Scope;
714 }
715
716 // Check for const references bound to temporary. Set type to pointee.
717 QualType QT = VD->getType();
718 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
719 QT = RT->getPointeeType();
720 if (!QT.isConstQualified())
721 return Scope;
722 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
723 return Scope;
724 }
725
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000726 // Check for constant size array. Set type to array element type.
727 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
728 if (AT->getSize() == 0)
729 return Scope;
730 QT = AT->getElementType();
731 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000732
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000733 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000734 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
735 if (!CD->hasTrivialDestructor()) {
736 // Add the variable to scope
737 Scope = createOrReuseLocalScope(Scope);
738 Scope->addVar(VD);
739 ScopePos = Scope->begin();
740 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000741 return Scope;
742}
743
744/// addLocalScopeAndDtors - For given statement add local scope for it and
745/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
746void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
747 if (!BuildOpts.AddImplicitDtors)
748 return;
749
750 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000751 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000752 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
753}
754
Marcin Swiderski53de1342010-09-30 22:54:37 +0000755/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
756/// automatic storage duration to CFGBlock's elements vector. Insertion will be
757/// performed in place specified with iterator.
758void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
759 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
760 BumpVectorContext& C = cfg->getBumpVectorContext();
761 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
762 while (B != E)
763 I = Blk->insertAutomaticObjDtor(I, *B++, S);
764}
765
766/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
767/// automatic storage duration to CFGBlock's elements vector. Elements will be
768/// appended to physical end of the vector which happens to be logical
769/// beginning.
770void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
771 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
772 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
773}
774
775/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
776/// variables with automatic storage duration to CFGBlock's elements vector.
777/// Elements will be prepended to physical beginning of the vector which
778/// happens to be logical end. Use blocks terminator as statement that specifies
779/// destructors call site.
780void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
781 LocalScope::const_iterator B, LocalScope::const_iterator E) {
782 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
783}
784
Ted Kremenek4f880632009-07-17 22:18:43 +0000785/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000786/// blocks for ternary operators, &&, and ||. We also process "," and
787/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000788CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000789tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000790 if (!S) {
791 badCFG = true;
792 return 0;
793 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 switch (S->getStmtClass()) {
795 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000796 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000797
798 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000799 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenek4f880632009-07-17 22:18:43 +0000801 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000802 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek4f880632009-07-17 22:18:43 +0000804 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000805 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000806
Ted Kremenek4f880632009-07-17 22:18:43 +0000807 case Stmt::BreakStmtClass:
808 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek4f880632009-07-17 22:18:43 +0000810 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000811 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000812 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 case Stmt::CaseStmtClass:
815 return VisitCaseStmt(cast<CaseStmt>(S));
816
817 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000818 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek4f880632009-07-17 22:18:43 +0000820 case Stmt::CompoundStmtClass:
821 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000824 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenek4f880632009-07-17 22:18:43 +0000826 case Stmt::ContinueStmtClass:
827 return VisitContinueStmt(cast<ContinueStmt>(S));
John McCallf6a16482010-12-04 03:47:34 +0000828
829 case Stmt::CStyleCastExprClass: {
830 CastExpr *castExpr = cast<CastExpr>(S);
831 if (castExpr->getCastKind() == CK_LValueToRValue) {
832 // temporary workaround
833 S = castExpr->getSubExpr();
834 goto tryAgain;
835 }
836 return VisitStmt(S, asc);
837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek021c8af2010-01-19 20:40:33 +0000839 case Stmt::CXXCatchStmtClass:
840 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
841
John McCall4765fa02010-12-06 08:20:24 +0000842 case Stmt::ExprWithCleanupsClass:
843 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000844
Zhongxing Xua725ed42010-11-01 13:04:58 +0000845 case Stmt::CXXBindTemporaryExprClass:
846 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
847
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000848 case Stmt::CXXConstructExprClass:
849 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
850
Zhongxing Xua725ed42010-11-01 13:04:58 +0000851 case Stmt::CXXFunctionalCastExprClass:
852 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
853
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000854 case Stmt::CXXTemporaryObjectExprClass:
855 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
856
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000857 case Stmt::CXXMemberCallExprClass:
858 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
859
Ted Kremenek021c8af2010-01-19 20:40:33 +0000860 case Stmt::CXXThrowExprClass:
861 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000862
Ted Kremenek021c8af2010-01-19 20:40:33 +0000863 case Stmt::CXXTryStmtClass:
864 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000865
Ted Kremenek4f880632009-07-17 22:18:43 +0000866 case Stmt::DeclStmtClass:
867 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Ted Kremenek4f880632009-07-17 22:18:43 +0000869 case Stmt::DefaultStmtClass:
870 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek4f880632009-07-17 22:18:43 +0000872 case Stmt::DoStmtClass:
873 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek4f880632009-07-17 22:18:43 +0000875 case Stmt::ForStmtClass:
876 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek4f880632009-07-17 22:18:43 +0000878 case Stmt::GotoStmtClass:
879 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek4f880632009-07-17 22:18:43 +0000881 case Stmt::IfStmtClass:
882 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000883
John McCallf6a16482010-12-04 03:47:34 +0000884 case Stmt::ImplicitCastExprClass: {
885 ImplicitCastExpr *castExpr = cast<ImplicitCastExpr>(S);
886 if (castExpr->getCastKind() == CK_LValueToRValue) {
887 // temporary workaround
888 S = castExpr->getSubExpr();
889 goto tryAgain;
890 }
891 return VisitImplicitCastExpr(castExpr, asc);
892 }
Zhongxing Xua725ed42010-11-01 13:04:58 +0000893
Ted Kremenek4f880632009-07-17 22:18:43 +0000894 case Stmt::IndirectGotoStmtClass:
895 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenek4f880632009-07-17 22:18:43 +0000897 case Stmt::LabelStmtClass:
898 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek115c1b92010-04-11 17:02:10 +0000900 case Stmt::MemberExprClass:
901 return VisitMemberExpr(cast<MemberExpr>(S), asc);
902
Ted Kremenek4f880632009-07-17 22:18:43 +0000903 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000904 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
905
Ted Kremenek4f880632009-07-17 22:18:43 +0000906 case Stmt::ObjCAtSynchronizedStmtClass:
907 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Ted Kremenek4f880632009-07-17 22:18:43 +0000909 case Stmt::ObjCAtThrowStmtClass:
910 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Ted Kremenek4f880632009-07-17 22:18:43 +0000912 case Stmt::ObjCAtTryStmtClass:
913 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Ted Kremenek4f880632009-07-17 22:18:43 +0000915 case Stmt::ObjCForCollectionStmtClass:
916 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Ted Kremenek4f880632009-07-17 22:18:43 +0000918 case Stmt::ParenExprClass:
919 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000920 goto tryAgain;
921
Ted Kremenek4f880632009-07-17 22:18:43 +0000922 case Stmt::NullStmtClass:
923 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Ted Kremenek4f880632009-07-17 22:18:43 +0000925 case Stmt::ReturnStmtClass:
926 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek4f880632009-07-17 22:18:43 +0000928 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000929 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek4f880632009-07-17 22:18:43 +0000931 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000932 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek4f880632009-07-17 22:18:43 +0000934 case Stmt::SwitchStmtClass:
935 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000937 case Stmt::UnaryOperatorClass:
938 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
939
Ted Kremenek4f880632009-07-17 22:18:43 +0000940 case Stmt::WhileStmtClass:
941 return VisitWhileStmt(cast<WhileStmt>(S));
942 }
943}
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremenek852274d2009-12-16 03:18:58 +0000945CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
946 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000947 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000948 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000949 }
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek4f880632009-07-17 22:18:43 +0000951 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000952}
Mike Stump6d9828c2009-07-17 01:31:16 +0000953
Ted Kremenek4f880632009-07-17 22:18:43 +0000954/// VisitChildren - Visit the children of a Stmt.
955CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
956 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000957 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000958 E = Terminator->child_end(); I != E; ++I) {
959 if (*I) B = Visit(*I);
960 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000961 return B;
962}
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Ted Kremenek852274d2009-12-16 03:18:58 +0000964CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
965 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000966 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000967
Ted Kremenek852274d2009-12-16 03:18:58 +0000968 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000969 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000970 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000971 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000972
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000973 return Block;
974}
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000976CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
977 AddStmtChoice asc) {
978 if (asc.alwaysAdd()) {
979 autoCreateBlock();
980 AppendStmt(Block, U, asc);
981 }
982
983 bool asLVal = U->isIncrementDecrementOp();
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +0000984 return Visit(U->getSubExpr(), AddStmtChoice().withAsLValue(asLVal));
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000985}
986
Ted Kremenek852274d2009-12-16 03:18:58 +0000987CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
988 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000989 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000990 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000991 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000993 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000994 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek4f880632009-07-17 22:18:43 +0000996 // create the block evaluating the LHS
997 CFGBlock* LHSBlock = createBlock(false);
998 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek4f880632009-07-17 22:18:43 +00001000 // create the block evaluating the RHS
1001 Succ = ConfluenceBlock;
1002 Block = NULL;
1003 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +00001004
1005 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001006 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +00001007 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001008 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +00001009 // Create an empty block for cases where the RHS doesn't require
1010 // any explicit statements in the CFG.
1011 RHSBlock = createBlock();
1012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Mike Stump00998a02009-07-23 23:25:26 +00001014 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001015 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +00001016 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001017 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001018
Ted Kremenek4f880632009-07-17 22:18:43 +00001019 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001020 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001021 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1022 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001023 } else {
John McCall2de56d12010-08-25 11:45:40 +00001024 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001025 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1026 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek4f880632009-07-17 22:18:43 +00001029 // Generate the blocks for evaluating the LHS.
1030 Block = LHSBlock;
1031 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +00001032 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001033
1034 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001035 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001036 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001037 addStmt(B->getRHS());
1038 return addStmt(B->getLHS());
1039 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001040
1041 if (B->isAssignmentOp()) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001042 if (asc.alwaysAdd()) {
1043 autoCreateBlock();
1044 AppendStmt(Block, B, asc);
1045 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001046
Marcin Swiderskie1667192010-10-24 08:21:40 +00001047 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
1048 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Marcin Swiderskie1667192010-10-24 08:21:40 +00001051 if (asc.alwaysAdd()) {
1052 autoCreateBlock();
1053 AppendStmt(Block, B, asc);
1054 }
1055
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001056 CFGBlock *RBlock = Visit(B->getRHS());
1057 CFGBlock *LBlock = Visit(B->getLHS());
1058 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1059 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1060 // return RBlock. Otherwise we'll incorrectly return NULL.
1061 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001062}
1063
Ted Kremenek852274d2009-12-16 03:18:58 +00001064CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1065 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001066 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001067 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001068 }
1069 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001070}
1071
Ted Kremenek4f880632009-07-17 22:18:43 +00001072CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1073 // "break" is a control-flow statement. Thus we stop processing the current
1074 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001075 if (badCFG)
1076 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Ted Kremenek4f880632009-07-17 22:18:43 +00001078 // Now create a new block that ends with the break statement.
1079 Block = createBlock(false);
1080 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Ted Kremenek4f880632009-07-17 22:18:43 +00001082 // If there is no target for the break, then we are looking at an incomplete
1083 // AST. This means that the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001084 if (BreakJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001085 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001086 AddSuccessor(Block, BreakJumpTarget.Block);
1087 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001088 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
1090
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001091 return Block;
1092}
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Mike Stump4c45aa12010-01-21 15:20:48 +00001094static bool CanThrow(Expr *E) {
1095 QualType Ty = E->getType();
1096 if (Ty->isFunctionPointerType())
1097 Ty = Ty->getAs<PointerType>()->getPointeeType();
1098 else if (Ty->isBlockPointerType())
1099 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001100
Mike Stump4c45aa12010-01-21 15:20:48 +00001101 const FunctionType *FT = Ty->getAs<FunctionType>();
1102 if (FT) {
1103 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1104 if (Proto->hasEmptyExceptionSpec())
1105 return false;
1106 }
1107 return true;
1108}
1109
Ted Kremenek852274d2009-12-16 03:18:58 +00001110CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001111 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001112 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001113 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001114 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001115 }
Mike Stump24556362009-07-25 21:26:53 +00001116
Mike Stump4c45aa12010-01-21 15:20:48 +00001117 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001118
1119 // Languages without exceptions are assumed to not throw.
1120 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001121 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001122 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001123 }
1124
1125 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001126 if (FD->hasAttr<NoReturnAttr>())
1127 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001128 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001129 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001130 }
Mike Stump24556362009-07-25 21:26:53 +00001131
Mike Stump4c45aa12010-01-21 15:20:48 +00001132 if (!CanThrow(C->getCallee()))
1133 AddEHEdge = false;
1134
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001135 if (!NoReturn && !AddEHEdge)
1136 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Mike Stump079bd722010-01-19 22:00:14 +00001138 if (Block) {
1139 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001140 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001141 return 0;
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Mike Stump079bd722010-01-19 22:00:14 +00001144 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +00001145 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001146
Mike Stump079bd722010-01-19 22:00:14 +00001147 if (NoReturn) {
1148 // Wire this to the exit block directly.
1149 AddSuccessor(Block, &cfg->getExit());
1150 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001151 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001152 // Add exceptional edges.
1153 if (TryTerminatedBlock)
1154 AddSuccessor(Block, TryTerminatedBlock);
1155 else
1156 AddSuccessor(Block, &cfg->getExit());
1157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Mike Stump24556362009-07-25 21:26:53 +00001159 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001160}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001161
Ted Kremenek852274d2009-12-16 03:18:58 +00001162CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1163 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001164 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001165 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001166 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001167 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001169 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001170 Succ = ConfluenceBlock;
1171 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001172 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001173 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001174 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001176 Succ = ConfluenceBlock;
1177 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001178 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001179 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001180 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001182 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001183 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001184 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001185 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1186 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001187 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001188 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001189}
Mike Stump1eb44332009-09-09 15:08:12 +00001190
1191
1192CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001193 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001194 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001195
1196 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1197 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001198 // If we hit a segment of code just containing ';' (NullStmts), we can
1199 // get a null block back. In such cases, just use the LastBlock
1200 if (CFGBlock *newBlock = addStmt(*I))
1201 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001203 if (badCFG)
1204 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001205 }
Mike Stump079bd722010-01-19 22:00:14 +00001206
Ted Kremenek4f880632009-07-17 22:18:43 +00001207 return LastBlock;
1208}
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Ted Kremenek852274d2009-12-16 03:18:58 +00001210CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1211 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001212 // Create the confluence block that will "merge" the results of the ternary
1213 // expression.
1214 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001215 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001216 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001217 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001219 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001220
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001221 // Create a block for the LHS expression if there is an LHS expression. A
1222 // GCC extension allows LHS to be NULL, causing the condition to be the
1223 // value that is returned instead.
1224 // e.g: x ?: y is shorthand for: x ? x : y;
1225 Succ = ConfluenceBlock;
1226 Block = NULL;
1227 CFGBlock* LHSBlock = NULL;
1228 if (C->getLHS()) {
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001229 LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001230 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001231 return 0;
1232 Block = NULL;
1233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001235 // Create the block for the RHS expression.
1236 Succ = ConfluenceBlock;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001237 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001238 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001239 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001241 // Create the block that will contain the condition.
1242 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Mike Stump00998a02009-07-23 23:25:26 +00001244 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001245 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001246 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001247 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001248 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +00001249 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001250 // If we know the condition is false, add NULL as the successor for
1251 // the block containing the condition. In this case, the confluence
1252 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001253 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +00001254 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001255 } else {
1256 // If we have no LHS expression, add the ConfluenceBlock as a direct
1257 // successor for the block containing the condition. Moreover, we need to
1258 // reverse the order of the predecessors in the ConfluenceBlock because
1259 // the RHSBlock will have been added to the succcessors already, and we
1260 // want the first predecessor to the the block containing the expression
1261 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001262 AddSuccessor(Block, ConfluenceBlock);
Ted Kremeneke4ae4dc2010-11-15 22:59:22 +00001263 // Note that there can possibly only be one predecessor if one of the
1264 // subexpressions resulted in calling a noreturn function.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001265 std::reverse(ConfluenceBlock->pred_begin(),
1266 ConfluenceBlock->pred_end());
1267 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001270 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001271 Block->setTerminator(C);
1272 return addStmt(C->getCond());
1273}
1274
Ted Kremenek4f880632009-07-17 22:18:43 +00001275CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001276 if (DS->isSingleDecl())
1277 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Ted Kremenek4f880632009-07-17 22:18:43 +00001279 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek4f880632009-07-17 22:18:43 +00001281 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1282 typedef llvm::SmallVector<Decl*,10> BufTy;
1283 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Ted Kremenek4f880632009-07-17 22:18:43 +00001285 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1286 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1287 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1288 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Ted Kremenek4f880632009-07-17 22:18:43 +00001290 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1291 // automatically freed with the CFG.
1292 DeclGroupRef DG(*I);
1293 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001294 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001295 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Ted Kremenek4f880632009-07-17 22:18:43 +00001297 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001298 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
1301 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001302}
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Ted Kremenek4f880632009-07-17 22:18:43 +00001304/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001305/// DeclStmts and initializers in them.
1306CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1307 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001308
Marcin Swiderski8599e762010-11-03 06:19:35 +00001309 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Marcin Swiderski8599e762010-11-03 06:19:35 +00001311 if (!VD) {
1312 autoCreateBlock();
1313 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001314 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Marcin Swiderski8599e762010-11-03 06:19:35 +00001317 bool IsReference = false;
1318 bool HasTemporaries = false;
1319
1320 // Destructors of temporaries in initialization expression should be called
1321 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001322 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001323 if (Init) {
1324 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001325 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001326
1327 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1328 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001329 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001330 IsReference);
1331 }
1332 }
1333
1334 autoCreateBlock();
1335 AppendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Ted Kremenek4f880632009-07-17 22:18:43 +00001337 if (Init) {
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001338 AddStmtChoice asc = AddStmtChoice().withAsLValue(IsReference);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001339 if (HasTemporaries)
1340 // For expression with temporaries go directly to subexpression to omit
1341 // generating destructors for the second time.
John McCall4765fa02010-12-06 08:20:24 +00001342 Visit(cast<ExprWithCleanups>(Init)->getSubExpr(), asc);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001343 else
1344 Visit(Init, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001345 }
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Ted Kremenek4f880632009-07-17 22:18:43 +00001347 // If the type of VD is a VLA, then we must process its size expressions.
1348 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1349 VA = FindVA(VA->getElementType().getTypePtr()))
1350 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001352 // Remove variable from local scope.
1353 if (ScopePos && VD == *ScopePos)
1354 ++ScopePos;
1355
Ted Kremenek4f880632009-07-17 22:18:43 +00001356 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357}
1358
1359CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001360 // We may see an if statement in the middle of a basic block, or it may be the
1361 // first statement we are processing. In either case, we create a new basic
1362 // block. First, we create the blocks for the then...else statements, and
1363 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001364 // middle of a block, we stop processing that block. That block is then the
1365 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001366
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001367 // Save local scope position because in case of condition variable ScopePos
1368 // won't be restored when traversing AST.
1369 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1370
1371 // Create local scope for possible condition variable.
1372 // Store scope position. Add implicit destructor.
1373 if (VarDecl* VD = I->getConditionVariable()) {
1374 LocalScope::const_iterator BeginScopePos = ScopePos;
1375 addLocalScopeForVarDecl(VD);
1376 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1377 }
1378
Mike Stump6d9828c2009-07-17 01:31:16 +00001379 // The block we were proccessing is now finished. Make it the successor
1380 // block.
1381 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001382 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001383 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001384 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001385 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001386
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001387 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001388 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001390 if (Stmt* Else = I->getElse()) {
1391 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001394 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001395 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001396
1397 // If branch is not a compound statement create implicit scope
1398 // and add destructors.
1399 if (!isa<CompoundStmt>(Else))
1400 addLocalScopeAndDtors(Else);
1401
Ted Kremenek4f880632009-07-17 22:18:43 +00001402 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001403
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001404 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1405 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001406 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001407 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001408 return 0;
1409 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001410 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001411
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001412 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001413 CFGBlock* ThenBlock;
1414 {
1415 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001416 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001417 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001418 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001419
1420 // If branch is not a compound statement create implicit scope
1421 // and add destructors.
1422 if (!isa<CompoundStmt>(Then))
1423 addLocalScopeAndDtors(Then);
1424
Ted Kremenek4f880632009-07-17 22:18:43 +00001425 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001426
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001427 if (!ThenBlock) {
1428 // We can reach here if the "then" body has all NullStmts.
1429 // Create an empty block so we can distinguish between true and false
1430 // branches in path-sensitive analyses.
1431 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001432 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001433 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001434 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001435 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001436 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001437 }
1438
Mike Stump6d9828c2009-07-17 01:31:16 +00001439 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001440 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001442 // Set the terminator of the new block to the If statement.
1443 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Mike Stump00998a02009-07-23 23:25:26 +00001445 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001446 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001447
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001448 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001449 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1450 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001451
1452 // Add the condition as the last statement in the new block. This may create
1453 // new blocks as the condition may contain control-flow. Any newly created
1454 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001455 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001456
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001457 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1458 // and the condition variable initialization to the CFG.
1459 if (VarDecl *VD = I->getConditionVariable()) {
1460 if (Expr *Init = VD->getInit()) {
1461 autoCreateBlock();
1462 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1463 addStmt(Init);
1464 }
1465 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001466
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001467 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001468}
Mike Stump6d9828c2009-07-17 01:31:16 +00001469
1470
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001471CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001472 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001473 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001474 // NOTE: If a "return" appears in the middle of a block, this means that the
1475 // code afterwards is DEAD (unreachable). We still keep a basic block
1476 // for that code; a simple "mark-and-sweep" from the entry block will be
1477 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001478
1479 // Create the new block.
1480 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001481
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001483 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001484 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001485
1486 // Add the return statement to the block. This may create new blocks if R
1487 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001488 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489}
1490
1491CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1492 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001493 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001494 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001495
Ted Kremenek4f880632009-07-17 22:18:43 +00001496 if (!LabelBlock) // This can happen when the body is empty, i.e.
1497 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001498
Ted Kremenek4f880632009-07-17 22:18:43 +00001499 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001500 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001501
1502 // Labels partition blocks, so this is the end of the basic block we were
1503 // processing (L is the block's label). Because this is label (and we have
1504 // already processed the substatement) there is no extra control-flow to worry
1505 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001506 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001507 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001508 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001509
1510 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001511 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001512
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001513 // This block is now the implicit successor of other blocks.
1514 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001515
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001516 return LabelBlock;
1517}
1518
1519CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001520 // Goto is a control-flow statement. Thus we stop processing the current
1521 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001522
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001523 Block = createBlock(false);
1524 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001525
1526 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001527 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001528
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001529 if (I == LabelMap.end())
1530 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001531 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1532 else {
1533 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001534 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001535 AddSuccessor(Block, JT.Block);
1536 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001537
Mike Stump6d9828c2009-07-17 01:31:16 +00001538 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001539}
1540
1541CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001542 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001543
Marcin Swiderski47575f12010-10-01 01:38:14 +00001544 // Save local scope position because in case of condition variable ScopePos
1545 // won't be restored when traversing AST.
1546 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1547
1548 // Create local scope for init statement and possible condition variable.
1549 // Add destructor for init statement and condition variable.
1550 // Store scope position for continue statement.
1551 if (Stmt* Init = F->getInit())
1552 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001553 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1554
Marcin Swiderski47575f12010-10-01 01:38:14 +00001555 if (VarDecl* VD = F->getConditionVariable())
1556 addLocalScopeForVarDecl(VD);
1557 LocalScope::const_iterator ContinueScopePos = ScopePos;
1558
1559 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1560
Mike Stumpfefb9f72009-07-21 01:12:51 +00001561 // "for" is a control-flow statement. Thus we stop processing the current
1562 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001563 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001564 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001565 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001566 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001567 } else
1568 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001569
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001570 // Save the current value for the break targets.
1571 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001572 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001573 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001574
Mike Stump6d9828c2009-07-17 01:31:16 +00001575 // Because of short-circuit evaluation, the condition of the loop can span
1576 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1577 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001578 CFGBlock* ExitConditionBlock = createBlock(false);
1579 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001580
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001581 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001582 ExitConditionBlock->setTerminator(F);
1583
1584 // Now add the actual condition to the condition block. Because the condition
1585 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001586 if (Stmt* C = F->getCond()) {
1587 Block = ExitConditionBlock;
1588 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001589 assert(Block == EntryConditionBlock ||
1590 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001591
1592 // If this block contains a condition variable, add both the condition
1593 // variable and initializer to the CFG.
1594 if (VarDecl *VD = F->getConditionVariable()) {
1595 if (Expr *Init = VD->getInit()) {
1596 autoCreateBlock();
1597 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1598 EntryConditionBlock = addStmt(Init);
1599 assert(Block == EntryConditionBlock);
1600 }
1601 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001602
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001603 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001604 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001605 return 0;
1606 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001607 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001608
Mike Stump6d9828c2009-07-17 01:31:16 +00001609 // The condition block is the implicit successor for the loop body as well as
1610 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001611 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001612
Mike Stump00998a02009-07-23 23:25:26 +00001613 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001614 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Mike Stump00998a02009-07-23 23:25:26 +00001616 if (F->getCond())
1617 KnownVal = TryEvaluateBool(F->getCond());
1618
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001619 // Now create the loop body.
1620 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001621 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001622
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001623 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001624 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1625 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001626
Ted Kremenekaf603f72007-08-30 18:39:40 +00001627 // Create a new block to contain the (bottom) of the loop body.
1628 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001629
1630 // Loop body should end with destructor of Condition variable (if any).
1631 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001632
Ted Kremeneke9334502008-09-04 21:48:47 +00001633 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001634 // Generate increment code in its own basic block. This is the target of
1635 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001636 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001637 } else {
1638 // No increment code. Create a special, empty, block that is used as the
1639 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001640 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001641 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001642 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001643
Ted Kremenek3575f842009-04-28 00:51:56 +00001644 // Finish up the increment (or empty) block if it hasn't been already.
1645 if (Block) {
1646 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001647 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001648 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001649 Block = 0;
1650 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Marcin Swiderski47575f12010-10-01 01:38:14 +00001652 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001653
Ted Kremenek3575f842009-04-28 00:51:56 +00001654 // The starting block for the loop increment is the block that should
1655 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001656 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001657
Marcin Swiderski47575f12010-10-01 01:38:14 +00001658 // If body is not a compound statement create implicit scope
1659 // and add destructors.
1660 if (!isa<CompoundStmt>(F->getBody()))
1661 addLocalScopeAndDtors(F->getBody());
1662
Mike Stump6d9828c2009-07-17 01:31:16 +00001663 // Now populate the body block, and in the process create new blocks as we
1664 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001665 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001666
1667 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001668 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001669 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001670 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001671
Ted Kremenek941fde82009-07-24 04:47:11 +00001672 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001673 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001674 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001675
Ted Kremenek941fde82009-07-24 04:47:11 +00001676 // Link up the condition block with the code that follows the loop. (the
1677 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001678 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001679
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001680 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001681 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001682 if (Stmt* I = F->getInit()) {
1683 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001684 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001685 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001686
1687 // There is no loop initialization. We are thus basically a while loop.
1688 // NULL out Block to force lazy block construction.
1689 Block = NULL;
1690 Succ = EntryConditionBlock;
1691 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001692}
1693
Ted Kremenek115c1b92010-04-11 17:02:10 +00001694CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1695 if (asc.alwaysAdd()) {
1696 autoCreateBlock();
1697 AppendStmt(Block, M, asc);
1698 }
1699 return Visit(M->getBase(),
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001700 AddStmtChoice().withAsLValue(!M->isArrow()));
Ted Kremenek115c1b92010-04-11 17:02:10 +00001701}
1702
Ted Kremenek514de5a2008-11-11 17:10:00 +00001703CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1704 // Objective-C fast enumeration 'for' statements:
1705 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1706 //
1707 // for ( Type newVariable in collection_expression ) { statements }
1708 //
1709 // becomes:
1710 //
1711 // prologue:
1712 // 1. collection_expression
1713 // T. jump to loop_entry
1714 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001715 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001716 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1717 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1718 // TB:
1719 // statements
1720 // T. jump to loop_entry
1721 // FB:
1722 // what comes after
1723 //
1724 // and
1725 //
1726 // Type existingItem;
1727 // for ( existingItem in expression ) { statements }
1728 //
1729 // becomes:
1730 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001731 // the same with newVariable replaced with existingItem; the binding works
1732 // the same except that for one ObjCForCollectionStmt::getElement() returns
1733 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001734 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001735
Ted Kremenek514de5a2008-11-11 17:10:00 +00001736 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001737
Ted Kremenek514de5a2008-11-11 17:10:00 +00001738 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001739 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001740 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001741 LoopSuccessor = Block;
1742 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001743 } else
1744 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001745
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001746 // Build the condition blocks.
1747 CFGBlock* ExitConditionBlock = createBlock(false);
1748 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001749
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001750 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001751 ExitConditionBlock->setTerminator(S);
1752
1753 // The last statement in the block should be the ObjCForCollectionStmt, which
1754 // performs the actual binding to 'element' and determines if there are any
1755 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001756 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001757 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001758
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001759 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001760 // generate new blocks as necesary. We DON'T add the statement by default to
1761 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001762 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001763 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001764 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001765 return 0;
1766 Block = 0;
1767 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001768
1769 // The condition block is the implicit successor for the loop body as well as
1770 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001771 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001772
Ted Kremenek514de5a2008-11-11 17:10:00 +00001773 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001774 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001775 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001776 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1777 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1778 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001779
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001780 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1781 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001782
Ted Kremenek4f880632009-07-17 22:18:43 +00001783 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001784
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001785 if (!BodyBlock)
1786 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001787 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001788 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001789 return 0;
1790 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001791
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001792 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001793 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001794 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001795
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001796 // Link up the condition block with the code that follows the loop.
1797 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001798 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001799
Ted Kremenek514de5a2008-11-11 17:10:00 +00001800 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001801 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001802 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001803}
1804
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001805CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1806 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001807
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001808 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001809 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001810
Ted Kremenekda5348e2009-05-05 23:11:51 +00001811 // The sync body starts its own basic block. This makes it a little easier
1812 // for diagnostic clients.
1813 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001814 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001815 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001816
Ted Kremenekda5348e2009-05-05 23:11:51 +00001817 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001818 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001819 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001820
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001821 // Add the @synchronized to the CFG.
1822 autoCreateBlock();
1823 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1824
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001825 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001826 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001827}
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001829CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001830 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001831 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001832}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001833
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001834CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001835 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001836
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001837 // Save local scope position because in case of condition variable ScopePos
1838 // won't be restored when traversing AST.
1839 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1840
1841 // Create local scope for possible condition variable.
1842 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001843 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001844 if (VarDecl* VD = W->getConditionVariable()) {
1845 addLocalScopeForVarDecl(VD);
1846 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1847 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001848
Mike Stumpfefb9f72009-07-21 01:12:51 +00001849 // "while" is a control-flow statement. Thus we stop processing the current
1850 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001851 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001852 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001853 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001854 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001855 } else
1856 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
1858 // Because of short-circuit evaluation, the condition of the loop can span
1859 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1860 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001861 CFGBlock* ExitConditionBlock = createBlock(false);
1862 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001863
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001864 // Set the terminator for the "exit" condition block.
1865 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
1867 // Now add the actual condition to the condition block. Because the condition
1868 // itself may contain control-flow, new blocks may be created. Thus we update
1869 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001870 if (Stmt* C = W->getCond()) {
1871 Block = ExitConditionBlock;
1872 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001873 // The condition might finish the current 'Block'.
1874 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001875
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001876 // If this block contains a condition variable, add both the condition
1877 // variable and initializer to the CFG.
1878 if (VarDecl *VD = W->getConditionVariable()) {
1879 if (Expr *Init = VD->getInit()) {
1880 autoCreateBlock();
1881 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1882 EntryConditionBlock = addStmt(Init);
1883 assert(Block == EntryConditionBlock);
1884 }
1885 }
1886
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001887 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001888 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001889 return 0;
1890 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001891 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001892
1893 // The condition block is the implicit successor for the loop body as well as
1894 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001895 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001896
Mike Stump00998a02009-07-23 23:25:26 +00001897 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001898 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001899
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001900 // Process the loop body.
1901 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001902 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001903
1904 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001905 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1906 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1907 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001908
Mike Stump6d9828c2009-07-17 01:31:16 +00001909 // Create an empty block to represent the transition block for looping back
1910 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001911 Block = 0;
1912 assert(Succ == EntryConditionBlock);
1913 Succ = createBlock();
1914 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001915 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001917 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001918 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001919
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001920 // NULL out Block to force lazy instantiation of blocks for the body.
1921 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001922
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001923 // Loop body should end with destructor of Condition variable (if any).
1924 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1925
1926 // If body is not a compound statement create implicit scope
1927 // and add destructors.
1928 if (!isa<CompoundStmt>(W->getBody()))
1929 addLocalScopeAndDtors(W->getBody());
1930
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001931 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001932 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001933
Ted Kremenekaf603f72007-08-30 18:39:40 +00001934 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001935 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001936 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001937 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001938 return 0;
1939 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001940
Ted Kremenek941fde82009-07-24 04:47:11 +00001941 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001942 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001943 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001944
Ted Kremenek941fde82009-07-24 04:47:11 +00001945 // Link up the condition block with the code that follows the loop. (the
1946 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001947 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
1949 // There can be no more statements in the condition block since we loop back
1950 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001951 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001952
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001953 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001954 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001955 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001956}
Mike Stump1eb44332009-09-09 15:08:12 +00001957
1958
Ted Kremenek4f880632009-07-17 22:18:43 +00001959CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1960 // FIXME: For now we pretend that @catch and the code it contains does not
1961 // exit.
1962 return Block;
1963}
Mike Stump6d9828c2009-07-17 01:31:16 +00001964
Ted Kremenek2fda5042008-12-09 20:20:09 +00001965CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1966 // FIXME: This isn't complete. We basically treat @throw like a return
1967 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenek6c249722009-09-24 18:45:41 +00001969 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001970 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001971 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremenek2fda5042008-12-09 20:20:09 +00001973 // Create the new block.
1974 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001975
Ted Kremenek2fda5042008-12-09 20:20:09 +00001976 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001977 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001978
1979 // Add the statement to the block. This may create new blocks if S contains
1980 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001981 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001982}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001983
Mike Stump0979d802009-07-22 22:56:04 +00001984CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001985 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001986 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001987 return 0;
1988
1989 // Create the new block.
1990 Block = createBlock(false);
1991
Mike Stump5d1d2022010-01-19 02:20:09 +00001992 if (TryTerminatedBlock)
1993 // The current try statement is the only successor.
1994 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001995 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001996 // otherwise the Exit block is the only successor.
1997 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001998
1999 // Add the statement to the block. This may create new blocks if S contains
2000 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00002001 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00002002}
2003
Ted Kremenek4f880632009-07-17 22:18:43 +00002004CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002005 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
Mike Stump8f9893a2009-07-21 01:27:50 +00002007 // "do...while" is a control-flow statement. Thus we stop processing the
2008 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002009 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002010 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002011 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002012 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002013 } else
2014 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00002015
2016 // Because of short-circuit evaluation, the condition of the loop can span
2017 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2018 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002019 CFGBlock* ExitConditionBlock = createBlock(false);
2020 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002021
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002022 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002023 ExitConditionBlock->setTerminator(D);
2024
2025 // Now add the actual condition to the condition block. Because the condition
2026 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002027 if (Stmt* C = D->getCond()) {
2028 Block = ExitConditionBlock;
2029 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002030 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002031 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002032 return 0;
2033 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002034 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Ted Kremenek54827132008-02-27 07:20:00 +00002036 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002037 Succ = EntryConditionBlock;
2038
Mike Stump00998a02009-07-23 23:25:26 +00002039 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00002040 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002041
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002042 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002043 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002044 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002045 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002046
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002047 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002048 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2049 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2050 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002051
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002052 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002053 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002054
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002055 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002056 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002057
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002058 // NULL out Block to force lazy instantiation of blocks for the body.
2059 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002060
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002061 // If body is not a compound statement create implicit scope
2062 // and add destructors.
2063 if (!isa<CompoundStmt>(D->getBody()))
2064 addLocalScopeAndDtors(D->getBody());
2065
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002066 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002067 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Ted Kremenekaf603f72007-08-30 18:39:40 +00002069 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002070 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002071 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002072 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002073 return 0;
2074 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002075
Ted Kremenekd173dc72010-08-17 20:59:56 +00002076 if (!KnownVal.isFalse()) {
2077 // Add an intermediate block between the BodyBlock and the
2078 // ExitConditionBlock to represent the "loop back" transition. Create an
2079 // empty block to represent the transition block for looping back to the
2080 // head of the loop.
2081 // FIXME: Can we do this more efficiently without adding another block?
2082 Block = NULL;
2083 Succ = BodyBlock;
2084 CFGBlock *LoopBackBlock = createBlock();
2085 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
Ted Kremenekd173dc72010-08-17 20:59:56 +00002087 // Add the loop body entry as a successor to the condition.
2088 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2089 }
2090 else
2091 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002092 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002093
Ted Kremenek941fde82009-07-24 04:47:11 +00002094 // Link up the condition block with the code that follows the loop.
2095 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002096 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002097
2098 // There can be no more statements in the body block(s) since we loop back to
2099 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002100 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002101
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002102 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002103 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002104 return BodyBlock;
2105}
2106
2107CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2108 // "continue" is a control-flow statement. Thus we stop processing the
2109 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002110 if (badCFG)
2111 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002112
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002113 // Now create a new block that ends with the continue statement.
2114 Block = createBlock(false);
2115 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002116
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002117 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002118 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002119 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00002120 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002121 AddSuccessor(Block, ContinueJumpTarget.Block);
2122 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002123 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002124
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002125 return Block;
2126}
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002128CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002129 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002130
Ted Kremenek852274d2009-12-16 03:18:58 +00002131 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002132 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002133 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002134 }
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Ted Kremenek4f880632009-07-17 22:18:43 +00002136 // VLA types have expressions that must be evaluated.
2137 if (E->isArgumentType()) {
2138 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2139 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2140 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002141 }
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Mike Stump6d9828c2009-07-17 01:31:16 +00002143 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002144}
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Ted Kremenek4f880632009-07-17 22:18:43 +00002146/// VisitStmtExpr - Utility method to handle (nested) statement
2147/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002148CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2149 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002150 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002151 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002152 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002153 return VisitCompoundStmt(SE->getSubStmt());
2154}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002155
Ted Kremenek411cdee2008-04-16 21:10:48 +00002156CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002157 // "switch" is a control-flow statement. Thus we stop processing the current
2158 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002159 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002160
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002161 // Save local scope position because in case of condition variable ScopePos
2162 // won't be restored when traversing AST.
2163 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2164
2165 // Create local scope for possible condition variable.
2166 // Store scope position. Add implicit destructor.
2167 if (VarDecl* VD = Terminator->getConditionVariable()) {
2168 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2169 addLocalScopeForVarDecl(VD);
2170 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2171 }
2172
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002173 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002174 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002175 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002176 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002177 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002178
2179 // Save the current "switch" context.
2180 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002181 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002182 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002183
Mike Stump6d9828c2009-07-17 01:31:16 +00002184 // Set the "default" case to be the block after the switch statement. If the
2185 // switch statement contains a "default:", this value will be overwritten with
2186 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002187 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002188
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002189 // Create a new block that will contain the switch statement.
2190 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002191
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002192 // Now process the switch body. The code after the switch is the implicit
2193 // successor.
2194 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002195 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002196
2197 // When visiting the body, the case statements should automatically get linked
2198 // up to the switch. We also don't keep a pointer to the body, since all
2199 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002200 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002201 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002202
2203 // If body is not a compound statement create implicit scope
2204 // and add destructors.
2205 if (!isa<CompoundStmt>(Terminator->getBody()))
2206 addLocalScopeAndDtors(Terminator->getBody());
2207
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002208 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002209 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002210 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002211 return 0;
2212 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002213
Mike Stump6d9828c2009-07-17 01:31:16 +00002214 // If we have no "default:" case, the default transition is to the code
2215 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002216 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002217
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002218 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002219 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002220 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002221 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002222 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002223
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002224 // Finally, if the SwitchStmt contains a condition variable, add both the
2225 // SwitchStmt and the condition variable initialization to the CFG.
2226 if (VarDecl *VD = Terminator->getConditionVariable()) {
2227 if (Expr *Init = VD->getInit()) {
2228 autoCreateBlock();
2229 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2230 addStmt(Init);
2231 }
2232 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002233
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002234 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002235}
2236
Ted Kremenek4f880632009-07-17 22:18:43 +00002237CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002238 // CaseStmts are essentially labels, so they are the first statement in a
2239 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002240 CFGBlock *TopBlock = 0, *LastBlock = 0;
2241
2242 if (Stmt *Sub = CS->getSubStmt()) {
2243 // For deeply nested chains of CaseStmts, instead of doing a recursion
2244 // (which can blow out the stack), manually unroll and create blocks
2245 // along the way.
2246 while (isa<CaseStmt>(Sub)) {
2247 CFGBlock *CurrentBlock = createBlock(false);
2248 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002249
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002250 if (TopBlock)
2251 AddSuccessor(LastBlock, CurrentBlock);
2252 else
2253 TopBlock = CurrentBlock;
2254
2255 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2256 LastBlock = CurrentBlock;
2257
2258 CS = cast<CaseStmt>(Sub);
2259 Sub = CS->getSubStmt();
2260 }
2261
2262 addStmt(Sub);
2263 }
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002265 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002266 if (!CaseBlock)
2267 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
2269 // Cases statements partition blocks, so this is the top of the basic block we
2270 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002271 CaseBlock->setLabel(CS);
2272
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002273 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002274 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002275
2276 // Add this block to the list of successors for the block with the switch
2277 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002278 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002279 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002280
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002281 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2282 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002283
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002284 if (TopBlock) {
2285 AddSuccessor(LastBlock, CaseBlock);
2286 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002287 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002288 // This block is now the implicit successor of other blocks.
2289 Succ = CaseBlock;
2290 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002291
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002292 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002293}
Mike Stump6d9828c2009-07-17 01:31:16 +00002294
Ted Kremenek411cdee2008-04-16 21:10:48 +00002295CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002296 if (Terminator->getSubStmt())
2297 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002299 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002300
2301 if (!DefaultCaseBlock)
2302 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002303
2304 // Default statements partition blocks, so this is the top of the basic block
2305 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002306 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002308 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002309 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002310
Mike Stump6d9828c2009-07-17 01:31:16 +00002311 // Unlike case statements, we don't add the default block to the successors
2312 // for the switch statement immediately. This is done when we finish
2313 // processing the switch statement. This allows for the default case
2314 // (including a fall-through to the code after the switch statement) to always
2315 // be the last successor of a switch-terminated block.
2316
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002317 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2318 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002319
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002320 // This block is now the implicit successor of other blocks.
2321 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002322
2323 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002324}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002325
Mike Stump5d1d2022010-01-19 02:20:09 +00002326CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2327 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2328 // current block.
2329 CFGBlock* TrySuccessor = NULL;
2330
2331 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002332 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002333 return 0;
2334 TrySuccessor = Block;
2335 } else TrySuccessor = Succ;
2336
Mike Stumpa1f93632010-01-20 01:15:34 +00002337 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002338
2339 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002340 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002341 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002342 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002343
Mike Stumpa1f93632010-01-20 01:15:34 +00002344 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002345 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2346 // The code after the try is the implicit successor.
2347 Succ = TrySuccessor;
2348 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002349 if (CS->getExceptionDecl() == 0) {
2350 HasCatchAll = true;
2351 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002352 Block = NULL;
2353 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2354 if (CatchBlock == 0)
2355 return 0;
2356 // Add this block to the list of successors for the block with the try
2357 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002358 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002359 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002360 if (!HasCatchAll) {
2361 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002362 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002363 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002364 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002365 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002366
2367 // The code after the try is the implicit successor.
2368 Succ = TrySuccessor;
2369
Mike Stumpf00cca52010-01-20 01:30:58 +00002370 // Save the current "try" context.
2371 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2372 TryTerminatedBlock = NewTryTerminatedBlock;
2373
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002374 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002375 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002376 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002377 return Block;
2378}
2379
2380CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2381 // CXXCatchStmt are treated like labels, so they are the first statement in a
2382 // block.
2383
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002384 // Save local scope position because in case of exception variable ScopePos
2385 // won't be restored when traversing AST.
2386 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2387
2388 // Create local scope for possible exception variable.
2389 // Store scope position. Add implicit destructor.
2390 if (VarDecl* VD = CS->getExceptionDecl()) {
2391 LocalScope::const_iterator BeginScopePos = ScopePos;
2392 addLocalScopeForVarDecl(VD);
2393 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2394 }
2395
Mike Stump5d1d2022010-01-19 02:20:09 +00002396 if (CS->getHandlerBlock())
2397 addStmt(CS->getHandlerBlock());
2398
2399 CFGBlock* CatchBlock = Block;
2400 if (!CatchBlock)
2401 CatchBlock = createBlock();
2402
2403 CatchBlock->setLabel(CS);
2404
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002405 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002406 return 0;
2407
2408 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2409 Block = NULL;
2410
2411 return CatchBlock;
2412}
2413
John McCall4765fa02010-12-06 08:20:24 +00002414CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002415 AddStmtChoice asc) {
2416 if (BuildOpts.AddImplicitDtors) {
2417 // If adding implicit destructors visit the full expression for adding
2418 // destructors of temporaries.
2419 VisitForTemporaryDtors(E->getSubExpr());
2420
2421 // Full expression has to be added as CFGStmt so it will be sequenced
2422 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002423 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002424 }
2425 return Visit(E->getSubExpr(), asc);
2426}
2427
Zhongxing Xua725ed42010-11-01 13:04:58 +00002428CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2429 AddStmtChoice asc) {
2430 if (asc.alwaysAdd()) {
2431 autoCreateBlock();
2432 AppendStmt(Block, E, asc);
2433
2434 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002435 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002436 }
2437 return Visit(E->getSubExpr(), asc);
2438}
2439
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002440CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2441 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002442 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002443 if (!C->isElidable())
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002444 AppendStmt(Block, C, asc.withAlwaysAdd(true));
2445
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002446 return VisitChildren(C);
2447}
2448
Zhongxing Xua725ed42010-11-01 13:04:58 +00002449CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2450 AddStmtChoice asc) {
2451 if (asc.alwaysAdd()) {
2452 autoCreateBlock();
2453 AppendStmt(Block, E, asc);
2454 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002455 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002456 }
2457 return Visit(E->getSubExpr(), asc);
2458}
2459
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002460CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2461 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002462 autoCreateBlock();
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002463 AppendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002464 return VisitChildren(C);
2465}
2466
Ted Kremenekad5a8942010-08-02 23:46:59 +00002467CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002468 AddStmtChoice asc) {
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002469 autoCreateBlock();
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002470 AppendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002471 return VisitChildren(C);
2472}
2473
Zhongxing Xua725ed42010-11-01 13:04:58 +00002474CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2475 AddStmtChoice asc) {
2476 if (asc.alwaysAdd()) {
2477 autoCreateBlock();
2478 AppendStmt(Block, E, asc);
2479 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002480 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002481 }
2482 return Visit(E->getSubExpr(), asc);
2483}
2484
Ted Kremenek19bb3562007-08-28 19:26:49 +00002485CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002486 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002487 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002488
Ted Kremenek19bb3562007-08-28 19:26:49 +00002489 if (!IBlock) {
2490 IBlock = createBlock(false);
2491 cfg->setIndirectGotoBlock(IBlock);
2492 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002493
Ted Kremenek19bb3562007-08-28 19:26:49 +00002494 // IndirectGoto is a control-flow statement. Thus we stop processing the
2495 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002496 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002497 return 0;
2498
Ted Kremenek19bb3562007-08-28 19:26:49 +00002499 Block = createBlock(false);
2500 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002501 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002502 return addStmt(I->getTarget());
2503}
2504
Marcin Swiderski8599e762010-11-03 06:19:35 +00002505CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2506tryAgain:
2507 if (!E) {
2508 badCFG = true;
2509 return NULL;
2510 }
2511 switch (E->getStmtClass()) {
2512 default:
2513 return VisitChildrenForTemporaryDtors(E);
2514
2515 case Stmt::BinaryOperatorClass:
2516 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2517
2518 case Stmt::CXXBindTemporaryExprClass:
2519 return VisitCXXBindTemporaryExprForTemporaryDtors(
2520 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2521
2522 case Stmt::ConditionalOperatorClass:
2523 return VisitConditionalOperatorForTemporaryDtors(
2524 cast<ConditionalOperator>(E), BindToTemporary);
2525
2526 case Stmt::ImplicitCastExprClass:
2527 // For implicit cast we want BindToTemporary to be passed further.
2528 E = cast<CastExpr>(E)->getSubExpr();
2529 goto tryAgain;
2530
2531 case Stmt::ParenExprClass:
2532 E = cast<ParenExpr>(E)->getSubExpr();
2533 goto tryAgain;
2534 }
2535}
2536
2537CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2538 // When visiting children for destructors we want to visit them in reverse
2539 // order. Because there's no reverse iterator for children must to reverse
2540 // them in helper vector.
2541 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2542 ChildrenVect ChildrenRev;
2543 for (Stmt::child_iterator I = E->child_begin(), L = E->child_end();
2544 I != L; ++I) {
2545 if (*I) ChildrenRev.push_back(*I);
2546 }
2547
2548 CFGBlock *B = Block;
2549 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2550 L = ChildrenRev.rend(); I != L; ++I) {
2551 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2552 B = R;
2553 }
2554 return B;
2555}
2556
2557CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2558 if (E->isLogicalOp()) {
2559 // Destructors for temporaries in LHS expression should be called after
2560 // those for RHS expression. Even if this will unnecessarily create a block,
2561 // this block will be used at least by the full expression.
2562 autoCreateBlock();
2563 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2564 if (badCFG)
2565 return NULL;
2566
2567 Succ = ConfluenceBlock;
2568 Block = NULL;
2569 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2570
2571 if (RHSBlock) {
2572 if (badCFG)
2573 return NULL;
2574
2575 // If RHS expression did produce destructors we need to connect created
2576 // blocks to CFG in same manner as for binary operator itself.
2577 CFGBlock *LHSBlock = createBlock(false);
2578 LHSBlock->setTerminator(CFGTerminator(E, true));
2579
2580 // For binary operator LHS block is before RHS in list of predecessors
2581 // of ConfluenceBlock.
2582 std::reverse(ConfluenceBlock->pred_begin(),
2583 ConfluenceBlock->pred_end());
2584
2585 // See if this is a known constant.
2586 TryResult KnownVal = TryEvaluateBool(E->getLHS());
2587 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2588 KnownVal.negate();
2589
2590 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2591 // itself.
2592 if (E->getOpcode() == BO_LOr) {
2593 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2594 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2595 } else {
2596 assert (E->getOpcode() == BO_LAnd);
2597 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2598 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2599 }
2600
2601 Block = LHSBlock;
2602 return LHSBlock;
2603 }
2604
2605 Block = ConfluenceBlock;
2606 return ConfluenceBlock;
2607 }
2608
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002609 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002610 // For assignment operator (=) LHS expression is visited
2611 // before RHS expression. For destructors visit them in reverse order.
2612 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2613 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2614 return LHSBlock ? LHSBlock : RHSBlock;
2615 }
2616
2617 // For any other binary operator RHS expression is visited before
2618 // LHS expression (order of children). For destructors visit them in reverse
2619 // order.
2620 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2621 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2622 return RHSBlock ? RHSBlock : LHSBlock;
2623}
2624
2625CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2626 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2627 // First add destructors for temporaries in subexpression.
2628 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002629 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002630 // If lifetime of temporary is not prolonged (by assigning to constant
2631 // reference) add destructor for it.
2632 autoCreateBlock();
2633 appendTemporaryDtor(Block, E);
2634 B = Block;
2635 }
2636 return B;
2637}
2638
2639CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2640 ConditionalOperator *E, bool BindToTemporary) {
2641 // First add destructors for condition expression. Even if this will
2642 // unnecessarily create a block, this block will be used at least by the full
2643 // expression.
2644 autoCreateBlock();
2645 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2646 if (badCFG)
2647 return NULL;
2648
2649 // Try to add block with destructors for LHS expression.
2650 CFGBlock *LHSBlock = NULL;
2651 if (E->getLHS()) {
2652 Succ = ConfluenceBlock;
2653 Block = NULL;
2654 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2655 if (badCFG)
2656 return NULL;
2657 }
2658
2659 // Try to add block with destructors for RHS expression;
2660 Succ = ConfluenceBlock;
2661 Block = NULL;
2662 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2663 if (badCFG)
2664 return NULL;
2665
2666 if (!RHSBlock && !LHSBlock) {
2667 // If neither LHS nor RHS expression had temporaries to destroy don't create
2668 // more blocks.
2669 Block = ConfluenceBlock;
2670 return Block;
2671 }
2672
2673 Block = createBlock(false);
2674 Block->setTerminator(CFGTerminator(E, true));
2675
2676 // See if this is a known constant.
2677 const TryResult &KnownVal = TryEvaluateBool(E->getCond());
2678
2679 if (LHSBlock) {
2680 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
2681 } else if (KnownVal.isFalse()) {
2682 AddSuccessor(Block, NULL);
2683 } else {
2684 AddSuccessor(Block, ConfluenceBlock);
2685 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2686 }
2687
2688 if (!RHSBlock)
2689 RHSBlock = ConfluenceBlock;
2690 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
2691
2692 return Block;
2693}
2694
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002695} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002696
Mike Stump6d9828c2009-07-17 01:31:16 +00002697/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2698/// no successors or predecessors. If this is the first block created in the
2699/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002700CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002701 bool first_block = begin() == end();
2702
2703 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002704 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2705 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2706 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002707
2708 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002709 if (first_block)
2710 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002711
2712 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002713 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002714}
2715
Ted Kremenek026473c2007-08-23 16:51:22 +00002716/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2717/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002718CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002719 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002720 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002721 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002722}
2723
Ted Kremenek63f58872007-10-01 19:33:33 +00002724//===----------------------------------------------------------------------===//
2725// CFG: Queries for BlkExprs.
2726//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002727
Ted Kremenek63f58872007-10-01 19:33:33 +00002728namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002729 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002730}
2731
Ted Kremenek8a693662009-12-23 23:37:10 +00002732static void FindSubExprAssignments(Stmt *S,
2733 llvm::SmallPtrSet<Expr*,50>& Set) {
2734 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002735 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002736
Ted Kremenek8a693662009-12-23 23:37:10 +00002737 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002738 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002739 if (!child)
2740 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002741
Ted Kremenek8a693662009-12-23 23:37:10 +00002742 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002743 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002744
Ted Kremenek8a693662009-12-23 23:37:10 +00002745 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002746 }
2747}
2748
Ted Kremenek63f58872007-10-01 19:33:33 +00002749static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2750 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002751
2752 // Look for assignments that are used as subexpressions. These are the only
2753 // assignments that we want to *possibly* register as a block-level
2754 // expression. Basically, if an assignment occurs both in a subexpression and
2755 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002756 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002757
Ted Kremenek63f58872007-10-01 19:33:33 +00002758 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002759 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002760 if (CFGStmt S = BI->getAs<CFGStmt>())
2761 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002762
Ted Kremenek411cdee2008-04-16 21:10:48 +00002763 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002764
2765 // Iterate over the statements again on identify the Expr* and Stmt* at the
2766 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002767
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002768 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2769 CFGStmt CS = BI->getAs<CFGStmt>();
2770 if (!CS.isValid())
2771 continue;
2772 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002773
Ted Kremenek411cdee2008-04-16 21:10:48 +00002774 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002775 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002776 // expression are really "statements" whose value is never used by
2777 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002778 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002779 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002780 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2781 // Special handling for statement expressions. The last statement in
2782 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002783 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002784 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002785 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002786 (*M)[C->body_back()] = x;
2787 }
2788 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002789
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002790 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002791 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002792 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002793 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002794
Ted Kremenek411cdee2008-04-16 21:10:48 +00002795 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002796
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002797 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002798
Ted Kremenek390e48b2008-11-12 21:11:49 +00002799 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002800 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002801 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002802 }
2803 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002804
Ted Kremenek63f58872007-10-01 19:33:33 +00002805 return M;
2806}
2807
Ted Kremenek86946742008-01-17 20:48:37 +00002808CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2809 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002810 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002811
Ted Kremenek63f58872007-10-01 19:33:33 +00002812 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002813 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002814 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002815}
2816
2817unsigned CFG::getNumBlkExprs() {
2818 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2819 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002820
2821 // We assume callers interested in the number of BlkExprs will want
2822 // the map constructed if it doesn't already exist.
2823 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2824 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00002825}
2826
Ted Kremenek274f4332008-04-28 18:00:46 +00002827//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002828// Filtered walking of the CFG.
2829//===----------------------------------------------------------------------===//
2830
2831bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002832 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002833
2834 if (F.IgnoreDefaultsWithCoveredEnums) {
2835 // If the 'To' has no label or is labeled but the label isn't a
2836 // CaseStmt then filter this edge.
2837 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002838 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002839 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002840 const Stmt *L = To->getLabel();
2841 if (!L || !isa<CaseStmt>(L))
2842 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002843 }
2844 }
2845 }
2846
2847 return false;
2848}
2849
2850//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002851// Cleanup: CFG dstor.
2852//===----------------------------------------------------------------------===//
2853
Ted Kremenek63f58872007-10-01 19:33:33 +00002854CFG::~CFG() {
2855 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2856}
Mike Stump6d9828c2009-07-17 01:31:16 +00002857
Ted Kremenek7dba8602007-08-29 21:56:09 +00002858//===----------------------------------------------------------------------===//
2859// CFG pretty printing
2860//===----------------------------------------------------------------------===//
2861
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002862namespace {
2863
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002864class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002865 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002866 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002867 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002868 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002869 signed CurrentBlock;
2870 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002871 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002872public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002873
Chris Lattnere4f21422009-06-30 01:26:17 +00002874 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2875 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002876 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2877 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002878 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002879 BI != BEnd; ++BI, ++j ) {
2880 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2881 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2882 StmtMap[SE] = P;
2883
2884 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2885 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002886
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002887 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2888 if (VarDecl* VD = IS->getConditionVariable())
2889 DeclMap[VD] = P;
2890
2891 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2892 if (VarDecl* VD = FS->getConditionVariable())
2893 DeclMap[VD] = P;
2894
2895 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2896 if (VarDecl* VD = WS->getConditionVariable())
2897 DeclMap[VD] = P;
2898
2899 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2900 if (VarDecl* VD = SS->getConditionVariable())
2901 DeclMap[VD] = P;
2902
2903 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2904 if (VarDecl* VD = CS->getExceptionDecl())
2905 DeclMap[VD] = P;
2906 }
2907 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002908 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002909 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002910 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002911
Ted Kremenek42a509f2007-08-31 21:30:12 +00002912 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002913
Chris Lattnere4f21422009-06-30 01:26:17 +00002914 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002915 void setBlockID(signed i) { CurrentBlock = i; }
2916 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002917
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002918 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2919 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002920
2921 if (I == StmtMap.end())
2922 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002923
2924 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002925 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002926 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002927 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002928
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002929 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002930 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002931 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002932
2933 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2934 DeclMapTy::iterator I = DeclMap.find(D);
2935
2936 if (I == DeclMap.end())
2937 return false;
2938
2939 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2940 && I->second.second == CurrentStmt) {
2941 return false;
2942 }
2943
2944 OS << "[B" << I->second.first << "." << I->second.second << "]";
2945 return true;
2946 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002947};
Chris Lattnere4f21422009-06-30 01:26:17 +00002948} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002949
Chris Lattnere4f21422009-06-30 01:26:17 +00002950
2951namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002952class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002953 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002954
Ted Kremeneka95d3752008-09-13 05:16:45 +00002955 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002956 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002957 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002958public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002959 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002960 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002961 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002962
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002963 void VisitIfStmt(IfStmt* I) {
2964 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002965 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002966 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002967
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002968 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002969 void VisitStmt(Stmt* Terminator) {
2970 Terminator->printPretty(OS, Helper, Policy);
2971 }
2972
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002973 void VisitForStmt(ForStmt* F) {
2974 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002975 if (F->getInit())
2976 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002977 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002978 if (Stmt* C = F->getCond())
2979 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002980 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002981 if (F->getInc())
2982 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002983 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002984 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002985
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002986 void VisitWhileStmt(WhileStmt* W) {
2987 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002988 if (Stmt* C = W->getCond())
2989 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002990 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002991
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002992 void VisitDoStmt(DoStmt* D) {
2993 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002994 if (Stmt* C = D->getCond())
2995 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002996 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002997
Ted Kremenek411cdee2008-04-16 21:10:48 +00002998 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002999 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003000 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00003001 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003002
Mike Stump5d1d2022010-01-19 02:20:09 +00003003 void VisitCXXTryStmt(CXXTryStmt* CS) {
3004 OS << "try ...";
3005 }
3006
Ted Kremenek805e9a82007-08-31 21:49:40 +00003007 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003008 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003009 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003010 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003011
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003012 void VisitChooseExpr(ChooseExpr* C) {
3013 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003014 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00003015 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00003016 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003017
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003018 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3019 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003020 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003021 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003022
Ted Kremenek805e9a82007-08-31 21:49:40 +00003023 void VisitBinaryOperator(BinaryOperator* B) {
3024 if (!B->isLogicalOp()) {
3025 VisitExpr(B);
3026 return;
3027 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003028
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003029 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003030
Ted Kremenek805e9a82007-08-31 21:49:40 +00003031 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003032 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003033 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003034 return;
John McCall2de56d12010-08-25 11:45:40 +00003035 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003036 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003037 return;
3038 default:
3039 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003040 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003041 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003042
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003043 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003044 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003045 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003046};
Chris Lattnere4f21422009-06-30 01:26:17 +00003047} // end anonymous namespace
3048
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003049static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003050 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003051 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3052 Stmt *S = CS;
3053
3054 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003055
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003056 // special printing for statement-expressions.
3057 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3058 CompoundStmt* Sub = SE->getSubStmt();
3059
3060 if (Sub->child_begin() != Sub->child_end()) {
3061 OS << "({ ... ; ";
3062 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3063 OS << " })\n";
3064 return;
3065 }
3066 }
3067 // special printing for comma expressions.
3068 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3069 if (B->getOpcode() == BO_Comma) {
3070 OS << "... , ";
3071 Helper->handledStmt(B->getRHS(),OS);
3072 OS << '\n';
3073 return;
3074 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003075 }
3076 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003077 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003078
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003079 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003080 OS << " (OperatorCall)";
3081 } else if (isa<CXXBindTemporaryExpr>(S)) {
3082 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003083 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003084
Zhongxing Xu5281b8e2010-11-24 06:33:02 +00003085 if (CS.asLValue())
3086 OS << " (asLValue)";
3087
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003088 // Expressions need a newline.
3089 if (isa<Expr>(S))
3090 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003091
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003092 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3093 CXXBaseOrMemberInitializer* I = IE;
3094 if (I->isBaseInitializer())
3095 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003096 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003097
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003098 OS << "(";
3099 if (Expr* IE = I->getInit())
3100 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3101 OS << ")";
3102
3103 if (I->isBaseInitializer())
3104 OS << " (Base initializer)\n";
3105 else OS << " (Member initializer)\n";
3106
3107 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3108 VarDecl* VD = DE.getVarDecl();
3109 Helper->handleDecl(VD, OS);
3110
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003111 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003112 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3113 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003114 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3115 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003116
3117 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3118 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003119
3120 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3121 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3122 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003123 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003124
3125 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3126 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003127
3128 const Type *T = FD->getType().getTypePtr();
3129 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3130 T = ET;
3131
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003132 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003133 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003134 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003135
3136 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3137 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3138 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3139 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003140 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003141}
Mike Stump6d9828c2009-07-17 01:31:16 +00003142
Chris Lattnere4f21422009-06-30 01:26:17 +00003143static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3144 const CFGBlock& B,
3145 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003146
Ted Kremenek42a509f2007-08-31 21:30:12 +00003147 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003148
Ted Kremenek7dba8602007-08-29 21:56:09 +00003149 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003150 OS << "\n [ B" << B.getBlockID();
3151
Ted Kremenek42a509f2007-08-31 21:30:12 +00003152 if (&B == &cfg->getEntry())
3153 OS << " (ENTRY) ]\n";
3154 else if (&B == &cfg->getExit())
3155 OS << " (EXIT) ]\n";
3156 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003157 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003158 else
3159 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003160
Ted Kremenek9cffe732007-08-29 23:20:49 +00003161 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003162 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003163
3164 if (print_edges)
3165 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003166
Mike Stump079bd722010-01-19 22:00:14 +00003167 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003168 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003169 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003170 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003171 C->getLHS()->printPretty(OS, Helper,
3172 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003173 if (C->getRHS()) {
3174 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003175 C->getRHS()->printPretty(OS, Helper,
3176 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003177 }
Mike Stump079bd722010-01-19 22:00:14 +00003178 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003179 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003180 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003181 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003182 if (CS->getExceptionDecl())
3183 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3184 0);
3185 else
3186 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003187 OS << ")";
3188
3189 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003190 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003191
Ted Kremenek9cffe732007-08-29 23:20:49 +00003192 OS << ":\n";
3193 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003194
Ted Kremenekfddd5182007-08-21 21:42:03 +00003195 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003196 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003197
Ted Kremenek42a509f2007-08-31 21:30:12 +00003198 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3199 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003200
Ted Kremenek9cffe732007-08-29 23:20:49 +00003201 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003202 if (print_edges)
3203 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003204
Ted Kremeneka95d3752008-09-13 05:16:45 +00003205 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003206
Ted Kremenek42a509f2007-08-31 21:30:12 +00003207 if (Helper)
3208 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003209
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003210 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003211 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003212
Ted Kremenek9cffe732007-08-29 23:20:49 +00003213 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003214 if (B.getTerminator()) {
3215 if (print_edges)
3216 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003217
Ted Kremenek9cffe732007-08-29 23:20:49 +00003218 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003219
Ted Kremenek42a509f2007-08-31 21:30:12 +00003220 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003221
Chris Lattnere4f21422009-06-30 01:26:17 +00003222 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3223 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003224 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003225 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003226 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003227
Ted Kremenek9cffe732007-08-29 23:20:49 +00003228 if (print_edges) {
3229 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003230 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003231 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003232
Ted Kremenek42a509f2007-08-31 21:30:12 +00003233 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3234 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003235
Ted Kremenek42a509f2007-08-31 21:30:12 +00003236 if (i == 8 || (i-8) == 0)
3237 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003238
Ted Kremenek9cffe732007-08-29 23:20:49 +00003239 OS << " B" << (*I)->getBlockID();
3240 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003241
Ted Kremenek42a509f2007-08-31 21:30:12 +00003242 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003243
Ted Kremenek42a509f2007-08-31 21:30:12 +00003244 // Print the successors of this block.
3245 OS << " Successors (" << B.succ_size() << "):";
3246 i = 0;
3247
3248 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3249 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003250
Ted Kremenek42a509f2007-08-31 21:30:12 +00003251 if (i == 8 || (i-8) % 10 == 0)
3252 OS << "\n ";
3253
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003254 if (*I)
3255 OS << " B" << (*I)->getBlockID();
3256 else
3257 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003258 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003259
Ted Kremenek9cffe732007-08-29 23:20:49 +00003260 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003261 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003262}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003263
Ted Kremenek42a509f2007-08-31 21:30:12 +00003264
3265/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003266void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003267
3268/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003269void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3270 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003271
Ted Kremenek42a509f2007-08-31 21:30:12 +00003272 // Print the entry block.
3273 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003274
Ted Kremenek42a509f2007-08-31 21:30:12 +00003275 // Iterate through the CFGBlocks and print them one by one.
3276 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3277 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003278 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003279 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003280
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003281 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003282 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003283
Ted Kremenek42a509f2007-08-31 21:30:12 +00003284 // Print the exit block.
3285 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003286 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003287}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003288
3289/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003290void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3291 print(llvm::errs(), cfg, LO);
3292}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003293
3294/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3295/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003296void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3297 const LangOptions &LO) const {
3298 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003299 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003300}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003301
Ted Kremeneka2925852008-01-30 23:02:42 +00003302/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003303void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003304 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003305 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003306 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003307}
3308
Ted Kremenek390e48b2008-11-12 21:11:49 +00003309Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003310 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003311 if (!Terminator)
3312 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003313
Ted Kremenek411cdee2008-04-16 21:10:48 +00003314 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003315
Ted Kremenek411cdee2008-04-16 21:10:48 +00003316 switch (Terminator->getStmtClass()) {
3317 default:
3318 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003319
Ted Kremenek411cdee2008-04-16 21:10:48 +00003320 case Stmt::ForStmtClass:
3321 E = cast<ForStmt>(Terminator)->getCond();
3322 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003323
Ted Kremenek411cdee2008-04-16 21:10:48 +00003324 case Stmt::WhileStmtClass:
3325 E = cast<WhileStmt>(Terminator)->getCond();
3326 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003327
Ted Kremenek411cdee2008-04-16 21:10:48 +00003328 case Stmt::DoStmtClass:
3329 E = cast<DoStmt>(Terminator)->getCond();
3330 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003331
Ted Kremenek411cdee2008-04-16 21:10:48 +00003332 case Stmt::IfStmtClass:
3333 E = cast<IfStmt>(Terminator)->getCond();
3334 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003335
Ted Kremenek411cdee2008-04-16 21:10:48 +00003336 case Stmt::ChooseExprClass:
3337 E = cast<ChooseExpr>(Terminator)->getCond();
3338 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003339
Ted Kremenek411cdee2008-04-16 21:10:48 +00003340 case Stmt::IndirectGotoStmtClass:
3341 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3342 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003343
Ted Kremenek411cdee2008-04-16 21:10:48 +00003344 case Stmt::SwitchStmtClass:
3345 E = cast<SwitchStmt>(Terminator)->getCond();
3346 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003347
Ted Kremenek411cdee2008-04-16 21:10:48 +00003348 case Stmt::ConditionalOperatorClass:
3349 E = cast<ConditionalOperator>(Terminator)->getCond();
3350 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003351
Ted Kremenek411cdee2008-04-16 21:10:48 +00003352 case Stmt::BinaryOperatorClass: // '&&' and '||'
3353 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003354 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003355
Ted Kremenek390e48b2008-11-12 21:11:49 +00003356 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003357 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003358 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003359
Ted Kremenek411cdee2008-04-16 21:10:48 +00003360 return E ? E->IgnoreParens() : NULL;
3361}
3362
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003363bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003364 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003365 if (!Terminator)
3366 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003367
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003368 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003369
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003370 switch (Terminator->getStmtClass()) {
3371 default:
3372 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003373
3374 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003375 case Stmt::WhileStmtClass:
3376 case Stmt::DoStmtClass:
3377 case Stmt::IfStmtClass:
3378 case Stmt::ChooseExprClass:
3379 case Stmt::ConditionalOperatorClass:
3380 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003381 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003382 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003383
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003384 return E ? E->IgnoreParens() : NULL;
3385}
3386
Ted Kremeneka2925852008-01-30 23:02:42 +00003387
Ted Kremenek7dba8602007-08-29 21:56:09 +00003388//===----------------------------------------------------------------------===//
3389// CFG Graphviz Visualization
3390//===----------------------------------------------------------------------===//
3391
Ted Kremenek42a509f2007-08-31 21:30:12 +00003392
3393#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003394static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003395#endif
3396
Chris Lattnere4f21422009-06-30 01:26:17 +00003397void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003398#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003399 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003400 GraphHelper = &H;
3401 llvm::ViewGraph(this,"CFG");
3402 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003403#endif
3404}
3405
Ted Kremenek7dba8602007-08-29 21:56:09 +00003406namespace llvm {
3407template<>
3408struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003409
3410 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3411
3412 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003413
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003414#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003415 std::string OutSStr;
3416 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003417 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003418 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003419
3420 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3421
3422 // Process string output to make it nicer...
3423 for (unsigned i = 0; i != OutStr.length(); ++i)
3424 if (OutStr[i] == '\n') { // Left justify
3425 OutStr[i] = '\\';
3426 OutStr.insert(OutStr.begin()+i+1, 'l');
3427 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003428
Ted Kremenek7dba8602007-08-29 21:56:09 +00003429 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003430#else
3431 return "";
3432#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003433 }
3434};
3435} // end namespace llvm