blob: ed9899e3f99402954ddbf653f0054e82d192bd7c [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Ted Kremenek852274d2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramer792bea92010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000047
Benjamin Kramer792bea92010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek431ac2d2010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5ba290a2010-03-02 21:43:54 +000050
Ted Kremenek852274d2009-12-16 03:18:58 +000051private:
Benjamin Kramer792bea92010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek852274d2009-12-16 03:18:58 +000053};
Mike Stump6d9828c2009-07-17 01:31:16 +000054
Marcin Swiderskif1308c72010-09-25 11:05:21 +000055/// LocalScope - Node in tree of local scopes created for C++ implicit
56/// destructor calls generation. It contains list of automatic variables
57/// declared in the scope and link to position in previous scope this scope
58/// began in.
59///
60/// The process of creating local scopes is as follows:
61/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
62/// - Before processing statements in scope (e.g. CompoundStmt) create
63/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
64/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000065/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000066/// at this VarDecl,
67/// - For every normal (without jump) end of scope add to CFGBlock destructors
68/// for objects in the current scope,
69/// - For every jump add to CFGBlock destructors for objects
70/// between CFGBuilder::ScopePos and local scope position saved for jump
71/// target. Thanks to C++ restrictions on goto jumps we can be sure that
72/// jump target position will be on the path to root from CFGBuilder::ScopePos
73/// (adding any variable that doesn't need constructor to be called to
74/// LocalScope can break this assumption),
75///
76class LocalScope {
77public:
78 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
79
80 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000081 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +000082 class const_iterator {
83 const LocalScope* Scope;
84
85 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
86 /// Invalid iterator (with null Scope) has VarIter equal to 0.
87 unsigned VarIter;
88
89 public:
90 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
91 /// Incrementing invalid iterator is allowed and will result in invalid
92 /// iterator.
93 const_iterator()
94 : Scope(NULL), VarIter(0) {}
95
96 /// Create valid iterator. In case when S.Prev is an invalid iterator and
97 /// I is equal to 0, this will create invalid iterator.
98 const_iterator(const LocalScope& S, unsigned I)
99 : Scope(&S), VarIter(I) {
100 // Iterator to "end" of scope is not allowed. Handle it by going up
101 // in scopes tree possibly up to invalid iterator in the root.
102 if (VarIter == 0 && Scope)
103 *this = Scope->Prev;
104 }
105
106 VarDecl* const* operator->() const {
107 assert (Scope && "Dereferencing invalid iterator is not allowed");
108 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
109 return &Scope->Vars[VarIter - 1];
110 }
111 VarDecl* operator*() const {
112 return *this->operator->();
113 }
114
115 const_iterator& operator++() {
116 if (!Scope)
117 return *this;
118
119 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
120 --VarIter;
121 if (VarIter == 0)
122 *this = Scope->Prev;
123 return *this;
124 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000125 const_iterator operator++(int) {
126 const_iterator P = *this;
127 ++*this;
128 return P;
129 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000130
131 bool operator==(const const_iterator& rhs) const {
132 return Scope == rhs.Scope && VarIter == rhs.VarIter;
133 }
134 bool operator!=(const const_iterator& rhs) const {
135 return !(*this == rhs);
136 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000137
138 operator bool() const {
139 return *this != const_iterator();
140 }
141
142 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000143 };
144
145 friend class const_iterator;
146
147private:
148 /// Automatic variables in order of declaration.
149 AutomaticVarsTy Vars;
150 /// Iterator to variable in previous scope that was declared just before
151 /// begin of this scope.
152 const_iterator Prev;
153
154public:
155 /// Constructs empty scope linked to previous scope in specified place.
156 LocalScope(const_iterator P)
157 : Vars()
158 , Prev(P) {}
159
160 /// Begin of scope in direction of CFG building (backwards).
161 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000162
163 void addVar(VarDecl* VD) {
164 Vars.push_back(VD);
165 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000166};
167
Marcin Swiderski35387a02010-09-30 22:42:32 +0000168/// distance - Calculates distance from this to L. L must be reachable from this
169/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
170/// number of scopes between this and L.
171int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
172 int D = 0;
173 const_iterator F = *this;
174 while (F.Scope != L.Scope) {
175 assert (F != const_iterator()
176 && "L iterator is not reachable from F iterator.");
177 D += F.VarIter;
178 F = F.Scope->Prev;
179 }
180 D += F.VarIter - L.VarIter;
181 return D;
182}
183
184/// BlockScopePosPair - Structure for specifying position in CFG during its
185/// build process. It consists of CFGBlock that specifies position in CFG graph
186/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000187struct BlockScopePosPair {
188 BlockScopePosPair() {}
189 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
190 : Block(B), ScopePos(S) {}
191
192 CFGBlock* Block;
193 LocalScope::const_iterator ScopePos;
194};
195
Ted Kremeneka34ea072008-08-04 22:51:42 +0000196/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000197/// The builder is stateful: an instance of the builder should be used to only
198/// construct a single CFG.
199///
200/// Example usage:
201///
202/// CFGBuilder builder;
203/// CFG* cfg = builder.BuildAST(stmt1);
204///
Mike Stump6d9828c2009-07-17 01:31:16 +0000205/// CFG construction is done via a recursive walk of an AST. We actually parse
206/// the AST in reverse order so that the successor of a basic block is
207/// constructed prior to its predecessor. This allows us to nicely capture
208/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000209///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000210class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000211 typedef BlockScopePosPair JumpTarget;
212 typedef BlockScopePosPair JumpSource;
213
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000214 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000215 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000216
Ted Kremenekfddd5182007-08-21 21:42:03 +0000217 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000218 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000219 JumpTarget ContinueJumpTarget;
220 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000221 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000222 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000223 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000225 // Current position in local scope.
226 LocalScope::const_iterator ScopePos;
227
228 // LabelMap records the mapping from Label expressions to their jump targets.
229 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000230 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000231
232 // A list of blocks that end with a "goto" that must be backpatched to their
233 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000234 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000235 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000236
Ted Kremenek19bb3562007-08-28 19:26:49 +0000237 // A list of labels whose address has been taken (for indirect gotos).
238 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
239 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000241 bool badCFG;
242 CFG::BuildOptions BuildOpts;
243
Mike Stump6d9828c2009-07-17 01:31:16 +0000244public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000245 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
246 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000247 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000248 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000249
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000250 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000251 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000252 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000253
Ted Kremenek4f880632009-07-17 22:18:43 +0000254private:
255 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000256 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
257 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
258 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000259 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000260 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
261 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
262 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000263 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
264 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000265 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000266 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
267 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000268 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
269 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000270 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000271 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000272 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000273 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000274 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000275 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000276 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000277 CFGBlock *VisitDeclStmt(DeclStmt *DS);
278 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000279 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
280 CFGBlock *VisitDoStmt(DoStmt *D);
281 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000282 CFGBlock *VisitGotoStmt(GotoStmt* G);
283 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000284 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000285 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
286 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000287 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000288 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
289 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
290 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
291 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
292 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
293 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000294 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
295 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000296 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
297 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000298
Ted Kremenek852274d2009-12-16 03:18:58 +0000299 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
300 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000301 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000302
Ted Kremenek274f4332008-04-28 18:00:46 +0000303 // NYS == Not Yet Supported
304 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000305 badCFG = true;
306 return Block;
307 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000308
Ted Kremenek4f880632009-07-17 22:18:43 +0000309 void autoCreateBlock() { if (!Block) Block = createBlock(); }
310 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000311
Zhongxing Xudf119892010-06-03 06:43:23 +0000312 CFGBlock *addStmt(Stmt *S) {
313 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000314 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000315 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000316 void addAutomaticObjDtors(LocalScope::const_iterator B,
317 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000318 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000319
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000320 // Local scopes creation.
321 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
322
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000323 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000324 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
325 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
326
327 void addLocalScopeAndDtors(Stmt* S);
328
329 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek852274d2009-12-16 03:18:58 +0000330 void AppendStmt(CFGBlock *B, Stmt *S,
331 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
332 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000333 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000334 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
335 B->appendInitializer(I, cfg->getBumpVectorContext());
336 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000337 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
338 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
339 }
340 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
341 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
342 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000343
Marcin Swiderski53de1342010-09-30 22:54:37 +0000344 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
345 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
346 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
347 LocalScope::const_iterator E, Stmt* S);
348 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
349 LocalScope::const_iterator B, LocalScope::const_iterator E);
350
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000351 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
352 B->addSuccessor(S, cfg->getBumpVectorContext());
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000355 /// TryResult - a class representing a variant over the values
356 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
357 /// and is used by the CFGBuilder to decide if a branch condition
358 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000359 class TryResult {
360 int X;
361 public:
362 TryResult(bool b) : X(b ? 1 : 0) {}
363 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek941fde82009-07-24 04:47:11 +0000365 bool isTrue() const { return X == 1; }
366 bool isFalse() const { return X == 0; }
367 bool isKnown() const { return X >= 0; }
368 void negate() {
369 assert(isKnown());
370 X ^= 0x1;
371 }
372 };
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Mike Stump00998a02009-07-23 23:25:26 +0000374 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
375 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000376 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000377 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000378 return TryResult();
379
Mike Stump00998a02009-07-23 23:25:26 +0000380 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000381 if (!S->isTypeDependent() && !S->isValueDependent() &&
382 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000383 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000384
385 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000386 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000387};
Mike Stump6d9828c2009-07-17 01:31:16 +0000388
Douglas Gregor898574e2008-12-05 23:32:09 +0000389// FIXME: Add support for dependent-sized array types in C++?
390// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000391static VariableArrayType* FindVA(Type* t) {
392 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
393 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
394 if (vat->getSizeExpr())
395 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000396
Ted Kremenek610a09e2008-09-26 22:58:57 +0000397 t = vt->getElementType().getTypePtr();
398 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000399
Ted Kremenek610a09e2008-09-26 22:58:57 +0000400 return 0;
401}
Mike Stump6d9828c2009-07-17 01:31:16 +0000402
403/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
404/// arbitrary statement. Examples include a single expression or a function
405/// body (compound statement). The ownership of the returned CFG is
406/// transferred to the caller. If CFG construction fails, this method returns
407/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000408CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000409 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000410
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000411 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000412 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000413 if (!Statement)
414 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000415
Ted Kremenek6c52c782010-09-14 23:41:16 +0000416 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000417
418 // Create an empty block that will serve as the exit block for the CFG. Since
419 // this is the first block added to the CFG, it will be implicitly registered
420 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000421 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000422 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000423 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000424
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000425 if (BuildOpts.AddImplicitDtors)
426 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
427 addImplicitDtorsForDestructor(DD);
428
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000429 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000430 CFGBlock *B = addStmt(Statement);
431
432 if (badCFG)
433 return NULL;
434
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000435 // For C++ constructor add initializers to CFG.
436 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
437 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
438 E = CD->init_rend(); I != E; ++I) {
439 B = addInitializer(*I);
440 if (badCFG)
441 return NULL;
442 }
443 }
444
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000445 if (B)
446 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000447
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000448 // Backpatch the gotos whose label -> block mappings we didn't know when we
449 // encountered them.
450 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
451 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000452
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000453 CFGBlock* B = I->Block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000454 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
455 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000456
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000457 // If there is no target for the goto, then we are looking at an
458 // incomplete AST. Handle this by not registering a successor.
459 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000460
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000461 JumpTarget JT = LI->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000462 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000463 AddSuccessor(B, JT.Block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000464 }
465
466 // Add successors to the Indirect Goto Dispatch block (if we have one).
467 if (CFGBlock* B = cfg->getIndirectGotoBlock())
468 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
469 E = AddressTakenLabels.end(); I != E; ++I ) {
470
471 // Lookup the target block.
472 LabelMapTy::iterator LI = LabelMap.find(*I);
473
474 // If there is no target block that contains label, then we are looking
475 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000476 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000477
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000478 AddSuccessor(B, LI->second.Block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000479 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000480
Mike Stump6d9828c2009-07-17 01:31:16 +0000481 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000482 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000483
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000484 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000485}
Mike Stump6d9828c2009-07-17 01:31:16 +0000486
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000487/// createBlock - Used to lazily create blocks that are connected
488/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000489CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000490 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000491 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000492 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000493 return B;
494}
Mike Stump6d9828c2009-07-17 01:31:16 +0000495
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000496/// addInitializer - Add C++ base or member initializer element to CFG.
497CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
498 if (!BuildOpts.AddInitializers)
499 return Block;
500
501 autoCreateBlock();
502 appendInitializer(Block, I);
503
504 if (Expr *Init = I->getInit()) {
505 AddStmtChoice::Kind K = AddStmtChoice::NotAlwaysAdd;
506 if (FieldDecl *FD = I->getMember())
507 if (FD->getType()->isReferenceType())
508 K = AddStmtChoice::AsLValueNotAlwaysAdd;
509
510 return Visit(Init, AddStmtChoice(K));
511 }
512
513 return Block;
514}
515
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000516/// addAutomaticObjDtors - Add to current block automatic objects destructors
517/// for objects in range of local scope positions. Use S as trigger statement
518/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000519void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
520 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000521 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000522 return;
523
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000524 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000525 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000526
527 autoCreateBlock();
528 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000529}
530
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000531/// addImplicitDtorsForDestructor - Add implicit destructors generated for
532/// base and member objects in destructor.
533void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
534 assert (BuildOpts.AddImplicitDtors
535 && "Can be called only when dtors should be added");
536 const CXXRecordDecl *RD = DD->getParent();
537
538 // At the end destroy virtual base objects.
539 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
540 VE = RD->vbases_end(); VI != VE; ++VI) {
541 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
542 if (!CD->hasTrivialDestructor()) {
543 autoCreateBlock();
544 appendBaseDtor(Block, VI);
545 }
546 }
547
548 // Before virtual bases destroy direct base objects.
549 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
550 BE = RD->bases_end(); BI != BE; ++BI) {
551 if (!BI->isVirtual()) {
552 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
553 if (!CD->hasTrivialDestructor()) {
554 autoCreateBlock();
555 appendBaseDtor(Block, BI);
556 }
557 }
558 }
559
560 // First destroy member objects.
561 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
562 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000563 // Check for constant size array. Set type to array element type.
564 QualType QT = FI->getType();
565 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
566 if (AT->getSize() == 0)
567 continue;
568 QT = AT->getElementType();
569 }
570
571 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000572 if (!CD->hasTrivialDestructor()) {
573 autoCreateBlock();
574 appendMemberDtor(Block, *FI);
575 }
576 }
577}
578
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000579/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
580/// way return valid LocalScope object.
581LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
582 if (!Scope) {
583 Scope = cfg->getAllocator().Allocate<LocalScope>();
584 new (Scope) LocalScope(ScopePos);
585 }
586 return Scope;
587}
588
589/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000590/// that should create implicit scope (e.g. if/else substatements).
591void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000592 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000593 return;
594
595 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000596
597 // For compound statement we will be creating explicit scope.
598 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
599 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
600 ; BI != BE; ++BI) {
601 Stmt* SI = *BI;
602 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
603 SI = LS->getSubStmt();
604 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
605 Scope = addLocalScopeForDeclStmt(DS, Scope);
606 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000607 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000608 }
609
610 // For any other statement scope will be implicit and as such will be
611 // interesting only for DeclStmt.
612 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
613 S = LS->getSubStmt();
614 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000615 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000616}
617
618/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
619/// reuse Scope if not NULL.
620LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000621 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000622 if (!BuildOpts.AddImplicitDtors)
623 return Scope;
624
625 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
626 ; DI != DE; ++DI) {
627 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
628 Scope = addLocalScopeForVarDecl(VD, Scope);
629 }
630 return Scope;
631}
632
633/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
634/// create add scope for automatic objects and temporary objects bound to
635/// const reference. Will reuse Scope if not NULL.
636LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000637 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000638 if (!BuildOpts.AddImplicitDtors)
639 return Scope;
640
641 // Check if variable is local.
642 switch (VD->getStorageClass()) {
643 case SC_None:
644 case SC_Auto:
645 case SC_Register:
646 break;
647 default: return Scope;
648 }
649
650 // Check for const references bound to temporary. Set type to pointee.
651 QualType QT = VD->getType();
652 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
653 QT = RT->getPointeeType();
654 if (!QT.isConstQualified())
655 return Scope;
656 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
657 return Scope;
658 }
659
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000660 // Check for constant size array. Set type to array element type.
661 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
662 if (AT->getSize() == 0)
663 return Scope;
664 QT = AT->getElementType();
665 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000666
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000667 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000668 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
669 if (!CD->hasTrivialDestructor()) {
670 // Add the variable to scope
671 Scope = createOrReuseLocalScope(Scope);
672 Scope->addVar(VD);
673 ScopePos = Scope->begin();
674 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000675 return Scope;
676}
677
678/// addLocalScopeAndDtors - For given statement add local scope for it and
679/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
680void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
681 if (!BuildOpts.AddImplicitDtors)
682 return;
683
684 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000685 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000686 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
687}
688
Marcin Swiderski53de1342010-09-30 22:54:37 +0000689/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
690/// automatic storage duration to CFGBlock's elements vector. Insertion will be
691/// performed in place specified with iterator.
692void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
693 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
694 BumpVectorContext& C = cfg->getBumpVectorContext();
695 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
696 while (B != E)
697 I = Blk->insertAutomaticObjDtor(I, *B++, S);
698}
699
700/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
701/// automatic storage duration to CFGBlock's elements vector. Elements will be
702/// appended to physical end of the vector which happens to be logical
703/// beginning.
704void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
705 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
706 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
707}
708
709/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
710/// variables with automatic storage duration to CFGBlock's elements vector.
711/// Elements will be prepended to physical beginning of the vector which
712/// happens to be logical end. Use blocks terminator as statement that specifies
713/// destructors call site.
714void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
715 LocalScope::const_iterator B, LocalScope::const_iterator E) {
716 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
717}
718
Ted Kremenek4f880632009-07-17 22:18:43 +0000719/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000720/// blocks for ternary operators, &&, and ||. We also process "," and
721/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000722CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000723tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000724 if (!S) {
725 badCFG = true;
726 return 0;
727 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000728 switch (S->getStmtClass()) {
729 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000730 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000731
732 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000733 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek4f880632009-07-17 22:18:43 +0000735 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000736 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek4f880632009-07-17 22:18:43 +0000738 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000739 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000740
Ted Kremenek4f880632009-07-17 22:18:43 +0000741 case Stmt::BreakStmtClass:
742 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Ted Kremenek4f880632009-07-17 22:18:43 +0000744 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000745 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000746 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenek4f880632009-07-17 22:18:43 +0000748 case Stmt::CaseStmtClass:
749 return VisitCaseStmt(cast<CaseStmt>(S));
750
751 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000752 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek4f880632009-07-17 22:18:43 +0000754 case Stmt::CompoundStmtClass:
755 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek4f880632009-07-17 22:18:43 +0000757 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000758 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek4f880632009-07-17 22:18:43 +0000760 case Stmt::ContinueStmtClass:
761 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek021c8af2010-01-19 20:40:33 +0000763 case Stmt::CXXCatchStmtClass:
764 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
765
Ted Kremenek47e331e2010-08-28 00:19:02 +0000766 case Stmt::CXXExprWithTemporariesClass: {
767 // FIXME: Handle temporaries. For now, just visit the subexpression
768 // so we don't artificially create extra blocks.
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000769 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000770 }
771
Zhongxing Xua725ed42010-11-01 13:04:58 +0000772 case Stmt::CXXBindTemporaryExprClass:
773 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
774
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000775 case Stmt::CXXConstructExprClass:
776 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
777
Zhongxing Xua725ed42010-11-01 13:04:58 +0000778 case Stmt::CXXFunctionalCastExprClass:
779 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
780
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000781 case Stmt::CXXTemporaryObjectExprClass:
782 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
783
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000784 case Stmt::CXXMemberCallExprClass:
785 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
786
Ted Kremenek021c8af2010-01-19 20:40:33 +0000787 case Stmt::CXXThrowExprClass:
788 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000789
Ted Kremenek021c8af2010-01-19 20:40:33 +0000790 case Stmt::CXXTryStmtClass:
791 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 case Stmt::DeclStmtClass:
794 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek4f880632009-07-17 22:18:43 +0000796 case Stmt::DefaultStmtClass:
797 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek4f880632009-07-17 22:18:43 +0000799 case Stmt::DoStmtClass:
800 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenek4f880632009-07-17 22:18:43 +0000802 case Stmt::ForStmtClass:
803 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Ted Kremenek4f880632009-07-17 22:18:43 +0000805 case Stmt::GotoStmtClass:
806 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek4f880632009-07-17 22:18:43 +0000808 case Stmt::IfStmtClass:
809 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Zhongxing Xua725ed42010-11-01 13:04:58 +0000811 case Stmt::ImplicitCastExprClass:
812 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
813
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 case Stmt::IndirectGotoStmtClass:
815 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Ted Kremenek4f880632009-07-17 22:18:43 +0000817 case Stmt::LabelStmtClass:
818 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek115c1b92010-04-11 17:02:10 +0000820 case Stmt::MemberExprClass:
821 return VisitMemberExpr(cast<MemberExpr>(S), asc);
822
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000824 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
825
Ted Kremenek4f880632009-07-17 22:18:43 +0000826 case Stmt::ObjCAtSynchronizedStmtClass:
827 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenek4f880632009-07-17 22:18:43 +0000829 case Stmt::ObjCAtThrowStmtClass:
830 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek4f880632009-07-17 22:18:43 +0000832 case Stmt::ObjCAtTryStmtClass:
833 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Ted Kremenek4f880632009-07-17 22:18:43 +0000835 case Stmt::ObjCForCollectionStmtClass:
836 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek4f880632009-07-17 22:18:43 +0000838 case Stmt::ParenExprClass:
839 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000840 goto tryAgain;
841
Ted Kremenek4f880632009-07-17 22:18:43 +0000842 case Stmt::NullStmtClass:
843 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek4f880632009-07-17 22:18:43 +0000845 case Stmt::ReturnStmtClass:
846 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek4f880632009-07-17 22:18:43 +0000848 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000849 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek4f880632009-07-17 22:18:43 +0000851 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000852 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek4f880632009-07-17 22:18:43 +0000854 case Stmt::SwitchStmtClass:
855 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek4f880632009-07-17 22:18:43 +0000857 case Stmt::WhileStmtClass:
858 return VisitWhileStmt(cast<WhileStmt>(S));
859 }
860}
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenek852274d2009-12-16 03:18:58 +0000862CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
863 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000864 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000865 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000866 }
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek4f880632009-07-17 22:18:43 +0000868 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000869}
Mike Stump6d9828c2009-07-17 01:31:16 +0000870
Ted Kremenek4f880632009-07-17 22:18:43 +0000871/// VisitChildren - Visit the children of a Stmt.
872CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
873 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000874 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000875 E = Terminator->child_end(); I != E; ++I) {
876 if (*I) B = Visit(*I);
877 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000878 return B;
879}
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek852274d2009-12-16 03:18:58 +0000881CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
882 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000883 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000884
Ted Kremenek852274d2009-12-16 03:18:58 +0000885 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000886 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000887 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000888 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000889
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000890 return Block;
891}
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek852274d2009-12-16 03:18:58 +0000893CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
894 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000895 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000896 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000897 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000899 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000900 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Ted Kremenek4f880632009-07-17 22:18:43 +0000902 // create the block evaluating the LHS
903 CFGBlock* LHSBlock = createBlock(false);
904 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Ted Kremenek4f880632009-07-17 22:18:43 +0000906 // create the block evaluating the RHS
907 Succ = ConfluenceBlock;
908 Block = NULL;
909 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000910
911 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000912 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000913 return 0;
914 }
915 else {
916 // Create an empty block for cases where the RHS doesn't require
917 // any explicit statements in the CFG.
918 RHSBlock = createBlock();
919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Mike Stump00998a02009-07-23 23:25:26 +0000921 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000922 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000923 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000924 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000925
Ted Kremenek4f880632009-07-17 22:18:43 +0000926 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000927 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000928 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
929 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000930 } else {
John McCall2de56d12010-08-25 11:45:40 +0000931 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000932 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
933 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Ted Kremenek4f880632009-07-17 22:18:43 +0000936 // Generate the blocks for evaluating the LHS.
937 Block = LHSBlock;
938 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000939 }
John McCall2de56d12010-08-25 11:45:40 +0000940 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000941 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000942 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000943 addStmt(B->getRHS());
944 return addStmt(B->getLHS());
945 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000946 else if (B->isAssignmentOp()) {
947 if (asc.alwaysAdd()) {
948 autoCreateBlock();
949 AppendStmt(Block, B, asc);
950 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000951
Marcin Swiderskie1667192010-10-24 08:21:40 +0000952 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
953 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +0000954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Marcin Swiderskie1667192010-10-24 08:21:40 +0000956 if (asc.alwaysAdd()) {
957 autoCreateBlock();
958 AppendStmt(Block, B, asc);
959 }
960
Zhongxing Xua1898dd2010-10-27 03:23:10 +0000961 CFGBlock *RBlock = Visit(B->getRHS());
962 CFGBlock *LBlock = Visit(B->getLHS());
963 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
964 // containing a DoStmt, and the LHS doesn't create a new block, then we should
965 // return RBlock. Otherwise we'll incorrectly return NULL.
966 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000967}
968
Ted Kremenek852274d2009-12-16 03:18:58 +0000969CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
970 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000971 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000972 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000973 }
974 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000975}
976
Ted Kremenek4f880632009-07-17 22:18:43 +0000977CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
978 // "break" is a control-flow statement. Thus we stop processing the current
979 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000980 if (badCFG)
981 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenek4f880632009-07-17 22:18:43 +0000983 // Now create a new block that ends with the break statement.
984 Block = createBlock(false);
985 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenek4f880632009-07-17 22:18:43 +0000987 // If there is no target for the break, then we are looking at an incomplete
988 // AST. This means that the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000989 if (BreakJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000990 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000991 AddSuccessor(Block, BreakJumpTarget.Block);
992 } else
Ted Kremenek4f880632009-07-17 22:18:43 +0000993 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
995
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000996 return Block;
997}
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Mike Stump4c45aa12010-01-21 15:20:48 +0000999static bool CanThrow(Expr *E) {
1000 QualType Ty = E->getType();
1001 if (Ty->isFunctionPointerType())
1002 Ty = Ty->getAs<PointerType>()->getPointeeType();
1003 else if (Ty->isBlockPointerType())
1004 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001005
Mike Stump4c45aa12010-01-21 15:20:48 +00001006 const FunctionType *FT = Ty->getAs<FunctionType>();
1007 if (FT) {
1008 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1009 if (Proto->hasEmptyExceptionSpec())
1010 return false;
1011 }
1012 return true;
1013}
1014
Ted Kremenek852274d2009-12-16 03:18:58 +00001015CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001016 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001017 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001018 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001019 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001020 }
Mike Stump24556362009-07-25 21:26:53 +00001021
Mike Stump4c45aa12010-01-21 15:20:48 +00001022 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001023
1024 // Languages without exceptions are assumed to not throw.
1025 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001026 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001027 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001028 }
1029
1030 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001031 if (FD->hasAttr<NoReturnAttr>())
1032 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001033 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001034 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001035 }
Mike Stump24556362009-07-25 21:26:53 +00001036
Mike Stump4c45aa12010-01-21 15:20:48 +00001037 if (!CanThrow(C->getCallee()))
1038 AddEHEdge = false;
1039
Zhongxing Xufc61d942010-06-03 06:23:18 +00001040 if (!NoReturn && !AddEHEdge) {
1041 if (asc.asLValue())
1042 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1043 else
1044 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Mike Stump079bd722010-01-19 22:00:14 +00001047 if (Block) {
1048 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001049 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001050 return 0;
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Mike Stump079bd722010-01-19 22:00:14 +00001053 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +00001054 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001055
Mike Stump079bd722010-01-19 22:00:14 +00001056 if (NoReturn) {
1057 // Wire this to the exit block directly.
1058 AddSuccessor(Block, &cfg->getExit());
1059 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001060 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001061 // Add exceptional edges.
1062 if (TryTerminatedBlock)
1063 AddSuccessor(Block, TryTerminatedBlock);
1064 else
1065 AddSuccessor(Block, &cfg->getExit());
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Mike Stump24556362009-07-25 21:26:53 +00001068 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001069}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001070
Ted Kremenek852274d2009-12-16 03:18:58 +00001071CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1072 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001073 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001074 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001075 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001076 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Ted Kremenek115c1b92010-04-11 17:02:10 +00001078 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1079 : AddStmtChoice::AlwaysAdd;
1080
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001081 Succ = ConfluenceBlock;
1082 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +00001083 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001084 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001085 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001087 Succ = ConfluenceBlock;
1088 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +00001089 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001090 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001091 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001093 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001094 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001095 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001096 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1097 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001098 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001099 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001100}
Mike Stump1eb44332009-09-09 15:08:12 +00001101
1102
1103CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001104 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001105 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001106
1107 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1108 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001109 // If we hit a segment of code just containing ';' (NullStmts), we can
1110 // get a null block back. In such cases, just use the LastBlock
1111 if (CFGBlock *newBlock = addStmt(*I))
1112 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001114 if (badCFG)
1115 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001116 }
Mike Stump079bd722010-01-19 22:00:14 +00001117
Ted Kremenek4f880632009-07-17 22:18:43 +00001118 return LastBlock;
1119}
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Ted Kremenek852274d2009-12-16 03:18:58 +00001121CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1122 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001123 // Create the confluence block that will "merge" the results of the ternary
1124 // expression.
1125 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001126 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001127 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001128 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Ted Kremenek115c1b92010-04-11 17:02:10 +00001130 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1131 : AddStmtChoice::AlwaysAdd;
1132
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001133 // Create a block for the LHS expression if there is an LHS expression. A
1134 // GCC extension allows LHS to be NULL, causing the condition to be the
1135 // value that is returned instead.
1136 // e.g: x ?: y is shorthand for: x ? x : y;
1137 Succ = ConfluenceBlock;
1138 Block = NULL;
1139 CFGBlock* LHSBlock = NULL;
1140 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +00001141 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001142 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001143 return 0;
1144 Block = NULL;
1145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001147 // Create the block for the RHS expression.
1148 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +00001149 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001150 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001151 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001153 // Create the block that will contain the condition.
1154 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Mike Stump00998a02009-07-23 23:25:26 +00001156 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001157 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001158 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001159 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001160 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +00001161 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001162 // If we know the condition is false, add NULL as the successor for
1163 // the block containing the condition. In this case, the confluence
1164 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001165 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +00001166 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001167 } else {
1168 // If we have no LHS expression, add the ConfluenceBlock as a direct
1169 // successor for the block containing the condition. Moreover, we need to
1170 // reverse the order of the predecessors in the ConfluenceBlock because
1171 // the RHSBlock will have been added to the succcessors already, and we
1172 // want the first predecessor to the the block containing the expression
1173 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001174 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +00001175 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001176 std::reverse(ConfluenceBlock->pred_begin(),
1177 ConfluenceBlock->pred_end());
1178 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001181 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001182 Block->setTerminator(C);
1183 return addStmt(C->getCond());
1184}
1185
Ted Kremenek4f880632009-07-17 22:18:43 +00001186CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
1187 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001188
Ted Kremenek4f880632009-07-17 22:18:43 +00001189 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001190 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001191 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +00001192 }
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Ted Kremenek4f880632009-07-17 22:18:43 +00001194 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenek4f880632009-07-17 22:18:43 +00001196 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1197 typedef llvm::SmallVector<Decl*,10> BufTy;
1198 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Ted Kremenek4f880632009-07-17 22:18:43 +00001200 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1201 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1202 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1203 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Ted Kremenek4f880632009-07-17 22:18:43 +00001205 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1206 // automatically freed with the CFG.
1207 DeclGroupRef DG(*I);
1208 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001209 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001210 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Ted Kremenek4f880632009-07-17 22:18:43 +00001212 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001213 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001214 B = VisitDeclSubExpr(D);
1215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
1217 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001218}
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Ted Kremenek4f880632009-07-17 22:18:43 +00001220/// VisitDeclSubExpr - Utility method to add block-level expressions for
1221/// initializers in Decls.
1222CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
1223 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +00001224
Ted Kremenek4f880632009-07-17 22:18:43 +00001225 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Ted Kremenek4f880632009-07-17 22:18:43 +00001227 if (!VD)
1228 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Ted Kremenek4f880632009-07-17 22:18:43 +00001230 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek4f880632009-07-17 22:18:43 +00001232 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +00001233 AddStmtChoice::Kind k =
1234 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
1235 : AddStmtChoice::NotAlwaysAdd;
1236 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +00001237 }
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Ted Kremenek4f880632009-07-17 22:18:43 +00001239 // If the type of VD is a VLA, then we must process its size expressions.
1240 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1241 VA = FindVA(VA->getElementType().getTypePtr()))
1242 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001244 // Remove variable from local scope.
1245 if (ScopePos && VD == *ScopePos)
1246 ++ScopePos;
1247
Ted Kremenek4f880632009-07-17 22:18:43 +00001248 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001249}
1250
1251CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001252 // We may see an if statement in the middle of a basic block, or it may be the
1253 // first statement we are processing. In either case, we create a new basic
1254 // block. First, we create the blocks for the then...else statements, and
1255 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001256 // middle of a block, we stop processing that block. That block is then the
1257 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001258
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001259 // Save local scope position because in case of condition variable ScopePos
1260 // won't be restored when traversing AST.
1261 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1262
1263 // Create local scope for possible condition variable.
1264 // Store scope position. Add implicit destructor.
1265 if (VarDecl* VD = I->getConditionVariable()) {
1266 LocalScope::const_iterator BeginScopePos = ScopePos;
1267 addLocalScopeForVarDecl(VD);
1268 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1269 }
1270
Mike Stump6d9828c2009-07-17 01:31:16 +00001271 // The block we were proccessing is now finished. Make it the successor
1272 // block.
1273 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001274 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001275 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001276 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001277 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001278
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001279 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001281
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001282 if (Stmt* Else = I->getElse()) {
1283 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001284
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001285 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001286 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001287 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001288
1289 // If branch is not a compound statement create implicit scope
1290 // and add destructors.
1291 if (!isa<CompoundStmt>(Else))
1292 addLocalScopeAndDtors(Else);
1293
Ted Kremenek4f880632009-07-17 22:18:43 +00001294 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001295
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001296 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1297 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001298 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001299 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001300 return 0;
1301 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001302 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001303
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001304 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305 CFGBlock* ThenBlock;
1306 {
1307 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001308 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001309 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001310 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001311
1312 // If branch is not a compound statement create implicit scope
1313 // and add destructors.
1314 if (!isa<CompoundStmt>(Then))
1315 addLocalScopeAndDtors(Then);
1316
Ted Kremenek4f880632009-07-17 22:18:43 +00001317 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001318
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001319 if (!ThenBlock) {
1320 // We can reach here if the "then" body has all NullStmts.
1321 // Create an empty block so we can distinguish between true and false
1322 // branches in path-sensitive analyses.
1323 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001324 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001325 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001326 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001327 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001328 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001329 }
1330
Mike Stump6d9828c2009-07-17 01:31:16 +00001331 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001332 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001333
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001334 // Set the terminator of the new block to the If statement.
1335 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001336
Mike Stump00998a02009-07-23 23:25:26 +00001337 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001338 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001339
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001340 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001341 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1342 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001343
1344 // Add the condition as the last statement in the new block. This may create
1345 // new blocks as the condition may contain control-flow. Any newly created
1346 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001347 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001348
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001349 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1350 // and the condition variable initialization to the CFG.
1351 if (VarDecl *VD = I->getConditionVariable()) {
1352 if (Expr *Init = VD->getInit()) {
1353 autoCreateBlock();
1354 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1355 addStmt(Init);
1356 }
1357 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001358
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001359 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360}
Mike Stump6d9828c2009-07-17 01:31:16 +00001361
1362
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001363CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001364 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001365 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001366 // NOTE: If a "return" appears in the middle of a block, this means that the
1367 // code afterwards is DEAD (unreachable). We still keep a basic block
1368 // for that code; a simple "mark-and-sweep" from the entry block will be
1369 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001370
1371 // Create the new block.
1372 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001373
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001374 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001375 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001376 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001377
1378 // Add the return statement to the block. This may create new blocks if R
1379 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001380 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001381}
1382
1383CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1384 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001385 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001386 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
Ted Kremenek4f880632009-07-17 22:18:43 +00001388 if (!LabelBlock) // This can happen when the body is empty, i.e.
1389 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001390
Ted Kremenek4f880632009-07-17 22:18:43 +00001391 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001392 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001393
1394 // Labels partition blocks, so this is the end of the basic block we were
1395 // processing (L is the block's label). Because this is label (and we have
1396 // already processed the substatement) there is no extra control-flow to worry
1397 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001398 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001399 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001400 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001401
1402 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001403 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001404
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001405 // This block is now the implicit successor of other blocks.
1406 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001407
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001408 return LabelBlock;
1409}
1410
1411CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001412 // Goto is a control-flow statement. Thus we stop processing the current
1413 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001414
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001415 Block = createBlock(false);
1416 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001417
1418 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001420
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001421 if (I == LabelMap.end())
1422 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001423 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1424 else {
1425 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001426 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001427 AddSuccessor(Block, JT.Block);
1428 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429
Mike Stump6d9828c2009-07-17 01:31:16 +00001430 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001431}
1432
1433CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001434 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001435
Marcin Swiderski47575f12010-10-01 01:38:14 +00001436 // Save local scope position because in case of condition variable ScopePos
1437 // won't be restored when traversing AST.
1438 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1439
1440 // Create local scope for init statement and possible condition variable.
1441 // Add destructor for init statement and condition variable.
1442 // Store scope position for continue statement.
1443 if (Stmt* Init = F->getInit())
1444 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001445 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1446
Marcin Swiderski47575f12010-10-01 01:38:14 +00001447 if (VarDecl* VD = F->getConditionVariable())
1448 addLocalScopeForVarDecl(VD);
1449 LocalScope::const_iterator ContinueScopePos = ScopePos;
1450
1451 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1452
Mike Stumpfefb9f72009-07-21 01:12:51 +00001453 // "for" is a control-flow statement. Thus we stop processing the current
1454 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001455 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001456 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001457 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001458 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001459 } else
1460 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001461
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001462 // Save the current value for the break targets.
1463 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001464 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001465 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001466
Mike Stump6d9828c2009-07-17 01:31:16 +00001467 // Because of short-circuit evaluation, the condition of the loop can span
1468 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1469 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001470 CFGBlock* ExitConditionBlock = createBlock(false);
1471 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001473 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001474 ExitConditionBlock->setTerminator(F);
1475
1476 // Now add the actual condition to the condition block. Because the condition
1477 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001478 if (Stmt* C = F->getCond()) {
1479 Block = ExitConditionBlock;
1480 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001481 assert(Block == EntryConditionBlock ||
1482 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001483
1484 // If this block contains a condition variable, add both the condition
1485 // variable and initializer to the CFG.
1486 if (VarDecl *VD = F->getConditionVariable()) {
1487 if (Expr *Init = VD->getInit()) {
1488 autoCreateBlock();
1489 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1490 EntryConditionBlock = addStmt(Init);
1491 assert(Block == EntryConditionBlock);
1492 }
1493 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001494
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001495 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001496 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001497 return 0;
1498 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001499 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001500
Mike Stump6d9828c2009-07-17 01:31:16 +00001501 // The condition block is the implicit successor for the loop body as well as
1502 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001503 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001504
Mike Stump00998a02009-07-23 23:25:26 +00001505 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001506 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Mike Stump00998a02009-07-23 23:25:26 +00001508 if (F->getCond())
1509 KnownVal = TryEvaluateBool(F->getCond());
1510
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001511 // Now create the loop body.
1512 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001513 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001514
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001515 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001516 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1517 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001518
Ted Kremenekaf603f72007-08-30 18:39:40 +00001519 // Create a new block to contain the (bottom) of the loop body.
1520 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001521
1522 // Loop body should end with destructor of Condition variable (if any).
1523 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001524
Ted Kremeneke9334502008-09-04 21:48:47 +00001525 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001526 // Generate increment code in its own basic block. This is the target of
1527 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001528 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001529 } else {
1530 // No increment code. Create a special, empty, block that is used as the
1531 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001532 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001533 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001534 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
Ted Kremenek3575f842009-04-28 00:51:56 +00001536 // Finish up the increment (or empty) block if it hasn't been already.
1537 if (Block) {
1538 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001539 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001540 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001541 Block = 0;
1542 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001543
Marcin Swiderski47575f12010-10-01 01:38:14 +00001544 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001545
Ted Kremenek3575f842009-04-28 00:51:56 +00001546 // The starting block for the loop increment is the block that should
1547 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001548 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001549
Marcin Swiderski47575f12010-10-01 01:38:14 +00001550 // If body is not a compound statement create implicit scope
1551 // and add destructors.
1552 if (!isa<CompoundStmt>(F->getBody()))
1553 addLocalScopeAndDtors(F->getBody());
1554
Mike Stump6d9828c2009-07-17 01:31:16 +00001555 // Now populate the body block, and in the process create new blocks as we
1556 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001557 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001558
1559 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001560 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001561 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001562 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001563
Ted Kremenek941fde82009-07-24 04:47:11 +00001564 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001565 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001566 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
Ted Kremenek941fde82009-07-24 04:47:11 +00001568 // Link up the condition block with the code that follows the loop. (the
1569 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001570 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001572 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001573 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001574 if (Stmt* I = F->getInit()) {
1575 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001576 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001577 } else {
1578 // There is no loop initialization. We are thus basically a while loop.
1579 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001580 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001581 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001582 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001583 }
1584}
1585
Ted Kremenek115c1b92010-04-11 17:02:10 +00001586CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1587 if (asc.alwaysAdd()) {
1588 autoCreateBlock();
1589 AppendStmt(Block, M, asc);
1590 }
1591 return Visit(M->getBase(),
1592 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1593 : AddStmtChoice::AsLValueNotAlwaysAdd);
1594}
1595
Ted Kremenek514de5a2008-11-11 17:10:00 +00001596CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1597 // Objective-C fast enumeration 'for' statements:
1598 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1599 //
1600 // for ( Type newVariable in collection_expression ) { statements }
1601 //
1602 // becomes:
1603 //
1604 // prologue:
1605 // 1. collection_expression
1606 // T. jump to loop_entry
1607 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001608 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001609 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1610 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1611 // TB:
1612 // statements
1613 // T. jump to loop_entry
1614 // FB:
1615 // what comes after
1616 //
1617 // and
1618 //
1619 // Type existingItem;
1620 // for ( existingItem in expression ) { statements }
1621 //
1622 // becomes:
1623 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001624 // the same with newVariable replaced with existingItem; the binding works
1625 // the same except that for one ObjCForCollectionStmt::getElement() returns
1626 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001627 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001628
Ted Kremenek514de5a2008-11-11 17:10:00 +00001629 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001630
Ted Kremenek514de5a2008-11-11 17:10:00 +00001631 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001632 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001633 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001634 LoopSuccessor = Block;
1635 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001636 } else
1637 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001638
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001639 // Build the condition blocks.
1640 CFGBlock* ExitConditionBlock = createBlock(false);
1641 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001642
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001643 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001644 ExitConditionBlock->setTerminator(S);
1645
1646 // The last statement in the block should be the ObjCForCollectionStmt, which
1647 // performs the actual binding to 'element' and determines if there are any
1648 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001649 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001650 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001652 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001653 // generate new blocks as necesary. We DON'T add the statement by default to
1654 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001655 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001656 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001657 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001658 return 0;
1659 Block = 0;
1660 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001661
1662 // The condition block is the implicit successor for the loop body as well as
1663 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001664 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001665
Ted Kremenek514de5a2008-11-11 17:10:00 +00001666 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001667 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001668 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001669 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1670 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1671 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001672
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001673 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1674 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001675
Ted Kremenek4f880632009-07-17 22:18:43 +00001676 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001677
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001678 if (!BodyBlock)
1679 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001680 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001681 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001682 return 0;
1683 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001684
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001685 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001686 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001687 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001688
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001689 // Link up the condition block with the code that follows the loop.
1690 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001691 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001692
Ted Kremenek514de5a2008-11-11 17:10:00 +00001693 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001694 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001695 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001696}
1697
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001698CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1699 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001700
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001701 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001702 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001703
Ted Kremenekda5348e2009-05-05 23:11:51 +00001704 // The sync body starts its own basic block. This makes it a little easier
1705 // for diagnostic clients.
1706 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001707 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001708 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001709
Ted Kremenekda5348e2009-05-05 23:11:51 +00001710 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001711 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001712 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001713
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001714 // Add the @synchronized to the CFG.
1715 autoCreateBlock();
1716 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1717
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001718 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001719 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001720}
Mike Stump6d9828c2009-07-17 01:31:16 +00001721
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001722CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001723 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001724 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001725}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001726
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001727CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001728 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001729
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001730 // Save local scope position because in case of condition variable ScopePos
1731 // won't be restored when traversing AST.
1732 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1733
1734 // Create local scope for possible condition variable.
1735 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001736 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001737 if (VarDecl* VD = W->getConditionVariable()) {
1738 addLocalScopeForVarDecl(VD);
1739 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1740 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001741
Mike Stumpfefb9f72009-07-21 01:12:51 +00001742 // "while" is a control-flow statement. Thus we stop processing the current
1743 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001744 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001745 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001746 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001747 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001748 } else
1749 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001750
1751 // Because of short-circuit evaluation, the condition of the loop can span
1752 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1753 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001754 CFGBlock* ExitConditionBlock = createBlock(false);
1755 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001756
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001757 // Set the terminator for the "exit" condition block.
1758 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001759
1760 // Now add the actual condition to the condition block. Because the condition
1761 // itself may contain control-flow, new blocks may be created. Thus we update
1762 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001763 if (Stmt* C = W->getCond()) {
1764 Block = ExitConditionBlock;
1765 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001766 // The condition might finish the current 'Block'.
1767 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001768
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001769 // If this block contains a condition variable, add both the condition
1770 // variable and initializer to the CFG.
1771 if (VarDecl *VD = W->getConditionVariable()) {
1772 if (Expr *Init = VD->getInit()) {
1773 autoCreateBlock();
1774 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1775 EntryConditionBlock = addStmt(Init);
1776 assert(Block == EntryConditionBlock);
1777 }
1778 }
1779
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001780 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001781 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001782 return 0;
1783 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001784 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001785
1786 // The condition block is the implicit successor for the loop body as well as
1787 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001788 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001789
Mike Stump00998a02009-07-23 23:25:26 +00001790 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001791 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001792
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001793 // Process the loop body.
1794 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001795 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001796
1797 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001798 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1799 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1800 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001801
Mike Stump6d9828c2009-07-17 01:31:16 +00001802 // Create an empty block to represent the transition block for looping back
1803 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001804 Block = 0;
1805 assert(Succ == EntryConditionBlock);
1806 Succ = createBlock();
1807 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001808 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001809
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001810 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001811 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001813 // NULL out Block to force lazy instantiation of blocks for the body.
1814 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001816 // Loop body should end with destructor of Condition variable (if any).
1817 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1818
1819 // If body is not a compound statement create implicit scope
1820 // and add destructors.
1821 if (!isa<CompoundStmt>(W->getBody()))
1822 addLocalScopeAndDtors(W->getBody());
1823
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001824 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001825 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001826
Ted Kremenekaf603f72007-08-30 18:39:40 +00001827 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001828 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001829 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001830 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001831 return 0;
1832 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001833
Ted Kremenek941fde82009-07-24 04:47:11 +00001834 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001835 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001836 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001837
Ted Kremenek941fde82009-07-24 04:47:11 +00001838 // Link up the condition block with the code that follows the loop. (the
1839 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001840 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001841
1842 // There can be no more statements in the condition block since we loop back
1843 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001844 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001846 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001847 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001848 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001849}
Mike Stump1eb44332009-09-09 15:08:12 +00001850
1851
Ted Kremenek4f880632009-07-17 22:18:43 +00001852CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1853 // FIXME: For now we pretend that @catch and the code it contains does not
1854 // exit.
1855 return Block;
1856}
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Ted Kremenek2fda5042008-12-09 20:20:09 +00001858CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1859 // FIXME: This isn't complete. We basically treat @throw like a return
1860 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001861
Ted Kremenek6c249722009-09-24 18:45:41 +00001862 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001863 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001864 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001865
Ted Kremenek2fda5042008-12-09 20:20:09 +00001866 // Create the new block.
1867 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek2fda5042008-12-09 20:20:09 +00001869 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001870 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001871
1872 // Add the statement to the block. This may create new blocks if S contains
1873 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001874 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001875}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001876
Mike Stump0979d802009-07-22 22:56:04 +00001877CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001878 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001879 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001880 return 0;
1881
1882 // Create the new block.
1883 Block = createBlock(false);
1884
Mike Stump5d1d2022010-01-19 02:20:09 +00001885 if (TryTerminatedBlock)
1886 // The current try statement is the only successor.
1887 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001888 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001889 // otherwise the Exit block is the only successor.
1890 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001891
1892 // Add the statement to the block. This may create new blocks if S contains
1893 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001894 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001895}
1896
Ted Kremenek4f880632009-07-17 22:18:43 +00001897CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001898 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001899
Mike Stump8f9893a2009-07-21 01:27:50 +00001900 // "do...while" is a control-flow statement. Thus we stop processing the
1901 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001902 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001903 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001904 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001905 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001906 } else
1907 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001908
1909 // Because of short-circuit evaluation, the condition of the loop can span
1910 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1911 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001912 CFGBlock* ExitConditionBlock = createBlock(false);
1913 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001914
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001915 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001916 ExitConditionBlock->setTerminator(D);
1917
1918 // Now add the actual condition to the condition block. Because the condition
1919 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001920 if (Stmt* C = D->getCond()) {
1921 Block = ExitConditionBlock;
1922 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001923 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001924 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001925 return 0;
1926 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001927 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001928
Ted Kremenek54827132008-02-27 07:20:00 +00001929 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001930 Succ = EntryConditionBlock;
1931
Mike Stump00998a02009-07-23 23:25:26 +00001932 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001933 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001934
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001935 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001936 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001937 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001938 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001939
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001940 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001941 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1942 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1943 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001944
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001945 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001946 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001947
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001948 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001949 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001950
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001951 // NULL out Block to force lazy instantiation of blocks for the body.
1952 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001953
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001954 // If body is not a compound statement create implicit scope
1955 // and add destructors.
1956 if (!isa<CompoundStmt>(D->getBody()))
1957 addLocalScopeAndDtors(D->getBody());
1958
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001959 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001960 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
Ted Kremenekaf603f72007-08-30 18:39:40 +00001962 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001963 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001964 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001965 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001966 return 0;
1967 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenekd173dc72010-08-17 20:59:56 +00001969 if (!KnownVal.isFalse()) {
1970 // Add an intermediate block between the BodyBlock and the
1971 // ExitConditionBlock to represent the "loop back" transition. Create an
1972 // empty block to represent the transition block for looping back to the
1973 // head of the loop.
1974 // FIXME: Can we do this more efficiently without adding another block?
1975 Block = NULL;
1976 Succ = BodyBlock;
1977 CFGBlock *LoopBackBlock = createBlock();
1978 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001979
Ted Kremenekd173dc72010-08-17 20:59:56 +00001980 // Add the loop body entry as a successor to the condition.
1981 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1982 }
1983 else
1984 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001985 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
Ted Kremenek941fde82009-07-24 04:47:11 +00001987 // Link up the condition block with the code that follows the loop.
1988 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001989 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001990
1991 // There can be no more statements in the body block(s) since we loop back to
1992 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001993 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001995 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001996 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001997 return BodyBlock;
1998}
1999
2000CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2001 // "continue" is a control-flow statement. Thus we stop processing the
2002 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002003 if (badCFG)
2004 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002005
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002006 // Now create a new block that ends with the continue statement.
2007 Block = createBlock(false);
2008 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002009
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002010 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002011 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002012 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00002013 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002014 AddSuccessor(Block, ContinueJumpTarget.Block);
2015 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002016 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002017
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002018 return Block;
2019}
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002021CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002022 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002023
Ted Kremenek852274d2009-12-16 03:18:58 +00002024 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002025 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002026 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002027 }
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Ted Kremenek4f880632009-07-17 22:18:43 +00002029 // VLA types have expressions that must be evaluated.
2030 if (E->isArgumentType()) {
2031 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2032 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2033 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002034 }
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Mike Stump6d9828c2009-07-17 01:31:16 +00002036 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002037}
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Ted Kremenek4f880632009-07-17 22:18:43 +00002039/// VisitStmtExpr - Utility method to handle (nested) statement
2040/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002041CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2042 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002043 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002044 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002045 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002046 return VisitCompoundStmt(SE->getSubStmt());
2047}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002048
Ted Kremenek411cdee2008-04-16 21:10:48 +00002049CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002050 // "switch" is a control-flow statement. Thus we stop processing the current
2051 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002052 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002053
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002054 // Save local scope position because in case of condition variable ScopePos
2055 // won't be restored when traversing AST.
2056 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2057
2058 // Create local scope for possible condition variable.
2059 // Store scope position. Add implicit destructor.
2060 if (VarDecl* VD = Terminator->getConditionVariable()) {
2061 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2062 addLocalScopeForVarDecl(VD);
2063 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2064 }
2065
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002066 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002067 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002068 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002069 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002070 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002071
2072 // Save the current "switch" context.
2073 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002074 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002075 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002076
Mike Stump6d9828c2009-07-17 01:31:16 +00002077 // Set the "default" case to be the block after the switch statement. If the
2078 // switch statement contains a "default:", this value will be overwritten with
2079 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002080 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002081
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002082 // Create a new block that will contain the switch statement.
2083 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002084
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002085 // Now process the switch body. The code after the switch is the implicit
2086 // successor.
2087 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002088 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002089
2090 // When visiting the body, the case statements should automatically get linked
2091 // up to the switch. We also don't keep a pointer to the body, since all
2092 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002093 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002094 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002095
2096 // If body is not a compound statement create implicit scope
2097 // and add destructors.
2098 if (!isa<CompoundStmt>(Terminator->getBody()))
2099 addLocalScopeAndDtors(Terminator->getBody());
2100
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002101 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002102 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002103 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002104 return 0;
2105 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002106
Mike Stump6d9828c2009-07-17 01:31:16 +00002107 // If we have no "default:" case, the default transition is to the code
2108 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002109 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002111 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002112 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002113 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002114 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002115 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002116
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002117 // Finally, if the SwitchStmt contains a condition variable, add both the
2118 // SwitchStmt and the condition variable initialization to the CFG.
2119 if (VarDecl *VD = Terminator->getConditionVariable()) {
2120 if (Expr *Init = VD->getInit()) {
2121 autoCreateBlock();
2122 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2123 addStmt(Init);
2124 }
2125 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002126
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002127 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002128}
2129
Ted Kremenek4f880632009-07-17 22:18:43 +00002130CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002131 // CaseStmts are essentially labels, so they are the first statement in a
2132 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002133 CFGBlock *TopBlock = 0, *LastBlock = 0;
2134
2135 if (Stmt *Sub = CS->getSubStmt()) {
2136 // For deeply nested chains of CaseStmts, instead of doing a recursion
2137 // (which can blow out the stack), manually unroll and create blocks
2138 // along the way.
2139 while (isa<CaseStmt>(Sub)) {
2140 CFGBlock *CurrentBlock = createBlock(false);
2141 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002142
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002143 if (TopBlock)
2144 AddSuccessor(LastBlock, CurrentBlock);
2145 else
2146 TopBlock = CurrentBlock;
2147
2148 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2149 LastBlock = CurrentBlock;
2150
2151 CS = cast<CaseStmt>(Sub);
2152 Sub = CS->getSubStmt();
2153 }
2154
2155 addStmt(Sub);
2156 }
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002158 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002159 if (!CaseBlock)
2160 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002161
2162 // Cases statements partition blocks, so this is the top of the basic block we
2163 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002164 CaseBlock->setLabel(CS);
2165
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002166 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002167 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002168
2169 // Add this block to the list of successors for the block with the switch
2170 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002171 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002172 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002173
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002174 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2175 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002176
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002177 if (TopBlock) {
2178 AddSuccessor(LastBlock, CaseBlock);
2179 Succ = TopBlock;
2180 }
2181 else {
2182 // This block is now the implicit successor of other blocks.
2183 Succ = CaseBlock;
2184 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002185
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002186 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002187}
Mike Stump6d9828c2009-07-17 01:31:16 +00002188
Ted Kremenek411cdee2008-04-16 21:10:48 +00002189CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002190 if (Terminator->getSubStmt())
2191 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002192
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002193 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002194
2195 if (!DefaultCaseBlock)
2196 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002197
2198 // Default statements partition blocks, so this is the top of the basic block
2199 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002200 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002202 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002203 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002204
Mike Stump6d9828c2009-07-17 01:31:16 +00002205 // Unlike case statements, we don't add the default block to the successors
2206 // for the switch statement immediately. This is done when we finish
2207 // processing the switch statement. This allows for the default case
2208 // (including a fall-through to the code after the switch statement) to always
2209 // be the last successor of a switch-terminated block.
2210
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002211 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2212 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002213
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002214 // This block is now the implicit successor of other blocks.
2215 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002216
2217 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002218}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002219
Mike Stump5d1d2022010-01-19 02:20:09 +00002220CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2221 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2222 // current block.
2223 CFGBlock* TrySuccessor = NULL;
2224
2225 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002226 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002227 return 0;
2228 TrySuccessor = Block;
2229 } else TrySuccessor = Succ;
2230
Mike Stumpa1f93632010-01-20 01:15:34 +00002231 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002232
2233 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002234 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002235 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002236 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002237
Mike Stumpa1f93632010-01-20 01:15:34 +00002238 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002239 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2240 // The code after the try is the implicit successor.
2241 Succ = TrySuccessor;
2242 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002243 if (CS->getExceptionDecl() == 0) {
2244 HasCatchAll = true;
2245 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002246 Block = NULL;
2247 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2248 if (CatchBlock == 0)
2249 return 0;
2250 // Add this block to the list of successors for the block with the try
2251 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002252 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002253 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002254 if (!HasCatchAll) {
2255 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002256 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002257 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002258 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002259 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002260
2261 // The code after the try is the implicit successor.
2262 Succ = TrySuccessor;
2263
Mike Stumpf00cca52010-01-20 01:30:58 +00002264 // Save the current "try" context.
2265 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2266 TryTerminatedBlock = NewTryTerminatedBlock;
2267
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002268 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002269 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002270 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002271 return Block;
2272}
2273
2274CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2275 // CXXCatchStmt are treated like labels, so they are the first statement in a
2276 // block.
2277
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002278 // Save local scope position because in case of exception variable ScopePos
2279 // won't be restored when traversing AST.
2280 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2281
2282 // Create local scope for possible exception variable.
2283 // Store scope position. Add implicit destructor.
2284 if (VarDecl* VD = CS->getExceptionDecl()) {
2285 LocalScope::const_iterator BeginScopePos = ScopePos;
2286 addLocalScopeForVarDecl(VD);
2287 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2288 }
2289
Mike Stump5d1d2022010-01-19 02:20:09 +00002290 if (CS->getHandlerBlock())
2291 addStmt(CS->getHandlerBlock());
2292
2293 CFGBlock* CatchBlock = Block;
2294 if (!CatchBlock)
2295 CatchBlock = createBlock();
2296
2297 CatchBlock->setLabel(CS);
2298
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002299 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002300 return 0;
2301
2302 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2303 Block = NULL;
2304
2305 return CatchBlock;
2306}
2307
Zhongxing Xua725ed42010-11-01 13:04:58 +00002308CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2309 AddStmtChoice asc) {
2310 if (asc.alwaysAdd()) {
2311 autoCreateBlock();
2312 AppendStmt(Block, E, asc);
2313
2314 // We do not want to propagate the AlwaysAdd property.
2315 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2316 : AddStmtChoice::NotAlwaysAdd);
2317 }
2318 return Visit(E->getSubExpr(), asc);
2319}
2320
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002321CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2322 AddStmtChoice asc) {
2323 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2324 : AddStmtChoice::AlwaysAdd;
2325 autoCreateBlock();
2326 AppendStmt(Block, C, AddStmtChoice(K));
2327 return VisitChildren(C);
2328}
2329
Zhongxing Xua725ed42010-11-01 13:04:58 +00002330CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2331 AddStmtChoice asc) {
2332 if (asc.alwaysAdd()) {
2333 autoCreateBlock();
2334 AppendStmt(Block, E, asc);
2335 // We do not want to propagate the AlwaysAdd property.
2336 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2337 : AddStmtChoice::NotAlwaysAdd);
2338 }
2339 return Visit(E->getSubExpr(), asc);
2340}
2341
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002342CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2343 AddStmtChoice asc) {
2344 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
2345 : AddStmtChoice::AlwaysAdd;
2346 autoCreateBlock();
2347 AppendStmt(Block, C, AddStmtChoice(K));
2348 return VisitChildren(C);
2349}
2350
Ted Kremenekad5a8942010-08-02 23:46:59 +00002351CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002352 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002353 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002354 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002355 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002356 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002357 return VisitChildren(C);
2358}
2359
Zhongxing Xua725ed42010-11-01 13:04:58 +00002360CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2361 AddStmtChoice asc) {
2362 if (asc.alwaysAdd()) {
2363 autoCreateBlock();
2364 AppendStmt(Block, E, asc);
2365 // We do not want to propagate the AlwaysAdd property.
2366 asc = AddStmtChoice(asc.asLValue() ? AddStmtChoice::AsLValueNotAlwaysAdd
2367 : AddStmtChoice::NotAlwaysAdd);
2368 }
2369 return Visit(E->getSubExpr(), asc);
2370}
2371
Ted Kremenek19bb3562007-08-28 19:26:49 +00002372CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002373 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002374 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002375
Ted Kremenek19bb3562007-08-28 19:26:49 +00002376 if (!IBlock) {
2377 IBlock = createBlock(false);
2378 cfg->setIndirectGotoBlock(IBlock);
2379 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002380
Ted Kremenek19bb3562007-08-28 19:26:49 +00002381 // IndirectGoto is a control-flow statement. Thus we stop processing the
2382 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002383 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002384 return 0;
2385
Ted Kremenek19bb3562007-08-28 19:26:49 +00002386 Block = createBlock(false);
2387 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002388 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002389 return addStmt(I->getTarget());
2390}
2391
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002392} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002393
Mike Stump6d9828c2009-07-17 01:31:16 +00002394/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2395/// no successors or predecessors. If this is the first block created in the
2396/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002397CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002398 bool first_block = begin() == end();
2399
2400 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002401 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2402 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2403 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002404
2405 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002406 if (first_block)
2407 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002408
2409 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002410 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002411}
2412
Ted Kremenek026473c2007-08-23 16:51:22 +00002413/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2414/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002415CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002416 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002417 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002418 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002419}
2420
Ted Kremenek63f58872007-10-01 19:33:33 +00002421//===----------------------------------------------------------------------===//
2422// CFG: Queries for BlkExprs.
2423//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002424
Ted Kremenek63f58872007-10-01 19:33:33 +00002425namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002426 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002427}
2428
Ted Kremenek8a693662009-12-23 23:37:10 +00002429static void FindSubExprAssignments(Stmt *S,
2430 llvm::SmallPtrSet<Expr*,50>& Set) {
2431 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002432 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002433
Ted Kremenek8a693662009-12-23 23:37:10 +00002434 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002435 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002436 if (!child)
2437 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002438
Ted Kremenek8a693662009-12-23 23:37:10 +00002439 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002440 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002441
Ted Kremenek8a693662009-12-23 23:37:10 +00002442 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002443 }
2444}
2445
Ted Kremenek63f58872007-10-01 19:33:33 +00002446static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2447 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002448
2449 // Look for assignments that are used as subexpressions. These are the only
2450 // assignments that we want to *possibly* register as a block-level
2451 // expression. Basically, if an assignment occurs both in a subexpression and
2452 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002453 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002454
Ted Kremenek63f58872007-10-01 19:33:33 +00002455 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002456 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002457 if (CFGStmt S = BI->getAs<CFGStmt>())
2458 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002459
Ted Kremenek411cdee2008-04-16 21:10:48 +00002460 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002461
2462 // Iterate over the statements again on identify the Expr* and Stmt* at the
2463 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002464
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002465 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2466 CFGStmt CS = BI->getAs<CFGStmt>();
2467 if (!CS.isValid())
2468 continue;
2469 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002470
Ted Kremenek411cdee2008-04-16 21:10:48 +00002471 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002472 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002473 // expression are really "statements" whose value is never used by
2474 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002475 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002476 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002477 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2478 // Special handling for statement expressions. The last statement in
2479 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002480 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002481 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002482 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002483 (*M)[C->body_back()] = x;
2484 }
2485 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002486
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002487 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002488 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002489 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002490 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002491
Ted Kremenek411cdee2008-04-16 21:10:48 +00002492 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002493
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002494 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002495
Ted Kremenek390e48b2008-11-12 21:11:49 +00002496 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002497 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002498 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002499 }
2500 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002501
Ted Kremenek63f58872007-10-01 19:33:33 +00002502 return M;
2503}
2504
Ted Kremenek86946742008-01-17 20:48:37 +00002505CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2506 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002507 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002508
Ted Kremenek63f58872007-10-01 19:33:33 +00002509 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002510 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002511 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002512}
2513
2514unsigned CFG::getNumBlkExprs() {
2515 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2516 return M->size();
2517 else {
2518 // We assume callers interested in the number of BlkExprs will want
2519 // the map constructed if it doesn't already exist.
2520 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2521 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2522 }
2523}
2524
Ted Kremenek274f4332008-04-28 18:00:46 +00002525//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002526// Filtered walking of the CFG.
2527//===----------------------------------------------------------------------===//
2528
2529bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002530 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002531
2532 if (F.IgnoreDefaultsWithCoveredEnums) {
2533 // If the 'To' has no label or is labeled but the label isn't a
2534 // CaseStmt then filter this edge.
2535 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002536 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002537 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002538 const Stmt *L = To->getLabel();
2539 if (!L || !isa<CaseStmt>(L))
2540 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002541 }
2542 }
2543 }
2544
2545 return false;
2546}
2547
2548//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002549// Cleanup: CFG dstor.
2550//===----------------------------------------------------------------------===//
2551
Ted Kremenek63f58872007-10-01 19:33:33 +00002552CFG::~CFG() {
2553 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2554}
Mike Stump6d9828c2009-07-17 01:31:16 +00002555
Ted Kremenek7dba8602007-08-29 21:56:09 +00002556//===----------------------------------------------------------------------===//
2557// CFG pretty printing
2558//===----------------------------------------------------------------------===//
2559
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002560namespace {
2561
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002562class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002563 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002564 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002565 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002566 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002567 signed CurrentBlock;
2568 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002569 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002570public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002571
Chris Lattnere4f21422009-06-30 01:26:17 +00002572 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2573 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002574 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2575 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002576 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002577 BI != BEnd; ++BI, ++j ) {
2578 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2579 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2580 StmtMap[SE] = P;
2581
2582 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2583 DeclMap[DS->getSingleDecl()] = P;
2584
2585 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2586 if (VarDecl* VD = IS->getConditionVariable())
2587 DeclMap[VD] = P;
2588
2589 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2590 if (VarDecl* VD = FS->getConditionVariable())
2591 DeclMap[VD] = P;
2592
2593 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2594 if (VarDecl* VD = WS->getConditionVariable())
2595 DeclMap[VD] = P;
2596
2597 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2598 if (VarDecl* VD = SS->getConditionVariable())
2599 DeclMap[VD] = P;
2600
2601 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2602 if (VarDecl* VD = CS->getExceptionDecl())
2603 DeclMap[VD] = P;
2604 }
2605 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002606 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002607 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002608 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002609
Ted Kremenek42a509f2007-08-31 21:30:12 +00002610 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002611
Chris Lattnere4f21422009-06-30 01:26:17 +00002612 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002613 void setBlockID(signed i) { CurrentBlock = i; }
2614 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002615
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002616 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2617 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002618
2619 if (I == StmtMap.end())
2620 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002621
2622 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002623 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002624 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002625 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002626
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002627 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002628 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002629 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002630
2631 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2632 DeclMapTy::iterator I = DeclMap.find(D);
2633
2634 if (I == DeclMap.end())
2635 return false;
2636
2637 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2638 && I->second.second == CurrentStmt) {
2639 return false;
2640 }
2641
2642 OS << "[B" << I->second.first << "." << I->second.second << "]";
2643 return true;
2644 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002645};
Chris Lattnere4f21422009-06-30 01:26:17 +00002646} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002647
Chris Lattnere4f21422009-06-30 01:26:17 +00002648
2649namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002650class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002651 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002652
Ted Kremeneka95d3752008-09-13 05:16:45 +00002653 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002654 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002655 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002656public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002657 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002658 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002659 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002660
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002661 void VisitIfStmt(IfStmt* I) {
2662 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002663 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002664 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002665
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002666 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002667 void VisitStmt(Stmt* Terminator) {
2668 Terminator->printPretty(OS, Helper, Policy);
2669 }
2670
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002671 void VisitForStmt(ForStmt* F) {
2672 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002673 if (F->getInit())
2674 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002675 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002676 if (Stmt* C = F->getCond())
2677 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002678 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002679 if (F->getInc())
2680 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002681 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002682 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002683
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002684 void VisitWhileStmt(WhileStmt* W) {
2685 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002686 if (Stmt* C = W->getCond())
2687 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002688 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002689
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002690 void VisitDoStmt(DoStmt* D) {
2691 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002692 if (Stmt* C = D->getCond())
2693 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002694 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002695
Ted Kremenek411cdee2008-04-16 21:10:48 +00002696 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002697 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002698 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002699 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002700
Mike Stump5d1d2022010-01-19 02:20:09 +00002701 void VisitCXXTryStmt(CXXTryStmt* CS) {
2702 OS << "try ...";
2703 }
2704
Ted Kremenek805e9a82007-08-31 21:49:40 +00002705 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002706 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002707 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002708 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002709
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002710 void VisitChooseExpr(ChooseExpr* C) {
2711 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002712 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002713 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002714 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002715
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002716 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2717 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002718 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002719 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002720
Ted Kremenek805e9a82007-08-31 21:49:40 +00002721 void VisitBinaryOperator(BinaryOperator* B) {
2722 if (!B->isLogicalOp()) {
2723 VisitExpr(B);
2724 return;
2725 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002726
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002727 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002728
Ted Kremenek805e9a82007-08-31 21:49:40 +00002729 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002730 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002731 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002732 return;
John McCall2de56d12010-08-25 11:45:40 +00002733 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002734 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002735 return;
2736 default:
2737 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002738 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002739 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002740
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002741 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002742 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002743 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002744};
Chris Lattnere4f21422009-06-30 01:26:17 +00002745} // end anonymous namespace
2746
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002747static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002748 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002749 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2750 Stmt *S = CS;
2751
2752 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002753
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002754 // special printing for statement-expressions.
2755 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2756 CompoundStmt* Sub = SE->getSubStmt();
2757
2758 if (Sub->child_begin() != Sub->child_end()) {
2759 OS << "({ ... ; ";
2760 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2761 OS << " })\n";
2762 return;
2763 }
2764 }
2765 // special printing for comma expressions.
2766 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2767 if (B->getOpcode() == BO_Comma) {
2768 OS << "... , ";
2769 Helper->handledStmt(B->getRHS(),OS);
2770 OS << '\n';
2771 return;
2772 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002773 }
2774 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002775 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002776
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002777 if (isa<CXXOperatorCallExpr>(S)) {
2778 OS << " (OperatorCall)";
Mike Stump6d9828c2009-07-17 01:31:16 +00002779 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002780 else if (isa<CXXBindTemporaryExpr>(S)) {
2781 OS << " (BindTemporary)";
2782 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002783
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002784 // Expressions need a newline.
2785 if (isa<Expr>(S))
2786 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002787
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002788 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2789 CXXBaseOrMemberInitializer* I = IE;
2790 if (I->isBaseInitializer())
2791 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2792 else OS << I->getMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00002793
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002794 OS << "(";
2795 if (Expr* IE = I->getInit())
2796 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2797 OS << ")";
2798
2799 if (I->isBaseInitializer())
2800 OS << " (Base initializer)\n";
2801 else OS << " (Member initializer)\n";
2802
2803 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2804 VarDecl* VD = DE.getVarDecl();
2805 Helper->handleDecl(VD, OS);
2806
Marcin Swiderskib1c52872010-10-25 07:00:40 +00002807 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002808 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2809 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00002810 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
2811 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002812
2813 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2814 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00002815
2816 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
2817 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
2818 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00002819 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00002820
2821 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
2822 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00002823
2824 const Type *T = FD->getType().getTypePtr();
2825 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
2826 T = ET;
2827
Marcin Swiderski7c625d82010-10-05 05:37:00 +00002828 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00002829 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00002830 OS << " (Member object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002831 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002832}
Mike Stump6d9828c2009-07-17 01:31:16 +00002833
Chris Lattnere4f21422009-06-30 01:26:17 +00002834static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2835 const CFGBlock& B,
2836 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002837
Ted Kremenek42a509f2007-08-31 21:30:12 +00002838 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002839
Ted Kremenek7dba8602007-08-29 21:56:09 +00002840 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002841 OS << "\n [ B" << B.getBlockID();
2842
Ted Kremenek42a509f2007-08-31 21:30:12 +00002843 if (&B == &cfg->getEntry())
2844 OS << " (ENTRY) ]\n";
2845 else if (&B == &cfg->getExit())
2846 OS << " (EXIT) ]\n";
2847 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002848 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002849 else
2850 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002851
Ted Kremenek9cffe732007-08-29 23:20:49 +00002852 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002853 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002854
2855 if (print_edges)
2856 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002857
Mike Stump079bd722010-01-19 22:00:14 +00002858 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002859 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002860 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002861 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002862 C->getLHS()->printPretty(OS, Helper,
2863 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002864 if (C->getRHS()) {
2865 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002866 C->getRHS()->printPretty(OS, Helper,
2867 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002868 }
Mike Stump079bd722010-01-19 22:00:14 +00002869 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002870 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002871 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002872 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002873 if (CS->getExceptionDecl())
2874 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2875 0);
2876 else
2877 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002878 OS << ")";
2879
2880 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002881 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002882
Ted Kremenek9cffe732007-08-29 23:20:49 +00002883 OS << ":\n";
2884 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002885
Ted Kremenekfddd5182007-08-21 21:42:03 +00002886 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002887 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002888
Ted Kremenek42a509f2007-08-31 21:30:12 +00002889 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2890 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002891
Ted Kremenek9cffe732007-08-29 23:20:49 +00002892 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002893 if (print_edges)
2894 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002895
Ted Kremeneka95d3752008-09-13 05:16:45 +00002896 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002897
Ted Kremenek42a509f2007-08-31 21:30:12 +00002898 if (Helper)
2899 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002900
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002901 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002902 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002903
Ted Kremenek9cffe732007-08-29 23:20:49 +00002904 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002905 if (B.getTerminator()) {
2906 if (print_edges)
2907 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002908
Ted Kremenek9cffe732007-08-29 23:20:49 +00002909 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002910
Ted Kremenek42a509f2007-08-31 21:30:12 +00002911 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002912
Chris Lattnere4f21422009-06-30 01:26:17 +00002913 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2914 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002915 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002916 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002917 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002918
Ted Kremenek9cffe732007-08-29 23:20:49 +00002919 if (print_edges) {
2920 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002921 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002922 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002923
Ted Kremenek42a509f2007-08-31 21:30:12 +00002924 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2925 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002926
Ted Kremenek42a509f2007-08-31 21:30:12 +00002927 if (i == 8 || (i-8) == 0)
2928 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002929
Ted Kremenek9cffe732007-08-29 23:20:49 +00002930 OS << " B" << (*I)->getBlockID();
2931 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002932
Ted Kremenek42a509f2007-08-31 21:30:12 +00002933 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002934
Ted Kremenek42a509f2007-08-31 21:30:12 +00002935 // Print the successors of this block.
2936 OS << " Successors (" << B.succ_size() << "):";
2937 i = 0;
2938
2939 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2940 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002941
Ted Kremenek42a509f2007-08-31 21:30:12 +00002942 if (i == 8 || (i-8) % 10 == 0)
2943 OS << "\n ";
2944
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002945 if (*I)
2946 OS << " B" << (*I)->getBlockID();
2947 else
2948 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002949 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002950
Ted Kremenek9cffe732007-08-29 23:20:49 +00002951 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002952 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002953}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002954
Ted Kremenek42a509f2007-08-31 21:30:12 +00002955
2956/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002957void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002958
2959/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002960void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2961 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002962
Ted Kremenek42a509f2007-08-31 21:30:12 +00002963 // Print the entry block.
2964 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002965
Ted Kremenek42a509f2007-08-31 21:30:12 +00002966 // Iterate through the CFGBlocks and print them one by one.
2967 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2968 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002969 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002970 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002971
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002972 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002973 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002974
Ted Kremenek42a509f2007-08-31 21:30:12 +00002975 // Print the exit block.
2976 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002977 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002978}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002979
2980/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002981void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2982 print(llvm::errs(), cfg, LO);
2983}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002984
2985/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2986/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002987void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2988 const LangOptions &LO) const {
2989 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002990 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002991}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002992
Ted Kremeneka2925852008-01-30 23:02:42 +00002993/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002994void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002995 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002996 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002997 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002998}
2999
Ted Kremenek390e48b2008-11-12 21:11:49 +00003000Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003001 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003002 if (!Terminator)
3003 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003004
Ted Kremenek411cdee2008-04-16 21:10:48 +00003005 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003006
Ted Kremenek411cdee2008-04-16 21:10:48 +00003007 switch (Terminator->getStmtClass()) {
3008 default:
3009 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003010
Ted Kremenek411cdee2008-04-16 21:10:48 +00003011 case Stmt::ForStmtClass:
3012 E = cast<ForStmt>(Terminator)->getCond();
3013 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003014
Ted Kremenek411cdee2008-04-16 21:10:48 +00003015 case Stmt::WhileStmtClass:
3016 E = cast<WhileStmt>(Terminator)->getCond();
3017 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003018
Ted Kremenek411cdee2008-04-16 21:10:48 +00003019 case Stmt::DoStmtClass:
3020 E = cast<DoStmt>(Terminator)->getCond();
3021 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003022
Ted Kremenek411cdee2008-04-16 21:10:48 +00003023 case Stmt::IfStmtClass:
3024 E = cast<IfStmt>(Terminator)->getCond();
3025 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003026
Ted Kremenek411cdee2008-04-16 21:10:48 +00003027 case Stmt::ChooseExprClass:
3028 E = cast<ChooseExpr>(Terminator)->getCond();
3029 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003030
Ted Kremenek411cdee2008-04-16 21:10:48 +00003031 case Stmt::IndirectGotoStmtClass:
3032 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3033 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003034
Ted Kremenek411cdee2008-04-16 21:10:48 +00003035 case Stmt::SwitchStmtClass:
3036 E = cast<SwitchStmt>(Terminator)->getCond();
3037 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003038
Ted Kremenek411cdee2008-04-16 21:10:48 +00003039 case Stmt::ConditionalOperatorClass:
3040 E = cast<ConditionalOperator>(Terminator)->getCond();
3041 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003042
Ted Kremenek411cdee2008-04-16 21:10:48 +00003043 case Stmt::BinaryOperatorClass: // '&&' and '||'
3044 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003045 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003046
Ted Kremenek390e48b2008-11-12 21:11:49 +00003047 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003048 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003049 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003050
Ted Kremenek411cdee2008-04-16 21:10:48 +00003051 return E ? E->IgnoreParens() : NULL;
3052}
3053
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003054bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003055 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003056 if (!Terminator)
3057 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003058
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003059 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003060
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003061 switch (Terminator->getStmtClass()) {
3062 default:
3063 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003064
3065 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003066 case Stmt::WhileStmtClass:
3067 case Stmt::DoStmtClass:
3068 case Stmt::IfStmtClass:
3069 case Stmt::ChooseExprClass:
3070 case Stmt::ConditionalOperatorClass:
3071 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003072 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003073 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003074
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003075 return E ? E->IgnoreParens() : NULL;
3076}
3077
Ted Kremeneka2925852008-01-30 23:02:42 +00003078
Ted Kremenek7dba8602007-08-29 21:56:09 +00003079//===----------------------------------------------------------------------===//
3080// CFG Graphviz Visualization
3081//===----------------------------------------------------------------------===//
3082
Ted Kremenek42a509f2007-08-31 21:30:12 +00003083
3084#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003085static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003086#endif
3087
Chris Lattnere4f21422009-06-30 01:26:17 +00003088void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003089#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003090 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003091 GraphHelper = &H;
3092 llvm::ViewGraph(this,"CFG");
3093 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003094#endif
3095}
3096
Ted Kremenek7dba8602007-08-29 21:56:09 +00003097namespace llvm {
3098template<>
3099struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003100
3101 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3102
3103 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003104
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003105#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003106 std::string OutSStr;
3107 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003108 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003109 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003110
3111 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3112
3113 // Process string output to make it nicer...
3114 for (unsigned i = 0; i != OutStr.length(); ++i)
3115 if (OutStr[i] == '\n') { // Left justify
3116 OutStr[i] = '\\';
3117 OutStr.insert(OutStr.begin()+i+1, 'l');
3118 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003119
Ted Kremenek7dba8602007-08-29 21:56:09 +00003120 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003121#else
3122 return "";
3123#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003124 }
3125};
3126} // end namespace llvm