blob: eab7da7e83709af094f4c69abb7ca61fea72d524 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000022#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor6e6ad602009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000037
38class AddStmtChoice {
39public:
40 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
41public:
42 AddStmtChoice(Kind kind) : k(kind) {}
43 bool alwaysAdd() const { return k != NotAlwaysAdd; }
44 bool asLValue() const { return k == AlwaysAddAsLValue; }
45private:
46 Kind k;
47};
Mike Stump31feda52009-07-17 01:31:16 +000048
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000049/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000050/// The builder is stateful: an instance of the builder should be used to only
51/// construct a single CFG.
52///
53/// Example usage:
54///
55/// CFGBuilder builder;
56/// CFG* cfg = builder.BuildAST(stmt1);
57///
Mike Stump31feda52009-07-17 01:31:16 +000058/// CFG construction is done via a recursive walk of an AST. We actually parse
59/// the AST in reverse order so that the successor of a basic block is
60/// constructed prior to its predecessor. This allows us to nicely capture
61/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000062///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000063class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000064 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000065 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000066
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000067 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000068 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000069 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000070 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000071 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000072 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000073
Ted Kremenekeda180e22007-08-28 19:26:49 +000074 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000075 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
76 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000077
78 // A list of blocks that end with a "goto" that must be backpatched to their
79 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000080 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000081 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenekeda180e22007-08-28 19:26:49 +000083 // A list of labels whose address has been taken (for indirect gotos).
84 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
85 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000086
87public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000088 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
89 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000090 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek289ae4f2009-10-12 20:55:07 +000091 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000092
Ted Kremenek9aae5132007-08-23 21:42:29 +000093 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000094 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000095
Ted Kremenek93668002009-07-17 22:18:43 +000096private:
97 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000098 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
99 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
100 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000101 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000102 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000103 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000104 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000105 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000106 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
107 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000108 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000109 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S) { return NYS(); }
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000110 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenekc98cdd12009-12-15 01:38:04 +0000111 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S) { return NYS(); }
Ted Kremenek93668002009-07-17 22:18:43 +0000112 CFGBlock *VisitDeclStmt(DeclStmt *DS);
113 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000114 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
115 CFGBlock *VisitDoStmt(DoStmt *D);
116 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitGotoStmt(GotoStmt* G);
118 CFGBlock *VisitIfStmt(IfStmt *I);
119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
120 CFGBlock *VisitLabelStmt(LabelStmt *L);
121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
126 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
128 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000129 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
130 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000131
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000132 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
133 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000134 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000135
Ted Kremenek6065ef62008-04-28 18:00:46 +0000136 // NYS == Not Yet Supported
137 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000138 badCFG = true;
139 return Block;
140 }
Mike Stump31feda52009-07-17 01:31:16 +0000141
Ted Kremenek93668002009-07-17 22:18:43 +0000142 void autoCreateBlock() { if (!Block) Block = createBlock(); }
143 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000144 bool FinishBlock(CFGBlock* B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000145 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
146 return Visit(S, asc);
147 }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000148
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000149 void AppendStmt(CFGBlock *B, Stmt *S,
150 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
151 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000152 }
153
154 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
155 B->addSuccessor(S, cfg->getBumpVectorContext());
156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek963cc312009-07-24 06:55:42 +0000158 /// TryResult - a class representing a variant over the values
159 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
160 /// and is used by the CFGBuilder to decide if a branch condition
161 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000162 class TryResult {
163 int X;
164 public:
165 TryResult(bool b) : X(b ? 1 : 0) {}
166 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek30754282009-07-24 04:47:11 +0000168 bool isTrue() const { return X == 1; }
169 bool isFalse() const { return X == 0; }
170 bool isKnown() const { return X >= 0; }
171 void negate() {
172 assert(isKnown());
173 X ^= 0x1;
174 }
175 };
Mike Stump11289f42009-09-09 15:08:12 +0000176
Mike Stump773582d2009-07-23 23:25:26 +0000177 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
178 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000179 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000180 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000181 if (!S->isTypeDependent() && !S->isValueDependent() &&
182 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000183 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000184
185 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000186 }
187
Ted Kremenekb64d1832008-03-13 03:04:22 +0000188 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000189};
Mike Stump31feda52009-07-17 01:31:16 +0000190
Douglas Gregor4619e432008-12-05 23:32:09 +0000191// FIXME: Add support for dependent-sized array types in C++?
192// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000193static VariableArrayType* FindVA(Type* t) {
194 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
195 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
196 if (vat->getSizeExpr())
197 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000198
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000199 t = vt->getElementType().getTypePtr();
200 }
Mike Stump31feda52009-07-17 01:31:16 +0000201
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000202 return 0;
203}
Mike Stump31feda52009-07-17 01:31:16 +0000204
205/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
206/// arbitrary statement. Examples include a single expression or a function
207/// body (compound statement). The ownership of the returned CFG is
208/// transferred to the caller. If CFG construction fails, this method returns
209/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000210CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
211 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000212 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000213 if (!Statement)
214 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000215
Ted Kremenekb64d1832008-03-13 03:04:22 +0000216 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000217
218 // Create an empty block that will serve as the exit block for the CFG. Since
219 // this is the first block added to the CFG, it will be implicitly registered
220 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000221 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000222 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000223 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenek9aae5132007-08-23 21:42:29 +0000225 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000226 CFGBlock* B = addStmt(Statement);
Ted Kremenek8aed4902009-10-20 23:46:25 +0000227 if (!B)
228 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000229
Ted Kremenek40d87632008-02-27 17:33:02 +0000230 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000231 // Finalize the last constructed block. This usually involves reversing the
232 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000233 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000234
235 // Backpatch the gotos whose label -> block mappings we didn't know when we
236 // encountered them.
237 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000238 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000239
Ted Kremenek9aae5132007-08-23 21:42:29 +0000240 CFGBlock* B = *I;
241 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
242 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
243
244 // If there is no target for the goto, then we are looking at an
245 // incomplete AST. Handle this by not registering a successor.
246 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000247
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000248 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000249 }
Mike Stump31feda52009-07-17 01:31:16 +0000250
Ted Kremenekeda180e22007-08-28 19:26:49 +0000251 // Add successors to the Indirect Goto Dispatch block (if we have one).
252 if (CFGBlock* B = cfg->getIndirectGotoBlock())
253 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
254 E = AddressTakenLabels.end(); I != E; ++I ) {
255
256 // Lookup the target block.
257 LabelMapTy::iterator LI = LabelMap.find(*I);
258
259 // If there is no target block that contains label, then we are looking
260 // at an incomplete AST. Handle this by not registering a successor.
261 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000262
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000263 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000264 }
Mike Stump31feda52009-07-17 01:31:16 +0000265
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000266 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000267 }
Mike Stump31feda52009-07-17 01:31:16 +0000268
269 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000270 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000271
Ted Kremenekab929bb2009-10-20 23:59:28 +0000272 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000273}
Mike Stump31feda52009-07-17 01:31:16 +0000274
Ted Kremenek9aae5132007-08-23 21:42:29 +0000275/// createBlock - Used to lazily create blocks that are connected
276/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000277CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000278 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000279 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000280 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000281 return B;
282}
Mike Stump31feda52009-07-17 01:31:16 +0000283
Ted Kremenek0868eea2009-09-24 18:45:41 +0000284/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000285bool CFGBuilder::FinishBlock(CFGBlock* B) {
286 if (badCFG)
287 return false;
288
Ted Kremenek93668002009-07-17 22:18:43 +0000289 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000290 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000291}
292
Ted Kremenek93668002009-07-17 22:18:43 +0000293/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000294/// blocks for ternary operators, &&, and ||. We also process "," and
295/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000296CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000297tryAgain:
298 switch (S->getStmtClass()) {
299 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000300 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000301
302 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000303 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek93668002009-07-17 22:18:43 +0000305 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000307
Ted Kremenek93668002009-07-17 22:18:43 +0000308 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000309 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000310
Ted Kremenek93668002009-07-17 22:18:43 +0000311 case Stmt::BreakStmtClass:
312 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek93668002009-07-17 22:18:43 +0000314 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000315 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek93668002009-07-17 22:18:43 +0000317 case Stmt::CaseStmtClass:
318 return VisitCaseStmt(cast<CaseStmt>(S));
319
320 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000321 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000322
Ted Kremenek93668002009-07-17 22:18:43 +0000323 case Stmt::CompoundStmtClass:
324 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenek93668002009-07-17 22:18:43 +0000326 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000327 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000328
Ted Kremenek93668002009-07-17 22:18:43 +0000329 case Stmt::ContinueStmtClass:
330 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek93668002009-07-17 22:18:43 +0000332 case Stmt::DeclStmtClass:
333 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000334
Ted Kremenek93668002009-07-17 22:18:43 +0000335 case Stmt::DefaultStmtClass:
336 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 case Stmt::DoStmtClass:
339 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 case Stmt::ForStmtClass:
342 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000343
Ted Kremenek93668002009-07-17 22:18:43 +0000344 case Stmt::GotoStmtClass:
345 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek93668002009-07-17 22:18:43 +0000347 case Stmt::IfStmtClass:
348 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek93668002009-07-17 22:18:43 +0000350 case Stmt::IndirectGotoStmtClass:
351 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenek93668002009-07-17 22:18:43 +0000353 case Stmt::LabelStmtClass:
354 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000355
Ted Kremenek93668002009-07-17 22:18:43 +0000356 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000357 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
358
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000359 case Stmt::CXXThrowExprClass:
360 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
361
Ted Kremenek93668002009-07-17 22:18:43 +0000362 case Stmt::ObjCAtSynchronizedStmtClass:
363 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000364
Ted Kremenek93668002009-07-17 22:18:43 +0000365 case Stmt::ObjCAtThrowStmtClass:
366 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek93668002009-07-17 22:18:43 +0000368 case Stmt::ObjCAtTryStmtClass:
369 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek93668002009-07-17 22:18:43 +0000371 case Stmt::ObjCForCollectionStmtClass:
372 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenek93668002009-07-17 22:18:43 +0000374 case Stmt::ParenExprClass:
375 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000376 goto tryAgain;
377
Ted Kremenek93668002009-07-17 22:18:43 +0000378 case Stmt::NullStmtClass:
379 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek93668002009-07-17 22:18:43 +0000381 case Stmt::ReturnStmtClass:
382 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek93668002009-07-17 22:18:43 +0000384 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000385 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek93668002009-07-17 22:18:43 +0000387 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000388 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek93668002009-07-17 22:18:43 +0000390 case Stmt::SwitchStmtClass:
391 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000392
Ted Kremenek93668002009-07-17 22:18:43 +0000393 case Stmt::WhileStmtClass:
394 return VisitWhileStmt(cast<WhileStmt>(S));
395 }
396}
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000398CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
399 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000400 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000401 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Ted Kremenek93668002009-07-17 22:18:43 +0000404 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000405}
Mike Stump31feda52009-07-17 01:31:16 +0000406
Ted Kremenek93668002009-07-17 22:18:43 +0000407/// VisitChildren - Visit the children of a Stmt.
408CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
409 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000410 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000411 E = Terminator->child_end(); I != E; ++I) {
412 if (*I) B = Visit(*I);
413 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000414 return B;
415}
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000417CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
418 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000419 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000420
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000421 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000422 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000423 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000424 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000425
Ted Kremenek9aae5132007-08-23 21:42:29 +0000426 return Block;
427}
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000429CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
430 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000431 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000432 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000433 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek93668002009-07-17 22:18:43 +0000435 if (!FinishBlock(ConfluenceBlock))
436 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 // create the block evaluating the LHS
439 CFGBlock* LHSBlock = createBlock(false);
440 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000441
Ted Kremenek93668002009-07-17 22:18:43 +0000442 // create the block evaluating the RHS
443 Succ = ConfluenceBlock;
444 Block = NULL;
445 CFGBlock* RHSBlock = addStmt(B->getRHS());
446 if (!FinishBlock(RHSBlock))
447 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000448
Mike Stump773582d2009-07-23 23:25:26 +0000449 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000450 TryResult KnownVal = TryEvaluateBool(B->getLHS());
451 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
452 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000453
Ted Kremenek93668002009-07-17 22:18:43 +0000454 // Now link the LHSBlock with RHSBlock.
455 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000456 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
457 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000458 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000459 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000460 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
461 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Ted Kremenek93668002009-07-17 22:18:43 +0000464 // Generate the blocks for evaluating the LHS.
465 Block = LHSBlock;
466 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000467 }
Ted Kremenek93668002009-07-17 22:18:43 +0000468 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000469 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000470 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000471 addStmt(B->getRHS());
472 return addStmt(B->getLHS());
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000475 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000476}
477
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000478CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
479 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000480 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000481 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000482 }
483 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000484}
485
Ted Kremenek93668002009-07-17 22:18:43 +0000486CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
487 // "break" is a control-flow statement. Thus we stop processing the current
488 // block.
489 if (Block && !FinishBlock(Block))
490 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Ted Kremenek93668002009-07-17 22:18:43 +0000492 // Now create a new block that ends with the break statement.
493 Block = createBlock(false);
494 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenek93668002009-07-17 22:18:43 +0000496 // If there is no target for the break, then we are looking at an incomplete
497 // AST. This means that the CFG cannot be constructed.
498 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000499 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000500 else
501 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000502
503
Ted Kremenek9aae5132007-08-23 21:42:29 +0000504 return Block;
505}
Mike Stump11289f42009-09-09 15:08:12 +0000506
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000507CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000508 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000509 bool NoReturn = false;
510 if (C->getCallee()->getType().getNoReturnAttr()) {
511 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000512 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000513
514 if (FunctionDecl *FD = C->getDirectCallee())
515 if (FD->hasAttr<NoReturnAttr>())
516 NoReturn = true;
517
518 if (!NoReturn)
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000519 return VisitStmt(C, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000520
Mike Stump8c5d7992009-07-25 21:26:53 +0000521 if (Block && !FinishBlock(Block))
522 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Mike Stump8c5d7992009-07-25 21:26:53 +0000524 // Create new block with no successor for the remaining pieces.
525 Block = createBlock(false);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000526 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000527
528 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000529 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000530
Mike Stump8c5d7992009-07-25 21:26:53 +0000531 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000532}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000533
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000534CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
535 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000536 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000537 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000538 if (!FinishBlock(ConfluenceBlock))
539 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenek21822592009-07-17 18:20:32 +0000541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000543 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000544 if (!FinishBlock(LHSBlock))
545 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000546
Ted Kremenek21822592009-07-17 18:20:32 +0000547 Succ = ConfluenceBlock;
548 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000549 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000550 if (!FinishBlock(RHSBlock))
551 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000552
Ted Kremenek21822592009-07-17 18:20:32 +0000553 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000554 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000555 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000556 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
557 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000558 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000559 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000560}
Mike Stump11289f42009-09-09 15:08:12 +0000561
562
563CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
564 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000565
566 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
567 I != E; ++I ) {
568 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000569
Ted Kremenekce499c22009-08-27 23:16:26 +0000570 if (badCFG)
571 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000572 }
Ted Kremenek93668002009-07-17 22:18:43 +0000573 return LastBlock;
574}
Mike Stump11289f42009-09-09 15:08:12 +0000575
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000576CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
577 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000578 // Create the confluence block that will "merge" the results of the ternary
579 // expression.
580 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000581 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000582 if (!FinishBlock(ConfluenceBlock))
583 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Ted Kremenek51d40b02009-07-17 18:15:54 +0000585 // Create a block for the LHS expression if there is an LHS expression. A
586 // GCC extension allows LHS to be NULL, causing the condition to be the
587 // value that is returned instead.
588 // e.g: x ?: y is shorthand for: x ? x : y;
589 Succ = ConfluenceBlock;
590 Block = NULL;
591 CFGBlock* LHSBlock = NULL;
592 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000593 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000594 if (!FinishBlock(LHSBlock))
595 return 0;
596 Block = NULL;
597 }
Mike Stump11289f42009-09-09 15:08:12 +0000598
Ted Kremenek51d40b02009-07-17 18:15:54 +0000599 // Create the block for the RHS expression.
600 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000601 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000602 if (!FinishBlock(RHSBlock))
603 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Ted Kremenek51d40b02009-07-17 18:15:54 +0000605 // Create the block that will contain the condition.
606 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Mike Stump773582d2009-07-23 23:25:26 +0000608 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000609 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000610 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000611 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000612 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000613 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000614 // If we know the condition is false, add NULL as the successor for
615 // the block containing the condition. In this case, the confluence
616 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000617 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000618 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000619 } else {
620 // If we have no LHS expression, add the ConfluenceBlock as a direct
621 // successor for the block containing the condition. Moreover, we need to
622 // reverse the order of the predecessors in the ConfluenceBlock because
623 // the RHSBlock will have been added to the succcessors already, and we
624 // want the first predecessor to the the block containing the expression
625 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000626 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000627 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000628 std::reverse(ConfluenceBlock->pred_begin(),
629 ConfluenceBlock->pred_end());
630 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000633 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000634 Block->setTerminator(C);
635 return addStmt(C->getCond());
636}
637
Ted Kremenek93668002009-07-17 22:18:43 +0000638CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
639 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000640
Ted Kremenek93668002009-07-17 22:18:43 +0000641 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000642 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000643 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000644 }
Mike Stump11289f42009-09-09 15:08:12 +0000645
Ted Kremenek93668002009-07-17 22:18:43 +0000646 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000647
Ted Kremenek93668002009-07-17 22:18:43 +0000648 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
649 typedef llvm::SmallVector<Decl*,10> BufTy;
650 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenek93668002009-07-17 22:18:43 +0000652 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
653 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
654 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
655 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000656
Ted Kremenek93668002009-07-17 22:18:43 +0000657 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
658 // automatically freed with the CFG.
659 DeclGroupRef DG(*I);
660 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000661 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000662 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000663
Ted Kremenek93668002009-07-17 22:18:43 +0000664 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000665 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000666 B = VisitDeclSubExpr(D);
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
669 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000670}
Mike Stump11289f42009-09-09 15:08:12 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672/// VisitDeclSubExpr - Utility method to add block-level expressions for
673/// initializers in Decls.
674CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
675 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000676
Ted Kremenek93668002009-07-17 22:18:43 +0000677 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenek93668002009-07-17 22:18:43 +0000679 if (!VD)
680 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenek93668002009-07-17 22:18:43 +0000682 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek93668002009-07-17 22:18:43 +0000684 if (Init) {
685 // Optimization: Don't create separate block-level statements for literals.
686 switch (Init->getStmtClass()) {
687 case Stmt::IntegerLiteralClass:
688 case Stmt::CharacterLiteralClass:
689 case Stmt::StringLiteralClass:
690 break;
691 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000692 Block = addStmt(Init,
693 VD->getType()->isReferenceType()
694 ? AddStmtChoice::AlwaysAddAsLValue
695 : AddStmtChoice::AlwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000696 }
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenek93668002009-07-17 22:18:43 +0000699 // If the type of VD is a VLA, then we must process its size expressions.
700 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
701 VA = FindVA(VA->getElementType().getTypePtr()))
702 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000703
Ted Kremenek93668002009-07-17 22:18:43 +0000704 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705}
706
707CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000708 // We may see an if statement in the middle of a basic block, or it may be the
709 // first statement we are processing. In either case, we create a new basic
710 // block. First, we create the blocks for the then...else statements, and
711 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000712 // middle of a block, we stop processing that block. That block is then the
713 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000714
715 // The block we were proccessing is now finished. Make it the successor
716 // block.
717 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000719 if (!FinishBlock(Block))
720 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000721 }
Mike Stump31feda52009-07-17 01:31:16 +0000722
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000723 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000724 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000725
Ted Kremenek9aae5132007-08-23 21:42:29 +0000726 if (Stmt* Else = I->getElse()) {
727 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000728
Ted Kremenek9aae5132007-08-23 21:42:29 +0000729 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000730 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000731 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000732 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000733
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000734 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
735 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000736 else if (Block) {
737 if (!FinishBlock(ElseBlock))
738 return 0;
739 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000740 }
Mike Stump31feda52009-07-17 01:31:16 +0000741
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000742 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000743 CFGBlock* ThenBlock;
744 {
745 Stmt* Then = I->getThen();
746 assert (Then);
747 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000748 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000749 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000750
Ted Kremenek1b379512009-04-01 03:52:47 +0000751 if (!ThenBlock) {
752 // We can reach here if the "then" body has all NullStmts.
753 // Create an empty block so we can distinguish between true and false
754 // branches in path-sensitive analyses.
755 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000756 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000757 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000758 if (!FinishBlock(ThenBlock))
759 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000760 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000761 }
762
Mike Stump31feda52009-07-17 01:31:16 +0000763 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000764 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000765
Ted Kremenek9aae5132007-08-23 21:42:29 +0000766 // Set the terminator of the new block to the If statement.
767 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000768
Mike Stump773582d2009-07-23 23:25:26 +0000769 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000770 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000771
Ted Kremenek9aae5132007-08-23 21:42:29 +0000772 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000773 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
774 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000775
776 // Add the condition as the last statement in the new block. This may create
777 // new blocks as the condition may contain control-flow. Any newly created
778 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000779 Block = addStmt(I->getCond());
780
781 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
782 // and the condition variable initialization to the CFG.
783 if (VarDecl *VD = I->getConditionVariable()) {
784 if (Expr *Init = VD->getInit()) {
785 autoCreateBlock();
786 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
787 addStmt(Init);
788 }
789 }
790
791 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000792}
Mike Stump31feda52009-07-17 01:31:16 +0000793
794
Ted Kremenek9aae5132007-08-23 21:42:29 +0000795CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000796 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000797 //
Mike Stump31feda52009-07-17 01:31:16 +0000798 // NOTE: If a "return" appears in the middle of a block, this means that the
799 // code afterwards is DEAD (unreachable). We still keep a basic block
800 // for that code; a simple "mark-and-sweep" from the entry block will be
801 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000802 if (Block)
803 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000804
805 // Create the new block.
806 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000807
Ted Kremenek9aae5132007-08-23 21:42:29 +0000808 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000809 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000810
811 // Add the return statement to the block. This may create new blocks if R
812 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000813 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000814}
815
816CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
817 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000818 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000819 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000820
Ted Kremenek93668002009-07-17 22:18:43 +0000821 if (!LabelBlock) // This can happen when the body is empty, i.e.
822 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000823
Ted Kremenek93668002009-07-17 22:18:43 +0000824 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000826
827 // Labels partition blocks, so this is the end of the basic block we were
828 // processing (L is the block's label). Because this is label (and we have
829 // already processed the substatement) there is no extra control-flow to worry
830 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000831 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000832 if (!FinishBlock(LabelBlock))
833 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000834
835 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000836 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000837
Ted Kremenek9aae5132007-08-23 21:42:29 +0000838 // This block is now the implicit successor of other blocks.
839 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000840
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 return LabelBlock;
842}
843
844CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000845 // Goto is a control-flow statement. Thus we stop processing the current
846 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000847 if (Block)
848 FinishBlock(Block);
849
Ted Kremenek9aae5132007-08-23 21:42:29 +0000850 Block = createBlock(false);
851 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000852
853 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000854 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000855
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 if (I == LabelMap.end())
857 // We will need to backpatch this block later.
858 BackpatchBlocks.push_back(Block);
859 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000860 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000861
Mike Stump31feda52009-07-17 01:31:16 +0000862 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000863}
864
865CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000866 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000867
Mike Stump014b3ea2009-07-21 01:12:51 +0000868 // "for" is a control-flow statement. Thus we stop processing the current
869 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000870 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000871 if (!FinishBlock(Block))
872 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000873 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000874 } else
875 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000876
877 // Because of short-circuit evaluation, the condition of the loop can span
878 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
879 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000880 CFGBlock* ExitConditionBlock = createBlock(false);
881 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000882
Ted Kremenek81e14852007-08-27 19:46:09 +0000883 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000884 ExitConditionBlock->setTerminator(F);
885
886 // Now add the actual condition to the condition block. Because the condition
887 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000888 if (Stmt* C = F->getCond()) {
889 Block = ExitConditionBlock;
890 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +0000891 assert(Block == EntryConditionBlock);
892
893 // If this block contains a condition variable, add both the condition
894 // variable and initializer to the CFG.
895 if (VarDecl *VD = F->getConditionVariable()) {
896 if (Expr *Init = VD->getInit()) {
897 autoCreateBlock();
898 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
899 EntryConditionBlock = addStmt(Init);
900 assert(Block == EntryConditionBlock);
901 }
902 }
903
Ted Kremenek55957a82009-05-02 00:13:27 +0000904 if (Block) {
905 if (!FinishBlock(EntryConditionBlock))
906 return 0;
907 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000908 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000909
Mike Stump31feda52009-07-17 01:31:16 +0000910 // The condition block is the implicit successor for the loop body as well as
911 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000912 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000913
Mike Stump773582d2009-07-23 23:25:26 +0000914 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000915 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Mike Stump773582d2009-07-23 23:25:26 +0000917 if (F->getCond())
918 KnownVal = TryEvaluateBool(F->getCond());
919
Ted Kremenek9aae5132007-08-23 21:42:29 +0000920 // Now create the loop body.
921 {
922 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000923
Ted Kremenek9aae5132007-08-23 21:42:29 +0000924 // Save the current values for Block, Succ, and continue and break targets
925 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
926 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000927 save_break(BreakTargetBlock);
928
Ted Kremeneke9610502007-08-30 18:39:40 +0000929 // Create a new block to contain the (bottom) of the loop body.
930 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000931
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000932 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000933 // Generate increment code in its own basic block. This is the target of
934 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000935 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000936 } else {
937 // No increment code. Create a special, empty, block that is used as the
938 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000939 assert(Succ == EntryConditionBlock);
940 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000941 }
Mike Stump31feda52009-07-17 01:31:16 +0000942
Ted Kremenek902393b2009-04-28 00:51:56 +0000943 // Finish up the increment (or empty) block if it hasn't been already.
944 if (Block) {
945 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000946 if (!FinishBlock(Block))
947 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000948 Block = 0;
949 }
Mike Stump31feda52009-07-17 01:31:16 +0000950
Ted Kremenek902393b2009-04-28 00:51:56 +0000951 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000952
Ted Kremenek902393b2009-04-28 00:51:56 +0000953 // The starting block for the loop increment is the block that should
954 // represent the 'loop target' for looping back to the start of the loop.
955 ContinueTargetBlock->setLoopTarget(F);
956
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000957 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000958 BreakTargetBlock = LoopSuccessor;
959
960 // Now populate the body block, and in the process create new blocks as we
961 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000962 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000963
964 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000965 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000966 else if (Block && !FinishBlock(BodyBlock))
967 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000968
Ted Kremenek30754282009-07-24 04:47:11 +0000969 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000970 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000971 }
Mike Stump31feda52009-07-17 01:31:16 +0000972
Ted Kremenek30754282009-07-24 04:47:11 +0000973 // Link up the condition block with the code that follows the loop. (the
974 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000975 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000976
Ted Kremenek9aae5132007-08-23 21:42:29 +0000977 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000978 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000979 if (Stmt* I = F->getInit()) {
980 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000981 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000982 } else {
983 // There is no loop initialization. We are thus basically a while loop.
984 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000985 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000986 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000987 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000988 }
989}
990
Ted Kremenek9d56e642008-11-11 17:10:00 +0000991CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
992 // Objective-C fast enumeration 'for' statements:
993 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
994 //
995 // for ( Type newVariable in collection_expression ) { statements }
996 //
997 // becomes:
998 //
999 // prologue:
1000 // 1. collection_expression
1001 // T. jump to loop_entry
1002 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001003 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001004 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1005 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1006 // TB:
1007 // statements
1008 // T. jump to loop_entry
1009 // FB:
1010 // what comes after
1011 //
1012 // and
1013 //
1014 // Type existingItem;
1015 // for ( existingItem in expression ) { statements }
1016 //
1017 // becomes:
1018 //
Mike Stump31feda52009-07-17 01:31:16 +00001019 // the same with newVariable replaced with existingItem; the binding works
1020 // the same except that for one ObjCForCollectionStmt::getElement() returns
1021 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001022 //
Mike Stump31feda52009-07-17 01:31:16 +00001023
Ted Kremenek9d56e642008-11-11 17:10:00 +00001024 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001025
Ted Kremenek9d56e642008-11-11 17:10:00 +00001026 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001027 if (!FinishBlock(Block))
1028 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001029 LoopSuccessor = Block;
1030 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001031 } else
1032 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001034 // Build the condition blocks.
1035 CFGBlock* ExitConditionBlock = createBlock(false);
1036 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001037
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001038 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001039 ExitConditionBlock->setTerminator(S);
1040
1041 // The last statement in the block should be the ObjCForCollectionStmt, which
1042 // performs the actual binding to 'element' and determines if there are any
1043 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001044 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001045 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001046
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001047 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001048 // generate new blocks as necesary. We DON'T add the statement by default to
1049 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001050 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001051 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001052 if (!FinishBlock(EntryConditionBlock))
1053 return 0;
1054 Block = 0;
1055 }
Mike Stump31feda52009-07-17 01:31:16 +00001056
1057 // The condition block is the implicit successor for the loop body as well as
1058 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001059 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001060
Ted Kremenek9d56e642008-11-11 17:10:00 +00001061 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001062 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001063 // Save the current values for Succ, continue and break targets.
1064 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001065 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1066
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001067 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001068 ContinueTargetBlock = EntryConditionBlock;
1069
Ted Kremenek93668002009-07-17 22:18:43 +00001070 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001071
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001072 if (!BodyBlock)
1073 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001074 else if (Block) {
1075 if (!FinishBlock(BodyBlock))
1076 return 0;
1077 }
Mike Stump31feda52009-07-17 01:31:16 +00001078
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001079 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001080 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001081 }
Mike Stump31feda52009-07-17 01:31:16 +00001082
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001083 // Link up the condition block with the code that follows the loop.
1084 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001085 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001086
Ted Kremenek9d56e642008-11-11 17:10:00 +00001087 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001088 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001089 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001090}
1091
Ted Kremenek49805452009-05-02 01:49:13 +00001092CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1093 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001094
Ted Kremenek49805452009-05-02 01:49:13 +00001095 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001096 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001097
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001098 // The sync body starts its own basic block. This makes it a little easier
1099 // for diagnostic clients.
1100 if (SyncBlock) {
1101 if (!FinishBlock(SyncBlock))
1102 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001103
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001104 Block = 0;
1105 }
Mike Stump31feda52009-07-17 01:31:16 +00001106
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001107 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001108
Ted Kremenek49805452009-05-02 01:49:13 +00001109 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001110 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001111}
Mike Stump31feda52009-07-17 01:31:16 +00001112
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001113CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001114 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001115 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001116}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001117
Ted Kremenek9aae5132007-08-23 21:42:29 +00001118CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001119 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001120
Mike Stump014b3ea2009-07-21 01:12:51 +00001121 // "while" is a control-flow statement. Thus we stop processing the current
1122 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001123 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001124 if (!FinishBlock(Block))
1125 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001126 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001127 } else
1128 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001129
1130 // Because of short-circuit evaluation, the condition of the loop can span
1131 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1132 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001133 CFGBlock* ExitConditionBlock = createBlock(false);
1134 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001135
Ted Kremenek81e14852007-08-27 19:46:09 +00001136 // Set the terminator for the "exit" condition block.
1137 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001138
1139 // Now add the actual condition to the condition block. Because the condition
1140 // itself may contain control-flow, new blocks may be created. Thus we update
1141 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001142 if (Stmt* C = W->getCond()) {
1143 Block = ExitConditionBlock;
1144 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001145 assert(Block == EntryConditionBlock);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001146
1147 // If this block contains a condition variable, add both the condition
1148 // variable and initializer to the CFG.
1149 if (VarDecl *VD = W->getConditionVariable()) {
1150 if (Expr *Init = VD->getInit()) {
1151 autoCreateBlock();
1152 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1153 EntryConditionBlock = addStmt(Init);
1154 assert(Block == EntryConditionBlock);
1155 }
1156 }
1157
Ted Kremenek55957a82009-05-02 00:13:27 +00001158 if (Block) {
1159 if (!FinishBlock(EntryConditionBlock))
1160 return 0;
1161 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001162 }
Mike Stump31feda52009-07-17 01:31:16 +00001163
1164 // The condition block is the implicit successor for the loop body as well as
1165 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001166 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001167
Mike Stump773582d2009-07-23 23:25:26 +00001168 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001169 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001170
Ted Kremenek9aae5132007-08-23 21:42:29 +00001171 // Process the loop body.
1172 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001173 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001174
1175 // Save the current values for Block, Succ, and continue and break targets
1176 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1177 save_continue(ContinueTargetBlock),
1178 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001179
Mike Stump31feda52009-07-17 01:31:16 +00001180 // Create an empty block to represent the transition block for looping back
1181 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001182 Block = 0;
1183 assert(Succ == EntryConditionBlock);
1184 Succ = createBlock();
1185 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001186 ContinueTargetBlock = Succ;
1187
Ted Kremenek9aae5132007-08-23 21:42:29 +00001188 // All breaks should go to the code following the loop.
1189 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001190
Ted Kremenek9aae5132007-08-23 21:42:29 +00001191 // NULL out Block to force lazy instantiation of blocks for the body.
1192 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001193
Ted Kremenek9aae5132007-08-23 21:42:29 +00001194 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001195 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001196
Ted Kremeneke9610502007-08-30 18:39:40 +00001197 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001198 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001199 else if (Block) {
1200 if (!FinishBlock(BodyBlock))
1201 return 0;
1202 }
Mike Stump31feda52009-07-17 01:31:16 +00001203
Ted Kremenek30754282009-07-24 04:47:11 +00001204 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001205 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001206 }
Mike Stump31feda52009-07-17 01:31:16 +00001207
Ted Kremenek30754282009-07-24 04:47:11 +00001208 // Link up the condition block with the code that follows the loop. (the
1209 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001210 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001211
1212 // There can be no more statements in the condition block since we loop back
1213 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001214 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001215
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001216 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001217 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001218 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001219}
Mike Stump11289f42009-09-09 15:08:12 +00001220
1221
Ted Kremenek93668002009-07-17 22:18:43 +00001222CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1223 // FIXME: For now we pretend that @catch and the code it contains does not
1224 // exit.
1225 return Block;
1226}
Mike Stump31feda52009-07-17 01:31:16 +00001227
Ted Kremenek93041ba2008-12-09 20:20:09 +00001228CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1229 // FIXME: This isn't complete. We basically treat @throw like a return
1230 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001231
Ted Kremenek0868eea2009-09-24 18:45:41 +00001232 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001233 if (Block && !FinishBlock(Block))
1234 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001235
Ted Kremenek93041ba2008-12-09 20:20:09 +00001236 // Create the new block.
1237 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001238
Ted Kremenek93041ba2008-12-09 20:20:09 +00001239 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001240 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001241
1242 // Add the statement to the block. This may create new blocks if S contains
1243 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001244 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001245}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001246
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001247CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001248 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001249 if (Block && !FinishBlock(Block))
1250 return 0;
1251
1252 // Create the new block.
1253 Block = createBlock(false);
1254
1255 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001256 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001257
1258 // Add the statement to the block. This may create new blocks if S contains
1259 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001260 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001261}
1262
Ted Kremenek93668002009-07-17 22:18:43 +00001263CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001264 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001265
Mike Stump8d50b6a2009-07-21 01:27:50 +00001266 // "do...while" is a control-flow statement. Thus we stop processing the
1267 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001268 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001269 if (!FinishBlock(Block))
1270 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001271 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001272 } else
1273 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001274
1275 // Because of short-circuit evaluation, the condition of the loop can span
1276 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1277 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001278 CFGBlock* ExitConditionBlock = createBlock(false);
1279 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001280
Ted Kremenek81e14852007-08-27 19:46:09 +00001281 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001282 ExitConditionBlock->setTerminator(D);
1283
1284 // Now add the actual condition to the condition block. Because the condition
1285 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001286 if (Stmt* C = D->getCond()) {
1287 Block = ExitConditionBlock;
1288 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001289 if (Block) {
1290 if (!FinishBlock(EntryConditionBlock))
1291 return 0;
1292 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001293 }
Mike Stump31feda52009-07-17 01:31:16 +00001294
Ted Kremeneka1523a32008-02-27 07:20:00 +00001295 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001296 Succ = EntryConditionBlock;
1297
Mike Stump773582d2009-07-23 23:25:26 +00001298 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001299 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001300
Ted Kremenek9aae5132007-08-23 21:42:29 +00001301 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001302 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001303 {
1304 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001305
Ted Kremenek9aae5132007-08-23 21:42:29 +00001306 // Save the current values for Block, Succ, and continue and break targets
1307 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1308 save_continue(ContinueTargetBlock),
1309 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001310
Ted Kremenek9aae5132007-08-23 21:42:29 +00001311 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001312 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001313
Ted Kremenek9aae5132007-08-23 21:42:29 +00001314 // All breaks should go to the code following the loop.
1315 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001316
Ted Kremenek9aae5132007-08-23 21:42:29 +00001317 // NULL out Block to force lazy instantiation of blocks for the body.
1318 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001319
Ted Kremenek9aae5132007-08-23 21:42:29 +00001320 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001321 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001322
Ted Kremeneke9610502007-08-30 18:39:40 +00001323 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001324 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001325 else if (Block) {
1326 if (!FinishBlock(BodyBlock))
1327 return 0;
1328 }
Mike Stump31feda52009-07-17 01:31:16 +00001329
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001330 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001331 // ExitConditionBlock to represent the "loop back" transition. Create an
1332 // empty block to represent the transition block for looping back to the
1333 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001334 // FIXME: Can we do this more efficiently without adding another block?
1335 Block = NULL;
1336 Succ = BodyBlock;
1337 CFGBlock *LoopBackBlock = createBlock();
1338 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001339
Ted Kremenek30754282009-07-24 04:47:11 +00001340 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001341 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342 }
Mike Stump31feda52009-07-17 01:31:16 +00001343
Ted Kremenek30754282009-07-24 04:47:11 +00001344 // Link up the condition block with the code that follows the loop.
1345 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001346 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001347
1348 // There can be no more statements in the body block(s) since we loop back to
1349 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001350 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001353 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 return BodyBlock;
1355}
1356
1357CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1358 // "continue" is a control-flow statement. Thus we stop processing the
1359 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001360 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001361 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001362
Ted Kremenek9aae5132007-08-23 21:42:29 +00001363 // Now create a new block that ends with the continue statement.
1364 Block = createBlock(false);
1365 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001366
Ted Kremenek9aae5132007-08-23 21:42:29 +00001367 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001368 // incomplete AST. This means the CFG cannot be constructed.
1369 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001370 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001371 else
1372 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001373
Ted Kremenek9aae5132007-08-23 21:42:29 +00001374 return Block;
1375}
Mike Stump11289f42009-09-09 15:08:12 +00001376
Ted Kremenek0747de62009-07-18 00:47:21 +00001377CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001378 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001379
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001380 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001381 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001382 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001383 }
Mike Stump11289f42009-09-09 15:08:12 +00001384
Ted Kremenek93668002009-07-17 22:18:43 +00001385 // VLA types have expressions that must be evaluated.
1386 if (E->isArgumentType()) {
1387 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1388 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1389 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
Mike Stump31feda52009-07-17 01:31:16 +00001392 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001393}
Mike Stump11289f42009-09-09 15:08:12 +00001394
Ted Kremenek93668002009-07-17 22:18:43 +00001395/// VisitStmtExpr - Utility method to handle (nested) statement
1396/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001397CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1398 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001399 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001400 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001401 }
Ted Kremenek93668002009-07-17 22:18:43 +00001402 return VisitCompoundStmt(SE->getSubStmt());
1403}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001405CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001406 // "switch" is a control-flow statement. Thus we stop processing the current
1407 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001408 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001409
Ted Kremenek9aae5132007-08-23 21:42:29 +00001410 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001411 if (!FinishBlock(Block))
1412 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001413 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001414 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415
1416 // Save the current "switch" context.
1417 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001418 save_break(BreakTargetBlock),
1419 save_default(DefaultCaseBlock);
1420
Mike Stump31feda52009-07-17 01:31:16 +00001421 // Set the "default" case to be the block after the switch statement. If the
1422 // switch statement contains a "default:", this value will be overwritten with
1423 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001424 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001425
Ted Kremenek9aae5132007-08-23 21:42:29 +00001426 // Create a new block that will contain the switch statement.
1427 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001428
Ted Kremenek9aae5132007-08-23 21:42:29 +00001429 // Now process the switch body. The code after the switch is the implicit
1430 // successor.
1431 Succ = SwitchSuccessor;
1432 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001433
1434 // When visiting the body, the case statements should automatically get linked
1435 // up to the switch. We also don't keep a pointer to the body, since all
1436 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001437 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001438 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001439 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001440 if (Block) {
1441 if (!FinishBlock(BodyBlock))
1442 return 0;
1443 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001444
Mike Stump31feda52009-07-17 01:31:16 +00001445 // If we have no "default:" case, the default transition is to the code
1446 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001447 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001448
Ted Kremenek81e14852007-08-27 19:46:09 +00001449 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001450 SwitchTerminatedBlock->setTerminator(Terminator);
1451 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001452 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001453 Block = addStmt(Terminator->getCond());
1454
1455 // Finally, if the SwitchStmt contains a condition variable, add both the
1456 // SwitchStmt and the condition variable initialization to the CFG.
1457 if (VarDecl *VD = Terminator->getConditionVariable()) {
1458 if (Expr *Init = VD->getInit()) {
1459 autoCreateBlock();
1460 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1461 addStmt(Init);
1462 }
1463 }
1464
1465 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001466}
1467
Ted Kremenek93668002009-07-17 22:18:43 +00001468CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001469 // CaseStmts are essentially labels, so they are the first statement in a
1470 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001471
Ted Kremenek93668002009-07-17 22:18:43 +00001472 if (CS->getSubStmt())
1473 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001474
Ted Kremenek55e91e82007-08-30 18:48:11 +00001475 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001476 if (!CaseBlock)
1477 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001478
1479 // Cases statements partition blocks, so this is the top of the basic block we
1480 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001481 CaseBlock->setLabel(CS);
1482
Ted Kremenek55957a82009-05-02 00:13:27 +00001483 if (!FinishBlock(CaseBlock))
1484 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001485
1486 // Add this block to the list of successors for the block with the switch
1487 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001488 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001489 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001490
Ted Kremenek9aae5132007-08-23 21:42:29 +00001491 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1492 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001493
Ted Kremenek9aae5132007-08-23 21:42:29 +00001494 // This block is now the implicit successor of other blocks.
1495 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001496
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001497 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001498}
Mike Stump31feda52009-07-17 01:31:16 +00001499
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001500CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001501 if (Terminator->getSubStmt())
1502 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001503
Ted Kremenek654c78f2008-02-13 22:05:39 +00001504 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001505
1506 if (!DefaultCaseBlock)
1507 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001508
1509 // Default statements partition blocks, so this is the top of the basic block
1510 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001511 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001512
Ted Kremenek55957a82009-05-02 00:13:27 +00001513 if (!FinishBlock(DefaultCaseBlock))
1514 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001515
Mike Stump31feda52009-07-17 01:31:16 +00001516 // Unlike case statements, we don't add the default block to the successors
1517 // for the switch statement immediately. This is done when we finish
1518 // processing the switch statement. This allows for the default case
1519 // (including a fall-through to the code after the switch statement) to always
1520 // be the last successor of a switch-terminated block.
1521
Ted Kremenek654c78f2008-02-13 22:05:39 +00001522 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1523 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001524
Ted Kremenek654c78f2008-02-13 22:05:39 +00001525 // This block is now the implicit successor of other blocks.
1526 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001527
1528 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001529}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001530
Ted Kremenekeda180e22007-08-28 19:26:49 +00001531CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001532 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001533 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001534
Ted Kremenekeda180e22007-08-28 19:26:49 +00001535 if (!IBlock) {
1536 IBlock = createBlock(false);
1537 cfg->setIndirectGotoBlock(IBlock);
1538 }
Mike Stump31feda52009-07-17 01:31:16 +00001539
Ted Kremenekeda180e22007-08-28 19:26:49 +00001540 // IndirectGoto is a control-flow statement. Thus we stop processing the
1541 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001542 if (Block && !FinishBlock(Block))
1543 return 0;
1544
Ted Kremenekeda180e22007-08-28 19:26:49 +00001545 Block = createBlock(false);
1546 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001547 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001548 return addStmt(I->getTarget());
1549}
1550
Ted Kremenek04cca642007-08-23 21:26:19 +00001551} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001552
Mike Stump31feda52009-07-17 01:31:16 +00001553/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1554/// no successors or predecessors. If this is the first block created in the
1555/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001556CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001557 bool first_block = begin() == end();
1558
1559 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001560 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1561 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1562 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001563
1564 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001565 if (first_block)
1566 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001567
1568 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001569 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001570}
1571
Ted Kremenek889073f2007-08-23 16:51:22 +00001572/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1573/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001574CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001575 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001576 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001577}
1578
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001579//===----------------------------------------------------------------------===//
1580// CFG: Queries for BlkExprs.
1581//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001582
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001583namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001584 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001585}
1586
Ted Kremenekbff98442009-12-23 23:37:10 +00001587static void FindSubExprAssignments(Stmt *S,
1588 llvm::SmallPtrSet<Expr*,50>& Set) {
1589 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001590 return;
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenekbff98442009-12-23 23:37:10 +00001592 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1593 Stmt *child = *I;
1594 if (!child)
1595 continue;
1596
1597 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001598 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenekbff98442009-12-23 23:37:10 +00001600 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001601 }
1602}
1603
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001604static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1605 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001606
1607 // Look for assignments that are used as subexpressions. These are the only
1608 // assignments that we want to *possibly* register as a block-level
1609 // expression. Basically, if an assignment occurs both in a subexpression and
1610 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001611 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001612
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001613 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001614 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001615 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001616
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001617 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001618
1619 // Iterate over the statements again on identify the Expr* and Stmt* at the
1620 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001621
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001622 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001623 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001624
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001625 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001626 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001627 // expression are really "statements" whose value is never used by
1628 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001629 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001630 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001631 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1632 // Special handling for statement expressions. The last statement in
1633 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001634 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001635 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001636 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001637 (*M)[C->body_back()] = x;
1638 }
1639 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001640
Ted Kremenek95a123c2008-01-26 00:03:27 +00001641 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001642 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001643 }
Mike Stump31feda52009-07-17 01:31:16 +00001644
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001645 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001646
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001647 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001649 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001650 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001651 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001652 }
1653 }
Mike Stump31feda52009-07-17 01:31:16 +00001654
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001655 return M;
1656}
1657
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001658CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1659 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001660 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001661
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001662 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001663 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001664
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001665 if (I == M->end()) return CFG::BlkExprNumTy();
1666 else return CFG::BlkExprNumTy(I->second);
1667}
1668
1669unsigned CFG::getNumBlkExprs() {
1670 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1671 return M->size();
1672 else {
1673 // We assume callers interested in the number of BlkExprs will want
1674 // the map constructed if it doesn't already exist.
1675 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1676 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1677 }
1678}
1679
Ted Kremenek6065ef62008-04-28 18:00:46 +00001680//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001681// Cleanup: CFG dstor.
1682//===----------------------------------------------------------------------===//
1683
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001684CFG::~CFG() {
1685 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1686}
Mike Stump31feda52009-07-17 01:31:16 +00001687
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001688//===----------------------------------------------------------------------===//
1689// CFG pretty printing
1690//===----------------------------------------------------------------------===//
1691
Ted Kremenek7e776b12007-08-22 18:22:34 +00001692namespace {
1693
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001694class StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001695
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001696 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1697 StmtMapTy StmtMap;
1698 signed CurrentBlock;
1699 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001700 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001701public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001702
Chris Lattnerc61089a2009-06-30 01:26:17 +00001703 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1704 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001705 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1706 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001707 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001708 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001709 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001710 }
1711 }
Mike Stump31feda52009-07-17 01:31:16 +00001712
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001713 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001714
Chris Lattnerc61089a2009-06-30 01:26:17 +00001715 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001716 void setBlockID(signed i) { CurrentBlock = i; }
1717 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001718
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001719 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001720
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001721 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001722
1723 if (I == StmtMap.end())
1724 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001725
1726 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001727 && I->second.second == CurrentStmt)
1728 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001729
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001730 OS << "[B" << I->second.first << "." << I->second.second << "]";
1731 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001732 }
1733};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001734} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001735
Chris Lattnerc61089a2009-06-30 01:26:17 +00001736
1737namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001738class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001739 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001740
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001741 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001742 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001743 PrintingPolicy Policy;
1744
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001745public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001746 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001747 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001748 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001749
Ted Kremenek9aae5132007-08-23 21:42:29 +00001750 void VisitIfStmt(IfStmt* I) {
1751 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001752 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001753 }
Mike Stump31feda52009-07-17 01:31:16 +00001754
Ted Kremenek9aae5132007-08-23 21:42:29 +00001755 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001756 void VisitStmt(Stmt* Terminator) {
1757 Terminator->printPretty(OS, Helper, Policy);
1758 }
1759
Ted Kremenek9aae5132007-08-23 21:42:29 +00001760 void VisitForStmt(ForStmt* F) {
1761 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001762 if (F->getInit()) OS << "...";
1763 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001764 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001765 OS << "; ";
1766 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001767 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001768 }
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenek9aae5132007-08-23 21:42:29 +00001770 void VisitWhileStmt(WhileStmt* W) {
1771 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001772 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001773 }
Mike Stump31feda52009-07-17 01:31:16 +00001774
Ted Kremenek9aae5132007-08-23 21:42:29 +00001775 void VisitDoStmt(DoStmt* D) {
1776 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001777 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001778 }
Mike Stump31feda52009-07-17 01:31:16 +00001779
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001780 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001781 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001782 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001783 }
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001785 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001786 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001787 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001788 }
Mike Stump31feda52009-07-17 01:31:16 +00001789
Ted Kremenek391f94a2007-08-31 22:29:13 +00001790 void VisitChooseExpr(ChooseExpr* C) {
1791 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001792 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001793 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001794 }
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001796 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1797 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001798 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001799 }
Mike Stump31feda52009-07-17 01:31:16 +00001800
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001801 void VisitBinaryOperator(BinaryOperator* B) {
1802 if (!B->isLogicalOp()) {
1803 VisitExpr(B);
1804 return;
1805 }
Mike Stump31feda52009-07-17 01:31:16 +00001806
Douglas Gregor7de59662009-05-29 20:38:28 +00001807 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001808
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001809 switch (B->getOpcode()) {
1810 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001811 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001812 return;
1813 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001814 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001815 return;
1816 default:
1817 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001818 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001819 }
Mike Stump31feda52009-07-17 01:31:16 +00001820
Ted Kremenek12687ff2007-08-27 21:54:41 +00001821 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001822 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001823 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001824};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001825} // end anonymous namespace
1826
Mike Stump31feda52009-07-17 01:31:16 +00001827
Chris Lattnerc61089a2009-06-30 01:26:17 +00001828static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1829 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001830 if (Helper) {
1831 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001832 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001833 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001835 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001836 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001837 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001838 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001839 return;
1840 }
1841 }
Mike Stump31feda52009-07-17 01:31:16 +00001842
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001843 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001844 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001845 if (B->getOpcode() == BinaryOperator::Comma) {
1846 OS << "... , ";
1847 Helper->handledStmt(B->getRHS(),OS);
1848 OS << '\n';
1849 return;
Mike Stump31feda52009-07-17 01:31:16 +00001850 }
1851 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001852 }
Mike Stump31feda52009-07-17 01:31:16 +00001853
Chris Lattnerc61089a2009-06-30 01:26:17 +00001854 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001856 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001857 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001858}
Mike Stump31feda52009-07-17 01:31:16 +00001859
Chris Lattnerc61089a2009-06-30 01:26:17 +00001860static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1861 const CFGBlock& B,
1862 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001863
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001864 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001865
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001866 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001867 OS << "\n [ B" << B.getBlockID();
1868
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001869 if (&B == &cfg->getEntry())
1870 OS << " (ENTRY) ]\n";
1871 else if (&B == &cfg->getExit())
1872 OS << " (EXIT) ]\n";
1873 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001874 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001875 else
1876 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001877
Ted Kremenek71eca012007-08-29 23:20:49 +00001878 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001879 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001880
1881 if (print_edges)
1882 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001883
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001884 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001885 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001886 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001887 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001888 C->getLHS()->printPretty(OS, Helper,
1889 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001890 if (C->getRHS()) {
1891 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001892 C->getRHS()->printPretty(OS, Helper,
1893 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001894 }
Mike Stump31feda52009-07-17 01:31:16 +00001895 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001896 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001897 else
1898 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001899
Ted Kremenek71eca012007-08-29 23:20:49 +00001900 OS << ":\n";
1901 }
Mike Stump31feda52009-07-17 01:31:16 +00001902
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001903 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001904 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001905
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001906 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1907 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001908
Ted Kremenek71eca012007-08-29 23:20:49 +00001909 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001910 if (print_edges)
1911 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001912
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001913 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001915 if (Helper)
1916 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001917
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001918 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001919 }
Mike Stump31feda52009-07-17 01:31:16 +00001920
Ted Kremenek71eca012007-08-29 23:20:49 +00001921 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001922 if (B.getTerminator()) {
1923 if (print_edges)
1924 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001925
Ted Kremenek71eca012007-08-29 23:20:49 +00001926 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001927
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001928 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001929
Chris Lattnerc61089a2009-06-30 01:26:17 +00001930 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1931 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001932 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001933 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001934 }
Mike Stump31feda52009-07-17 01:31:16 +00001935
Ted Kremenek71eca012007-08-29 23:20:49 +00001936 if (print_edges) {
1937 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001938 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001939 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001940
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001941 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1942 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001943
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001944 if (i == 8 || (i-8) == 0)
1945 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001946
Ted Kremenek71eca012007-08-29 23:20:49 +00001947 OS << " B" << (*I)->getBlockID();
1948 }
Mike Stump31feda52009-07-17 01:31:16 +00001949
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001950 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001951
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001952 // Print the successors of this block.
1953 OS << " Successors (" << B.succ_size() << "):";
1954 i = 0;
1955
1956 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1957 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001958
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001959 if (i == 8 || (i-8) % 10 == 0)
1960 OS << "\n ";
1961
Mike Stump0d76d072009-07-20 23:24:15 +00001962 if (*I)
1963 OS << " B" << (*I)->getBlockID();
1964 else
1965 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001966 }
Mike Stump31feda52009-07-17 01:31:16 +00001967
Ted Kremenek71eca012007-08-29 23:20:49 +00001968 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001969 }
Mike Stump31feda52009-07-17 01:31:16 +00001970}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001971
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001972
1973/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001974void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001975
1976/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001977void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1978 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001979
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001980 // Print the entry block.
1981 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001982
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001983 // Iterate through the CFGBlocks and print them one by one.
1984 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1985 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001986 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001987 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001988
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001989 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001990 }
Mike Stump31feda52009-07-17 01:31:16 +00001991
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001992 // Print the exit block.
1993 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001994 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001995}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001996
1997/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001998void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1999 print(llvm::errs(), cfg, LO);
2000}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002001
2002/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2003/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002004void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2005 const LangOptions &LO) const {
2006 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002007 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002008}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002009
Ted Kremenek15647632008-01-30 23:02:42 +00002010/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002011void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002012 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002013 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002014 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2015}
2016
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002017Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002019 if (!Terminator)
2020 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002021
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002022 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002023
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002024 switch (Terminator->getStmtClass()) {
2025 default:
2026 break;
Mike Stump31feda52009-07-17 01:31:16 +00002027
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002028 case Stmt::ForStmtClass:
2029 E = cast<ForStmt>(Terminator)->getCond();
2030 break;
Mike Stump31feda52009-07-17 01:31:16 +00002031
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002032 case Stmt::WhileStmtClass:
2033 E = cast<WhileStmt>(Terminator)->getCond();
2034 break;
Mike Stump31feda52009-07-17 01:31:16 +00002035
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002036 case Stmt::DoStmtClass:
2037 E = cast<DoStmt>(Terminator)->getCond();
2038 break;
Mike Stump31feda52009-07-17 01:31:16 +00002039
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002040 case Stmt::IfStmtClass:
2041 E = cast<IfStmt>(Terminator)->getCond();
2042 break;
Mike Stump31feda52009-07-17 01:31:16 +00002043
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002044 case Stmt::ChooseExprClass:
2045 E = cast<ChooseExpr>(Terminator)->getCond();
2046 break;
Mike Stump31feda52009-07-17 01:31:16 +00002047
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002048 case Stmt::IndirectGotoStmtClass:
2049 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2050 break;
Mike Stump31feda52009-07-17 01:31:16 +00002051
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002052 case Stmt::SwitchStmtClass:
2053 E = cast<SwitchStmt>(Terminator)->getCond();
2054 break;
Mike Stump31feda52009-07-17 01:31:16 +00002055
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002056 case Stmt::ConditionalOperatorClass:
2057 E = cast<ConditionalOperator>(Terminator)->getCond();
2058 break;
Mike Stump31feda52009-07-17 01:31:16 +00002059
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002060 case Stmt::BinaryOperatorClass: // '&&' and '||'
2061 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002062 break;
Mike Stump31feda52009-07-17 01:31:16 +00002063
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002064 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002065 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002066 }
Mike Stump31feda52009-07-17 01:31:16 +00002067
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002068 return E ? E->IgnoreParens() : NULL;
2069}
2070
Ted Kremenek92137a32008-05-16 16:06:00 +00002071bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002072
Ted Kremenek92137a32008-05-16 16:06:00 +00002073 if (!Terminator)
2074 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002075
Ted Kremenek92137a32008-05-16 16:06:00 +00002076 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002077
Ted Kremenek92137a32008-05-16 16:06:00 +00002078 switch (Terminator->getStmtClass()) {
2079 default:
2080 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002081
2082 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002083 case Stmt::WhileStmtClass:
2084 case Stmt::DoStmtClass:
2085 case Stmt::IfStmtClass:
2086 case Stmt::ChooseExprClass:
2087 case Stmt::ConditionalOperatorClass:
2088 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002089 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002090 }
Mike Stump31feda52009-07-17 01:31:16 +00002091
Ted Kremenek92137a32008-05-16 16:06:00 +00002092 return E ? E->IgnoreParens() : NULL;
2093}
2094
Ted Kremenek15647632008-01-30 23:02:42 +00002095
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002096//===----------------------------------------------------------------------===//
2097// CFG Graphviz Visualization
2098//===----------------------------------------------------------------------===//
2099
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002100
2101#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002102static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002103#endif
2104
Chris Lattnerc61089a2009-06-30 01:26:17 +00002105void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002106#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002107 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002108 GraphHelper = &H;
2109 llvm::ViewGraph(this,"CFG");
2110 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002111#endif
2112}
2113
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002114namespace llvm {
2115template<>
2116struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002117
2118 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2119
2120 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002121
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002122#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002123 std::string OutSStr;
2124 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002125 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002126 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002127
2128 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2129
2130 // Process string output to make it nicer...
2131 for (unsigned i = 0; i != OutStr.length(); ++i)
2132 if (OutStr[i] == '\n') { // Left justify
2133 OutStr[i] = '\\';
2134 OutStr.insert(OutStr.begin()+i+1, 'l');
2135 }
Mike Stump31feda52009-07-17 01:31:16 +00002136
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002137 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002138#else
2139 return "";
2140#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002141 }
2142};
2143} // end namespace llvm