blob: a0ec5febbeff05a66db097526d4393273d14314f [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 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000036}
Ted Kremenekad5a8942010-08-02 23:46:59 +000037
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000038/// The CFG builder uses a recursive algorithm to build the CFG. When
39/// we process an expression, sometimes we know that we must add the
40/// subexpressions as block-level expressions. For example:
41///
42/// exp1 || exp2
43///
44/// When processing the '||' expression, we know that exp1 and exp2
45/// need to be added as block-level expressions, even though they
46/// might not normally need to be. AddStmtChoice records this
47/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
48/// the builder has an option not to add a subexpression as a
49/// block-level expression.
50///
Ted Kremenek852274d2009-12-16 03:18:58 +000051class AddStmtChoice {
52public:
Ted Kremenek892697d2010-12-16 07:46:53 +000053 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000054
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000055 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000056
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000057 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000058
59 /// Return a copy of this object, except with the 'always-add' bit
60 /// set as specified.
61 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
62 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
63 Kind(kind & ~AlwaysAdd));
64 }
65
Ted Kremenek852274d2009-12-16 03:18:58 +000066private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000067 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000068};
Mike Stump6d9828c2009-07-17 01:31:16 +000069
Marcin Swiderskif1308c72010-09-25 11:05:21 +000070/// LocalScope - Node in tree of local scopes created for C++ implicit
71/// destructor calls generation. It contains list of automatic variables
72/// declared in the scope and link to position in previous scope this scope
73/// began in.
74///
75/// The process of creating local scopes is as follows:
76/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
77/// - Before processing statements in scope (e.g. CompoundStmt) create
78/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
79/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000080/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000081/// at this VarDecl,
82/// - For every normal (without jump) end of scope add to CFGBlock destructors
83/// for objects in the current scope,
84/// - For every jump add to CFGBlock destructors for objects
85/// between CFGBuilder::ScopePos and local scope position saved for jump
86/// target. Thanks to C++ restrictions on goto jumps we can be sure that
87/// jump target position will be on the path to root from CFGBuilder::ScopePos
88/// (adding any variable that doesn't need constructor to be called to
89/// LocalScope can break this assumption),
90///
91class LocalScope {
92public:
Ted Kremenekfe59b742011-02-15 02:47:45 +000093 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderskif1308c72010-09-25 11:05:21 +000094
95 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000096 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +000097 class const_iterator {
98 const LocalScope* Scope;
99
100 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
101 /// Invalid iterator (with null Scope) has VarIter equal to 0.
102 unsigned VarIter;
103
104 public:
105 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
106 /// Incrementing invalid iterator is allowed and will result in invalid
107 /// iterator.
108 const_iterator()
109 : Scope(NULL), VarIter(0) {}
110
111 /// Create valid iterator. In case when S.Prev is an invalid iterator and
112 /// I is equal to 0, this will create invalid iterator.
113 const_iterator(const LocalScope& S, unsigned I)
114 : Scope(&S), VarIter(I) {
115 // Iterator to "end" of scope is not allowed. Handle it by going up
116 // in scopes tree possibly up to invalid iterator in the root.
117 if (VarIter == 0 && Scope)
118 *this = Scope->Prev;
119 }
120
121 VarDecl* const* operator->() const {
122 assert (Scope && "Dereferencing invalid iterator is not allowed");
123 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
124 return &Scope->Vars[VarIter - 1];
125 }
126 VarDecl* operator*() const {
127 return *this->operator->();
128 }
129
130 const_iterator& operator++() {
131 if (!Scope)
132 return *this;
133
134 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
135 --VarIter;
136 if (VarIter == 0)
137 *this = Scope->Prev;
138 return *this;
139 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000140 const_iterator operator++(int) {
141 const_iterator P = *this;
142 ++*this;
143 return P;
144 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000145
146 bool operator==(const const_iterator& rhs) const {
147 return Scope == rhs.Scope && VarIter == rhs.VarIter;
148 }
149 bool operator!=(const const_iterator& rhs) const {
150 return !(*this == rhs);
151 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000152
153 operator bool() const {
154 return *this != const_iterator();
155 }
156
157 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000158 };
159
160 friend class const_iterator;
161
162private:
Ted Kremenekfe59b742011-02-15 02:47:45 +0000163 BumpVectorContext ctx;
164
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000165 /// Automatic variables in order of declaration.
166 AutomaticVarsTy Vars;
167 /// Iterator to variable in previous scope that was declared just before
168 /// begin of this scope.
169 const_iterator Prev;
170
171public:
172 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekfe59b742011-02-15 02:47:45 +0000173 LocalScope(BumpVectorContext &ctx, const_iterator P)
174 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000175
176 /// Begin of scope in direction of CFG building (backwards).
177 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000178
179 void addVar(VarDecl* VD) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000180 Vars.push_back(VD, ctx);
Marcin Swiderski35387a02010-09-30 22:42:32 +0000181 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000182};
183
Marcin Swiderski35387a02010-09-30 22:42:32 +0000184/// distance - Calculates distance from this to L. L must be reachable from this
185/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
186/// number of scopes between this and L.
187int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
188 int D = 0;
189 const_iterator F = *this;
190 while (F.Scope != L.Scope) {
191 assert (F != const_iterator()
192 && "L iterator is not reachable from F iterator.");
193 D += F.VarIter;
194 F = F.Scope->Prev;
195 }
196 D += F.VarIter - L.VarIter;
197 return D;
198}
199
200/// BlockScopePosPair - Structure for specifying position in CFG during its
201/// build process. It consists of CFGBlock that specifies position in CFG graph
202/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000203struct BlockScopePosPair {
Ted Kremenek9ce52702011-01-07 19:37:16 +0000204 BlockScopePosPair() : block(0) {}
205 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
206 : block(b), scopePosition(scopePos) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000207
Ted Kremenek9ce52702011-01-07 19:37:16 +0000208 CFGBlock *block;
209 LocalScope::const_iterator scopePosition;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000210};
211
Ted Kremeneka34ea072008-08-04 22:51:42 +0000212/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000213/// The builder is stateful: an instance of the builder should be used to only
214/// construct a single CFG.
215///
216/// Example usage:
217///
218/// CFGBuilder builder;
219/// CFG* cfg = builder.BuildAST(stmt1);
220///
Mike Stump6d9828c2009-07-17 01:31:16 +0000221/// CFG construction is done via a recursive walk of an AST. We actually parse
222/// the AST in reverse order so that the successor of a basic block is
223/// constructed prior to its predecessor. This allows us to nicely capture
224/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000225///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000226class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000227 typedef BlockScopePosPair JumpTarget;
228 typedef BlockScopePosPair JumpSource;
229
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000230 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000231 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000232
Ted Kremenekfddd5182007-08-21 21:42:03 +0000233 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000234 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000235 JumpTarget ContinueJumpTarget;
236 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000237 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000238 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000239 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000241 // Current position in local scope.
242 LocalScope::const_iterator ScopePos;
243
244 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000245 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000246 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000247
248 // A list of blocks that end with a "goto" that must be backpatched to their
249 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000250 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000251 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000252
Ted Kremenek19bb3562007-08-28 19:26:49 +0000253 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000254 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenek19bb3562007-08-28 19:26:49 +0000255 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000256
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000257 bool badCFG;
258 CFG::BuildOptions BuildOpts;
259
Mike Stump6d9828c2009-07-17 01:31:16 +0000260public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000261 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
262 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000263 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000264 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000265
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000266 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000267 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000268 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000269
Ted Kremenek4f880632009-07-17 22:18:43 +0000270private:
271 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000272 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
273 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
274 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000275 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000276 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000277 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000278 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000279 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
280 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000281 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
282 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000283 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000284 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
285 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000286 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
287 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000288 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000289 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000290 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000291 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000292 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCall56ca35d2011-02-17 10:25:35 +0000293 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
294 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000295 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000296 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000297 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000298 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
299 CFGBlock *VisitDoStmt(DoStmt *D);
300 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000301 CFGBlock *VisitGotoStmt(GotoStmt* G);
302 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000303 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000304 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
305 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000306 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000307 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
308 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
309 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
310 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
311 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
312 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000313 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
314 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000315 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000316 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000317 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000318
Ted Kremenek852274d2009-12-16 03:18:58 +0000319 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
320 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000321 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000322
Marcin Swiderski8599e762010-11-03 06:19:35 +0000323 // Visitors to walk an AST and generate destructors of temporaries in
324 // full expression.
325 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
326 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
327 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
328 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
329 bool BindToTemporary);
John McCall56ca35d2011-02-17 10:25:35 +0000330 CFGBlock *
331 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
332 bool BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000333
Ted Kremenek274f4332008-04-28 18:00:46 +0000334 // NYS == Not Yet Supported
335 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000336 badCFG = true;
337 return Block;
338 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000339
Ted Kremenek4f880632009-07-17 22:18:43 +0000340 void autoCreateBlock() { if (!Block) Block = createBlock(); }
341 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000342
Zhongxing Xudf119892010-06-03 06:43:23 +0000343 CFGBlock *addStmt(Stmt *S) {
344 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000345 }
Sean Huntcbb67482011-01-08 20:30:50 +0000346 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000347 void addAutomaticObjDtors(LocalScope::const_iterator B,
348 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000349 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000350
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000351 // Local scopes creation.
352 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
353
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000354 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000355 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
356 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
357
358 void addLocalScopeAndDtors(Stmt* S);
359
360 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek892697d2010-12-16 07:46:53 +0000361 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek852274d2009-12-16 03:18:58 +0000362 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000363 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000364 }
Sean Huntcbb67482011-01-08 20:30:50 +0000365 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000366 B->appendInitializer(I, cfg->getBumpVectorContext());
367 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000368 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
369 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
370 }
371 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
372 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
373 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000374 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
375 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
376 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000377
Marcin Swiderski53de1342010-09-30 22:54:37 +0000378 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
379 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
380 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
381 LocalScope::const_iterator E, Stmt* S);
382 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
383 LocalScope::const_iterator B, LocalScope::const_iterator E);
384
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000385 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000386 B->addSuccessor(S, cfg->getBumpVectorContext());
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000389 /// TryResult - a class representing a variant over the values
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000390 /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000391 /// and is used by the CFGBuilder to decide if a branch condition
392 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000393 class TryResult {
394 int X;
395 public:
396 TryResult(bool b) : X(b ? 1 : 0) {}
397 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek941fde82009-07-24 04:47:11 +0000399 bool isTrue() const { return X == 1; }
400 bool isFalse() const { return X == 0; }
401 bool isKnown() const { return X >= 0; }
402 void negate() {
403 assert(isKnown());
404 X ^= 0x1;
405 }
406 };
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000408 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump00998a02009-07-23 23:25:26 +0000409 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000410 TryResult tryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000411 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000412 return TryResult();
413
Mike Stump00998a02009-07-23 23:25:26 +0000414 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000415 if (!S->isTypeDependent() && !S->isValueDependent() &&
416 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000417 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000418
419 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000420 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000421};
Mike Stump6d9828c2009-07-17 01:31:16 +0000422
Douglas Gregor898574e2008-12-05 23:32:09 +0000423// FIXME: Add support for dependent-sized array types in C++?
424// Does it even make sense to build a CFG for an uninstantiated template?
John McCallf4c73712011-01-19 06:33:43 +0000425static const VariableArrayType *FindVA(const Type *t) {
426 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
427 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenek610a09e2008-09-26 22:58:57 +0000428 if (vat->getSizeExpr())
429 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000430
Ted Kremenek610a09e2008-09-26 22:58:57 +0000431 t = vt->getElementType().getTypePtr();
432 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000433
Ted Kremenek610a09e2008-09-26 22:58:57 +0000434 return 0;
435}
Mike Stump6d9828c2009-07-17 01:31:16 +0000436
437/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
438/// arbitrary statement. Examples include a single expression or a function
439/// body (compound statement). The ownership of the returned CFG is
440/// transferred to the caller. If CFG construction fails, this method returns
441/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000442CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000443 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000444
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000445 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000446 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000447 if (!Statement)
448 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000449
Ted Kremenek6c52c782010-09-14 23:41:16 +0000450 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000451
452 // Create an empty block that will serve as the exit block for the CFG. Since
453 // this is the first block added to the CFG, it will be implicitly registered
454 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000455 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000456 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000457 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000458
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000459 if (BuildOpts.AddImplicitDtors)
460 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
461 addImplicitDtorsForDestructor(DD);
462
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000463 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000464 CFGBlock *B = addStmt(Statement);
465
466 if (badCFG)
467 return NULL;
468
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000469 // For C++ constructor add initializers to CFG.
470 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
471 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
472 E = CD->init_rend(); I != E; ++I) {
473 B = addInitializer(*I);
474 if (badCFG)
475 return NULL;
476 }
477 }
478
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000479 if (B)
480 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000481
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000482 // Backpatch the gotos whose label -> block mappings we didn't know when we
483 // encountered them.
484 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
485 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000486
Ted Kremenek9ce52702011-01-07 19:37:16 +0000487 CFGBlock* B = I->block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000488 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
489 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000490
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000491 // If there is no target for the goto, then we are looking at an
492 // incomplete AST. Handle this by not registering a successor.
493 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000494
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000495 JumpTarget JT = LI->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +0000496 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
497 JT.scopePosition);
498 addSuccessor(B, JT.block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000499 }
500
501 // Add successors to the Indirect Goto Dispatch block (if we have one).
502 if (CFGBlock* B = cfg->getIndirectGotoBlock())
503 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
504 E = AddressTakenLabels.end(); I != E; ++I ) {
505
506 // Lookup the target block.
507 LabelMapTy::iterator LI = LabelMap.find(*I);
508
509 // If there is no target block that contains label, then we are looking
510 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000511 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000512
Ted Kremenek9ce52702011-01-07 19:37:16 +0000513 addSuccessor(B, LI->second.block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000514 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000515
Mike Stump6d9828c2009-07-17 01:31:16 +0000516 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000517 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000518
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000519 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000520}
Mike Stump6d9828c2009-07-17 01:31:16 +0000521
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000522/// createBlock - Used to lazily create blocks that are connected
523/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000524CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000525 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000526 if (add_successor && Succ)
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000527 addSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000528 return B;
529}
Mike Stump6d9828c2009-07-17 01:31:16 +0000530
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000531/// addInitializer - Add C++ base or member initializer element to CFG.
Sean Huntcbb67482011-01-08 20:30:50 +0000532CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000533 if (!BuildOpts.AddInitializers)
534 return Block;
535
Marcin Swiderski8599e762010-11-03 06:19:35 +0000536 bool IsReference = false;
537 bool HasTemporaries = false;
538
539 // Destructors of temporaries in initialization expression should be called
540 // after initialization finishes.
541 Expr *Init = I->getInit();
542 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000543 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000544 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000545 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000546
547 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
548 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000549 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000550 IsReference);
551 }
552 }
553
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000554 autoCreateBlock();
555 appendInitializer(Block, I);
556
Marcin Swiderski8599e762010-11-03 06:19:35 +0000557 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000558 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000559 // For expression with temporaries go directly to subexpression to omit
560 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000561 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
562 }
563 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000564 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000565
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000566 return Block;
567}
568
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000569/// addAutomaticObjDtors - Add to current block automatic objects destructors
570/// for objects in range of local scope positions. Use S as trigger statement
571/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000572void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
573 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000574 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000575 return;
576
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000577 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000578 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000579
580 autoCreateBlock();
581 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000582}
583
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000584/// addImplicitDtorsForDestructor - Add implicit destructors generated for
585/// base and member objects in destructor.
586void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
587 assert (BuildOpts.AddImplicitDtors
588 && "Can be called only when dtors should be added");
589 const CXXRecordDecl *RD = DD->getParent();
590
591 // At the end destroy virtual base objects.
592 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
593 VE = RD->vbases_end(); VI != VE; ++VI) {
594 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
595 if (!CD->hasTrivialDestructor()) {
596 autoCreateBlock();
597 appendBaseDtor(Block, VI);
598 }
599 }
600
601 // Before virtual bases destroy direct base objects.
602 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
603 BE = RD->bases_end(); BI != BE; ++BI) {
604 if (!BI->isVirtual()) {
605 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
606 if (!CD->hasTrivialDestructor()) {
607 autoCreateBlock();
608 appendBaseDtor(Block, BI);
609 }
610 }
611 }
612
613 // First destroy member objects.
614 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
615 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000616 // Check for constant size array. Set type to array element type.
617 QualType QT = FI->getType();
618 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
619 if (AT->getSize() == 0)
620 continue;
621 QT = AT->getElementType();
622 }
623
624 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000625 if (!CD->hasTrivialDestructor()) {
626 autoCreateBlock();
627 appendMemberDtor(Block, *FI);
628 }
629 }
630}
631
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000632/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
633/// way return valid LocalScope object.
634LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
635 if (!Scope) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000636 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
637 Scope = alloc.Allocate<LocalScope>();
638 BumpVectorContext ctx(alloc);
639 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000640 }
641 return Scope;
642}
643
644/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000645/// that should create implicit scope (e.g. if/else substatements).
646void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000647 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000648 return;
649
650 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000651
652 // For compound statement we will be creating explicit scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000653 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000654 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
655 ; BI != BE; ++BI) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000656 Stmt *SI = *BI;
657 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000658 SI = LS->getSubStmt();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000659 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000660 Scope = addLocalScopeForDeclStmt(DS, Scope);
661 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000662 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000663 }
664
665 // For any other statement scope will be implicit and as such will be
666 // interesting only for DeclStmt.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000667 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000668 S = LS->getSubStmt();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000669 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000670 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000671}
672
673/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
674/// reuse Scope if not NULL.
675LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000676 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000677 if (!BuildOpts.AddImplicitDtors)
678 return Scope;
679
680 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
681 ; DI != DE; ++DI) {
682 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
683 Scope = addLocalScopeForVarDecl(VD, Scope);
684 }
685 return Scope;
686}
687
688/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
689/// create add scope for automatic objects and temporary objects bound to
690/// const reference. Will reuse Scope if not NULL.
691LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000692 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000693 if (!BuildOpts.AddImplicitDtors)
694 return Scope;
695
696 // Check if variable is local.
697 switch (VD->getStorageClass()) {
698 case SC_None:
699 case SC_Auto:
700 case SC_Register:
701 break;
702 default: return Scope;
703 }
704
705 // Check for const references bound to temporary. Set type to pointee.
706 QualType QT = VD->getType();
707 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
708 QT = RT->getPointeeType();
709 if (!QT.isConstQualified())
710 return Scope;
711 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
712 return Scope;
713 }
714
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000715 // Check for constant size array. Set type to array element type.
716 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
717 if (AT->getSize() == 0)
718 return Scope;
719 QT = AT->getElementType();
720 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000721
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000722 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000723 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
724 if (!CD->hasTrivialDestructor()) {
725 // Add the variable to scope
726 Scope = createOrReuseLocalScope(Scope);
727 Scope->addVar(VD);
728 ScopePos = Scope->begin();
729 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000730 return Scope;
731}
732
733/// addLocalScopeAndDtors - For given statement add local scope for it and
734/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
735void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
736 if (!BuildOpts.AddImplicitDtors)
737 return;
738
739 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000740 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000741 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
742}
743
Marcin Swiderski53de1342010-09-30 22:54:37 +0000744/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
745/// automatic storage duration to CFGBlock's elements vector. Insertion will be
746/// performed in place specified with iterator.
747void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
748 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
749 BumpVectorContext& C = cfg->getBumpVectorContext();
750 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
751 while (B != E)
752 I = Blk->insertAutomaticObjDtor(I, *B++, S);
753}
754
755/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
756/// automatic storage duration to CFGBlock's elements vector. Elements will be
757/// appended to physical end of the vector which happens to be logical
758/// beginning.
759void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
760 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
761 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
762}
763
764/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
765/// variables with automatic storage duration to CFGBlock's elements vector.
766/// Elements will be prepended to physical beginning of the vector which
767/// happens to be logical end. Use blocks terminator as statement that specifies
768/// destructors call site.
769void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
770 LocalScope::const_iterator B, LocalScope::const_iterator E) {
771 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
772}
773
Ted Kremenek4f880632009-07-17 22:18:43 +0000774/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000775/// blocks for ternary operators, &&, and ||. We also process "," and
776/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000777CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000778tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000779 if (!S) {
780 badCFG = true;
781 return 0;
782 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000783 switch (S->getStmtClass()) {
784 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000785 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000786
787 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000788 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
John McCall56ca35d2011-02-17 10:25:35 +0000790 case Stmt::BinaryConditionalOperatorClass:
791 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000794 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek4f880632009-07-17 22:18:43 +0000796 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000797 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000798
Ted Kremenek4f880632009-07-17 22:18:43 +0000799 case Stmt::BreakStmtClass:
800 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek4f880632009-07-17 22:18:43 +0000802 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000803 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000804 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek4f880632009-07-17 22:18:43 +0000806 case Stmt::CaseStmtClass:
807 return VisitCaseStmt(cast<CaseStmt>(S));
808
809 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000810 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek4f880632009-07-17 22:18:43 +0000812 case Stmt::CompoundStmtClass:
813 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek4f880632009-07-17 22:18:43 +0000815 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000816 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremenek4f880632009-07-17 22:18:43 +0000818 case Stmt::ContinueStmtClass:
819 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Ted Kremenek021c8af2010-01-19 20:40:33 +0000821 case Stmt::CXXCatchStmtClass:
822 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
823
John McCall4765fa02010-12-06 08:20:24 +0000824 case Stmt::ExprWithCleanupsClass:
825 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000826
Zhongxing Xua725ed42010-11-01 13:04:58 +0000827 case Stmt::CXXBindTemporaryExprClass:
828 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
829
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000830 case Stmt::CXXConstructExprClass:
831 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
832
Zhongxing Xua725ed42010-11-01 13:04:58 +0000833 case Stmt::CXXFunctionalCastExprClass:
834 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
835
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000836 case Stmt::CXXTemporaryObjectExprClass:
837 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
838
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000839 case Stmt::CXXMemberCallExprClass:
840 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
841
Ted Kremenek021c8af2010-01-19 20:40:33 +0000842 case Stmt::CXXThrowExprClass:
843 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000844
Ted Kremenek021c8af2010-01-19 20:40:33 +0000845 case Stmt::CXXTryStmtClass:
846 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000847
Ted Kremenek4f880632009-07-17 22:18:43 +0000848 case Stmt::DeclStmtClass:
849 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek4f880632009-07-17 22:18:43 +0000851 case Stmt::DefaultStmtClass:
852 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek4f880632009-07-17 22:18:43 +0000854 case Stmt::DoStmtClass:
855 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek4f880632009-07-17 22:18:43 +0000857 case Stmt::ForStmtClass:
858 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Ted Kremenek4f880632009-07-17 22:18:43 +0000860 case Stmt::GotoStmtClass:
861 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Ted Kremenek4f880632009-07-17 22:18:43 +0000863 case Stmt::IfStmtClass:
864 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremenek892697d2010-12-16 07:46:53 +0000866 case Stmt::ImplicitCastExprClass:
867 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000868
Ted Kremenek4f880632009-07-17 22:18:43 +0000869 case Stmt::IndirectGotoStmtClass:
870 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek4f880632009-07-17 22:18:43 +0000872 case Stmt::LabelStmtClass:
873 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek115c1b92010-04-11 17:02:10 +0000875 case Stmt::MemberExprClass:
876 return VisitMemberExpr(cast<MemberExpr>(S), asc);
877
Ted Kremenek4f880632009-07-17 22:18:43 +0000878 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000879 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
880
Ted Kremenek4f880632009-07-17 22:18:43 +0000881 case Stmt::ObjCAtSynchronizedStmtClass:
882 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek4f880632009-07-17 22:18:43 +0000884 case Stmt::ObjCAtThrowStmtClass:
885 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Ted Kremenek4f880632009-07-17 22:18:43 +0000887 case Stmt::ObjCAtTryStmtClass:
888 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenek4f880632009-07-17 22:18:43 +0000890 case Stmt::ObjCForCollectionStmtClass:
891 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek4f880632009-07-17 22:18:43 +0000893 case Stmt::ParenExprClass:
894 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000895 goto tryAgain;
896
Ted Kremenek4f880632009-07-17 22:18:43 +0000897 case Stmt::NullStmtClass:
898 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek4f880632009-07-17 22:18:43 +0000900 case Stmt::ReturnStmtClass:
901 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremenek4f880632009-07-17 22:18:43 +0000903 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000904 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremenek4f880632009-07-17 22:18:43 +0000906 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000907 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Ted Kremenek4f880632009-07-17 22:18:43 +0000909 case Stmt::SwitchStmtClass:
910 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000912 case Stmt::UnaryOperatorClass:
913 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
914
Ted Kremenek4f880632009-07-17 22:18:43 +0000915 case Stmt::WhileStmtClass:
916 return VisitWhileStmt(cast<WhileStmt>(S));
917 }
918}
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Ted Kremenek852274d2009-12-16 03:18:58 +0000920CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
921 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000922 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000923 appendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek4f880632009-07-17 22:18:43 +0000926 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000927}
Mike Stump6d9828c2009-07-17 01:31:16 +0000928
Ted Kremenek4f880632009-07-17 22:18:43 +0000929/// VisitChildren - Visit the children of a Stmt.
930CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
931 CFGBlock *B = Block;
John McCall7502c1d2011-02-13 04:07:26 +0000932 for (Stmt::child_range I = Terminator->children(); I; ++I) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000933 if (*I) B = Visit(*I);
934 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000935 return B;
936}
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Ted Kremenek852274d2009-12-16 03:18:58 +0000938CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
939 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000940 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000941
Ted Kremenek852274d2009-12-16 03:18:58 +0000942 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000943 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000944 appendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000945 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000946
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000947 return Block;
948}
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000950CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +0000951 AddStmtChoice asc) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000952 if (asc.alwaysAdd()) {
953 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000954 appendStmt(Block, U, asc);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000955 }
956
Ted Kremenek892697d2010-12-16 07:46:53 +0000957 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000958}
959
Ted Kremenek852274d2009-12-16 03:18:58 +0000960CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
961 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000962 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000963 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000964 appendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000966 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000967 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Ted Kremenek4f880632009-07-17 22:18:43 +0000969 // create the block evaluating the LHS
970 CFGBlock* LHSBlock = createBlock(false);
971 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Ted Kremenek4f880632009-07-17 22:18:43 +0000973 // create the block evaluating the RHS
974 Succ = ConfluenceBlock;
975 Block = NULL;
976 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000977
978 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000979 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000980 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +0000981 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +0000982 // Create an empty block for cases where the RHS doesn't require
983 // any explicit statements in the CFG.
984 RHSBlock = createBlock();
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Mike Stump00998a02009-07-23 23:25:26 +0000987 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000988 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000989 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000990 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000991
Ted Kremenek4f880632009-07-17 22:18:43 +0000992 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000993 if (B->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000994 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
995 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000996 } else {
John McCall2de56d12010-08-25 11:45:40 +0000997 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000998 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
999 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek4f880632009-07-17 22:18:43 +00001002 // Generate the blocks for evaluating the LHS.
1003 Block = LHSBlock;
1004 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +00001005 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001006
1007 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001008 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001009 appendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001010 addStmt(B->getRHS());
1011 return addStmt(B->getLHS());
1012 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001013
1014 if (B->isAssignmentOp()) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001015 if (asc.alwaysAdd()) {
1016 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001017 appendStmt(Block, B, asc);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001018 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001019 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001020 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Marcin Swiderskie1667192010-10-24 08:21:40 +00001023 if (asc.alwaysAdd()) {
1024 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001025 appendStmt(Block, B, asc);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001026 }
1027
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001028 CFGBlock *RBlock = Visit(B->getRHS());
1029 CFGBlock *LBlock = Visit(B->getLHS());
1030 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1031 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1032 // return RBlock. Otherwise we'll incorrectly return NULL.
1033 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001034}
1035
Ted Kremenek852274d2009-12-16 03:18:58 +00001036CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1037 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001038 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001039 appendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001040 }
1041 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001042}
1043
Ted Kremenek4f880632009-07-17 22:18:43 +00001044CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1045 // "break" is a control-flow statement. Thus we stop processing the current
1046 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001047 if (badCFG)
1048 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek4f880632009-07-17 22:18:43 +00001050 // Now create a new block that ends with the break statement.
1051 Block = createBlock(false);
1052 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremenek4f880632009-07-17 22:18:43 +00001054 // If there is no target for the break, then we are looking at an incomplete
1055 // AST. This means that the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001056 if (BreakJumpTarget.block) {
1057 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1058 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001059 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001060 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001061
1062
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001063 return Block;
1064}
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Mike Stump4c45aa12010-01-21 15:20:48 +00001066static bool CanThrow(Expr *E) {
1067 QualType Ty = E->getType();
1068 if (Ty->isFunctionPointerType())
1069 Ty = Ty->getAs<PointerType>()->getPointeeType();
1070 else if (Ty->isBlockPointerType())
1071 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001072
Mike Stump4c45aa12010-01-21 15:20:48 +00001073 const FunctionType *FT = Ty->getAs<FunctionType>();
1074 if (FT) {
1075 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1076 if (Proto->hasEmptyExceptionSpec())
1077 return false;
1078 }
1079 return true;
1080}
1081
Ted Kremenek852274d2009-12-16 03:18:58 +00001082CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001083 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001084 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001085 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001086 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001087 }
Mike Stump24556362009-07-25 21:26:53 +00001088
Mike Stump4c45aa12010-01-21 15:20:48 +00001089 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001090
1091 // Languages without exceptions are assumed to not throw.
Anders Carlssonc1cfdf82011-02-20 00:20:27 +00001092 if (Context->getLangOptions().areExceptionsEnabled()) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001093 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001094 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001095 }
1096
1097 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001098 if (FD->hasAttr<NoReturnAttr>())
1099 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001100 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001101 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001102 }
Mike Stump24556362009-07-25 21:26:53 +00001103
Mike Stump4c45aa12010-01-21 15:20:48 +00001104 if (!CanThrow(C->getCallee()))
1105 AddEHEdge = false;
1106
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001107 if (!NoReturn && !AddEHEdge)
1108 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Mike Stump079bd722010-01-19 22:00:14 +00001110 if (Block) {
1111 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001112 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001113 return 0;
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Mike Stump079bd722010-01-19 22:00:14 +00001116 Block = createBlock(!NoReturn);
Ted Kremenek892697d2010-12-16 07:46:53 +00001117 appendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001118
Mike Stump079bd722010-01-19 22:00:14 +00001119 if (NoReturn) {
1120 // Wire this to the exit block directly.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001121 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001122 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001123 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001124 // Add exceptional edges.
1125 if (TryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001126 addSuccessor(Block, TryTerminatedBlock);
Mike Stump079bd722010-01-19 22:00:14 +00001127 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001128 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001129 }
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Mike Stump24556362009-07-25 21:26:53 +00001131 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001132}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001133
Ted Kremenek852274d2009-12-16 03:18:58 +00001134CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1135 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001136 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001137 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001138 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001139 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001141 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001142 Succ = ConfluenceBlock;
1143 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001144 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001145 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001146 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001148 Succ = ConfluenceBlock;
1149 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001150 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001151 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001152 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001154 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001155 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001156 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1157 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1158 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001159 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001160 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001161}
Mike Stump1eb44332009-09-09 15:08:12 +00001162
1163
1164CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001165 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001166 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001167
1168 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1169 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001170 // If we hit a segment of code just containing ';' (NullStmts), we can
1171 // get a null block back. In such cases, just use the LastBlock
1172 if (CFGBlock *newBlock = addStmt(*I))
1173 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001175 if (badCFG)
1176 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001177 }
Mike Stump079bd722010-01-19 22:00:14 +00001178
Ted Kremenek4f880632009-07-17 22:18:43 +00001179 return LastBlock;
1180}
Mike Stump1eb44332009-09-09 15:08:12 +00001181
John McCall56ca35d2011-02-17 10:25:35 +00001182CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek852274d2009-12-16 03:18:58 +00001183 AddStmtChoice asc) {
John McCall56ca35d2011-02-17 10:25:35 +00001184 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1185 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1186
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001187 // Create the confluence block that will "merge" the results of the ternary
1188 // expression.
1189 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001190 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001191 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001192 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001194 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001195
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001196 // Create a block for the LHS expression if there is an LHS expression. A
1197 // GCC extension allows LHS to be NULL, causing the condition to be the
1198 // value that is returned instead.
1199 // e.g: x ?: y is shorthand for: x ? x : y;
1200 Succ = ConfluenceBlock;
1201 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00001202 CFGBlock* LHSBlock = 0;
1203 const Expr *trueExpr = C->getTrueExpr();
1204 if (trueExpr != opaqueValue) {
1205 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001206 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001207 return 0;
1208 Block = NULL;
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001211 // Create the block for the RHS expression.
1212 Succ = ConfluenceBlock;
John McCall56ca35d2011-02-17 10:25:35 +00001213 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001214 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001215 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001217 // Create the block that will contain the condition.
1218 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Mike Stump00998a02009-07-23 23:25:26 +00001220 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001221 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
John McCall56ca35d2011-02-17 10:25:35 +00001222 if (LHSBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001223 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001224 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001225 Block->setTerminator(C);
John McCall56ca35d2011-02-17 10:25:35 +00001226 Expr *condExpr = C->getCond();
John McCalld40baf62011-02-19 03:13:26 +00001227
1228 CFGBlock *result = 0;
1229
1230 // Run the condition expression if it's not trivially expressed in
1231 // terms of the opaque value (or if there is no opaque value).
John McCall56ca35d2011-02-17 10:25:35 +00001232 if (condExpr != opaqueValue) result = addStmt(condExpr);
John McCalld40baf62011-02-19 03:13:26 +00001233
1234 // Before that, run the common subexpression if there was one.
1235 // At least one of this or the above will be run.
1236 if (opaqueValue) result = addStmt(BCO->getCommon());
1237
John McCall56ca35d2011-02-17 10:25:35 +00001238 return result;
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001239}
1240
Ted Kremenek4f880632009-07-17 22:18:43 +00001241CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001242 if (DS->isSingleDecl())
1243 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenek4f880632009-07-17 22:18:43 +00001245 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenek4f880632009-07-17 22:18:43 +00001247 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1248 typedef llvm::SmallVector<Decl*,10> BufTy;
1249 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremenek4f880632009-07-17 22:18:43 +00001251 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1252 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1253 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1254 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenek4f880632009-07-17 22:18:43 +00001256 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1257 // automatically freed with the CFG.
1258 DeclGroupRef DG(*I);
1259 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001260 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001261 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenek4f880632009-07-17 22:18:43 +00001263 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001264 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
1267 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001268}
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenek4f880632009-07-17 22:18:43 +00001270/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001271/// DeclStmts and initializers in them.
1272CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1273 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001274
Marcin Swiderski8599e762010-11-03 06:19:35 +00001275 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Marcin Swiderski8599e762010-11-03 06:19:35 +00001277 if (!VD) {
1278 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001279 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001280 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001281 }
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Marcin Swiderski8599e762010-11-03 06:19:35 +00001283 bool IsReference = false;
1284 bool HasTemporaries = false;
1285
1286 // Destructors of temporaries in initialization expression should be called
1287 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001288 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001289 if (Init) {
1290 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001291 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001292
1293 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1294 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001295 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001296 IsReference);
1297 }
1298 }
1299
1300 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001301 appendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Ted Kremenek4f880632009-07-17 22:18:43 +00001303 if (Init) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001304 if (HasTemporaries)
1305 // For expression with temporaries go directly to subexpression to omit
1306 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +00001307 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski8599e762010-11-03 06:19:35 +00001308 else
Ted Kremenek892697d2010-12-16 07:46:53 +00001309 Visit(Init);
Ted Kremenek4f880632009-07-17 22:18:43 +00001310 }
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Ted Kremenek4f880632009-07-17 22:18:43 +00001312 // If the type of VD is a VLA, then we must process its size expressions.
John McCallf4c73712011-01-19 06:33:43 +00001313 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1314 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek4f880632009-07-17 22:18:43 +00001315 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001317 // Remove variable from local scope.
1318 if (ScopePos && VD == *ScopePos)
1319 ++ScopePos;
1320
Ted Kremenek4f880632009-07-17 22:18:43 +00001321 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001322}
1323
1324CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001325 // We may see an if statement in the middle of a basic block, or it may be the
1326 // first statement we are processing. In either case, we create a new basic
1327 // block. First, we create the blocks for the then...else statements, and
1328 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001329 // middle of a block, we stop processing that block. That block is then the
1330 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001331
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001332 // Save local scope position because in case of condition variable ScopePos
1333 // won't be restored when traversing AST.
1334 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1335
1336 // Create local scope for possible condition variable.
1337 // Store scope position. Add implicit destructor.
1338 if (VarDecl* VD = I->getConditionVariable()) {
1339 LocalScope::const_iterator BeginScopePos = ScopePos;
1340 addLocalScopeForVarDecl(VD);
1341 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1342 }
1343
Mike Stump6d9828c2009-07-17 01:31:16 +00001344 // The block we were proccessing is now finished. Make it the successor
1345 // block.
1346 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001347 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001348 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001349 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001350 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001351
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001352 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001353 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001354
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001355 if (Stmt* Else = I->getElse()) {
1356 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001357
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001358 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001359 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001361
1362 // If branch is not a compound statement create implicit scope
1363 // and add destructors.
1364 if (!isa<CompoundStmt>(Else))
1365 addLocalScopeAndDtors(Else);
1366
Ted Kremenek4f880632009-07-17 22:18:43 +00001367 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001368
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001369 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1370 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001371 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001372 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001373 return 0;
1374 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001375 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001376
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001377 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378 CFGBlock* ThenBlock;
1379 {
1380 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001381 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001382 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001383 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001384
1385 // If branch is not a compound statement create implicit scope
1386 // and add destructors.
1387 if (!isa<CompoundStmt>(Then))
1388 addLocalScopeAndDtors(Then);
1389
Ted Kremenek4f880632009-07-17 22:18:43 +00001390 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001391
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001392 if (!ThenBlock) {
1393 // We can reach here if the "then" body has all NullStmts.
1394 // Create an empty block so we can distinguish between true and false
1395 // branches in path-sensitive analyses.
1396 ThenBlock = createBlock(false);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001397 addSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001398 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001399 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001400 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001401 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001402 }
1403
Mike Stump6d9828c2009-07-17 01:31:16 +00001404 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001405 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001407 // Set the terminator of the new block to the If statement.
1408 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001409
Mike Stump00998a02009-07-23 23:25:26 +00001410 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001411 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001412
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001413 // Now add the successors.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001414 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1415 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001416
1417 // Add the condition as the last statement in the new block. This may create
1418 // new blocks as the condition may contain control-flow. Any newly created
1419 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001420 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001421
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001422 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1423 // and the condition variable initialization to the CFG.
1424 if (VarDecl *VD = I->getConditionVariable()) {
1425 if (Expr *Init = VD->getInit()) {
1426 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001427 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001428 addStmt(Init);
1429 }
1430 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001431
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001432 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433}
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
1435
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001436CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001437 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001438 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001439 // NOTE: If a "return" appears in the middle of a block, this means that the
1440 // code afterwards is DEAD (unreachable). We still keep a basic block
1441 // for that code; a simple "mark-and-sweep" from the entry block will be
1442 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001443
1444 // Create the new block.
1445 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001446
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001447 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001448 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001449 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001450
1451 // Add the return statement to the block. This may create new blocks if R
1452 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001453 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001454}
1455
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001456CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001457 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001458 addStmt(L->getSubStmt());
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001459 CFGBlock *LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
Ted Kremenek4f880632009-07-17 22:18:43 +00001461 if (!LabelBlock) // This can happen when the body is empty, i.e.
1462 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001463
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001464 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1465 "label already in map");
1466 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001467
1468 // Labels partition blocks, so this is the end of the basic block we were
1469 // processing (L is the block's label). Because this is label (and we have
1470 // already processed the substatement) there is no extra control-flow to worry
1471 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001472 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001473 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001474 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
1476 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001477 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001478
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001479 // This block is now the implicit successor of other blocks.
1480 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001481
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482 return LabelBlock;
1483}
1484
1485CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001486 // Goto is a control-flow statement. Thus we stop processing the current
1487 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001488
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489 Block = createBlock(false);
1490 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001491
1492 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001493 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001494
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001495 if (I == LabelMap.end())
1496 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001497 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1498 else {
1499 JumpTarget JT = I->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +00001500 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1501 addSuccessor(Block, JT.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001502 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001503
Mike Stump6d9828c2009-07-17 01:31:16 +00001504 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001505}
1506
1507CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001508 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001509
Marcin Swiderski47575f12010-10-01 01:38:14 +00001510 // Save local scope position because in case of condition variable ScopePos
1511 // won't be restored when traversing AST.
1512 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1513
1514 // Create local scope for init statement and possible condition variable.
1515 // Add destructor for init statement and condition variable.
1516 // Store scope position for continue statement.
1517 if (Stmt* Init = F->getInit())
1518 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001519 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1520
Marcin Swiderski47575f12010-10-01 01:38:14 +00001521 if (VarDecl* VD = F->getConditionVariable())
1522 addLocalScopeForVarDecl(VD);
1523 LocalScope::const_iterator ContinueScopePos = ScopePos;
1524
1525 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1526
Mike Stumpfefb9f72009-07-21 01:12:51 +00001527 // "for" is a control-flow statement. Thus we stop processing the current
1528 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001529 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001530 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001531 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001532 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001533 } else
1534 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001536 // Save the current value for the break targets.
1537 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001538 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001539 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001540
Mike Stump6d9828c2009-07-17 01:31:16 +00001541 // Because of short-circuit evaluation, the condition of the loop can span
1542 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1543 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001544 CFGBlock* ExitConditionBlock = createBlock(false);
1545 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001546
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001547 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001548 ExitConditionBlock->setTerminator(F);
1549
1550 // Now add the actual condition to the condition block. Because the condition
1551 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001552 if (Stmt* C = F->getCond()) {
1553 Block = ExitConditionBlock;
1554 EntryConditionBlock = addStmt(C);
Ted Kremenek9ce52702011-01-07 19:37:16 +00001555 if (badCFG)
1556 return 0;
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001557 assert(Block == EntryConditionBlock ||
1558 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001559
1560 // If this block contains a condition variable, add both the condition
1561 // variable and initializer to the CFG.
1562 if (VarDecl *VD = F->getConditionVariable()) {
1563 if (Expr *Init = VD->getInit()) {
1564 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001565 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001566 EntryConditionBlock = addStmt(Init);
1567 assert(Block == EntryConditionBlock);
1568 }
1569 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001570
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001571 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001572 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001573 return 0;
1574 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001575 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001576
Mike Stump6d9828c2009-07-17 01:31:16 +00001577 // The condition block is the implicit successor for the loop body as well as
1578 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001579 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001580
Mike Stump00998a02009-07-23 23:25:26 +00001581 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001582 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Mike Stump00998a02009-07-23 23:25:26 +00001584 if (F->getCond())
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001585 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001586
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001587 // Now create the loop body.
1588 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001589 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001590
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001591 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001592 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1593 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001594
Ted Kremenekaf603f72007-08-30 18:39:40 +00001595 // Create a new block to contain the (bottom) of the loop body.
1596 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001597
1598 // Loop body should end with destructor of Condition variable (if any).
1599 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001600
Ted Kremeneke9334502008-09-04 21:48:47 +00001601 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001602 // Generate increment code in its own basic block. This is the target of
1603 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001604 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001605 } else {
1606 // No increment code. Create a special, empty, block that is used as the
1607 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001608 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001609 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001610 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001611
Ted Kremenek3575f842009-04-28 00:51:56 +00001612 // Finish up the increment (or empty) block if it hasn't been already.
1613 if (Block) {
1614 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001615 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001616 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001617 Block = 0;
1618 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001619
Marcin Swiderski47575f12010-10-01 01:38:14 +00001620 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001621
Ted Kremenek3575f842009-04-28 00:51:56 +00001622 // The starting block for the loop increment is the block that should
1623 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001624 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001625
Marcin Swiderski47575f12010-10-01 01:38:14 +00001626 // If body is not a compound statement create implicit scope
1627 // and add destructors.
1628 if (!isa<CompoundStmt>(F->getBody()))
1629 addLocalScopeAndDtors(F->getBody());
1630
Mike Stump6d9828c2009-07-17 01:31:16 +00001631 // Now populate the body block, and in the process create new blocks as we
1632 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001633 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001634
1635 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001636 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001637 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001638 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001639
Ted Kremenek941fde82009-07-24 04:47:11 +00001640 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001641 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001642 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001643
Ted Kremenek941fde82009-07-24 04:47:11 +00001644 // Link up the condition block with the code that follows the loop. (the
1645 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001646 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001647
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001648 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001649 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001650 if (Stmt* I = F->getInit()) {
1651 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001652 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001653 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001654
1655 // There is no loop initialization. We are thus basically a while loop.
1656 // NULL out Block to force lazy block construction.
1657 Block = NULL;
1658 Succ = EntryConditionBlock;
1659 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001660}
1661
Ted Kremenek115c1b92010-04-11 17:02:10 +00001662CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1663 if (asc.alwaysAdd()) {
1664 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001665 appendStmt(Block, M, asc);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001666 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001667 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001668}
1669
Ted Kremenek514de5a2008-11-11 17:10:00 +00001670CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1671 // Objective-C fast enumeration 'for' statements:
1672 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1673 //
1674 // for ( Type newVariable in collection_expression ) { statements }
1675 //
1676 // becomes:
1677 //
1678 // prologue:
1679 // 1. collection_expression
1680 // T. jump to loop_entry
1681 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001682 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001683 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1684 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1685 // TB:
1686 // statements
1687 // T. jump to loop_entry
1688 // FB:
1689 // what comes after
1690 //
1691 // and
1692 //
1693 // Type existingItem;
1694 // for ( existingItem in expression ) { statements }
1695 //
1696 // becomes:
1697 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001698 // the same with newVariable replaced with existingItem; the binding works
1699 // the same except that for one ObjCForCollectionStmt::getElement() returns
1700 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001701 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001702
Ted Kremenek514de5a2008-11-11 17:10:00 +00001703 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001704
Ted Kremenek514de5a2008-11-11 17:10:00 +00001705 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001706 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001707 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001708 LoopSuccessor = Block;
1709 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001710 } else
1711 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001712
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001713 // Build the condition blocks.
1714 CFGBlock* ExitConditionBlock = createBlock(false);
1715 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001716
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001717 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001718 ExitConditionBlock->setTerminator(S);
1719
1720 // The last statement in the block should be the ObjCForCollectionStmt, which
1721 // performs the actual binding to 'element' and determines if there are any
1722 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001723 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001724 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001725
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001726 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001727 // generate new blocks as necesary. We DON'T add the statement by default to
1728 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001729 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001730 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001731 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001732 return 0;
1733 Block = 0;
1734 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001735
1736 // The condition block is the implicit successor for the loop body as well as
1737 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001738 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001739
Ted Kremenek514de5a2008-11-11 17:10:00 +00001740 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001741 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001742 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001743 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1744 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1745 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001746
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001747 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1748 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001749
Ted Kremenek4f880632009-07-17 22:18:43 +00001750 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001751
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001752 if (!BodyBlock)
1753 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001754 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001755 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001756 return 0;
1757 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001758
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001759 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001760 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001761 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001762
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001763 // Link up the condition block with the code that follows the loop.
1764 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001765 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001766
Ted Kremenek514de5a2008-11-11 17:10:00 +00001767 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001768 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001769 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001770}
1771
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001772CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1773 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001774
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001775 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001776 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001777
Ted Kremenekda5348e2009-05-05 23:11:51 +00001778 // The sync body starts its own basic block. This makes it a little easier
1779 // for diagnostic clients.
1780 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001781 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001782 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001783
Ted Kremenekda5348e2009-05-05 23:11:51 +00001784 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001785 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001786 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001787
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001788 // Add the @synchronized to the CFG.
1789 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001790 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001791
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001792 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001793 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001794}
Mike Stump6d9828c2009-07-17 01:31:16 +00001795
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001796CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001797 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001798 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001799}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001800
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001801CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001802 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001803
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001804 // Save local scope position because in case of condition variable ScopePos
1805 // won't be restored when traversing AST.
1806 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1807
1808 // Create local scope for possible condition variable.
1809 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001810 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001811 if (VarDecl* VD = W->getConditionVariable()) {
1812 addLocalScopeForVarDecl(VD);
1813 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1814 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001815
Mike Stumpfefb9f72009-07-21 01:12:51 +00001816 // "while" is a control-flow statement. Thus we stop processing the current
1817 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001818 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001819 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001820 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001821 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001822 } else
1823 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001824
1825 // Because of short-circuit evaluation, the condition of the loop can span
1826 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1827 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001828 CFGBlock* ExitConditionBlock = createBlock(false);
1829 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001830
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001831 // Set the terminator for the "exit" condition block.
1832 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001833
1834 // Now add the actual condition to the condition block. Because the condition
1835 // itself may contain control-flow, new blocks may be created. Thus we update
1836 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001837 if (Stmt* C = W->getCond()) {
1838 Block = ExitConditionBlock;
1839 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001840 // The condition might finish the current 'Block'.
1841 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001842
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001843 // If this block contains a condition variable, add both the condition
1844 // variable and initializer to the CFG.
1845 if (VarDecl *VD = W->getConditionVariable()) {
1846 if (Expr *Init = VD->getInit()) {
1847 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001848 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001849 EntryConditionBlock = addStmt(Init);
1850 assert(Block == EntryConditionBlock);
1851 }
1852 }
1853
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001854 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001855 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001856 return 0;
1857 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001858 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001859
1860 // The condition block is the implicit successor for the loop body as well as
1861 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001862 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001863
Mike Stump00998a02009-07-23 23:25:26 +00001864 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001865 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001866
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001867 // Process the loop body.
1868 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001869 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001870
1871 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001872 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1873 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1874 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001875
Mike Stump6d9828c2009-07-17 01:31:16 +00001876 // Create an empty block to represent the transition block for looping back
1877 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001878 Block = 0;
1879 assert(Succ == EntryConditionBlock);
1880 Succ = createBlock();
1881 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001882 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001884 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001885 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001886
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001887 // NULL out Block to force lazy instantiation of blocks for the body.
1888 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001889
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001890 // Loop body should end with destructor of Condition variable (if any).
1891 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1892
1893 // If body is not a compound statement create implicit scope
1894 // and add destructors.
1895 if (!isa<CompoundStmt>(W->getBody()))
1896 addLocalScopeAndDtors(W->getBody());
1897
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001898 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001899 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001900
Ted Kremenekaf603f72007-08-30 18:39:40 +00001901 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001902 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001903 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001904 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001905 return 0;
1906 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001907
Ted Kremenek941fde82009-07-24 04:47:11 +00001908 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001909 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001910 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001911
Ted Kremenek941fde82009-07-24 04:47:11 +00001912 // Link up the condition block with the code that follows the loop. (the
1913 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001914 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001915
1916 // There can be no more statements in the condition block since we loop back
1917 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001918 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001919
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001920 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001921 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001922 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001923}
Mike Stump1eb44332009-09-09 15:08:12 +00001924
1925
Ted Kremenek4f880632009-07-17 22:18:43 +00001926CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1927 // FIXME: For now we pretend that @catch and the code it contains does not
1928 // exit.
1929 return Block;
1930}
Mike Stump6d9828c2009-07-17 01:31:16 +00001931
Ted Kremenek2fda5042008-12-09 20:20:09 +00001932CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1933 // FIXME: This isn't complete. We basically treat @throw like a return
1934 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001935
Ted Kremenek6c249722009-09-24 18:45:41 +00001936 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001937 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001938 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001939
Ted Kremenek2fda5042008-12-09 20:20:09 +00001940 // Create the new block.
1941 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001942
Ted Kremenek2fda5042008-12-09 20:20:09 +00001943 // The Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001944 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
1946 // Add the statement to the block. This may create new blocks if S contains
1947 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001948 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001949}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001950
Mike Stump0979d802009-07-22 22:56:04 +00001951CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
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)
Mike Stump0979d802009-07-22 22:56:04 +00001954 return 0;
1955
1956 // Create the new block.
1957 Block = createBlock(false);
1958
Mike Stump5d1d2022010-01-19 02:20:09 +00001959 if (TryTerminatedBlock)
1960 // The current try statement is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001961 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001962 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001963 // otherwise the Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001964 addSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001965
1966 // Add the statement to the block. This may create new blocks if S contains
1967 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001968 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001969}
1970
Ted Kremenek4f880632009-07-17 22:18:43 +00001971CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001972 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001973
Mike Stump8f9893a2009-07-21 01:27:50 +00001974 // "do...while" is a control-flow statement. Thus we stop processing the
1975 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001976 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001977 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001978 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001979 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001980 } else
1981 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
1983 // Because of short-circuit evaluation, the condition of the loop can span
1984 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1985 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001986 CFGBlock* ExitConditionBlock = createBlock(false);
1987 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001988
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001989 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001990 ExitConditionBlock->setTerminator(D);
1991
1992 // Now add the actual condition to the condition block. Because the condition
1993 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001994 if (Stmt* C = D->getCond()) {
1995 Block = ExitConditionBlock;
1996 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001997 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001998 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001999 return 0;
2000 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002001 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002002
Ted Kremenek54827132008-02-27 07:20:00 +00002003 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002004 Succ = EntryConditionBlock;
2005
Mike Stump00998a02009-07-23 23:25:26 +00002006 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002007 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002008
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002009 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002010 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002011 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002012 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002013
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002014 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002015 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2016 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2017 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002018
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002019 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002020 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002021
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002022 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002023 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002024
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002025 // NULL out Block to force lazy instantiation of blocks for the body.
2026 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002027
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002028 // If body is not a compound statement create implicit scope
2029 // and add destructors.
2030 if (!isa<CompoundStmt>(D->getBody()))
2031 addLocalScopeAndDtors(D->getBody());
2032
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002033 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002034 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Ted Kremenekaf603f72007-08-30 18:39:40 +00002036 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002037 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002038 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002039 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002040 return 0;
2041 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002042
Ted Kremenekd173dc72010-08-17 20:59:56 +00002043 if (!KnownVal.isFalse()) {
2044 // Add an intermediate block between the BodyBlock and the
2045 // ExitConditionBlock to represent the "loop back" transition. Create an
2046 // empty block to represent the transition block for looping back to the
2047 // head of the loop.
2048 // FIXME: Can we do this more efficiently without adding another block?
2049 Block = NULL;
2050 Succ = BodyBlock;
2051 CFGBlock *LoopBackBlock = createBlock();
2052 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002053
Ted Kremenekd173dc72010-08-17 20:59:56 +00002054 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002055 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenekd173dc72010-08-17 20:59:56 +00002056 }
2057 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002058 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002059 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002060
Ted Kremenek941fde82009-07-24 04:47:11 +00002061 // Link up the condition block with the code that follows the loop.
2062 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002063 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002064
2065 // There can be no more statements in the body block(s) since we loop back to
2066 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002067 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002069 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002070 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002071 return BodyBlock;
2072}
2073
2074CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2075 // "continue" is a control-flow statement. Thus we stop processing the
2076 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002077 if (badCFG)
2078 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002079
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002080 // Now create a new block that ends with the continue statement.
2081 Block = createBlock(false);
2082 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002083
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002084 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002085 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00002086 if (ContinueJumpTarget.block) {
2087 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2088 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002089 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002090 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002091
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002092 return Block;
2093}
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002095CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002096 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002097
Ted Kremenek852274d2009-12-16 03:18:58 +00002098 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002099 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002100 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002101 }
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Ted Kremenek4f880632009-07-17 22:18:43 +00002103 // VLA types have expressions that must be evaluated.
2104 if (E->isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002105 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek4f880632009-07-17 22:18:43 +00002106 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2107 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002108 }
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Mike Stump6d9828c2009-07-17 01:31:16 +00002110 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002111}
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Ted Kremenek4f880632009-07-17 22:18:43 +00002113/// VisitStmtExpr - Utility method to handle (nested) statement
2114/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002115CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2116 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002117 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002118 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002119 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002120 return VisitCompoundStmt(SE->getSubStmt());
2121}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002122
Ted Kremenek411cdee2008-04-16 21:10:48 +00002123CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002124 // "switch" is a control-flow statement. Thus we stop processing the current
2125 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002126 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002127
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002128 // Save local scope position because in case of condition variable ScopePos
2129 // won't be restored when traversing AST.
2130 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2131
2132 // Create local scope for possible condition variable.
2133 // Store scope position. Add implicit destructor.
2134 if (VarDecl* VD = Terminator->getConditionVariable()) {
2135 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2136 addLocalScopeForVarDecl(VD);
2137 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2138 }
2139
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002140 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002141 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002142 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002143 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002144 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002145
2146 // Save the current "switch" context.
2147 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002148 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002149 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002150
Mike Stump6d9828c2009-07-17 01:31:16 +00002151 // Set the "default" case to be the block after the switch statement. If the
2152 // switch statement contains a "default:", this value will be overwritten with
2153 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002154 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002155
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002156 // Create a new block that will contain the switch statement.
2157 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002158
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002159 // Now process the switch body. The code after the switch is the implicit
2160 // successor.
2161 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002162 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002163
2164 // When visiting the body, the case statements should automatically get linked
2165 // up to the switch. We also don't keep a pointer to the body, since all
2166 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002167 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002168 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002169
2170 // If body is not a compound statement create implicit scope
2171 // and add destructors.
2172 if (!isa<CompoundStmt>(Terminator->getBody()))
2173 addLocalScopeAndDtors(Terminator->getBody());
2174
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002175 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002176 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002177 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002178 return 0;
2179 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002180
Mike Stump6d9828c2009-07-17 01:31:16 +00002181 // If we have no "default:" case, the default transition is to the code
2182 // following the switch body.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002183 addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002185 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002186 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002187 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002188 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002189 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002190
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002191 // Finally, if the SwitchStmt contains a condition variable, add both the
2192 // SwitchStmt and the condition variable initialization to the CFG.
2193 if (VarDecl *VD = Terminator->getConditionVariable()) {
2194 if (Expr *Init = VD->getInit()) {
2195 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002196 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002197 addStmt(Init);
2198 }
2199 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002200
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002201 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002202}
2203
Ted Kremenek4f880632009-07-17 22:18:43 +00002204CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002205 // CaseStmts are essentially labels, so they are the first statement in a
2206 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002207 CFGBlock *TopBlock = 0, *LastBlock = 0;
2208
2209 if (Stmt *Sub = CS->getSubStmt()) {
2210 // For deeply nested chains of CaseStmts, instead of doing a recursion
2211 // (which can blow out the stack), manually unroll and create blocks
2212 // along the way.
2213 while (isa<CaseStmt>(Sub)) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002214 CFGBlock *currentBlock = createBlock(false);
2215 currentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002216
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002217 if (TopBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002218 addSuccessor(LastBlock, currentBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002219 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002220 TopBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002221
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002222 addSuccessor(SwitchTerminatedBlock, currentBlock);
2223 LastBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002224
2225 CS = cast<CaseStmt>(Sub);
2226 Sub = CS->getSubStmt();
2227 }
2228
2229 addStmt(Sub);
2230 }
Mike Stump1eb44332009-09-09 15:08:12 +00002231
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002232 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002233 if (!CaseBlock)
2234 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002235
2236 // Cases statements partition blocks, so this is the top of the basic block we
2237 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002238 CaseBlock->setLabel(CS);
2239
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002240 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002241 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002242
2243 // Add this block to the list of successors for the block with the switch
2244 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002245 assert(SwitchTerminatedBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002246 addSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002247
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002248 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2249 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002250
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002251 if (TopBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002252 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002253 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002254 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002255 // This block is now the implicit successor of other blocks.
2256 Succ = CaseBlock;
2257 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002258
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002259 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002260}
Mike Stump6d9828c2009-07-17 01:31:16 +00002261
Ted Kremenek411cdee2008-04-16 21:10:48 +00002262CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002263 if (Terminator->getSubStmt())
2264 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002266 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002267
2268 if (!DefaultCaseBlock)
2269 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002270
2271 // Default statements partition blocks, so this is the top of the basic block
2272 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002273 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002275 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002276 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002277
Mike Stump6d9828c2009-07-17 01:31:16 +00002278 // Unlike case statements, we don't add the default block to the successors
2279 // for the switch statement immediately. This is done when we finish
2280 // processing the switch statement. This allows for the default case
2281 // (including a fall-through to the code after the switch statement) to always
2282 // be the last successor of a switch-terminated block.
2283
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002284 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2285 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002286
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002287 // This block is now the implicit successor of other blocks.
2288 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002289
2290 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002291}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002292
Mike Stump5d1d2022010-01-19 02:20:09 +00002293CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2294 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2295 // current block.
2296 CFGBlock* TrySuccessor = NULL;
2297
2298 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002299 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002300 return 0;
2301 TrySuccessor = Block;
2302 } else TrySuccessor = Succ;
2303
Mike Stumpa1f93632010-01-20 01:15:34 +00002304 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002305
2306 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002307 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002308 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002309 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002310
Mike Stumpa1f93632010-01-20 01:15:34 +00002311 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002312 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2313 // The code after the try is the implicit successor.
2314 Succ = TrySuccessor;
2315 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002316 if (CS->getExceptionDecl() == 0) {
2317 HasCatchAll = true;
2318 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002319 Block = NULL;
2320 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2321 if (CatchBlock == 0)
2322 return 0;
2323 // Add this block to the list of successors for the block with the try
2324 // statement.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002325 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002326 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002327 if (!HasCatchAll) {
2328 if (PrevTryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002329 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002330 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002331 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002332 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002333
2334 // The code after the try is the implicit successor.
2335 Succ = TrySuccessor;
2336
Mike Stumpf00cca52010-01-20 01:30:58 +00002337 // Save the current "try" context.
2338 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2339 TryTerminatedBlock = NewTryTerminatedBlock;
2340
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002341 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002342 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002343 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002344 return Block;
2345}
2346
2347CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2348 // CXXCatchStmt are treated like labels, so they are the first statement in a
2349 // block.
2350
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002351 // Save local scope position because in case of exception variable ScopePos
2352 // won't be restored when traversing AST.
2353 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2354
2355 // Create local scope for possible exception variable.
2356 // Store scope position. Add implicit destructor.
2357 if (VarDecl* VD = CS->getExceptionDecl()) {
2358 LocalScope::const_iterator BeginScopePos = ScopePos;
2359 addLocalScopeForVarDecl(VD);
2360 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2361 }
2362
Mike Stump5d1d2022010-01-19 02:20:09 +00002363 if (CS->getHandlerBlock())
2364 addStmt(CS->getHandlerBlock());
2365
2366 CFGBlock* CatchBlock = Block;
2367 if (!CatchBlock)
2368 CatchBlock = createBlock();
2369
2370 CatchBlock->setLabel(CS);
2371
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002372 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002373 return 0;
2374
2375 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2376 Block = NULL;
2377
2378 return CatchBlock;
2379}
2380
John McCall4765fa02010-12-06 08:20:24 +00002381CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002382 AddStmtChoice asc) {
2383 if (BuildOpts.AddImplicitDtors) {
2384 // If adding implicit destructors visit the full expression for adding
2385 // destructors of temporaries.
2386 VisitForTemporaryDtors(E->getSubExpr());
2387
2388 // Full expression has to be added as CFGStmt so it will be sequenced
2389 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002390 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002391 }
2392 return Visit(E->getSubExpr(), asc);
2393}
2394
Zhongxing Xua725ed42010-11-01 13:04:58 +00002395CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2396 AddStmtChoice asc) {
2397 if (asc.alwaysAdd()) {
2398 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002399 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002400
2401 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002402 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002403 }
2404 return Visit(E->getSubExpr(), asc);
2405}
2406
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002407CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2408 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002409 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002410 if (!C->isElidable())
Ted Kremenek892697d2010-12-16 07:46:53 +00002411 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002412
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002413 return VisitChildren(C);
2414}
2415
Zhongxing Xua725ed42010-11-01 13:04:58 +00002416CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2417 AddStmtChoice asc) {
2418 if (asc.alwaysAdd()) {
2419 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002420 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002421 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002422 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002423 }
2424 return Visit(E->getSubExpr(), asc);
2425}
2426
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002427CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2428 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002429 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002430 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002431 return VisitChildren(C);
2432}
2433
Ted Kremenekad5a8942010-08-02 23:46:59 +00002434CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002435 AddStmtChoice asc) {
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002436 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002437 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002438 return VisitChildren(C);
2439}
2440
Zhongxing Xua725ed42010-11-01 13:04:58 +00002441CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2442 AddStmtChoice asc) {
2443 if (asc.alwaysAdd()) {
2444 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002445 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002446 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002447 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002448}
2449
Ted Kremenek19bb3562007-08-28 19:26:49 +00002450CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002451 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002452 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002453
Ted Kremenek19bb3562007-08-28 19:26:49 +00002454 if (!IBlock) {
2455 IBlock = createBlock(false);
2456 cfg->setIndirectGotoBlock(IBlock);
2457 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002458
Ted Kremenek19bb3562007-08-28 19:26:49 +00002459 // IndirectGoto is a control-flow statement. Thus we stop processing the
2460 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002461 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002462 return 0;
2463
Ted Kremenek19bb3562007-08-28 19:26:49 +00002464 Block = createBlock(false);
2465 Block->setTerminator(I);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002466 addSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002467 return addStmt(I->getTarget());
2468}
2469
Marcin Swiderski8599e762010-11-03 06:19:35 +00002470CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2471tryAgain:
2472 if (!E) {
2473 badCFG = true;
2474 return NULL;
2475 }
2476 switch (E->getStmtClass()) {
2477 default:
2478 return VisitChildrenForTemporaryDtors(E);
2479
2480 case Stmt::BinaryOperatorClass:
2481 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2482
2483 case Stmt::CXXBindTemporaryExprClass:
2484 return VisitCXXBindTemporaryExprForTemporaryDtors(
2485 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2486
John McCall56ca35d2011-02-17 10:25:35 +00002487 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski8599e762010-11-03 06:19:35 +00002488 case Stmt::ConditionalOperatorClass:
2489 return VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002490 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002491
2492 case Stmt::ImplicitCastExprClass:
2493 // For implicit cast we want BindToTemporary to be passed further.
2494 E = cast<CastExpr>(E)->getSubExpr();
2495 goto tryAgain;
2496
2497 case Stmt::ParenExprClass:
2498 E = cast<ParenExpr>(E)->getSubExpr();
2499 goto tryAgain;
2500 }
2501}
2502
2503CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2504 // When visiting children for destructors we want to visit them in reverse
2505 // order. Because there's no reverse iterator for children must to reverse
2506 // them in helper vector.
2507 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2508 ChildrenVect ChildrenRev;
John McCall7502c1d2011-02-13 04:07:26 +00002509 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002510 if (*I) ChildrenRev.push_back(*I);
2511 }
2512
2513 CFGBlock *B = Block;
2514 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2515 L = ChildrenRev.rend(); I != L; ++I) {
2516 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2517 B = R;
2518 }
2519 return B;
2520}
2521
2522CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2523 if (E->isLogicalOp()) {
2524 // Destructors for temporaries in LHS expression should be called after
2525 // those for RHS expression. Even if this will unnecessarily create a block,
2526 // this block will be used at least by the full expression.
2527 autoCreateBlock();
2528 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2529 if (badCFG)
2530 return NULL;
2531
2532 Succ = ConfluenceBlock;
2533 Block = NULL;
2534 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2535
2536 if (RHSBlock) {
2537 if (badCFG)
2538 return NULL;
2539
2540 // If RHS expression did produce destructors we need to connect created
2541 // blocks to CFG in same manner as for binary operator itself.
2542 CFGBlock *LHSBlock = createBlock(false);
2543 LHSBlock->setTerminator(CFGTerminator(E, true));
2544
2545 // For binary operator LHS block is before RHS in list of predecessors
2546 // of ConfluenceBlock.
2547 std::reverse(ConfluenceBlock->pred_begin(),
2548 ConfluenceBlock->pred_end());
2549
2550 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002551 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002552 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2553 KnownVal.negate();
2554
2555 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2556 // itself.
2557 if (E->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002558 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2559 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002560 } else {
2561 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002562 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2563 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002564 }
2565
2566 Block = LHSBlock;
2567 return LHSBlock;
2568 }
2569
2570 Block = ConfluenceBlock;
2571 return ConfluenceBlock;
2572 }
2573
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002574 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002575 // For assignment operator (=) LHS expression is visited
2576 // before RHS expression. For destructors visit them in reverse order.
2577 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2578 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2579 return LHSBlock ? LHSBlock : RHSBlock;
2580 }
2581
2582 // For any other binary operator RHS expression is visited before
2583 // LHS expression (order of children). For destructors visit them in reverse
2584 // order.
2585 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2586 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2587 return RHSBlock ? RHSBlock : LHSBlock;
2588}
2589
2590CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2591 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2592 // First add destructors for temporaries in subexpression.
2593 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002594 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002595 // If lifetime of temporary is not prolonged (by assigning to constant
2596 // reference) add destructor for it.
2597 autoCreateBlock();
2598 appendTemporaryDtor(Block, E);
2599 B = Block;
2600 }
2601 return B;
2602}
2603
2604CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002605 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002606 // First add destructors for condition expression. Even if this will
2607 // unnecessarily create a block, this block will be used at least by the full
2608 // expression.
2609 autoCreateBlock();
2610 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2611 if (badCFG)
2612 return NULL;
John McCall56ca35d2011-02-17 10:25:35 +00002613 if (BinaryConditionalOperator *BCO
2614 = dyn_cast<BinaryConditionalOperator>(E)) {
2615 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002616 if (badCFG)
2617 return NULL;
2618 }
2619
John McCall56ca35d2011-02-17 10:25:35 +00002620 // Try to add block with destructors for LHS expression.
2621 CFGBlock *LHSBlock = NULL;
2622 Succ = ConfluenceBlock;
2623 Block = NULL;
2624 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2625 if (badCFG)
2626 return NULL;
2627
Marcin Swiderski8599e762010-11-03 06:19:35 +00002628 // Try to add block with destructors for RHS expression;
2629 Succ = ConfluenceBlock;
2630 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00002631 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2632 BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002633 if (badCFG)
2634 return NULL;
2635
2636 if (!RHSBlock && !LHSBlock) {
2637 // If neither LHS nor RHS expression had temporaries to destroy don't create
2638 // more blocks.
2639 Block = ConfluenceBlock;
2640 return Block;
2641 }
2642
2643 Block = createBlock(false);
2644 Block->setTerminator(CFGTerminator(E, true));
2645
2646 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002647 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002648
2649 if (LHSBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002650 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002651 } else if (KnownVal.isFalse()) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002652 addSuccessor(Block, NULL);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002653 } else {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002654 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002655 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2656 }
2657
2658 if (!RHSBlock)
2659 RHSBlock = ConfluenceBlock;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002660 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002661
2662 return Block;
2663}
2664
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002665} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002666
Mike Stump6d9828c2009-07-17 01:31:16 +00002667/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2668/// no successors or predecessors. If this is the first block created in the
2669/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002670CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002671 bool first_block = begin() == end();
2672
2673 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002674 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2675 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2676 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002677
2678 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002679 if (first_block)
2680 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002681
2682 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002683 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002684}
2685
Ted Kremenek026473c2007-08-23 16:51:22 +00002686/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2687/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002688CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002689 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002690 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002691 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002692}
2693
Ted Kremenek63f58872007-10-01 19:33:33 +00002694//===----------------------------------------------------------------------===//
2695// CFG: Queries for BlkExprs.
2696//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002697
Ted Kremenek63f58872007-10-01 19:33:33 +00002698namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002699 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002700}
2701
Ted Kremenek8a693662009-12-23 23:37:10 +00002702static void FindSubExprAssignments(Stmt *S,
2703 llvm::SmallPtrSet<Expr*,50>& Set) {
2704 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002705 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002706
John McCall7502c1d2011-02-13 04:07:26 +00002707 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002708 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002709 if (!child)
2710 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002711
Ted Kremenek8a693662009-12-23 23:37:10 +00002712 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002713 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002714
Ted Kremenek8a693662009-12-23 23:37:10 +00002715 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002716 }
2717}
2718
Ted Kremenek63f58872007-10-01 19:33:33 +00002719static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2720 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002721
2722 // Look for assignments that are used as subexpressions. These are the only
2723 // assignments that we want to *possibly* register as a block-level
2724 // expression. Basically, if an assignment occurs both in a subexpression and
2725 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002726 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002727
Ted Kremenek63f58872007-10-01 19:33:33 +00002728 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002729 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002730 if (CFGStmt S = BI->getAs<CFGStmt>())
2731 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002732
Ted Kremenek411cdee2008-04-16 21:10:48 +00002733 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002734
2735 // Iterate over the statements again on identify the Expr* and Stmt* at the
2736 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002737
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002738 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2739 CFGStmt CS = BI->getAs<CFGStmt>();
2740 if (!CS.isValid())
2741 continue;
2742 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002743
Ted Kremenek411cdee2008-04-16 21:10:48 +00002744 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002745 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002746 // expression are really "statements" whose value is never used by
2747 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002748 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002749 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002750 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2751 // Special handling for statement expressions. The last statement in
2752 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002753 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002754 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002755 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002756 (*M)[C->body_back()] = x;
2757 }
2758 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002759
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002760 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002761 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002762 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002763 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002764
Ted Kremenek411cdee2008-04-16 21:10:48 +00002765 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002766
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002767 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002768
Ted Kremenek390e48b2008-11-12 21:11:49 +00002769 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002770 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002771 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002772 }
2773 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002774
Ted Kremenek63f58872007-10-01 19:33:33 +00002775 return M;
2776}
2777
Ted Kremenek86946742008-01-17 20:48:37 +00002778CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2779 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002780 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002781
Ted Kremenek63f58872007-10-01 19:33:33 +00002782 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002783 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002784 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002785}
2786
2787unsigned CFG::getNumBlkExprs() {
2788 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2789 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002790
2791 // We assume callers interested in the number of BlkExprs will want
2792 // the map constructed if it doesn't already exist.
2793 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2794 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00002795}
2796
Ted Kremenek274f4332008-04-28 18:00:46 +00002797//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002798// Filtered walking of the CFG.
2799//===----------------------------------------------------------------------===//
2800
2801bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002802 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002803
2804 if (F.IgnoreDefaultsWithCoveredEnums) {
2805 // If the 'To' has no label or is labeled but the label isn't a
2806 // CaseStmt then filter this edge.
2807 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002808 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002809 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002810 const Stmt *L = To->getLabel();
2811 if (!L || !isa<CaseStmt>(L))
2812 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002813 }
2814 }
2815 }
2816
2817 return false;
2818}
2819
2820//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002821// Cleanup: CFG dstor.
2822//===----------------------------------------------------------------------===//
2823
Ted Kremenek63f58872007-10-01 19:33:33 +00002824CFG::~CFG() {
2825 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2826}
Mike Stump6d9828c2009-07-17 01:31:16 +00002827
Ted Kremenek7dba8602007-08-29 21:56:09 +00002828//===----------------------------------------------------------------------===//
2829// CFG pretty printing
2830//===----------------------------------------------------------------------===//
2831
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002832namespace {
2833
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002834class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002835 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002836 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002837 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002838 DeclMapTy DeclMap;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002839 signed currentBlock;
2840 unsigned currentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002841 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002842public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002843
Chris Lattnere4f21422009-06-30 01:26:17 +00002844 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002845 : currentBlock(0), currentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002846 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2847 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002848 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002849 BI != BEnd; ++BI, ++j ) {
2850 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2851 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2852 StmtMap[SE] = P;
2853
2854 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2855 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002856
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002857 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2858 if (VarDecl* VD = IS->getConditionVariable())
2859 DeclMap[VD] = P;
2860
2861 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2862 if (VarDecl* VD = FS->getConditionVariable())
2863 DeclMap[VD] = P;
2864
2865 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2866 if (VarDecl* VD = WS->getConditionVariable())
2867 DeclMap[VD] = P;
2868
2869 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2870 if (VarDecl* VD = SS->getConditionVariable())
2871 DeclMap[VD] = P;
2872
2873 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2874 if (VarDecl* VD = CS->getExceptionDecl())
2875 DeclMap[VD] = P;
2876 }
2877 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002878 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002879 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002880 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002881
Ted Kremenek42a509f2007-08-31 21:30:12 +00002882 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002883
Chris Lattnere4f21422009-06-30 01:26:17 +00002884 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002885 void setBlockID(signed i) { currentBlock = i; }
2886 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002887
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002888 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2889 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002890
2891 if (I == StmtMap.end())
2892 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002893
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002894 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2895 && I->second.second == currentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002896 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002897 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002898
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002899 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002900 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002901 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002902
2903 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2904 DeclMapTy::iterator I = DeclMap.find(D);
2905
2906 if (I == DeclMap.end())
2907 return false;
2908
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002909 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2910 && I->second.second == currentStmt) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002911 return false;
2912 }
2913
2914 OS << "[B" << I->second.first << "." << I->second.second << "]";
2915 return true;
2916 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002917};
Chris Lattnere4f21422009-06-30 01:26:17 +00002918} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002919
Chris Lattnere4f21422009-06-30 01:26:17 +00002920
2921namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002922class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002923 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002924
Ted Kremeneka95d3752008-09-13 05:16:45 +00002925 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002926 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002927 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002928public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002929 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002930 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002931 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002932
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002933 void VisitIfStmt(IfStmt* I) {
2934 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002935 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002936 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002937
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002938 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002939 void VisitStmt(Stmt* Terminator) {
2940 Terminator->printPretty(OS, Helper, Policy);
2941 }
2942
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002943 void VisitForStmt(ForStmt* F) {
2944 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002945 if (F->getInit())
2946 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002947 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002948 if (Stmt* C = F->getCond())
2949 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002950 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002951 if (F->getInc())
2952 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002953 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002954 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002955
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002956 void VisitWhileStmt(WhileStmt* W) {
2957 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002958 if (Stmt* C = W->getCond())
2959 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002960 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002961
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002962 void VisitDoStmt(DoStmt* D) {
2963 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002964 if (Stmt* C = D->getCond())
2965 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002966 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002967
Ted Kremenek411cdee2008-04-16 21:10:48 +00002968 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002969 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002970 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002971 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002972
Mike Stump5d1d2022010-01-19 02:20:09 +00002973 void VisitCXXTryStmt(CXXTryStmt* CS) {
2974 OS << "try ...";
2975 }
2976
John McCall56ca35d2011-02-17 10:25:35 +00002977 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002978 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002979 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002980 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002981
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002982 void VisitChooseExpr(ChooseExpr* C) {
2983 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002984 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002985 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002986 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002987
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002988 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2989 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002990 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002991 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002992
Ted Kremenek805e9a82007-08-31 21:49:40 +00002993 void VisitBinaryOperator(BinaryOperator* B) {
2994 if (!B->isLogicalOp()) {
2995 VisitExpr(B);
2996 return;
2997 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002998
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002999 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003000
Ted Kremenek805e9a82007-08-31 21:49:40 +00003001 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003002 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003003 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003004 return;
John McCall2de56d12010-08-25 11:45:40 +00003005 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003006 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003007 return;
3008 default:
3009 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003010 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003011 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003012
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003013 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003014 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003015 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003016};
Chris Lattnere4f21422009-06-30 01:26:17 +00003017} // end anonymous namespace
3018
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003019static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003020 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003021 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3022 Stmt *S = CS;
3023
3024 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003025
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003026 // special printing for statement-expressions.
3027 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3028 CompoundStmt* Sub = SE->getSubStmt();
3029
John McCall7502c1d2011-02-13 04:07:26 +00003030 if (Sub->children()) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003031 OS << "({ ... ; ";
3032 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3033 OS << " })\n";
3034 return;
3035 }
3036 }
3037 // special printing for comma expressions.
3038 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3039 if (B->getOpcode() == BO_Comma) {
3040 OS << "... , ";
3041 Helper->handledStmt(B->getRHS(),OS);
3042 OS << '\n';
3043 return;
3044 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003045 }
3046 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003047 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003048
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003049 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003050 OS << " (OperatorCall)";
3051 } else if (isa<CXXBindTemporaryExpr>(S)) {
3052 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003053 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003054
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003055 // Expressions need a newline.
3056 if (isa<Expr>(S))
3057 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003058
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003059 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
Sean Huntcbb67482011-01-08 20:30:50 +00003060 CXXCtorInitializer* I = IE;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003061 if (I->isBaseInitializer())
3062 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003063 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003064
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003065 OS << "(";
3066 if (Expr* IE = I->getInit())
3067 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3068 OS << ")";
3069
3070 if (I->isBaseInitializer())
3071 OS << " (Base initializer)\n";
3072 else OS << " (Member initializer)\n";
3073
3074 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3075 VarDecl* VD = DE.getVarDecl();
3076 Helper->handleDecl(VD, OS);
3077
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003078 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003079 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3080 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003081 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3082 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003083
3084 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3085 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003086
3087 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3088 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3089 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003090 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003091
3092 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3093 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003094
3095 const Type *T = FD->getType().getTypePtr();
3096 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3097 T = ET;
3098
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003099 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003100 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003101 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003102
3103 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3104 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3105 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3106 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003107 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003108}
Mike Stump6d9828c2009-07-17 01:31:16 +00003109
Chris Lattnere4f21422009-06-30 01:26:17 +00003110static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3111 const CFGBlock& B,
3112 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003113
Ted Kremenek42a509f2007-08-31 21:30:12 +00003114 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003115
Ted Kremenek7dba8602007-08-29 21:56:09 +00003116 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003117 OS << "\n [ B" << B.getBlockID();
3118
Ted Kremenek42a509f2007-08-31 21:30:12 +00003119 if (&B == &cfg->getEntry())
3120 OS << " (ENTRY) ]\n";
3121 else if (&B == &cfg->getExit())
3122 OS << " (EXIT) ]\n";
3123 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003124 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003125 else
3126 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003127
Ted Kremenek9cffe732007-08-29 23:20:49 +00003128 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003129 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003130
3131 if (print_edges)
3132 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003133
Mike Stump079bd722010-01-19 22:00:14 +00003134 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003135 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003136 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003137 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003138 C->getLHS()->printPretty(OS, Helper,
3139 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003140 if (C->getRHS()) {
3141 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003142 C->getRHS()->printPretty(OS, Helper,
3143 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003144 }
Mike Stump079bd722010-01-19 22:00:14 +00003145 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003146 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003147 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003148 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003149 if (CS->getExceptionDecl())
3150 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3151 0);
3152 else
3153 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003154 OS << ")";
3155
3156 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003157 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003158
Ted Kremenek9cffe732007-08-29 23:20:49 +00003159 OS << ":\n";
3160 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003161
Ted Kremenekfddd5182007-08-21 21:42:03 +00003162 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003163 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003164
Ted Kremenek42a509f2007-08-31 21:30:12 +00003165 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3166 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003167
Ted Kremenek9cffe732007-08-29 23:20:49 +00003168 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003169 if (print_edges)
3170 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003171
Ted Kremeneka95d3752008-09-13 05:16:45 +00003172 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003173
Ted Kremenek42a509f2007-08-31 21:30:12 +00003174 if (Helper)
3175 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003176
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003177 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003178 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003179
Ted Kremenek9cffe732007-08-29 23:20:49 +00003180 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003181 if (B.getTerminator()) {
3182 if (print_edges)
3183 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003184
Ted Kremenek9cffe732007-08-29 23:20:49 +00003185 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003186
Ted Kremenek42a509f2007-08-31 21:30:12 +00003187 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003188
Chris Lattnere4f21422009-06-30 01:26:17 +00003189 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3190 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003191 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003192 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003193 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003194
Ted Kremenek9cffe732007-08-29 23:20:49 +00003195 if (print_edges) {
3196 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003197 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003198 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003199
Ted Kremenek42a509f2007-08-31 21:30:12 +00003200 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3201 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003202
Ted Kremenek42a509f2007-08-31 21:30:12 +00003203 if (i == 8 || (i-8) == 0)
3204 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003205
Ted Kremenek9cffe732007-08-29 23:20:49 +00003206 OS << " B" << (*I)->getBlockID();
3207 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003208
Ted Kremenek42a509f2007-08-31 21:30:12 +00003209 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003210
Ted Kremenek42a509f2007-08-31 21:30:12 +00003211 // Print the successors of this block.
3212 OS << " Successors (" << B.succ_size() << "):";
3213 i = 0;
3214
3215 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3216 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003217
Ted Kremenek42a509f2007-08-31 21:30:12 +00003218 if (i == 8 || (i-8) % 10 == 0)
3219 OS << "\n ";
3220
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003221 if (*I)
3222 OS << " B" << (*I)->getBlockID();
3223 else
3224 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003225 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003226
Ted Kremenek9cffe732007-08-29 23:20:49 +00003227 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003228 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003229}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003230
Ted Kremenek42a509f2007-08-31 21:30:12 +00003231
3232/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003233void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003234
3235/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003236void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3237 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003238
Ted Kremenek42a509f2007-08-31 21:30:12 +00003239 // Print the entry block.
3240 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003241
Ted Kremenek42a509f2007-08-31 21:30:12 +00003242 // Iterate through the CFGBlocks and print them one by one.
3243 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3244 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003245 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003246 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003247
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003248 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003249 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003250
Ted Kremenek42a509f2007-08-31 21:30:12 +00003251 // Print the exit block.
3252 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003253 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003254}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003255
3256/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003257void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3258 print(llvm::errs(), cfg, LO);
3259}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003260
3261/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3262/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003263void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3264 const LangOptions &LO) const {
3265 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003266 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003267}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003268
Ted Kremeneka2925852008-01-30 23:02:42 +00003269/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003270void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003271 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003272 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003273 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003274}
3275
Ted Kremenek390e48b2008-11-12 21:11:49 +00003276Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003277 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003278 if (!Terminator)
3279 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003280
Ted Kremenek411cdee2008-04-16 21:10:48 +00003281 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003282
Ted Kremenek411cdee2008-04-16 21:10:48 +00003283 switch (Terminator->getStmtClass()) {
3284 default:
3285 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003286
Ted Kremenek411cdee2008-04-16 21:10:48 +00003287 case Stmt::ForStmtClass:
3288 E = cast<ForStmt>(Terminator)->getCond();
3289 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003290
Ted Kremenek411cdee2008-04-16 21:10:48 +00003291 case Stmt::WhileStmtClass:
3292 E = cast<WhileStmt>(Terminator)->getCond();
3293 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003294
Ted Kremenek411cdee2008-04-16 21:10:48 +00003295 case Stmt::DoStmtClass:
3296 E = cast<DoStmt>(Terminator)->getCond();
3297 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003298
Ted Kremenek411cdee2008-04-16 21:10:48 +00003299 case Stmt::IfStmtClass:
3300 E = cast<IfStmt>(Terminator)->getCond();
3301 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003302
Ted Kremenek411cdee2008-04-16 21:10:48 +00003303 case Stmt::ChooseExprClass:
3304 E = cast<ChooseExpr>(Terminator)->getCond();
3305 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003306
Ted Kremenek411cdee2008-04-16 21:10:48 +00003307 case Stmt::IndirectGotoStmtClass:
3308 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3309 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003310
Ted Kremenek411cdee2008-04-16 21:10:48 +00003311 case Stmt::SwitchStmtClass:
3312 E = cast<SwitchStmt>(Terminator)->getCond();
3313 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003314
John McCall56ca35d2011-02-17 10:25:35 +00003315 case Stmt::BinaryConditionalOperatorClass:
3316 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3317 break;
3318
Ted Kremenek411cdee2008-04-16 21:10:48 +00003319 case Stmt::ConditionalOperatorClass:
3320 E = cast<ConditionalOperator>(Terminator)->getCond();
3321 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003322
Ted Kremenek411cdee2008-04-16 21:10:48 +00003323 case Stmt::BinaryOperatorClass: // '&&' and '||'
3324 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003325 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003326
Ted Kremenek390e48b2008-11-12 21:11:49 +00003327 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003328 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003329 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003330
Ted Kremenek411cdee2008-04-16 21:10:48 +00003331 return E ? E->IgnoreParens() : NULL;
3332}
3333
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003334bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003335 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003336 if (!Terminator)
3337 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003338
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003339 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003340
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003341 switch (Terminator->getStmtClass()) {
3342 default:
3343 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003344
3345 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003346 case Stmt::WhileStmtClass:
3347 case Stmt::DoStmtClass:
3348 case Stmt::IfStmtClass:
3349 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +00003350 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003351 case Stmt::ConditionalOperatorClass:
3352 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003353 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003354 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003355
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003356 return E ? E->IgnoreParens() : NULL;
3357}
3358
Ted Kremeneka2925852008-01-30 23:02:42 +00003359
Ted Kremenek7dba8602007-08-29 21:56:09 +00003360//===----------------------------------------------------------------------===//
3361// CFG Graphviz Visualization
3362//===----------------------------------------------------------------------===//
3363
Ted Kremenek42a509f2007-08-31 21:30:12 +00003364
3365#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003366static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003367#endif
3368
Chris Lattnere4f21422009-06-30 01:26:17 +00003369void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003370#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003371 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003372 GraphHelper = &H;
3373 llvm::ViewGraph(this,"CFG");
3374 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003375#endif
3376}
3377
Ted Kremenek7dba8602007-08-29 21:56:09 +00003378namespace llvm {
3379template<>
3380struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003381
3382 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3383
3384 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003385
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003386#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003387 std::string OutSStr;
3388 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003389 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003390 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003391
3392 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3393
3394 // Process string output to make it nicer...
3395 for (unsigned i = 0; i != OutStr.length(); ++i)
3396 if (OutStr[i] == '\n') { // Left justify
3397 OutStr[i] = '\\';
3398 OutStr.insert(OutStr.begin()+i+1, 'l');
3399 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003400
Ted Kremenek7dba8602007-08-29 21:56:09 +00003401 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003402#else
3403 return "";
3404#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003405 }
3406};
3407} // end namespace llvm