blob: c7eb53d36135937283f694902bc47357b2d14690 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenek4aa1e8b2007-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 Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-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 Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000038
39class AddStmtChoice {
40public:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramera3b13412010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000047
Benjamin Kramera3b13412010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek66de6032010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000050
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051private:
Benjamin Kramera3b13412010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000053};
Mike Stump31feda52009-07-17 01:31:16 +000054
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-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 Stump31feda52009-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 Kremenek1b8ac852007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000072
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +000080
Ted Kremenekeda180e22007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump31feda52009-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 Kremenekde979ae2007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000089
Ted Kremenekeda180e22007-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 Stump31feda52009-07-17 01:31:16 +000093
94public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +0000100
Ted Kremenek9aae5132007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Mike Stump04c68512010-01-21 15:20:48 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges,
103 bool AddScopes);
Mike Stump31feda52009-07-17 01:31:16 +0000104
Ted Kremenek93668002009-07-17 22:18:43 +0000105private:
106 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000107 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
108 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
109 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000110 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000114 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000115 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000116 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000117 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000118 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000119 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000120 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000121 CFGBlock *VisitDeclStmt(DeclStmt *DS);
122 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000123 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
124 CFGBlock *VisitDoStmt(DoStmt *D);
125 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000126 CFGBlock *VisitGotoStmt(GotoStmt* G);
127 CFGBlock *VisitIfStmt(IfStmt *I);
128 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
129 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000130 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000131 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
132 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
133 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
134 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
135 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
136 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000137 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
138 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000139 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
140 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000141
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000142 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
143 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000144 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000145
Ted Kremenek6065ef62008-04-28 18:00:46 +0000146 // NYS == Not Yet Supported
147 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000148 badCFG = true;
149 return Block;
150 }
Mike Stump31feda52009-07-17 01:31:16 +0000151
Mike Stump92244b02010-01-19 22:00:14 +0000152 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
153 if (!AddScopes)
154 return B;
155
156 if (B == 0)
157 B = createBlock();
158 B->StartScope(S, cfg->getBumpVectorContext());
159 return B;
160 }
161
162 void EndScope(Stmt *S) {
163 if (!AddScopes)
164 return;
165
166 if (Block == 0)
167 Block = createBlock();
168 Block->EndScope(S, cfg->getBumpVectorContext());
169 }
170
Ted Kremenek93668002009-07-17 22:18:43 +0000171 void autoCreateBlock() { if (!Block) Block = createBlock(); }
172 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000173 bool FinishBlock(CFGBlock* B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000174 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
175 return Visit(S, asc);
176 }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000177
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000178 void AppendStmt(CFGBlock *B, Stmt *S,
179 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
180 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000181 }
182
183 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
184 B->addSuccessor(S, cfg->getBumpVectorContext());
185 }
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek963cc312009-07-24 06:55:42 +0000187 /// TryResult - a class representing a variant over the values
188 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
189 /// and is used by the CFGBuilder to decide if a branch condition
190 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000191 class TryResult {
192 int X;
193 public:
194 TryResult(bool b) : X(b ? 1 : 0) {}
195 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000196
Ted Kremenek30754282009-07-24 04:47:11 +0000197 bool isTrue() const { return X == 1; }
198 bool isFalse() const { return X == 0; }
199 bool isKnown() const { return X >= 0; }
200 void negate() {
201 assert(isKnown());
202 X ^= 0x1;
203 }
204 };
Mike Stump11289f42009-09-09 15:08:12 +0000205
Mike Stump773582d2009-07-23 23:25:26 +0000206 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
207 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000208 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000209 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000210 if (!S->isTypeDependent() && !S->isValueDependent() &&
211 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000212 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000213
214 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000215 }
216
Ted Kremenekb64d1832008-03-13 03:04:22 +0000217 bool badCFG;
Mike Stump04c68512010-01-21 15:20:48 +0000218
219 // True iff EH edges on CallExprs should be added to the CFG.
220 bool AddEHEdges;
221
222 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump92244b02010-01-19 22:00:14 +0000223 bool AddScopes;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000224};
Mike Stump31feda52009-07-17 01:31:16 +0000225
Douglas Gregor4619e432008-12-05 23:32:09 +0000226// FIXME: Add support for dependent-sized array types in C++?
227// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000228static VariableArrayType* FindVA(Type* t) {
229 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
230 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
231 if (vat->getSizeExpr())
232 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000233
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000234 t = vt->getElementType().getTypePtr();
235 }
Mike Stump31feda52009-07-17 01:31:16 +0000236
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000237 return 0;
238}
Mike Stump31feda52009-07-17 01:31:16 +0000239
240/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
241/// arbitrary statement. Examples include a single expression or a function
242/// body (compound statement). The ownership of the returned CFG is
243/// transferred to the caller. If CFG construction fails, this method returns
244/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000245CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stumpcc3a8532010-01-21 17:21:23 +0000246 bool addehedges, bool AddScopes) {
247 AddEHEdges = addehedges;
Mike Stump0d76d072009-07-20 23:24:15 +0000248 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000249 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000250 if (!Statement)
251 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000252
Mike Stump92244b02010-01-19 22:00:14 +0000253 this->AddScopes = AddScopes;
Ted Kremenekb64d1832008-03-13 03:04:22 +0000254 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000255
256 // Create an empty block that will serve as the exit block for the CFG. Since
257 // this is the first block added to the CFG, it will be implicitly registered
258 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000259 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000260 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000261 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000262
Ted Kremenek9aae5132007-08-23 21:42:29 +0000263 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000264 CFGBlock* B = addStmt(Statement);
Mike Stump6bf1c082010-01-21 02:21:40 +0000265
266 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
267 // FIXME: Add code for base initializers and member initializers.
268 (void)CD;
269 }
Ted Kremenek8aed4902009-10-20 23:46:25 +0000270 if (!B)
271 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000272
Daniel Dunbar260918c2010-02-22 05:58:59 +0000273 if (B) {
274 // Finalize the last constructed block. This usually involves reversing the
275 // order of the statements in the block.
276 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000277
Daniel Dunbar260918c2010-02-22 05:58:59 +0000278 // Backpatch the gotos whose label -> block mappings we didn't know when we
279 // encountered them.
280 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000281 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000282
Daniel Dunbar260918c2010-02-22 05:58:59 +0000283 CFGBlock* B = *I;
284 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
285 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000286
Daniel Dunbar260918c2010-02-22 05:58:59 +0000287 // If there is no target for the goto, then we are looking at an
288 // incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000289 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000290
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000291 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000292 }
Mike Stump31feda52009-07-17 01:31:16 +0000293
Daniel Dunbar260918c2010-02-22 05:58:59 +0000294 // Add successors to the Indirect Goto Dispatch block (if we have one).
295 if (CFGBlock* B = cfg->getIndirectGotoBlock())
296 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
297 E = AddressTakenLabels.end(); I != E; ++I ) {
298
299 // Lookup the target block.
300 LabelMapTy::iterator LI = LabelMap.find(*I);
301
302 // If there is no target block that contains label, then we are looking
303 // at an incomplete AST. Handle this by not registering a successor.
304 if (LI == LabelMap.end()) continue;
305
306 AddSuccessor(B, LI->second);
307 }
308
309 Succ = B;
310 }
Mike Stump31feda52009-07-17 01:31:16 +0000311
312 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000313 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000314
Ted Kremenekab929bb2009-10-20 23:59:28 +0000315 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000316}
Mike Stump31feda52009-07-17 01:31:16 +0000317
Ted Kremenek9aae5132007-08-23 21:42:29 +0000318/// createBlock - Used to lazily create blocks that are connected
319/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000320CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000321 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000322 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000323 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000324 return B;
325}
Mike Stump31feda52009-07-17 01:31:16 +0000326
Ted Kremenek0868eea2009-09-24 18:45:41 +0000327/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000328bool CFGBuilder::FinishBlock(CFGBlock* B) {
329 if (badCFG)
330 return false;
331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000333 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000334}
335
Ted Kremenek93668002009-07-17 22:18:43 +0000336/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000337/// blocks for ternary operators, &&, and ||. We also process "," and
338/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000339CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000340tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000341 if (!S) {
342 badCFG = true;
343 return 0;
344 }
Ted Kremenek93668002009-07-17 22:18:43 +0000345 switch (S->getStmtClass()) {
346 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000347 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000348
349 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000350 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek93668002009-07-17 22:18:43 +0000352 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000353 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenek93668002009-07-17 22:18:43 +0000355 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000356 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000357
Ted Kremenek93668002009-07-17 22:18:43 +0000358 case Stmt::BreakStmtClass:
359 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000360
Ted Kremenek93668002009-07-17 22:18:43 +0000361 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000362 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremenek93668002009-07-17 22:18:43 +0000364 case Stmt::CaseStmtClass:
365 return VisitCaseStmt(cast<CaseStmt>(S));
366
367 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000368 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremenek93668002009-07-17 22:18:43 +0000370 case Stmt::CompoundStmtClass:
371 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremenek93668002009-07-17 22:18:43 +0000373 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000374 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek93668002009-07-17 22:18:43 +0000376 case Stmt::ContinueStmtClass:
377 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenekb27378c2010-01-19 20:40:33 +0000379 case Stmt::CXXCatchStmtClass:
380 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
381
Zhongxing Xu7e612172010-04-13 09:38:01 +0000382 case Stmt::CXXMemberCallExprClass:
383 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
384
Ted Kremenekb27378c2010-01-19 20:40:33 +0000385 case Stmt::CXXThrowExprClass:
386 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
387
388 case Stmt::CXXTryStmtClass:
389 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
390
Ted Kremenek93668002009-07-17 22:18:43 +0000391 case Stmt::DeclStmtClass:
392 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000393
Ted Kremenek93668002009-07-17 22:18:43 +0000394 case Stmt::DefaultStmtClass:
395 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek93668002009-07-17 22:18:43 +0000397 case Stmt::DoStmtClass:
398 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenek93668002009-07-17 22:18:43 +0000400 case Stmt::ForStmtClass:
401 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000402
Ted Kremenek93668002009-07-17 22:18:43 +0000403 case Stmt::GotoStmtClass:
404 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000405
Ted Kremenek93668002009-07-17 22:18:43 +0000406 case Stmt::IfStmtClass:
407 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000408
Ted Kremenek93668002009-07-17 22:18:43 +0000409 case Stmt::IndirectGotoStmtClass:
410 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenek93668002009-07-17 22:18:43 +0000412 case Stmt::LabelStmtClass:
413 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek5868ec62010-04-11 17:02:10 +0000415 case Stmt::MemberExprClass:
416 return VisitMemberExpr(cast<MemberExpr>(S), asc);
417
Ted Kremenek93668002009-07-17 22:18:43 +0000418 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000419 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
420
Ted Kremenek93668002009-07-17 22:18:43 +0000421 case Stmt::ObjCAtSynchronizedStmtClass:
422 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000423
Ted Kremenek93668002009-07-17 22:18:43 +0000424 case Stmt::ObjCAtThrowStmtClass:
425 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenek93668002009-07-17 22:18:43 +0000427 case Stmt::ObjCAtTryStmtClass:
428 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000429
Ted Kremenek93668002009-07-17 22:18:43 +0000430 case Stmt::ObjCForCollectionStmtClass:
431 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000432
Ted Kremenek93668002009-07-17 22:18:43 +0000433 case Stmt::ParenExprClass:
434 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000435 goto tryAgain;
436
Ted Kremenek93668002009-07-17 22:18:43 +0000437 case Stmt::NullStmtClass:
438 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Ted Kremenek93668002009-07-17 22:18:43 +0000440 case Stmt::ReturnStmtClass:
441 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000442
Ted Kremenek93668002009-07-17 22:18:43 +0000443 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000444 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek93668002009-07-17 22:18:43 +0000446 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000447 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000448
Ted Kremenek93668002009-07-17 22:18:43 +0000449 case Stmt::SwitchStmtClass:
450 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenek93668002009-07-17 22:18:43 +0000452 case Stmt::WhileStmtClass:
453 return VisitWhileStmt(cast<WhileStmt>(S));
454 }
455}
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000457CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
458 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000459 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000460 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000461 }
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenek93668002009-07-17 22:18:43 +0000463 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000464}
Mike Stump31feda52009-07-17 01:31:16 +0000465
Ted Kremenek93668002009-07-17 22:18:43 +0000466/// VisitChildren - Visit the children of a Stmt.
467CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
468 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000469 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000470 E = Terminator->child_end(); I != E; ++I) {
471 if (*I) B = Visit(*I);
472 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000473 return B;
474}
Mike Stump11289f42009-09-09 15:08:12 +0000475
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000476CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
477 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000478 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000479
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000480 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000481 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000482 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000483 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000484
Ted Kremenek9aae5132007-08-23 21:42:29 +0000485 return Block;
486}
Mike Stump11289f42009-09-09 15:08:12 +0000487
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000488CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
489 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000490 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000491 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000492 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek93668002009-07-17 22:18:43 +0000494 if (!FinishBlock(ConfluenceBlock))
495 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Ted Kremenek93668002009-07-17 22:18:43 +0000497 // create the block evaluating the LHS
498 CFGBlock* LHSBlock = createBlock(false);
499 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremenek93668002009-07-17 22:18:43 +0000501 // create the block evaluating the RHS
502 Succ = ConfluenceBlock;
503 Block = NULL;
504 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000505
506 if (RHSBlock) {
507 if (!FinishBlock(RHSBlock))
508 return 0;
509 }
510 else {
511 // Create an empty block for cases where the RHS doesn't require
512 // any explicit statements in the CFG.
513 RHSBlock = createBlock();
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Mike Stump773582d2009-07-23 23:25:26 +0000516 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000517 TryResult KnownVal = TryEvaluateBool(B->getLHS());
518 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
519 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000520
Ted Kremenek93668002009-07-17 22:18:43 +0000521 // Now link the LHSBlock with RHSBlock.
522 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000523 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
524 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000525 } else {
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000526 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000527 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
528 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000529 }
Mike Stump11289f42009-09-09 15:08:12 +0000530
Ted Kremenek93668002009-07-17 22:18:43 +0000531 // Generate the blocks for evaluating the LHS.
532 Block = LHSBlock;
533 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000534 }
Ted Kremenek93668002009-07-17 22:18:43 +0000535 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000536 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000537 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000538 addStmt(B->getRHS());
539 return addStmt(B->getLHS());
540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000542 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000543}
544
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000545CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
546 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000547 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000548 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000549 }
550 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000551}
552
Ted Kremenek93668002009-07-17 22:18:43 +0000553CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
554 // "break" is a control-flow statement. Thus we stop processing the current
555 // block.
556 if (Block && !FinishBlock(Block))
557 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000558
Ted Kremenek93668002009-07-17 22:18:43 +0000559 // Now create a new block that ends with the break statement.
560 Block = createBlock(false);
561 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000562
Ted Kremenek93668002009-07-17 22:18:43 +0000563 // If there is no target for the break, then we are looking at an incomplete
564 // AST. This means that the CFG cannot be constructed.
565 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000566 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000567 else
568 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000569
570
Ted Kremenek9aae5132007-08-23 21:42:29 +0000571 return Block;
572}
Mike Stump11289f42009-09-09 15:08:12 +0000573
Mike Stump04c68512010-01-21 15:20:48 +0000574static bool CanThrow(Expr *E) {
575 QualType Ty = E->getType();
576 if (Ty->isFunctionPointerType())
577 Ty = Ty->getAs<PointerType>()->getPointeeType();
578 else if (Ty->isBlockPointerType())
579 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
580
581 const FunctionType *FT = Ty->getAs<FunctionType>();
582 if (FT) {
583 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
584 if (Proto->hasEmptyExceptionSpec())
585 return false;
586 }
587 return true;
588}
589
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000590CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000591 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000592 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000593 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000594 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000595 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000596
Mike Stump04c68512010-01-21 15:20:48 +0000597 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000598
599 // Languages without exceptions are assumed to not throw.
600 if (Context->getLangOptions().Exceptions) {
Mike Stump04c68512010-01-21 15:20:48 +0000601 if (AddEHEdges)
602 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000603 }
604
605 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000606 if (FD->hasAttr<NoReturnAttr>())
607 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000608 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000609 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000610 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000611
Mike Stump04c68512010-01-21 15:20:48 +0000612 if (!CanThrow(C->getCallee()))
613 AddEHEdge = false;
614
615 if (!NoReturn && !AddEHEdge)
Zhongxing Xua396e612010-02-24 02:19:28 +0000616 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000617
Mike Stump92244b02010-01-19 22:00:14 +0000618 if (Block) {
619 Succ = Block;
620 if (!FinishBlock(Block))
621 return 0;
622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Mike Stump92244b02010-01-19 22:00:14 +0000624 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000625 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000626
Mike Stump92244b02010-01-19 22:00:14 +0000627 if (NoReturn) {
628 // Wire this to the exit block directly.
629 AddSuccessor(Block, &cfg->getExit());
630 }
Mike Stump04c68512010-01-21 15:20:48 +0000631 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000632 // Add exceptional edges.
633 if (TryTerminatedBlock)
634 AddSuccessor(Block, TryTerminatedBlock);
635 else
636 AddSuccessor(Block, &cfg->getExit());
637 }
Mike Stump11289f42009-09-09 15:08:12 +0000638
Mike Stump8c5d7992009-07-25 21:26:53 +0000639 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000640}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000641
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000642CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
643 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000644 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000645 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000646 if (!FinishBlock(ConfluenceBlock))
647 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000648
Ted Kremenek5868ec62010-04-11 17:02:10 +0000649 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
650 : AddStmtChoice::AlwaysAdd;
651
Ted Kremenek21822592009-07-17 18:20:32 +0000652 Succ = ConfluenceBlock;
653 Block = NULL;
Ted Kremenek5868ec62010-04-11 17:02:10 +0000654 CFGBlock* LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000655 if (!FinishBlock(LHSBlock))
656 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Ted Kremenek21822592009-07-17 18:20:32 +0000658 Succ = ConfluenceBlock;
659 Block = NULL;
Ted Kremenek5868ec62010-04-11 17:02:10 +0000660 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000661 if (!FinishBlock(RHSBlock))
662 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Ted Kremenek21822592009-07-17 18:20:32 +0000664 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000665 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000666 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000667 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
668 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000669 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000670 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000671}
Mike Stump11289f42009-09-09 15:08:12 +0000672
673
674CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump92244b02010-01-19 22:00:14 +0000675 EndScope(C);
676
Mike Stump11289f42009-09-09 15:08:12 +0000677 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000678
679 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
680 I != E; ++I ) {
681 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000682
Ted Kremenekce499c22009-08-27 23:16:26 +0000683 if (badCFG)
684 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000685 }
Mike Stump92244b02010-01-19 22:00:14 +0000686
687 LastBlock = StartScope(C, LastBlock);
688
Ted Kremenek93668002009-07-17 22:18:43 +0000689 return LastBlock;
690}
Mike Stump11289f42009-09-09 15:08:12 +0000691
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000692CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
693 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000694 // Create the confluence block that will "merge" the results of the ternary
695 // expression.
696 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000697 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000698 if (!FinishBlock(ConfluenceBlock))
699 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000700
Ted Kremenek5868ec62010-04-11 17:02:10 +0000701 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
702 : AddStmtChoice::AlwaysAdd;
703
Ted Kremenek51d40b02009-07-17 18:15:54 +0000704 // Create a block for the LHS expression if there is an LHS expression. A
705 // GCC extension allows LHS to be NULL, causing the condition to be the
706 // value that is returned instead.
707 // e.g: x ?: y is shorthand for: x ? x : y;
708 Succ = ConfluenceBlock;
709 Block = NULL;
710 CFGBlock* LHSBlock = NULL;
711 if (C->getLHS()) {
Ted Kremenek5868ec62010-04-11 17:02:10 +0000712 LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000713 if (!FinishBlock(LHSBlock))
714 return 0;
715 Block = NULL;
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Ted Kremenek51d40b02009-07-17 18:15:54 +0000718 // Create the block for the RHS expression.
719 Succ = ConfluenceBlock;
Ted Kremenek5868ec62010-04-11 17:02:10 +0000720 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000721 if (!FinishBlock(RHSBlock))
722 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000723
Ted Kremenek51d40b02009-07-17 18:15:54 +0000724 // Create the block that will contain the condition.
725 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Mike Stump773582d2009-07-23 23:25:26 +0000727 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000728 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000729 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000730 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000731 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000732 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000733 // If we know the condition is false, add NULL as the successor for
734 // the block containing the condition. In this case, the confluence
735 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000736 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000737 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000738 } else {
739 // If we have no LHS expression, add the ConfluenceBlock as a direct
740 // successor for the block containing the condition. Moreover, we need to
741 // reverse the order of the predecessors in the ConfluenceBlock because
742 // the RHSBlock will have been added to the succcessors already, and we
743 // want the first predecessor to the the block containing the expression
744 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000745 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000746 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000747 std::reverse(ConfluenceBlock->pred_begin(),
748 ConfluenceBlock->pred_end());
749 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000752 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000753 Block->setTerminator(C);
754 return addStmt(C->getCond());
755}
756
Ted Kremenek93668002009-07-17 22:18:43 +0000757CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
758 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000759
Ted Kremenek93668002009-07-17 22:18:43 +0000760 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000761 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000762 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenek93668002009-07-17 22:18:43 +0000765 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000766
Ted Kremenek93668002009-07-17 22:18:43 +0000767 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
768 typedef llvm::SmallVector<Decl*,10> BufTy;
769 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000770
Ted Kremenek93668002009-07-17 22:18:43 +0000771 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
772 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
773 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
774 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ted Kremenek93668002009-07-17 22:18:43 +0000776 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
777 // automatically freed with the CFG.
778 DeclGroupRef DG(*I);
779 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000780 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000781 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000782
Ted Kremenek93668002009-07-17 22:18:43 +0000783 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000784 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000785 B = VisitDeclSubExpr(D);
786 }
Mike Stump11289f42009-09-09 15:08:12 +0000787
788 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000789}
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791/// VisitDeclSubExpr - Utility method to add block-level expressions for
792/// initializers in Decls.
793CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
794 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000795
Ted Kremenek93668002009-07-17 22:18:43 +0000796 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenek93668002009-07-17 22:18:43 +0000798 if (!VD)
799 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek93668002009-07-17 22:18:43 +0000801 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000802
Ted Kremenek93668002009-07-17 22:18:43 +0000803 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000804 AddStmtChoice::Kind k =
805 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
806 : AddStmtChoice::NotAlwaysAdd;
807 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 // If the type of VD is a VLA, then we must process its size expressions.
811 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
812 VA = FindVA(VA->getElementType().getTypePtr()))
813 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek93668002009-07-17 22:18:43 +0000815 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000816}
817
818CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000819 // We may see an if statement in the middle of a basic block, or it may be the
820 // first statement we are processing. In either case, we create a new basic
821 // block. First, we create the blocks for the then...else statements, and
822 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000823 // middle of a block, we stop processing that block. That block is then the
824 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000825
826 // The block we were proccessing is now finished. Make it the successor
827 // block.
828 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000829 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000830 if (!FinishBlock(Block))
831 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832 }
Mike Stump31feda52009-07-17 01:31:16 +0000833
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000834 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000835 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000836
Ted Kremenek9aae5132007-08-23 21:42:29 +0000837 if (Stmt* Else = I->getElse()) {
838 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000839
Ted Kremenek9aae5132007-08-23 21:42:29 +0000840 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000841 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000842 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000843 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000844
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000845 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
846 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000847 else if (Block) {
848 if (!FinishBlock(ElseBlock))
849 return 0;
850 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000851 }
Mike Stump31feda52009-07-17 01:31:16 +0000852
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000853 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000854 CFGBlock* ThenBlock;
855 {
856 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000857 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000858 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000859 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000860 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000861
Ted Kremenek1b379512009-04-01 03:52:47 +0000862 if (!ThenBlock) {
863 // We can reach here if the "then" body has all NullStmts.
864 // Create an empty block so we can distinguish between true and false
865 // branches in path-sensitive analyses.
866 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000867 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000868 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000869 if (!FinishBlock(ThenBlock))
870 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000871 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000872 }
873
Mike Stump31feda52009-07-17 01:31:16 +0000874 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000875 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000876
Ted Kremenek9aae5132007-08-23 21:42:29 +0000877 // Set the terminator of the new block to the If statement.
878 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000879
Mike Stump773582d2009-07-23 23:25:26 +0000880 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000881 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000882
Ted Kremenek9aae5132007-08-23 21:42:29 +0000883 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000884 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
885 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000886
887 // Add the condition as the last statement in the new block. This may create
888 // new blocks as the condition may contain control-flow. Any newly created
889 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000890 Block = addStmt(I->getCond());
891
892 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
893 // and the condition variable initialization to the CFG.
894 if (VarDecl *VD = I->getConditionVariable()) {
895 if (Expr *Init = VD->getInit()) {
896 autoCreateBlock();
897 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
898 addStmt(Init);
899 }
900 }
901
902 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000903}
Mike Stump31feda52009-07-17 01:31:16 +0000904
905
Ted Kremenek9aae5132007-08-23 21:42:29 +0000906CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000907 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000908 //
Mike Stump31feda52009-07-17 01:31:16 +0000909 // NOTE: If a "return" appears in the middle of a block, this means that the
910 // code afterwards is DEAD (unreachable). We still keep a basic block
911 // for that code; a simple "mark-and-sweep" from the entry block will be
912 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000913 if (Block)
914 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000915
916 // Create the new block.
917 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000918
Ted Kremenek9aae5132007-08-23 21:42:29 +0000919 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000920 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000921
922 // Add the return statement to the block. This may create new blocks if R
923 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000924 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000925}
926
927CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
928 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000929 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000930 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000931
Ted Kremenek93668002009-07-17 22:18:43 +0000932 if (!LabelBlock) // This can happen when the body is empty, i.e.
933 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000934
Ted Kremenek93668002009-07-17 22:18:43 +0000935 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000936 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000937
938 // Labels partition blocks, so this is the end of the basic block we were
939 // processing (L is the block's label). Because this is label (and we have
940 // already processed the substatement) there is no extra control-flow to worry
941 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000942 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000943 if (!FinishBlock(LabelBlock))
944 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000945
946 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000947 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000948
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949 // This block is now the implicit successor of other blocks.
950 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000951
Ted Kremenek9aae5132007-08-23 21:42:29 +0000952 return LabelBlock;
953}
954
955CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000956 // Goto is a control-flow statement. Thus we stop processing the current
957 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000958 if (Block)
959 FinishBlock(Block);
960
Ted Kremenek9aae5132007-08-23 21:42:29 +0000961 Block = createBlock(false);
962 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000963
964 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000965 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000966
Ted Kremenek9aae5132007-08-23 21:42:29 +0000967 if (I == LabelMap.end())
968 // We will need to backpatch this block later.
969 BackpatchBlocks.push_back(Block);
970 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000971 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000972
Mike Stump31feda52009-07-17 01:31:16 +0000973 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000974}
975
976CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000977 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000978
Mike Stump014b3ea2009-07-21 01:12:51 +0000979 // "for" is a control-flow statement. Thus we stop processing the current
980 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000981 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000982 if (!FinishBlock(Block))
983 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000984 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000985 } else
986 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000987
988 // Because of short-circuit evaluation, the condition of the loop can span
989 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
990 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000991 CFGBlock* ExitConditionBlock = createBlock(false);
992 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000993
Ted Kremenek81e14852007-08-27 19:46:09 +0000994 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000995 ExitConditionBlock->setTerminator(F);
996
997 // Now add the actual condition to the condition block. Because the condition
998 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000999 if (Stmt* C = F->getCond()) {
1000 Block = ExitConditionBlock;
1001 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +00001002 assert(Block == EntryConditionBlock);
1003
1004 // If this block contains a condition variable, add both the condition
1005 // variable and initializer to the CFG.
1006 if (VarDecl *VD = F->getConditionVariable()) {
1007 if (Expr *Init = VD->getInit()) {
1008 autoCreateBlock();
1009 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1010 EntryConditionBlock = addStmt(Init);
1011 assert(Block == EntryConditionBlock);
1012 }
1013 }
1014
Ted Kremenek55957a82009-05-02 00:13:27 +00001015 if (Block) {
1016 if (!FinishBlock(EntryConditionBlock))
1017 return 0;
1018 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001019 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001020
Mike Stump31feda52009-07-17 01:31:16 +00001021 // The condition block is the implicit successor for the loop body as well as
1022 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001023 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001024
Mike Stump773582d2009-07-23 23:25:26 +00001025 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001026 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001027
Mike Stump773582d2009-07-23 23:25:26 +00001028 if (F->getCond())
1029 KnownVal = TryEvaluateBool(F->getCond());
1030
Ted Kremenek9aae5132007-08-23 21:42:29 +00001031 // Now create the loop body.
1032 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001033 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001034
Ted Kremenek9aae5132007-08-23 21:42:29 +00001035 // Save the current values for Block, Succ, and continue and break targets
1036 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001037 save_continue(ContinueTargetBlock),
1038 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001039
Ted Kremeneke9610502007-08-30 18:39:40 +00001040 // Create a new block to contain the (bottom) of the loop body.
1041 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001042
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001043 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001044 // Generate increment code in its own basic block. This is the target of
1045 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001046 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001047 } else {
1048 // No increment code. Create a special, empty, block that is used as the
1049 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001050 assert(Succ == EntryConditionBlock);
1051 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001052 }
Mike Stump31feda52009-07-17 01:31:16 +00001053
Ted Kremenek902393b2009-04-28 00:51:56 +00001054 // Finish up the increment (or empty) block if it hasn't been already.
1055 if (Block) {
1056 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +00001057 if (!FinishBlock(Block))
1058 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001059 Block = 0;
1060 }
Mike Stump31feda52009-07-17 01:31:16 +00001061
Ted Kremenek902393b2009-04-28 00:51:56 +00001062 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001063
Ted Kremenek902393b2009-04-28 00:51:56 +00001064 // The starting block for the loop increment is the block that should
1065 // represent the 'loop target' for looping back to the start of the loop.
1066 ContinueTargetBlock->setLoopTarget(F);
1067
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001068 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +00001069 BreakTargetBlock = LoopSuccessor;
1070
1071 // Now populate the body block, and in the process create new blocks as we
1072 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001073 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001074
1075 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001076 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +00001077 else if (Block && !FinishBlock(BodyBlock))
1078 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001079
Ted Kremenek30754282009-07-24 04:47:11 +00001080 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001081 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001082 }
Mike Stump31feda52009-07-17 01:31:16 +00001083
Ted Kremenek30754282009-07-24 04:47:11 +00001084 // Link up the condition block with the code that follows the loop. (the
1085 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001086 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001087
Ted Kremenek9aae5132007-08-23 21:42:29 +00001088 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001089 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001090 if (Stmt* I = F->getInit()) {
1091 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001092 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001093 } else {
1094 // There is no loop initialization. We are thus basically a while loop.
1095 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001096 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001097 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001098 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001099 }
1100}
1101
Ted Kremenek5868ec62010-04-11 17:02:10 +00001102CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1103 if (asc.alwaysAdd()) {
1104 autoCreateBlock();
1105 AppendStmt(Block, M, asc);
1106 }
1107 return Visit(M->getBase(),
1108 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1109 : AddStmtChoice::AsLValueNotAlwaysAdd);
1110}
1111
Ted Kremenek9d56e642008-11-11 17:10:00 +00001112CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1113 // Objective-C fast enumeration 'for' statements:
1114 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1115 //
1116 // for ( Type newVariable in collection_expression ) { statements }
1117 //
1118 // becomes:
1119 //
1120 // prologue:
1121 // 1. collection_expression
1122 // T. jump to loop_entry
1123 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001124 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001125 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1126 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1127 // TB:
1128 // statements
1129 // T. jump to loop_entry
1130 // FB:
1131 // what comes after
1132 //
1133 // and
1134 //
1135 // Type existingItem;
1136 // for ( existingItem in expression ) { statements }
1137 //
1138 // becomes:
1139 //
Mike Stump31feda52009-07-17 01:31:16 +00001140 // the same with newVariable replaced with existingItem; the binding works
1141 // the same except that for one ObjCForCollectionStmt::getElement() returns
1142 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001143 //
Mike Stump31feda52009-07-17 01:31:16 +00001144
Ted Kremenek9d56e642008-11-11 17:10:00 +00001145 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremenek9d56e642008-11-11 17:10:00 +00001147 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001148 if (!FinishBlock(Block))
1149 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001150 LoopSuccessor = Block;
1151 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001152 } else
1153 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001154
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001155 // Build the condition blocks.
1156 CFGBlock* ExitConditionBlock = createBlock(false);
1157 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001158
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001159 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001160 ExitConditionBlock->setTerminator(S);
1161
1162 // The last statement in the block should be the ObjCForCollectionStmt, which
1163 // performs the actual binding to 'element' and determines if there are any
1164 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001165 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001166 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001167
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001168 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001169 // generate new blocks as necesary. We DON'T add the statement by default to
1170 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001171 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001172 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001173 if (!FinishBlock(EntryConditionBlock))
1174 return 0;
1175 Block = 0;
1176 }
Mike Stump31feda52009-07-17 01:31:16 +00001177
1178 // The condition block is the implicit successor for the loop body as well as
1179 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001180 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001181
Ted Kremenek9d56e642008-11-11 17:10:00 +00001182 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001183 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001184 // Save the current values for Succ, continue and break targets.
1185 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001186 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1187
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001188 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001189 ContinueTargetBlock = EntryConditionBlock;
1190
Ted Kremenek93668002009-07-17 22:18:43 +00001191 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001192
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001193 if (!BodyBlock)
1194 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001195 else if (Block) {
1196 if (!FinishBlock(BodyBlock))
1197 return 0;
1198 }
Mike Stump31feda52009-07-17 01:31:16 +00001199
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001200 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001201 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001202 }
Mike Stump31feda52009-07-17 01:31:16 +00001203
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001204 // Link up the condition block with the code that follows the loop.
1205 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001206 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001207
Ted Kremenek9d56e642008-11-11 17:10:00 +00001208 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001209 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001210 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001211}
1212
Ted Kremenek49805452009-05-02 01:49:13 +00001213CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1214 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001215
Ted Kremenek49805452009-05-02 01:49:13 +00001216 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001217 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001218
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001219 // The sync body starts its own basic block. This makes it a little easier
1220 // for diagnostic clients.
1221 if (SyncBlock) {
1222 if (!FinishBlock(SyncBlock))
1223 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001224
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001225 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001226 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001227 }
Mike Stump31feda52009-07-17 01:31:16 +00001228
Ted Kremenek49805452009-05-02 01:49:13 +00001229 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001230 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001231}
Mike Stump31feda52009-07-17 01:31:16 +00001232
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001233CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001234 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001235 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001236}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001237
Ted Kremenek9aae5132007-08-23 21:42:29 +00001238CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001239 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001240
Mike Stump014b3ea2009-07-21 01:12:51 +00001241 // "while" is a control-flow statement. Thus we stop processing the current
1242 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001243 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001244 if (!FinishBlock(Block))
1245 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001246 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001247 } else
1248 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001249
1250 // Because of short-circuit evaluation, the condition of the loop can span
1251 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1252 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001253 CFGBlock* ExitConditionBlock = createBlock(false);
1254 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001255
Ted Kremenek81e14852007-08-27 19:46:09 +00001256 // Set the terminator for the "exit" condition block.
1257 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001258
1259 // Now add the actual condition to the condition block. Because the condition
1260 // itself may contain control-flow, new blocks may be created. Thus we update
1261 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001262 if (Stmt* C = W->getCond()) {
1263 Block = ExitConditionBlock;
1264 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001265 assert(Block == EntryConditionBlock);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001266
1267 // If this block contains a condition variable, add both the condition
1268 // variable and initializer to the CFG.
1269 if (VarDecl *VD = W->getConditionVariable()) {
1270 if (Expr *Init = VD->getInit()) {
1271 autoCreateBlock();
1272 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1273 EntryConditionBlock = addStmt(Init);
1274 assert(Block == EntryConditionBlock);
1275 }
1276 }
1277
Ted Kremenek55957a82009-05-02 00:13:27 +00001278 if (Block) {
1279 if (!FinishBlock(EntryConditionBlock))
1280 return 0;
1281 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001282 }
Mike Stump31feda52009-07-17 01:31:16 +00001283
1284 // The condition block is the implicit successor for the loop body as well as
1285 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001286 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001287
Mike Stump773582d2009-07-23 23:25:26 +00001288 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001289 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001290
Ted Kremenek9aae5132007-08-23 21:42:29 +00001291 // Process the loop body.
1292 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001293 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001294
1295 // Save the current values for Block, Succ, and continue and break targets
1296 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1297 save_continue(ContinueTargetBlock),
1298 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001299
Mike Stump31feda52009-07-17 01:31:16 +00001300 // Create an empty block to represent the transition block for looping back
1301 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001302 Block = 0;
1303 assert(Succ == EntryConditionBlock);
1304 Succ = createBlock();
1305 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001306 ContinueTargetBlock = Succ;
1307
Ted Kremenek9aae5132007-08-23 21:42:29 +00001308 // All breaks should go to the code following the loop.
1309 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001310
Ted Kremenek9aae5132007-08-23 21:42:29 +00001311 // NULL out Block to force lazy instantiation of blocks for the body.
1312 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001313
Ted Kremenek9aae5132007-08-23 21:42:29 +00001314 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001315 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001316
Ted Kremeneke9610502007-08-30 18:39:40 +00001317 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001318 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001319 else if (Block) {
1320 if (!FinishBlock(BodyBlock))
1321 return 0;
1322 }
Mike Stump31feda52009-07-17 01:31:16 +00001323
Ted Kremenek30754282009-07-24 04:47:11 +00001324 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001325 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001326 }
Mike Stump31feda52009-07-17 01:31:16 +00001327
Ted Kremenek30754282009-07-24 04:47:11 +00001328 // Link up the condition block with the code that follows the loop. (the
1329 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001330 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001331
1332 // There can be no more statements in the condition block since we loop back
1333 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001334 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001335
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001336 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001337 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001338 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001339}
Mike Stump11289f42009-09-09 15:08:12 +00001340
1341
Ted Kremenek93668002009-07-17 22:18:43 +00001342CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1343 // FIXME: For now we pretend that @catch and the code it contains does not
1344 // exit.
1345 return Block;
1346}
Mike Stump31feda52009-07-17 01:31:16 +00001347
Ted Kremenek93041ba2008-12-09 20:20:09 +00001348CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1349 // FIXME: This isn't complete. We basically treat @throw like a return
1350 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenek0868eea2009-09-24 18:45:41 +00001352 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001353 if (Block && !FinishBlock(Block))
1354 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek93041ba2008-12-09 20:20:09 +00001356 // Create the new block.
1357 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremenek93041ba2008-12-09 20:20:09 +00001359 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001360 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001361
1362 // Add the statement to the block. This may create new blocks if S contains
1363 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001364 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001365}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001366
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001367CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001368 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001369 if (Block && !FinishBlock(Block))
1370 return 0;
1371
1372 // Create the new block.
1373 Block = createBlock(false);
1374
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001375 if (TryTerminatedBlock)
1376 // The current try statement is the only successor.
1377 AddSuccessor(Block, TryTerminatedBlock);
1378 else
1379 // otherwise the Exit block is the only successor.
1380 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001381
1382 // Add the statement to the block. This may create new blocks if S contains
1383 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001384 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001385}
1386
Ted Kremenek93668002009-07-17 22:18:43 +00001387CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001388 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001389
Mike Stump8d50b6a2009-07-21 01:27:50 +00001390 // "do...while" is a control-flow statement. Thus we stop processing the
1391 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001392 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001393 if (!FinishBlock(Block))
1394 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001395 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001396 } else
1397 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001398
1399 // Because of short-circuit evaluation, the condition of the loop can span
1400 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1401 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001402 CFGBlock* ExitConditionBlock = createBlock(false);
1403 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001404
Ted Kremenek81e14852007-08-27 19:46:09 +00001405 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001406 ExitConditionBlock->setTerminator(D);
1407
1408 // Now add the actual condition to the condition block. Because the condition
1409 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001410 if (Stmt* C = D->getCond()) {
1411 Block = ExitConditionBlock;
1412 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001413 if (Block) {
1414 if (!FinishBlock(EntryConditionBlock))
1415 return 0;
1416 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001417 }
Mike Stump31feda52009-07-17 01:31:16 +00001418
Ted Kremeneka1523a32008-02-27 07:20:00 +00001419 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001420 Succ = EntryConditionBlock;
1421
Mike Stump773582d2009-07-23 23:25:26 +00001422 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001423 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001424
Ted Kremenek9aae5132007-08-23 21:42:29 +00001425 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001426 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001428 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenek9aae5132007-08-23 21:42:29 +00001430 // Save the current values for Block, Succ, and continue and break targets
1431 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001432 save_continue(ContinueTargetBlock),
1433 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001434
Ted Kremenek9aae5132007-08-23 21:42:29 +00001435 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001436 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001437
Ted Kremenek9aae5132007-08-23 21:42:29 +00001438 // All breaks should go to the code following the loop.
1439 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001440
Ted Kremenek9aae5132007-08-23 21:42:29 +00001441 // NULL out Block to force lazy instantiation of blocks for the body.
1442 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001443
Ted Kremenek9aae5132007-08-23 21:42:29 +00001444 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001445 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001446
Ted Kremeneke9610502007-08-30 18:39:40 +00001447 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001448 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001449 else if (Block) {
1450 if (!FinishBlock(BodyBlock))
1451 return 0;
1452 }
Mike Stump31feda52009-07-17 01:31:16 +00001453
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001454 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001455 // ExitConditionBlock to represent the "loop back" transition. Create an
1456 // empty block to represent the transition block for looping back to the
1457 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001458 // FIXME: Can we do this more efficiently without adding another block?
1459 Block = NULL;
1460 Succ = BodyBlock;
1461 CFGBlock *LoopBackBlock = createBlock();
1462 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001463
Ted Kremenek30754282009-07-24 04:47:11 +00001464 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001465 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001466 }
Mike Stump31feda52009-07-17 01:31:16 +00001467
Ted Kremenek30754282009-07-24 04:47:11 +00001468 // Link up the condition block with the code that follows the loop.
1469 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001470 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001471
1472 // There can be no more statements in the body block(s) since we loop back to
1473 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001474 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001475
Ted Kremenek9aae5132007-08-23 21:42:29 +00001476 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001477 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001478 return BodyBlock;
1479}
1480
1481CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1482 // "continue" is a control-flow statement. Thus we stop processing the
1483 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001484 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001485 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001486
Ted Kremenek9aae5132007-08-23 21:42:29 +00001487 // Now create a new block that ends with the continue statement.
1488 Block = createBlock(false);
1489 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001490
Ted Kremenek9aae5132007-08-23 21:42:29 +00001491 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001492 // incomplete AST. This means the CFG cannot be constructed.
1493 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001494 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001495 else
1496 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001497
Ted Kremenek9aae5132007-08-23 21:42:29 +00001498 return Block;
1499}
Mike Stump11289f42009-09-09 15:08:12 +00001500
Ted Kremenek0747de62009-07-18 00:47:21 +00001501CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001502 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001503
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001504 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001505 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001506 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001507 }
Mike Stump11289f42009-09-09 15:08:12 +00001508
Ted Kremenek93668002009-07-17 22:18:43 +00001509 // VLA types have expressions that must be evaluated.
1510 if (E->isArgumentType()) {
1511 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1512 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1513 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001514 }
Mike Stump11289f42009-09-09 15:08:12 +00001515
Mike Stump31feda52009-07-17 01:31:16 +00001516 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001517}
Mike Stump11289f42009-09-09 15:08:12 +00001518
Ted Kremenek93668002009-07-17 22:18:43 +00001519/// VisitStmtExpr - Utility method to handle (nested) statement
1520/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001521CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1522 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001523 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001524 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001525 }
Ted Kremenek93668002009-07-17 22:18:43 +00001526 return VisitCompoundStmt(SE->getSubStmt());
1527}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001528
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001529CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001530 // "switch" is a control-flow statement. Thus we stop processing the current
1531 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001532 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001533
Ted Kremenek9aae5132007-08-23 21:42:29 +00001534 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001535 if (!FinishBlock(Block))
1536 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001537 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001538 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001539
1540 // Save the current "switch" context.
1541 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001542 save_break(BreakTargetBlock),
1543 save_default(DefaultCaseBlock);
1544
Mike Stump31feda52009-07-17 01:31:16 +00001545 // Set the "default" case to be the block after the switch statement. If the
1546 // switch statement contains a "default:", this value will be overwritten with
1547 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001548 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001549
Ted Kremenek9aae5132007-08-23 21:42:29 +00001550 // Create a new block that will contain the switch statement.
1551 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001552
Ted Kremenek9aae5132007-08-23 21:42:29 +00001553 // Now process the switch body. The code after the switch is the implicit
1554 // successor.
1555 Succ = SwitchSuccessor;
1556 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001557
1558 // When visiting the body, the case statements should automatically get linked
1559 // up to the switch. We also don't keep a pointer to the body, since all
1560 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001561 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001562 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001563 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001564 if (Block) {
1565 if (!FinishBlock(BodyBlock))
1566 return 0;
1567 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001568
Mike Stump31feda52009-07-17 01:31:16 +00001569 // If we have no "default:" case, the default transition is to the code
1570 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001571 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001572
Ted Kremenek81e14852007-08-27 19:46:09 +00001573 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001574 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001575 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001576 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001577 Block = addStmt(Terminator->getCond());
1578
1579 // Finally, if the SwitchStmt contains a condition variable, add both the
1580 // SwitchStmt and the condition variable initialization to the CFG.
1581 if (VarDecl *VD = Terminator->getConditionVariable()) {
1582 if (Expr *Init = VD->getInit()) {
1583 autoCreateBlock();
1584 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1585 addStmt(Init);
1586 }
1587 }
1588
1589 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001590}
1591
Ted Kremenek93668002009-07-17 22:18:43 +00001592CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001593 // CaseStmts are essentially labels, so they are the first statement in a
1594 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001595
Ted Kremenek93668002009-07-17 22:18:43 +00001596 if (CS->getSubStmt())
1597 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001598
Ted Kremenek55e91e82007-08-30 18:48:11 +00001599 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001600 if (!CaseBlock)
1601 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001602
1603 // Cases statements partition blocks, so this is the top of the basic block we
1604 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001605 CaseBlock->setLabel(CS);
1606
Ted Kremenek55957a82009-05-02 00:13:27 +00001607 if (!FinishBlock(CaseBlock))
1608 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001609
1610 // Add this block to the list of successors for the block with the switch
1611 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001612 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001613 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001614
Ted Kremenek9aae5132007-08-23 21:42:29 +00001615 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1616 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001617
Ted Kremenek9aae5132007-08-23 21:42:29 +00001618 // This block is now the implicit successor of other blocks.
1619 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001620
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001621 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001622}
Mike Stump31feda52009-07-17 01:31:16 +00001623
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001624CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001625 if (Terminator->getSubStmt())
1626 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001627
Ted Kremenek654c78f2008-02-13 22:05:39 +00001628 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001629
1630 if (!DefaultCaseBlock)
1631 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001632
1633 // Default statements partition blocks, so this is the top of the basic block
1634 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001635 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001636
Ted Kremenek55957a82009-05-02 00:13:27 +00001637 if (!FinishBlock(DefaultCaseBlock))
1638 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001639
Mike Stump31feda52009-07-17 01:31:16 +00001640 // Unlike case statements, we don't add the default block to the successors
1641 // for the switch statement immediately. This is done when we finish
1642 // processing the switch statement. This allows for the default case
1643 // (including a fall-through to the code after the switch statement) to always
1644 // be the last successor of a switch-terminated block.
1645
Ted Kremenek654c78f2008-02-13 22:05:39 +00001646 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1647 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek654c78f2008-02-13 22:05:39 +00001649 // This block is now the implicit successor of other blocks.
1650 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001651
1652 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001653}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001654
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001655CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1656 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1657 // current block.
1658 CFGBlock* TrySuccessor = NULL;
1659
1660 if (Block) {
1661 if (!FinishBlock(Block))
1662 return 0;
1663 TrySuccessor = Block;
1664 } else TrySuccessor = Succ;
1665
Mike Stump0bdba6c2010-01-20 01:15:34 +00001666 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001667
1668 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001669 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001670 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001671 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001672
Mike Stump0bdba6c2010-01-20 01:15:34 +00001673 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001674 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1675 // The code after the try is the implicit successor.
1676 Succ = TrySuccessor;
1677 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001678 if (CS->getExceptionDecl() == 0) {
1679 HasCatchAll = true;
1680 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001681 Block = NULL;
1682 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1683 if (CatchBlock == 0)
1684 return 0;
1685 // Add this block to the list of successors for the block with the try
1686 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001687 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001688 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001689 if (!HasCatchAll) {
1690 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001691 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001692 else
Mike Stump845384a2010-01-20 01:30:58 +00001693 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001694 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001695
1696 // The code after the try is the implicit successor.
1697 Succ = TrySuccessor;
1698
Mike Stump845384a2010-01-20 01:30:58 +00001699 // Save the current "try" context.
1700 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1701 TryTerminatedBlock = NewTryTerminatedBlock;
1702
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001703 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001704 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001705 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001706 return Block;
1707}
1708
1709CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1710 // CXXCatchStmt are treated like labels, so they are the first statement in a
1711 // block.
1712
1713 if (CS->getHandlerBlock())
1714 addStmt(CS->getHandlerBlock());
1715
1716 CFGBlock* CatchBlock = Block;
1717 if (!CatchBlock)
1718 CatchBlock = createBlock();
1719
1720 CatchBlock->setLabel(CS);
1721
1722 if (!FinishBlock(CatchBlock))
1723 return 0;
1724
1725 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1726 Block = NULL;
1727
1728 return CatchBlock;
1729}
1730
Zhongxing Xu7e612172010-04-13 09:38:01 +00001731CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
1732 AddStmtChoice asc) {
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001733 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1734 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001735 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001736 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001737 return VisitChildren(C);
1738}
1739
Ted Kremenekeda180e22007-08-28 19:26:49 +00001740CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001741 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001742 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001743
Ted Kremenekeda180e22007-08-28 19:26:49 +00001744 if (!IBlock) {
1745 IBlock = createBlock(false);
1746 cfg->setIndirectGotoBlock(IBlock);
1747 }
Mike Stump31feda52009-07-17 01:31:16 +00001748
Ted Kremenekeda180e22007-08-28 19:26:49 +00001749 // IndirectGoto is a control-flow statement. Thus we stop processing the
1750 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001751 if (Block && !FinishBlock(Block))
1752 return 0;
1753
Ted Kremenekeda180e22007-08-28 19:26:49 +00001754 Block = createBlock(false);
1755 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001756 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001757 return addStmt(I->getTarget());
1758}
1759
Ted Kremenek04cca642007-08-23 21:26:19 +00001760} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001761
Mike Stump31feda52009-07-17 01:31:16 +00001762/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1763/// no successors or predecessors. If this is the first block created in the
1764/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001765CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001766 bool first_block = begin() == end();
1767
1768 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001769 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1770 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1771 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001772
1773 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001774 if (first_block)
1775 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001776
1777 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001778 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001779}
1780
Ted Kremenek889073f2007-08-23 16:51:22 +00001781/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1782/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001783CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump04c68512010-01-21 15:20:48 +00001784 bool AddEHEdges, bool AddScopes) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001785 CFGBuilder Builder;
Mike Stump04c68512010-01-21 15:20:48 +00001786 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek889073f2007-08-23 16:51:22 +00001787}
1788
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001789//===----------------------------------------------------------------------===//
1790// CFG: Queries for BlkExprs.
1791//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001792
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001793namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001794 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001795}
1796
Ted Kremenekbff98442009-12-23 23:37:10 +00001797static void FindSubExprAssignments(Stmt *S,
1798 llvm::SmallPtrSet<Expr*,50>& Set) {
1799 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001800 return;
Mike Stump31feda52009-07-17 01:31:16 +00001801
Ted Kremenekbff98442009-12-23 23:37:10 +00001802 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1803 Stmt *child = *I;
1804 if (!child)
1805 continue;
1806
1807 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001808 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001809
Ted Kremenekbff98442009-12-23 23:37:10 +00001810 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001811 }
1812}
1813
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001814static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1815 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001816
1817 // Look for assignments that are used as subexpressions. These are the only
1818 // assignments that we want to *possibly* register as a block-level
1819 // expression. Basically, if an assignment occurs both in a subexpression and
1820 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001821 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001822
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001823 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001824 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001825 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001826
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001827 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001828
1829 // Iterate over the statements again on identify the Expr* and Stmt* at the
1830 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001831
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001832 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001833 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001835 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001836 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001837 // expression are really "statements" whose value is never used by
1838 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001839 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001840 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001841 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1842 // Special handling for statement expressions. The last statement in
1843 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001844 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001845 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001846 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001847 (*M)[C->body_back()] = x;
1848 }
1849 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001850
Ted Kremenek95a123c2008-01-26 00:03:27 +00001851 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001852 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001853 }
Mike Stump31feda52009-07-17 01:31:16 +00001854
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001855 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001856
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001857 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001858
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001859 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001860 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001861 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001862 }
1863 }
Mike Stump31feda52009-07-17 01:31:16 +00001864
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001865 return M;
1866}
1867
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001868CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1869 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001870 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001871
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001872 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001873 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001874 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001875}
1876
1877unsigned CFG::getNumBlkExprs() {
1878 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1879 return M->size();
1880 else {
1881 // We assume callers interested in the number of BlkExprs will want
1882 // the map constructed if it doesn't already exist.
1883 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1884 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1885 }
1886}
1887
Ted Kremenek6065ef62008-04-28 18:00:46 +00001888//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001889// Cleanup: CFG dstor.
1890//===----------------------------------------------------------------------===//
1891
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001892CFG::~CFG() {
1893 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1894}
Mike Stump31feda52009-07-17 01:31:16 +00001895
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001896//===----------------------------------------------------------------------===//
1897// CFG pretty printing
1898//===----------------------------------------------------------------------===//
1899
Ted Kremenek7e776b12007-08-22 18:22:34 +00001900namespace {
1901
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001902class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001903 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1904 StmtMapTy StmtMap;
1905 signed CurrentBlock;
1906 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001907 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001908public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001909
Chris Lattnerc61089a2009-06-30 01:26:17 +00001910 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1911 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001912 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1913 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001914 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001915 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001916 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001917 }
1918 }
Mike Stump31feda52009-07-17 01:31:16 +00001919
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001920 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001921
Chris Lattnerc61089a2009-06-30 01:26:17 +00001922 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001923 void setBlockID(signed i) { CurrentBlock = i; }
1924 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001925
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001926 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001927
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001928 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001929
1930 if (I == StmtMap.end())
1931 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001932
1933 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00001934 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001935 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001936 }
Mike Stump31feda52009-07-17 01:31:16 +00001937
Ted Kremenek60983dc2010-01-19 20:52:05 +00001938 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001939 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001940 }
1941};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001942} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001943
Chris Lattnerc61089a2009-06-30 01:26:17 +00001944
1945namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001946class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001947 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001948
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001949 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001950 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001951 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001952public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001953 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001954 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001955 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001956
Ted Kremenek9aae5132007-08-23 21:42:29 +00001957 void VisitIfStmt(IfStmt* I) {
1958 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001959 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001960 }
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenek9aae5132007-08-23 21:42:29 +00001962 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001963 void VisitStmt(Stmt* Terminator) {
1964 Terminator->printPretty(OS, Helper, Policy);
1965 }
1966
Ted Kremenek9aae5132007-08-23 21:42:29 +00001967 void VisitForStmt(ForStmt* F) {
1968 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001969 if (F->getInit())
1970 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001971 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001972 if (Stmt* C = F->getCond())
1973 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001974 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001975 if (F->getInc())
1976 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001977 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001978 }
Mike Stump31feda52009-07-17 01:31:16 +00001979
Ted Kremenek9aae5132007-08-23 21:42:29 +00001980 void VisitWhileStmt(WhileStmt* W) {
1981 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001982 if (Stmt* C = W->getCond())
1983 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001984 }
Mike Stump31feda52009-07-17 01:31:16 +00001985
Ted Kremenek9aae5132007-08-23 21:42:29 +00001986 void VisitDoStmt(DoStmt* D) {
1987 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001988 if (Stmt* C = D->getCond())
1989 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001990 }
Mike Stump31feda52009-07-17 01:31:16 +00001991
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001992 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001993 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001994 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001995 }
Mike Stump31feda52009-07-17 01:31:16 +00001996
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001997 void VisitCXXTryStmt(CXXTryStmt* CS) {
1998 OS << "try ...";
1999 }
2000
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002001 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002002 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002003 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002004 }
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenek391f94a2007-08-31 22:29:13 +00002006 void VisitChooseExpr(ChooseExpr* C) {
2007 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002008 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002009 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002010 }
Mike Stump31feda52009-07-17 01:31:16 +00002011
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002012 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2013 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002014 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002015 }
Mike Stump31feda52009-07-17 01:31:16 +00002016
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002017 void VisitBinaryOperator(BinaryOperator* B) {
2018 if (!B->isLogicalOp()) {
2019 VisitExpr(B);
2020 return;
2021 }
Mike Stump31feda52009-07-17 01:31:16 +00002022
Douglas Gregor7de59662009-05-29 20:38:28 +00002023 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002024
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002025 switch (B->getOpcode()) {
2026 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002027 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002028 return;
2029 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002030 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002031 return;
2032 default:
2033 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002034 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002035 }
Mike Stump31feda52009-07-17 01:31:16 +00002036
Ted Kremenek12687ff2007-08-27 21:54:41 +00002037 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002038 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002039 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002040};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002041} // end anonymous namespace
2042
Mike Stump31feda52009-07-17 01:31:16 +00002043
Chris Lattnerc61089a2009-06-30 01:26:17 +00002044static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002045 const CFGElement &E) {
2046 Stmt *Terminator = E;
2047
2048 if (E.asStartScope()) {
2049 OS << "start scope\n";
2050 return;
2051 }
2052 if (E.asEndScope()) {
2053 OS << "end scope\n";
2054 return;
2055 }
2056
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002057 if (Helper) {
2058 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002059 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002060 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00002061
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002062 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00002063 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00002064 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00002065 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002066 return;
2067 }
2068 }
Mike Stump31feda52009-07-17 01:31:16 +00002069
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002070 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002071 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002072 if (B->getOpcode() == BinaryOperator::Comma) {
2073 OS << "... , ";
2074 Helper->handledStmt(B->getRHS(),OS);
2075 OS << '\n';
2076 return;
Mike Stump31feda52009-07-17 01:31:16 +00002077 }
2078 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002079 }
Mike Stump31feda52009-07-17 01:31:16 +00002080
Chris Lattnerc61089a2009-06-30 01:26:17 +00002081 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002082
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002083 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002084 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002085}
Mike Stump31feda52009-07-17 01:31:16 +00002086
Chris Lattnerc61089a2009-06-30 01:26:17 +00002087static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2088 const CFGBlock& B,
2089 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002090
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002091 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002092
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002093 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002094 OS << "\n [ B" << B.getBlockID();
2095
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002096 if (&B == &cfg->getEntry())
2097 OS << " (ENTRY) ]\n";
2098 else if (&B == &cfg->getExit())
2099 OS << " (EXIT) ]\n";
2100 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002101 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002102 else
2103 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002104
Ted Kremenek71eca012007-08-29 23:20:49 +00002105 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002106 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002107
2108 if (print_edges)
2109 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002110
Mike Stump92244b02010-01-19 22:00:14 +00002111 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002112 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002113 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002114 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002115 C->getLHS()->printPretty(OS, Helper,
2116 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002117 if (C->getRHS()) {
2118 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002119 C->getRHS()->printPretty(OS, Helper,
2120 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002121 }
Mike Stump92244b02010-01-19 22:00:14 +00002122 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002123 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002124 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002125 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002126 if (CS->getExceptionDecl())
2127 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2128 0);
2129 else
2130 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002131 OS << ")";
2132
2133 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002134 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002135
Ted Kremenek71eca012007-08-29 23:20:49 +00002136 OS << ":\n";
2137 }
Mike Stump31feda52009-07-17 01:31:16 +00002138
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002139 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002140 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002141
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002142 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2143 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002144
Ted Kremenek71eca012007-08-29 23:20:49 +00002145 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002146 if (print_edges)
2147 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002148
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002149 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002150
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002151 if (Helper)
2152 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002153
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002154 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002155 }
Mike Stump31feda52009-07-17 01:31:16 +00002156
Ted Kremenek71eca012007-08-29 23:20:49 +00002157 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002158 if (B.getTerminator()) {
2159 if (print_edges)
2160 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002161
Ted Kremenek71eca012007-08-29 23:20:49 +00002162 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002163
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002164 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002165
Chris Lattnerc61089a2009-06-30 01:26:17 +00002166 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2167 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002168 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002169 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002170 }
Mike Stump31feda52009-07-17 01:31:16 +00002171
Ted Kremenek71eca012007-08-29 23:20:49 +00002172 if (print_edges) {
2173 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002174 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002175 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002176
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002177 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2178 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002179
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002180 if (i == 8 || (i-8) == 0)
2181 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002182
Ted Kremenek71eca012007-08-29 23:20:49 +00002183 OS << " B" << (*I)->getBlockID();
2184 }
Mike Stump31feda52009-07-17 01:31:16 +00002185
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002186 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002187
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002188 // Print the successors of this block.
2189 OS << " Successors (" << B.succ_size() << "):";
2190 i = 0;
2191
2192 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2193 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002194
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002195 if (i == 8 || (i-8) % 10 == 0)
2196 OS << "\n ";
2197
Mike Stump0d76d072009-07-20 23:24:15 +00002198 if (*I)
2199 OS << " B" << (*I)->getBlockID();
2200 else
2201 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002202 }
Mike Stump31feda52009-07-17 01:31:16 +00002203
Ted Kremenek71eca012007-08-29 23:20:49 +00002204 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002205 }
Mike Stump31feda52009-07-17 01:31:16 +00002206}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002207
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002208
2209/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002210void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002211
2212/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002213void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2214 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002215
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002216 // Print the entry block.
2217 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002218
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002219 // Iterate through the CFGBlocks and print them one by one.
2220 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2221 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002222 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002223 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002224
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002225 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002226 }
Mike Stump31feda52009-07-17 01:31:16 +00002227
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002228 // Print the exit block.
2229 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002230 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002231}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002232
2233/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002234void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2235 print(llvm::errs(), cfg, LO);
2236}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002237
2238/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2239/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002240void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2241 const LangOptions &LO) const {
2242 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002243 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002244}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002245
Ted Kremenek15647632008-01-30 23:02:42 +00002246/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002247void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002248 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002249 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002250 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2251}
2252
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002253Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002254
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002255 if (!Terminator)
2256 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002257
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002258 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002259
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002260 switch (Terminator->getStmtClass()) {
2261 default:
2262 break;
Mike Stump31feda52009-07-17 01:31:16 +00002263
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002264 case Stmt::ForStmtClass:
2265 E = cast<ForStmt>(Terminator)->getCond();
2266 break;
Mike Stump31feda52009-07-17 01:31:16 +00002267
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002268 case Stmt::WhileStmtClass:
2269 E = cast<WhileStmt>(Terminator)->getCond();
2270 break;
Mike Stump31feda52009-07-17 01:31:16 +00002271
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002272 case Stmt::DoStmtClass:
2273 E = cast<DoStmt>(Terminator)->getCond();
2274 break;
Mike Stump31feda52009-07-17 01:31:16 +00002275
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002276 case Stmt::IfStmtClass:
2277 E = cast<IfStmt>(Terminator)->getCond();
2278 break;
Mike Stump31feda52009-07-17 01:31:16 +00002279
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002280 case Stmt::ChooseExprClass:
2281 E = cast<ChooseExpr>(Terminator)->getCond();
2282 break;
Mike Stump31feda52009-07-17 01:31:16 +00002283
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002284 case Stmt::IndirectGotoStmtClass:
2285 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2286 break;
Mike Stump31feda52009-07-17 01:31:16 +00002287
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002288 case Stmt::SwitchStmtClass:
2289 E = cast<SwitchStmt>(Terminator)->getCond();
2290 break;
Mike Stump31feda52009-07-17 01:31:16 +00002291
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002292 case Stmt::ConditionalOperatorClass:
2293 E = cast<ConditionalOperator>(Terminator)->getCond();
2294 break;
Mike Stump31feda52009-07-17 01:31:16 +00002295
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002296 case Stmt::BinaryOperatorClass: // '&&' and '||'
2297 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002298 break;
Mike Stump31feda52009-07-17 01:31:16 +00002299
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002300 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002301 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002302 }
Mike Stump31feda52009-07-17 01:31:16 +00002303
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002304 return E ? E->IgnoreParens() : NULL;
2305}
2306
Ted Kremenek92137a32008-05-16 16:06:00 +00002307bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002308
Ted Kremenek92137a32008-05-16 16:06:00 +00002309 if (!Terminator)
2310 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002311
Ted Kremenek92137a32008-05-16 16:06:00 +00002312 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002313
Ted Kremenek92137a32008-05-16 16:06:00 +00002314 switch (Terminator->getStmtClass()) {
2315 default:
2316 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002317
2318 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002319 case Stmt::WhileStmtClass:
2320 case Stmt::DoStmtClass:
2321 case Stmt::IfStmtClass:
2322 case Stmt::ChooseExprClass:
2323 case Stmt::ConditionalOperatorClass:
2324 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002325 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002326 }
Mike Stump31feda52009-07-17 01:31:16 +00002327
Ted Kremenek92137a32008-05-16 16:06:00 +00002328 return E ? E->IgnoreParens() : NULL;
2329}
2330
Ted Kremenek15647632008-01-30 23:02:42 +00002331
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002332//===----------------------------------------------------------------------===//
2333// CFG Graphviz Visualization
2334//===----------------------------------------------------------------------===//
2335
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002336
2337#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002338static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002339#endif
2340
Chris Lattnerc61089a2009-06-30 01:26:17 +00002341void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002342#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002343 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002344 GraphHelper = &H;
2345 llvm::ViewGraph(this,"CFG");
2346 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002347#endif
2348}
2349
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002350namespace llvm {
2351template<>
2352struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002353
2354 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2355
2356 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002357
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002358#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002359 std::string OutSStr;
2360 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002361 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002362 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002363
2364 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2365
2366 // Process string output to make it nicer...
2367 for (unsigned i = 0; i != OutStr.length(); ++i)
2368 if (OutStr[i] == '\n') { // Left justify
2369 OutStr[i] = '\\';
2370 OutStr.insert(OutStr.begin()+i+1, 'l');
2371 }
Mike Stump31feda52009-07-17 01:31:16 +00002372
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002373 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002374#else
2375 return "";
2376#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002377 }
2378};
2379} // end namespace llvm