blob: cc6e9c592a1e30d9b65c41802d669899d6d47b4f [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"
Ted Kremenekc56c0042011-02-23 05:11:46 +000020#include "clang/AST/CharUnits.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000022#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000027
Ted Kremenekfddd5182007-08-21 21:42:03 +000028using namespace clang;
29
30namespace {
31
Douglas Gregor4afa39d2009-01-20 01:17:11 +000032static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000033 if (VarDecl* VD = dyn_cast<VarDecl>(D))
34 if (Expr* Ex = VD->getInit())
35 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000036 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000039/// The CFG builder uses a recursive algorithm to build the CFG. When
40/// we process an expression, sometimes we know that we must add the
41/// subexpressions as block-level expressions. For example:
42///
43/// exp1 || exp2
44///
45/// When processing the '||' expression, we know that exp1 and exp2
46/// need to be added as block-level expressions, even though they
47/// might not normally need to be. AddStmtChoice records this
48/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
49/// the builder has an option not to add a subexpression as a
50/// block-level expression.
51///
Ted Kremenek852274d2009-12-16 03:18:58 +000052class AddStmtChoice {
53public:
Ted Kremenek892697d2010-12-16 07:46:53 +000054 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000055
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000056 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000057
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000058 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000059
60 /// Return a copy of this object, except with the 'always-add' bit
61 /// set as specified.
62 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
63 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
64 Kind(kind & ~AlwaysAdd));
65 }
66
Ted Kremenek852274d2009-12-16 03:18:58 +000067private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000068 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000069};
Mike Stump6d9828c2009-07-17 01:31:16 +000070
Marcin Swiderskif1308c72010-09-25 11:05:21 +000071/// LocalScope - Node in tree of local scopes created for C++ implicit
72/// destructor calls generation. It contains list of automatic variables
73/// declared in the scope and link to position in previous scope this scope
74/// began in.
75///
76/// The process of creating local scopes is as follows:
77/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
78/// - Before processing statements in scope (e.g. CompoundStmt) create
79/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
80/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000081/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000082/// at this VarDecl,
83/// - For every normal (without jump) end of scope add to CFGBlock destructors
84/// for objects in the current scope,
85/// - For every jump add to CFGBlock destructors for objects
86/// between CFGBuilder::ScopePos and local scope position saved for jump
87/// target. Thanks to C++ restrictions on goto jumps we can be sure that
88/// jump target position will be on the path to root from CFGBuilder::ScopePos
89/// (adding any variable that doesn't need constructor to be called to
90/// LocalScope can break this assumption),
91///
92class LocalScope {
93public:
Ted Kremenekfe59b742011-02-15 02:47:45 +000094 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderskif1308c72010-09-25 11:05:21 +000095
96 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000097 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +000098 class const_iterator {
99 const LocalScope* Scope;
100
101 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
102 /// Invalid iterator (with null Scope) has VarIter equal to 0.
103 unsigned VarIter;
104
105 public:
106 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
107 /// Incrementing invalid iterator is allowed and will result in invalid
108 /// iterator.
109 const_iterator()
110 : Scope(NULL), VarIter(0) {}
111
112 /// Create valid iterator. In case when S.Prev is an invalid iterator and
113 /// I is equal to 0, this will create invalid iterator.
114 const_iterator(const LocalScope& S, unsigned I)
115 : Scope(&S), VarIter(I) {
116 // Iterator to "end" of scope is not allowed. Handle it by going up
117 // in scopes tree possibly up to invalid iterator in the root.
118 if (VarIter == 0 && Scope)
119 *this = Scope->Prev;
120 }
121
122 VarDecl* const* operator->() const {
123 assert (Scope && "Dereferencing invalid iterator is not allowed");
124 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
125 return &Scope->Vars[VarIter - 1];
126 }
127 VarDecl* operator*() const {
128 return *this->operator->();
129 }
130
131 const_iterator& operator++() {
132 if (!Scope)
133 return *this;
134
135 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
136 --VarIter;
137 if (VarIter == 0)
138 *this = Scope->Prev;
139 return *this;
140 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000141 const_iterator operator++(int) {
142 const_iterator P = *this;
143 ++*this;
144 return P;
145 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000146
147 bool operator==(const const_iterator& rhs) const {
148 return Scope == rhs.Scope && VarIter == rhs.VarIter;
149 }
150 bool operator!=(const const_iterator& rhs) const {
151 return !(*this == rhs);
152 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000153
154 operator bool() const {
155 return *this != const_iterator();
156 }
157
158 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000159 };
160
161 friend class const_iterator;
162
163private:
Ted Kremenekfe59b742011-02-15 02:47:45 +0000164 BumpVectorContext ctx;
165
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000166 /// Automatic variables in order of declaration.
167 AutomaticVarsTy Vars;
168 /// Iterator to variable in previous scope that was declared just before
169 /// begin of this scope.
170 const_iterator Prev;
171
172public:
173 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekfe59b742011-02-15 02:47:45 +0000174 LocalScope(BumpVectorContext &ctx, const_iterator P)
175 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000176
177 /// Begin of scope in direction of CFG building (backwards).
178 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000179
180 void addVar(VarDecl* VD) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000181 Vars.push_back(VD, ctx);
Marcin Swiderski35387a02010-09-30 22:42:32 +0000182 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000183};
184
Marcin Swiderski35387a02010-09-30 22:42:32 +0000185/// distance - Calculates distance from this to L. L must be reachable from this
186/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
187/// number of scopes between this and L.
188int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
189 int D = 0;
190 const_iterator F = *this;
191 while (F.Scope != L.Scope) {
192 assert (F != const_iterator()
193 && "L iterator is not reachable from F iterator.");
194 D += F.VarIter;
195 F = F.Scope->Prev;
196 }
197 D += F.VarIter - L.VarIter;
198 return D;
199}
200
201/// BlockScopePosPair - Structure for specifying position in CFG during its
202/// build process. It consists of CFGBlock that specifies position in CFG graph
203/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000204struct BlockScopePosPair {
Ted Kremenek9ce52702011-01-07 19:37:16 +0000205 BlockScopePosPair() : block(0) {}
206 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
207 : block(b), scopePosition(scopePos) {}
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000208
Ted Kremenek9ce52702011-01-07 19:37:16 +0000209 CFGBlock *block;
210 LocalScope::const_iterator scopePosition;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000211};
212
Ted Kremeneka34ea072008-08-04 22:51:42 +0000213/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000214/// The builder is stateful: an instance of the builder should be used to only
215/// construct a single CFG.
216///
217/// Example usage:
218///
219/// CFGBuilder builder;
220/// CFG* cfg = builder.BuildAST(stmt1);
221///
Mike Stump6d9828c2009-07-17 01:31:16 +0000222/// CFG construction is done via a recursive walk of an AST. We actually parse
223/// the AST in reverse order so that the successor of a basic block is
224/// constructed prior to its predecessor. This allows us to nicely capture
225/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000226///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000227class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000228 typedef BlockScopePosPair JumpTarget;
229 typedef BlockScopePosPair JumpSource;
230
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000231 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000232 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000233
Ted Kremenekfddd5182007-08-21 21:42:03 +0000234 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000235 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000236 JumpTarget ContinueJumpTarget;
237 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000238 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000239 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000240 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000241
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000242 // Current position in local scope.
243 LocalScope::const_iterator ScopePos;
244
245 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000246 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000247 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000248
249 // A list of blocks that end with a "goto" that must be backpatched to their
250 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000251 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000252 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000253
Ted Kremenek19bb3562007-08-28 19:26:49 +0000254 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000255 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenek19bb3562007-08-28 19:26:49 +0000256 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000257
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000258 bool badCFG;
259 CFG::BuildOptions BuildOpts;
260
Mike Stump6d9828c2009-07-17 01:31:16 +0000261public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000262 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
263 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000264 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000265 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000266
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000267 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000268 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000269 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000270
Ted Kremenek4f880632009-07-17 22:18:43 +0000271private:
272 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000273 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
274 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
275 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000276 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000277 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000278 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000279 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000280 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
281 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000282 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
283 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000284 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000285 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
286 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000287 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
288 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000289 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000290 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000291 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000292 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000293 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCall56ca35d2011-02-17 10:25:35 +0000294 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
295 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000296 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000297 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000298 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000299 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
300 CFGBlock *VisitDoStmt(DoStmt *D);
301 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000302 CFGBlock *VisitGotoStmt(GotoStmt* G);
303 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000304 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000305 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
306 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000307 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000308 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
309 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
310 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
311 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
312 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
313 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000314 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
315 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000316 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000317 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000318 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000319
Ted Kremenek852274d2009-12-16 03:18:58 +0000320 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
321 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000322 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000323
Marcin Swiderski8599e762010-11-03 06:19:35 +0000324 // Visitors to walk an AST and generate destructors of temporaries in
325 // full expression.
326 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
327 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
328 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
329 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
330 bool BindToTemporary);
John McCall56ca35d2011-02-17 10:25:35 +0000331 CFGBlock *
332 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
333 bool BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000334
Ted Kremenek274f4332008-04-28 18:00:46 +0000335 // NYS == Not Yet Supported
336 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000337 badCFG = true;
338 return Block;
339 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000340
Ted Kremenek4f880632009-07-17 22:18:43 +0000341 void autoCreateBlock() { if (!Block) Block = createBlock(); }
342 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000343
Zhongxing Xudf119892010-06-03 06:43:23 +0000344 CFGBlock *addStmt(Stmt *S) {
345 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000346 }
Sean Huntcbb67482011-01-08 20:30:50 +0000347 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000348 void addAutomaticObjDtors(LocalScope::const_iterator B,
349 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000350 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000351
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000352 // Local scopes creation.
353 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
354
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000355 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000356 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
357 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
358
359 void addLocalScopeAndDtors(Stmt* S);
360
361 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek892697d2010-12-16 07:46:53 +0000362 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek852274d2009-12-16 03:18:58 +0000363 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000364 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000365 }
Sean Huntcbb67482011-01-08 20:30:50 +0000366 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000367 B->appendInitializer(I, cfg->getBumpVectorContext());
368 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000369 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
370 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
371 }
372 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
373 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
374 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000375 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
376 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
377 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000378
Marcin Swiderski53de1342010-09-30 22:54:37 +0000379 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
380 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
381 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
382 LocalScope::const_iterator E, Stmt* S);
383 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
384 LocalScope::const_iterator B, LocalScope::const_iterator E);
385
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000386 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000387 B->addSuccessor(S, cfg->getBumpVectorContext());
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000390 /// TryResult - a class representing a variant over the values
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000391 /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000392 /// and is used by the CFGBuilder to decide if a branch condition
393 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000394 class TryResult {
395 int X;
396 public:
397 TryResult(bool b) : X(b ? 1 : 0) {}
398 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek941fde82009-07-24 04:47:11 +0000400 bool isTrue() const { return X == 1; }
401 bool isFalse() const { return X == 0; }
402 bool isKnown() const { return X >= 0; }
403 void negate() {
404 assert(isKnown());
405 X ^= 0x1;
406 }
407 };
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000409 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump00998a02009-07-23 23:25:26 +0000410 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000411 TryResult tryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000412 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000413 return TryResult();
414
Mike Stump00998a02009-07-23 23:25:26 +0000415 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000416 if (!S->isTypeDependent() && !S->isValueDependent() &&
Ted Kremenekc56c0042011-02-23 05:11:46 +0000417 S->Evaluate(Result, *Context)) {
418 if (Result.Val.isInt())
419 return Result.Val.getInt().getBoolValue();
420 if (Result.Val.isLValue()) {
421 Expr *e = Result.Val.getLValueBase();
422 const CharUnits &c = Result.Val.getLValueOffset();
423 if (!e && c.isZero())
424 return false;
425 }
426 }
Ted Kremenek941fde82009-07-24 04:47:11 +0000427 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000428 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000429};
Mike Stump6d9828c2009-07-17 01:31:16 +0000430
Douglas Gregor898574e2008-12-05 23:32:09 +0000431// FIXME: Add support for dependent-sized array types in C++?
432// Does it even make sense to build a CFG for an uninstantiated template?
John McCallf4c73712011-01-19 06:33:43 +0000433static const VariableArrayType *FindVA(const Type *t) {
434 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
435 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenek610a09e2008-09-26 22:58:57 +0000436 if (vat->getSizeExpr())
437 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000438
Ted Kremenek610a09e2008-09-26 22:58:57 +0000439 t = vt->getElementType().getTypePtr();
440 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000441
Ted Kremenek610a09e2008-09-26 22:58:57 +0000442 return 0;
443}
Mike Stump6d9828c2009-07-17 01:31:16 +0000444
445/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
446/// arbitrary statement. Examples include a single expression or a function
447/// body (compound statement). The ownership of the returned CFG is
448/// transferred to the caller. If CFG construction fails, this method returns
449/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000450CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000451 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000452
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000453 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000454 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000455 if (!Statement)
456 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000457
Ted Kremenek6c52c782010-09-14 23:41:16 +0000458 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000459
460 // Create an empty block that will serve as the exit block for the CFG. Since
461 // this is the first block added to the CFG, it will be implicitly registered
462 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000463 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000464 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000465 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000466
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000467 if (BuildOpts.AddImplicitDtors)
468 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
469 addImplicitDtorsForDestructor(DD);
470
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000471 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000472 CFGBlock *B = addStmt(Statement);
473
474 if (badCFG)
475 return NULL;
476
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000477 // For C++ constructor add initializers to CFG.
478 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
479 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
480 E = CD->init_rend(); I != E; ++I) {
481 B = addInitializer(*I);
482 if (badCFG)
483 return NULL;
484 }
485 }
486
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000487 if (B)
488 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000489
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000490 // Backpatch the gotos whose label -> block mappings we didn't know when we
491 // encountered them.
492 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
493 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000494
Ted Kremenek9ce52702011-01-07 19:37:16 +0000495 CFGBlock* B = I->block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000496 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
497 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000498
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000499 // If there is no target for the goto, then we are looking at an
500 // incomplete AST. Handle this by not registering a successor.
501 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000502
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000503 JumpTarget JT = LI->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +0000504 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
505 JT.scopePosition);
506 addSuccessor(B, JT.block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000507 }
508
509 // Add successors to the Indirect Goto Dispatch block (if we have one).
510 if (CFGBlock* B = cfg->getIndirectGotoBlock())
511 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
512 E = AddressTakenLabels.end(); I != E; ++I ) {
513
514 // Lookup the target block.
515 LabelMapTy::iterator LI = LabelMap.find(*I);
516
517 // If there is no target block that contains label, then we are looking
518 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000519 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000520
Ted Kremenek9ce52702011-01-07 19:37:16 +0000521 addSuccessor(B, LI->second.block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000522 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000523
Mike Stump6d9828c2009-07-17 01:31:16 +0000524 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000525 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000526
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000527 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000528}
Mike Stump6d9828c2009-07-17 01:31:16 +0000529
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000530/// createBlock - Used to lazily create blocks that are connected
531/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000532CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000533 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000534 if (add_successor && Succ)
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000535 addSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000536 return B;
537}
Mike Stump6d9828c2009-07-17 01:31:16 +0000538
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000539/// addInitializer - Add C++ base or member initializer element to CFG.
Sean Huntcbb67482011-01-08 20:30:50 +0000540CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000541 if (!BuildOpts.AddInitializers)
542 return Block;
543
Marcin Swiderski8599e762010-11-03 06:19:35 +0000544 bool IsReference = false;
545 bool HasTemporaries = false;
546
547 // Destructors of temporaries in initialization expression should be called
548 // after initialization finishes.
549 Expr *Init = I->getInit();
550 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000551 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000552 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000553 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000554
555 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
556 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000557 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000558 IsReference);
559 }
560 }
561
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000562 autoCreateBlock();
563 appendInitializer(Block, I);
564
Marcin Swiderski8599e762010-11-03 06:19:35 +0000565 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000566 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000567 // For expression with temporaries go directly to subexpression to omit
568 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000569 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
570 }
571 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000572 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000573
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000574 return Block;
575}
576
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000577/// addAutomaticObjDtors - Add to current block automatic objects destructors
578/// for objects in range of local scope positions. Use S as trigger statement
579/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000580void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
581 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000582 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000583 return;
584
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000585 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000586 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000587
588 autoCreateBlock();
589 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000590}
591
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000592/// addImplicitDtorsForDestructor - Add implicit destructors generated for
593/// base and member objects in destructor.
594void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
595 assert (BuildOpts.AddImplicitDtors
596 && "Can be called only when dtors should be added");
597 const CXXRecordDecl *RD = DD->getParent();
598
599 // At the end destroy virtual base objects.
600 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
601 VE = RD->vbases_end(); VI != VE; ++VI) {
602 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
603 if (!CD->hasTrivialDestructor()) {
604 autoCreateBlock();
605 appendBaseDtor(Block, VI);
606 }
607 }
608
609 // Before virtual bases destroy direct base objects.
610 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
611 BE = RD->bases_end(); BI != BE; ++BI) {
612 if (!BI->isVirtual()) {
613 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
614 if (!CD->hasTrivialDestructor()) {
615 autoCreateBlock();
616 appendBaseDtor(Block, BI);
617 }
618 }
619 }
620
621 // First destroy member objects.
622 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
623 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000624 // Check for constant size array. Set type to array element type.
625 QualType QT = FI->getType();
626 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
627 if (AT->getSize() == 0)
628 continue;
629 QT = AT->getElementType();
630 }
631
632 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000633 if (!CD->hasTrivialDestructor()) {
634 autoCreateBlock();
635 appendMemberDtor(Block, *FI);
636 }
637 }
638}
639
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000640/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
641/// way return valid LocalScope object.
642LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
643 if (!Scope) {
Ted Kremenekfe59b742011-02-15 02:47:45 +0000644 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
645 Scope = alloc.Allocate<LocalScope>();
646 BumpVectorContext ctx(alloc);
647 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000648 }
649 return Scope;
650}
651
652/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000653/// that should create implicit scope (e.g. if/else substatements).
654void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000655 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000656 return;
657
658 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000659
660 // For compound statement we will be creating explicit scope.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000661 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000662 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
663 ; BI != BE; ++BI) {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000664 Stmt *SI = *BI;
665 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000666 SI = LS->getSubStmt();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000667 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000668 Scope = addLocalScopeForDeclStmt(DS, Scope);
669 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000670 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000671 }
672
673 // For any other statement scope will be implicit and as such will be
674 // interesting only for DeclStmt.
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000675 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000676 S = LS->getSubStmt();
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000677 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000678 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000679}
680
681/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
682/// reuse Scope if not NULL.
683LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000684 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000685 if (!BuildOpts.AddImplicitDtors)
686 return Scope;
687
688 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
689 ; DI != DE; ++DI) {
690 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
691 Scope = addLocalScopeForVarDecl(VD, Scope);
692 }
693 return Scope;
694}
695
696/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
697/// create add scope for automatic objects and temporary objects bound to
698/// const reference. Will reuse Scope if not NULL.
699LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000700 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000701 if (!BuildOpts.AddImplicitDtors)
702 return Scope;
703
704 // Check if variable is local.
705 switch (VD->getStorageClass()) {
706 case SC_None:
707 case SC_Auto:
708 case SC_Register:
709 break;
710 default: return Scope;
711 }
712
713 // Check for const references bound to temporary. Set type to pointee.
714 QualType QT = VD->getType();
715 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
716 QT = RT->getPointeeType();
717 if (!QT.isConstQualified())
718 return Scope;
719 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
720 return Scope;
721 }
722
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000723 // Check for constant size array. Set type to array element type.
724 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
725 if (AT->getSize() == 0)
726 return Scope;
727 QT = AT->getElementType();
728 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000729
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000730 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000731 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
732 if (!CD->hasTrivialDestructor()) {
733 // Add the variable to scope
734 Scope = createOrReuseLocalScope(Scope);
735 Scope->addVar(VD);
736 ScopePos = Scope->begin();
737 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000738 return Scope;
739}
740
741/// addLocalScopeAndDtors - For given statement add local scope for it and
742/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
743void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
744 if (!BuildOpts.AddImplicitDtors)
745 return;
746
747 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000748 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000749 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
750}
751
Marcin Swiderski53de1342010-09-30 22:54:37 +0000752/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
753/// automatic storage duration to CFGBlock's elements vector. Insertion will be
754/// performed in place specified with iterator.
755void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
756 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
757 BumpVectorContext& C = cfg->getBumpVectorContext();
758 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
759 while (B != E)
760 I = Blk->insertAutomaticObjDtor(I, *B++, S);
761}
762
763/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
764/// automatic storage duration to CFGBlock's elements vector. Elements will be
765/// appended to physical end of the vector which happens to be logical
766/// beginning.
767void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
768 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
769 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
770}
771
772/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
773/// variables with automatic storage duration to CFGBlock's elements vector.
774/// Elements will be prepended to physical beginning of the vector which
775/// happens to be logical end. Use blocks terminator as statement that specifies
776/// destructors call site.
777void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
778 LocalScope::const_iterator B, LocalScope::const_iterator E) {
779 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
780}
781
Ted Kremenek4f880632009-07-17 22:18:43 +0000782/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000783/// blocks for ternary operators, &&, and ||. We also process "," and
784/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000785CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000786tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000787 if (!S) {
788 badCFG = true;
789 return 0;
790 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000791 switch (S->getStmtClass()) {
792 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000793 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000794
795 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000796 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000797
John McCall56ca35d2011-02-17 10:25:35 +0000798 case Stmt::BinaryConditionalOperatorClass:
799 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
800
Ted Kremenek4f880632009-07-17 22:18:43 +0000801 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000802 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek4f880632009-07-17 22:18:43 +0000804 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000805 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000806
Ted Kremenek4f880632009-07-17 22:18:43 +0000807 case Stmt::BreakStmtClass:
808 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek4f880632009-07-17 22:18:43 +0000810 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000811 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000812 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 case Stmt::CaseStmtClass:
815 return VisitCaseStmt(cast<CaseStmt>(S));
816
817 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000818 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek4f880632009-07-17 22:18:43 +0000820 case Stmt::CompoundStmtClass:
821 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000824 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenek4f880632009-07-17 22:18:43 +0000826 case Stmt::ContinueStmtClass:
827 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek021c8af2010-01-19 20:40:33 +0000829 case Stmt::CXXCatchStmtClass:
830 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
831
John McCall4765fa02010-12-06 08:20:24 +0000832 case Stmt::ExprWithCleanupsClass:
833 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000834
Zhongxing Xua725ed42010-11-01 13:04:58 +0000835 case Stmt::CXXBindTemporaryExprClass:
836 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
837
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000838 case Stmt::CXXConstructExprClass:
839 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
840
Zhongxing Xua725ed42010-11-01 13:04:58 +0000841 case Stmt::CXXFunctionalCastExprClass:
842 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
843
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000844 case Stmt::CXXTemporaryObjectExprClass:
845 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
846
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000847 case Stmt::CXXMemberCallExprClass:
848 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
849
Ted Kremenek021c8af2010-01-19 20:40:33 +0000850 case Stmt::CXXThrowExprClass:
851 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000852
Ted Kremenek021c8af2010-01-19 20:40:33 +0000853 case Stmt::CXXTryStmtClass:
854 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000855
Ted Kremenek4f880632009-07-17 22:18:43 +0000856 case Stmt::DeclStmtClass:
857 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Ted Kremenek4f880632009-07-17 22:18:43 +0000859 case Stmt::DefaultStmtClass:
860 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenek4f880632009-07-17 22:18:43 +0000862 case Stmt::DoStmtClass:
863 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Ted Kremenek4f880632009-07-17 22:18:43 +0000865 case Stmt::ForStmtClass:
866 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek4f880632009-07-17 22:18:43 +0000868 case Stmt::GotoStmtClass:
869 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek4f880632009-07-17 22:18:43 +0000871 case Stmt::IfStmtClass:
872 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Ted Kremenek892697d2010-12-16 07:46:53 +0000874 case Stmt::ImplicitCastExprClass:
875 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000876
Ted Kremenek4f880632009-07-17 22:18:43 +0000877 case Stmt::IndirectGotoStmtClass:
878 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek4f880632009-07-17 22:18:43 +0000880 case Stmt::LabelStmtClass:
881 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek115c1b92010-04-11 17:02:10 +0000883 case Stmt::MemberExprClass:
884 return VisitMemberExpr(cast<MemberExpr>(S), asc);
885
Ted Kremenek4f880632009-07-17 22:18:43 +0000886 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000887 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
888
Ted Kremenek4f880632009-07-17 22:18:43 +0000889 case Stmt::ObjCAtSynchronizedStmtClass:
890 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Ted Kremenek4f880632009-07-17 22:18:43 +0000892 case Stmt::ObjCAtThrowStmtClass:
893 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Ted Kremenek4f880632009-07-17 22:18:43 +0000895 case Stmt::ObjCAtTryStmtClass:
896 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Ted Kremenek4f880632009-07-17 22:18:43 +0000898 case Stmt::ObjCForCollectionStmtClass:
899 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremenek4f880632009-07-17 22:18:43 +0000901 case Stmt::ParenExprClass:
902 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000903 goto tryAgain;
904
Ted Kremenek4f880632009-07-17 22:18:43 +0000905 case Stmt::NullStmtClass:
906 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Ted Kremenek4f880632009-07-17 22:18:43 +0000908 case Stmt::ReturnStmtClass:
909 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek4f880632009-07-17 22:18:43 +0000911 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000912 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Ted Kremenek4f880632009-07-17 22:18:43 +0000914 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000915 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek4f880632009-07-17 22:18:43 +0000917 case Stmt::SwitchStmtClass:
918 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000920 case Stmt::UnaryOperatorClass:
921 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
922
Ted Kremenek4f880632009-07-17 22:18:43 +0000923 case Stmt::WhileStmtClass:
924 return VisitWhileStmt(cast<WhileStmt>(S));
925 }
926}
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek852274d2009-12-16 03:18:58 +0000928CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
929 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000930 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000931 appendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek4f880632009-07-17 22:18:43 +0000934 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000935}
Mike Stump6d9828c2009-07-17 01:31:16 +0000936
Ted Kremenek4f880632009-07-17 22:18:43 +0000937/// VisitChildren - Visit the children of a Stmt.
938CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek6b12da92011-02-21 22:11:26 +0000939 CFGBlock *lastBlock = Block;
940 for (Stmt::child_range I = Terminator->children(); I; ++I)
941 if (Stmt *child = *I)
942 if (CFGBlock *b = Visit(child))
943 lastBlock = b;
944
945 return lastBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000946}
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Ted Kremenek852274d2009-12-16 03:18:58 +0000948CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
949 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000950 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000951
Ted Kremenek852274d2009-12-16 03:18:58 +0000952 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000953 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000954 appendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000955 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000956
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000957 return Block;
958}
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000960CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +0000961 AddStmtChoice asc) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000962 if (asc.alwaysAdd()) {
963 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000964 appendStmt(Block, U, asc);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000965 }
966
Ted Kremenek892697d2010-12-16 07:46:53 +0000967 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000968}
969
Ted Kremenek852274d2009-12-16 03:18:58 +0000970CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
971 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000972 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000973 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000974 appendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000976 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000977 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek4f880632009-07-17 22:18:43 +0000979 // create the block evaluating the LHS
980 CFGBlock* LHSBlock = createBlock(false);
981 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek4f880632009-07-17 22:18:43 +0000983 // create the block evaluating the RHS
984 Succ = ConfluenceBlock;
985 Block = NULL;
986 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000987
988 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000989 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000990 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +0000991 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +0000992 // Create an empty block for cases where the RHS doesn't require
993 // any explicit statements in the CFG.
994 RHSBlock = createBlock();
995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Mike Stump00998a02009-07-23 23:25:26 +0000997 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +0000998 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000999 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +00001000 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +00001001
Ted Kremenek4f880632009-07-17 22:18:43 +00001002 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +00001003 if (B->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001004 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1005 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001006 } else {
John McCall2de56d12010-08-25 11:45:40 +00001007 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001008 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1009 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenek4f880632009-07-17 22:18:43 +00001012 // Generate the blocks for evaluating the LHS.
1013 Block = LHSBlock;
1014 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +00001015 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001016
1017 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001018 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001019 appendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001020 addStmt(B->getRHS());
1021 return addStmt(B->getLHS());
1022 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001023
1024 if (B->isAssignmentOp()) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001025 if (asc.alwaysAdd()) {
1026 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001027 appendStmt(Block, B, asc);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001028 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001029 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001030 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Marcin Swiderskie1667192010-10-24 08:21:40 +00001033 if (asc.alwaysAdd()) {
1034 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001035 appendStmt(Block, B, asc);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001036 }
1037
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001038 CFGBlock *RBlock = Visit(B->getRHS());
1039 CFGBlock *LBlock = Visit(B->getLHS());
1040 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1041 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1042 // return RBlock. Otherwise we'll incorrectly return NULL.
1043 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001044}
1045
Ted Kremenek852274d2009-12-16 03:18:58 +00001046CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1047 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001048 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001049 appendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001050 }
1051 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001052}
1053
Ted Kremenek4f880632009-07-17 22:18:43 +00001054CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1055 // "break" is a control-flow statement. Thus we stop processing the current
1056 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001057 if (badCFG)
1058 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Ted Kremenek4f880632009-07-17 22:18:43 +00001060 // Now create a new block that ends with the break statement.
1061 Block = createBlock(false);
1062 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek4f880632009-07-17 22:18:43 +00001064 // If there is no target for the break, then we are looking at an incomplete
1065 // AST. This means that the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001066 if (BreakJumpTarget.block) {
1067 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1068 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001069 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001070 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001071
1072
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001073 return Block;
1074}
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Mike Stump4c45aa12010-01-21 15:20:48 +00001076static bool CanThrow(Expr *E) {
1077 QualType Ty = E->getType();
1078 if (Ty->isFunctionPointerType())
1079 Ty = Ty->getAs<PointerType>()->getPointeeType();
1080 else if (Ty->isBlockPointerType())
1081 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001082
Mike Stump4c45aa12010-01-21 15:20:48 +00001083 const FunctionType *FT = Ty->getAs<FunctionType>();
1084 if (FT) {
1085 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1086 if (Proto->hasEmptyExceptionSpec())
1087 return false;
1088 }
1089 return true;
1090}
1091
Ted Kremenek852274d2009-12-16 03:18:58 +00001092CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001093 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001094 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001095 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001096 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001097 }
Mike Stump24556362009-07-25 21:26:53 +00001098
Mike Stump4c45aa12010-01-21 15:20:48 +00001099 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001100
1101 // Languages without exceptions are assumed to not throw.
Anders Carlssonc1cfdf82011-02-20 00:20:27 +00001102 if (Context->getLangOptions().areExceptionsEnabled()) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001103 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001104 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001105 }
1106
1107 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001108 if (FD->hasAttr<NoReturnAttr>())
1109 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001110 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001111 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001112 }
Mike Stump24556362009-07-25 21:26:53 +00001113
Mike Stump4c45aa12010-01-21 15:20:48 +00001114 if (!CanThrow(C->getCallee()))
1115 AddEHEdge = false;
1116
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001117 if (!NoReturn && !AddEHEdge)
1118 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Mike Stump079bd722010-01-19 22:00:14 +00001120 if (Block) {
1121 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001122 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001123 return 0;
1124 }
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Mike Stump079bd722010-01-19 22:00:14 +00001126 Block = createBlock(!NoReturn);
Ted Kremenek892697d2010-12-16 07:46:53 +00001127 appendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001128
Mike Stump079bd722010-01-19 22:00:14 +00001129 if (NoReturn) {
1130 // Wire this to the exit block directly.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001131 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001132 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001133 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001134 // Add exceptional edges.
1135 if (TryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001136 addSuccessor(Block, TryTerminatedBlock);
Mike Stump079bd722010-01-19 22:00:14 +00001137 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001138 addSuccessor(Block, &cfg->getExit());
Mike Stump079bd722010-01-19 22:00:14 +00001139 }
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Mike Stump24556362009-07-25 21:26:53 +00001141 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001142}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001143
Ted Kremenek852274d2009-12-16 03:18:58 +00001144CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1145 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001146 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001147 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001148 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001149 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001151 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001152 Succ = ConfluenceBlock;
1153 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001154 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001155 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001156 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001158 Succ = ConfluenceBlock;
1159 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001160 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001161 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001162 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001164 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001165 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001166 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1167 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1168 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001169 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001170 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001171}
Mike Stump1eb44332009-09-09 15:08:12 +00001172
1173
1174CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001175 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001176 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001177
1178 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1179 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001180 // If we hit a segment of code just containing ';' (NullStmts), we can
1181 // get a null block back. In such cases, just use the LastBlock
1182 if (CFGBlock *newBlock = addStmt(*I))
1183 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001185 if (badCFG)
1186 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001187 }
Mike Stump079bd722010-01-19 22:00:14 +00001188
Ted Kremenek4f880632009-07-17 22:18:43 +00001189 return LastBlock;
1190}
Mike Stump1eb44332009-09-09 15:08:12 +00001191
John McCall56ca35d2011-02-17 10:25:35 +00001192CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek852274d2009-12-16 03:18:58 +00001193 AddStmtChoice asc) {
John McCall56ca35d2011-02-17 10:25:35 +00001194 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1195 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1196
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001197 // Create the confluence block that will "merge" the results of the ternary
1198 // expression.
1199 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001200 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001201 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001202 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001204 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001205
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001206 // Create a block for the LHS expression if there is an LHS expression. A
1207 // GCC extension allows LHS to be NULL, causing the condition to be the
1208 // value that is returned instead.
1209 // e.g: x ?: y is shorthand for: x ? x : y;
1210 Succ = ConfluenceBlock;
1211 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00001212 CFGBlock* LHSBlock = 0;
1213 const Expr *trueExpr = C->getTrueExpr();
1214 if (trueExpr != opaqueValue) {
1215 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001216 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001217 return 0;
1218 Block = NULL;
1219 }
Ted Kremenekf226d182011-02-24 03:09:15 +00001220 else
1221 LHSBlock = ConfluenceBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001223 // Create the block for the RHS expression.
1224 Succ = ConfluenceBlock;
John McCall56ca35d2011-02-17 10:25:35 +00001225 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001226 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001227 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001229 // Create the block that will contain the condition.
1230 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Mike Stump00998a02009-07-23 23:25:26 +00001232 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001233 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Ted Kremenekf226d182011-02-24 03:09:15 +00001234 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001235 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001236 Block->setTerminator(C);
John McCall56ca35d2011-02-17 10:25:35 +00001237 Expr *condExpr = C->getCond();
John McCalld40baf62011-02-19 03:13:26 +00001238
Ted Kremenekf226d182011-02-24 03:09:15 +00001239 if (opaqueValue) {
1240 // Run the condition expression if it's not trivially expressed in
1241 // terms of the opaque value (or if there is no opaque value).
1242 if (condExpr != opaqueValue)
1243 addStmt(condExpr);
John McCalld40baf62011-02-19 03:13:26 +00001244
Ted Kremenekf226d182011-02-24 03:09:15 +00001245 // Before that, run the common subexpression if there was one.
1246 // At least one of this or the above will be run.
1247 return addStmt(BCO->getCommon());
1248 }
1249
1250 return addStmt(condExpr);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001251}
1252
Ted Kremenek4f880632009-07-17 22:18:43 +00001253CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001254 if (DS->isSingleDecl())
1255 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek4f880632009-07-17 22:18:43 +00001257 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek4f880632009-07-17 22:18:43 +00001259 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1260 typedef llvm::SmallVector<Decl*,10> BufTy;
1261 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenek4f880632009-07-17 22:18:43 +00001263 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1264 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1265 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1266 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek4f880632009-07-17 22:18:43 +00001268 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1269 // automatically freed with the CFG.
1270 DeclGroupRef DG(*I);
1271 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001272 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001273 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Ted Kremenek4f880632009-07-17 22:18:43 +00001275 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001276 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001277 }
Mike Stump1eb44332009-09-09 15:08:12 +00001278
1279 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001280}
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenek4f880632009-07-17 22:18:43 +00001282/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001283/// DeclStmts and initializers in them.
1284CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1285 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001286
Marcin Swiderski8599e762010-11-03 06:19:35 +00001287 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Marcin Swiderski8599e762010-11-03 06:19:35 +00001289 if (!VD) {
1290 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001291 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001292 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Marcin Swiderski8599e762010-11-03 06:19:35 +00001295 bool IsReference = false;
1296 bool HasTemporaries = false;
1297
1298 // Destructors of temporaries in initialization expression should be called
1299 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001300 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001301 if (Init) {
1302 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001303 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001304
1305 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1306 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001307 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001308 IsReference);
1309 }
1310 }
1311
1312 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001313 appendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Ted Kremenek4f880632009-07-17 22:18:43 +00001315 if (Init) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001316 if (HasTemporaries)
1317 // For expression with temporaries go directly to subexpression to omit
1318 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +00001319 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski8599e762010-11-03 06:19:35 +00001320 else
Ted Kremenek892697d2010-12-16 07:46:53 +00001321 Visit(Init);
Ted Kremenek4f880632009-07-17 22:18:43 +00001322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Ted Kremenek4f880632009-07-17 22:18:43 +00001324 // If the type of VD is a VLA, then we must process its size expressions.
John McCallf4c73712011-01-19 06:33:43 +00001325 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1326 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek4f880632009-07-17 22:18:43 +00001327 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001329 // Remove variable from local scope.
1330 if (ScopePos && VD == *ScopePos)
1331 ++ScopePos;
1332
Ted Kremenek4f880632009-07-17 22:18:43 +00001333 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001334}
1335
1336CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001337 // We may see an if statement in the middle of a basic block, or it may be the
1338 // first statement we are processing. In either case, we create a new basic
1339 // block. First, we create the blocks for the then...else statements, and
1340 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001341 // middle of a block, we stop processing that block. That block is then the
1342 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001343
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001344 // Save local scope position because in case of condition variable ScopePos
1345 // won't be restored when traversing AST.
1346 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1347
1348 // Create local scope for possible condition variable.
1349 // Store scope position. Add implicit destructor.
1350 if (VarDecl* VD = I->getConditionVariable()) {
1351 LocalScope::const_iterator BeginScopePos = ScopePos;
1352 addLocalScopeForVarDecl(VD);
1353 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1354 }
1355
Mike Stump6d9828c2009-07-17 01:31:16 +00001356 // The block we were proccessing is now finished. Make it the successor
1357 // block.
1358 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001359 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001360 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001361 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001362 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001363
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001364 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001365 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001366
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001367 if (Stmt* Else = I->getElse()) {
1368 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001369
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001370 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001371 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001372 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001373
1374 // If branch is not a compound statement create implicit scope
1375 // and add destructors.
1376 if (!isa<CompoundStmt>(Else))
1377 addLocalScopeAndDtors(Else);
1378
Ted Kremenek4f880632009-07-17 22:18:43 +00001379 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001380
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001381 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1382 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001383 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001384 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001385 return 0;
1386 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001387 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001388
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001389 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001390 CFGBlock* ThenBlock;
1391 {
1392 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001393 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001394 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001395 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001396
1397 // If branch is not a compound statement create implicit scope
1398 // and add destructors.
1399 if (!isa<CompoundStmt>(Then))
1400 addLocalScopeAndDtors(Then);
1401
Ted Kremenek4f880632009-07-17 22:18:43 +00001402 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001403
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001404 if (!ThenBlock) {
1405 // We can reach here if the "then" body has all NullStmts.
1406 // Create an empty block so we can distinguish between true and false
1407 // branches in path-sensitive analyses.
1408 ThenBlock = createBlock(false);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001409 addSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001410 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001411 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001412 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001413 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001414 }
1415
Mike Stump6d9828c2009-07-17 01:31:16 +00001416 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001417 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001418
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419 // Set the terminator of the new block to the If statement.
1420 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001421
Mike Stump00998a02009-07-23 23:25:26 +00001422 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001423 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001424
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001425 // Now add the successors.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001426 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1427 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001428
1429 // Add the condition as the last statement in the new block. This may create
1430 // new blocks as the condition may contain control-flow. Any newly created
1431 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001432 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001433
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001434 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1435 // and the condition variable initialization to the CFG.
1436 if (VarDecl *VD = I->getConditionVariable()) {
1437 if (Expr *Init = VD->getInit()) {
1438 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001439 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001440 addStmt(Init);
1441 }
1442 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001443
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001444 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001445}
Mike Stump6d9828c2009-07-17 01:31:16 +00001446
1447
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001448CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001449 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001450 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001451 // NOTE: If a "return" appears in the middle of a block, this means that the
1452 // code afterwards is DEAD (unreachable). We still keep a basic block
1453 // for that code; a simple "mark-and-sweep" from the entry block will be
1454 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001455
1456 // Create the new block.
1457 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001458
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001459 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001460 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001461 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001462
1463 // Add the return statement to the block. This may create new blocks if R
1464 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001465 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001466}
1467
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001468CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001469 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001470 addStmt(L->getSubStmt());
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001471 CFGBlock *LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
Ted Kremenek4f880632009-07-17 22:18:43 +00001473 if (!LabelBlock) // This can happen when the body is empty, i.e.
1474 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001476 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1477 "label already in map");
1478 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001479
1480 // Labels partition blocks, so this is the end of the basic block we were
1481 // processing (L is the block's label). Because this is label (and we have
1482 // already processed the substatement) there is no extra control-flow to worry
1483 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001484 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001485 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001486 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001487
1488 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001490
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001491 // This block is now the implicit successor of other blocks.
1492 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001493
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001494 return LabelBlock;
1495}
1496
1497CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001498 // Goto is a control-flow statement. Thus we stop processing the current
1499 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001500
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001501 Block = createBlock(false);
1502 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001503
1504 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001505 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001506
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001507 if (I == LabelMap.end())
1508 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001509 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1510 else {
1511 JumpTarget JT = I->second;
Ted Kremenek9ce52702011-01-07 19:37:16 +00001512 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1513 addSuccessor(Block, JT.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001514 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001515
Mike Stump6d9828c2009-07-17 01:31:16 +00001516 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001517}
1518
1519CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001520 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001521
Marcin Swiderski47575f12010-10-01 01:38:14 +00001522 // Save local scope position because in case of condition variable ScopePos
1523 // won't be restored when traversing AST.
1524 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1525
1526 // Create local scope for init statement and possible condition variable.
1527 // Add destructor for init statement and condition variable.
1528 // Store scope position for continue statement.
1529 if (Stmt* Init = F->getInit())
1530 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001531 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1532
Marcin Swiderski47575f12010-10-01 01:38:14 +00001533 if (VarDecl* VD = F->getConditionVariable())
1534 addLocalScopeForVarDecl(VD);
1535 LocalScope::const_iterator ContinueScopePos = ScopePos;
1536
1537 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1538
Mike Stumpfefb9f72009-07-21 01:12:51 +00001539 // "for" is a control-flow statement. Thus we stop processing the current
1540 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001541 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001542 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001543 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001544 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001545 } else
1546 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001547
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001548 // Save the current value for the break targets.
1549 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001550 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001551 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001552
Mike Stump6d9828c2009-07-17 01:31:16 +00001553 // Because of short-circuit evaluation, the condition of the loop can span
1554 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1555 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001556 CFGBlock* ExitConditionBlock = createBlock(false);
1557 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001558
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001559 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001560 ExitConditionBlock->setTerminator(F);
1561
1562 // Now add the actual condition to the condition block. Because the condition
1563 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001564 if (Stmt* C = F->getCond()) {
1565 Block = ExitConditionBlock;
1566 EntryConditionBlock = addStmt(C);
Ted Kremenek9ce52702011-01-07 19:37:16 +00001567 if (badCFG)
1568 return 0;
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001569 assert(Block == EntryConditionBlock ||
1570 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001571
1572 // If this block contains a condition variable, add both the condition
1573 // variable and initializer to the CFG.
1574 if (VarDecl *VD = F->getConditionVariable()) {
1575 if (Expr *Init = VD->getInit()) {
1576 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001577 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001578 EntryConditionBlock = addStmt(Init);
1579 assert(Block == EntryConditionBlock);
1580 }
1581 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001582
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001583 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001584 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001585 return 0;
1586 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001587 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001588
Mike Stump6d9828c2009-07-17 01:31:16 +00001589 // The condition block is the implicit successor for the loop body as well as
1590 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001591 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001592
Mike Stump00998a02009-07-23 23:25:26 +00001593 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001594 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Mike Stump00998a02009-07-23 23:25:26 +00001596 if (F->getCond())
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001597 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001598
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001599 // Now create the loop body.
1600 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001601 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001602
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001603 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001604 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1605 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001606
Ted Kremenekaf603f72007-08-30 18:39:40 +00001607 // Create a new block to contain the (bottom) of the loop body.
1608 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001609
1610 // Loop body should end with destructor of Condition variable (if any).
1611 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001612
Ted Kremeneke9334502008-09-04 21:48:47 +00001613 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001614 // Generate increment code in its own basic block. This is the target of
1615 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001616 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001617 } else {
1618 // No increment code. Create a special, empty, block that is used as the
1619 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001620 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001621 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001622 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001623
Ted Kremenek3575f842009-04-28 00:51:56 +00001624 // Finish up the increment (or empty) block if it hasn't been already.
1625 if (Block) {
1626 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001627 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001628 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001629 Block = 0;
1630 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001631
Marcin Swiderski47575f12010-10-01 01:38:14 +00001632 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001633
Ted Kremenek3575f842009-04-28 00:51:56 +00001634 // The starting block for the loop increment is the block that should
1635 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenek9ce52702011-01-07 19:37:16 +00001636 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001637
Marcin Swiderski47575f12010-10-01 01:38:14 +00001638 // If body is not a compound statement create implicit scope
1639 // and add destructors.
1640 if (!isa<CompoundStmt>(F->getBody()))
1641 addLocalScopeAndDtors(F->getBody());
1642
Mike Stump6d9828c2009-07-17 01:31:16 +00001643 // Now populate the body block, and in the process create new blocks as we
1644 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001645 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001646
1647 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001648 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001649 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001650 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Ted Kremenek941fde82009-07-24 04:47:11 +00001652 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001653 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001654 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001655
Ted Kremenek941fde82009-07-24 04:47:11 +00001656 // Link up the condition block with the code that follows the loop. (the
1657 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001658 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001659
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001660 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001661 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001662 if (Stmt* I = F->getInit()) {
1663 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001664 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001665 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001666
1667 // There is no loop initialization. We are thus basically a while loop.
1668 // NULL out Block to force lazy block construction.
1669 Block = NULL;
1670 Succ = EntryConditionBlock;
1671 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001672}
1673
Ted Kremenek115c1b92010-04-11 17:02:10 +00001674CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1675 if (asc.alwaysAdd()) {
1676 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001677 appendStmt(Block, M, asc);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001678 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001679 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001680}
1681
Ted Kremenek514de5a2008-11-11 17:10:00 +00001682CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1683 // Objective-C fast enumeration 'for' statements:
1684 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1685 //
1686 // for ( Type newVariable in collection_expression ) { statements }
1687 //
1688 // becomes:
1689 //
1690 // prologue:
1691 // 1. collection_expression
1692 // T. jump to loop_entry
1693 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001694 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001695 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1696 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1697 // TB:
1698 // statements
1699 // T. jump to loop_entry
1700 // FB:
1701 // what comes after
1702 //
1703 // and
1704 //
1705 // Type existingItem;
1706 // for ( existingItem in expression ) { statements }
1707 //
1708 // becomes:
1709 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001710 // the same with newVariable replaced with existingItem; the binding works
1711 // the same except that for one ObjCForCollectionStmt::getElement() returns
1712 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001713 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001714
Ted Kremenek514de5a2008-11-11 17:10:00 +00001715 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001716
Ted Kremenek514de5a2008-11-11 17:10:00 +00001717 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001718 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001719 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001720 LoopSuccessor = Block;
1721 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001722 } else
1723 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001724
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001725 // Build the condition blocks.
1726 CFGBlock* ExitConditionBlock = createBlock(false);
1727 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001729 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001730 ExitConditionBlock->setTerminator(S);
1731
1732 // The last statement in the block should be the ObjCForCollectionStmt, which
1733 // performs the actual binding to 'element' and determines if there are any
1734 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001735 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001736 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001737
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001738 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001739 // generate new blocks as necesary. We DON'T add the statement by default to
1740 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001741 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001742 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001743 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001744 return 0;
1745 Block = 0;
1746 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001747
1748 // The condition block is the implicit successor for the loop body as well as
1749 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001750 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001751
Ted Kremenek514de5a2008-11-11 17:10:00 +00001752 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001753 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001754 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001755 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1756 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1757 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001758
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001759 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1760 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001761
Ted Kremenek4f880632009-07-17 22:18:43 +00001762 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001763
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001764 if (!BodyBlock)
1765 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001766 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001767 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001768 return 0;
1769 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001770
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001771 // This new body block is a successor to our "exit" condition block.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001772 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001773 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001774
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001775 // Link up the condition block with the code that follows the loop.
1776 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001777 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001778
Ted Kremenek514de5a2008-11-11 17:10:00 +00001779 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001780 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001781 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001782}
1783
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001784CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1785 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001786
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001787 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001788 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001789
Ted Kremenekda5348e2009-05-05 23:11:51 +00001790 // The sync body starts its own basic block. This makes it a little easier
1791 // for diagnostic clients.
1792 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001793 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001794 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001795
Ted Kremenekda5348e2009-05-05 23:11:51 +00001796 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001797 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001798 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001799
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001800 // Add the @synchronized to the CFG.
1801 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001802 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001803
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001804 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001805 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001806}
Mike Stump6d9828c2009-07-17 01:31:16 +00001807
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001808CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001809 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001810 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001811}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001812
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001813CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001814 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001816 // Save local scope position because in case of condition variable ScopePos
1817 // won't be restored when traversing AST.
1818 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1819
1820 // Create local scope for possible condition variable.
1821 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001822 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001823 if (VarDecl* VD = W->getConditionVariable()) {
1824 addLocalScopeForVarDecl(VD);
1825 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1826 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001827
Mike Stumpfefb9f72009-07-21 01:12:51 +00001828 // "while" is a control-flow statement. Thus we stop processing the current
1829 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001830 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001831 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001832 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001833 LoopSuccessor = Block;
Ted Kremenek6b12da92011-02-21 22:11:26 +00001834 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001835 } else
1836 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001837
1838 // Because of short-circuit evaluation, the condition of the loop can span
1839 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1840 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001841 CFGBlock* ExitConditionBlock = createBlock(false);
1842 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001843
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001844 // Set the terminator for the "exit" condition block.
1845 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001846
1847 // Now add the actual condition to the condition block. Because the condition
1848 // itself may contain control-flow, new blocks may be created. Thus we update
1849 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001850 if (Stmt* C = W->getCond()) {
1851 Block = ExitConditionBlock;
1852 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001853 // The condition might finish the current 'Block'.
1854 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001855
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001856 // If this block contains a condition variable, add both the condition
1857 // variable and initializer to the CFG.
1858 if (VarDecl *VD = W->getConditionVariable()) {
1859 if (Expr *Init = VD->getInit()) {
1860 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001861 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001862 EntryConditionBlock = addStmt(Init);
1863 assert(Block == EntryConditionBlock);
1864 }
1865 }
1866
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001867 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001868 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001869 return 0;
1870 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001871 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001872
1873 // The condition block is the implicit successor for the loop body as well as
1874 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001875 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Mike Stump00998a02009-07-23 23:25:26 +00001877 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001878 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001879
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001880 // Process the loop body.
1881 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001882 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001883
1884 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001885 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1886 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1887 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001888
Mike Stump6d9828c2009-07-17 01:31:16 +00001889 // Create an empty block to represent the transition block for looping back
1890 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001891 Block = 0;
1892 assert(Succ == EntryConditionBlock);
1893 Succ = createBlock();
1894 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001895 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001896
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001897 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001898 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001899
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001900 // NULL out Block to force lazy instantiation of blocks for the body.
1901 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001903 // Loop body should end with destructor of Condition variable (if any).
1904 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1905
1906 // If body is not a compound statement create implicit scope
1907 // and add destructors.
1908 if (!isa<CompoundStmt>(W->getBody()))
1909 addLocalScopeAndDtors(W->getBody());
1910
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001911 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001912 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001913
Ted Kremenekaf603f72007-08-30 18:39:40 +00001914 if (!BodyBlock)
Ted Kremenek9ce52702011-01-07 19:37:16 +00001915 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001916 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001917 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001918 return 0;
1919 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001920
Ted Kremenek941fde82009-07-24 04:47:11 +00001921 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001922 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001923 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001924
Ted Kremenek941fde82009-07-24 04:47:11 +00001925 // Link up the condition block with the code that follows the loop. (the
1926 // false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001927 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001928
1929 // There can be no more statements in the condition block since we loop back
1930 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001931 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001932
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001933 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001934 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001935 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001936}
Mike Stump1eb44332009-09-09 15:08:12 +00001937
1938
Ted Kremenek4f880632009-07-17 22:18:43 +00001939CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1940 // FIXME: For now we pretend that @catch and the code it contains does not
1941 // exit.
1942 return Block;
1943}
Mike Stump6d9828c2009-07-17 01:31:16 +00001944
Ted Kremenek2fda5042008-12-09 20:20:09 +00001945CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1946 // FIXME: This isn't complete. We basically treat @throw like a return
1947 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
Ted Kremenek6c249722009-09-24 18:45:41 +00001949 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001950 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001951 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001952
Ted Kremenek2fda5042008-12-09 20:20:09 +00001953 // Create the new block.
1954 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001955
Ted Kremenek2fda5042008-12-09 20:20:09 +00001956 // The Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001957 addSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001958
1959 // Add the statement to the block. This may create new blocks if S contains
1960 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001961 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001962}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001963
Mike Stump0979d802009-07-22 22:56:04 +00001964CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001965 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001966 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001967 return 0;
1968
1969 // Create the new block.
1970 Block = createBlock(false);
1971
Mike Stump5d1d2022010-01-19 02:20:09 +00001972 if (TryTerminatedBlock)
1973 // The current try statement is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001974 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001975 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001976 // otherwise the Exit block is the only successor.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00001977 addSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001978
1979 // Add the statement to the block. This may create new blocks if S contains
1980 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001981 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001982}
1983
Ted Kremenek4f880632009-07-17 22:18:43 +00001984CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001985 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
Mike Stump8f9893a2009-07-21 01:27:50 +00001987 // "do...while" is a control-flow statement. Thus we stop processing the
1988 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001989 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001990 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001991 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001992 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001993 } else
1994 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001995
1996 // Because of short-circuit evaluation, the condition of the loop can span
1997 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1998 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001999 CFGBlock* ExitConditionBlock = createBlock(false);
2000 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002001
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002002 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00002003 ExitConditionBlock->setTerminator(D);
2004
2005 // Now add the actual condition to the condition block. Because the condition
2006 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002007 if (Stmt* C = D->getCond()) {
2008 Block = ExitConditionBlock;
2009 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002010 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002011 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002012 return 0;
2013 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002014 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002015
Ted Kremenek54827132008-02-27 07:20:00 +00002016 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002017 Succ = EntryConditionBlock;
2018
Mike Stump00998a02009-07-23 23:25:26 +00002019 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002020 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002021
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002022 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002023 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002024 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002025 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002026
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002027 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002028 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2029 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2030 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002031
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002032 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002033 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002034
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002035 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002036 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002037
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002038 // NULL out Block to force lazy instantiation of blocks for the body.
2039 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002041 // If body is not a compound statement create implicit scope
2042 // and add destructors.
2043 if (!isa<CompoundStmt>(D->getBody()))
2044 addLocalScopeAndDtors(D->getBody());
2045
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002046 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002047 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002048
Ted Kremenekaf603f72007-08-30 18:39:40 +00002049 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002050 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002051 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002052 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002053 return 0;
2054 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002055
Ted Kremenekd173dc72010-08-17 20:59:56 +00002056 if (!KnownVal.isFalse()) {
2057 // Add an intermediate block between the BodyBlock and the
2058 // ExitConditionBlock to represent the "loop back" transition. Create an
2059 // empty block to represent the transition block for looping back to the
2060 // head of the loop.
2061 // FIXME: Can we do this more efficiently without adding another block?
2062 Block = NULL;
2063 Succ = BodyBlock;
2064 CFGBlock *LoopBackBlock = createBlock();
2065 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002066
Ted Kremenekd173dc72010-08-17 20:59:56 +00002067 // Add the loop body entry as a successor to the condition.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002068 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenekd173dc72010-08-17 20:59:56 +00002069 }
2070 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002071 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002072 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002073
Ted Kremenek941fde82009-07-24 04:47:11 +00002074 // Link up the condition block with the code that follows the loop.
2075 // (the false branch).
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002076 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002077
2078 // There can be no more statements in the body block(s) since we loop back to
2079 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002080 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002081
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002082 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002083 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002084 return BodyBlock;
2085}
2086
2087CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2088 // "continue" is a control-flow statement. Thus we stop processing the
2089 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002090 if (badCFG)
2091 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002092
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002093 // Now create a new block that ends with the continue statement.
2094 Block = createBlock(false);
2095 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002096
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002097 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002098 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenek9ce52702011-01-07 19:37:16 +00002099 if (ContinueJumpTarget.block) {
2100 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2101 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002102 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002103 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002104
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002105 return Block;
2106}
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002108CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002109 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002110
Ted Kremenek852274d2009-12-16 03:18:58 +00002111 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002112 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002113 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002114 }
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Ted Kremenek4f880632009-07-17 22:18:43 +00002116 // VLA types have expressions that must be evaluated.
2117 if (E->isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002118 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek4f880632009-07-17 22:18:43 +00002119 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2120 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002121 }
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Mike Stump6d9828c2009-07-17 01:31:16 +00002123 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002124}
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Ted Kremenek4f880632009-07-17 22:18:43 +00002126/// VisitStmtExpr - Utility method to handle (nested) statement
2127/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002128CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2129 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002130 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002131 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002132 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002133 return VisitCompoundStmt(SE->getSubStmt());
2134}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002135
Ted Kremenek411cdee2008-04-16 21:10:48 +00002136CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002137 // "switch" is a control-flow statement. Thus we stop processing the current
2138 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002139 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002140
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002141 // Save local scope position because in case of condition variable ScopePos
2142 // won't be restored when traversing AST.
2143 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2144
2145 // Create local scope for possible condition variable.
2146 // Store scope position. Add implicit destructor.
2147 if (VarDecl* VD = Terminator->getConditionVariable()) {
2148 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2149 addLocalScopeForVarDecl(VD);
2150 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2151 }
2152
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002153 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002154 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002155 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002156 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002157 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002158
2159 // Save the current "switch" context.
2160 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002161 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002162 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002163
Mike Stump6d9828c2009-07-17 01:31:16 +00002164 // Set the "default" case to be the block after the switch statement. If the
2165 // switch statement contains a "default:", this value will be overwritten with
2166 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002167 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002168
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002169 // Create a new block that will contain the switch statement.
2170 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002171
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002172 // Now process the switch body. The code after the switch is the implicit
2173 // successor.
2174 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002175 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002176
2177 // When visiting the body, the case statements should automatically get linked
2178 // up to the switch. We also don't keep a pointer to the body, since all
2179 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002180 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002181 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002182
2183 // If body is not a compound statement create implicit scope
2184 // and add destructors.
2185 if (!isa<CompoundStmt>(Terminator->getBody()))
2186 addLocalScopeAndDtors(Terminator->getBody());
2187
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002188 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002189 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002190 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002191 return 0;
2192 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002193
Mike Stump6d9828c2009-07-17 01:31:16 +00002194 // If we have no "default:" case, the default transition is to the code
2195 // following the switch body.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002196 addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002197
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002198 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002199 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002200 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002201 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002202 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002203
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002204 // Finally, if the SwitchStmt contains a condition variable, add both the
2205 // SwitchStmt and the condition variable initialization to the CFG.
2206 if (VarDecl *VD = Terminator->getConditionVariable()) {
2207 if (Expr *Init = VD->getInit()) {
2208 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002209 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002210 addStmt(Init);
2211 }
2212 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002213
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002214 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002215}
2216
Ted Kremenek4f880632009-07-17 22:18:43 +00002217CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002218 // CaseStmts are essentially labels, so they are the first statement in a
2219 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002220 CFGBlock *TopBlock = 0, *LastBlock = 0;
2221
2222 if (Stmt *Sub = CS->getSubStmt()) {
2223 // For deeply nested chains of CaseStmts, instead of doing a recursion
2224 // (which can blow out the stack), manually unroll and create blocks
2225 // along the way.
2226 while (isa<CaseStmt>(Sub)) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002227 CFGBlock *currentBlock = createBlock(false);
2228 currentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002229
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002230 if (TopBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002231 addSuccessor(LastBlock, currentBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002232 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002233 TopBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002234
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002235 addSuccessor(SwitchTerminatedBlock, currentBlock);
2236 LastBlock = currentBlock;
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002237
2238 CS = cast<CaseStmt>(Sub);
2239 Sub = CS->getSubStmt();
2240 }
2241
2242 addStmt(Sub);
2243 }
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002245 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002246 if (!CaseBlock)
2247 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002248
2249 // Cases statements partition blocks, so this is the top of the basic block we
2250 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002251 CaseBlock->setLabel(CS);
2252
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002253 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002254 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002255
2256 // Add this block to the list of successors for the block with the switch
2257 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002258 assert(SwitchTerminatedBlock);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002259 addSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002260
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002261 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2262 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002263
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002264 if (TopBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002265 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002266 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002267 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002268 // This block is now the implicit successor of other blocks.
2269 Succ = CaseBlock;
2270 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002271
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002272 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002273}
Mike Stump6d9828c2009-07-17 01:31:16 +00002274
Ted Kremenek411cdee2008-04-16 21:10:48 +00002275CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002276 if (Terminator->getSubStmt())
2277 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002279 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002280
2281 if (!DefaultCaseBlock)
2282 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002283
2284 // Default statements partition blocks, so this is the top of the basic block
2285 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002286 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002288 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002289 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002290
Mike Stump6d9828c2009-07-17 01:31:16 +00002291 // Unlike case statements, we don't add the default block to the successors
2292 // for the switch statement immediately. This is done when we finish
2293 // processing the switch statement. This allows for the default case
2294 // (including a fall-through to the code after the switch statement) to always
2295 // be the last successor of a switch-terminated block.
2296
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002297 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2298 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002299
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002300 // This block is now the implicit successor of other blocks.
2301 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002302
2303 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002304}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002305
Mike Stump5d1d2022010-01-19 02:20:09 +00002306CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2307 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2308 // current block.
2309 CFGBlock* TrySuccessor = NULL;
2310
2311 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002312 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002313 return 0;
2314 TrySuccessor = Block;
2315 } else TrySuccessor = Succ;
2316
Mike Stumpa1f93632010-01-20 01:15:34 +00002317 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002318
2319 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002320 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002321 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002322 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002323
Mike Stumpa1f93632010-01-20 01:15:34 +00002324 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002325 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2326 // The code after the try is the implicit successor.
2327 Succ = TrySuccessor;
2328 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002329 if (CS->getExceptionDecl() == 0) {
2330 HasCatchAll = true;
2331 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002332 Block = NULL;
2333 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2334 if (CatchBlock == 0)
2335 return 0;
2336 // Add this block to the list of successors for the block with the try
2337 // statement.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002338 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002339 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002340 if (!HasCatchAll) {
2341 if (PrevTryTerminatedBlock)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002342 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002343 else
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002344 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002345 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002346
2347 // The code after the try is the implicit successor.
2348 Succ = TrySuccessor;
2349
Mike Stumpf00cca52010-01-20 01:30:58 +00002350 // Save the current "try" context.
2351 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2352 TryTerminatedBlock = NewTryTerminatedBlock;
2353
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002354 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002355 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002356 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002357 return Block;
2358}
2359
2360CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2361 // CXXCatchStmt are treated like labels, so they are the first statement in a
2362 // block.
2363
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002364 // Save local scope position because in case of exception variable ScopePos
2365 // won't be restored when traversing AST.
2366 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2367
2368 // Create local scope for possible exception variable.
2369 // Store scope position. Add implicit destructor.
2370 if (VarDecl* VD = CS->getExceptionDecl()) {
2371 LocalScope::const_iterator BeginScopePos = ScopePos;
2372 addLocalScopeForVarDecl(VD);
2373 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2374 }
2375
Mike Stump5d1d2022010-01-19 02:20:09 +00002376 if (CS->getHandlerBlock())
2377 addStmt(CS->getHandlerBlock());
2378
2379 CFGBlock* CatchBlock = Block;
2380 if (!CatchBlock)
2381 CatchBlock = createBlock();
2382
2383 CatchBlock->setLabel(CS);
2384
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002385 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002386 return 0;
2387
2388 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2389 Block = NULL;
2390
2391 return CatchBlock;
2392}
2393
John McCall4765fa02010-12-06 08:20:24 +00002394CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002395 AddStmtChoice asc) {
2396 if (BuildOpts.AddImplicitDtors) {
2397 // If adding implicit destructors visit the full expression for adding
2398 // destructors of temporaries.
2399 VisitForTemporaryDtors(E->getSubExpr());
2400
2401 // Full expression has to be added as CFGStmt so it will be sequenced
2402 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002403 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002404 }
2405 return Visit(E->getSubExpr(), asc);
2406}
2407
Zhongxing Xua725ed42010-11-01 13:04:58 +00002408CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2409 AddStmtChoice asc) {
2410 if (asc.alwaysAdd()) {
2411 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002412 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002413
2414 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002415 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002416 }
2417 return Visit(E->getSubExpr(), asc);
2418}
2419
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002420CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2421 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002422 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002423 if (!C->isElidable())
Ted Kremenek892697d2010-12-16 07:46:53 +00002424 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002425
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002426 return VisitChildren(C);
2427}
2428
Zhongxing Xua725ed42010-11-01 13:04:58 +00002429CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2430 AddStmtChoice asc) {
2431 if (asc.alwaysAdd()) {
2432 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002433 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002434 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002435 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002436 }
2437 return Visit(E->getSubExpr(), asc);
2438}
2439
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002440CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2441 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002442 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002443 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002444 return VisitChildren(C);
2445}
2446
Ted Kremenekad5a8942010-08-02 23:46:59 +00002447CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002448 AddStmtChoice asc) {
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002449 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002450 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002451 return VisitChildren(C);
2452}
2453
Zhongxing Xua725ed42010-11-01 13:04:58 +00002454CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2455 AddStmtChoice asc) {
2456 if (asc.alwaysAdd()) {
2457 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002458 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002459 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002460 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002461}
2462
Ted Kremenek19bb3562007-08-28 19:26:49 +00002463CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002464 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002465 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002466
Ted Kremenek19bb3562007-08-28 19:26:49 +00002467 if (!IBlock) {
2468 IBlock = createBlock(false);
2469 cfg->setIndirectGotoBlock(IBlock);
2470 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002471
Ted Kremenek19bb3562007-08-28 19:26:49 +00002472 // IndirectGoto is a control-flow statement. Thus we stop processing the
2473 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002474 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002475 return 0;
2476
Ted Kremenek19bb3562007-08-28 19:26:49 +00002477 Block = createBlock(false);
2478 Block->setTerminator(I);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002479 addSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002480 return addStmt(I->getTarget());
2481}
2482
Marcin Swiderski8599e762010-11-03 06:19:35 +00002483CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2484tryAgain:
2485 if (!E) {
2486 badCFG = true;
2487 return NULL;
2488 }
2489 switch (E->getStmtClass()) {
2490 default:
2491 return VisitChildrenForTemporaryDtors(E);
2492
2493 case Stmt::BinaryOperatorClass:
2494 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2495
2496 case Stmt::CXXBindTemporaryExprClass:
2497 return VisitCXXBindTemporaryExprForTemporaryDtors(
2498 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2499
John McCall56ca35d2011-02-17 10:25:35 +00002500 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski8599e762010-11-03 06:19:35 +00002501 case Stmt::ConditionalOperatorClass:
2502 return VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002503 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002504
2505 case Stmt::ImplicitCastExprClass:
2506 // For implicit cast we want BindToTemporary to be passed further.
2507 E = cast<CastExpr>(E)->getSubExpr();
2508 goto tryAgain;
2509
2510 case Stmt::ParenExprClass:
2511 E = cast<ParenExpr>(E)->getSubExpr();
2512 goto tryAgain;
2513 }
2514}
2515
2516CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2517 // When visiting children for destructors we want to visit them in reverse
2518 // order. Because there's no reverse iterator for children must to reverse
2519 // them in helper vector.
2520 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2521 ChildrenVect ChildrenRev;
John McCall7502c1d2011-02-13 04:07:26 +00002522 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002523 if (*I) ChildrenRev.push_back(*I);
2524 }
2525
2526 CFGBlock *B = Block;
2527 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2528 L = ChildrenRev.rend(); I != L; ++I) {
2529 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2530 B = R;
2531 }
2532 return B;
2533}
2534
2535CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2536 if (E->isLogicalOp()) {
2537 // Destructors for temporaries in LHS expression should be called after
2538 // those for RHS expression. Even if this will unnecessarily create a block,
2539 // this block will be used at least by the full expression.
2540 autoCreateBlock();
2541 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2542 if (badCFG)
2543 return NULL;
2544
2545 Succ = ConfluenceBlock;
2546 Block = NULL;
2547 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2548
2549 if (RHSBlock) {
2550 if (badCFG)
2551 return NULL;
2552
2553 // If RHS expression did produce destructors we need to connect created
2554 // blocks to CFG in same manner as for binary operator itself.
2555 CFGBlock *LHSBlock = createBlock(false);
2556 LHSBlock->setTerminator(CFGTerminator(E, true));
2557
2558 // For binary operator LHS block is before RHS in list of predecessors
2559 // of ConfluenceBlock.
2560 std::reverse(ConfluenceBlock->pred_begin(),
2561 ConfluenceBlock->pred_end());
2562
2563 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002564 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002565 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2566 KnownVal.negate();
2567
2568 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2569 // itself.
2570 if (E->getOpcode() == BO_LOr) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002571 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2572 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002573 } else {
2574 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002575 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2576 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002577 }
2578
2579 Block = LHSBlock;
2580 return LHSBlock;
2581 }
2582
2583 Block = ConfluenceBlock;
2584 return ConfluenceBlock;
2585 }
2586
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002587 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002588 // For assignment operator (=) LHS expression is visited
2589 // before RHS expression. For destructors visit them in reverse order.
2590 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2591 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2592 return LHSBlock ? LHSBlock : RHSBlock;
2593 }
2594
2595 // For any other binary operator RHS expression is visited before
2596 // LHS expression (order of children). For destructors visit them in reverse
2597 // order.
2598 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2599 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2600 return RHSBlock ? RHSBlock : LHSBlock;
2601}
2602
2603CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2604 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2605 // First add destructors for temporaries in subexpression.
2606 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002607 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002608 // If lifetime of temporary is not prolonged (by assigning to constant
2609 // reference) add destructor for it.
2610 autoCreateBlock();
2611 appendTemporaryDtor(Block, E);
2612 B = Block;
2613 }
2614 return B;
2615}
2616
2617CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCall56ca35d2011-02-17 10:25:35 +00002618 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002619 // First add destructors for condition expression. Even if this will
2620 // unnecessarily create a block, this block will be used at least by the full
2621 // expression.
2622 autoCreateBlock();
2623 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2624 if (badCFG)
2625 return NULL;
John McCall56ca35d2011-02-17 10:25:35 +00002626 if (BinaryConditionalOperator *BCO
2627 = dyn_cast<BinaryConditionalOperator>(E)) {
2628 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002629 if (badCFG)
2630 return NULL;
2631 }
2632
John McCall56ca35d2011-02-17 10:25:35 +00002633 // Try to add block with destructors for LHS expression.
2634 CFGBlock *LHSBlock = NULL;
2635 Succ = ConfluenceBlock;
2636 Block = NULL;
2637 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2638 if (badCFG)
2639 return NULL;
2640
Marcin Swiderski8599e762010-11-03 06:19:35 +00002641 // Try to add block with destructors for RHS expression;
2642 Succ = ConfluenceBlock;
2643 Block = NULL;
John McCall56ca35d2011-02-17 10:25:35 +00002644 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2645 BindToTemporary);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002646 if (badCFG)
2647 return NULL;
2648
2649 if (!RHSBlock && !LHSBlock) {
2650 // If neither LHS nor RHS expression had temporaries to destroy don't create
2651 // more blocks.
2652 Block = ConfluenceBlock;
2653 return Block;
2654 }
2655
2656 Block = createBlock(false);
2657 Block->setTerminator(CFGTerminator(E, true));
2658
2659 // See if this is a known constant.
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002660 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski8599e762010-11-03 06:19:35 +00002661
2662 if (LHSBlock) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002663 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002664 } else if (KnownVal.isFalse()) {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002665 addSuccessor(Block, NULL);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002666 } else {
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002667 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002668 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2669 }
2670
2671 if (!RHSBlock)
2672 RHSBlock = ConfluenceBlock;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002673 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002674
2675 return Block;
2676}
2677
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002678} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002679
Mike Stump6d9828c2009-07-17 01:31:16 +00002680/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2681/// no successors or predecessors. If this is the first block created in the
2682/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002683CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002684 bool first_block = begin() == end();
2685
2686 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002687 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2688 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2689 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002690
2691 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002692 if (first_block)
2693 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002694
2695 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002696 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002697}
2698
Ted Kremenek026473c2007-08-23 16:51:22 +00002699/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2700/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002701CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002702 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002703 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002704 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002705}
2706
Ted Kremenek63f58872007-10-01 19:33:33 +00002707//===----------------------------------------------------------------------===//
2708// CFG: Queries for BlkExprs.
2709//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002710
Ted Kremenek63f58872007-10-01 19:33:33 +00002711namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002712 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002713}
2714
Ted Kremenek8a693662009-12-23 23:37:10 +00002715static void FindSubExprAssignments(Stmt *S,
2716 llvm::SmallPtrSet<Expr*,50>& Set) {
2717 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002718 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002719
John McCall7502c1d2011-02-13 04:07:26 +00002720 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002721 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002722 if (!child)
2723 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002724
Ted Kremenek8a693662009-12-23 23:37:10 +00002725 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002726 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002727
Ted Kremenek8a693662009-12-23 23:37:10 +00002728 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002729 }
2730}
2731
Ted Kremenek63f58872007-10-01 19:33:33 +00002732static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2733 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002734
2735 // Look for assignments that are used as subexpressions. These are the only
2736 // assignments that we want to *possibly* register as a block-level
2737 // expression. Basically, if an assignment occurs both in a subexpression and
2738 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002739 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002740
Ted Kremenek63f58872007-10-01 19:33:33 +00002741 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002742 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002743 if (CFGStmt S = BI->getAs<CFGStmt>())
2744 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002745
Ted Kremenek411cdee2008-04-16 21:10:48 +00002746 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002747
2748 // Iterate over the statements again on identify the Expr* and Stmt* at the
2749 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002750
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002751 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2752 CFGStmt CS = BI->getAs<CFGStmt>();
2753 if (!CS.isValid())
2754 continue;
2755 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002756
Ted Kremenek411cdee2008-04-16 21:10:48 +00002757 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002758 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002759 // expression are really "statements" whose value is never used by
2760 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002761 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002762 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002763 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2764 // Special handling for statement expressions. The last statement in
2765 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002766 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002767 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002768 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002769 (*M)[C->body_back()] = x;
2770 }
2771 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002772
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002773 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002774 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002775 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002776 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002777
Ted Kremenek411cdee2008-04-16 21:10:48 +00002778 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002779
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002780 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002781
Ted Kremenek390e48b2008-11-12 21:11:49 +00002782 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002783 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002784 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002785 }
2786 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002787
Ted Kremenek63f58872007-10-01 19:33:33 +00002788 return M;
2789}
2790
Ted Kremenek86946742008-01-17 20:48:37 +00002791CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2792 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002793 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002794
Ted Kremenek63f58872007-10-01 19:33:33 +00002795 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002796 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002797 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002798}
2799
2800unsigned CFG::getNumBlkExprs() {
2801 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2802 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002803
2804 // We assume callers interested in the number of BlkExprs will want
2805 // the map constructed if it doesn't already exist.
2806 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2807 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00002808}
2809
Ted Kremenek274f4332008-04-28 18:00:46 +00002810//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002811// Filtered walking of the CFG.
2812//===----------------------------------------------------------------------===//
2813
2814bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002815 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002816
2817 if (F.IgnoreDefaultsWithCoveredEnums) {
2818 // If the 'To' has no label or is labeled but the label isn't a
2819 // CaseStmt then filter this edge.
2820 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002821 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002822 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002823 const Stmt *L = To->getLabel();
2824 if (!L || !isa<CaseStmt>(L))
2825 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002826 }
2827 }
2828 }
2829
2830 return false;
2831}
2832
2833//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002834// Cleanup: CFG dstor.
2835//===----------------------------------------------------------------------===//
2836
Ted Kremenek63f58872007-10-01 19:33:33 +00002837CFG::~CFG() {
2838 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2839}
Mike Stump6d9828c2009-07-17 01:31:16 +00002840
Ted Kremenek7dba8602007-08-29 21:56:09 +00002841//===----------------------------------------------------------------------===//
2842// CFG pretty printing
2843//===----------------------------------------------------------------------===//
2844
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002845namespace {
2846
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002847class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002848 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002849 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002850 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002851 DeclMapTy DeclMap;
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002852 signed currentBlock;
2853 unsigned currentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002854 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002855public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002856
Chris Lattnere4f21422009-06-30 01:26:17 +00002857 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002858 : currentBlock(0), currentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002859 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2860 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002861 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002862 BI != BEnd; ++BI, ++j ) {
2863 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2864 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2865 StmtMap[SE] = P;
2866
2867 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2868 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002869
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002870 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2871 if (VarDecl* VD = IS->getConditionVariable())
2872 DeclMap[VD] = P;
2873
2874 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2875 if (VarDecl* VD = FS->getConditionVariable())
2876 DeclMap[VD] = P;
2877
2878 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2879 if (VarDecl* VD = WS->getConditionVariable())
2880 DeclMap[VD] = P;
2881
2882 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2883 if (VarDecl* VD = SS->getConditionVariable())
2884 DeclMap[VD] = P;
2885
2886 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2887 if (VarDecl* VD = CS->getExceptionDecl())
2888 DeclMap[VD] = P;
2889 }
2890 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002891 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002892 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002893 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002894
Ted Kremenek42a509f2007-08-31 21:30:12 +00002895 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002896
Chris Lattnere4f21422009-06-30 01:26:17 +00002897 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002898 void setBlockID(signed i) { currentBlock = i; }
2899 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002900
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002901 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2902 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002903
2904 if (I == StmtMap.end())
2905 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002906
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002907 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2908 && I->second.second == currentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002909 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002910 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002911
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002912 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002913 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002914 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002915
2916 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2917 DeclMapTy::iterator I = DeclMap.find(D);
2918
2919 if (I == DeclMap.end())
2920 return false;
2921
Ted Kremenek0a3ed312010-12-17 04:44:39 +00002922 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2923 && I->second.second == currentStmt) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002924 return false;
2925 }
2926
2927 OS << "[B" << I->second.first << "." << I->second.second << "]";
2928 return true;
2929 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002930};
Chris Lattnere4f21422009-06-30 01:26:17 +00002931} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002932
Chris Lattnere4f21422009-06-30 01:26:17 +00002933
2934namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002935class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002936 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002937
Ted Kremeneka95d3752008-09-13 05:16:45 +00002938 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002939 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002940 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002941public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002942 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002943 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002944 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002945
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002946 void VisitIfStmt(IfStmt* I) {
2947 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002948 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002949 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002950
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002951 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002952 void VisitStmt(Stmt* Terminator) {
2953 Terminator->printPretty(OS, Helper, Policy);
2954 }
2955
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002956 void VisitForStmt(ForStmt* F) {
2957 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002958 if (F->getInit())
2959 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002960 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002961 if (Stmt* C = F->getCond())
2962 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002963 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002964 if (F->getInc())
2965 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002966 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002967 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002968
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002969 void VisitWhileStmt(WhileStmt* W) {
2970 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002971 if (Stmt* C = W->getCond())
2972 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002973 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002974
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002975 void VisitDoStmt(DoStmt* D) {
2976 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002977 if (Stmt* C = D->getCond())
2978 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002979 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002980
Ted Kremenek411cdee2008-04-16 21:10:48 +00002981 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002982 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002983 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002984 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002985
Mike Stump5d1d2022010-01-19 02:20:09 +00002986 void VisitCXXTryStmt(CXXTryStmt* CS) {
2987 OS << "try ...";
2988 }
2989
John McCall56ca35d2011-02-17 10:25:35 +00002990 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002991 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002992 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002993 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002994
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002995 void VisitChooseExpr(ChooseExpr* C) {
2996 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002997 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002998 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002999 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003000
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003001 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3002 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003003 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003004 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003005
Ted Kremenek805e9a82007-08-31 21:49:40 +00003006 void VisitBinaryOperator(BinaryOperator* B) {
3007 if (!B->isLogicalOp()) {
3008 VisitExpr(B);
3009 return;
3010 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003011
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003012 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003013
Ted Kremenek805e9a82007-08-31 21:49:40 +00003014 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00003015 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00003016 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003017 return;
John McCall2de56d12010-08-25 11:45:40 +00003018 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00003019 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00003020 return;
3021 default:
3022 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003023 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003024 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003025
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003026 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003027 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003028 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003029};
Chris Lattnere4f21422009-06-30 01:26:17 +00003030} // end anonymous namespace
3031
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003032static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003033 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003034 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3035 Stmt *S = CS;
3036
3037 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003038
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003039 // special printing for statement-expressions.
3040 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3041 CompoundStmt* Sub = SE->getSubStmt();
3042
John McCall7502c1d2011-02-13 04:07:26 +00003043 if (Sub->children()) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003044 OS << "({ ... ; ";
3045 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3046 OS << " })\n";
3047 return;
3048 }
3049 }
3050 // special printing for comma expressions.
3051 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3052 if (B->getOpcode() == BO_Comma) {
3053 OS << "... , ";
3054 Helper->handledStmt(B->getRHS(),OS);
3055 OS << '\n';
3056 return;
3057 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003058 }
3059 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003060 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003061
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003062 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003063 OS << " (OperatorCall)";
3064 } else if (isa<CXXBindTemporaryExpr>(S)) {
3065 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003066 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003067
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003068 // Expressions need a newline.
3069 if (isa<Expr>(S))
3070 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003071
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003072 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
Sean Huntcbb67482011-01-08 20:30:50 +00003073 CXXCtorInitializer* I = IE;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003074 if (I->isBaseInitializer())
3075 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003076 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003077
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003078 OS << "(";
3079 if (Expr* IE = I->getInit())
3080 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3081 OS << ")";
3082
3083 if (I->isBaseInitializer())
3084 OS << " (Base initializer)\n";
3085 else OS << " (Member initializer)\n";
3086
3087 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3088 VarDecl* VD = DE.getVarDecl();
3089 Helper->handleDecl(VD, OS);
3090
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003091 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003092 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3093 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003094 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3095 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003096
3097 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3098 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003099
3100 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3101 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3102 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003103 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003104
3105 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3106 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003107
3108 const Type *T = FD->getType().getTypePtr();
3109 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3110 T = ET;
3111
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003112 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003113 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003114 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003115
3116 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3117 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3118 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3119 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003120 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003121}
Mike Stump6d9828c2009-07-17 01:31:16 +00003122
Chris Lattnere4f21422009-06-30 01:26:17 +00003123static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3124 const CFGBlock& B,
3125 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003126
Ted Kremenek42a509f2007-08-31 21:30:12 +00003127 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003128
Ted Kremenek7dba8602007-08-29 21:56:09 +00003129 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003130 OS << "\n [ B" << B.getBlockID();
3131
Ted Kremenek42a509f2007-08-31 21:30:12 +00003132 if (&B == &cfg->getEntry())
3133 OS << " (ENTRY) ]\n";
3134 else if (&B == &cfg->getExit())
3135 OS << " (EXIT) ]\n";
3136 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003137 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003138 else
3139 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003140
Ted Kremenek9cffe732007-08-29 23:20:49 +00003141 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003142 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003143
3144 if (print_edges)
3145 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003146
Mike Stump079bd722010-01-19 22:00:14 +00003147 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003148 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003149 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003150 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003151 C->getLHS()->printPretty(OS, Helper,
3152 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003153 if (C->getRHS()) {
3154 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003155 C->getRHS()->printPretty(OS, Helper,
3156 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003157 }
Mike Stump079bd722010-01-19 22:00:14 +00003158 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003159 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003160 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003161 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003162 if (CS->getExceptionDecl())
3163 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3164 0);
3165 else
3166 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003167 OS << ")";
3168
3169 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003170 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003171
Ted Kremenek9cffe732007-08-29 23:20:49 +00003172 OS << ":\n";
3173 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003174
Ted Kremenekfddd5182007-08-21 21:42:03 +00003175 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003176 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003177
Ted Kremenek42a509f2007-08-31 21:30:12 +00003178 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3179 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003180
Ted Kremenek9cffe732007-08-29 23:20:49 +00003181 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003182 if (print_edges)
3183 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003184
Ted Kremeneka95d3752008-09-13 05:16:45 +00003185 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003186
Ted Kremenek42a509f2007-08-31 21:30:12 +00003187 if (Helper)
3188 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003189
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003190 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003191 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003192
Ted Kremenek9cffe732007-08-29 23:20:49 +00003193 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003194 if (B.getTerminator()) {
3195 if (print_edges)
3196 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003197
Ted Kremenek9cffe732007-08-29 23:20:49 +00003198 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003199
Ted Kremenek42a509f2007-08-31 21:30:12 +00003200 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003201
Chris Lattnere4f21422009-06-30 01:26:17 +00003202 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3203 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003204 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003205 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003206 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003207
Ted Kremenek9cffe732007-08-29 23:20:49 +00003208 if (print_edges) {
3209 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003210 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003211 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003212
Ted Kremenek42a509f2007-08-31 21:30:12 +00003213 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3214 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003215
Ted Kremenek42a509f2007-08-31 21:30:12 +00003216 if (i == 8 || (i-8) == 0)
3217 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003218
Ted Kremenek9cffe732007-08-29 23:20:49 +00003219 OS << " B" << (*I)->getBlockID();
3220 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003221
Ted Kremenek42a509f2007-08-31 21:30:12 +00003222 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003223
Ted Kremenek42a509f2007-08-31 21:30:12 +00003224 // Print the successors of this block.
3225 OS << " Successors (" << B.succ_size() << "):";
3226 i = 0;
3227
3228 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3229 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003230
Ted Kremenek42a509f2007-08-31 21:30:12 +00003231 if (i == 8 || (i-8) % 10 == 0)
3232 OS << "\n ";
3233
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003234 if (*I)
3235 OS << " B" << (*I)->getBlockID();
3236 else
3237 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003238 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003239
Ted Kremenek9cffe732007-08-29 23:20:49 +00003240 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003241 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003242}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003243
Ted Kremenek42a509f2007-08-31 21:30:12 +00003244
3245/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003246void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003247
3248/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003249void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3250 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003251
Ted Kremenek42a509f2007-08-31 21:30:12 +00003252 // Print the entry block.
3253 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003254
Ted Kremenek42a509f2007-08-31 21:30:12 +00003255 // Iterate through the CFGBlocks and print them one by one.
3256 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3257 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003258 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003259 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003260
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003261 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003262 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003263
Ted Kremenek42a509f2007-08-31 21:30:12 +00003264 // Print the exit block.
3265 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003266 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003267}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003268
3269/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003270void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3271 print(llvm::errs(), cfg, LO);
3272}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003273
3274/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3275/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003276void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3277 const LangOptions &LO) const {
3278 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003279 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003280}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003281
Ted Kremeneka2925852008-01-30 23:02:42 +00003282/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003283void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003284 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003285 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003286 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003287}
3288
Ted Kremenek390e48b2008-11-12 21:11:49 +00003289Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003290 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003291 if (!Terminator)
3292 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003293
Ted Kremenek411cdee2008-04-16 21:10:48 +00003294 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003295
Ted Kremenek411cdee2008-04-16 21:10:48 +00003296 switch (Terminator->getStmtClass()) {
3297 default:
3298 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003299
Ted Kremenek411cdee2008-04-16 21:10:48 +00003300 case Stmt::ForStmtClass:
3301 E = cast<ForStmt>(Terminator)->getCond();
3302 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003303
Ted Kremenek411cdee2008-04-16 21:10:48 +00003304 case Stmt::WhileStmtClass:
3305 E = cast<WhileStmt>(Terminator)->getCond();
3306 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003307
Ted Kremenek411cdee2008-04-16 21:10:48 +00003308 case Stmt::DoStmtClass:
3309 E = cast<DoStmt>(Terminator)->getCond();
3310 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003311
Ted Kremenek411cdee2008-04-16 21:10:48 +00003312 case Stmt::IfStmtClass:
3313 E = cast<IfStmt>(Terminator)->getCond();
3314 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003315
Ted Kremenek411cdee2008-04-16 21:10:48 +00003316 case Stmt::ChooseExprClass:
3317 E = cast<ChooseExpr>(Terminator)->getCond();
3318 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003319
Ted Kremenek411cdee2008-04-16 21:10:48 +00003320 case Stmt::IndirectGotoStmtClass:
3321 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3322 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003323
Ted Kremenek411cdee2008-04-16 21:10:48 +00003324 case Stmt::SwitchStmtClass:
3325 E = cast<SwitchStmt>(Terminator)->getCond();
3326 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003327
John McCall56ca35d2011-02-17 10:25:35 +00003328 case Stmt::BinaryConditionalOperatorClass:
3329 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3330 break;
3331
Ted Kremenek411cdee2008-04-16 21:10:48 +00003332 case Stmt::ConditionalOperatorClass:
3333 E = cast<ConditionalOperator>(Terminator)->getCond();
3334 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003335
Ted Kremenek411cdee2008-04-16 21:10:48 +00003336 case Stmt::BinaryOperatorClass: // '&&' and '||'
3337 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003338 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003339
Ted Kremenek390e48b2008-11-12 21:11:49 +00003340 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003341 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003342 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003343
Ted Kremenek411cdee2008-04-16 21:10:48 +00003344 return E ? E->IgnoreParens() : NULL;
3345}
3346
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003347bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003348 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003349 if (!Terminator)
3350 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003351
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003352 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003353
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003354 switch (Terminator->getStmtClass()) {
3355 default:
3356 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003357
3358 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003359 case Stmt::WhileStmtClass:
3360 case Stmt::DoStmtClass:
3361 case Stmt::IfStmtClass:
3362 case Stmt::ChooseExprClass:
John McCall56ca35d2011-02-17 10:25:35 +00003363 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003364 case Stmt::ConditionalOperatorClass:
3365 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003366 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003367 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003368
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003369 return E ? E->IgnoreParens() : NULL;
3370}
3371
Ted Kremeneka2925852008-01-30 23:02:42 +00003372
Ted Kremenek7dba8602007-08-29 21:56:09 +00003373//===----------------------------------------------------------------------===//
3374// CFG Graphviz Visualization
3375//===----------------------------------------------------------------------===//
3376
Ted Kremenek42a509f2007-08-31 21:30:12 +00003377
3378#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003379static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003380#endif
3381
Chris Lattnere4f21422009-06-30 01:26:17 +00003382void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003383#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003384 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003385 GraphHelper = &H;
3386 llvm::ViewGraph(this,"CFG");
3387 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003388#endif
3389}
3390
Ted Kremenek7dba8602007-08-29 21:56:09 +00003391namespace llvm {
3392template<>
3393struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003394
3395 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3396
3397 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003398
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003399#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003400 std::string OutSStr;
3401 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003402 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003403 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003404
3405 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3406
3407 // Process string output to make it nicer...
3408 for (unsigned i = 0; i != OutStr.length(); ++i)
3409 if (OutStr[i] == '\n') { // Left justify
3410 OutStr[i] = '\\';
3411 OutStr.insert(OutStr.begin()+i+1, 'l');
3412 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003413
Ted Kremenek7dba8602007-08-29 21:56:09 +00003414 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003415#else
3416 return "";
3417#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003418 }
3419};
3420} // end namespace llvm