blob: c97692f57082e0acdaa4149ba52d868c9c4af73d [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"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000022#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor6e6ad602009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Mike Stump31feda52009-07-17 01:31:16 +000037
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000038/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000039/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;
45/// CFG* cfg = builder.BuildAST(stmt1);
46///
Mike Stump31feda52009-07-17 01:31:16 +000047/// CFG construction is done via a recursive walk of an AST. We actually parse
48/// the AST in reverse order so that the successor of a basic block is
49/// constructed prior to its predecessor. This allows us to nicely capture
50/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000051///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000052class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000053 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000054 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000055
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000062
Ted Kremenekeda180e22007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000071
Ted Kremenekeda180e22007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000075
76public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000077 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
78 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000079 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek289ae4f2009-10-12 20:55:07 +000080 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000081
Ted Kremenek9aae5132007-08-23 21:42:29 +000082 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000083 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000084
Ted Kremenek93668002009-07-17 22:18:43 +000085private:
86 // Visitors to walk an AST and construct the CFG.
87 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
88 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000089 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
90 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000091 CFGBlock *VisitBreakStmt(BreakStmt *B);
92 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
93 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000094 CFGBlock *VisitChooseExpr(ChooseExpr *C);
95 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
96 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
97 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +000098 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +000099 CFGBlock *VisitDeclStmt(DeclStmt *DS);
100 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000101 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
102 CFGBlock *VisitDoStmt(DoStmt *D);
103 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000104 CFGBlock *VisitGotoStmt(GotoStmt* G);
105 CFGBlock *VisitIfStmt(IfStmt *I);
106 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
107 CFGBlock *VisitLabelStmt(LabelStmt *L);
108 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
109 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
110 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
111 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
112 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
113 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000114 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
115 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000116 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
117 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000118
Ted Kremenek93668002009-07-17 22:18:43 +0000119 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
120 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
121 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000122
Ted Kremenek6065ef62008-04-28 18:00:46 +0000123 // NYS == Not Yet Supported
124 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000125 badCFG = true;
126 return Block;
127 }
Mike Stump31feda52009-07-17 01:31:16 +0000128
Ted Kremenek93668002009-07-17 22:18:43 +0000129 void autoCreateBlock() { if (!Block) Block = createBlock(); }
130 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000131 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000132 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000133
134 void AppendStmt(CFGBlock *B, Stmt *S) {
135 B->appendStmt(S, cfg->getBumpVectorContext());
136 }
137
138 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
139 B->addSuccessor(S, cfg->getBumpVectorContext());
140 }
Mike Stump11289f42009-09-09 15:08:12 +0000141
Ted Kremenek963cc312009-07-24 06:55:42 +0000142 /// TryResult - a class representing a variant over the values
143 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
144 /// and is used by the CFGBuilder to decide if a branch condition
145 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000146 class TryResult {
147 int X;
148 public:
149 TryResult(bool b) : X(b ? 1 : 0) {}
150 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000151
Ted Kremenek30754282009-07-24 04:47:11 +0000152 bool isTrue() const { return X == 1; }
153 bool isFalse() const { return X == 0; }
154 bool isKnown() const { return X >= 0; }
155 void negate() {
156 assert(isKnown());
157 X ^= 0x1;
158 }
159 };
Mike Stump11289f42009-09-09 15:08:12 +0000160
Mike Stump773582d2009-07-23 23:25:26 +0000161 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
162 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000163 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000164 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000165 if (!S->isTypeDependent() && !S->isValueDependent() &&
166 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000167 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000168
169 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000170 }
171
Ted Kremenekb64d1832008-03-13 03:04:22 +0000172 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000173};
Mike Stump31feda52009-07-17 01:31:16 +0000174
Douglas Gregor4619e432008-12-05 23:32:09 +0000175// FIXME: Add support for dependent-sized array types in C++?
176// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000177static VariableArrayType* FindVA(Type* t) {
178 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
179 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
180 if (vat->getSizeExpr())
181 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000182
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000183 t = vt->getElementType().getTypePtr();
184 }
Mike Stump31feda52009-07-17 01:31:16 +0000185
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000186 return 0;
187}
Mike Stump31feda52009-07-17 01:31:16 +0000188
189/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
190/// arbitrary statement. Examples include a single expression or a function
191/// body (compound statement). The ownership of the returned CFG is
192/// transferred to the caller. If CFG construction fails, this method returns
193/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000194CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
195 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000196 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000197 if (!Statement)
198 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000199
Ted Kremenekb64d1832008-03-13 03:04:22 +0000200 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000201
202 // Create an empty block that will serve as the exit block for the CFG. Since
203 // this is the first block added to the CFG, it will be implicitly registered
204 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000205 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000206 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000207 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000208
Ted Kremenek9aae5132007-08-23 21:42:29 +0000209 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000210 CFGBlock* B = addStmt(Statement);
Ted Kremenek8aed4902009-10-20 23:46:25 +0000211 if (!B)
212 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000213
Ted Kremenek40d87632008-02-27 17:33:02 +0000214 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000215 // Finalize the last constructed block. This usually involves reversing the
216 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000217 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000218
219 // Backpatch the gotos whose label -> block mappings we didn't know when we
220 // encountered them.
221 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000222 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000223
Ted Kremenek9aae5132007-08-23 21:42:29 +0000224 CFGBlock* B = *I;
225 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
226 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
227
228 // If there is no target for the goto, then we are looking at an
229 // incomplete AST. Handle this by not registering a successor.
230 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000231
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000232 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000233 }
Mike Stump31feda52009-07-17 01:31:16 +0000234
Ted Kremenekeda180e22007-08-28 19:26:49 +0000235 // Add successors to the Indirect Goto Dispatch block (if we have one).
236 if (CFGBlock* B = cfg->getIndirectGotoBlock())
237 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
238 E = AddressTakenLabels.end(); I != E; ++I ) {
239
240 // Lookup the target block.
241 LabelMapTy::iterator LI = LabelMap.find(*I);
242
243 // If there is no target block that contains label, then we are looking
244 // at an incomplete AST. Handle this by not registering a successor.
245 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000246
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000247 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000248 }
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000250 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000251 }
Mike Stump31feda52009-07-17 01:31:16 +0000252
253 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000254 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000255
Ted Kremenekab929bb2009-10-20 23:59:28 +0000256 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000257}
Mike Stump31feda52009-07-17 01:31:16 +0000258
Ted Kremenek9aae5132007-08-23 21:42:29 +0000259/// createBlock - Used to lazily create blocks that are connected
260/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000261CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000262 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000263 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000264 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000265 return B;
266}
Mike Stump31feda52009-07-17 01:31:16 +0000267
Ted Kremenek0868eea2009-09-24 18:45:41 +0000268/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000269bool CFGBuilder::FinishBlock(CFGBlock* B) {
270 if (badCFG)
271 return false;
272
Ted Kremenek93668002009-07-17 22:18:43 +0000273 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000274 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000275}
276
Ted Kremenek93668002009-07-17 22:18:43 +0000277/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000278/// blocks for ternary operators, &&, and ||. We also process "," and
279/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000280CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
281tryAgain:
282 switch (S->getStmtClass()) {
283 default:
284 return VisitStmt(S, alwaysAdd);
285
286 case Stmt::AddrLabelExprClass:
287 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000288
Ted Kremenek93668002009-07-17 22:18:43 +0000289 case Stmt::BinaryOperatorClass:
290 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000291
Ted Kremenek93668002009-07-17 22:18:43 +0000292 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000293 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000294
295 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000296 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000297
Ted Kremenek93668002009-07-17 22:18:43 +0000298 case Stmt::BreakStmtClass:
299 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremenek93668002009-07-17 22:18:43 +0000301 case Stmt::CallExprClass:
302 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000303
Ted Kremenek93668002009-07-17 22:18:43 +0000304 case Stmt::CaseStmtClass:
305 return VisitCaseStmt(cast<CaseStmt>(S));
306
307 case Stmt::ChooseExprClass:
308 return VisitChooseExpr(cast<ChooseExpr>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenek93668002009-07-17 22:18:43 +0000310 case Stmt::CompoundStmtClass:
311 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000312
Ted Kremenek93668002009-07-17 22:18:43 +0000313 case Stmt::ConditionalOperatorClass:
314 return VisitConditionalOperator(cast<ConditionalOperator>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenek93668002009-07-17 22:18:43 +0000316 case Stmt::ContinueStmtClass:
317 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek93668002009-07-17 22:18:43 +0000319 case Stmt::DeclStmtClass:
320 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenek93668002009-07-17 22:18:43 +0000322 case Stmt::DefaultStmtClass:
323 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenek93668002009-07-17 22:18:43 +0000325 case Stmt::DoStmtClass:
326 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremenek93668002009-07-17 22:18:43 +0000328 case Stmt::ForStmtClass:
329 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000330
Ted Kremenek93668002009-07-17 22:18:43 +0000331 case Stmt::GotoStmtClass:
332 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenek93668002009-07-17 22:18:43 +0000334 case Stmt::IfStmtClass:
335 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek93668002009-07-17 22:18:43 +0000337 case Stmt::IndirectGotoStmtClass:
338 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000339
Ted Kremenek93668002009-07-17 22:18:43 +0000340 case Stmt::LabelStmtClass:
341 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000342
Ted Kremenek93668002009-07-17 22:18:43 +0000343 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000344 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
345
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000346 case Stmt::CXXThrowExprClass:
347 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
348
Ted Kremenek93668002009-07-17 22:18:43 +0000349 case Stmt::ObjCAtSynchronizedStmtClass:
350 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek93668002009-07-17 22:18:43 +0000352 case Stmt::ObjCAtThrowStmtClass:
353 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenek93668002009-07-17 22:18:43 +0000355 case Stmt::ObjCAtTryStmtClass:
356 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenek93668002009-07-17 22:18:43 +0000358 case Stmt::ObjCForCollectionStmtClass:
359 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000360
Ted Kremenek93668002009-07-17 22:18:43 +0000361 case Stmt::ParenExprClass:
362 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000363 goto tryAgain;
364
Ted Kremenek93668002009-07-17 22:18:43 +0000365 case Stmt::NullStmtClass:
366 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek93668002009-07-17 22:18:43 +0000368 case Stmt::ReturnStmtClass:
369 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek93668002009-07-17 22:18:43 +0000371 case Stmt::SizeOfAlignOfExprClass:
Mike Stump11289f42009-09-09 15:08:12 +0000372 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
373
Ted Kremenek93668002009-07-17 22:18:43 +0000374 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000375 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremenek93668002009-07-17 22:18:43 +0000377 case Stmt::SwitchStmtClass:
378 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000379
Ted Kremenek93668002009-07-17 22:18:43 +0000380 case Stmt::WhileStmtClass:
381 return VisitWhileStmt(cast<WhileStmt>(S));
382 }
383}
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ted Kremenek93668002009-07-17 22:18:43 +0000385CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
386 if (alwaysAdd) {
387 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000388 AppendStmt(Block, S);
Mike Stump31feda52009-07-17 01:31:16 +0000389 }
Mike Stump11289f42009-09-09 15:08:12 +0000390
Ted Kremenek93668002009-07-17 22:18:43 +0000391 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000392}
Mike Stump31feda52009-07-17 01:31:16 +0000393
Ted Kremenek93668002009-07-17 22:18:43 +0000394/// VisitChildren - Visit the children of a Stmt.
395CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
396 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000397 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000398 E = Terminator->child_end(); I != E; ++I) {
399 if (*I) B = Visit(*I);
400 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000401 return B;
402}
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek93668002009-07-17 22:18:43 +0000404CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
405 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000406
Ted Kremenek93668002009-07-17 22:18:43 +0000407 if (alwaysAdd) {
408 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000409 AppendStmt(Block, A);
Ted Kremenek93668002009-07-17 22:18:43 +0000410 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000411
Ted Kremenek9aae5132007-08-23 21:42:29 +0000412 return Block;
413}
Mike Stump11289f42009-09-09 15:08:12 +0000414
Ted Kremenek93668002009-07-17 22:18:43 +0000415CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
416 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000417 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000418 AppendStmt(ConfluenceBlock, B);
Mike Stump11289f42009-09-09 15:08:12 +0000419
Ted Kremenek93668002009-07-17 22:18:43 +0000420 if (!FinishBlock(ConfluenceBlock))
421 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000422
Ted Kremenek93668002009-07-17 22:18:43 +0000423 // create the block evaluating the LHS
424 CFGBlock* LHSBlock = createBlock(false);
425 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000426
Ted Kremenek93668002009-07-17 22:18:43 +0000427 // create the block evaluating the RHS
428 Succ = ConfluenceBlock;
429 Block = NULL;
430 CFGBlock* RHSBlock = addStmt(B->getRHS());
431 if (!FinishBlock(RHSBlock))
432 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000433
Mike Stump773582d2009-07-23 23:25:26 +0000434 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000435 TryResult KnownVal = TryEvaluateBool(B->getLHS());
436 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
437 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000438
Ted Kremenek93668002009-07-17 22:18:43 +0000439 // Now link the LHSBlock with RHSBlock.
440 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000441 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
442 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000443 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000444 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000445 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
446 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000447 }
Mike Stump11289f42009-09-09 15:08:12 +0000448
Ted Kremenek93668002009-07-17 22:18:43 +0000449 // Generate the blocks for evaluating the LHS.
450 Block = LHSBlock;
451 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000452 }
Ted Kremenek93668002009-07-17 22:18:43 +0000453 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000454 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000455 AppendStmt(Block, B);
Ted Kremenek93668002009-07-17 22:18:43 +0000456 addStmt(B->getRHS());
457 return addStmt(B->getLHS());
458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Ted Kremenek93668002009-07-17 22:18:43 +0000460 return VisitStmt(B, alwaysAdd);
461}
462
Ted Kremenek470bfa42009-11-25 01:34:30 +0000463CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, bool alwaysAdd) {
464 if (alwaysAdd) {
465 autoCreateBlock();
466 AppendStmt(Block, E);
467 }
468 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000469}
470
Ted Kremenek0747de62009-07-18 00:47:21 +0000471CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
472 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000473 // FIXME
474 return NYS();
475}
Mike Stump11289f42009-09-09 15:08:12 +0000476
Ted Kremenek93668002009-07-17 22:18:43 +0000477CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
478 // "break" is a control-flow statement. Thus we stop processing the current
479 // block.
480 if (Block && !FinishBlock(Block))
481 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000482
Ted Kremenek93668002009-07-17 22:18:43 +0000483 // Now create a new block that ends with the break statement.
484 Block = createBlock(false);
485 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek93668002009-07-17 22:18:43 +0000487 // If there is no target for the break, then we are looking at an incomplete
488 // AST. This means that the CFG cannot be constructed.
489 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000490 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000491 else
492 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000493
494
Ted Kremenek9aae5132007-08-23 21:42:29 +0000495 return Block;
496}
Mike Stump11289f42009-09-09 15:08:12 +0000497
Ted Kremenek93668002009-07-17 22:18:43 +0000498CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
499 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000500 bool NoReturn = false;
501 if (C->getCallee()->getType().getNoReturnAttr()) {
502 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000503 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000504
505 if (FunctionDecl *FD = C->getDirectCallee())
506 if (FD->hasAttr<NoReturnAttr>())
507 NoReturn = true;
508
509 if (!NoReturn)
510 return VisitStmt(C, alwaysAdd);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Mike Stump8c5d7992009-07-25 21:26:53 +0000512 if (Block && !FinishBlock(Block))
513 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Mike Stump8c5d7992009-07-25 21:26:53 +0000515 // Create new block with no successor for the remaining pieces.
516 Block = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000517 AppendStmt(Block, C);
Mike Stump8c5d7992009-07-25 21:26:53 +0000518
519 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000520 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000521
Mike Stump8c5d7992009-07-25 21:26:53 +0000522 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000523}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000524
Ted Kremenek21822592009-07-17 18:20:32 +0000525CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
526 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000527 AppendStmt(ConfluenceBlock, C);
Ted Kremenek21822592009-07-17 18:20:32 +0000528 if (!FinishBlock(ConfluenceBlock))
529 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Ted Kremenek21822592009-07-17 18:20:32 +0000531 Succ = ConfluenceBlock;
532 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000533 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000534 if (!FinishBlock(LHSBlock))
535 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Ted Kremenek21822592009-07-17 18:20:32 +0000537 Succ = ConfluenceBlock;
538 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000539 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000540 if (!FinishBlock(RHSBlock))
541 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000542
Ted Kremenek21822592009-07-17 18:20:32 +0000543 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000544 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000545 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000546 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
547 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000548 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000549 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000550}
Mike Stump11289f42009-09-09 15:08:12 +0000551
552
553CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
554 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000555
556 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
557 I != E; ++I ) {
558 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Ted Kremenekce499c22009-08-27 23:16:26 +0000560 if (badCFG)
561 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000562 }
Ted Kremenek93668002009-07-17 22:18:43 +0000563 return LastBlock;
564}
Mike Stump11289f42009-09-09 15:08:12 +0000565
Ted Kremenek51d40b02009-07-17 18:15:54 +0000566CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
567 // Create the confluence block that will "merge" the results of the ternary
568 // expression.
569 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000570 AppendStmt(ConfluenceBlock, C);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000571 if (!FinishBlock(ConfluenceBlock))
572 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek51d40b02009-07-17 18:15:54 +0000574 // Create a block for the LHS expression if there is an LHS expression. A
575 // GCC extension allows LHS to be NULL, causing the condition to be the
576 // value that is returned instead.
577 // e.g: x ?: y is shorthand for: x ? x : y;
578 Succ = ConfluenceBlock;
579 Block = NULL;
580 CFGBlock* LHSBlock = NULL;
581 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000582 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000583 if (!FinishBlock(LHSBlock))
584 return 0;
585 Block = NULL;
586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Ted Kremenek51d40b02009-07-17 18:15:54 +0000588 // Create the block for the RHS expression.
589 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000590 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000591 if (!FinishBlock(RHSBlock))
592 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000593
Ted Kremenek51d40b02009-07-17 18:15:54 +0000594 // Create the block that will contain the condition.
595 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Mike Stump773582d2009-07-23 23:25:26 +0000597 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000598 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000599 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000600 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000601 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000602 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000603 // If we know the condition is false, add NULL as the successor for
604 // the block containing the condition. In this case, the confluence
605 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000606 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000607 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000608 } else {
609 // If we have no LHS expression, add the ConfluenceBlock as a direct
610 // successor for the block containing the condition. Moreover, we need to
611 // reverse the order of the predecessors in the ConfluenceBlock because
612 // the RHSBlock will have been added to the succcessors already, and we
613 // want the first predecessor to the the block containing the expression
614 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000615 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000616 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000617 std::reverse(ConfluenceBlock->pred_begin(),
618 ConfluenceBlock->pred_end());
619 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000620 }
Mike Stump11289f42009-09-09 15:08:12 +0000621
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000622 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000623 Block->setTerminator(C);
624 return addStmt(C->getCond());
625}
626
Ted Kremenek93668002009-07-17 22:18:43 +0000627CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
628 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000629
Ted Kremenek93668002009-07-17 22:18:43 +0000630 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000631 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000632 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
Ted Kremenek93668002009-07-17 22:18:43 +0000635 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Ted Kremenek93668002009-07-17 22:18:43 +0000637 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
638 typedef llvm::SmallVector<Decl*,10> BufTy;
639 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000640
Ted Kremenek93668002009-07-17 22:18:43 +0000641 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
642 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
643 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
644 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000645
Ted Kremenek93668002009-07-17 22:18:43 +0000646 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
647 // automatically freed with the CFG.
648 DeclGroupRef DG(*I);
649 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000650 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000651 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000652
Ted Kremenek93668002009-07-17 22:18:43 +0000653 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000654 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000655 B = VisitDeclSubExpr(D);
656 }
Mike Stump11289f42009-09-09 15:08:12 +0000657
658 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000659}
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremenek93668002009-07-17 22:18:43 +0000661/// VisitDeclSubExpr - Utility method to add block-level expressions for
662/// initializers in Decls.
663CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
664 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000665
Ted Kremenek93668002009-07-17 22:18:43 +0000666 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000667
Ted Kremenek93668002009-07-17 22:18:43 +0000668 if (!VD)
669 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000670
Ted Kremenek93668002009-07-17 22:18:43 +0000671 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000672
Ted Kremenek93668002009-07-17 22:18:43 +0000673 if (Init) {
674 // Optimization: Don't create separate block-level statements for literals.
675 switch (Init->getStmtClass()) {
676 case Stmt::IntegerLiteralClass:
677 case Stmt::CharacterLiteralClass:
678 case Stmt::StringLiteralClass:
679 break;
680 default:
681 Block = addStmt(Init);
682 }
683 }
Mike Stump11289f42009-09-09 15:08:12 +0000684
Ted Kremenek93668002009-07-17 22:18:43 +0000685 // If the type of VD is a VLA, then we must process its size expressions.
686 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
687 VA = FindVA(VA->getElementType().getTypePtr()))
688 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenek93668002009-07-17 22:18:43 +0000690 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000691}
692
693CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000694 // We may see an if statement in the middle of a basic block, or it may be the
695 // first statement we are processing. In either case, we create a new basic
696 // block. First, we create the blocks for the then...else statements, and
697 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000698 // middle of a block, we stop processing that block. That block is then the
699 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000700
701 // The block we were proccessing is now finished. Make it the successor
702 // block.
703 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000704 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000705 if (!FinishBlock(Block))
706 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000707 }
Mike Stump31feda52009-07-17 01:31:16 +0000708
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000709 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000710 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000711
Ted Kremenek9aae5132007-08-23 21:42:29 +0000712 if (Stmt* Else = I->getElse()) {
713 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000714
Ted Kremenek9aae5132007-08-23 21:42:29 +0000715 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000716 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000717 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000718 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000719
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000720 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
721 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000722 else if (Block) {
723 if (!FinishBlock(ElseBlock))
724 return 0;
725 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000726 }
Mike Stump31feda52009-07-17 01:31:16 +0000727
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000728 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000729 CFGBlock* ThenBlock;
730 {
731 Stmt* Then = I->getThen();
732 assert (Then);
733 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000734 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000735 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000736
Ted Kremenek1b379512009-04-01 03:52:47 +0000737 if (!ThenBlock) {
738 // We can reach here if the "then" body has all NullStmts.
739 // Create an empty block so we can distinguish between true and false
740 // branches in path-sensitive analyses.
741 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000742 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000743 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000744 if (!FinishBlock(ThenBlock))
745 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000746 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000747 }
748
Mike Stump31feda52009-07-17 01:31:16 +0000749 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000750 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000751
Ted Kremenek9aae5132007-08-23 21:42:29 +0000752 // Set the terminator of the new block to the If statement.
753 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000754
Mike Stump773582d2009-07-23 23:25:26 +0000755 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000756 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000757
Ted Kremenek9aae5132007-08-23 21:42:29 +0000758 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000759 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
760 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000761
762 // Add the condition as the last statement in the new block. This may create
763 // new blocks as the condition may contain control-flow. Any newly created
764 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000765 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000766}
Mike Stump31feda52009-07-17 01:31:16 +0000767
768
Ted Kremenek9aae5132007-08-23 21:42:29 +0000769CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000770 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000771 //
Mike Stump31feda52009-07-17 01:31:16 +0000772 // NOTE: If a "return" appears in the middle of a block, this means that the
773 // code afterwards is DEAD (unreachable). We still keep a basic block
774 // for that code; a simple "mark-and-sweep" from the entry block will be
775 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000776 if (Block)
777 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000778
779 // Create the new block.
780 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000781
Ted Kremenek9aae5132007-08-23 21:42:29 +0000782 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000783 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000784
785 // Add the return statement to the block. This may create new blocks if R
786 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000787 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000788}
789
790CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
791 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000792 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000793 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000794
Ted Kremenek93668002009-07-17 22:18:43 +0000795 if (!LabelBlock) // This can happen when the body is empty, i.e.
796 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000797
Ted Kremenek93668002009-07-17 22:18:43 +0000798 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000799 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000800
801 // Labels partition blocks, so this is the end of the basic block we were
802 // processing (L is the block's label). Because this is label (and we have
803 // already processed the substatement) there is no extra control-flow to worry
804 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000805 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000806 if (!FinishBlock(LabelBlock))
807 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000808
809 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000810 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000811
Ted Kremenek9aae5132007-08-23 21:42:29 +0000812 // This block is now the implicit successor of other blocks.
813 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000814
Ted Kremenek9aae5132007-08-23 21:42:29 +0000815 return LabelBlock;
816}
817
818CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000819 // Goto is a control-flow statement. Thus we stop processing the current
820 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000821 if (Block)
822 FinishBlock(Block);
823
Ted Kremenek9aae5132007-08-23 21:42:29 +0000824 Block = createBlock(false);
825 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000826
827 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000828 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000829
Ted Kremenek9aae5132007-08-23 21:42:29 +0000830 if (I == LabelMap.end())
831 // We will need to backpatch this block later.
832 BackpatchBlocks.push_back(Block);
833 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000834 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000835
Mike Stump31feda52009-07-17 01:31:16 +0000836 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000837}
838
839CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000840 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000841
Mike Stump014b3ea2009-07-21 01:12:51 +0000842 // "for" is a control-flow statement. Thus we stop processing the current
843 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000844 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000845 if (!FinishBlock(Block))
846 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000847 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000848 } else
849 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000850
851 // Because of short-circuit evaluation, the condition of the loop can span
852 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
853 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000854 CFGBlock* ExitConditionBlock = createBlock(false);
855 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000856
Ted Kremenek81e14852007-08-27 19:46:09 +0000857 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000858 ExitConditionBlock->setTerminator(F);
859
860 // Now add the actual condition to the condition block. Because the condition
861 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000862 if (Stmt* C = F->getCond()) {
863 Block = ExitConditionBlock;
864 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000865 if (Block) {
866 if (!FinishBlock(EntryConditionBlock))
867 return 0;
868 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000869 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000870
Mike Stump31feda52009-07-17 01:31:16 +0000871 // The condition block is the implicit successor for the loop body as well as
872 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000873 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000874
Mike Stump773582d2009-07-23 23:25:26 +0000875 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000876 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000877
Mike Stump773582d2009-07-23 23:25:26 +0000878 if (F->getCond())
879 KnownVal = TryEvaluateBool(F->getCond());
880
Ted Kremenek9aae5132007-08-23 21:42:29 +0000881 // Now create the loop body.
882 {
883 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000884
Ted Kremenek9aae5132007-08-23 21:42:29 +0000885 // Save the current values for Block, Succ, and continue and break targets
886 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
887 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000888 save_break(BreakTargetBlock);
889
Ted Kremeneke9610502007-08-30 18:39:40 +0000890 // Create a new block to contain the (bottom) of the loop body.
891 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000892
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000893 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000894 // Generate increment code in its own basic block. This is the target of
895 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000896 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000897 } else {
898 // No increment code. Create a special, empty, block that is used as the
899 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000900 assert(Succ == EntryConditionBlock);
901 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000902 }
Mike Stump31feda52009-07-17 01:31:16 +0000903
Ted Kremenek902393b2009-04-28 00:51:56 +0000904 // Finish up the increment (or empty) block if it hasn't been already.
905 if (Block) {
906 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000907 if (!FinishBlock(Block))
908 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000909 Block = 0;
910 }
Mike Stump31feda52009-07-17 01:31:16 +0000911
Ted Kremenek902393b2009-04-28 00:51:56 +0000912 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000913
Ted Kremenek902393b2009-04-28 00:51:56 +0000914 // The starting block for the loop increment is the block that should
915 // represent the 'loop target' for looping back to the start of the loop.
916 ContinueTargetBlock->setLoopTarget(F);
917
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000918 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000919 BreakTargetBlock = LoopSuccessor;
920
921 // Now populate the body block, and in the process create new blocks as we
922 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000923 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000924
925 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000926 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000927 else if (Block && !FinishBlock(BodyBlock))
928 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000929
Ted Kremenek30754282009-07-24 04:47:11 +0000930 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000931 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000932 }
Mike Stump31feda52009-07-17 01:31:16 +0000933
Ted Kremenek30754282009-07-24 04:47:11 +0000934 // Link up the condition block with the code that follows the loop. (the
935 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000936 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenek9aae5132007-08-23 21:42:29 +0000938 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000939 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000940 if (Stmt* I = F->getInit()) {
941 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000942 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000943 } else {
944 // There is no loop initialization. We are thus basically a while loop.
945 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000946 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000947 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000948 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949 }
950}
951
Ted Kremenek9d56e642008-11-11 17:10:00 +0000952CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
953 // Objective-C fast enumeration 'for' statements:
954 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
955 //
956 // for ( Type newVariable in collection_expression ) { statements }
957 //
958 // becomes:
959 //
960 // prologue:
961 // 1. collection_expression
962 // T. jump to loop_entry
963 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000964 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000965 // 1. ObjCForCollectionStmt [performs binding to newVariable]
966 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
967 // TB:
968 // statements
969 // T. jump to loop_entry
970 // FB:
971 // what comes after
972 //
973 // and
974 //
975 // Type existingItem;
976 // for ( existingItem in expression ) { statements }
977 //
978 // becomes:
979 //
Mike Stump31feda52009-07-17 01:31:16 +0000980 // the same with newVariable replaced with existingItem; the binding works
981 // the same except that for one ObjCForCollectionStmt::getElement() returns
982 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000983 //
Mike Stump31feda52009-07-17 01:31:16 +0000984
Ted Kremenek9d56e642008-11-11 17:10:00 +0000985 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000986
Ted Kremenek9d56e642008-11-11 17:10:00 +0000987 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000988 if (!FinishBlock(Block))
989 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000990 LoopSuccessor = Block;
991 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000992 } else
993 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000994
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000995 // Build the condition blocks.
996 CFGBlock* ExitConditionBlock = createBlock(false);
997 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000998
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000999 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001000 ExitConditionBlock->setTerminator(S);
1001
1002 // The last statement in the block should be the ObjCForCollectionStmt, which
1003 // performs the actual binding to 'element' and determines if there are any
1004 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001005 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001006 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001007
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001008 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001009 // generate new blocks as necesary. We DON'T add the statement by default to
1010 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001011 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001012 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001013 if (!FinishBlock(EntryConditionBlock))
1014 return 0;
1015 Block = 0;
1016 }
Mike Stump31feda52009-07-17 01:31:16 +00001017
1018 // The condition block is the implicit successor for the loop body as well as
1019 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001020 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001021
Ted Kremenek9d56e642008-11-11 17:10:00 +00001022 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001023 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001024 // Save the current values for Succ, continue and break targets.
1025 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001026 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1027
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001028 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001029 ContinueTargetBlock = EntryConditionBlock;
1030
Ted Kremenek93668002009-07-17 22:18:43 +00001031 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001032
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001033 if (!BodyBlock)
1034 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001035 else if (Block) {
1036 if (!FinishBlock(BodyBlock))
1037 return 0;
1038 }
Mike Stump31feda52009-07-17 01:31:16 +00001039
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001040 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001041 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001042 }
Mike Stump31feda52009-07-17 01:31:16 +00001043
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001044 // Link up the condition block with the code that follows the loop.
1045 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001046 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001047
Ted Kremenek9d56e642008-11-11 17:10:00 +00001048 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001049 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001050 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001051}
1052
Ted Kremenek49805452009-05-02 01:49:13 +00001053CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1054 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001055
Ted Kremenek49805452009-05-02 01:49:13 +00001056 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001057 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001058
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001059 // The sync body starts its own basic block. This makes it a little easier
1060 // for diagnostic clients.
1061 if (SyncBlock) {
1062 if (!FinishBlock(SyncBlock))
1063 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001064
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001065 Block = 0;
1066 }
Mike Stump31feda52009-07-17 01:31:16 +00001067
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001068 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001069
Ted Kremenek49805452009-05-02 01:49:13 +00001070 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001071 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001072}
Mike Stump31feda52009-07-17 01:31:16 +00001073
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001074CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001075 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001076 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001077}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001078
Ted Kremenek9aae5132007-08-23 21:42:29 +00001079CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001080 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001081
Mike Stump014b3ea2009-07-21 01:12:51 +00001082 // "while" is a control-flow statement. Thus we stop processing the current
1083 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001084 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001085 if (!FinishBlock(Block))
1086 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001087 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001088 } else
1089 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001090
1091 // Because of short-circuit evaluation, the condition of the loop can span
1092 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1093 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001094 CFGBlock* ExitConditionBlock = createBlock(false);
1095 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001096
Ted Kremenek81e14852007-08-27 19:46:09 +00001097 // Set the terminator for the "exit" condition block.
1098 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001099
1100 // Now add the actual condition to the condition block. Because the condition
1101 // itself may contain control-flow, new blocks may be created. Thus we update
1102 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001103 if (Stmt* C = W->getCond()) {
1104 Block = ExitConditionBlock;
1105 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001106 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001107 if (Block) {
1108 if (!FinishBlock(EntryConditionBlock))
1109 return 0;
1110 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001111 }
Mike Stump31feda52009-07-17 01:31:16 +00001112
1113 // The condition block is the implicit successor for the loop body as well as
1114 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001115 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001116
Mike Stump773582d2009-07-23 23:25:26 +00001117 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001118 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001119
Ted Kremenek9aae5132007-08-23 21:42:29 +00001120 // Process the loop body.
1121 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001122 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001123
1124 // Save the current values for Block, Succ, and continue and break targets
1125 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1126 save_continue(ContinueTargetBlock),
1127 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001128
Mike Stump31feda52009-07-17 01:31:16 +00001129 // Create an empty block to represent the transition block for looping back
1130 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001131 Block = 0;
1132 assert(Succ == EntryConditionBlock);
1133 Succ = createBlock();
1134 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001135 ContinueTargetBlock = Succ;
1136
Ted Kremenek9aae5132007-08-23 21:42:29 +00001137 // All breaks should go to the code following the loop.
1138 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001139
Ted Kremenek9aae5132007-08-23 21:42:29 +00001140 // NULL out Block to force lazy instantiation of blocks for the body.
1141 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001142
Ted Kremenek9aae5132007-08-23 21:42:29 +00001143 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001144 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001145
Ted Kremeneke9610502007-08-30 18:39:40 +00001146 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001147 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001148 else if (Block) {
1149 if (!FinishBlock(BodyBlock))
1150 return 0;
1151 }
Mike Stump31feda52009-07-17 01:31:16 +00001152
Ted Kremenek30754282009-07-24 04:47:11 +00001153 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001154 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001155 }
Mike Stump31feda52009-07-17 01:31:16 +00001156
Ted Kremenek30754282009-07-24 04:47:11 +00001157 // Link up the condition block with the code that follows the loop. (the
1158 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001159 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001160
1161 // There can be no more statements in the condition block since we loop back
1162 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001163 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek9aae5132007-08-23 21:42:29 +00001165 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001166 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001167 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001168}
Mike Stump11289f42009-09-09 15:08:12 +00001169
1170
Ted Kremenek93668002009-07-17 22:18:43 +00001171CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1172 // FIXME: For now we pretend that @catch and the code it contains does not
1173 // exit.
1174 return Block;
1175}
Mike Stump31feda52009-07-17 01:31:16 +00001176
Ted Kremenek93041ba2008-12-09 20:20:09 +00001177CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1178 // FIXME: This isn't complete. We basically treat @throw like a return
1179 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001180
Ted Kremenek0868eea2009-09-24 18:45:41 +00001181 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001182 if (Block && !FinishBlock(Block))
1183 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001184
Ted Kremenek93041ba2008-12-09 20:20:09 +00001185 // Create the new block.
1186 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001187
Ted Kremenek93041ba2008-12-09 20:20:09 +00001188 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001189 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001190
1191 // Add the statement to the block. This may create new blocks if S contains
1192 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001193 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001194}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001195
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001196CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001197 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001198 if (Block && !FinishBlock(Block))
1199 return 0;
1200
1201 // Create the new block.
1202 Block = createBlock(false);
1203
1204 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001205 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001206
1207 // Add the statement to the block. This may create new blocks if S contains
1208 // control-flow (short-circuit operations).
1209 return VisitStmt(T, true);
1210}
1211
Ted Kremenek93668002009-07-17 22:18:43 +00001212CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001213 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001214
Mike Stump8d50b6a2009-07-21 01:27:50 +00001215 // "do...while" is a control-flow statement. Thus we stop processing the
1216 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001217 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001218 if (!FinishBlock(Block))
1219 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001220 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001221 } else
1222 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001223
1224 // Because of short-circuit evaluation, the condition of the loop can span
1225 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1226 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001227 CFGBlock* ExitConditionBlock = createBlock(false);
1228 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001229
Ted Kremenek81e14852007-08-27 19:46:09 +00001230 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001231 ExitConditionBlock->setTerminator(D);
1232
1233 // Now add the actual condition to the condition block. Because the condition
1234 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001235 if (Stmt* C = D->getCond()) {
1236 Block = ExitConditionBlock;
1237 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001238 if (Block) {
1239 if (!FinishBlock(EntryConditionBlock))
1240 return 0;
1241 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001242 }
Mike Stump31feda52009-07-17 01:31:16 +00001243
Ted Kremeneka1523a32008-02-27 07:20:00 +00001244 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001245 Succ = EntryConditionBlock;
1246
Mike Stump773582d2009-07-23 23:25:26 +00001247 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001248 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001249
Ted Kremenek9aae5132007-08-23 21:42:29 +00001250 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001251 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001252 {
1253 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001254
Ted Kremenek9aae5132007-08-23 21:42:29 +00001255 // Save the current values for Block, Succ, and continue and break targets
1256 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1257 save_continue(ContinueTargetBlock),
1258 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001259
Ted Kremenek9aae5132007-08-23 21:42:29 +00001260 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001261 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001262
Ted Kremenek9aae5132007-08-23 21:42:29 +00001263 // All breaks should go to the code following the loop.
1264 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001265
Ted Kremenek9aae5132007-08-23 21:42:29 +00001266 // NULL out Block to force lazy instantiation of blocks for the body.
1267 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001268
Ted Kremenek9aae5132007-08-23 21:42:29 +00001269 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001270 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001271
Ted Kremeneke9610502007-08-30 18:39:40 +00001272 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001273 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001274 else if (Block) {
1275 if (!FinishBlock(BodyBlock))
1276 return 0;
1277 }
Mike Stump31feda52009-07-17 01:31:16 +00001278
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001279 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001280 // ExitConditionBlock to represent the "loop back" transition. Create an
1281 // empty block to represent the transition block for looping back to the
1282 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001283 // FIXME: Can we do this more efficiently without adding another block?
1284 Block = NULL;
1285 Succ = BodyBlock;
1286 CFGBlock *LoopBackBlock = createBlock();
1287 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001288
Ted Kremenek30754282009-07-24 04:47:11 +00001289 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001290 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001291 }
Mike Stump31feda52009-07-17 01:31:16 +00001292
Ted Kremenek30754282009-07-24 04:47:11 +00001293 // Link up the condition block with the code that follows the loop.
1294 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001295 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001296
1297 // There can be no more statements in the body block(s) since we loop back to
1298 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001299 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001300
Ted Kremenek9aae5132007-08-23 21:42:29 +00001301 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001302 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001303 return BodyBlock;
1304}
1305
1306CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1307 // "continue" is a control-flow statement. Thus we stop processing the
1308 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001309 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001310 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001311
Ted Kremenek9aae5132007-08-23 21:42:29 +00001312 // Now create a new block that ends with the continue statement.
1313 Block = createBlock(false);
1314 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001315
Ted Kremenek9aae5132007-08-23 21:42:29 +00001316 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001317 // incomplete AST. This means the CFG cannot be constructed.
1318 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001319 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001320 else
1321 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001322
Ted Kremenek9aae5132007-08-23 21:42:29 +00001323 return Block;
1324}
Mike Stump11289f42009-09-09 15:08:12 +00001325
Ted Kremenek0747de62009-07-18 00:47:21 +00001326CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1327 bool alwaysAdd) {
1328
1329 if (alwaysAdd) {
1330 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001331 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001332 }
Mike Stump11289f42009-09-09 15:08:12 +00001333
Ted Kremenek93668002009-07-17 22:18:43 +00001334 // VLA types have expressions that must be evaluated.
1335 if (E->isArgumentType()) {
1336 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1337 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1338 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001339 }
Mike Stump11289f42009-09-09 15:08:12 +00001340
Mike Stump31feda52009-07-17 01:31:16 +00001341 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342}
Mike Stump11289f42009-09-09 15:08:12 +00001343
Ted Kremenek93668002009-07-17 22:18:43 +00001344/// VisitStmtExpr - Utility method to handle (nested) statement
1345/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001346CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1347 if (alwaysAdd) {
1348 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001349 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001350 }
Ted Kremenek93668002009-07-17 22:18:43 +00001351 return VisitCompoundStmt(SE->getSubStmt());
1352}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001353
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001354CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001355 // "switch" is a control-flow statement. Thus we stop processing the current
1356 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001357 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremenek9aae5132007-08-23 21:42:29 +00001359 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001360 if (!FinishBlock(Block))
1361 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001362 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001363 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001364
1365 // Save the current "switch" context.
1366 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001367 save_break(BreakTargetBlock),
1368 save_default(DefaultCaseBlock);
1369
Mike Stump31feda52009-07-17 01:31:16 +00001370 // Set the "default" case to be the block after the switch statement. If the
1371 // switch statement contains a "default:", this value will be overwritten with
1372 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001373 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001374
Ted Kremenek9aae5132007-08-23 21:42:29 +00001375 // Create a new block that will contain the switch statement.
1376 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001377
Ted Kremenek9aae5132007-08-23 21:42:29 +00001378 // Now process the switch body. The code after the switch is the implicit
1379 // successor.
1380 Succ = SwitchSuccessor;
1381 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001382
1383 // When visiting the body, the case statements should automatically get linked
1384 // up to the switch. We also don't keep a pointer to the body, since all
1385 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001386 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001387 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001388 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001389 if (Block) {
1390 if (!FinishBlock(BodyBlock))
1391 return 0;
1392 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001393
Mike Stump31feda52009-07-17 01:31:16 +00001394 // If we have no "default:" case, the default transition is to the code
1395 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001396 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001397
Ted Kremenek81e14852007-08-27 19:46:09 +00001398 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001399 SwitchTerminatedBlock->setTerminator(Terminator);
1400 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001401 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001402
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001403 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404}
1405
Ted Kremenek93668002009-07-17 22:18:43 +00001406CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001407 // CaseStmts are essentially labels, so they are the first statement in a
1408 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001409
Ted Kremenek93668002009-07-17 22:18:43 +00001410 if (CS->getSubStmt())
1411 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001412
Ted Kremenek55e91e82007-08-30 18:48:11 +00001413 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001414 if (!CaseBlock)
1415 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001416
1417 // Cases statements partition blocks, so this is the top of the basic block we
1418 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001419 CaseBlock->setLabel(CS);
1420
Ted Kremenek55957a82009-05-02 00:13:27 +00001421 if (!FinishBlock(CaseBlock))
1422 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001423
1424 // Add this block to the list of successors for the block with the switch
1425 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001426 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001427 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001428
Ted Kremenek9aae5132007-08-23 21:42:29 +00001429 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1430 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001431
Ted Kremenek9aae5132007-08-23 21:42:29 +00001432 // This block is now the implicit successor of other blocks.
1433 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001434
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001435 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001436}
Mike Stump31feda52009-07-17 01:31:16 +00001437
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001438CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001439 if (Terminator->getSubStmt())
1440 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001441
Ted Kremenek654c78f2008-02-13 22:05:39 +00001442 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001443
1444 if (!DefaultCaseBlock)
1445 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001446
1447 // Default statements partition blocks, so this is the top of the basic block
1448 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001449 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001450
Ted Kremenek55957a82009-05-02 00:13:27 +00001451 if (!FinishBlock(DefaultCaseBlock))
1452 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001453
Mike Stump31feda52009-07-17 01:31:16 +00001454 // Unlike case statements, we don't add the default block to the successors
1455 // for the switch statement immediately. This is done when we finish
1456 // processing the switch statement. This allows for the default case
1457 // (including a fall-through to the code after the switch statement) to always
1458 // be the last successor of a switch-terminated block.
1459
Ted Kremenek654c78f2008-02-13 22:05:39 +00001460 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1461 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek654c78f2008-02-13 22:05:39 +00001463 // This block is now the implicit successor of other blocks.
1464 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001465
1466 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001467}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001468
Ted Kremenekeda180e22007-08-28 19:26:49 +00001469CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001470 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001471 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001472
Ted Kremenekeda180e22007-08-28 19:26:49 +00001473 if (!IBlock) {
1474 IBlock = createBlock(false);
1475 cfg->setIndirectGotoBlock(IBlock);
1476 }
Mike Stump31feda52009-07-17 01:31:16 +00001477
Ted Kremenekeda180e22007-08-28 19:26:49 +00001478 // IndirectGoto is a control-flow statement. Thus we stop processing the
1479 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001480 if (Block && !FinishBlock(Block))
1481 return 0;
1482
Ted Kremenekeda180e22007-08-28 19:26:49 +00001483 Block = createBlock(false);
1484 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001485 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001486 return addStmt(I->getTarget());
1487}
1488
Ted Kremenek04cca642007-08-23 21:26:19 +00001489} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001490
Mike Stump31feda52009-07-17 01:31:16 +00001491/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1492/// no successors or predecessors. If this is the first block created in the
1493/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001494CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001495 bool first_block = begin() == end();
1496
1497 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001498 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1499 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1500 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001501
1502 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001503 if (first_block)
1504 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001505
1506 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001507 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001508}
1509
Ted Kremenek889073f2007-08-23 16:51:22 +00001510/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1511/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001512CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001513 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001514 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001515}
1516
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001517//===----------------------------------------------------------------------===//
1518// CFG: Queries for BlkExprs.
1519//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001520
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001521namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001522 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001523}
1524
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001525static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1526 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001527 return;
Mike Stump31feda52009-07-17 01:31:16 +00001528
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001529 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001530 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001531
Ted Kremenek95a123c2008-01-26 00:03:27 +00001532 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1533 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001534
Ted Kremenek95a123c2008-01-26 00:03:27 +00001535 FindSubExprAssignments(*I, Set);
1536 }
1537}
1538
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001539static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1540 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001541
1542 // Look for assignments that are used as subexpressions. These are the only
1543 // assignments that we want to *possibly* register as a block-level
1544 // expression. Basically, if an assignment occurs both in a subexpression and
1545 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001546 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001547
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001548 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001549 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001550 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001551
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001552 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001553
1554 // Iterate over the statements again on identify the Expr* and Stmt* at the
1555 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001556
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001557 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001558 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001559
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001560 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001561 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001562 // expression are really "statements" whose value is never used by
1563 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001564 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001565 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001566 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1567 // Special handling for statement expressions. The last statement in
1568 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001569 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001570 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001571 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001572 (*M)[C->body_back()] = x;
1573 }
1574 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001575
Ted Kremenek95a123c2008-01-26 00:03:27 +00001576 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001577 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001578 }
Mike Stump31feda52009-07-17 01:31:16 +00001579
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001580 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001581
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001582 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001583
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001584 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001585 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001586 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001587 }
1588 }
Mike Stump31feda52009-07-17 01:31:16 +00001589
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001590 return M;
1591}
1592
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001593CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1594 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001595 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001596
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001597 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001598 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001600 if (I == M->end()) return CFG::BlkExprNumTy();
1601 else return CFG::BlkExprNumTy(I->second);
1602}
1603
1604unsigned CFG::getNumBlkExprs() {
1605 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1606 return M->size();
1607 else {
1608 // We assume callers interested in the number of BlkExprs will want
1609 // the map constructed if it doesn't already exist.
1610 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1611 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1612 }
1613}
1614
Ted Kremenek6065ef62008-04-28 18:00:46 +00001615//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001616// Cleanup: CFG dstor.
1617//===----------------------------------------------------------------------===//
1618
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001619CFG::~CFG() {
1620 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1621}
Mike Stump31feda52009-07-17 01:31:16 +00001622
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001623//===----------------------------------------------------------------------===//
1624// CFG pretty printing
1625//===----------------------------------------------------------------------===//
1626
Ted Kremenek7e776b12007-08-22 18:22:34 +00001627namespace {
1628
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001629class StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001630
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001631 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1632 StmtMapTy StmtMap;
1633 signed CurrentBlock;
1634 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001635 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001636public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001637
Chris Lattnerc61089a2009-06-30 01:26:17 +00001638 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1639 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001640 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1641 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001642 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001643 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001644 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001645 }
1646 }
Mike Stump31feda52009-07-17 01:31:16 +00001647
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001648 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001649
Chris Lattnerc61089a2009-06-30 01:26:17 +00001650 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001651 void setBlockID(signed i) { CurrentBlock = i; }
1652 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001653
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001654 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001655
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001656 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001657
1658 if (I == StmtMap.end())
1659 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001660
1661 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001662 && I->second.second == CurrentStmt)
1663 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001664
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001665 OS << "[B" << I->second.first << "." << I->second.second << "]";
1666 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001667 }
1668};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001669} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001670
Chris Lattnerc61089a2009-06-30 01:26:17 +00001671
1672namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001673class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001674 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001675
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001676 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001677 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001678 PrintingPolicy Policy;
1679
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001680public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001681 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001682 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001683 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001684
Ted Kremenek9aae5132007-08-23 21:42:29 +00001685 void VisitIfStmt(IfStmt* I) {
1686 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001687 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001688 }
Mike Stump31feda52009-07-17 01:31:16 +00001689
Ted Kremenek9aae5132007-08-23 21:42:29 +00001690 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001691 void VisitStmt(Stmt* Terminator) {
1692 Terminator->printPretty(OS, Helper, Policy);
1693 }
1694
Ted Kremenek9aae5132007-08-23 21:42:29 +00001695 void VisitForStmt(ForStmt* F) {
1696 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001697 if (F->getInit()) OS << "...";
1698 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001699 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001700 OS << "; ";
1701 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001702 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001703 }
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenek9aae5132007-08-23 21:42:29 +00001705 void VisitWhileStmt(WhileStmt* W) {
1706 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001707 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001708 }
Mike Stump31feda52009-07-17 01:31:16 +00001709
Ted Kremenek9aae5132007-08-23 21:42:29 +00001710 void VisitDoStmt(DoStmt* D) {
1711 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001712 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001713 }
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001715 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001716 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001717 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001718 }
Mike Stump31feda52009-07-17 01:31:16 +00001719
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001720 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001721 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001722 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001723 }
Mike Stump31feda52009-07-17 01:31:16 +00001724
Ted Kremenek391f94a2007-08-31 22:29:13 +00001725 void VisitChooseExpr(ChooseExpr* C) {
1726 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001727 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001728 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001729 }
Mike Stump31feda52009-07-17 01:31:16 +00001730
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001731 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1732 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001733 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001734 }
Mike Stump31feda52009-07-17 01:31:16 +00001735
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001736 void VisitBinaryOperator(BinaryOperator* B) {
1737 if (!B->isLogicalOp()) {
1738 VisitExpr(B);
1739 return;
1740 }
Mike Stump31feda52009-07-17 01:31:16 +00001741
Douglas Gregor7de59662009-05-29 20:38:28 +00001742 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001743
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001744 switch (B->getOpcode()) {
1745 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001746 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001747 return;
1748 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001749 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001750 return;
1751 default:
1752 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001753 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001754 }
Mike Stump31feda52009-07-17 01:31:16 +00001755
Ted Kremenek12687ff2007-08-27 21:54:41 +00001756 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001757 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001758 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001759};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001760} // end anonymous namespace
1761
Mike Stump31feda52009-07-17 01:31:16 +00001762
Chris Lattnerc61089a2009-06-30 01:26:17 +00001763static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1764 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001765 if (Helper) {
1766 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001767 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001768 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001770 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001771 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001772 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001773 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001774 return;
1775 }
1776 }
Mike Stump31feda52009-07-17 01:31:16 +00001777
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001778 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001779 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001780 if (B->getOpcode() == BinaryOperator::Comma) {
1781 OS << "... , ";
1782 Helper->handledStmt(B->getRHS(),OS);
1783 OS << '\n';
1784 return;
Mike Stump31feda52009-07-17 01:31:16 +00001785 }
1786 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001787 }
Mike Stump31feda52009-07-17 01:31:16 +00001788
Chris Lattnerc61089a2009-06-30 01:26:17 +00001789 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001790
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001791 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001792 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001793}
Mike Stump31feda52009-07-17 01:31:16 +00001794
Chris Lattnerc61089a2009-06-30 01:26:17 +00001795static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1796 const CFGBlock& B,
1797 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001798
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001799 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001800
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001801 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001802 OS << "\n [ B" << B.getBlockID();
1803
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001804 if (&B == &cfg->getEntry())
1805 OS << " (ENTRY) ]\n";
1806 else if (&B == &cfg->getExit())
1807 OS << " (EXIT) ]\n";
1808 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001809 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001810 else
1811 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001812
Ted Kremenek71eca012007-08-29 23:20:49 +00001813 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001814 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001815
1816 if (print_edges)
1817 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001818
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001819 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001820 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001821 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001822 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001823 C->getLHS()->printPretty(OS, Helper,
1824 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001825 if (C->getRHS()) {
1826 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001827 C->getRHS()->printPretty(OS, Helper,
1828 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001829 }
Mike Stump31feda52009-07-17 01:31:16 +00001830 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001831 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001832 else
1833 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenek71eca012007-08-29 23:20:49 +00001835 OS << ":\n";
1836 }
Mike Stump31feda52009-07-17 01:31:16 +00001837
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001838 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001839 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001840
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001841 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1842 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001843
Ted Kremenek71eca012007-08-29 23:20:49 +00001844 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001845 if (print_edges)
1846 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001847
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001848 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001849
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001850 if (Helper)
1851 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001853 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001854 }
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenek71eca012007-08-29 23:20:49 +00001856 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001857 if (B.getTerminator()) {
1858 if (print_edges)
1859 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001860
Ted Kremenek71eca012007-08-29 23:20:49 +00001861 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001862
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001863 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001864
Chris Lattnerc61089a2009-06-30 01:26:17 +00001865 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1866 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001867 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001868 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001869 }
Mike Stump31feda52009-07-17 01:31:16 +00001870
Ted Kremenek71eca012007-08-29 23:20:49 +00001871 if (print_edges) {
1872 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001873 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001874 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001875
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001876 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1877 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001878
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001879 if (i == 8 || (i-8) == 0)
1880 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenek71eca012007-08-29 23:20:49 +00001882 OS << " B" << (*I)->getBlockID();
1883 }
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001885 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001887 // Print the successors of this block.
1888 OS << " Successors (" << B.succ_size() << "):";
1889 i = 0;
1890
1891 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1892 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001893
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001894 if (i == 8 || (i-8) % 10 == 0)
1895 OS << "\n ";
1896
Mike Stump0d76d072009-07-20 23:24:15 +00001897 if (*I)
1898 OS << " B" << (*I)->getBlockID();
1899 else
1900 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001901 }
Mike Stump31feda52009-07-17 01:31:16 +00001902
Ted Kremenek71eca012007-08-29 23:20:49 +00001903 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001904 }
Mike Stump31feda52009-07-17 01:31:16 +00001905}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001906
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001907
1908/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001909void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001910
1911/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001912void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1913 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001915 // Print the entry block.
1916 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001917
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001918 // Iterate through the CFGBlocks and print them one by one.
1919 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1920 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001921 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001922 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001923
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001924 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001925 }
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001927 // Print the exit block.
1928 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001929 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001930}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001931
1932/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001933void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1934 print(llvm::errs(), cfg, LO);
1935}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001936
1937/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1938/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001939void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1940 const LangOptions &LO) const {
1941 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001942 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001943}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001944
Ted Kremenek15647632008-01-30 23:02:42 +00001945/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001946void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001947 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001948 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001949 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1950}
1951
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001952Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001953
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001954 if (!Terminator)
1955 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001956
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001957 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001958
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001959 switch (Terminator->getStmtClass()) {
1960 default:
1961 break;
Mike Stump31feda52009-07-17 01:31:16 +00001962
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001963 case Stmt::ForStmtClass:
1964 E = cast<ForStmt>(Terminator)->getCond();
1965 break;
Mike Stump31feda52009-07-17 01:31:16 +00001966
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001967 case Stmt::WhileStmtClass:
1968 E = cast<WhileStmt>(Terminator)->getCond();
1969 break;
Mike Stump31feda52009-07-17 01:31:16 +00001970
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001971 case Stmt::DoStmtClass:
1972 E = cast<DoStmt>(Terminator)->getCond();
1973 break;
Mike Stump31feda52009-07-17 01:31:16 +00001974
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001975 case Stmt::IfStmtClass:
1976 E = cast<IfStmt>(Terminator)->getCond();
1977 break;
Mike Stump31feda52009-07-17 01:31:16 +00001978
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001979 case Stmt::ChooseExprClass:
1980 E = cast<ChooseExpr>(Terminator)->getCond();
1981 break;
Mike Stump31feda52009-07-17 01:31:16 +00001982
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001983 case Stmt::IndirectGotoStmtClass:
1984 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1985 break;
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001987 case Stmt::SwitchStmtClass:
1988 E = cast<SwitchStmt>(Terminator)->getCond();
1989 break;
Mike Stump31feda52009-07-17 01:31:16 +00001990
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001991 case Stmt::ConditionalOperatorClass:
1992 E = cast<ConditionalOperator>(Terminator)->getCond();
1993 break;
Mike Stump31feda52009-07-17 01:31:16 +00001994
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001995 case Stmt::BinaryOperatorClass: // '&&' and '||'
1996 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001997 break;
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001999 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002000 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002001 }
Mike Stump31feda52009-07-17 01:31:16 +00002002
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002003 return E ? E->IgnoreParens() : NULL;
2004}
2005
Ted Kremenek92137a32008-05-16 16:06:00 +00002006bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002007
Ted Kremenek92137a32008-05-16 16:06:00 +00002008 if (!Terminator)
2009 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenek92137a32008-05-16 16:06:00 +00002011 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002012
Ted Kremenek92137a32008-05-16 16:06:00 +00002013 switch (Terminator->getStmtClass()) {
2014 default:
2015 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002016
2017 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002018 case Stmt::WhileStmtClass:
2019 case Stmt::DoStmtClass:
2020 case Stmt::IfStmtClass:
2021 case Stmt::ChooseExprClass:
2022 case Stmt::ConditionalOperatorClass:
2023 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002024 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002025 }
Mike Stump31feda52009-07-17 01:31:16 +00002026
Ted Kremenek92137a32008-05-16 16:06:00 +00002027 return E ? E->IgnoreParens() : NULL;
2028}
2029
Ted Kremenek15647632008-01-30 23:02:42 +00002030
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002031//===----------------------------------------------------------------------===//
2032// CFG Graphviz Visualization
2033//===----------------------------------------------------------------------===//
2034
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002035
2036#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002037static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002038#endif
2039
Chris Lattnerc61089a2009-06-30 01:26:17 +00002040void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002041#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002042 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002043 GraphHelper = &H;
2044 llvm::ViewGraph(this,"CFG");
2045 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002046#endif
2047}
2048
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002049namespace llvm {
2050template<>
2051struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002052
2053 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2054
2055 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002056
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002057#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002058 std::string OutSStr;
2059 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002060 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002061 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002062
2063 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2064
2065 // Process string output to make it nicer...
2066 for (unsigned i = 0; i != OutStr.length(); ++i)
2067 if (OutStr[i] == '\n') { // Left justify
2068 OutStr[i] = '\\';
2069 OutStr.insert(OutStr.begin()+i+1, 'l');
2070 }
Mike Stump31feda52009-07-17 01:31:16 +00002071
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002072 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002073#else
2074 return "";
2075#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002076 }
2077};
2078} // end namespace llvm