blob: 5e2b6a3fc9cdee88f423e237d625bae7a2954d33 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramera3b13412010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000047
Benjamin Kramera3b13412010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek66de6032010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000050
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051private:
Benjamin Kramera3b13412010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000053};
Mike Stump31feda52009-07-17 01:31:16 +000054
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump31feda52009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000072
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +000080
Ted Kremenekeda180e22007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000089
Ted Kremenekeda180e22007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000093
Zhongxing Xud38fb842010-09-16 03:28:18 +000094 bool badCFG;
95 CFG::BuildOptions BuildOpts;
96
Mike Stump31feda52009-07-17 01:31:16 +000097public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000098 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
99 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +0000100 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000101 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000102 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000103
Ted Kremenek9aae5132007-08-23 21:42:29 +0000104 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000105 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000106 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000107
Ted Kremenek93668002009-07-17 22:18:43 +0000108private:
109 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000110 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
111 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
112 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000113 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000114 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
115 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
116 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000117 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000118 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000119 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000120 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000121 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000122 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000123 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000124 CFGBlock *VisitDeclStmt(DeclStmt *DS);
125 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000126 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
127 CFGBlock *VisitDoStmt(DoStmt *D);
128 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000129 CFGBlock *VisitGotoStmt(GotoStmt* G);
130 CFGBlock *VisitIfStmt(IfStmt *I);
131 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
132 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000133 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000134 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
135 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
136 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
137 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
138 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
139 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000140 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
141 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000142 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
143 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000144
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000145 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
146 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000147 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000148
Ted Kremenek6065ef62008-04-28 18:00:46 +0000149 // NYS == Not Yet Supported
150 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000151 badCFG = true;
152 return Block;
153 }
Mike Stump31feda52009-07-17 01:31:16 +0000154
Ted Kremenek93668002009-07-17 22:18:43 +0000155 void autoCreateBlock() { if (!Block) Block = createBlock(); }
156 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000157
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000158 CFGBlock *addStmt(Stmt *S) {
159 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000160 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000161
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000162 void AppendStmt(CFGBlock *B, Stmt *S,
163 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
164 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000165 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000166
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000167 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
168 B->addSuccessor(S, cfg->getBumpVectorContext());
169 }
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenek963cc312009-07-24 06:55:42 +0000171 /// TryResult - a class representing a variant over the values
172 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
173 /// and is used by the CFGBuilder to decide if a branch condition
174 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000175 class TryResult {
176 int X;
177 public:
178 TryResult(bool b) : X(b ? 1 : 0) {}
179 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000180
Ted Kremenek30754282009-07-24 04:47:11 +0000181 bool isTrue() const { return X == 1; }
182 bool isFalse() const { return X == 0; }
183 bool isKnown() const { return X >= 0; }
184 void negate() {
185 assert(isKnown());
186 X ^= 0x1;
187 }
188 };
Mike Stump11289f42009-09-09 15:08:12 +0000189
Mike Stump773582d2009-07-23 23:25:26 +0000190 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
191 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000192 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000193 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000194 return TryResult();
195
Mike Stump773582d2009-07-23 23:25:26 +0000196 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000197 if (!S->isTypeDependent() && !S->isValueDependent() &&
198 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000199 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000200
201 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000202 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000203};
Mike Stump31feda52009-07-17 01:31:16 +0000204
Douglas Gregor4619e432008-12-05 23:32:09 +0000205// FIXME: Add support for dependent-sized array types in C++?
206// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000207static VariableArrayType* FindVA(Type* t) {
208 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
209 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
210 if (vat->getSizeExpr())
211 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000212
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000213 t = vt->getElementType().getTypePtr();
214 }
Mike Stump31feda52009-07-17 01:31:16 +0000215
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000216 return 0;
217}
Mike Stump31feda52009-07-17 01:31:16 +0000218
219/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
220/// arbitrary statement. Examples include a single expression or a function
221/// body (compound statement). The ownership of the returned CFG is
222/// transferred to the caller. If CFG construction fails, this method returns
223/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000224CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000225 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000226
Mike Stump0d76d072009-07-20 23:24:15 +0000227 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000228 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000229 if (!Statement)
230 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000231
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000232 BuildOpts = BO;
233 if (!C->getLangOptions().CPlusPlus)
234 BuildOpts.AddImplicitDtors = false;
Mike Stump31feda52009-07-17 01:31:16 +0000235
236 // Create an empty block that will serve as the exit block for the CFG. Since
237 // this is the first block added to the CFG, it will be implicitly registered
238 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000239 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000240 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000241 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000242
Ted Kremenek9aae5132007-08-23 21:42:29 +0000243 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000244 CFGBlock *B = addStmt(Statement);
245
246 if (badCFG)
247 return NULL;
248
249 if (B)
250 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000251
252 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
253 // FIXME: Add code for base initializers and member initializers.
254 (void)CD;
255 }
Mike Stump31feda52009-07-17 01:31:16 +0000256
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000257 // Backpatch the gotos whose label -> block mappings we didn't know when we
258 // encountered them.
259 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
260 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000261
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000262 CFGBlock* B = *I;
263 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
264 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000265
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000266 // If there is no target for the goto, then we are looking at an
267 // incomplete AST. Handle this by not registering a successor.
268 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000269
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000270 AddSuccessor(B, LI->second);
271 }
272
273 // Add successors to the Indirect Goto Dispatch block (if we have one).
274 if (CFGBlock* B = cfg->getIndirectGotoBlock())
275 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
276 E = AddressTakenLabels.end(); I != E; ++I ) {
277
278 // Lookup the target block.
279 LabelMapTy::iterator LI = LabelMap.find(*I);
280
281 // If there is no target block that contains label, then we are looking
282 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000283 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000284
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000285 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000286 }
Mike Stump31feda52009-07-17 01:31:16 +0000287
Mike Stump31feda52009-07-17 01:31:16 +0000288 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000289 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000290
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000291 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000292}
Mike Stump31feda52009-07-17 01:31:16 +0000293
Ted Kremenek9aae5132007-08-23 21:42:29 +0000294/// createBlock - Used to lazily create blocks that are connected
295/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000296CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000297 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000298 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000299 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000300 return B;
301}
Mike Stump31feda52009-07-17 01:31:16 +0000302
Ted Kremenek93668002009-07-17 22:18:43 +0000303/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000304/// blocks for ternary operators, &&, and ||. We also process "," and
305/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000307tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000308 if (!S) {
309 badCFG = true;
310 return 0;
311 }
Ted Kremenek93668002009-07-17 22:18:43 +0000312 switch (S->getStmtClass()) {
313 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000314 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000315
316 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000317 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek93668002009-07-17 22:18:43 +0000319 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000320 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000321
Ted Kremenek93668002009-07-17 22:18:43 +0000322 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000323 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000324
Ted Kremenek93668002009-07-17 22:18:43 +0000325 case Stmt::BreakStmtClass:
326 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremenek93668002009-07-17 22:18:43 +0000328 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000329 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000330 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 case Stmt::CaseStmtClass:
333 return VisitCaseStmt(cast<CaseStmt>(S));
334
335 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000336 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 case Stmt::CompoundStmtClass:
339 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000342 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek93668002009-07-17 22:18:43 +0000344 case Stmt::ContinueStmtClass:
345 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenekb27378c2010-01-19 20:40:33 +0000347 case Stmt::CXXCatchStmtClass:
348 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
349
Ted Kremenek82bfc862010-08-28 00:19:02 +0000350 case Stmt::CXXExprWithTemporariesClass: {
351 // FIXME: Handle temporaries. For now, just visit the subexpression
352 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000353 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000354 }
355
Zhongxing Xu7e612172010-04-13 09:38:01 +0000356 case Stmt::CXXMemberCallExprClass:
357 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
358
Ted Kremenekb27378c2010-01-19 20:40:33 +0000359 case Stmt::CXXThrowExprClass:
360 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000361
Ted Kremenekb27378c2010-01-19 20:40:33 +0000362 case Stmt::CXXTryStmtClass:
363 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000364
Ted Kremenek93668002009-07-17 22:18:43 +0000365 case Stmt::DeclStmtClass:
366 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek93668002009-07-17 22:18:43 +0000368 case Stmt::DefaultStmtClass:
369 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek93668002009-07-17 22:18:43 +0000371 case Stmt::DoStmtClass:
372 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenek93668002009-07-17 22:18:43 +0000374 case Stmt::ForStmtClass:
375 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremenek93668002009-07-17 22:18:43 +0000377 case Stmt::GotoStmtClass:
378 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000379
Ted Kremenek93668002009-07-17 22:18:43 +0000380 case Stmt::IfStmtClass:
381 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremenek93668002009-07-17 22:18:43 +0000383 case Stmt::IndirectGotoStmtClass:
384 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek93668002009-07-17 22:18:43 +0000386 case Stmt::LabelStmtClass:
387 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek5868ec62010-04-11 17:02:10 +0000389 case Stmt::MemberExprClass:
390 return VisitMemberExpr(cast<MemberExpr>(S), asc);
391
Ted Kremenek93668002009-07-17 22:18:43 +0000392 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000393 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
394
Ted Kremenek93668002009-07-17 22:18:43 +0000395 case Stmt::ObjCAtSynchronizedStmtClass:
396 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek93668002009-07-17 22:18:43 +0000398 case Stmt::ObjCAtThrowStmtClass:
399 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000400
Ted Kremenek93668002009-07-17 22:18:43 +0000401 case Stmt::ObjCAtTryStmtClass:
402 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek93668002009-07-17 22:18:43 +0000404 case Stmt::ObjCForCollectionStmtClass:
405 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000406
Ted Kremenek93668002009-07-17 22:18:43 +0000407 case Stmt::ParenExprClass:
408 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000409 goto tryAgain;
410
Ted Kremenek93668002009-07-17 22:18:43 +0000411 case Stmt::NullStmtClass:
412 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenek93668002009-07-17 22:18:43 +0000414 case Stmt::ReturnStmtClass:
415 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek93668002009-07-17 22:18:43 +0000417 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000418 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000419
Ted Kremenek93668002009-07-17 22:18:43 +0000420 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000421 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000422
Ted Kremenek93668002009-07-17 22:18:43 +0000423 case Stmt::SwitchStmtClass:
424 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek93668002009-07-17 22:18:43 +0000426 case Stmt::WhileStmtClass:
427 return VisitWhileStmt(cast<WhileStmt>(S));
428 }
429}
Mike Stump11289f42009-09-09 15:08:12 +0000430
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000431CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
432 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000433 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000434 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000435 }
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek93668002009-07-17 22:18:43 +0000437 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000438}
Mike Stump31feda52009-07-17 01:31:16 +0000439
Ted Kremenek93668002009-07-17 22:18:43 +0000440/// VisitChildren - Visit the children of a Stmt.
441CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
442 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000443 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000444 E = Terminator->child_end(); I != E; ++I) {
445 if (*I) B = Visit(*I);
446 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000447 return B;
448}
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000450CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
451 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000452 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000453
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000454 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000455 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000456 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000457 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000458
Ted Kremenek9aae5132007-08-23 21:42:29 +0000459 return Block;
460}
Mike Stump11289f42009-09-09 15:08:12 +0000461
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000462CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
463 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000464 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000465 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000466 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000467
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000468 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000469 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenek93668002009-07-17 22:18:43 +0000471 // create the block evaluating the LHS
472 CFGBlock* LHSBlock = createBlock(false);
473 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek93668002009-07-17 22:18:43 +0000475 // create the block evaluating the RHS
476 Succ = ConfluenceBlock;
477 Block = NULL;
478 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000479
480 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000481 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000482 return 0;
483 }
484 else {
485 // Create an empty block for cases where the RHS doesn't require
486 // any explicit statements in the CFG.
487 RHSBlock = createBlock();
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Mike Stump773582d2009-07-23 23:25:26 +0000490 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000491 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000492 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000493 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000494
Ted Kremenek93668002009-07-17 22:18:43 +0000495 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000496 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000497 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
498 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000499 } else {
John McCalle3027922010-08-25 11:45:40 +0000500 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000501 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
502 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek93668002009-07-17 22:18:43 +0000505 // Generate the blocks for evaluating the LHS.
506 Block = LHSBlock;
507 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000508 }
John McCalle3027922010-08-25 11:45:40 +0000509 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000510 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000511 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000512 addStmt(B->getRHS());
513 return addStmt(B->getLHS());
514 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000515 else if (B->isAssignmentOp()) {
516 if (asc.alwaysAdd()) {
517 autoCreateBlock();
518 AppendStmt(Block, B, asc);
519 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000520
Ted Kremenek8abff772010-09-14 01:13:32 +0000521 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
522 // create a new block, then we should return RBlock. Otherwise
523 // we'll incorrectly return NULL.
524 CFGBlock *RBlock = Visit(B->getRHS());
525 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
526 return LBlock ? LBlock : RBlock;
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000529 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000530}
531
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000532CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
533 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000534 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000535 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000536 }
537 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000538}
539
Ted Kremenek93668002009-07-17 22:18:43 +0000540CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
541 // "break" is a control-flow statement. Thus we stop processing the current
542 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000543 if (badCFG)
544 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Ted Kremenek93668002009-07-17 22:18:43 +0000546 // Now create a new block that ends with the break statement.
547 Block = createBlock(false);
548 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000549
Ted Kremenek93668002009-07-17 22:18:43 +0000550 // If there is no target for the break, then we are looking at an incomplete
551 // AST. This means that the CFG cannot be constructed.
552 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000553 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000554 else
555 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000556
557
Ted Kremenek9aae5132007-08-23 21:42:29 +0000558 return Block;
559}
Mike Stump11289f42009-09-09 15:08:12 +0000560
Mike Stump04c68512010-01-21 15:20:48 +0000561static bool CanThrow(Expr *E) {
562 QualType Ty = E->getType();
563 if (Ty->isFunctionPointerType())
564 Ty = Ty->getAs<PointerType>()->getPointeeType();
565 else if (Ty->isBlockPointerType())
566 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000567
Mike Stump04c68512010-01-21 15:20:48 +0000568 const FunctionType *FT = Ty->getAs<FunctionType>();
569 if (FT) {
570 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
571 if (Proto->hasEmptyExceptionSpec())
572 return false;
573 }
574 return true;
575}
576
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000577CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000578 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000579 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000580 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000581 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000582 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000583
Mike Stump04c68512010-01-21 15:20:48 +0000584 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000585
586 // Languages without exceptions are assumed to not throw.
587 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000588 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +0000589 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000590 }
591
592 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000593 if (FD->hasAttr<NoReturnAttr>())
594 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000595 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000596 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000597 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000598
Mike Stump04c68512010-01-21 15:20:48 +0000599 if (!CanThrow(C->getCallee()))
600 AddEHEdge = false;
601
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000602 if (!NoReturn && !AddEHEdge) {
603 if (asc.asLValue())
604 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
605 else
606 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Mike Stump92244b02010-01-19 22:00:14 +0000609 if (Block) {
610 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000611 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +0000612 return 0;
613 }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Mike Stump92244b02010-01-19 22:00:14 +0000615 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000616 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000617
Mike Stump92244b02010-01-19 22:00:14 +0000618 if (NoReturn) {
619 // Wire this to the exit block directly.
620 AddSuccessor(Block, &cfg->getExit());
621 }
Mike Stump04c68512010-01-21 15:20:48 +0000622 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000623 // Add exceptional edges.
624 if (TryTerminatedBlock)
625 AddSuccessor(Block, TryTerminatedBlock);
626 else
627 AddSuccessor(Block, &cfg->getExit());
628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Mike Stump8c5d7992009-07-25 21:26:53 +0000630 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000631}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000632
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000633CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
634 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000635 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000636 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000637 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000638 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Ted Kremenek5868ec62010-04-11 17:02:10 +0000640 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
641 : AddStmtChoice::AlwaysAdd;
642
Ted Kremenek21822592009-07-17 18:20:32 +0000643 Succ = ConfluenceBlock;
644 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000645 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000646 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000647 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000648
Ted Kremenek21822592009-07-17 18:20:32 +0000649 Succ = ConfluenceBlock;
650 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000651 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000652 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000653 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000654
Ted Kremenek21822592009-07-17 18:20:32 +0000655 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000656 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000657 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000658 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
659 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000660 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000661 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000662}
Mike Stump11289f42009-09-09 15:08:12 +0000663
664
665CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
666 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000667
668 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
669 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000670 // If we hit a segment of code just containing ';' (NullStmts), we can
671 // get a null block back. In such cases, just use the LastBlock
672 if (CFGBlock *newBlock = addStmt(*I))
673 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Ted Kremenekce499c22009-08-27 23:16:26 +0000675 if (badCFG)
676 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000677 }
Mike Stump92244b02010-01-19 22:00:14 +0000678
Ted Kremenek93668002009-07-17 22:18:43 +0000679 return LastBlock;
680}
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000682CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
683 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000684 // Create the confluence block that will "merge" the results of the ternary
685 // expression.
686 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000687 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000688 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000689 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000690
Ted Kremenek5868ec62010-04-11 17:02:10 +0000691 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
692 : AddStmtChoice::AlwaysAdd;
693
Ted Kremenek51d40b02009-07-17 18:15:54 +0000694 // Create a block for the LHS expression if there is an LHS expression. A
695 // GCC extension allows LHS to be NULL, causing the condition to be the
696 // value that is returned instead.
697 // e.g: x ?: y is shorthand for: x ? x : y;
698 Succ = ConfluenceBlock;
699 Block = NULL;
700 CFGBlock* LHSBlock = NULL;
701 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000702 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000703 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000704 return 0;
705 Block = NULL;
706 }
Mike Stump11289f42009-09-09 15:08:12 +0000707
Ted Kremenek51d40b02009-07-17 18:15:54 +0000708 // Create the block for the RHS expression.
709 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000710 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000711 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000712 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000713
Ted Kremenek51d40b02009-07-17 18:15:54 +0000714 // Create the block that will contain the condition.
715 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000716
Mike Stump773582d2009-07-23 23:25:26 +0000717 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000718 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000719 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000720 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000721 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000722 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000723 // If we know the condition is false, add NULL as the successor for
724 // the block containing the condition. In this case, the confluence
725 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000726 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000727 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000728 } else {
729 // If we have no LHS expression, add the ConfluenceBlock as a direct
730 // successor for the block containing the condition. Moreover, we need to
731 // reverse the order of the predecessors in the ConfluenceBlock because
732 // the RHSBlock will have been added to the succcessors already, and we
733 // want the first predecessor to the the block containing the expression
734 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000735 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000736 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000737 std::reverse(ConfluenceBlock->pred_begin(),
738 ConfluenceBlock->pred_end());
739 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000742 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000743 Block->setTerminator(C);
744 return addStmt(C->getCond());
745}
746
Ted Kremenek93668002009-07-17 22:18:43 +0000747CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
748 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000749
Ted Kremenek93668002009-07-17 22:18:43 +0000750 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000751 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000752 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Ted Kremenek93668002009-07-17 22:18:43 +0000755 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Ted Kremenek93668002009-07-17 22:18:43 +0000757 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
758 typedef llvm::SmallVector<Decl*,10> BufTy;
759 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000760
Ted Kremenek93668002009-07-17 22:18:43 +0000761 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
762 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
763 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
764 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000765
Ted Kremenek93668002009-07-17 22:18:43 +0000766 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
767 // automatically freed with the CFG.
768 DeclGroupRef DG(*I);
769 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000770 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000771 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000772
Ted Kremenek93668002009-07-17 22:18:43 +0000773 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000774 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000775 B = VisitDeclSubExpr(D);
776 }
Mike Stump11289f42009-09-09 15:08:12 +0000777
778 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000779}
Mike Stump11289f42009-09-09 15:08:12 +0000780
Ted Kremenek93668002009-07-17 22:18:43 +0000781/// VisitDeclSubExpr - Utility method to add block-level expressions for
782/// initializers in Decls.
783CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
784 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000785
Ted Kremenek93668002009-07-17 22:18:43 +0000786 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000787
Ted Kremenek93668002009-07-17 22:18:43 +0000788 if (!VD)
789 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000792
Ted Kremenek93668002009-07-17 22:18:43 +0000793 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000794 AddStmtChoice::Kind k =
795 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
796 : AddStmtChoice::NotAlwaysAdd;
797 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000798 }
Mike Stump11289f42009-09-09 15:08:12 +0000799
Ted Kremenek93668002009-07-17 22:18:43 +0000800 // If the type of VD is a VLA, then we must process its size expressions.
801 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
802 VA = FindVA(VA->getElementType().getTypePtr()))
803 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000804
Ted Kremenek93668002009-07-17 22:18:43 +0000805 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000806}
807
808CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000809 // We may see an if statement in the middle of a basic block, or it may be the
810 // first statement we are processing. In either case, we create a new basic
811 // block. First, we create the blocks for the then...else statements, and
812 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000813 // middle of a block, we stop processing that block. That block is then the
814 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000815
816 // The block we were proccessing is now finished. Make it the successor
817 // block.
818 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000819 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000820 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000821 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000822 }
Mike Stump31feda52009-07-17 01:31:16 +0000823
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000824 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000826
Ted Kremenek9aae5132007-08-23 21:42:29 +0000827 if (Stmt* Else = I->getElse()) {
828 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000829
Ted Kremenek9aae5132007-08-23 21:42:29 +0000830 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000831 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000833 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000834
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000835 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
836 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000837 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000838 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000839 return 0;
840 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 }
Mike Stump31feda52009-07-17 01:31:16 +0000842
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000843 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000844 CFGBlock* ThenBlock;
845 {
846 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000847 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000848 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000849 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000850 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000851
Ted Kremenek1b379512009-04-01 03:52:47 +0000852 if (!ThenBlock) {
853 // We can reach here if the "then" body has all NullStmts.
854 // Create an empty block so we can distinguish between true and false
855 // branches in path-sensitive analyses.
856 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000857 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000858 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000859 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000860 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000861 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000862 }
863
Mike Stump31feda52009-07-17 01:31:16 +0000864 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000865 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000866
Ted Kremenek9aae5132007-08-23 21:42:29 +0000867 // Set the terminator of the new block to the If statement.
868 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000869
Mike Stump773582d2009-07-23 23:25:26 +0000870 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000871 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000872
Ted Kremenek9aae5132007-08-23 21:42:29 +0000873 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000874 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
875 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000876
877 // Add the condition as the last statement in the new block. This may create
878 // new blocks as the condition may contain control-flow. Any newly created
879 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000880 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000881
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000882 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
883 // and the condition variable initialization to the CFG.
884 if (VarDecl *VD = I->getConditionVariable()) {
885 if (Expr *Init = VD->getInit()) {
886 autoCreateBlock();
887 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
888 addStmt(Init);
889 }
890 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000891
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000892 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000893}
Mike Stump31feda52009-07-17 01:31:16 +0000894
895
Ted Kremenek9aae5132007-08-23 21:42:29 +0000896CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000897 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000898 //
Mike Stump31feda52009-07-17 01:31:16 +0000899 // NOTE: If a "return" appears in the middle of a block, this means that the
900 // code afterwards is DEAD (unreachable). We still keep a basic block
901 // for that code; a simple "mark-and-sweep" from the entry block will be
902 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000903
904 // Create the new block.
905 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000906
Ted Kremenek9aae5132007-08-23 21:42:29 +0000907 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000908 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000909
910 // Add the return statement to the block. This may create new blocks if R
911 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000912 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000913}
914
915CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
916 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000917 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000918 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000919
Ted Kremenek93668002009-07-17 22:18:43 +0000920 if (!LabelBlock) // This can happen when the body is empty, i.e.
921 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000922
Ted Kremenek93668002009-07-17 22:18:43 +0000923 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000924 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000925
926 // Labels partition blocks, so this is the end of the basic block we were
927 // processing (L is the block's label). Because this is label (and we have
928 // already processed the substatement) there is no extra control-flow to worry
929 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000930 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000931 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000932 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000933
934 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000935 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000936
Ted Kremenek9aae5132007-08-23 21:42:29 +0000937 // This block is now the implicit successor of other blocks.
938 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000939
Ted Kremenek9aae5132007-08-23 21:42:29 +0000940 return LabelBlock;
941}
942
943CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000944 // Goto is a control-flow statement. Thus we stop processing the current
945 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000946
Ted Kremenek9aae5132007-08-23 21:42:29 +0000947 Block = createBlock(false);
948 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000949
950 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000951 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000952
Ted Kremenek9aae5132007-08-23 21:42:29 +0000953 if (I == LabelMap.end())
954 // We will need to backpatch this block later.
955 BackpatchBlocks.push_back(Block);
956 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000957 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000958
Mike Stump31feda52009-07-17 01:31:16 +0000959 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000960}
961
962CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000963 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000964
Mike Stump014b3ea2009-07-21 01:12:51 +0000965 // "for" is a control-flow statement. Thus we stop processing the current
966 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000967 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000968 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000969 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000970 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000971 } else
972 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000973
Ted Kremenek304a9532010-05-21 20:30:15 +0000974 // Save the current value for the break targets.
975 // All breaks should go to the code following the loop.
976 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
977 BreakTargetBlock = LoopSuccessor;
978
Mike Stump31feda52009-07-17 01:31:16 +0000979 // Because of short-circuit evaluation, the condition of the loop can span
980 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
981 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000982 CFGBlock* ExitConditionBlock = createBlock(false);
983 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000984
Ted Kremenek81e14852007-08-27 19:46:09 +0000985 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000986 ExitConditionBlock->setTerminator(F);
987
988 // Now add the actual condition to the condition block. Because the condition
989 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000990 if (Stmt* C = F->getCond()) {
991 Block = ExitConditionBlock;
992 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +0000993 assert(Block == EntryConditionBlock ||
994 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +0000995
996 // If this block contains a condition variable, add both the condition
997 // variable and initializer to the CFG.
998 if (VarDecl *VD = F->getConditionVariable()) {
999 if (Expr *Init = VD->getInit()) {
1000 autoCreateBlock();
1001 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1002 EntryConditionBlock = addStmt(Init);
1003 assert(Block == EntryConditionBlock);
1004 }
1005 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001006
Ted Kremenek55957a82009-05-02 00:13:27 +00001007 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001008 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001009 return 0;
1010 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001011 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001012
Mike Stump31feda52009-07-17 01:31:16 +00001013 // The condition block is the implicit successor for the loop body as well as
1014 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001015 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001016
Mike Stump773582d2009-07-23 23:25:26 +00001017 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001018 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001019
Mike Stump773582d2009-07-23 23:25:26 +00001020 if (F->getCond())
1021 KnownVal = TryEvaluateBool(F->getCond());
1022
Ted Kremenek9aae5132007-08-23 21:42:29 +00001023 // Now create the loop body.
1024 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001025 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001026
Ted Kremenek304a9532010-05-21 20:30:15 +00001027 // Save the current values for Block, Succ, and continue targets.
1028 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1029 save_continue(ContinueTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001030
Ted Kremeneke9610502007-08-30 18:39:40 +00001031 // Create a new block to contain the (bottom) of the loop body.
1032 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001034 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001035 // Generate increment code in its own basic block. This is the target of
1036 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001037 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001038 } else {
1039 // No increment code. Create a special, empty, block that is used as the
1040 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001041 assert(Succ == EntryConditionBlock);
1042 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001043 }
Mike Stump31feda52009-07-17 01:31:16 +00001044
Ted Kremenek902393b2009-04-28 00:51:56 +00001045 // Finish up the increment (or empty) block if it hasn't been already.
1046 if (Block) {
1047 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001048 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001049 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001050 Block = 0;
1051 }
Mike Stump31feda52009-07-17 01:31:16 +00001052
Ted Kremenek902393b2009-04-28 00:51:56 +00001053 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001054
Ted Kremenek902393b2009-04-28 00:51:56 +00001055 // The starting block for the loop increment is the block that should
1056 // represent the 'loop target' for looping back to the start of the loop.
1057 ContinueTargetBlock->setLoopTarget(F);
1058
Mike Stump31feda52009-07-17 01:31:16 +00001059 // Now populate the body block, and in the process create new blocks as we
1060 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001061 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001062
1063 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001064 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001065 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001066 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001067
Ted Kremenek30754282009-07-24 04:47:11 +00001068 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001069 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001070 }
Mike Stump31feda52009-07-17 01:31:16 +00001071
Ted Kremenek30754282009-07-24 04:47:11 +00001072 // Link up the condition block with the code that follows the loop. (the
1073 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001074 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001075
Ted Kremenek9aae5132007-08-23 21:42:29 +00001076 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001077 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001078 if (Stmt* I = F->getInit()) {
1079 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001080 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001081 } else {
1082 // There is no loop initialization. We are thus basically a while loop.
1083 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001084 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001085 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001086 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001087 }
1088}
1089
Ted Kremenek5868ec62010-04-11 17:02:10 +00001090CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1091 if (asc.alwaysAdd()) {
1092 autoCreateBlock();
1093 AppendStmt(Block, M, asc);
1094 }
1095 return Visit(M->getBase(),
1096 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1097 : AddStmtChoice::AsLValueNotAlwaysAdd);
1098}
1099
Ted Kremenek9d56e642008-11-11 17:10:00 +00001100CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1101 // Objective-C fast enumeration 'for' statements:
1102 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1103 //
1104 // for ( Type newVariable in collection_expression ) { statements }
1105 //
1106 // becomes:
1107 //
1108 // prologue:
1109 // 1. collection_expression
1110 // T. jump to loop_entry
1111 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001112 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001113 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1114 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1115 // TB:
1116 // statements
1117 // T. jump to loop_entry
1118 // FB:
1119 // what comes after
1120 //
1121 // and
1122 //
1123 // Type existingItem;
1124 // for ( existingItem in expression ) { statements }
1125 //
1126 // becomes:
1127 //
Mike Stump31feda52009-07-17 01:31:16 +00001128 // the same with newVariable replaced with existingItem; the binding works
1129 // the same except that for one ObjCForCollectionStmt::getElement() returns
1130 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001131 //
Mike Stump31feda52009-07-17 01:31:16 +00001132
Ted Kremenek9d56e642008-11-11 17:10:00 +00001133 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001134
Ted Kremenek9d56e642008-11-11 17:10:00 +00001135 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001136 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001137 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001138 LoopSuccessor = Block;
1139 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001140 } else
1141 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001142
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001143 // Build the condition blocks.
1144 CFGBlock* ExitConditionBlock = createBlock(false);
1145 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001147 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001148 ExitConditionBlock->setTerminator(S);
1149
1150 // The last statement in the block should be the ObjCForCollectionStmt, which
1151 // performs the actual binding to 'element' and determines if there are any
1152 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001153 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001154 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001155
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001156 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001157 // generate new blocks as necesary. We DON'T add the statement by default to
1158 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001159 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001160 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001161 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001162 return 0;
1163 Block = 0;
1164 }
Mike Stump31feda52009-07-17 01:31:16 +00001165
1166 // The condition block is the implicit successor for the loop body as well as
1167 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001168 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001169
Ted Kremenek9d56e642008-11-11 17:10:00 +00001170 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001171 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001172 // Save the current values for Succ, continue and break targets.
1173 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001174 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1175
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001176 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001177 ContinueTargetBlock = EntryConditionBlock;
1178
Ted Kremenek93668002009-07-17 22:18:43 +00001179 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001180
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001181 if (!BodyBlock)
1182 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001183 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001184 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001185 return 0;
1186 }
Mike Stump31feda52009-07-17 01:31:16 +00001187
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001188 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001189 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001190 }
Mike Stump31feda52009-07-17 01:31:16 +00001191
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001192 // Link up the condition block with the code that follows the loop.
1193 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001194 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001195
Ted Kremenek9d56e642008-11-11 17:10:00 +00001196 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001197 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001198 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001199}
1200
Ted Kremenek49805452009-05-02 01:49:13 +00001201CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1202 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001203
Ted Kremenek49805452009-05-02 01:49:13 +00001204 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001205 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001206
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001207 // The sync body starts its own basic block. This makes it a little easier
1208 // for diagnostic clients.
1209 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001210 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001211 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001212
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001213 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001214 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001215 }
Mike Stump31feda52009-07-17 01:31:16 +00001216
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001217 // Add the @synchronized to the CFG.
1218 autoCreateBlock();
1219 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1220
Ted Kremenek49805452009-05-02 01:49:13 +00001221 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001222 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001223}
Mike Stump31feda52009-07-17 01:31:16 +00001224
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001225CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001226 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001227 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001228}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001229
Ted Kremenek9aae5132007-08-23 21:42:29 +00001230CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001231 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001232
Mike Stump014b3ea2009-07-21 01:12:51 +00001233 // "while" is a control-flow statement. Thus we stop processing the current
1234 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001235 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001236 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001237 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001238 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001239 } else
1240 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001241
1242 // Because of short-circuit evaluation, the condition of the loop can span
1243 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1244 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001245 CFGBlock* ExitConditionBlock = createBlock(false);
1246 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001247
Ted Kremenek81e14852007-08-27 19:46:09 +00001248 // Set the terminator for the "exit" condition block.
1249 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001250
1251 // Now add the actual condition to the condition block. Because the condition
1252 // itself may contain control-flow, new blocks may be created. Thus we update
1253 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001254 if (Stmt* C = W->getCond()) {
1255 Block = ExitConditionBlock;
1256 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001257 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001258
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001259 // If this block contains a condition variable, add both the condition
1260 // variable and initializer to the CFG.
1261 if (VarDecl *VD = W->getConditionVariable()) {
1262 if (Expr *Init = VD->getInit()) {
1263 autoCreateBlock();
1264 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1265 EntryConditionBlock = addStmt(Init);
1266 assert(Block == EntryConditionBlock);
1267 }
1268 }
1269
Ted Kremenek55957a82009-05-02 00:13:27 +00001270 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001271 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001272 return 0;
1273 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001274 }
Mike Stump31feda52009-07-17 01:31:16 +00001275
1276 // The condition block is the implicit successor for the loop body as well as
1277 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001278 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001279
Mike Stump773582d2009-07-23 23:25:26 +00001280 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001281 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001282
Ted Kremenek9aae5132007-08-23 21:42:29 +00001283 // Process the loop body.
1284 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001285 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286
1287 // Save the current values for Block, Succ, and continue and break targets
1288 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1289 save_continue(ContinueTargetBlock),
1290 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001291
Mike Stump31feda52009-07-17 01:31:16 +00001292 // Create an empty block to represent the transition block for looping back
1293 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001294 Block = 0;
1295 assert(Succ == EntryConditionBlock);
1296 Succ = createBlock();
1297 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001298 ContinueTargetBlock = Succ;
1299
Ted Kremenek9aae5132007-08-23 21:42:29 +00001300 // All breaks should go to the code following the loop.
1301 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001302
Ted Kremenek9aae5132007-08-23 21:42:29 +00001303 // NULL out Block to force lazy instantiation of blocks for the body.
1304 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001305
Ted Kremenek9aae5132007-08-23 21:42:29 +00001306 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001307 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001308
Ted Kremeneke9610502007-08-30 18:39:40 +00001309 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001310 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001311 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001312 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001313 return 0;
1314 }
Mike Stump31feda52009-07-17 01:31:16 +00001315
Ted Kremenek30754282009-07-24 04:47:11 +00001316 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001317 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001318 }
Mike Stump31feda52009-07-17 01:31:16 +00001319
Ted Kremenek30754282009-07-24 04:47:11 +00001320 // Link up the condition block with the code that follows the loop. (the
1321 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001322 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001323
1324 // There can be no more statements in the condition block since we loop back
1325 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001326 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001327
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001328 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001329 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001330 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001331}
Mike Stump11289f42009-09-09 15:08:12 +00001332
1333
Ted Kremenek93668002009-07-17 22:18:43 +00001334CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1335 // FIXME: For now we pretend that @catch and the code it contains does not
1336 // exit.
1337 return Block;
1338}
Mike Stump31feda52009-07-17 01:31:16 +00001339
Ted Kremenek93041ba2008-12-09 20:20:09 +00001340CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1341 // FIXME: This isn't complete. We basically treat @throw like a return
1342 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001343
Ted Kremenek0868eea2009-09-24 18:45:41 +00001344 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001345 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001346 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001347
Ted Kremenek93041ba2008-12-09 20:20:09 +00001348 // Create the new block.
1349 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001350
Ted Kremenek93041ba2008-12-09 20:20:09 +00001351 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001352 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001353
1354 // Add the statement to the block. This may create new blocks if S contains
1355 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001356 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001357}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001358
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001359CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001360 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001361 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001362 return 0;
1363
1364 // Create the new block.
1365 Block = createBlock(false);
1366
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001367 if (TryTerminatedBlock)
1368 // The current try statement is the only successor.
1369 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001370 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001371 // otherwise the Exit block is the only successor.
1372 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001373
1374 // Add the statement to the block. This may create new blocks if S contains
1375 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001376 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001377}
1378
Ted Kremenek93668002009-07-17 22:18:43 +00001379CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001381
Mike Stump8d50b6a2009-07-21 01:27:50 +00001382 // "do...while" is a control-flow statement. Thus we stop processing the
1383 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001384 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001385 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001386 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001387 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001388 } else
1389 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001390
1391 // Because of short-circuit evaluation, the condition of the loop can span
1392 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1393 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001394 CFGBlock* ExitConditionBlock = createBlock(false);
1395 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001396
Ted Kremenek81e14852007-08-27 19:46:09 +00001397 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001398 ExitConditionBlock->setTerminator(D);
1399
1400 // Now add the actual condition to the condition block. Because the condition
1401 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001402 if (Stmt* C = D->getCond()) {
1403 Block = ExitConditionBlock;
1404 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001405 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001406 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001407 return 0;
1408 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001409 }
Mike Stump31feda52009-07-17 01:31:16 +00001410
Ted Kremeneka1523a32008-02-27 07:20:00 +00001411 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001412 Succ = EntryConditionBlock;
1413
Mike Stump773582d2009-07-23 23:25:26 +00001414 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001415 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001416
Ted Kremenek9aae5132007-08-23 21:42:29 +00001417 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001418 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001419 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001420 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001421
Ted Kremenek9aae5132007-08-23 21:42:29 +00001422 // Save the current values for Block, Succ, and continue and break targets
1423 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001424 save_continue(ContinueTargetBlock),
1425 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001426
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001428 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenek9aae5132007-08-23 21:42:29 +00001430 // All breaks should go to the code following the loop.
1431 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001432
Ted Kremenek9aae5132007-08-23 21:42:29 +00001433 // NULL out Block to force lazy instantiation of blocks for the body.
1434 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001435
Ted Kremenek9aae5132007-08-23 21:42:29 +00001436 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001437 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001438
Ted Kremeneke9610502007-08-30 18:39:40 +00001439 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001440 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001441 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001442 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001443 return 0;
1444 }
Mike Stump31feda52009-07-17 01:31:16 +00001445
Ted Kremenek110974d2010-08-17 20:59:56 +00001446 if (!KnownVal.isFalse()) {
1447 // Add an intermediate block between the BodyBlock and the
1448 // ExitConditionBlock to represent the "loop back" transition. Create an
1449 // empty block to represent the transition block for looping back to the
1450 // head of the loop.
1451 // FIXME: Can we do this more efficiently without adding another block?
1452 Block = NULL;
1453 Succ = BodyBlock;
1454 CFGBlock *LoopBackBlock = createBlock();
1455 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001456
Ted Kremenek110974d2010-08-17 20:59:56 +00001457 // Add the loop body entry as a successor to the condition.
1458 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1459 }
1460 else
1461 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001462 }
Mike Stump31feda52009-07-17 01:31:16 +00001463
Ted Kremenek30754282009-07-24 04:47:11 +00001464 // Link up the condition block with the code that follows the loop.
1465 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001466 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001467
1468 // There can be no more statements in the body block(s) since we loop back to
1469 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001470 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001471
Ted Kremenek9aae5132007-08-23 21:42:29 +00001472 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001473 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001474 return BodyBlock;
1475}
1476
1477CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1478 // "continue" is a control-flow statement. Thus we stop processing the
1479 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001480 if (badCFG)
1481 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001482
Ted Kremenek9aae5132007-08-23 21:42:29 +00001483 // Now create a new block that ends with the continue statement.
1484 Block = createBlock(false);
1485 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001486
Ted Kremenek9aae5132007-08-23 21:42:29 +00001487 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001488 // incomplete AST. This means the CFG cannot be constructed.
1489 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001490 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001491 else
1492 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001493
Ted Kremenek9aae5132007-08-23 21:42:29 +00001494 return Block;
1495}
Mike Stump11289f42009-09-09 15:08:12 +00001496
Ted Kremenek0747de62009-07-18 00:47:21 +00001497CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001498 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001499
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001500 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001501 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001502 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001503 }
Mike Stump11289f42009-09-09 15:08:12 +00001504
Ted Kremenek93668002009-07-17 22:18:43 +00001505 // VLA types have expressions that must be evaluated.
1506 if (E->isArgumentType()) {
1507 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1508 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1509 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001510 }
Mike Stump11289f42009-09-09 15:08:12 +00001511
Mike Stump31feda52009-07-17 01:31:16 +00001512 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001513}
Mike Stump11289f42009-09-09 15:08:12 +00001514
Ted Kremenek93668002009-07-17 22:18:43 +00001515/// VisitStmtExpr - Utility method to handle (nested) statement
1516/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001517CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1518 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001519 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001520 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001521 }
Ted Kremenek93668002009-07-17 22:18:43 +00001522 return VisitCompoundStmt(SE->getSubStmt());
1523}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001524
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001525CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001526 // "switch" is a control-flow statement. Thus we stop processing the current
1527 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001528 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001529
Ted Kremenek9aae5132007-08-23 21:42:29 +00001530 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001531 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001532 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001533 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001534 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001535
1536 // Save the current "switch" context.
1537 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001538 save_break(BreakTargetBlock),
1539 save_default(DefaultCaseBlock);
1540
Mike Stump31feda52009-07-17 01:31:16 +00001541 // Set the "default" case to be the block after the switch statement. If the
1542 // switch statement contains a "default:", this value will be overwritten with
1543 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001544 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001545
Ted Kremenek9aae5132007-08-23 21:42:29 +00001546 // Create a new block that will contain the switch statement.
1547 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001548
Ted Kremenek9aae5132007-08-23 21:42:29 +00001549 // Now process the switch body. The code after the switch is the implicit
1550 // successor.
1551 Succ = SwitchSuccessor;
1552 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001553
1554 // When visiting the body, the case statements should automatically get linked
1555 // up to the switch. We also don't keep a pointer to the body, since all
1556 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001557 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001558 Block = NULL;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001559 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001560 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001561 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001562 return 0;
1563 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001564
Mike Stump31feda52009-07-17 01:31:16 +00001565 // If we have no "default:" case, the default transition is to the code
1566 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001567 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001568
Ted Kremenek81e14852007-08-27 19:46:09 +00001569 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001570 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001571 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001572 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001573 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001574
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001575 // Finally, if the SwitchStmt contains a condition variable, add both the
1576 // SwitchStmt and the condition variable initialization to the CFG.
1577 if (VarDecl *VD = Terminator->getConditionVariable()) {
1578 if (Expr *Init = VD->getInit()) {
1579 autoCreateBlock();
1580 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1581 addStmt(Init);
1582 }
1583 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001584
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001585 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001586}
1587
Ted Kremenek93668002009-07-17 22:18:43 +00001588CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001589 // CaseStmts are essentially labels, so they are the first statement in a
1590 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001591 CFGBlock *TopBlock = 0, *LastBlock = 0;
1592
1593 if (Stmt *Sub = CS->getSubStmt()) {
1594 // For deeply nested chains of CaseStmts, instead of doing a recursion
1595 // (which can blow out the stack), manually unroll and create blocks
1596 // along the way.
1597 while (isa<CaseStmt>(Sub)) {
1598 CFGBlock *CurrentBlock = createBlock(false);
1599 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001600
Ted Kremenek60fa6572010-08-04 23:54:30 +00001601 if (TopBlock)
1602 AddSuccessor(LastBlock, CurrentBlock);
1603 else
1604 TopBlock = CurrentBlock;
1605
1606 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1607 LastBlock = CurrentBlock;
1608
1609 CS = cast<CaseStmt>(Sub);
1610 Sub = CS->getSubStmt();
1611 }
1612
1613 addStmt(Sub);
1614 }
Mike Stump11289f42009-09-09 15:08:12 +00001615
Ted Kremenek55e91e82007-08-30 18:48:11 +00001616 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001617 if (!CaseBlock)
1618 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001619
1620 // Cases statements partition blocks, so this is the top of the basic block we
1621 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001622 CaseBlock->setLabel(CS);
1623
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001624 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001625 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001626
1627 // Add this block to the list of successors for the block with the switch
1628 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001629 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001630 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001631
Ted Kremenek9aae5132007-08-23 21:42:29 +00001632 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1633 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001634
Ted Kremenek60fa6572010-08-04 23:54:30 +00001635 if (TopBlock) {
1636 AddSuccessor(LastBlock, CaseBlock);
1637 Succ = TopBlock;
1638 }
1639 else {
1640 // This block is now the implicit successor of other blocks.
1641 Succ = CaseBlock;
1642 }
Mike Stump31feda52009-07-17 01:31:16 +00001643
Ted Kremenek60fa6572010-08-04 23:54:30 +00001644 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001645}
Mike Stump31feda52009-07-17 01:31:16 +00001646
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001647CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001648 if (Terminator->getSubStmt())
1649 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001650
Ted Kremenek654c78f2008-02-13 22:05:39 +00001651 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001652
1653 if (!DefaultCaseBlock)
1654 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001655
1656 // Default statements partition blocks, so this is the top of the basic block
1657 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001658 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001659
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001660 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001661 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001662
Mike Stump31feda52009-07-17 01:31:16 +00001663 // Unlike case statements, we don't add the default block to the successors
1664 // for the switch statement immediately. This is done when we finish
1665 // processing the switch statement. This allows for the default case
1666 // (including a fall-through to the code after the switch statement) to always
1667 // be the last successor of a switch-terminated block.
1668
Ted Kremenek654c78f2008-02-13 22:05:39 +00001669 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1670 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001671
Ted Kremenek654c78f2008-02-13 22:05:39 +00001672 // This block is now the implicit successor of other blocks.
1673 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001674
1675 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001676}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001677
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001678CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1679 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1680 // current block.
1681 CFGBlock* TrySuccessor = NULL;
1682
1683 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001684 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001685 return 0;
1686 TrySuccessor = Block;
1687 } else TrySuccessor = Succ;
1688
Mike Stump0bdba6c2010-01-20 01:15:34 +00001689 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001690
1691 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001692 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001693 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001694 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001695
Mike Stump0bdba6c2010-01-20 01:15:34 +00001696 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001697 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1698 // The code after the try is the implicit successor.
1699 Succ = TrySuccessor;
1700 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001701 if (CS->getExceptionDecl() == 0) {
1702 HasCatchAll = true;
1703 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001704 Block = NULL;
1705 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1706 if (CatchBlock == 0)
1707 return 0;
1708 // Add this block to the list of successors for the block with the try
1709 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001710 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001711 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001712 if (!HasCatchAll) {
1713 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001714 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001715 else
Mike Stump845384a2010-01-20 01:30:58 +00001716 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001717 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001718
1719 // The code after the try is the implicit successor.
1720 Succ = TrySuccessor;
1721
Mike Stump845384a2010-01-20 01:30:58 +00001722 // Save the current "try" context.
1723 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1724 TryTerminatedBlock = NewTryTerminatedBlock;
1725
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001726 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001727 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001728 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001729 return Block;
1730}
1731
1732CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1733 // CXXCatchStmt are treated like labels, so they are the first statement in a
1734 // block.
1735
1736 if (CS->getHandlerBlock())
1737 addStmt(CS->getHandlerBlock());
1738
1739 CFGBlock* CatchBlock = Block;
1740 if (!CatchBlock)
1741 CatchBlock = createBlock();
1742
1743 CatchBlock->setLabel(CS);
1744
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001745 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001746 return 0;
1747
1748 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1749 Block = NULL;
1750
1751 return CatchBlock;
1752}
1753
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001754CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001755 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001756 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001757 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001758 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001759 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001760 return VisitChildren(C);
1761}
1762
Ted Kremenekeda180e22007-08-28 19:26:49 +00001763CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001764 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001765 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenekeda180e22007-08-28 19:26:49 +00001767 if (!IBlock) {
1768 IBlock = createBlock(false);
1769 cfg->setIndirectGotoBlock(IBlock);
1770 }
Mike Stump31feda52009-07-17 01:31:16 +00001771
Ted Kremenekeda180e22007-08-28 19:26:49 +00001772 // IndirectGoto is a control-flow statement. Thus we stop processing the
1773 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001774 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001775 return 0;
1776
Ted Kremenekeda180e22007-08-28 19:26:49 +00001777 Block = createBlock(false);
1778 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001779 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001780 return addStmt(I->getTarget());
1781}
1782
Ted Kremenek04cca642007-08-23 21:26:19 +00001783} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001784
Mike Stump31feda52009-07-17 01:31:16 +00001785/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1786/// no successors or predecessors. If this is the first block created in the
1787/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001788CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001789 bool first_block = begin() == end();
1790
1791 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001792 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1793 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1794 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001795
1796 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001797 if (first_block)
1798 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001799
1800 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001801 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001802}
1803
Ted Kremenek889073f2007-08-23 16:51:22 +00001804/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1805/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001806CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001807 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001808 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001809 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00001810}
1811
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001812//===----------------------------------------------------------------------===//
1813// CFG: Queries for BlkExprs.
1814//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001815
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001816namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001817 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001818}
1819
Ted Kremenekbff98442009-12-23 23:37:10 +00001820static void FindSubExprAssignments(Stmt *S,
1821 llvm::SmallPtrSet<Expr*,50>& Set) {
1822 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001823 return;
Mike Stump31feda52009-07-17 01:31:16 +00001824
Ted Kremenekbff98442009-12-23 23:37:10 +00001825 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001826 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001827 if (!child)
1828 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001829
Ted Kremenekbff98442009-12-23 23:37:10 +00001830 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001831 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001832
Ted Kremenekbff98442009-12-23 23:37:10 +00001833 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001834 }
1835}
1836
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001837static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1838 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001839
1840 // Look for assignments that are used as subexpressions. These are the only
1841 // assignments that we want to *possibly* register as a block-level
1842 // expression. Basically, if an assignment occurs both in a subexpression and
1843 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001844 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001845
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001846 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001847 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00001848 if (CFGStmt S = BI->getAs<CFGStmt>())
1849 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001850
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001851 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001852
1853 // Iterate over the statements again on identify the Expr* and Stmt* at the
1854 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001855
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00001856 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
1857 CFGStmt CS = BI->getAs<CFGStmt>();
1858 if (!CS.isValid())
1859 continue;
1860 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00001861
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001862 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001863 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001864 // expression are really "statements" whose value is never used by
1865 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001866 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001867 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001868 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1869 // Special handling for statement expressions. The last statement in
1870 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001871 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001872 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001873 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001874 (*M)[C->body_back()] = x;
1875 }
1876 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001877
Ted Kremenek95a123c2008-01-26 00:03:27 +00001878 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001879 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001880 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00001881 }
Mike Stump31feda52009-07-17 01:31:16 +00001882
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001883 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001885 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001887 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001888 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001889 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001890 }
1891 }
Mike Stump31feda52009-07-17 01:31:16 +00001892
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001893 return M;
1894}
1895
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001896CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1897 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001898 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001899
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001900 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001901 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001902 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001903}
1904
1905unsigned CFG::getNumBlkExprs() {
1906 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1907 return M->size();
1908 else {
1909 // We assume callers interested in the number of BlkExprs will want
1910 // the map constructed if it doesn't already exist.
1911 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1912 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1913 }
1914}
1915
Ted Kremenek6065ef62008-04-28 18:00:46 +00001916//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00001917// Filtered walking of the CFG.
1918//===----------------------------------------------------------------------===//
1919
1920bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00001921 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00001922
1923 if (F.IgnoreDefaultsWithCoveredEnums) {
1924 // If the 'To' has no label or is labeled but the label isn't a
1925 // CaseStmt then filter this edge.
1926 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00001927 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00001928 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00001929 const Stmt *L = To->getLabel();
1930 if (!L || !isa<CaseStmt>(L))
1931 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00001932 }
1933 }
1934 }
1935
1936 return false;
1937}
1938
1939//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001940// Cleanup: CFG dstor.
1941//===----------------------------------------------------------------------===//
1942
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001943CFG::~CFG() {
1944 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1945}
Mike Stump31feda52009-07-17 01:31:16 +00001946
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001947//===----------------------------------------------------------------------===//
1948// CFG pretty printing
1949//===----------------------------------------------------------------------===//
1950
Ted Kremenek7e776b12007-08-22 18:22:34 +00001951namespace {
1952
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001953class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001954 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00001955 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001956 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00001957 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001958 signed CurrentBlock;
1959 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001960 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001961public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001962
Chris Lattnerc61089a2009-06-30 01:26:17 +00001963 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1964 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001965 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1966 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001967 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00001968 BI != BEnd; ++BI, ++j ) {
1969 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
1970 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
1971 StmtMap[SE] = P;
1972
1973 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
1974 DeclMap[DS->getSingleDecl()] = P;
1975
1976 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
1977 if (VarDecl* VD = IS->getConditionVariable())
1978 DeclMap[VD] = P;
1979
1980 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
1981 if (VarDecl* VD = FS->getConditionVariable())
1982 DeclMap[VD] = P;
1983
1984 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
1985 if (VarDecl* VD = WS->getConditionVariable())
1986 DeclMap[VD] = P;
1987
1988 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
1989 if (VarDecl* VD = SS->getConditionVariable())
1990 DeclMap[VD] = P;
1991
1992 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
1993 if (VarDecl* VD = CS->getExceptionDecl())
1994 DeclMap[VD] = P;
1995 }
1996 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001997 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00001998 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001999 }
Mike Stump31feda52009-07-17 01:31:16 +00002000
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002001 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002002
Chris Lattnerc61089a2009-06-30 01:26:17 +00002003 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002004 void setBlockID(signed i) { CurrentBlock = i; }
2005 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002006
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002007 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2008 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002009
2010 if (I == StmtMap.end())
2011 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002012
2013 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002014 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002015 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002016 }
Mike Stump31feda52009-07-17 01:31:16 +00002017
Ted Kremenek60983dc2010-01-19 20:52:05 +00002018 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002019 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002020 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002021
2022 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2023 DeclMapTy::iterator I = DeclMap.find(D);
2024
2025 if (I == DeclMap.end())
2026 return false;
2027
2028 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2029 && I->second.second == CurrentStmt) {
2030 return false;
2031 }
2032
2033 OS << "[B" << I->second.first << "." << I->second.second << "]";
2034 return true;
2035 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002036};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002037} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002038
Chris Lattnerc61089a2009-06-30 01:26:17 +00002039
2040namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002041class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002042 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002043
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002044 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002045 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002046 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002047public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002048 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002049 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002050 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002051
Ted Kremenek9aae5132007-08-23 21:42:29 +00002052 void VisitIfStmt(IfStmt* I) {
2053 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002054 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002055 }
Mike Stump31feda52009-07-17 01:31:16 +00002056
Ted Kremenek9aae5132007-08-23 21:42:29 +00002057 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002058 void VisitStmt(Stmt* Terminator) {
2059 Terminator->printPretty(OS, Helper, Policy);
2060 }
2061
Ted Kremenek9aae5132007-08-23 21:42:29 +00002062 void VisitForStmt(ForStmt* F) {
2063 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002064 if (F->getInit())
2065 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002066 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002067 if (Stmt* C = F->getCond())
2068 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002069 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002070 if (F->getInc())
2071 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002072 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002073 }
Mike Stump31feda52009-07-17 01:31:16 +00002074
Ted Kremenek9aae5132007-08-23 21:42:29 +00002075 void VisitWhileStmt(WhileStmt* W) {
2076 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002077 if (Stmt* C = W->getCond())
2078 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002079 }
Mike Stump31feda52009-07-17 01:31:16 +00002080
Ted Kremenek9aae5132007-08-23 21:42:29 +00002081 void VisitDoStmt(DoStmt* D) {
2082 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002083 if (Stmt* C = D->getCond())
2084 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002085 }
Mike Stump31feda52009-07-17 01:31:16 +00002086
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002087 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002088 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002089 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002090 }
Mike Stump31feda52009-07-17 01:31:16 +00002091
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002092 void VisitCXXTryStmt(CXXTryStmt* CS) {
2093 OS << "try ...";
2094 }
2095
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002096 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002097 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002098 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002099 }
Mike Stump31feda52009-07-17 01:31:16 +00002100
Ted Kremenek391f94a2007-08-31 22:29:13 +00002101 void VisitChooseExpr(ChooseExpr* C) {
2102 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002103 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002104 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002105 }
Mike Stump31feda52009-07-17 01:31:16 +00002106
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002107 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2108 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002109 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002110 }
Mike Stump31feda52009-07-17 01:31:16 +00002111
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002112 void VisitBinaryOperator(BinaryOperator* B) {
2113 if (!B->isLogicalOp()) {
2114 VisitExpr(B);
2115 return;
2116 }
Mike Stump31feda52009-07-17 01:31:16 +00002117
Douglas Gregor7de59662009-05-29 20:38:28 +00002118 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002119
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002120 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002121 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002122 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002123 return;
John McCalle3027922010-08-25 11:45:40 +00002124 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002125 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002126 return;
2127 default:
2128 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002129 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002130 }
Mike Stump31feda52009-07-17 01:31:16 +00002131
Ted Kremenek12687ff2007-08-27 21:54:41 +00002132 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002133 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002134 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002135};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002136} // end anonymous namespace
2137
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002138static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002139 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002140 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2141 Stmt *S = CS;
2142
2143 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002144
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002145 // special printing for statement-expressions.
2146 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2147 CompoundStmt* Sub = SE->getSubStmt();
2148
2149 if (Sub->child_begin() != Sub->child_end()) {
2150 OS << "({ ... ; ";
2151 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2152 OS << " })\n";
2153 return;
2154 }
2155 }
2156 // special printing for comma expressions.
2157 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2158 if (B->getOpcode() == BO_Comma) {
2159 OS << "... , ";
2160 Helper->handledStmt(B->getRHS(),OS);
2161 OS << '\n';
2162 return;
2163 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002164 }
2165 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002166 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002167
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002168 if (isa<CXXOperatorCallExpr>(S)) {
2169 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002170 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002171 else if (isa<CXXBindTemporaryExpr>(S)) {
2172 OS << " (BindTemporary)";
2173 }
Mike Stump31feda52009-07-17 01:31:16 +00002174
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002175 // Expressions need a newline.
2176 if (isa<Expr>(S))
2177 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002178
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002179 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2180 CXXBaseOrMemberInitializer* I = IE;
2181 if (I->isBaseInitializer())
2182 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2183 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002184
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002185 OS << "(";
2186 if (Expr* IE = I->getInit())
2187 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2188 OS << ")";
2189
2190 if (I->isBaseInitializer())
2191 OS << " (Base initializer)\n";
2192 else OS << " (Member initializer)\n";
2193
2194 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2195 VarDecl* VD = DE.getVarDecl();
2196 Helper->handleDecl(VD, OS);
2197
2198 Type* T = VD->getType().getTypePtr();
2199 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2200 T = RT->getPointeeType().getTypePtr();
2201
2202 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2203 OS << " (Implicit destructor)\n";
2204 }
2205 }
Mike Stump31feda52009-07-17 01:31:16 +00002206
Chris Lattnerc61089a2009-06-30 01:26:17 +00002207static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2208 const CFGBlock& B,
2209 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002210
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002211 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002212
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002213 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002214 OS << "\n [ B" << B.getBlockID();
2215
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002216 if (&B == &cfg->getEntry())
2217 OS << " (ENTRY) ]\n";
2218 else if (&B == &cfg->getExit())
2219 OS << " (EXIT) ]\n";
2220 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002221 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002222 else
2223 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002224
Ted Kremenek71eca012007-08-29 23:20:49 +00002225 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002226 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002227
2228 if (print_edges)
2229 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002230
Mike Stump92244b02010-01-19 22:00:14 +00002231 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002232 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002233 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002234 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002235 C->getLHS()->printPretty(OS, Helper,
2236 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002237 if (C->getRHS()) {
2238 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002239 C->getRHS()->printPretty(OS, Helper,
2240 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002241 }
Mike Stump92244b02010-01-19 22:00:14 +00002242 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002243 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002244 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002245 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002246 if (CS->getExceptionDecl())
2247 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2248 0);
2249 else
2250 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002251 OS << ")";
2252
2253 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002254 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002255
Ted Kremenek71eca012007-08-29 23:20:49 +00002256 OS << ":\n";
2257 }
Mike Stump31feda52009-07-17 01:31:16 +00002258
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002259 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002260 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002261
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002262 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2263 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002264
Ted Kremenek71eca012007-08-29 23:20:49 +00002265 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002266 if (print_edges)
2267 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002268
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002269 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002270
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002271 if (Helper)
2272 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002273
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002274 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002275 }
Mike Stump31feda52009-07-17 01:31:16 +00002276
Ted Kremenek71eca012007-08-29 23:20:49 +00002277 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002278 if (B.getTerminator()) {
2279 if (print_edges)
2280 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002281
Ted Kremenek71eca012007-08-29 23:20:49 +00002282 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002283
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002284 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002285
Chris Lattnerc61089a2009-06-30 01:26:17 +00002286 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2287 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002288 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002289 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002290 }
Mike Stump31feda52009-07-17 01:31:16 +00002291
Ted Kremenek71eca012007-08-29 23:20:49 +00002292 if (print_edges) {
2293 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002294 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002295 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002296
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002297 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2298 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002299
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002300 if (i == 8 || (i-8) == 0)
2301 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002302
Ted Kremenek71eca012007-08-29 23:20:49 +00002303 OS << " B" << (*I)->getBlockID();
2304 }
Mike Stump31feda52009-07-17 01:31:16 +00002305
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002306 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002307
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002308 // Print the successors of this block.
2309 OS << " Successors (" << B.succ_size() << "):";
2310 i = 0;
2311
2312 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2313 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002314
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002315 if (i == 8 || (i-8) % 10 == 0)
2316 OS << "\n ";
2317
Mike Stump0d76d072009-07-20 23:24:15 +00002318 if (*I)
2319 OS << " B" << (*I)->getBlockID();
2320 else
2321 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002322 }
Mike Stump31feda52009-07-17 01:31:16 +00002323
Ted Kremenek71eca012007-08-29 23:20:49 +00002324 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002325 }
Mike Stump31feda52009-07-17 01:31:16 +00002326}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002327
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002328
2329/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002330void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002331
2332/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002333void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2334 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002335
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002336 // Print the entry block.
2337 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002338
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002339 // Iterate through the CFGBlocks and print them one by one.
2340 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2341 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002342 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002343 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002344
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002345 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002346 }
Mike Stump31feda52009-07-17 01:31:16 +00002347
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002348 // Print the exit block.
2349 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002350 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002351}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002352
2353/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002354void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2355 print(llvm::errs(), cfg, LO);
2356}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002357
2358/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2359/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002360void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2361 const LangOptions &LO) const {
2362 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002363 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002364}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002365
Ted Kremenek15647632008-01-30 23:02:42 +00002366/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002367void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002368 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002369 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002370 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2371}
2372
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002373Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002374
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002375 if (!Terminator)
2376 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002377
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002378 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002379
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002380 switch (Terminator->getStmtClass()) {
2381 default:
2382 break;
Mike Stump31feda52009-07-17 01:31:16 +00002383
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002384 case Stmt::ForStmtClass:
2385 E = cast<ForStmt>(Terminator)->getCond();
2386 break;
Mike Stump31feda52009-07-17 01:31:16 +00002387
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002388 case Stmt::WhileStmtClass:
2389 E = cast<WhileStmt>(Terminator)->getCond();
2390 break;
Mike Stump31feda52009-07-17 01:31:16 +00002391
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002392 case Stmt::DoStmtClass:
2393 E = cast<DoStmt>(Terminator)->getCond();
2394 break;
Mike Stump31feda52009-07-17 01:31:16 +00002395
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002396 case Stmt::IfStmtClass:
2397 E = cast<IfStmt>(Terminator)->getCond();
2398 break;
Mike Stump31feda52009-07-17 01:31:16 +00002399
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002400 case Stmt::ChooseExprClass:
2401 E = cast<ChooseExpr>(Terminator)->getCond();
2402 break;
Mike Stump31feda52009-07-17 01:31:16 +00002403
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002404 case Stmt::IndirectGotoStmtClass:
2405 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2406 break;
Mike Stump31feda52009-07-17 01:31:16 +00002407
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002408 case Stmt::SwitchStmtClass:
2409 E = cast<SwitchStmt>(Terminator)->getCond();
2410 break;
Mike Stump31feda52009-07-17 01:31:16 +00002411
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002412 case Stmt::ConditionalOperatorClass:
2413 E = cast<ConditionalOperator>(Terminator)->getCond();
2414 break;
Mike Stump31feda52009-07-17 01:31:16 +00002415
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002416 case Stmt::BinaryOperatorClass: // '&&' and '||'
2417 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002418 break;
Mike Stump31feda52009-07-17 01:31:16 +00002419
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002420 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002421 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002422 }
Mike Stump31feda52009-07-17 01:31:16 +00002423
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002424 return E ? E->IgnoreParens() : NULL;
2425}
2426
Ted Kremenek92137a32008-05-16 16:06:00 +00002427bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002428
Ted Kremenek92137a32008-05-16 16:06:00 +00002429 if (!Terminator)
2430 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002431
Ted Kremenek92137a32008-05-16 16:06:00 +00002432 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002433
Ted Kremenek92137a32008-05-16 16:06:00 +00002434 switch (Terminator->getStmtClass()) {
2435 default:
2436 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002437
2438 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002439 case Stmt::WhileStmtClass:
2440 case Stmt::DoStmtClass:
2441 case Stmt::IfStmtClass:
2442 case Stmt::ChooseExprClass:
2443 case Stmt::ConditionalOperatorClass:
2444 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002445 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002446 }
Mike Stump31feda52009-07-17 01:31:16 +00002447
Ted Kremenek92137a32008-05-16 16:06:00 +00002448 return E ? E->IgnoreParens() : NULL;
2449}
2450
Ted Kremenek15647632008-01-30 23:02:42 +00002451
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002452//===----------------------------------------------------------------------===//
2453// CFG Graphviz Visualization
2454//===----------------------------------------------------------------------===//
2455
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002456
2457#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002458static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002459#endif
2460
Chris Lattnerc61089a2009-06-30 01:26:17 +00002461void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002462#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002463 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002464 GraphHelper = &H;
2465 llvm::ViewGraph(this,"CFG");
2466 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002467#endif
2468}
2469
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002470namespace llvm {
2471template<>
2472struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002473
2474 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2475
2476 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002477
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002478#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002479 std::string OutSStr;
2480 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002481 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002482 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002483
2484 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2485
2486 // Process string output to make it nicer...
2487 for (unsigned i = 0; i != OutStr.length(); ++i)
2488 if (OutStr[i] == '\n') { // Left justify
2489 OutStr[i] = '\\';
2490 OutStr.insert(OutStr.begin()+i+1, 'l');
2491 }
Mike Stump31feda52009-07-17 01:31:16 +00002492
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002493 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002494#else
2495 return "";
2496#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002497 }
2498};
2499} // end namespace llvm