blob: ea30ec01f6e0fdf7c6b61a118e98895a0b867b15 [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
Ted Kremeneka34ea072008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump6d9828c2009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000072
Ted Kremenekfddd5182007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000080
Ted Kremenek19bb3562007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000089
Ted Kremenek19bb3562007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000093
94public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000100
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
103 bool pruneTriviallyFalseEdges, bool AddEHEdges,
Mike Stump4c45aa12010-01-21 15:20:48 +0000104 bool AddScopes);
Mike Stump6d9828c2009-07-17 01:31:16 +0000105
Ted Kremenek4f880632009-07-17 22:18:43 +0000106private:
107 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000108 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
109 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
110 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000111 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000112 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
113 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
114 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000115 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000116 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000117 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000118 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000119 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000120 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000121 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000122 CFGBlock *VisitDeclStmt(DeclStmt *DS);
123 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000124 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
125 CFGBlock *VisitDoStmt(DoStmt *D);
126 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000127 CFGBlock *VisitGotoStmt(GotoStmt* G);
128 CFGBlock *VisitIfStmt(IfStmt *I);
129 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
130 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000131 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000132 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
133 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
134 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
135 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
136 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
137 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000138 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
139 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000140 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
141 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000142
Ted Kremenek852274d2009-12-16 03:18:58 +0000143 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
144 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000145 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000146
Ted Kremenek274f4332008-04-28 18:00:46 +0000147 // NYS == Not Yet Supported
148 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000149 badCFG = true;
150 return Block;
151 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000152
Mike Stump079bd722010-01-19 22:00:14 +0000153 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
154 if (!AddScopes)
155 return B;
156
157 if (B == 0)
158 B = createBlock();
159 B->StartScope(S, cfg->getBumpVectorContext());
160 return B;
161 }
162
163 void EndScope(Stmt *S) {
164 if (!AddScopes)
165 return;
166
167 if (Block == 0)
168 Block = createBlock();
169 Block->EndScope(S, cfg->getBumpVectorContext());
170 }
171
Ted Kremenek4f880632009-07-17 22:18:43 +0000172 void autoCreateBlock() { if (!Block) Block = createBlock(); }
173 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000174 bool FinishBlock(CFGBlock* B);
Zhongxing Xudf119892010-06-03 06:43:23 +0000175 CFGBlock *addStmt(Stmt *S) {
176 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000177 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000178
Ted Kremenek852274d2009-12-16 03:18:58 +0000179 void AppendStmt(CFGBlock *B, Stmt *S,
180 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
181 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000182 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000183
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000184 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
185 B->addSuccessor(S, cfg->getBumpVectorContext());
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000188 /// TryResult - a class representing a variant over the values
189 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
190 /// and is used by the CFGBuilder to decide if a branch condition
191 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000192 class TryResult {
193 int X;
194 public:
195 TryResult(bool b) : X(b ? 1 : 0) {}
196 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek941fde82009-07-24 04:47:11 +0000198 bool isTrue() const { return X == 1; }
199 bool isFalse() const { return X == 0; }
200 bool isKnown() const { return X >= 0; }
201 void negate() {
202 assert(isKnown());
203 X ^= 0x1;
204 }
205 };
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Mike Stump00998a02009-07-23 23:25:26 +0000207 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
208 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000209 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000210 if (!PruneTriviallyFalseEdges)
211 return TryResult();
212
Mike Stump00998a02009-07-23 23:25:26 +0000213 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000214 if (!S->isTypeDependent() && !S->isValueDependent() &&
215 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000216 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000217
218 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000219 }
220
Ted Kremenek4102af92008-03-13 03:04:22 +0000221 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000222
Ted Kremenekad5a8942010-08-02 23:46:59 +0000223 // True iff trivially false edges should be pruned from the CFG.
224 bool PruneTriviallyFalseEdges;
225
Mike Stump4c45aa12010-01-21 15:20:48 +0000226 // True iff EH edges on CallExprs should be added to the CFG.
227 bool AddEHEdges;
228
229 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000230 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000231};
Mike Stump6d9828c2009-07-17 01:31:16 +0000232
Douglas Gregor898574e2008-12-05 23:32:09 +0000233// FIXME: Add support for dependent-sized array types in C++?
234// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000235static VariableArrayType* FindVA(Type* t) {
236 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
237 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
238 if (vat->getSizeExpr())
239 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
Ted Kremenek610a09e2008-09-26 22:58:57 +0000241 t = vt->getElementType().getTypePtr();
242 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000243
Ted Kremenek610a09e2008-09-26 22:58:57 +0000244 return 0;
245}
Mike Stump6d9828c2009-07-17 01:31:16 +0000246
247/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
248/// arbitrary statement. Examples include a single expression or a function
249/// body (compound statement). The ownership of the returned CFG is
250/// transferred to the caller. If CFG construction fails, this method returns
251/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000252CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenekad5a8942010-08-02 23:46:59 +0000253 bool pruneTriviallyFalseEdges,
Mike Stump55f988e2010-01-21 17:21:23 +0000254 bool addehedges, bool AddScopes) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000255
Mike Stump55f988e2010-01-21 17:21:23 +0000256 AddEHEdges = addehedges;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000257 PruneTriviallyFalseEdges = pruneTriviallyFalseEdges;
258
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000259 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000260 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000261 if (!Statement)
262 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000263
Mike Stump079bd722010-01-19 22:00:14 +0000264 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000265 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000266
267 // Create an empty block that will serve as the exit block for the CFG. Since
268 // this is the first block added to the CFG, it will be implicitly registered
269 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000270 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000271 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000272 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000273
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000274 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000275 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000276
277 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
278 // FIXME: Add code for base initializers and member initializers.
279 (void)CD;
280 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000281 if (!B)
282 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000283
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000284 if (B) {
285 // Finalize the last constructed block. This usually involves reversing the
286 // order of the statements in the block.
287 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000288
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000289 // Backpatch the gotos whose label -> block mappings we didn't know when we
290 // encountered them.
291 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000292 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000293
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000294 CFGBlock* B = *I;
295 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
296 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000297
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000298 // If there is no target for the goto, then we are looking at an
299 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000300 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000301
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000302 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000303 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000304
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000305 // Add successors to the Indirect Goto Dispatch block (if we have one).
306 if (CFGBlock* B = cfg->getIndirectGotoBlock())
307 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
308 E = AddressTakenLabels.end(); I != E; ++I ) {
309
310 // Lookup the target block.
311 LabelMapTy::iterator LI = LabelMap.find(*I);
312
313 // If there is no target block that contains label, then we are looking
314 // at an incomplete AST. Handle this by not registering a successor.
315 if (LI == LabelMap.end()) continue;
316
317 AddSuccessor(B, LI->second);
318 }
319
320 Succ = B;
321 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000322
323 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000324 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000325
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000326 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000327}
Mike Stump6d9828c2009-07-17 01:31:16 +0000328
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000329/// createBlock - Used to lazily create blocks that are connected
330/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000331CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000332 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000333 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000334 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000335 return B;
336}
Mike Stump6d9828c2009-07-17 01:31:16 +0000337
Ted Kremenek6c249722009-09-24 18:45:41 +0000338/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000339bool CFGBuilder::FinishBlock(CFGBlock* B) {
340 if (badCFG)
341 return false;
342
Ted Kremenek4f880632009-07-17 22:18:43 +0000343 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000344 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000345}
346
Ted Kremenek4f880632009-07-17 22:18:43 +0000347/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000348/// blocks for ternary operators, &&, and ||. We also process "," and
349/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000350CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000351tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000352 if (!S) {
353 badCFG = true;
354 return 0;
355 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 switch (S->getStmtClass()) {
357 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000358 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000359
360 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000361 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek4f880632009-07-17 22:18:43 +0000363 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000364 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek4f880632009-07-17 22:18:43 +0000366 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000367 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000368
Ted Kremenek4f880632009-07-17 22:18:43 +0000369 case Stmt::BreakStmtClass:
370 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek4f880632009-07-17 22:18:43 +0000372 case Stmt::CallExprClass:
Douglas Gregor35fe7ee2010-08-31 05:10:27 +0000373 case Stmt::CXXOperatorCallExprClass: // FIXME: handle specially?
Ted Kremenek852274d2009-12-16 03:18:58 +0000374 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek4f880632009-07-17 22:18:43 +0000376 case Stmt::CaseStmtClass:
377 return VisitCaseStmt(cast<CaseStmt>(S));
378
379 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000380 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek4f880632009-07-17 22:18:43 +0000382 case Stmt::CompoundStmtClass:
383 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek4f880632009-07-17 22:18:43 +0000385 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000386 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek4f880632009-07-17 22:18:43 +0000388 case Stmt::ContinueStmtClass:
389 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek021c8af2010-01-19 20:40:33 +0000391 case Stmt::CXXCatchStmtClass:
392 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
393
Ted Kremenek47e331e2010-08-28 00:19:02 +0000394 case Stmt::CXXExprWithTemporariesClass: {
395 // FIXME: Handle temporaries. For now, just visit the subexpression
396 // so we don't artificially create extra blocks.
397 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr());
398 }
399
Douglas Gregor35fe7ee2010-08-31 05:10:27 +0000400 case Stmt::CXXBindTemporaryExprClass: {
401 // FIXME: Handle temporary binding. For now, just visit the subexpression
402 // so we don't artificially create extra blocks.
403 return Visit(cast<CXXBindTemporaryExpr>(S)->getSubExpr());
404 }
405
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000406 case Stmt::CXXMemberCallExprClass:
407 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
408
Ted Kremenek021c8af2010-01-19 20:40:33 +0000409 case Stmt::CXXThrowExprClass:
410 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000411
Ted Kremenek021c8af2010-01-19 20:40:33 +0000412 case Stmt::CXXTryStmtClass:
413 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000414
Ted Kremenek4f880632009-07-17 22:18:43 +0000415 case Stmt::DeclStmtClass:
416 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek4f880632009-07-17 22:18:43 +0000418 case Stmt::DefaultStmtClass:
419 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 case Stmt::DoStmtClass:
422 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 case Stmt::ForStmtClass:
425 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek4f880632009-07-17 22:18:43 +0000427 case Stmt::GotoStmtClass:
428 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek4f880632009-07-17 22:18:43 +0000430 case Stmt::IfStmtClass:
431 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek4f880632009-07-17 22:18:43 +0000433 case Stmt::IndirectGotoStmtClass:
434 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek4f880632009-07-17 22:18:43 +0000436 case Stmt::LabelStmtClass:
437 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek115c1b92010-04-11 17:02:10 +0000439 case Stmt::MemberExprClass:
440 return VisitMemberExpr(cast<MemberExpr>(S), asc);
441
Ted Kremenek4f880632009-07-17 22:18:43 +0000442 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000443 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
444
Ted Kremenek4f880632009-07-17 22:18:43 +0000445 case Stmt::ObjCAtSynchronizedStmtClass:
446 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek4f880632009-07-17 22:18:43 +0000448 case Stmt::ObjCAtThrowStmtClass:
449 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 case Stmt::ObjCAtTryStmtClass:
452 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 case Stmt::ObjCForCollectionStmtClass:
455 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek4f880632009-07-17 22:18:43 +0000457 case Stmt::ParenExprClass:
458 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000459 goto tryAgain;
460
Ted Kremenek4f880632009-07-17 22:18:43 +0000461 case Stmt::NullStmtClass:
462 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek4f880632009-07-17 22:18:43 +0000464 case Stmt::ReturnStmtClass:
465 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek4f880632009-07-17 22:18:43 +0000467 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000468 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremenek4f880632009-07-17 22:18:43 +0000470 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000471 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Ted Kremenek4f880632009-07-17 22:18:43 +0000473 case Stmt::SwitchStmtClass:
474 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek4f880632009-07-17 22:18:43 +0000476 case Stmt::WhileStmtClass:
477 return VisitWhileStmt(cast<WhileStmt>(S));
478 }
479}
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek852274d2009-12-16 03:18:58 +0000481CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
482 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000483 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000484 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Ted Kremenek4f880632009-07-17 22:18:43 +0000487 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000488}
Mike Stump6d9828c2009-07-17 01:31:16 +0000489
Ted Kremenek4f880632009-07-17 22:18:43 +0000490/// VisitChildren - Visit the children of a Stmt.
491CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
492 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000493 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000494 E = Terminator->child_end(); I != E; ++I) {
495 if (*I) B = Visit(*I);
496 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000497 return B;
498}
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Ted Kremenek852274d2009-12-16 03:18:58 +0000500CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
501 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000502 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000503
Ted Kremenek852274d2009-12-16 03:18:58 +0000504 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000505 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000506 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000507 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000508
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000509 return Block;
510}
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek852274d2009-12-16 03:18:58 +0000512CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
513 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000514 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000515 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000516 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek4f880632009-07-17 22:18:43 +0000518 if (!FinishBlock(ConfluenceBlock))
519 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek4f880632009-07-17 22:18:43 +0000521 // create the block evaluating the LHS
522 CFGBlock* LHSBlock = createBlock(false);
523 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Ted Kremenek4f880632009-07-17 22:18:43 +0000525 // create the block evaluating the RHS
526 Succ = ConfluenceBlock;
527 Block = NULL;
528 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000529
530 if (RHSBlock) {
531 if (!FinishBlock(RHSBlock))
532 return 0;
533 }
534 else {
535 // Create an empty block for cases where the RHS doesn't require
536 // any explicit statements in the CFG.
537 RHSBlock = createBlock();
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Mike Stump00998a02009-07-23 23:25:26 +0000540 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000541 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000542 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000543 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000544
Ted Kremenek4f880632009-07-17 22:18:43 +0000545 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000546 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000547 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
548 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000549 } else {
John McCall2de56d12010-08-25 11:45:40 +0000550 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000551 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
552 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek4f880632009-07-17 22:18:43 +0000555 // Generate the blocks for evaluating the LHS.
556 Block = LHSBlock;
557 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000558 }
John McCall2de56d12010-08-25 11:45:40 +0000559 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000560 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000561 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000562 addStmt(B->getRHS());
563 return addStmt(B->getLHS());
564 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000565 else if (B->isAssignmentOp()) {
566 if (asc.alwaysAdd()) {
567 autoCreateBlock();
568 AppendStmt(Block, B, asc);
569 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000570
Zhongxing Xufc61d942010-06-03 06:23:18 +0000571 Visit(B->getRHS());
572 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Ted Kremenek852274d2009-12-16 03:18:58 +0000575 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000576}
577
Ted Kremenek852274d2009-12-16 03:18:58 +0000578CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
579 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000580 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000581 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000582 }
583 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000584}
585
Ted Kremenek4f880632009-07-17 22:18:43 +0000586CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
587 // "break" is a control-flow statement. Thus we stop processing the current
588 // block.
589 if (Block && !FinishBlock(Block))
590 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Ted Kremenek4f880632009-07-17 22:18:43 +0000592 // Now create a new block that ends with the break statement.
593 Block = createBlock(false);
594 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Ted Kremenek4f880632009-07-17 22:18:43 +0000596 // If there is no target for the break, then we are looking at an incomplete
597 // AST. This means that the CFG cannot be constructed.
598 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000599 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000600 else
601 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
603
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000604 return Block;
605}
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Mike Stump4c45aa12010-01-21 15:20:48 +0000607static bool CanThrow(Expr *E) {
608 QualType Ty = E->getType();
609 if (Ty->isFunctionPointerType())
610 Ty = Ty->getAs<PointerType>()->getPointeeType();
611 else if (Ty->isBlockPointerType())
612 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +0000613
Mike Stump4c45aa12010-01-21 15:20:48 +0000614 const FunctionType *FT = Ty->getAs<FunctionType>();
615 if (FT) {
616 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
617 if (Proto->hasEmptyExceptionSpec())
618 return false;
619 }
620 return true;
621}
622
Ted Kremenek852274d2009-12-16 03:18:58 +0000623CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000624 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000625 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000626 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000627 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000628 }
Mike Stump24556362009-07-25 21:26:53 +0000629
Mike Stump4c45aa12010-01-21 15:20:48 +0000630 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000631
632 // Languages without exceptions are assumed to not throw.
633 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000634 if (AddEHEdges)
635 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000636 }
637
638 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000639 if (FD->hasAttr<NoReturnAttr>())
640 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000641 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000642 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000643 }
Mike Stump24556362009-07-25 21:26:53 +0000644
Mike Stump4c45aa12010-01-21 15:20:48 +0000645 if (!CanThrow(C->getCallee()))
646 AddEHEdge = false;
647
Zhongxing Xufc61d942010-06-03 06:23:18 +0000648 if (!NoReturn && !AddEHEdge) {
649 if (asc.asLValue())
650 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
651 else
652 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Mike Stump079bd722010-01-19 22:00:14 +0000655 if (Block) {
656 Succ = Block;
657 if (!FinishBlock(Block))
658 return 0;
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Mike Stump079bd722010-01-19 22:00:14 +0000661 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000662 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000663
Mike Stump079bd722010-01-19 22:00:14 +0000664 if (NoReturn) {
665 // Wire this to the exit block directly.
666 AddSuccessor(Block, &cfg->getExit());
667 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000668 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000669 // Add exceptional edges.
670 if (TryTerminatedBlock)
671 AddSuccessor(Block, TryTerminatedBlock);
672 else
673 AddSuccessor(Block, &cfg->getExit());
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Mike Stump24556362009-07-25 21:26:53 +0000676 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000677}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000678
Ted Kremenek852274d2009-12-16 03:18:58 +0000679CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
680 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000681 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000682 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000683 if (!FinishBlock(ConfluenceBlock))
684 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Ted Kremenek115c1b92010-04-11 17:02:10 +0000686 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
687 : AddStmtChoice::AlwaysAdd;
688
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000689 Succ = ConfluenceBlock;
690 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000691 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000692 if (!FinishBlock(LHSBlock))
693 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000695 Succ = ConfluenceBlock;
696 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000697 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000698 if (!FinishBlock(RHSBlock))
699 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000701 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000702 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000703 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000704 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
705 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000706 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000707 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000708}
Mike Stump1eb44332009-09-09 15:08:12 +0000709
710
711CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000712 EndScope(C);
713
Mike Stump1eb44332009-09-09 15:08:12 +0000714 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000715
716 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
717 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +0000718 // If we hit a segment of code just containing ';' (NullStmts), we can
719 // get a null block back. In such cases, just use the LastBlock
720 if (CFGBlock *newBlock = addStmt(*I))
721 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000723 if (badCFG)
724 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000725 }
Mike Stump079bd722010-01-19 22:00:14 +0000726
727 LastBlock = StartScope(C, LastBlock);
728
Ted Kremenek4f880632009-07-17 22:18:43 +0000729 return LastBlock;
730}
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek852274d2009-12-16 03:18:58 +0000732CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
733 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000734 // Create the confluence block that will "merge" the results of the ternary
735 // expression.
736 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000737 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000738 if (!FinishBlock(ConfluenceBlock))
739 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek115c1b92010-04-11 17:02:10 +0000741 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
742 : AddStmtChoice::AlwaysAdd;
743
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000744 // Create a block for the LHS expression if there is an LHS expression. A
745 // GCC extension allows LHS to be NULL, causing the condition to be the
746 // value that is returned instead.
747 // e.g: x ?: y is shorthand for: x ? x : y;
748 Succ = ConfluenceBlock;
749 Block = NULL;
750 CFGBlock* LHSBlock = NULL;
751 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +0000752 LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000753 if (!FinishBlock(LHSBlock))
754 return 0;
755 Block = NULL;
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000758 // Create the block for the RHS expression.
759 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +0000760 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000761 if (!FinishBlock(RHSBlock))
762 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000764 // Create the block that will contain the condition.
765 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Mike Stump00998a02009-07-23 23:25:26 +0000767 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000768 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000769 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000770 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000771 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000772 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000773 // If we know the condition is false, add NULL as the successor for
774 // the block containing the condition. In this case, the confluence
775 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000776 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000777 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000778 } else {
779 // If we have no LHS expression, add the ConfluenceBlock as a direct
780 // successor for the block containing the condition. Moreover, we need to
781 // reverse the order of the predecessors in the ConfluenceBlock because
782 // the RHSBlock will have been added to the succcessors already, and we
783 // want the first predecessor to the the block containing the expression
784 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000785 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000786 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000787 std::reverse(ConfluenceBlock->pred_begin(),
788 ConfluenceBlock->pred_end());
789 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000792 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000793 Block->setTerminator(C);
794 return addStmt(C->getCond());
795}
796
Ted Kremenek4f880632009-07-17 22:18:43 +0000797CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
798 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000799
Ted Kremenek4f880632009-07-17 22:18:43 +0000800 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000801 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000802 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Ted Kremenek4f880632009-07-17 22:18:43 +0000805 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek4f880632009-07-17 22:18:43 +0000807 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
808 typedef llvm::SmallVector<Decl*,10> BufTy;
809 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek4f880632009-07-17 22:18:43 +0000811 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
812 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
813 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
814 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek4f880632009-07-17 22:18:43 +0000816 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
817 // automatically freed with the CFG.
818 DeclGroupRef DG(*I);
819 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000820 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000821 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000824 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000825 B = VisitDeclSubExpr(D);
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
828 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000829}
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek4f880632009-07-17 22:18:43 +0000831/// VisitDeclSubExpr - Utility method to add block-level expressions for
832/// initializers in Decls.
833CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
834 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000835
Ted Kremenek4f880632009-07-17 22:18:43 +0000836 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek4f880632009-07-17 22:18:43 +0000838 if (!VD)
839 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek4f880632009-07-17 22:18:43 +0000841 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek4f880632009-07-17 22:18:43 +0000843 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000844 AddStmtChoice::Kind k =
845 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
846 : AddStmtChoice::NotAlwaysAdd;
847 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremenek4f880632009-07-17 22:18:43 +0000850 // If the type of VD is a VLA, then we must process its size expressions.
851 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
852 VA = FindVA(VA->getElementType().getTypePtr()))
853 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek4f880632009-07-17 22:18:43 +0000855 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000856}
857
858CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000859 // We may see an if statement in the middle of a basic block, or it may be the
860 // first statement we are processing. In either case, we create a new basic
861 // block. First, we create the blocks for the then...else statements, and
862 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000863 // middle of a block, we stop processing that block. That block is then the
864 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000865
866 // The block we were proccessing is now finished. Make it the successor
867 // block.
868 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000869 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000870 if (!FinishBlock(Block))
871 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000872 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000873
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000874 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000875 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000876
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000877 if (Stmt* Else = I->getElse()) {
878 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000879
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000880 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000881 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000882 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000883 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000884
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000885 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
886 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000887 else if (Block) {
888 if (!FinishBlock(ElseBlock))
889 return 0;
890 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000891 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000892
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000893 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000894 CFGBlock* ThenBlock;
895 {
896 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000897 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000898 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000899 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000900 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000901
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000902 if (!ThenBlock) {
903 // We can reach here if the "then" body has all NullStmts.
904 // Create an empty block so we can distinguish between true and false
905 // branches in path-sensitive analyses.
906 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000907 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000908 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000909 if (!FinishBlock(ThenBlock))
910 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000911 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000912 }
913
Mike Stump6d9828c2009-07-17 01:31:16 +0000914 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000915 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000916
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000917 // Set the terminator of the new block to the If statement.
918 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000919
Mike Stump00998a02009-07-23 23:25:26 +0000920 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000921 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000922
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000923 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000924 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
925 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000926
927 // Add the condition as the last statement in the new block. This may create
928 // new blocks as the condition may contain control-flow. Any newly created
929 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000930 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +0000931
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000932 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
933 // and the condition variable initialization to the CFG.
934 if (VarDecl *VD = I->getConditionVariable()) {
935 if (Expr *Init = VD->getInit()) {
936 autoCreateBlock();
937 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
938 addStmt(Init);
939 }
940 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000941
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000942 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000943}
Mike Stump6d9828c2009-07-17 01:31:16 +0000944
945
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000946CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000947 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000948 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000949 // NOTE: If a "return" appears in the middle of a block, this means that the
950 // code afterwards is DEAD (unreachable). We still keep a basic block
951 // for that code; a simple "mark-and-sweep" from the entry block will be
952 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000953 if (Block)
954 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000955
956 // Create the new block.
957 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000958
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000959 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000960 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000961
962 // Add the return statement to the block. This may create new blocks if R
963 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000964 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000965}
966
967CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
968 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000969 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000970 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000971
Ted Kremenek4f880632009-07-17 22:18:43 +0000972 if (!LabelBlock) // This can happen when the body is empty, i.e.
973 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000974
Ted Kremenek4f880632009-07-17 22:18:43 +0000975 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000976 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000977
978 // Labels partition blocks, so this is the end of the basic block we were
979 // processing (L is the block's label). Because this is label (and we have
980 // already processed the substatement) there is no extra control-flow to worry
981 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000982 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000983 if (!FinishBlock(LabelBlock))
984 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000985
986 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000987 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000988
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000989 // This block is now the implicit successor of other blocks.
990 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000991
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000992 return LabelBlock;
993}
994
995CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000996 // Goto is a control-flow statement. Thus we stop processing the current
997 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000998 if (Block)
999 FinishBlock(Block);
1000
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001001 Block = createBlock(false);
1002 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001003
1004 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001005 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001006
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001007 if (I == LabelMap.end())
1008 // We will need to backpatch this block later.
1009 BackpatchBlocks.push_back(Block);
1010 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001011 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001012
Mike Stump6d9828c2009-07-17 01:31:16 +00001013 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001014}
1015
1016CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001017 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001018
Mike Stumpfefb9f72009-07-21 01:12:51 +00001019 // "for" is a control-flow statement. Thus we stop processing the current
1020 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001021 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001022 if (!FinishBlock(Block))
1023 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001024 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001025 } else
1026 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001027
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001028 // Save the current value for the break targets.
1029 // All breaks should go to the code following the loop.
1030 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1031 BreakTargetBlock = LoopSuccessor;
1032
Mike Stump6d9828c2009-07-17 01:31:16 +00001033 // Because of short-circuit evaluation, the condition of the loop can span
1034 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1035 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001036 CFGBlock* ExitConditionBlock = createBlock(false);
1037 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001038
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001039 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001040 ExitConditionBlock->setTerminator(F);
1041
1042 // Now add the actual condition to the condition block. Because the condition
1043 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001044 if (Stmt* C = F->getCond()) {
1045 Block = ExitConditionBlock;
1046 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001047 assert(Block == EntryConditionBlock);
1048
1049 // If this block contains a condition variable, add both the condition
1050 // variable and initializer to the CFG.
1051 if (VarDecl *VD = F->getConditionVariable()) {
1052 if (Expr *Init = VD->getInit()) {
1053 autoCreateBlock();
1054 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1055 EntryConditionBlock = addStmt(Init);
1056 assert(Block == EntryConditionBlock);
1057 }
1058 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001059
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001060 if (Block) {
1061 if (!FinishBlock(EntryConditionBlock))
1062 return 0;
1063 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001064 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001065
Mike Stump6d9828c2009-07-17 01:31:16 +00001066 // The condition block is the implicit successor for the loop body as well as
1067 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001068 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001069
Mike Stump00998a02009-07-23 23:25:26 +00001070 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001071 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Mike Stump00998a02009-07-23 23:25:26 +00001073 if (F->getCond())
1074 KnownVal = TryEvaluateBool(F->getCond());
1075
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001076 // Now create the loop body.
1077 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001078 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001079
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001080 // Save the current values for Block, Succ, and continue targets.
1081 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1082 save_continue(ContinueTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001083
Ted Kremenekaf603f72007-08-30 18:39:40 +00001084 // Create a new block to contain the (bottom) of the loop body.
1085 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001086
Ted Kremeneke9334502008-09-04 21:48:47 +00001087 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001088 // Generate increment code in its own basic block. This is the target of
1089 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001090 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001091 } else {
1092 // No increment code. Create a special, empty, block that is used as the
1093 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001094 assert(Succ == EntryConditionBlock);
1095 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001096 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001097
Ted Kremenek3575f842009-04-28 00:51:56 +00001098 // Finish up the increment (or empty) block if it hasn't been already.
1099 if (Block) {
1100 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001101 if (!FinishBlock(Block))
1102 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001103 Block = 0;
1104 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001105
Ted Kremenek3575f842009-04-28 00:51:56 +00001106 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001107
Ted Kremenek3575f842009-04-28 00:51:56 +00001108 // The starting block for the loop increment is the block that should
1109 // represent the 'loop target' for looping back to the start of the loop.
1110 ContinueTargetBlock->setLoopTarget(F);
1111
Mike Stump6d9828c2009-07-17 01:31:16 +00001112 // Now populate the body block, and in the process create new blocks as we
1113 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001114 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001115
1116 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001117 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001118 else if (Block && !FinishBlock(BodyBlock))
1119 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001120
Ted Kremenek941fde82009-07-24 04:47:11 +00001121 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001122 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001123 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001124
Ted Kremenek941fde82009-07-24 04:47:11 +00001125 // Link up the condition block with the code that follows the loop. (the
1126 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001127 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001128
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001129 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001130 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001131 if (Stmt* I = F->getInit()) {
1132 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001133 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001134 } else {
1135 // There is no loop initialization. We are thus basically a while loop.
1136 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001137 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001138 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001139 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001140 }
1141}
1142
Ted Kremenek115c1b92010-04-11 17:02:10 +00001143CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1144 if (asc.alwaysAdd()) {
1145 autoCreateBlock();
1146 AppendStmt(Block, M, asc);
1147 }
1148 return Visit(M->getBase(),
1149 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1150 : AddStmtChoice::AsLValueNotAlwaysAdd);
1151}
1152
Ted Kremenek514de5a2008-11-11 17:10:00 +00001153CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1154 // Objective-C fast enumeration 'for' statements:
1155 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1156 //
1157 // for ( Type newVariable in collection_expression ) { statements }
1158 //
1159 // becomes:
1160 //
1161 // prologue:
1162 // 1. collection_expression
1163 // T. jump to loop_entry
1164 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001165 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001166 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1167 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1168 // TB:
1169 // statements
1170 // T. jump to loop_entry
1171 // FB:
1172 // what comes after
1173 //
1174 // and
1175 //
1176 // Type existingItem;
1177 // for ( existingItem in expression ) { statements }
1178 //
1179 // becomes:
1180 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001181 // the same with newVariable replaced with existingItem; the binding works
1182 // the same except that for one ObjCForCollectionStmt::getElement() returns
1183 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001184 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001185
Ted Kremenek514de5a2008-11-11 17:10:00 +00001186 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001187
Ted Kremenek514de5a2008-11-11 17:10:00 +00001188 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001189 if (!FinishBlock(Block))
1190 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001191 LoopSuccessor = Block;
1192 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001193 } else
1194 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001195
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001196 // Build the condition blocks.
1197 CFGBlock* ExitConditionBlock = createBlock(false);
1198 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001199
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001200 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001201 ExitConditionBlock->setTerminator(S);
1202
1203 // The last statement in the block should be the ObjCForCollectionStmt, which
1204 // performs the actual binding to 'element' and determines if there are any
1205 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001206 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001207 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001208
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001209 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001210 // generate new blocks as necesary. We DON'T add the statement by default to
1211 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001212 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001213 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001214 if (!FinishBlock(EntryConditionBlock))
1215 return 0;
1216 Block = 0;
1217 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
1219 // The condition block is the implicit successor for the loop body as well as
1220 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001221 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001222
Ted Kremenek514de5a2008-11-11 17:10:00 +00001223 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001224 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001225 // Save the current values for Succ, continue and break targets.
1226 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001227 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1228
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001229 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001230 ContinueTargetBlock = EntryConditionBlock;
1231
Ted Kremenek4f880632009-07-17 22:18:43 +00001232 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001233
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001234 if (!BodyBlock)
1235 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001236 else if (Block) {
1237 if (!FinishBlock(BodyBlock))
1238 return 0;
1239 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001240
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001241 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001242 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001243 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001244
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001245 // Link up the condition block with the code that follows the loop.
1246 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001247 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001248
Ted Kremenek514de5a2008-11-11 17:10:00 +00001249 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001250 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001251 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001252}
1253
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001254CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1255 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001256
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001257 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001258 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001259
Ted Kremenekda5348e2009-05-05 23:11:51 +00001260 // The sync body starts its own basic block. This makes it a little easier
1261 // for diagnostic clients.
1262 if (SyncBlock) {
1263 if (!FinishBlock(SyncBlock))
1264 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001265
Ted Kremenekda5348e2009-05-05 23:11:51 +00001266 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001267 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001268 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001269
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001270 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001271 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001272}
Mike Stump6d9828c2009-07-17 01:31:16 +00001273
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001274CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001275 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001276 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001277}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001278
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001279CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001281
Mike Stumpfefb9f72009-07-21 01:12:51 +00001282 // "while" is a control-flow statement. Thus we stop processing the current
1283 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001284 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001285 if (!FinishBlock(Block))
1286 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001287 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001288 } else
1289 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001290
1291 // Because of short-circuit evaluation, the condition of the loop can span
1292 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1293 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001294 CFGBlock* ExitConditionBlock = createBlock(false);
1295 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001296
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001297 // Set the terminator for the "exit" condition block.
1298 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001299
1300 // Now add the actual condition to the condition block. Because the condition
1301 // itself may contain control-flow, new blocks may be created. Thus we update
1302 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001303 if (Stmt* C = W->getCond()) {
1304 Block = ExitConditionBlock;
1305 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001306 assert(Block == EntryConditionBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001307
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001308 // If this block contains a condition variable, add both the condition
1309 // variable and initializer to the CFG.
1310 if (VarDecl *VD = W->getConditionVariable()) {
1311 if (Expr *Init = VD->getInit()) {
1312 autoCreateBlock();
1313 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1314 EntryConditionBlock = addStmt(Init);
1315 assert(Block == EntryConditionBlock);
1316 }
1317 }
1318
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001319 if (Block) {
1320 if (!FinishBlock(EntryConditionBlock))
1321 return 0;
1322 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001323 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
1325 // The condition block is the implicit successor for the loop body as well as
1326 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001327 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001328
Mike Stump00998a02009-07-23 23:25:26 +00001329 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001330 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001331
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001332 // Process the loop body.
1333 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001334 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001335
1336 // Save the current values for Block, Succ, and continue and break targets
1337 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1338 save_continue(ContinueTargetBlock),
1339 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001340
Mike Stump6d9828c2009-07-17 01:31:16 +00001341 // Create an empty block to represent the transition block for looping back
1342 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001343 Block = 0;
1344 assert(Succ == EntryConditionBlock);
1345 Succ = createBlock();
1346 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001347 ContinueTargetBlock = Succ;
1348
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001349 // All breaks should go to the code following the loop.
1350 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001351
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001352 // NULL out Block to force lazy instantiation of blocks for the body.
1353 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001354
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001355 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001356 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001357
Ted Kremenekaf603f72007-08-30 18:39:40 +00001358 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001359 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001360 else if (Block) {
1361 if (!FinishBlock(BodyBlock))
1362 return 0;
1363 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001364
Ted Kremenek941fde82009-07-24 04:47:11 +00001365 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001366 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001367 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001368
Ted Kremenek941fde82009-07-24 04:47:11 +00001369 // Link up the condition block with the code that follows the loop. (the
1370 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001371 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
1373 // There can be no more statements in the condition block since we loop back
1374 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001375 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001376
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001377 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001378 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001379 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001380}
Mike Stump1eb44332009-09-09 15:08:12 +00001381
1382
Ted Kremenek4f880632009-07-17 22:18:43 +00001383CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1384 // FIXME: For now we pretend that @catch and the code it contains does not
1385 // exit.
1386 return Block;
1387}
Mike Stump6d9828c2009-07-17 01:31:16 +00001388
Ted Kremenek2fda5042008-12-09 20:20:09 +00001389CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1390 // FIXME: This isn't complete. We basically treat @throw like a return
1391 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
Ted Kremenek6c249722009-09-24 18:45:41 +00001393 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001394 if (Block && !FinishBlock(Block))
1395 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001396
Ted Kremenek2fda5042008-12-09 20:20:09 +00001397 // Create the new block.
1398 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001399
Ted Kremenek2fda5042008-12-09 20:20:09 +00001400 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001401 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001402
1403 // Add the statement to the block. This may create new blocks if S contains
1404 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001405 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001406}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001407
Mike Stump0979d802009-07-22 22:56:04 +00001408CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001409 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001410 if (Block && !FinishBlock(Block))
1411 return 0;
1412
1413 // Create the new block.
1414 Block = createBlock(false);
1415
Mike Stump5d1d2022010-01-19 02:20:09 +00001416 if (TryTerminatedBlock)
1417 // The current try statement is the only successor.
1418 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001419 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001420 // otherwise the Exit block is the only successor.
1421 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001422
1423 // Add the statement to the block. This may create new blocks if S contains
1424 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001425 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001426}
1427
Ted Kremenek4f880632009-07-17 22:18:43 +00001428CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001430
Mike Stump8f9893a2009-07-21 01:27:50 +00001431 // "do...while" is a control-flow statement. Thus we stop processing the
1432 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001434 if (!FinishBlock(Block))
1435 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001436 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001437 } else
1438 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001439
1440 // Because of short-circuit evaluation, the condition of the loop can span
1441 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1442 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001443 CFGBlock* ExitConditionBlock = createBlock(false);
1444 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001445
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001446 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001447 ExitConditionBlock->setTerminator(D);
1448
1449 // Now add the actual condition to the condition block. Because the condition
1450 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001451 if (Stmt* C = D->getCond()) {
1452 Block = ExitConditionBlock;
1453 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001454 if (Block) {
1455 if (!FinishBlock(EntryConditionBlock))
1456 return 0;
1457 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001458 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001459
Ted Kremenek54827132008-02-27 07:20:00 +00001460 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001461 Succ = EntryConditionBlock;
1462
Mike Stump00998a02009-07-23 23:25:26 +00001463 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001464 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001465
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001466 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001467 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001468 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001469 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001470
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001471 // Save the current values for Block, Succ, and continue and break targets
1472 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001473 save_continue(ContinueTargetBlock),
1474 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001476 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001477 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001478
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001479 // All breaks should go to the code following the loop.
1480 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001481
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482 // NULL out Block to force lazy instantiation of blocks for the body.
1483 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001484
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001485 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001486 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001487
Ted Kremenekaf603f72007-08-30 18:39:40 +00001488 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001489 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001490 else if (Block) {
1491 if (!FinishBlock(BodyBlock))
1492 return 0;
1493 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001494
Ted Kremenekd173dc72010-08-17 20:59:56 +00001495 if (!KnownVal.isFalse()) {
1496 // Add an intermediate block between the BodyBlock and the
1497 // ExitConditionBlock to represent the "loop back" transition. Create an
1498 // empty block to represent the transition block for looping back to the
1499 // head of the loop.
1500 // FIXME: Can we do this more efficiently without adding another block?
1501 Block = NULL;
1502 Succ = BodyBlock;
1503 CFGBlock *LoopBackBlock = createBlock();
1504 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001505
Ted Kremenekd173dc72010-08-17 20:59:56 +00001506 // Add the loop body entry as a successor to the condition.
1507 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1508 }
1509 else
1510 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001511 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001512
Ted Kremenek941fde82009-07-24 04:47:11 +00001513 // Link up the condition block with the code that follows the loop.
1514 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001515 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001516
1517 // There can be no more statements in the body block(s) since we loop back to
1518 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001519 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001520
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001521 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001522 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001523 return BodyBlock;
1524}
1525
1526CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1527 // "continue" is a control-flow statement. Thus we stop processing the
1528 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001529 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001530 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001531
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001532 // Now create a new block that ends with the continue statement.
1533 Block = createBlock(false);
1534 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001536 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001537 // incomplete AST. This means the CFG cannot be constructed.
1538 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001539 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001540 else
1541 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001542
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001543 return Block;
1544}
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001546CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001547 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001548
Ted Kremenek852274d2009-12-16 03:18:58 +00001549 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001550 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001551 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Ted Kremenek4f880632009-07-17 22:18:43 +00001554 // VLA types have expressions that must be evaluated.
1555 if (E->isArgumentType()) {
1556 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1557 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1558 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Mike Stump6d9828c2009-07-17 01:31:16 +00001561 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001562}
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek4f880632009-07-17 22:18:43 +00001564/// VisitStmtExpr - Utility method to handle (nested) statement
1565/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001566CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1567 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001568 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001569 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001570 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001571 return VisitCompoundStmt(SE->getSubStmt());
1572}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001573
Ted Kremenek411cdee2008-04-16 21:10:48 +00001574CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001575 // "switch" is a control-flow statement. Thus we stop processing the current
1576 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001577 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001578
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001579 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001580 if (!FinishBlock(Block))
1581 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001582 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001583 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001584
1585 // Save the current "switch" context.
1586 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001587 save_break(BreakTargetBlock),
1588 save_default(DefaultCaseBlock);
1589
Mike Stump6d9828c2009-07-17 01:31:16 +00001590 // Set the "default" case to be the block after the switch statement. If the
1591 // switch statement contains a "default:", this value will be overwritten with
1592 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001593 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001594
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001595 // Create a new block that will contain the switch statement.
1596 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001597
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001598 // Now process the switch body. The code after the switch is the implicit
1599 // successor.
1600 Succ = SwitchSuccessor;
1601 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001602
1603 // When visiting the body, the case statements should automatically get linked
1604 // up to the switch. We also don't keep a pointer to the body, since all
1605 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001606 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001607 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001608 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001609 if (Block) {
1610 if (!FinishBlock(BodyBlock))
1611 return 0;
1612 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001613
Mike Stump6d9828c2009-07-17 01:31:16 +00001614 // If we have no "default:" case, the default transition is to the code
1615 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001616 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001618 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001619 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001620 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001621 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001622 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001623
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001624 // Finally, if the SwitchStmt contains a condition variable, add both the
1625 // SwitchStmt and the condition variable initialization to the CFG.
1626 if (VarDecl *VD = Terminator->getConditionVariable()) {
1627 if (Expr *Init = VD->getInit()) {
1628 autoCreateBlock();
1629 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1630 addStmt(Init);
1631 }
1632 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001633
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001634 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001635}
1636
Ted Kremenek4f880632009-07-17 22:18:43 +00001637CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001638 // CaseStmts are essentially labels, so they are the first statement in a
1639 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001640 CFGBlock *TopBlock = 0, *LastBlock = 0;
1641
1642 if (Stmt *Sub = CS->getSubStmt()) {
1643 // For deeply nested chains of CaseStmts, instead of doing a recursion
1644 // (which can blow out the stack), manually unroll and create blocks
1645 // along the way.
1646 while (isa<CaseStmt>(Sub)) {
1647 CFGBlock *CurrentBlock = createBlock(false);
1648 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001649
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001650 if (TopBlock)
1651 AddSuccessor(LastBlock, CurrentBlock);
1652 else
1653 TopBlock = CurrentBlock;
1654
1655 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1656 LastBlock = CurrentBlock;
1657
1658 CS = cast<CaseStmt>(Sub);
1659 Sub = CS->getSubStmt();
1660 }
1661
1662 addStmt(Sub);
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001665 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001666 if (!CaseBlock)
1667 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001668
1669 // Cases statements partition blocks, so this is the top of the basic block we
1670 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001671 CaseBlock->setLabel(CS);
1672
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001673 if (!FinishBlock(CaseBlock))
1674 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001675
1676 // Add this block to the list of successors for the block with the switch
1677 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001678 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001679 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001680
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001681 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1682 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001683
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001684 if (TopBlock) {
1685 AddSuccessor(LastBlock, CaseBlock);
1686 Succ = TopBlock;
1687 }
1688 else {
1689 // This block is now the implicit successor of other blocks.
1690 Succ = CaseBlock;
1691 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001692
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001693 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001694}
Mike Stump6d9828c2009-07-17 01:31:16 +00001695
Ted Kremenek411cdee2008-04-16 21:10:48 +00001696CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001697 if (Terminator->getSubStmt())
1698 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001700 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001701
1702 if (!DefaultCaseBlock)
1703 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001704
1705 // Default statements partition blocks, so this is the top of the basic block
1706 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001707 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001709 if (!FinishBlock(DefaultCaseBlock))
1710 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001711
Mike Stump6d9828c2009-07-17 01:31:16 +00001712 // Unlike case statements, we don't add the default block to the successors
1713 // for the switch statement immediately. This is done when we finish
1714 // processing the switch statement. This allows for the default case
1715 // (including a fall-through to the code after the switch statement) to always
1716 // be the last successor of a switch-terminated block.
1717
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001718 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1719 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001720
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001721 // This block is now the implicit successor of other blocks.
1722 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001723
1724 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001725}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001726
Mike Stump5d1d2022010-01-19 02:20:09 +00001727CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1728 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1729 // current block.
1730 CFGBlock* TrySuccessor = NULL;
1731
1732 if (Block) {
1733 if (!FinishBlock(Block))
1734 return 0;
1735 TrySuccessor = Block;
1736 } else TrySuccessor = Succ;
1737
Mike Stumpa1f93632010-01-20 01:15:34 +00001738 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001739
1740 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001741 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001742 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001743 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001744
Mike Stumpa1f93632010-01-20 01:15:34 +00001745 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001746 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1747 // The code after the try is the implicit successor.
1748 Succ = TrySuccessor;
1749 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001750 if (CS->getExceptionDecl() == 0) {
1751 HasCatchAll = true;
1752 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001753 Block = NULL;
1754 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1755 if (CatchBlock == 0)
1756 return 0;
1757 // Add this block to the list of successors for the block with the try
1758 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001759 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001760 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001761 if (!HasCatchAll) {
1762 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001763 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001764 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001765 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001766 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001767
1768 // The code after the try is the implicit successor.
1769 Succ = TrySuccessor;
1770
Mike Stumpf00cca52010-01-20 01:30:58 +00001771 // Save the current "try" context.
1772 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1773 TryTerminatedBlock = NewTryTerminatedBlock;
1774
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001775 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001776 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001777 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001778 return Block;
1779}
1780
1781CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1782 // CXXCatchStmt are treated like labels, so they are the first statement in a
1783 // block.
1784
1785 if (CS->getHandlerBlock())
1786 addStmt(CS->getHandlerBlock());
1787
1788 CFGBlock* CatchBlock = Block;
1789 if (!CatchBlock)
1790 CatchBlock = createBlock();
1791
1792 CatchBlock->setLabel(CS);
1793
1794 if (!FinishBlock(CatchBlock))
1795 return 0;
1796
1797 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1798 Block = NULL;
1799
1800 return CatchBlock;
1801}
1802
Ted Kremenekad5a8942010-08-02 23:46:59 +00001803CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001804 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001805 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001806 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001807 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001808 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001809 return VisitChildren(C);
1810}
1811
Ted Kremenek19bb3562007-08-28 19:26:49 +00001812CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001813 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001814 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Ted Kremenek19bb3562007-08-28 19:26:49 +00001816 if (!IBlock) {
1817 IBlock = createBlock(false);
1818 cfg->setIndirectGotoBlock(IBlock);
1819 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001820
Ted Kremenek19bb3562007-08-28 19:26:49 +00001821 // IndirectGoto is a control-flow statement. Thus we stop processing the
1822 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001823 if (Block && !FinishBlock(Block))
1824 return 0;
1825
Ted Kremenek19bb3562007-08-28 19:26:49 +00001826 Block = createBlock(false);
1827 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001828 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001829 return addStmt(I->getTarget());
1830}
1831
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001832} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001833
Mike Stump6d9828c2009-07-17 01:31:16 +00001834/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1835/// no successors or predecessors. If this is the first block created in the
1836/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001837CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001838 bool first_block = begin() == end();
1839
1840 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001841 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1842 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1843 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001844
1845 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001846 if (first_block)
1847 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001848
1849 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001850 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001851}
1852
Ted Kremenek026473c2007-08-23 16:51:22 +00001853/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1854/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001855CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekad5a8942010-08-02 23:46:59 +00001856 bool PruneTriviallyFalse,
Mike Stump4c45aa12010-01-21 15:20:48 +00001857 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001858 CFGBuilder Builder;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001859 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
1860 AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001861}
1862
Ted Kremenek63f58872007-10-01 19:33:33 +00001863//===----------------------------------------------------------------------===//
1864// CFG: Queries for BlkExprs.
1865//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001866
Ted Kremenek63f58872007-10-01 19:33:33 +00001867namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001868 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001869}
1870
Ted Kremenek8a693662009-12-23 23:37:10 +00001871static void FindSubExprAssignments(Stmt *S,
1872 llvm::SmallPtrSet<Expr*,50>& Set) {
1873 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001874 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001875
Ted Kremenek8a693662009-12-23 23:37:10 +00001876 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001877 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00001878 if (!child)
1879 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001880
Ted Kremenek8a693662009-12-23 23:37:10 +00001881 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001882 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
Ted Kremenek8a693662009-12-23 23:37:10 +00001884 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001885 }
1886}
1887
Ted Kremenek63f58872007-10-01 19:33:33 +00001888static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1889 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001890
1891 // Look for assignments that are used as subexpressions. These are the only
1892 // assignments that we want to *possibly* register as a block-level
1893 // expression. Basically, if an assignment occurs both in a subexpression and
1894 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001895 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001896
Ted Kremenek63f58872007-10-01 19:33:33 +00001897 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001898 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001899 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001900
Ted Kremenek411cdee2008-04-16 21:10:48 +00001901 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
1903 // Iterate over the statements again on identify the Expr* and Stmt* at the
1904 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001905
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001906 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001907 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001908
Ted Kremenek411cdee2008-04-16 21:10:48 +00001909 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001910 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001911 // expression are really "statements" whose value is never used by
1912 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001913 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001914 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001915 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1916 // Special handling for statement expressions. The last statement in
1917 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001918 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001919 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001920 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001921 (*M)[C->body_back()] = x;
1922 }
1923 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001924
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001925 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001926 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001927 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001928
Ted Kremenek411cdee2008-04-16 21:10:48 +00001929 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001930
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001931 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001932
Ted Kremenek390e48b2008-11-12 21:11:49 +00001933 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001934 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001935 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001936 }
1937 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001938
Ted Kremenek63f58872007-10-01 19:33:33 +00001939 return M;
1940}
1941
Ted Kremenek86946742008-01-17 20:48:37 +00001942CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1943 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001944 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
Ted Kremenek63f58872007-10-01 19:33:33 +00001946 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001947 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001948 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001949}
1950
1951unsigned CFG::getNumBlkExprs() {
1952 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1953 return M->size();
1954 else {
1955 // We assume callers interested in the number of BlkExprs will want
1956 // the map constructed if it doesn't already exist.
1957 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1958 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1959 }
1960}
1961
Ted Kremenek274f4332008-04-28 18:00:46 +00001962//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001963// Cleanup: CFG dstor.
1964//===----------------------------------------------------------------------===//
1965
Ted Kremenek63f58872007-10-01 19:33:33 +00001966CFG::~CFG() {
1967 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1968}
Mike Stump6d9828c2009-07-17 01:31:16 +00001969
Ted Kremenek7dba8602007-08-29 21:56:09 +00001970//===----------------------------------------------------------------------===//
1971// CFG pretty printing
1972//===----------------------------------------------------------------------===//
1973
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001974namespace {
1975
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001976class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001977 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1978 StmtMapTy StmtMap;
1979 signed CurrentBlock;
1980 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001981 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001982public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001983
Chris Lattnere4f21422009-06-30 01:26:17 +00001984 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1985 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001986 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1987 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001988 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001989 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001990 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001991 }
1992 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001993
Ted Kremenek42a509f2007-08-31 21:30:12 +00001994 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001995
Chris Lattnere4f21422009-06-30 01:26:17 +00001996 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001997 void setBlockID(signed i) { CurrentBlock = i; }
1998 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001999
Ted Kremeneka95d3752008-09-13 05:16:45 +00002000 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002001
Ted Kremenek411cdee2008-04-16 21:10:48 +00002002 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002003
2004 if (I == StmtMap.end())
2005 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
2007 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002008 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002009 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002010 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002011
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002012 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002013 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002014 }
2015};
Chris Lattnere4f21422009-06-30 01:26:17 +00002016} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002017
Chris Lattnere4f21422009-06-30 01:26:17 +00002018
2019namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002020class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002021 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002022
Ted Kremeneka95d3752008-09-13 05:16:45 +00002023 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002024 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002025 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002026public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002027 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002028 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002029 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002030
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002031 void VisitIfStmt(IfStmt* I) {
2032 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002033 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002034 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002036 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002037 void VisitStmt(Stmt* Terminator) {
2038 Terminator->printPretty(OS, Helper, Policy);
2039 }
2040
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002041 void VisitForStmt(ForStmt* F) {
2042 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002043 if (F->getInit())
2044 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002045 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002046 if (Stmt* C = F->getCond())
2047 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002048 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002049 if (F->getInc())
2050 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002051 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002052 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002053
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002054 void VisitWhileStmt(WhileStmt* W) {
2055 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002056 if (Stmt* C = W->getCond())
2057 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002058 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002059
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002060 void VisitDoStmt(DoStmt* D) {
2061 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002062 if (Stmt* C = D->getCond())
2063 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002064 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002065
Ted Kremenek411cdee2008-04-16 21:10:48 +00002066 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002067 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002068 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002069 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002070
Mike Stump5d1d2022010-01-19 02:20:09 +00002071 void VisitCXXTryStmt(CXXTryStmt* CS) {
2072 OS << "try ...";
2073 }
2074
Ted Kremenek805e9a82007-08-31 21:49:40 +00002075 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002076 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002077 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002078 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002079
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002080 void VisitChooseExpr(ChooseExpr* C) {
2081 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002082 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002083 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002084 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002085
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002086 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2087 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002088 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002089 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002090
Ted Kremenek805e9a82007-08-31 21:49:40 +00002091 void VisitBinaryOperator(BinaryOperator* B) {
2092 if (!B->isLogicalOp()) {
2093 VisitExpr(B);
2094 return;
2095 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002096
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002097 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002098
Ted Kremenek805e9a82007-08-31 21:49:40 +00002099 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002100 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002101 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002102 return;
John McCall2de56d12010-08-25 11:45:40 +00002103 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002104 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002105 return;
2106 default:
2107 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002108 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002109 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002111 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002112 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002113 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002114};
Chris Lattnere4f21422009-06-30 01:26:17 +00002115} // end anonymous namespace
2116
Mike Stump6d9828c2009-07-17 01:31:16 +00002117
Chris Lattnere4f21422009-06-30 01:26:17 +00002118static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002119 const CFGElement &E) {
2120 Stmt *Terminator = E;
2121
2122 if (E.asStartScope()) {
2123 OS << "start scope\n";
2124 return;
2125 }
2126 if (E.asEndScope()) {
2127 OS << "end scope\n";
2128 return;
2129 }
2130
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002131 if (Helper) {
2132 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002133 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002134 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002135
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002136 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002137 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002138 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002139 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002140 return;
2141 }
2142 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002143
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002144 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002145 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
John McCall2de56d12010-08-25 11:45:40 +00002146 if (B->getOpcode() == BO_Comma) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002147 OS << "... , ";
2148 Helper->handledStmt(B->getRHS(),OS);
2149 OS << '\n';
2150 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002151 }
2152 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002153 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002154
Chris Lattnere4f21422009-06-30 01:26:17 +00002155 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002156
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002157 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002158 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002159}
Mike Stump6d9828c2009-07-17 01:31:16 +00002160
Chris Lattnere4f21422009-06-30 01:26:17 +00002161static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2162 const CFGBlock& B,
2163 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002164
Ted Kremenek42a509f2007-08-31 21:30:12 +00002165 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002166
Ted Kremenek7dba8602007-08-29 21:56:09 +00002167 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002168 OS << "\n [ B" << B.getBlockID();
2169
Ted Kremenek42a509f2007-08-31 21:30:12 +00002170 if (&B == &cfg->getEntry())
2171 OS << " (ENTRY) ]\n";
2172 else if (&B == &cfg->getExit())
2173 OS << " (EXIT) ]\n";
2174 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002175 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002176 else
2177 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002178
Ted Kremenek9cffe732007-08-29 23:20:49 +00002179 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002180 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002181
2182 if (print_edges)
2183 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Mike Stump079bd722010-01-19 22:00:14 +00002185 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002186 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002187 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002188 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002189 C->getLHS()->printPretty(OS, Helper,
2190 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002191 if (C->getRHS()) {
2192 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002193 C->getRHS()->printPretty(OS, Helper,
2194 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002195 }
Mike Stump079bd722010-01-19 22:00:14 +00002196 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002197 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002198 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002199 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002200 if (CS->getExceptionDecl())
2201 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2202 0);
2203 else
2204 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002205 OS << ")";
2206
2207 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002208 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002209
Ted Kremenek9cffe732007-08-29 23:20:49 +00002210 OS << ":\n";
2211 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002212
Ted Kremenekfddd5182007-08-21 21:42:03 +00002213 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002214 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
Ted Kremenek42a509f2007-08-31 21:30:12 +00002216 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2217 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002218
Ted Kremenek9cffe732007-08-29 23:20:49 +00002219 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002220 if (print_edges)
2221 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002222
Ted Kremeneka95d3752008-09-13 05:16:45 +00002223 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002224
Ted Kremenek42a509f2007-08-31 21:30:12 +00002225 if (Helper)
2226 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002227
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002228 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002229 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002230
Ted Kremenek9cffe732007-08-29 23:20:49 +00002231 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002232 if (B.getTerminator()) {
2233 if (print_edges)
2234 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002235
Ted Kremenek9cffe732007-08-29 23:20:49 +00002236 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002237
Ted Kremenek42a509f2007-08-31 21:30:12 +00002238 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Chris Lattnere4f21422009-06-30 01:26:17 +00002240 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2241 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002242 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002243 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002244 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002245
Ted Kremenek9cffe732007-08-29 23:20:49 +00002246 if (print_edges) {
2247 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002248 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002249 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002250
Ted Kremenek42a509f2007-08-31 21:30:12 +00002251 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2252 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002253
Ted Kremenek42a509f2007-08-31 21:30:12 +00002254 if (i == 8 || (i-8) == 0)
2255 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002256
Ted Kremenek9cffe732007-08-29 23:20:49 +00002257 OS << " B" << (*I)->getBlockID();
2258 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002259
Ted Kremenek42a509f2007-08-31 21:30:12 +00002260 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002261
Ted Kremenek42a509f2007-08-31 21:30:12 +00002262 // Print the successors of this block.
2263 OS << " Successors (" << B.succ_size() << "):";
2264 i = 0;
2265
2266 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2267 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek42a509f2007-08-31 21:30:12 +00002269 if (i == 8 || (i-8) % 10 == 0)
2270 OS << "\n ";
2271
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002272 if (*I)
2273 OS << " B" << (*I)->getBlockID();
2274 else
2275 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002276 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002277
Ted Kremenek9cffe732007-08-29 23:20:49 +00002278 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002279 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002280}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002281
Ted Kremenek42a509f2007-08-31 21:30:12 +00002282
2283/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002284void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002285
2286/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002287void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2288 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002289
Ted Kremenek42a509f2007-08-31 21:30:12 +00002290 // Print the entry block.
2291 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002292
Ted Kremenek42a509f2007-08-31 21:30:12 +00002293 // Iterate through the CFGBlocks and print them one by one.
2294 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2295 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002296 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002297 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002298
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002299 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002300 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002301
Ted Kremenek42a509f2007-08-31 21:30:12 +00002302 // Print the exit block.
2303 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002304 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002305}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002306
2307/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002308void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2309 print(llvm::errs(), cfg, LO);
2310}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002311
2312/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2313/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002314void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2315 const LangOptions &LO) const {
2316 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002317 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002318}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002319
Ted Kremeneka2925852008-01-30 23:02:42 +00002320/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002321void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002322 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002323 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002324 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2325}
2326
Ted Kremenek390e48b2008-11-12 21:11:49 +00002327Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002328
Ted Kremenek411cdee2008-04-16 21:10:48 +00002329 if (!Terminator)
2330 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002331
Ted Kremenek411cdee2008-04-16 21:10:48 +00002332 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002333
Ted Kremenek411cdee2008-04-16 21:10:48 +00002334 switch (Terminator->getStmtClass()) {
2335 default:
2336 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002337
Ted Kremenek411cdee2008-04-16 21:10:48 +00002338 case Stmt::ForStmtClass:
2339 E = cast<ForStmt>(Terminator)->getCond();
2340 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002341
Ted Kremenek411cdee2008-04-16 21:10:48 +00002342 case Stmt::WhileStmtClass:
2343 E = cast<WhileStmt>(Terminator)->getCond();
2344 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002345
Ted Kremenek411cdee2008-04-16 21:10:48 +00002346 case Stmt::DoStmtClass:
2347 E = cast<DoStmt>(Terminator)->getCond();
2348 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002349
Ted Kremenek411cdee2008-04-16 21:10:48 +00002350 case Stmt::IfStmtClass:
2351 E = cast<IfStmt>(Terminator)->getCond();
2352 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002353
Ted Kremenek411cdee2008-04-16 21:10:48 +00002354 case Stmt::ChooseExprClass:
2355 E = cast<ChooseExpr>(Terminator)->getCond();
2356 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002357
Ted Kremenek411cdee2008-04-16 21:10:48 +00002358 case Stmt::IndirectGotoStmtClass:
2359 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2360 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002361
Ted Kremenek411cdee2008-04-16 21:10:48 +00002362 case Stmt::SwitchStmtClass:
2363 E = cast<SwitchStmt>(Terminator)->getCond();
2364 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002365
Ted Kremenek411cdee2008-04-16 21:10:48 +00002366 case Stmt::ConditionalOperatorClass:
2367 E = cast<ConditionalOperator>(Terminator)->getCond();
2368 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002369
Ted Kremenek411cdee2008-04-16 21:10:48 +00002370 case Stmt::BinaryOperatorClass: // '&&' and '||'
2371 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002372 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002373
Ted Kremenek390e48b2008-11-12 21:11:49 +00002374 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002375 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002376 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002377
Ted Kremenek411cdee2008-04-16 21:10:48 +00002378 return E ? E->IgnoreParens() : NULL;
2379}
2380
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002381bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002382
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002383 if (!Terminator)
2384 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002385
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002386 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002387
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002388 switch (Terminator->getStmtClass()) {
2389 default:
2390 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002391
2392 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002393 case Stmt::WhileStmtClass:
2394 case Stmt::DoStmtClass:
2395 case Stmt::IfStmtClass:
2396 case Stmt::ChooseExprClass:
2397 case Stmt::ConditionalOperatorClass:
2398 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002399 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002400 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002401
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002402 return E ? E->IgnoreParens() : NULL;
2403}
2404
Ted Kremeneka2925852008-01-30 23:02:42 +00002405
Ted Kremenek7dba8602007-08-29 21:56:09 +00002406//===----------------------------------------------------------------------===//
2407// CFG Graphviz Visualization
2408//===----------------------------------------------------------------------===//
2409
Ted Kremenek42a509f2007-08-31 21:30:12 +00002410
2411#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002412static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002413#endif
2414
Chris Lattnere4f21422009-06-30 01:26:17 +00002415void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002416#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002417 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002418 GraphHelper = &H;
2419 llvm::ViewGraph(this,"CFG");
2420 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002421#endif
2422}
2423
Ted Kremenek7dba8602007-08-29 21:56:09 +00002424namespace llvm {
2425template<>
2426struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002427
2428 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2429
2430 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002431
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002432#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002433 std::string OutSStr;
2434 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002435 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002436 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002437
2438 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2439
2440 // Process string output to make it nicer...
2441 for (unsigned i = 0; i != OutStr.length(); ++i)
2442 if (OutStr[i] == '\n') { // Left justify
2443 OutStr[i] = '\\';
2444 OutStr.insert(OutStr.begin()+i+1, 'l');
2445 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002446
Ted Kremenek7dba8602007-08-29 21:56:09 +00002447 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002448#else
2449 return "";
2450#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002451 }
2452};
2453} // end namespace llvm