blob: 57e67e4268af698d67d7f5a1d378eb7ff9c75136 [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);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000293 CFGBlock *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
294 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) {
556 if (FieldDecl *FD = I->getMember())
557 IsReference = FD->getType()->isReferenceType();
558 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
559
560 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
561 // Generate destructors for temporaries in initialization expression.
562 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
563 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.
575 return Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
576 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));
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek021c8af2010-01-19 20:40:33 +0000829 case Stmt::CXXCatchStmtClass:
830 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
831
Marcin Swiderski8599e762010-11-03 06:19:35 +0000832 case Stmt::CXXExprWithTemporariesClass:
833 return VisitCXXExprWithTemporaries(cast<CXXExprWithTemporaries>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000834
Zhongxing Xua725ed42010-11-01 13:04:58 +0000835 case Stmt::CXXBindTemporaryExprClass:
836 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
837
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000838 case Stmt::CXXConstructExprClass:
839 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
840
Zhongxing Xua725ed42010-11-01 13:04:58 +0000841 case Stmt::CXXFunctionalCastExprClass:
842 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
843
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000844 case Stmt::CXXTemporaryObjectExprClass:
845 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
846
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000847 case Stmt::CXXMemberCallExprClass:
848 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
849
Ted Kremenek021c8af2010-01-19 20:40:33 +0000850 case Stmt::CXXThrowExprClass:
851 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000852
Ted Kremenek021c8af2010-01-19 20:40:33 +0000853 case Stmt::CXXTryStmtClass:
854 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000855
Ted Kremenek4f880632009-07-17 22:18:43 +0000856 case Stmt::DeclStmtClass:
857 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremenek4f880632009-07-17 22:18:43 +0000859 case Stmt::DefaultStmtClass:
860 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenek4f880632009-07-17 22:18:43 +0000862 case Stmt::DoStmtClass:
863 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Ted Kremenek4f880632009-07-17 22:18:43 +0000865 case Stmt::ForStmtClass:
866 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek4f880632009-07-17 22:18:43 +0000868 case Stmt::GotoStmtClass:
869 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek4f880632009-07-17 22:18:43 +0000871 case Stmt::IfStmtClass:
872 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Zhongxing Xua725ed42010-11-01 13:04:58 +0000874 case Stmt::ImplicitCastExprClass:
875 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
876
Ted Kremenek4f880632009-07-17 22:18:43 +0000877 case Stmt::IndirectGotoStmtClass:
878 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek4f880632009-07-17 22:18:43 +0000880 case Stmt::LabelStmtClass:
881 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek115c1b92010-04-11 17:02:10 +0000883 case Stmt::MemberExprClass:
884 return VisitMemberExpr(cast<MemberExpr>(S), asc);
885
Ted Kremenek4f880632009-07-17 22:18:43 +0000886 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000887 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
888
Ted Kremenek4f880632009-07-17 22:18:43 +0000889 case Stmt::ObjCAtSynchronizedStmtClass:
890 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Ted Kremenek4f880632009-07-17 22:18:43 +0000892 case Stmt::ObjCAtThrowStmtClass:
893 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Ted Kremenek4f880632009-07-17 22:18:43 +0000895 case Stmt::ObjCAtTryStmtClass:
896 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek4f880632009-07-17 22:18:43 +0000898 case Stmt::ObjCForCollectionStmtClass:
899 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremenek4f880632009-07-17 22:18:43 +0000901 case Stmt::ParenExprClass:
902 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000903 goto tryAgain;
904
Ted Kremenek4f880632009-07-17 22:18:43 +0000905 case Stmt::NullStmtClass:
906 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek4f880632009-07-17 22:18:43 +0000908 case Stmt::ReturnStmtClass:
909 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek4f880632009-07-17 22:18:43 +0000911 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000912 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek4f880632009-07-17 22:18:43 +0000914 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000915 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek4f880632009-07-17 22:18:43 +0000917 case Stmt::SwitchStmtClass:
918 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000920 case Stmt::UnaryOperatorClass:
921 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
922
Ted Kremenek4f880632009-07-17 22:18:43 +0000923 case Stmt::WhileStmtClass:
924 return VisitWhileStmt(cast<WhileStmt>(S));
925 }
926}
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek852274d2009-12-16 03:18:58 +0000928CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
929 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000930 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000931 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek4f880632009-07-17 22:18:43 +0000934 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000935}
Mike Stump6d9828c2009-07-17 01:31:16 +0000936
Ted Kremenek4f880632009-07-17 22:18:43 +0000937/// VisitChildren - Visit the children of a Stmt.
938CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
939 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000940 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000941 E = Terminator->child_end(); I != E; ++I) {
942 if (*I) B = Visit(*I);
943 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000944 return B;
945}
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Ted Kremenek852274d2009-12-16 03:18:58 +0000947CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
948 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000949 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000950
Ted Kremenek852274d2009-12-16 03:18:58 +0000951 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000952 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000953 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000954 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000955
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000956 return Block;
957}
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000959CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
960 AddStmtChoice asc) {
961 if (asc.alwaysAdd()) {
962 autoCreateBlock();
963 AppendStmt(Block, U, asc);
964 }
965
966 bool asLVal = U->isIncrementDecrementOp();
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +0000967 return Visit(U->getSubExpr(), AddStmtChoice().withAsLValue(asLVal));
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000968}
969
Ted Kremenek852274d2009-12-16 03:18:58 +0000970CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
971 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000972 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000973 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000974 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000976 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000977 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek4f880632009-07-17 22:18:43 +0000979 // create the block evaluating the LHS
980 CFGBlock* LHSBlock = createBlock(false);
981 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek4f880632009-07-17 22:18:43 +0000983 // create the block evaluating the RHS
984 Succ = ConfluenceBlock;
985 Block = NULL;
986 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000987
988 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000989 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000990 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +0000991 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +0000992 // Create an empty block for cases where the RHS doesn't require
993 // any explicit statements in the CFG.
994 RHSBlock = createBlock();
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Mike Stump00998a02009-07-23 23:25:26 +0000997 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000998 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000999 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001000 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001001
Ted Kremenek4f880632009-07-17 22:18:43 +00001002 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001003 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001004 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1005 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001006 } else {
John McCall2de56d12010-08-25 11:45:40 +00001007 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001008 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1009 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenek4f880632009-07-17 22:18:43 +00001012 // Generate the blocks for evaluating the LHS.
1013 Block = LHSBlock;
1014 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +00001015 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001016
1017 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001018 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001019 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001020 addStmt(B->getRHS());
1021 return addStmt(B->getLHS());
1022 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001023
1024 if (B->isAssignmentOp()) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001025 if (asc.alwaysAdd()) {
1026 autoCreateBlock();
1027 AppendStmt(Block, B, asc);
1028 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001029
Marcin Swiderskie1667192010-10-24 08:21:40 +00001030 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
1031 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Marcin Swiderskie1667192010-10-24 08:21:40 +00001034 if (asc.alwaysAdd()) {
1035 autoCreateBlock();
1036 AppendStmt(Block, B, asc);
1037 }
1038
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001039 CFGBlock *RBlock = Visit(B->getRHS());
1040 CFGBlock *LBlock = Visit(B->getLHS());
1041 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1042 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1043 // return RBlock. Otherwise we'll incorrectly return NULL.
1044 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001045}
1046
Ted Kremenek852274d2009-12-16 03:18:58 +00001047CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1048 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001049 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001050 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001051 }
1052 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001053}
1054
Ted Kremenek4f880632009-07-17 22:18:43 +00001055CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1056 // "break" is a control-flow statement. Thus we stop processing the current
1057 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001058 if (badCFG)
1059 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremenek4f880632009-07-17 22:18:43 +00001061 // Now create a new block that ends with the break statement.
1062 Block = createBlock(false);
1063 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Ted Kremenek4f880632009-07-17 22:18:43 +00001065 // If there is no target for the break, then we are looking at an incomplete
1066 // AST. This means that the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001067 if (BreakJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001068 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001069 AddSuccessor(Block, BreakJumpTarget.Block);
1070 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001071 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001072
1073
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001074 return Block;
1075}
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Mike Stump4c45aa12010-01-21 15:20:48 +00001077static bool CanThrow(Expr *E) {
1078 QualType Ty = E->getType();
1079 if (Ty->isFunctionPointerType())
1080 Ty = Ty->getAs<PointerType>()->getPointeeType();
1081 else if (Ty->isBlockPointerType())
1082 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001083
Mike Stump4c45aa12010-01-21 15:20:48 +00001084 const FunctionType *FT = Ty->getAs<FunctionType>();
1085 if (FT) {
1086 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1087 if (Proto->hasEmptyExceptionSpec())
1088 return false;
1089 }
1090 return true;
1091}
1092
Ted Kremenek852274d2009-12-16 03:18:58 +00001093CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001094 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001095 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001096 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001097 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001098 }
Mike Stump24556362009-07-25 21:26:53 +00001099
Mike Stump4c45aa12010-01-21 15:20:48 +00001100 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001101
1102 // Languages without exceptions are assumed to not throw.
1103 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001104 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001105 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001106 }
1107
1108 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001109 if (FD->hasAttr<NoReturnAttr>())
1110 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001111 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001112 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001113 }
Mike Stump24556362009-07-25 21:26:53 +00001114
Mike Stump4c45aa12010-01-21 15:20:48 +00001115 if (!CanThrow(C->getCallee()))
1116 AddEHEdge = false;
1117
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001118 if (!NoReturn && !AddEHEdge)
1119 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Mike Stump079bd722010-01-19 22:00:14 +00001121 if (Block) {
1122 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001123 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001124 return 0;
1125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Mike Stump079bd722010-01-19 22:00:14 +00001127 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +00001128 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001129
Mike Stump079bd722010-01-19 22:00:14 +00001130 if (NoReturn) {
1131 // Wire this to the exit block directly.
1132 AddSuccessor(Block, &cfg->getExit());
1133 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001134 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001135 // Add exceptional edges.
1136 if (TryTerminatedBlock)
1137 AddSuccessor(Block, TryTerminatedBlock);
1138 else
1139 AddSuccessor(Block, &cfg->getExit());
1140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Mike Stump24556362009-07-25 21:26:53 +00001142 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001143}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001144
Ted Kremenek852274d2009-12-16 03:18:58 +00001145CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1146 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001147 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001148 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001149 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001150 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001152 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001153 Succ = ConfluenceBlock;
1154 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001155 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001156 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001157 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001159 Succ = ConfluenceBlock;
1160 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001161 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001162 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001163 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001165 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001166 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001167 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001168 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1169 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001170 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001171 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001172}
Mike Stump1eb44332009-09-09 15:08:12 +00001173
1174
1175CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001176 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001177 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001178
1179 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1180 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001181 // If we hit a segment of code just containing ';' (NullStmts), we can
1182 // get a null block back. In such cases, just use the LastBlock
1183 if (CFGBlock *newBlock = addStmt(*I))
1184 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001186 if (badCFG)
1187 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001188 }
Mike Stump079bd722010-01-19 22:00:14 +00001189
Ted Kremenek4f880632009-07-17 22:18:43 +00001190 return LastBlock;
1191}
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremenek852274d2009-12-16 03:18:58 +00001193CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1194 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001195 // Create the confluence block that will "merge" the results of the ternary
1196 // expression.
1197 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001198 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001199 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001200 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001202 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001203
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001204 // Create a block for the LHS expression if there is an LHS expression. A
1205 // GCC extension allows LHS to be NULL, causing the condition to be the
1206 // value that is returned instead.
1207 // e.g: x ?: y is shorthand for: x ? x : y;
1208 Succ = ConfluenceBlock;
1209 Block = NULL;
1210 CFGBlock* LHSBlock = NULL;
1211 if (C->getLHS()) {
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001212 LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001213 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001214 return 0;
1215 Block = NULL;
1216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001218 // Create the block for the RHS expression.
1219 Succ = ConfluenceBlock;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001220 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001221 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001222 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001224 // Create the block that will contain the condition.
1225 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Mike Stump00998a02009-07-23 23:25:26 +00001227 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001228 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001229 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001230 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001231 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +00001232 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001233 // If we know the condition is false, add NULL as the successor for
1234 // the block containing the condition. In this case, the confluence
1235 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001236 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +00001237 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001238 } else {
1239 // If we have no LHS expression, add the ConfluenceBlock as a direct
1240 // successor for the block containing the condition. Moreover, we need to
1241 // reverse the order of the predecessors in the ConfluenceBlock because
1242 // the RHSBlock will have been added to the succcessors already, and we
1243 // want the first predecessor to the the block containing the expression
1244 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001245 AddSuccessor(Block, ConfluenceBlock);
Ted Kremeneke4ae4dc2010-11-15 22:59:22 +00001246 // Note that there can possibly only be one predecessor if one of the
1247 // subexpressions resulted in calling a noreturn function.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001248 std::reverse(ConfluenceBlock->pred_begin(),
1249 ConfluenceBlock->pred_end());
1250 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001253 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001254 Block->setTerminator(C);
1255 return addStmt(C->getCond());
1256}
1257
Ted Kremenek4f880632009-07-17 22:18:43 +00001258CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001259 if (DS->isSingleDecl())
1260 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Ted Kremenek4f880632009-07-17 22:18:43 +00001262 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Ted Kremenek4f880632009-07-17 22:18:43 +00001264 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1265 typedef llvm::SmallVector<Decl*,10> BufTy;
1266 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek4f880632009-07-17 22:18:43 +00001268 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1269 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1270 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1271 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Ted Kremenek4f880632009-07-17 22:18:43 +00001273 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1274 // automatically freed with the CFG.
1275 DeclGroupRef DG(*I);
1276 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001277 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001278 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek4f880632009-07-17 22:18:43 +00001280 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001281 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
1284 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001285}
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Ted Kremenek4f880632009-07-17 22:18:43 +00001287/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001288/// DeclStmts and initializers in them.
1289CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1290 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001291
Marcin Swiderski8599e762010-11-03 06:19:35 +00001292 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Marcin Swiderski8599e762010-11-03 06:19:35 +00001294 if (!VD) {
1295 autoCreateBlock();
1296 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001297 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001298 }
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Marcin Swiderski8599e762010-11-03 06:19:35 +00001300 bool IsReference = false;
1301 bool HasTemporaries = false;
1302
1303 // Destructors of temporaries in initialization expression should be called
1304 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001305 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001306 if (Init) {
1307 IsReference = VD->getType()->isReferenceType();
1308 HasTemporaries = isa<CXXExprWithTemporaries>(Init);
1309
1310 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1311 // Generate destructors for temporaries in initialization expression.
1312 VisitForTemporaryDtors(cast<CXXExprWithTemporaries>(Init)->getSubExpr(),
1313 IsReference);
1314 }
1315 }
1316
1317 autoCreateBlock();
1318 AppendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Ted Kremenek4f880632009-07-17 22:18:43 +00001320 if (Init) {
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001321 AddStmtChoice asc = AddStmtChoice().withAsLValue(IsReference);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001322 if (HasTemporaries)
1323 // For expression with temporaries go directly to subexpression to omit
1324 // generating destructors for the second time.
1325 Visit(cast<CXXExprWithTemporaries>(Init)->getSubExpr(), asc);
1326 else
1327 Visit(Init, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Ted Kremenek4f880632009-07-17 22:18:43 +00001330 // If the type of VD is a VLA, then we must process its size expressions.
1331 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1332 VA = FindVA(VA->getElementType().getTypePtr()))
1333 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001335 // Remove variable from local scope.
1336 if (ScopePos && VD == *ScopePos)
1337 ++ScopePos;
1338
Ted Kremenek4f880632009-07-17 22:18:43 +00001339 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001340}
1341
1342CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001343 // We may see an if statement in the middle of a basic block, or it may be the
1344 // first statement we are processing. In either case, we create a new basic
1345 // block. First, we create the blocks for the then...else statements, and
1346 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001347 // middle of a block, we stop processing that block. That block is then the
1348 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001350 // Save local scope position because in case of condition variable ScopePos
1351 // won't be restored when traversing AST.
1352 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1353
1354 // Create local scope for possible condition variable.
1355 // Store scope position. Add implicit destructor.
1356 if (VarDecl* VD = I->getConditionVariable()) {
1357 LocalScope::const_iterator BeginScopePos = ScopePos;
1358 addLocalScopeForVarDecl(VD);
1359 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1360 }
1361
Mike Stump6d9828c2009-07-17 01:31:16 +00001362 // The block we were proccessing is now finished. Make it the successor
1363 // block.
1364 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001365 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001366 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001367 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001368 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001369
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001370 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001371 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 if (Stmt* Else = I->getElse()) {
1374 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001375
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001376 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001377 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001379
1380 // If branch is not a compound statement create implicit scope
1381 // and add destructors.
1382 if (!isa<CompoundStmt>(Else))
1383 addLocalScopeAndDtors(Else);
1384
Ted Kremenek4f880632009-07-17 22:18:43 +00001385 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001386
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001387 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1388 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001389 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001390 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001391 return 0;
1392 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001394
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001395 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001396 CFGBlock* ThenBlock;
1397 {
1398 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001399 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001400 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001401 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001402
1403 // If branch is not a compound statement create implicit scope
1404 // and add destructors.
1405 if (!isa<CompoundStmt>(Then))
1406 addLocalScopeAndDtors(Then);
1407
Ted Kremenek4f880632009-07-17 22:18:43 +00001408 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001409
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001410 if (!ThenBlock) {
1411 // We can reach here if the "then" body has all NullStmts.
1412 // Create an empty block so we can distinguish between true and false
1413 // branches in path-sensitive analyses.
1414 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001415 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001416 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001417 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001418 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001419 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001420 }
1421
Mike Stump6d9828c2009-07-17 01:31:16 +00001422 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001423 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001424
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001425 // Set the terminator of the new block to the If statement.
1426 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
Mike Stump00998a02009-07-23 23:25:26 +00001428 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001429 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001430
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001431 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001432 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1433 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
1435 // Add the condition as the last statement in the new block. This may create
1436 // new blocks as the condition may contain control-flow. Any newly created
1437 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001438 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001439
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001440 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1441 // and the condition variable initialization to the CFG.
1442 if (VarDecl *VD = I->getConditionVariable()) {
1443 if (Expr *Init = VD->getInit()) {
1444 autoCreateBlock();
1445 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1446 addStmt(Init);
1447 }
1448 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001449
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001450 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001451}
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
1453
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001454CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001455 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001456 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001457 // NOTE: If a "return" appears in the middle of a block, this means that the
1458 // code afterwards is DEAD (unreachable). We still keep a basic block
1459 // for that code; a simple "mark-and-sweep" from the entry block will be
1460 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001461
1462 // Create the new block.
1463 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001464
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001465 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001466 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001467 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001468
1469 // Add the return statement to the block. This may create new blocks if R
1470 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001471 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472}
1473
1474CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1475 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001476 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001477 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001478
Ted Kremenek4f880632009-07-17 22:18:43 +00001479 if (!LabelBlock) // This can happen when the body is empty, i.e.
1480 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001481
Ted Kremenek4f880632009-07-17 22:18:43 +00001482 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001483 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001484
1485 // Labels partition blocks, so this is the end of the basic block we were
1486 // processing (L is the block's label). Because this is label (and we have
1487 // already processed the substatement) there is no extra control-flow to worry
1488 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001489 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001490 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001491 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001492
1493 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001494 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001495
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001496 // This block is now the implicit successor of other blocks.
1497 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001498
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001499 return LabelBlock;
1500}
1501
1502CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001503 // Goto is a control-flow statement. Thus we stop processing the current
1504 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001505
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001506 Block = createBlock(false);
1507 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001508
1509 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001510 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001511
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001512 if (I == LabelMap.end())
1513 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001514 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1515 else {
1516 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001517 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001518 AddSuccessor(Block, JT.Block);
1519 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001520
Mike Stump6d9828c2009-07-17 01:31:16 +00001521 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001522}
1523
1524CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001525 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001526
Marcin Swiderski47575f12010-10-01 01:38:14 +00001527 // Save local scope position because in case of condition variable ScopePos
1528 // won't be restored when traversing AST.
1529 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1530
1531 // Create local scope for init statement and possible condition variable.
1532 // Add destructor for init statement and condition variable.
1533 // Store scope position for continue statement.
1534 if (Stmt* Init = F->getInit())
1535 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001536 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1537
Marcin Swiderski47575f12010-10-01 01:38:14 +00001538 if (VarDecl* VD = F->getConditionVariable())
1539 addLocalScopeForVarDecl(VD);
1540 LocalScope::const_iterator ContinueScopePos = ScopePos;
1541
1542 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1543
Mike Stumpfefb9f72009-07-21 01:12:51 +00001544 // "for" is a control-flow statement. Thus we stop processing the current
1545 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001546 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001547 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001548 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001549 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001550 } else
1551 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001552
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001553 // Save the current value for the break targets.
1554 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001555 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001556 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001557
Mike Stump6d9828c2009-07-17 01:31:16 +00001558 // Because of short-circuit evaluation, the condition of the loop can span
1559 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1560 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001561 CFGBlock* ExitConditionBlock = createBlock(false);
1562 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001563
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001564 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001565 ExitConditionBlock->setTerminator(F);
1566
1567 // Now add the actual condition to the condition block. Because the condition
1568 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001569 if (Stmt* C = F->getCond()) {
1570 Block = ExitConditionBlock;
1571 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001572 assert(Block == EntryConditionBlock ||
1573 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001574
1575 // If this block contains a condition variable, add both the condition
1576 // variable and initializer to the CFG.
1577 if (VarDecl *VD = F->getConditionVariable()) {
1578 if (Expr *Init = VD->getInit()) {
1579 autoCreateBlock();
1580 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1581 EntryConditionBlock = addStmt(Init);
1582 assert(Block == EntryConditionBlock);
1583 }
1584 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001585
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001586 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001587 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001588 return 0;
1589 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001590 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001591
Mike Stump6d9828c2009-07-17 01:31:16 +00001592 // The condition block is the implicit successor for the loop body as well as
1593 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001594 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001595
Mike Stump00998a02009-07-23 23:25:26 +00001596 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001597 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Mike Stump00998a02009-07-23 23:25:26 +00001599 if (F->getCond())
1600 KnownVal = TryEvaluateBool(F->getCond());
1601
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001602 // Now create the loop body.
1603 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001604 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001605
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001606 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001607 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1608 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001609
Ted Kremenekaf603f72007-08-30 18:39:40 +00001610 // Create a new block to contain the (bottom) of the loop body.
1611 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001612
1613 // Loop body should end with destructor of Condition variable (if any).
1614 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001615
Ted Kremeneke9334502008-09-04 21:48:47 +00001616 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001617 // Generate increment code in its own basic block. This is the target of
1618 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001619 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001620 } else {
1621 // No increment code. Create a special, empty, block that is used as the
1622 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001623 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001624 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001625 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001626
Ted Kremenek3575f842009-04-28 00:51:56 +00001627 // Finish up the increment (or empty) block if it hasn't been already.
1628 if (Block) {
1629 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001630 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001631 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001632 Block = 0;
1633 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001634
Marcin Swiderski47575f12010-10-01 01:38:14 +00001635 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
Ted Kremenek3575f842009-04-28 00:51:56 +00001637 // The starting block for the loop increment is the block that should
1638 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001639 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001640
Marcin Swiderski47575f12010-10-01 01:38:14 +00001641 // If body is not a compound statement create implicit scope
1642 // and add destructors.
1643 if (!isa<CompoundStmt>(F->getBody()))
1644 addLocalScopeAndDtors(F->getBody());
1645
Mike Stump6d9828c2009-07-17 01:31:16 +00001646 // Now populate the body block, and in the process create new blocks as we
1647 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001648 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001649
1650 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001651 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001652 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001653 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001654
Ted Kremenek941fde82009-07-24 04:47:11 +00001655 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001656 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001657 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001658
Ted Kremenek941fde82009-07-24 04:47:11 +00001659 // Link up the condition block with the code that follows the loop. (the
1660 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001661 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001662
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001663 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001664 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001665 if (Stmt* I = F->getInit()) {
1666 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001667 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001668 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001669
1670 // There is no loop initialization. We are thus basically a while loop.
1671 // NULL out Block to force lazy block construction.
1672 Block = NULL;
1673 Succ = EntryConditionBlock;
1674 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001675}
1676
Ted Kremenek115c1b92010-04-11 17:02:10 +00001677CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1678 if (asc.alwaysAdd()) {
1679 autoCreateBlock();
1680 AppendStmt(Block, M, asc);
1681 }
1682 return Visit(M->getBase(),
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001683 AddStmtChoice().withAsLValue(!M->isArrow()));
Ted Kremenek115c1b92010-04-11 17:02:10 +00001684}
1685
Ted Kremenek514de5a2008-11-11 17:10:00 +00001686CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1687 // Objective-C fast enumeration 'for' statements:
1688 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1689 //
1690 // for ( Type newVariable in collection_expression ) { statements }
1691 //
1692 // becomes:
1693 //
1694 // prologue:
1695 // 1. collection_expression
1696 // T. jump to loop_entry
1697 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001698 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001699 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1700 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1701 // TB:
1702 // statements
1703 // T. jump to loop_entry
1704 // FB:
1705 // what comes after
1706 //
1707 // and
1708 //
1709 // Type existingItem;
1710 // for ( existingItem in expression ) { statements }
1711 //
1712 // becomes:
1713 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001714 // the same with newVariable replaced with existingItem; the binding works
1715 // the same except that for one ObjCForCollectionStmt::getElement() returns
1716 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001717 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
Ted Kremenek514de5a2008-11-11 17:10:00 +00001719 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001720
Ted Kremenek514de5a2008-11-11 17:10:00 +00001721 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001722 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001723 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001724 LoopSuccessor = Block;
1725 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001726 } else
1727 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001729 // Build the condition blocks.
1730 CFGBlock* ExitConditionBlock = createBlock(false);
1731 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001732
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001733 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001734 ExitConditionBlock->setTerminator(S);
1735
1736 // The last statement in the block should be the ObjCForCollectionStmt, which
1737 // performs the actual binding to 'element' and determines if there are any
1738 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001739 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001740 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001742 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001743 // generate new blocks as necesary. We DON'T add the statement by default to
1744 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001745 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001746 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001747 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001748 return 0;
1749 Block = 0;
1750 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001751
1752 // The condition block is the implicit successor for the loop body as well as
1753 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001754 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001755
Ted Kremenek514de5a2008-11-11 17:10:00 +00001756 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001757 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001758 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001759 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1760 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1761 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001762
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001763 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1764 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001765
Ted Kremenek4f880632009-07-17 22:18:43 +00001766 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001768 if (!BodyBlock)
1769 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001770 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001771 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001772 return 0;
1773 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001774
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001775 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001776 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001777 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001778
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001779 // Link up the condition block with the code that follows the loop.
1780 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001781 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001782
Ted Kremenek514de5a2008-11-11 17:10:00 +00001783 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001784 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001785 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001786}
1787
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001788CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1789 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001791 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001792 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001793
Ted Kremenekda5348e2009-05-05 23:11:51 +00001794 // The sync body starts its own basic block. This makes it a little easier
1795 // for diagnostic clients.
1796 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001797 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001798 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001799
Ted Kremenekda5348e2009-05-05 23:11:51 +00001800 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001801 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001802 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001803
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001804 // Add the @synchronized to the CFG.
1805 autoCreateBlock();
1806 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1807
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001808 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001809 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001810}
Mike Stump6d9828c2009-07-17 01:31:16 +00001811
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001812CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001813 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001814 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001815}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001816
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001817CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001818 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001819
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001820 // Save local scope position because in case of condition variable ScopePos
1821 // won't be restored when traversing AST.
1822 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1823
1824 // Create local scope for possible condition variable.
1825 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001826 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001827 if (VarDecl* VD = W->getConditionVariable()) {
1828 addLocalScopeForVarDecl(VD);
1829 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1830 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001831
Mike Stumpfefb9f72009-07-21 01:12:51 +00001832 // "while" is a control-flow statement. Thus we stop processing the current
1833 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001834 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001835 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001836 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001837 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001838 } else
1839 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001840
1841 // Because of short-circuit evaluation, the condition of the loop can span
1842 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1843 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001844 CFGBlock* ExitConditionBlock = createBlock(false);
1845 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001846
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001847 // Set the terminator for the "exit" condition block.
1848 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001849
1850 // Now add the actual condition to the condition block. Because the condition
1851 // itself may contain control-flow, new blocks may be created. Thus we update
1852 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001853 if (Stmt* C = W->getCond()) {
1854 Block = ExitConditionBlock;
1855 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001856 // The condition might finish the current 'Block'.
1857 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001858
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001859 // If this block contains a condition variable, add both the condition
1860 // variable and initializer to the CFG.
1861 if (VarDecl *VD = W->getConditionVariable()) {
1862 if (Expr *Init = VD->getInit()) {
1863 autoCreateBlock();
1864 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1865 EntryConditionBlock = addStmt(Init);
1866 assert(Block == EntryConditionBlock);
1867 }
1868 }
1869
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001870 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001871 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001872 return 0;
1873 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001874 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001875
1876 // The condition block is the implicit successor for the loop body as well as
1877 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001878 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001879
Mike Stump00998a02009-07-23 23:25:26 +00001880 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001881 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001882
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001883 // Process the loop body.
1884 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001885 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001886
1887 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001888 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1889 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1890 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001891
Mike Stump6d9828c2009-07-17 01:31:16 +00001892 // Create an empty block to represent the transition block for looping back
1893 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001894 Block = 0;
1895 assert(Succ == EntryConditionBlock);
1896 Succ = createBlock();
1897 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001898 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001899
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001900 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001901 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001903 // NULL out Block to force lazy instantiation of blocks for the body.
1904 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001905
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001906 // Loop body should end with destructor of Condition variable (if any).
1907 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1908
1909 // If body is not a compound statement create implicit scope
1910 // and add destructors.
1911 if (!isa<CompoundStmt>(W->getBody()))
1912 addLocalScopeAndDtors(W->getBody());
1913
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001914 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001915 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenekaf603f72007-08-30 18:39:40 +00001917 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001918 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001919 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001920 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001921 return 0;
1922 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001923
Ted Kremenek941fde82009-07-24 04:47:11 +00001924 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001925 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001926 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001927
Ted Kremenek941fde82009-07-24 04:47:11 +00001928 // Link up the condition block with the code that follows the loop. (the
1929 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001930 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001931
1932 // There can be no more statements in the condition block since we loop back
1933 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001934 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001935
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001936 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001937 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001938 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001939}
Mike Stump1eb44332009-09-09 15:08:12 +00001940
1941
Ted Kremenek4f880632009-07-17 22:18:43 +00001942CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1943 // FIXME: For now we pretend that @catch and the code it contains does not
1944 // exit.
1945 return Block;
1946}
Mike Stump6d9828c2009-07-17 01:31:16 +00001947
Ted Kremenek2fda5042008-12-09 20:20:09 +00001948CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1949 // FIXME: This isn't complete. We basically treat @throw like a return
1950 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001951
Ted Kremenek6c249722009-09-24 18:45:41 +00001952 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001953 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001954 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001955
Ted Kremenek2fda5042008-12-09 20:20:09 +00001956 // Create the new block.
1957 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001958
Ted Kremenek2fda5042008-12-09 20:20:09 +00001959 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001960 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
1962 // Add the statement to the block. This may create new blocks if S contains
1963 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001964 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001965}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001966
Mike Stump0979d802009-07-22 22:56:04 +00001967CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001968 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001969 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001970 return 0;
1971
1972 // Create the new block.
1973 Block = createBlock(false);
1974
Mike Stump5d1d2022010-01-19 02:20:09 +00001975 if (TryTerminatedBlock)
1976 // The current try statement is the only successor.
1977 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001978 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001979 // otherwise the Exit block is the only successor.
1980 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001981
1982 // Add the statement to the block. This may create new blocks if S contains
1983 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001984 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001985}
1986
Ted Kremenek4f880632009-07-17 22:18:43 +00001987CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001988 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001989
Mike Stump8f9893a2009-07-21 01:27:50 +00001990 // "do...while" is a control-flow statement. Thus we stop processing the
1991 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001992 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001993 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001994 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001995 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001996 } else
1997 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
1999 // Because of short-circuit evaluation, the condition of the loop can span
2000 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
2001 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002002 CFGBlock* ExitConditionBlock = createBlock(false);
2003 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002004
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002005 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002006 ExitConditionBlock->setTerminator(D);
2007
2008 // Now add the actual condition to the condition block. Because the condition
2009 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002010 if (Stmt* C = D->getCond()) {
2011 Block = ExitConditionBlock;
2012 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002013 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002014 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002015 return 0;
2016 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002017 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002018
Ted Kremenek54827132008-02-27 07:20:00 +00002019 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002020 Succ = EntryConditionBlock;
2021
Mike Stump00998a02009-07-23 23:25:26 +00002022 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00002023 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002024
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002025 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002026 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002027 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002028 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002029
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002030 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002031 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2032 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2033 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002034
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002035 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002036 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002037
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002038 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002039 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002041 // NULL out Block to force lazy instantiation of blocks for the body.
2042 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002043
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002044 // If body is not a compound statement create implicit scope
2045 // and add destructors.
2046 if (!isa<CompoundStmt>(D->getBody()))
2047 addLocalScopeAndDtors(D->getBody());
2048
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002049 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002050 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002051
Ted Kremenekaf603f72007-08-30 18:39:40 +00002052 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002053 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002054 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002055 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002056 return 0;
2057 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenekd173dc72010-08-17 20:59:56 +00002059 if (!KnownVal.isFalse()) {
2060 // Add an intermediate block between the BodyBlock and the
2061 // ExitConditionBlock to represent the "loop back" transition. Create an
2062 // empty block to represent the transition block for looping back to the
2063 // head of the loop.
2064 // FIXME: Can we do this more efficiently without adding another block?
2065 Block = NULL;
2066 Succ = BodyBlock;
2067 CFGBlock *LoopBackBlock = createBlock();
2068 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002069
Ted Kremenekd173dc72010-08-17 20:59:56 +00002070 // Add the loop body entry as a successor to the condition.
2071 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2072 }
2073 else
2074 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002075 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002076
Ted Kremenek941fde82009-07-24 04:47:11 +00002077 // Link up the condition block with the code that follows the loop.
2078 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002079 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002080
2081 // There can be no more statements in the body block(s) since we loop back to
2082 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002083 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002084
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002085 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002086 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002087 return BodyBlock;
2088}
2089
2090CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2091 // "continue" is a control-flow statement. Thus we stop processing the
2092 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002093 if (badCFG)
2094 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002095
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002096 // Now create a new block that ends with the continue statement.
2097 Block = createBlock(false);
2098 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002099
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002100 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002101 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002102 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00002103 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002104 AddSuccessor(Block, ContinueJumpTarget.Block);
2105 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002106 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002107
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002108 return Block;
2109}
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002111CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002112 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002113
Ted Kremenek852274d2009-12-16 03:18:58 +00002114 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002115 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002116 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002117 }
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Ted Kremenek4f880632009-07-17 22:18:43 +00002119 // VLA types have expressions that must be evaluated.
2120 if (E->isArgumentType()) {
2121 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2122 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2123 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002124 }
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Mike Stump6d9828c2009-07-17 01:31:16 +00002126 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002127}
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Ted Kremenek4f880632009-07-17 22:18:43 +00002129/// VisitStmtExpr - Utility method to handle (nested) statement
2130/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002131CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2132 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002133 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002134 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002135 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002136 return VisitCompoundStmt(SE->getSubStmt());
2137}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002138
Ted Kremenek411cdee2008-04-16 21:10:48 +00002139CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002140 // "switch" is a control-flow statement. Thus we stop processing the current
2141 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002142 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002143
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002144 // Save local scope position because in case of condition variable ScopePos
2145 // won't be restored when traversing AST.
2146 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2147
2148 // Create local scope for possible condition variable.
2149 // Store scope position. Add implicit destructor.
2150 if (VarDecl* VD = Terminator->getConditionVariable()) {
2151 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2152 addLocalScopeForVarDecl(VD);
2153 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2154 }
2155
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002156 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002157 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002158 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002159 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002160 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002161
2162 // Save the current "switch" context.
2163 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002164 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002165 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002166
Mike Stump6d9828c2009-07-17 01:31:16 +00002167 // Set the "default" case to be the block after the switch statement. If the
2168 // switch statement contains a "default:", this value will be overwritten with
2169 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002170 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002171
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002172 // Create a new block that will contain the switch statement.
2173 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002174
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002175 // Now process the switch body. The code after the switch is the implicit
2176 // successor.
2177 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002178 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
2180 // When visiting the body, the case statements should automatically get linked
2181 // up to the switch. We also don't keep a pointer to the body, since all
2182 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002183 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002184 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002185
2186 // If body is not a compound statement create implicit scope
2187 // and add destructors.
2188 if (!isa<CompoundStmt>(Terminator->getBody()))
2189 addLocalScopeAndDtors(Terminator->getBody());
2190
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002191 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002192 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002193 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002194 return 0;
2195 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002196
Mike Stump6d9828c2009-07-17 01:31:16 +00002197 // If we have no "default:" case, the default transition is to the code
2198 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002199 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002200
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002201 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002202 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002203 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002204 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002205 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002206
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002207 // Finally, if the SwitchStmt contains a condition variable, add both the
2208 // SwitchStmt and the condition variable initialization to the CFG.
2209 if (VarDecl *VD = Terminator->getConditionVariable()) {
2210 if (Expr *Init = VD->getInit()) {
2211 autoCreateBlock();
2212 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2213 addStmt(Init);
2214 }
2215 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002216
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002217 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002218}
2219
Ted Kremenek4f880632009-07-17 22:18:43 +00002220CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002221 // CaseStmts are essentially labels, so they are the first statement in a
2222 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002223 CFGBlock *TopBlock = 0, *LastBlock = 0;
2224
2225 if (Stmt *Sub = CS->getSubStmt()) {
2226 // For deeply nested chains of CaseStmts, instead of doing a recursion
2227 // (which can blow out the stack), manually unroll and create blocks
2228 // along the way.
2229 while (isa<CaseStmt>(Sub)) {
2230 CFGBlock *CurrentBlock = createBlock(false);
2231 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002232
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002233 if (TopBlock)
2234 AddSuccessor(LastBlock, CurrentBlock);
2235 else
2236 TopBlock = CurrentBlock;
2237
2238 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2239 LastBlock = CurrentBlock;
2240
2241 CS = cast<CaseStmt>(Sub);
2242 Sub = CS->getSubStmt();
2243 }
2244
2245 addStmt(Sub);
2246 }
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002248 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002249 if (!CaseBlock)
2250 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002251
2252 // Cases statements partition blocks, so this is the top of the basic block we
2253 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002254 CaseBlock->setLabel(CS);
2255
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002256 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002257 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002258
2259 // Add this block to the list of successors for the block with the switch
2260 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002261 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002262 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002263
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002264 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2265 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002266
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002267 if (TopBlock) {
2268 AddSuccessor(LastBlock, CaseBlock);
2269 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002270 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002271 // This block is now the implicit successor of other blocks.
2272 Succ = CaseBlock;
2273 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002274
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002275 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002276}
Mike Stump6d9828c2009-07-17 01:31:16 +00002277
Ted Kremenek411cdee2008-04-16 21:10:48 +00002278CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002279 if (Terminator->getSubStmt())
2280 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002282 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002283
2284 if (!DefaultCaseBlock)
2285 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002286
2287 // Default statements partition blocks, so this is the top of the basic block
2288 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002289 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002290
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002291 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002292 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002293
Mike Stump6d9828c2009-07-17 01:31:16 +00002294 // Unlike case statements, we don't add the default block to the successors
2295 // for the switch statement immediately. This is done when we finish
2296 // processing the switch statement. This allows for the default case
2297 // (including a fall-through to the code after the switch statement) to always
2298 // be the last successor of a switch-terminated block.
2299
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002300 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2301 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002302
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002303 // This block is now the implicit successor of other blocks.
2304 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002305
2306 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002307}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002308
Mike Stump5d1d2022010-01-19 02:20:09 +00002309CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2310 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2311 // current block.
2312 CFGBlock* TrySuccessor = NULL;
2313
2314 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002315 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002316 return 0;
2317 TrySuccessor = Block;
2318 } else TrySuccessor = Succ;
2319
Mike Stumpa1f93632010-01-20 01:15:34 +00002320 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002321
2322 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002323 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002324 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002325 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002326
Mike Stumpa1f93632010-01-20 01:15:34 +00002327 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002328 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2329 // The code after the try is the implicit successor.
2330 Succ = TrySuccessor;
2331 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002332 if (CS->getExceptionDecl() == 0) {
2333 HasCatchAll = true;
2334 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002335 Block = NULL;
2336 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2337 if (CatchBlock == 0)
2338 return 0;
2339 // Add this block to the list of successors for the block with the try
2340 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002341 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002342 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002343 if (!HasCatchAll) {
2344 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002345 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002346 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002347 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002348 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002349
2350 // The code after the try is the implicit successor.
2351 Succ = TrySuccessor;
2352
Mike Stumpf00cca52010-01-20 01:30:58 +00002353 // Save the current "try" context.
2354 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2355 TryTerminatedBlock = NewTryTerminatedBlock;
2356
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002357 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002358 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002359 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002360 return Block;
2361}
2362
2363CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2364 // CXXCatchStmt are treated like labels, so they are the first statement in a
2365 // block.
2366
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002367 // Save local scope position because in case of exception variable ScopePos
2368 // won't be restored when traversing AST.
2369 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2370
2371 // Create local scope for possible exception variable.
2372 // Store scope position. Add implicit destructor.
2373 if (VarDecl* VD = CS->getExceptionDecl()) {
2374 LocalScope::const_iterator BeginScopePos = ScopePos;
2375 addLocalScopeForVarDecl(VD);
2376 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2377 }
2378
Mike Stump5d1d2022010-01-19 02:20:09 +00002379 if (CS->getHandlerBlock())
2380 addStmt(CS->getHandlerBlock());
2381
2382 CFGBlock* CatchBlock = Block;
2383 if (!CatchBlock)
2384 CatchBlock = createBlock();
2385
2386 CatchBlock->setLabel(CS);
2387
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002388 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002389 return 0;
2390
2391 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2392 Block = NULL;
2393
2394 return CatchBlock;
2395}
2396
Marcin Swiderski8599e762010-11-03 06:19:35 +00002397CFGBlock *CFGBuilder::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E,
2398 AddStmtChoice asc) {
2399 if (BuildOpts.AddImplicitDtors) {
2400 // If adding implicit destructors visit the full expression for adding
2401 // destructors of temporaries.
2402 VisitForTemporaryDtors(E->getSubExpr());
2403
2404 // Full expression has to be added as CFGStmt so it will be sequenced
2405 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002406 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002407 }
2408 return Visit(E->getSubExpr(), asc);
2409}
2410
Zhongxing Xua725ed42010-11-01 13:04:58 +00002411CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2412 AddStmtChoice asc) {
2413 if (asc.alwaysAdd()) {
2414 autoCreateBlock();
2415 AppendStmt(Block, E, asc);
2416
2417 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002418 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002419 }
2420 return Visit(E->getSubExpr(), asc);
2421}
2422
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002423CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2424 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002425 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002426 if (!C->isElidable())
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002427 AppendStmt(Block, C, asc.withAlwaysAdd(true));
2428
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002429 return VisitChildren(C);
2430}
2431
Zhongxing Xua725ed42010-11-01 13:04:58 +00002432CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2433 AddStmtChoice asc) {
2434 if (asc.alwaysAdd()) {
2435 autoCreateBlock();
2436 AppendStmt(Block, E, asc);
2437 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002438 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002439 }
2440 return Visit(E->getSubExpr(), asc);
2441}
2442
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002443CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2444 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002445 autoCreateBlock();
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002446 AppendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002447 return VisitChildren(C);
2448}
2449
Ted Kremenekad5a8942010-08-02 23:46:59 +00002450CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002451 AddStmtChoice asc) {
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002452 autoCreateBlock();
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002453 AppendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002454 return VisitChildren(C);
2455}
2456
Zhongxing Xua725ed42010-11-01 13:04:58 +00002457CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2458 AddStmtChoice asc) {
2459 if (asc.alwaysAdd()) {
2460 autoCreateBlock();
2461 AppendStmt(Block, E, asc);
2462 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002463 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002464 }
2465 return Visit(E->getSubExpr(), asc);
2466}
2467
Ted Kremenek19bb3562007-08-28 19:26:49 +00002468CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002469 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002470 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002471
Ted Kremenek19bb3562007-08-28 19:26:49 +00002472 if (!IBlock) {
2473 IBlock = createBlock(false);
2474 cfg->setIndirectGotoBlock(IBlock);
2475 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002476
Ted Kremenek19bb3562007-08-28 19:26:49 +00002477 // IndirectGoto is a control-flow statement. Thus we stop processing the
2478 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002479 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002480 return 0;
2481
Ted Kremenek19bb3562007-08-28 19:26:49 +00002482 Block = createBlock(false);
2483 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002484 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002485 return addStmt(I->getTarget());
2486}
2487
Marcin Swiderski8599e762010-11-03 06:19:35 +00002488CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2489tryAgain:
2490 if (!E) {
2491 badCFG = true;
2492 return NULL;
2493 }
2494 switch (E->getStmtClass()) {
2495 default:
2496 return VisitChildrenForTemporaryDtors(E);
2497
2498 case Stmt::BinaryOperatorClass:
2499 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2500
2501 case Stmt::CXXBindTemporaryExprClass:
2502 return VisitCXXBindTemporaryExprForTemporaryDtors(
2503 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2504
2505 case Stmt::ConditionalOperatorClass:
2506 return VisitConditionalOperatorForTemporaryDtors(
2507 cast<ConditionalOperator>(E), BindToTemporary);
2508
2509 case Stmt::ImplicitCastExprClass:
2510 // For implicit cast we want BindToTemporary to be passed further.
2511 E = cast<CastExpr>(E)->getSubExpr();
2512 goto tryAgain;
2513
2514 case Stmt::ParenExprClass:
2515 E = cast<ParenExpr>(E)->getSubExpr();
2516 goto tryAgain;
2517 }
2518}
2519
2520CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2521 // When visiting children for destructors we want to visit them in reverse
2522 // order. Because there's no reverse iterator for children must to reverse
2523 // them in helper vector.
2524 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2525 ChildrenVect ChildrenRev;
2526 for (Stmt::child_iterator I = E->child_begin(), L = E->child_end();
2527 I != L; ++I) {
2528 if (*I) ChildrenRev.push_back(*I);
2529 }
2530
2531 CFGBlock *B = Block;
2532 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2533 L = ChildrenRev.rend(); I != L; ++I) {
2534 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2535 B = R;
2536 }
2537 return B;
2538}
2539
2540CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2541 if (E->isLogicalOp()) {
2542 // Destructors for temporaries in LHS expression should be called after
2543 // those for RHS expression. Even if this will unnecessarily create a block,
2544 // this block will be used at least by the full expression.
2545 autoCreateBlock();
2546 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2547 if (badCFG)
2548 return NULL;
2549
2550 Succ = ConfluenceBlock;
2551 Block = NULL;
2552 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2553
2554 if (RHSBlock) {
2555 if (badCFG)
2556 return NULL;
2557
2558 // If RHS expression did produce destructors we need to connect created
2559 // blocks to CFG in same manner as for binary operator itself.
2560 CFGBlock *LHSBlock = createBlock(false);
2561 LHSBlock->setTerminator(CFGTerminator(E, true));
2562
2563 // For binary operator LHS block is before RHS in list of predecessors
2564 // of ConfluenceBlock.
2565 std::reverse(ConfluenceBlock->pred_begin(),
2566 ConfluenceBlock->pred_end());
2567
2568 // See if this is a known constant.
2569 TryResult KnownVal = TryEvaluateBool(E->getLHS());
2570 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2571 KnownVal.negate();
2572
2573 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2574 // itself.
2575 if (E->getOpcode() == BO_LOr) {
2576 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2577 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2578 } else {
2579 assert (E->getOpcode() == BO_LAnd);
2580 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2581 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2582 }
2583
2584 Block = LHSBlock;
2585 return LHSBlock;
2586 }
2587
2588 Block = ConfluenceBlock;
2589 return ConfluenceBlock;
2590 }
2591
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002592 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002593 // For assignment operator (=) LHS expression is visited
2594 // before RHS expression. For destructors visit them in reverse order.
2595 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2596 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2597 return LHSBlock ? LHSBlock : RHSBlock;
2598 }
2599
2600 // For any other binary operator RHS expression is visited before
2601 // LHS expression (order of children). For destructors visit them in reverse
2602 // order.
2603 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2604 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2605 return RHSBlock ? RHSBlock : LHSBlock;
2606}
2607
2608CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2609 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2610 // First add destructors for temporaries in subexpression.
2611 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002612 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002613 // If lifetime of temporary is not prolonged (by assigning to constant
2614 // reference) add destructor for it.
2615 autoCreateBlock();
2616 appendTemporaryDtor(Block, E);
2617 B = Block;
2618 }
2619 return B;
2620}
2621
2622CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2623 ConditionalOperator *E, bool BindToTemporary) {
2624 // First add destructors for condition expression. Even if this will
2625 // unnecessarily create a block, this block will be used at least by the full
2626 // expression.
2627 autoCreateBlock();
2628 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2629 if (badCFG)
2630 return NULL;
2631
2632 // Try to add block with destructors for LHS expression.
2633 CFGBlock *LHSBlock = NULL;
2634 if (E->getLHS()) {
2635 Succ = ConfluenceBlock;
2636 Block = NULL;
2637 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2638 if (badCFG)
2639 return NULL;
2640 }
2641
2642 // Try to add block with destructors for RHS expression;
2643 Succ = ConfluenceBlock;
2644 Block = NULL;
2645 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2646 if (badCFG)
2647 return NULL;
2648
2649 if (!RHSBlock && !LHSBlock) {
2650 // If neither LHS nor RHS expression had temporaries to destroy don't create
2651 // more blocks.
2652 Block = ConfluenceBlock;
2653 return Block;
2654 }
2655
2656 Block = createBlock(false);
2657 Block->setTerminator(CFGTerminator(E, true));
2658
2659 // See if this is a known constant.
2660 const TryResult &KnownVal = TryEvaluateBool(E->getCond());
2661
2662 if (LHSBlock) {
2663 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
2664 } else if (KnownVal.isFalse()) {
2665 AddSuccessor(Block, NULL);
2666 } else {
2667 AddSuccessor(Block, ConfluenceBlock);
2668 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2669 }
2670
2671 if (!RHSBlock)
2672 RHSBlock = ConfluenceBlock;
2673 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
2674
2675 return Block;
2676}
2677
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002678} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002679
Mike Stump6d9828c2009-07-17 01:31:16 +00002680/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2681/// no successors or predecessors. If this is the first block created in the
2682/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002683CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002684 bool first_block = begin() == end();
2685
2686 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002687 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2688 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2689 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002690
2691 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002692 if (first_block)
2693 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002694
2695 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002696 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002697}
2698
Ted Kremenek026473c2007-08-23 16:51:22 +00002699/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2700/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002701CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002702 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002703 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002704 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002705}
2706
Ted Kremenek63f58872007-10-01 19:33:33 +00002707//===----------------------------------------------------------------------===//
2708// CFG: Queries for BlkExprs.
2709//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002710
Ted Kremenek63f58872007-10-01 19:33:33 +00002711namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002712 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002713}
2714
Ted Kremenek8a693662009-12-23 23:37:10 +00002715static void FindSubExprAssignments(Stmt *S,
2716 llvm::SmallPtrSet<Expr*,50>& Set) {
2717 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002718 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002719
Ted Kremenek8a693662009-12-23 23:37:10 +00002720 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002721 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002722 if (!child)
2723 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002724
Ted Kremenek8a693662009-12-23 23:37:10 +00002725 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002726 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002727
Ted Kremenek8a693662009-12-23 23:37:10 +00002728 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002729 }
2730}
2731
Ted Kremenek63f58872007-10-01 19:33:33 +00002732static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2733 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002734
2735 // Look for assignments that are used as subexpressions. These are the only
2736 // assignments that we want to *possibly* register as a block-level
2737 // expression. Basically, if an assignment occurs both in a subexpression and
2738 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002739 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002740
Ted Kremenek63f58872007-10-01 19:33:33 +00002741 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002742 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002743 if (CFGStmt S = BI->getAs<CFGStmt>())
2744 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002745
Ted Kremenek411cdee2008-04-16 21:10:48 +00002746 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002747
2748 // Iterate over the statements again on identify the Expr* and Stmt* at the
2749 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002750
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002751 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2752 CFGStmt CS = BI->getAs<CFGStmt>();
2753 if (!CS.isValid())
2754 continue;
2755 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002756
Ted Kremenek411cdee2008-04-16 21:10:48 +00002757 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002758 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002759 // expression are really "statements" whose value is never used by
2760 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002761 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002762 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002763 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2764 // Special handling for statement expressions. The last statement in
2765 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002766 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002767 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002768 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002769 (*M)[C->body_back()] = x;
2770 }
2771 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002772
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002773 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002774 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002775 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002776 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002777
Ted Kremenek411cdee2008-04-16 21:10:48 +00002778 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002779
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002780 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002781
Ted Kremenek390e48b2008-11-12 21:11:49 +00002782 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002783 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002784 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002785 }
2786 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002787
Ted Kremenek63f58872007-10-01 19:33:33 +00002788 return M;
2789}
2790
Ted Kremenek86946742008-01-17 20:48:37 +00002791CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2792 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002793 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002794
Ted Kremenek63f58872007-10-01 19:33:33 +00002795 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002796 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002797 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002798}
2799
2800unsigned CFG::getNumBlkExprs() {
2801 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2802 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002803
2804 // We assume callers interested in the number of BlkExprs will want
2805 // the map constructed if it doesn't already exist.
2806 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2807 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00002808}
2809
Ted Kremenek274f4332008-04-28 18:00:46 +00002810//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002811// Filtered walking of the CFG.
2812//===----------------------------------------------------------------------===//
2813
2814bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002815 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002816
2817 if (F.IgnoreDefaultsWithCoveredEnums) {
2818 // If the 'To' has no label or is labeled but the label isn't a
2819 // CaseStmt then filter this edge.
2820 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002821 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002822 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002823 const Stmt *L = To->getLabel();
2824 if (!L || !isa<CaseStmt>(L))
2825 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002826 }
2827 }
2828 }
2829
2830 return false;
2831}
2832
2833//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002834// Cleanup: CFG dstor.
2835//===----------------------------------------------------------------------===//
2836
Ted Kremenek63f58872007-10-01 19:33:33 +00002837CFG::~CFG() {
2838 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2839}
Mike Stump6d9828c2009-07-17 01:31:16 +00002840
Ted Kremenek7dba8602007-08-29 21:56:09 +00002841//===----------------------------------------------------------------------===//
2842// CFG pretty printing
2843//===----------------------------------------------------------------------===//
2844
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002845namespace {
2846
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002847class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002848 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002849 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002850 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002851 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002852 signed CurrentBlock;
2853 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002854 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002855public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002856
Chris Lattnere4f21422009-06-30 01:26:17 +00002857 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2858 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002859 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2860 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002861 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002862 BI != BEnd; ++BI, ++j ) {
2863 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2864 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2865 StmtMap[SE] = P;
2866
2867 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2868 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002869
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002870 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2871 if (VarDecl* VD = IS->getConditionVariable())
2872 DeclMap[VD] = P;
2873
2874 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2875 if (VarDecl* VD = FS->getConditionVariable())
2876 DeclMap[VD] = P;
2877
2878 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2879 if (VarDecl* VD = WS->getConditionVariable())
2880 DeclMap[VD] = P;
2881
2882 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2883 if (VarDecl* VD = SS->getConditionVariable())
2884 DeclMap[VD] = P;
2885
2886 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2887 if (VarDecl* VD = CS->getExceptionDecl())
2888 DeclMap[VD] = P;
2889 }
2890 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002891 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002892 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002893 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002894
Ted Kremenek42a509f2007-08-31 21:30:12 +00002895 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002896
Chris Lattnere4f21422009-06-30 01:26:17 +00002897 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002898 void setBlockID(signed i) { CurrentBlock = i; }
2899 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002900
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002901 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2902 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002903
2904 if (I == StmtMap.end())
2905 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002906
2907 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002908 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002909 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002910 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002911
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002912 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002913 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002914 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002915
2916 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2917 DeclMapTy::iterator I = DeclMap.find(D);
2918
2919 if (I == DeclMap.end())
2920 return false;
2921
2922 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2923 && I->second.second == CurrentStmt) {
2924 return false;
2925 }
2926
2927 OS << "[B" << I->second.first << "." << I->second.second << "]";
2928 return true;
2929 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002930};
Chris Lattnere4f21422009-06-30 01:26:17 +00002931} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002932
Chris Lattnere4f21422009-06-30 01:26:17 +00002933
2934namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002935class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002936 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002937
Ted Kremeneka95d3752008-09-13 05:16:45 +00002938 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002939 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002940 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002941public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002942 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002943 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002944 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002945
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002946 void VisitIfStmt(IfStmt* I) {
2947 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002948 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002949 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002950
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002951 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002952 void VisitStmt(Stmt* Terminator) {
2953 Terminator->printPretty(OS, Helper, Policy);
2954 }
2955
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002956 void VisitForStmt(ForStmt* F) {
2957 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002958 if (F->getInit())
2959 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002960 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002961 if (Stmt* C = F->getCond())
2962 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002963 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002964 if (F->getInc())
2965 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002966 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002967 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002968
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002969 void VisitWhileStmt(WhileStmt* W) {
2970 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002971 if (Stmt* C = W->getCond())
2972 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002973 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002974
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002975 void VisitDoStmt(DoStmt* D) {
2976 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002977 if (Stmt* C = D->getCond())
2978 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002979 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002980
Ted Kremenek411cdee2008-04-16 21:10:48 +00002981 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002982 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002983 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002984 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002985
Mike Stump5d1d2022010-01-19 02:20:09 +00002986 void VisitCXXTryStmt(CXXTryStmt* CS) {
2987 OS << "try ...";
2988 }
2989
Ted Kremenek805e9a82007-08-31 21:49:40 +00002990 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002991 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002992 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002993 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002994
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002995 void VisitChooseExpr(ChooseExpr* C) {
2996 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002997 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002998 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002999 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003000
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003001 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3002 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003003 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003004 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003005
Ted Kremenek805e9a82007-08-31 21:49:40 +00003006 void VisitBinaryOperator(BinaryOperator* B) {
3007 if (!B->isLogicalOp()) {
3008 VisitExpr(B);
3009 return;
3010 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003011
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003012 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003013
Ted Kremenek805e9a82007-08-31 21:49:40 +00003014 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003015 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003016 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003017 return;
John McCall2de56d12010-08-25 11:45:40 +00003018 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003019 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003020 return;
3021 default:
3022 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003023 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003024 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003025
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003026 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003027 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003028 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003029};
Chris Lattnere4f21422009-06-30 01:26:17 +00003030} // end anonymous namespace
3031
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003032static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003033 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003034 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3035 Stmt *S = CS;
3036
3037 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003038
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003039 // special printing for statement-expressions.
3040 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3041 CompoundStmt* Sub = SE->getSubStmt();
3042
3043 if (Sub->child_begin() != Sub->child_end()) {
3044 OS << "({ ... ; ";
3045 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3046 OS << " })\n";
3047 return;
3048 }
3049 }
3050 // special printing for comma expressions.
3051 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3052 if (B->getOpcode() == BO_Comma) {
3053 OS << "... , ";
3054 Helper->handledStmt(B->getRHS(),OS);
3055 OS << '\n';
3056 return;
3057 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003058 }
3059 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003060 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003061
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003062 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003063 OS << " (OperatorCall)";
3064 } else if (isa<CXXBindTemporaryExpr>(S)) {
3065 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003066 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003067
Zhongxing Xu5281b8e2010-11-24 06:33:02 +00003068 if (CS.asLValue())
3069 OS << " (asLValue)";
3070
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003071 // Expressions need a newline.
3072 if (isa<Expr>(S))
3073 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003074
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003075 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3076 CXXBaseOrMemberInitializer* I = IE;
3077 if (I->isBaseInitializer())
3078 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
3079 else OS << I->getMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003080
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003081 OS << "(";
3082 if (Expr* IE = I->getInit())
3083 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3084 OS << ")";
3085
3086 if (I->isBaseInitializer())
3087 OS << " (Base initializer)\n";
3088 else OS << " (Member initializer)\n";
3089
3090 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3091 VarDecl* VD = DE.getVarDecl();
3092 Helper->handleDecl(VD, OS);
3093
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003094 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003095 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3096 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003097 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3098 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003099
3100 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3101 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003102
3103 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3104 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3105 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003106 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003107
3108 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3109 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003110
3111 const Type *T = FD->getType().getTypePtr();
3112 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3113 T = ET;
3114
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003115 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003116 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003117 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003118
3119 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3120 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3121 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3122 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003123 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003124}
Mike Stump6d9828c2009-07-17 01:31:16 +00003125
Chris Lattnere4f21422009-06-30 01:26:17 +00003126static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3127 const CFGBlock& B,
3128 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003129
Ted Kremenek42a509f2007-08-31 21:30:12 +00003130 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003131
Ted Kremenek7dba8602007-08-29 21:56:09 +00003132 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003133 OS << "\n [ B" << B.getBlockID();
3134
Ted Kremenek42a509f2007-08-31 21:30:12 +00003135 if (&B == &cfg->getEntry())
3136 OS << " (ENTRY) ]\n";
3137 else if (&B == &cfg->getExit())
3138 OS << " (EXIT) ]\n";
3139 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003140 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003141 else
3142 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003143
Ted Kremenek9cffe732007-08-29 23:20:49 +00003144 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003145 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003146
3147 if (print_edges)
3148 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003149
Mike Stump079bd722010-01-19 22:00:14 +00003150 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003151 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003152 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003153 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003154 C->getLHS()->printPretty(OS, Helper,
3155 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003156 if (C->getRHS()) {
3157 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003158 C->getRHS()->printPretty(OS, Helper,
3159 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003160 }
Mike Stump079bd722010-01-19 22:00:14 +00003161 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003162 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003163 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003164 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003165 if (CS->getExceptionDecl())
3166 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3167 0);
3168 else
3169 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003170 OS << ")";
3171
3172 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003173 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003174
Ted Kremenek9cffe732007-08-29 23:20:49 +00003175 OS << ":\n";
3176 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003177
Ted Kremenekfddd5182007-08-21 21:42:03 +00003178 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003179 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003180
Ted Kremenek42a509f2007-08-31 21:30:12 +00003181 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3182 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003183
Ted Kremenek9cffe732007-08-29 23:20:49 +00003184 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003185 if (print_edges)
3186 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003187
Ted Kremeneka95d3752008-09-13 05:16:45 +00003188 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003189
Ted Kremenek42a509f2007-08-31 21:30:12 +00003190 if (Helper)
3191 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003192
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003193 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003194 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003195
Ted Kremenek9cffe732007-08-29 23:20:49 +00003196 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003197 if (B.getTerminator()) {
3198 if (print_edges)
3199 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003200
Ted Kremenek9cffe732007-08-29 23:20:49 +00003201 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003202
Ted Kremenek42a509f2007-08-31 21:30:12 +00003203 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003204
Chris Lattnere4f21422009-06-30 01:26:17 +00003205 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3206 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003207 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003208 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003209 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003210
Ted Kremenek9cffe732007-08-29 23:20:49 +00003211 if (print_edges) {
3212 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003213 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003214 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003215
Ted Kremenek42a509f2007-08-31 21:30:12 +00003216 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3217 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003218
Ted Kremenek42a509f2007-08-31 21:30:12 +00003219 if (i == 8 || (i-8) == 0)
3220 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003221
Ted Kremenek9cffe732007-08-29 23:20:49 +00003222 OS << " B" << (*I)->getBlockID();
3223 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003224
Ted Kremenek42a509f2007-08-31 21:30:12 +00003225 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003226
Ted Kremenek42a509f2007-08-31 21:30:12 +00003227 // Print the successors of this block.
3228 OS << " Successors (" << B.succ_size() << "):";
3229 i = 0;
3230
3231 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3232 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003233
Ted Kremenek42a509f2007-08-31 21:30:12 +00003234 if (i == 8 || (i-8) % 10 == 0)
3235 OS << "\n ";
3236
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003237 if (*I)
3238 OS << " B" << (*I)->getBlockID();
3239 else
3240 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003241 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003242
Ted Kremenek9cffe732007-08-29 23:20:49 +00003243 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003244 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003245}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003246
Ted Kremenek42a509f2007-08-31 21:30:12 +00003247
3248/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003249void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003250
3251/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003252void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3253 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003254
Ted Kremenek42a509f2007-08-31 21:30:12 +00003255 // Print the entry block.
3256 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003257
Ted Kremenek42a509f2007-08-31 21:30:12 +00003258 // Iterate through the CFGBlocks and print them one by one.
3259 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3260 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003261 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003262 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003263
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003264 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003265 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003266
Ted Kremenek42a509f2007-08-31 21:30:12 +00003267 // Print the exit block.
3268 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003269 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003270}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003271
3272/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003273void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3274 print(llvm::errs(), cfg, LO);
3275}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003276
3277/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3278/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003279void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3280 const LangOptions &LO) const {
3281 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003282 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003283}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003284
Ted Kremeneka2925852008-01-30 23:02:42 +00003285/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003286void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003287 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003288 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003289 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003290}
3291
Ted Kremenek390e48b2008-11-12 21:11:49 +00003292Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003293 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003294 if (!Terminator)
3295 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003296
Ted Kremenek411cdee2008-04-16 21:10:48 +00003297 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003298
Ted Kremenek411cdee2008-04-16 21:10:48 +00003299 switch (Terminator->getStmtClass()) {
3300 default:
3301 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003302
Ted Kremenek411cdee2008-04-16 21:10:48 +00003303 case Stmt::ForStmtClass:
3304 E = cast<ForStmt>(Terminator)->getCond();
3305 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003306
Ted Kremenek411cdee2008-04-16 21:10:48 +00003307 case Stmt::WhileStmtClass:
3308 E = cast<WhileStmt>(Terminator)->getCond();
3309 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003310
Ted Kremenek411cdee2008-04-16 21:10:48 +00003311 case Stmt::DoStmtClass:
3312 E = cast<DoStmt>(Terminator)->getCond();
3313 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003314
Ted Kremenek411cdee2008-04-16 21:10:48 +00003315 case Stmt::IfStmtClass:
3316 E = cast<IfStmt>(Terminator)->getCond();
3317 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003318
Ted Kremenek411cdee2008-04-16 21:10:48 +00003319 case Stmt::ChooseExprClass:
3320 E = cast<ChooseExpr>(Terminator)->getCond();
3321 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003322
Ted Kremenek411cdee2008-04-16 21:10:48 +00003323 case Stmt::IndirectGotoStmtClass:
3324 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3325 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003326
Ted Kremenek411cdee2008-04-16 21:10:48 +00003327 case Stmt::SwitchStmtClass:
3328 E = cast<SwitchStmt>(Terminator)->getCond();
3329 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003330
Ted Kremenek411cdee2008-04-16 21:10:48 +00003331 case Stmt::ConditionalOperatorClass:
3332 E = cast<ConditionalOperator>(Terminator)->getCond();
3333 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003334
Ted Kremenek411cdee2008-04-16 21:10:48 +00003335 case Stmt::BinaryOperatorClass: // '&&' and '||'
3336 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003337 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003338
Ted Kremenek390e48b2008-11-12 21:11:49 +00003339 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003340 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003341 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003342
Ted Kremenek411cdee2008-04-16 21:10:48 +00003343 return E ? E->IgnoreParens() : NULL;
3344}
3345
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003346bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003347 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003348 if (!Terminator)
3349 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003350
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003351 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003352
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003353 switch (Terminator->getStmtClass()) {
3354 default:
3355 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003356
3357 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003358 case Stmt::WhileStmtClass:
3359 case Stmt::DoStmtClass:
3360 case Stmt::IfStmtClass:
3361 case Stmt::ChooseExprClass:
3362 case Stmt::ConditionalOperatorClass:
3363 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003364 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003365 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003366
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003367 return E ? E->IgnoreParens() : NULL;
3368}
3369
Ted Kremeneka2925852008-01-30 23:02:42 +00003370
Ted Kremenek7dba8602007-08-29 21:56:09 +00003371//===----------------------------------------------------------------------===//
3372// CFG Graphviz Visualization
3373//===----------------------------------------------------------------------===//
3374
Ted Kremenek42a509f2007-08-31 21:30:12 +00003375
3376#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003377static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003378#endif
3379
Chris Lattnere4f21422009-06-30 01:26:17 +00003380void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003381#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003382 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003383 GraphHelper = &H;
3384 llvm::ViewGraph(this,"CFG");
3385 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003386#endif
3387}
3388
Ted Kremenek7dba8602007-08-29 21:56:09 +00003389namespace llvm {
3390template<>
3391struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003392
3393 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3394
3395 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003396
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003397#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003398 std::string OutSStr;
3399 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003400 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003401 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003402
3403 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3404
3405 // Process string output to make it nicer...
3406 for (unsigned i = 0; i != OutStr.length(); ++i)
3407 if (OutStr[i] == '\n') { // Left justify
3408 OutStr[i] = '\\';
3409 OutStr.insert(OutStr.begin()+i+1, 'l');
3410 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003411
Ted Kremenek7dba8602007-08-29 21:56:09 +00003412 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003413#else
3414 return "";
3415#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003416 }
3417};
3418} // end namespace llvm