blob: 69aae746b15f70da22aff1706101fa3c2f11ee94 [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 Stumpbbf5ba62010-01-19 02:20:09 +000073 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +000074
Ted Kremenekeda180e22007-08-28 19:26:49 +000075 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000076 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
77 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000078
79 // A list of blocks that end with a "goto" that must be backpatched to their
80 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000081 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000082 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000083
Ted Kremenekeda180e22007-08-28 19:26:49 +000084 // A list of labels whose address has been taken (for indirect gotos).
85 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
86 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000087
88public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000089 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
90 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000091 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +000092 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
93 TryTerminatedBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +000094
Ted Kremenek9aae5132007-08-23 21:42:29 +000095 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000096 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000097
Ted Kremenek93668002009-07-17 22:18:43 +000098private:
99 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000100 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
101 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
102 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000103 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000104 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000105 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000106 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000107 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000108 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
109 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000110 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Ted Kremenek93668002009-07-17 22:18:43 +0000114 CFGBlock *VisitDeclStmt(DeclStmt *DS);
115 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000116 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
117 CFGBlock *VisitDoStmt(DoStmt *D);
118 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000119 CFGBlock *VisitGotoStmt(GotoStmt* G);
120 CFGBlock *VisitIfStmt(IfStmt *I);
121 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
122 CFGBlock *VisitLabelStmt(LabelStmt *L);
123 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
124 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
125 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
126 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
127 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
128 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000129 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
130 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000131 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
132 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000133
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000134 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
135 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000136 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000137
Ted Kremenek6065ef62008-04-28 18:00:46 +0000138 // NYS == Not Yet Supported
139 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000140 badCFG = true;
141 return Block;
142 }
Mike Stump31feda52009-07-17 01:31:16 +0000143
Ted Kremenek93668002009-07-17 22:18:43 +0000144 void autoCreateBlock() { if (!Block) Block = createBlock(); }
145 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000146 bool FinishBlock(CFGBlock* B);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000147 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
148 return Visit(S, asc);
149 }
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000150
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000151 void AppendStmt(CFGBlock *B, Stmt *S,
152 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
153 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000154 }
155
156 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
157 B->addSuccessor(S, cfg->getBumpVectorContext());
158 }
Mike Stump11289f42009-09-09 15:08:12 +0000159
Ted Kremenek963cc312009-07-24 06:55:42 +0000160 /// TryResult - a class representing a variant over the values
161 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
162 /// and is used by the CFGBuilder to decide if a branch condition
163 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000164 class TryResult {
165 int X;
166 public:
167 TryResult(bool b) : X(b ? 1 : 0) {}
168 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000169
Ted Kremenek30754282009-07-24 04:47:11 +0000170 bool isTrue() const { return X == 1; }
171 bool isFalse() const { return X == 0; }
172 bool isKnown() const { return X >= 0; }
173 void negate() {
174 assert(isKnown());
175 X ^= 0x1;
176 }
177 };
Mike Stump11289f42009-09-09 15:08:12 +0000178
Mike Stump773582d2009-07-23 23:25:26 +0000179 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
180 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000181 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000182 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000183 if (!S->isTypeDependent() && !S->isValueDependent() &&
184 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000185 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000186
187 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000188 }
189
Ted Kremenekb64d1832008-03-13 03:04:22 +0000190 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000191};
Mike Stump31feda52009-07-17 01:31:16 +0000192
Douglas Gregor4619e432008-12-05 23:32:09 +0000193// FIXME: Add support for dependent-sized array types in C++?
194// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000195static VariableArrayType* FindVA(Type* t) {
196 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
197 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
198 if (vat->getSizeExpr())
199 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000200
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000201 t = vt->getElementType().getTypePtr();
202 }
Mike Stump31feda52009-07-17 01:31:16 +0000203
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000204 return 0;
205}
Mike Stump31feda52009-07-17 01:31:16 +0000206
207/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
208/// arbitrary statement. Examples include a single expression or a function
209/// body (compound statement). The ownership of the returned CFG is
210/// transferred to the caller. If CFG construction fails, this method returns
211/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000212CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
213 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000214 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000215 if (!Statement)
216 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000217
Ted Kremenekb64d1832008-03-13 03:04:22 +0000218 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000219
220 // Create an empty block that will serve as the exit block for the CFG. Since
221 // this is the first block added to the CFG, it will be implicitly registered
222 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000223 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000224 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000225 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000226
Ted Kremenek9aae5132007-08-23 21:42:29 +0000227 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000228 CFGBlock* B = addStmt(Statement);
Ted Kremenek8aed4902009-10-20 23:46:25 +0000229 if (!B)
230 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000231
Ted Kremenek40d87632008-02-27 17:33:02 +0000232 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000233 // Finalize the last constructed block. This usually involves reversing the
234 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000235 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000236
237 // Backpatch the gotos whose label -> block mappings we didn't know when we
238 // encountered them.
239 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000240 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000241
Ted Kremenek9aae5132007-08-23 21:42:29 +0000242 CFGBlock* B = *I;
243 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
244 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
245
246 // If there is no target for the goto, then we are looking at an
247 // incomplete AST. Handle this by not registering a successor.
248 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000250 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000251 }
Mike Stump31feda52009-07-17 01:31:16 +0000252
Ted Kremenekeda180e22007-08-28 19:26:49 +0000253 // Add successors to the Indirect Goto Dispatch block (if we have one).
254 if (CFGBlock* B = cfg->getIndirectGotoBlock())
255 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
256 E = AddressTakenLabels.end(); I != E; ++I ) {
257
258 // Lookup the target block.
259 LabelMapTy::iterator LI = LabelMap.find(*I);
260
261 // If there is no target block that contains label, then we are looking
262 // at an incomplete AST. Handle this by not registering a successor.
263 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000264
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000265 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000266 }
Mike Stump31feda52009-07-17 01:31:16 +0000267
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000268 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000269 }
Mike Stump31feda52009-07-17 01:31:16 +0000270
271 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000272 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000273
Ted Kremenekab929bb2009-10-20 23:59:28 +0000274 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000275}
Mike Stump31feda52009-07-17 01:31:16 +0000276
Ted Kremenek9aae5132007-08-23 21:42:29 +0000277/// createBlock - Used to lazily create blocks that are connected
278/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000279CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000280 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000281 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000282 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000283 return B;
284}
Mike Stump31feda52009-07-17 01:31:16 +0000285
Ted Kremenek0868eea2009-09-24 18:45:41 +0000286/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000287bool CFGBuilder::FinishBlock(CFGBlock* B) {
288 if (badCFG)
289 return false;
290
Ted Kremenek93668002009-07-17 22:18:43 +0000291 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000292 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000293}
294
Ted Kremenek93668002009-07-17 22:18:43 +0000295/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000296/// blocks for ternary operators, &&, and ||. We also process "," and
297/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000298CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000299tryAgain:
300 switch (S->getStmtClass()) {
301 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000302 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000303
304 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000305 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000306
Ted Kremenek93668002009-07-17 22:18:43 +0000307 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000308 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000309
Ted Kremenek93668002009-07-17 22:18:43 +0000310 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000311 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000312
Ted Kremenek93668002009-07-17 22:18:43 +0000313 case Stmt::BreakStmtClass:
314 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenek93668002009-07-17 22:18:43 +0000316 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000317 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000318
Ted Kremenek93668002009-07-17 22:18:43 +0000319 case Stmt::CaseStmtClass:
320 return VisitCaseStmt(cast<CaseStmt>(S));
321
322 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000323 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenek93668002009-07-17 22:18:43 +0000325 case Stmt::CompoundStmtClass:
326 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremenek93668002009-07-17 22:18:43 +0000328 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000329 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Ted Kremenek93668002009-07-17 22:18:43 +0000331 case Stmt::ContinueStmtClass:
332 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenek93668002009-07-17 22:18:43 +0000334 case Stmt::DeclStmtClass:
335 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek93668002009-07-17 22:18:43 +0000337 case Stmt::DefaultStmtClass:
338 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000339
Ted Kremenek93668002009-07-17 22:18:43 +0000340 case Stmt::DoStmtClass:
341 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000342
Ted Kremenek93668002009-07-17 22:18:43 +0000343 case Stmt::ForStmtClass:
344 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000345
Ted Kremenek93668002009-07-17 22:18:43 +0000346 case Stmt::GotoStmtClass:
347 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000348
Ted Kremenek93668002009-07-17 22:18:43 +0000349 case Stmt::IfStmtClass:
350 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenek93668002009-07-17 22:18:43 +0000352 case Stmt::IndirectGotoStmtClass:
353 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenek93668002009-07-17 22:18:43 +0000355 case Stmt::LabelStmtClass:
356 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenek93668002009-07-17 22:18:43 +0000358 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000359 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
360
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000361 case Stmt::CXXThrowExprClass:
362 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
363
Ted Kremenek93668002009-07-17 22:18:43 +0000364 case Stmt::ObjCAtSynchronizedStmtClass:
365 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000366
Ted Kremenek93668002009-07-17 22:18:43 +0000367 case Stmt::ObjCAtThrowStmtClass:
368 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremenek93668002009-07-17 22:18:43 +0000370 case Stmt::ObjCAtTryStmtClass:
371 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000372
Ted Kremenek93668002009-07-17 22:18:43 +0000373 case Stmt::ObjCForCollectionStmtClass:
374 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek93668002009-07-17 22:18:43 +0000376 case Stmt::ParenExprClass:
377 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000378 goto tryAgain;
379
Ted Kremenek93668002009-07-17 22:18:43 +0000380 case Stmt::NullStmtClass:
381 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000382
Ted Kremenek93668002009-07-17 22:18:43 +0000383 case Stmt::ReturnStmtClass:
384 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek93668002009-07-17 22:18:43 +0000386 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000387 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek93668002009-07-17 22:18:43 +0000389 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000390 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000391
Ted Kremenek93668002009-07-17 22:18:43 +0000392 case Stmt::SwitchStmtClass:
393 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000394
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000395 case Stmt::CXXTryStmtClass:
396 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
397
398 case Stmt::CXXCatchStmtClass:
399 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
400
Ted Kremenek93668002009-07-17 22:18:43 +0000401 case Stmt::WhileStmtClass:
402 return VisitWhileStmt(cast<WhileStmt>(S));
403 }
404}
Mike Stump11289f42009-09-09 15:08:12 +0000405
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000406CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
407 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000408 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000409 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000410 }
Mike Stump11289f42009-09-09 15:08:12 +0000411
Ted Kremenek93668002009-07-17 22:18:43 +0000412 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000413}
Mike Stump31feda52009-07-17 01:31:16 +0000414
Ted Kremenek93668002009-07-17 22:18:43 +0000415/// VisitChildren - Visit the children of a Stmt.
416CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
417 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000418 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000419 E = Terminator->child_end(); I != E; ++I) {
420 if (*I) B = Visit(*I);
421 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000422 return B;
423}
Mike Stump11289f42009-09-09 15:08:12 +0000424
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000425CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
426 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000427 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000428
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000429 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000430 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000431 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000432 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000433
Ted Kremenek9aae5132007-08-23 21:42:29 +0000434 return Block;
435}
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000437CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
438 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000439 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000440 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000441 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000442
Ted Kremenek93668002009-07-17 22:18:43 +0000443 if (!FinishBlock(ConfluenceBlock))
444 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000445
Ted Kremenek93668002009-07-17 22:18:43 +0000446 // create the block evaluating the LHS
447 CFGBlock* LHSBlock = createBlock(false);
448 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenek93668002009-07-17 22:18:43 +0000450 // create the block evaluating the RHS
451 Succ = ConfluenceBlock;
452 Block = NULL;
453 CFGBlock* RHSBlock = addStmt(B->getRHS());
454 if (!FinishBlock(RHSBlock))
455 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000456
Mike Stump773582d2009-07-23 23:25:26 +0000457 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000458 TryResult KnownVal = TryEvaluateBool(B->getLHS());
459 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
460 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000461
Ted Kremenek93668002009-07-17 22:18:43 +0000462 // Now link the LHSBlock with RHSBlock.
463 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000464 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
465 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000466 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000467 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000468 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
469 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000470 }
Mike Stump11289f42009-09-09 15:08:12 +0000471
Ted Kremenek93668002009-07-17 22:18:43 +0000472 // Generate the blocks for evaluating the LHS.
473 Block = LHSBlock;
474 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000475 }
Ted Kremenek93668002009-07-17 22:18:43 +0000476 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000477 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000478 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000479 addStmt(B->getRHS());
480 return addStmt(B->getLHS());
481 }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000483 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000484}
485
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000486CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
487 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000488 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000489 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000490 }
491 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000492}
493
Ted Kremenek93668002009-07-17 22:18:43 +0000494CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
495 // "break" is a control-flow statement. Thus we stop processing the current
496 // block.
497 if (Block && !FinishBlock(Block))
498 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000499
Ted Kremenek93668002009-07-17 22:18:43 +0000500 // Now create a new block that ends with the break statement.
501 Block = createBlock(false);
502 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000503
Ted Kremenek93668002009-07-17 22:18:43 +0000504 // If there is no target for the break, then we are looking at an incomplete
505 // AST. This means that the CFG cannot be constructed.
506 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000507 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000508 else
509 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000510
511
Ted Kremenek9aae5132007-08-23 21:42:29 +0000512 return Block;
513}
Mike Stump11289f42009-09-09 15:08:12 +0000514
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000515CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000516 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000517 bool NoReturn = false;
518 if (C->getCallee()->getType().getNoReturnAttr()) {
519 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000520 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000521
522 if (FunctionDecl *FD = C->getDirectCallee())
523 if (FD->hasAttr<NoReturnAttr>())
524 NoReturn = true;
525
526 if (!NoReturn)
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000527 return VisitStmt(C, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000528
Mike Stump8c5d7992009-07-25 21:26:53 +0000529 if (Block && !FinishBlock(Block))
530 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Mike Stump8c5d7992009-07-25 21:26:53 +0000532 // Create new block with no successor for the remaining pieces.
533 Block = createBlock(false);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000534 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000535
536 // Wire this to the exit block directly.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000537 AddSuccessor(Block, &cfg->getExit());
Mike Stump11289f42009-09-09 15:08:12 +0000538
Mike Stump8c5d7992009-07-25 21:26:53 +0000539 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000540}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000541
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000542CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
543 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000544 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000545 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000546 if (!FinishBlock(ConfluenceBlock))
547 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Ted Kremenek21822592009-07-17 18:20:32 +0000549 Succ = ConfluenceBlock;
550 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000551 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000552 if (!FinishBlock(LHSBlock))
553 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000554
Ted Kremenek21822592009-07-17 18:20:32 +0000555 Succ = ConfluenceBlock;
556 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000557 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000558 if (!FinishBlock(RHSBlock))
559 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000560
Ted Kremenek21822592009-07-17 18:20:32 +0000561 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000562 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000563 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000564 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
565 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000566 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000567 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000568}
Mike Stump11289f42009-09-09 15:08:12 +0000569
570
571CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
572 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000573
574 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
575 I != E; ++I ) {
576 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenekce499c22009-08-27 23:16:26 +0000578 if (badCFG)
579 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000580 }
Ted Kremenek93668002009-07-17 22:18:43 +0000581 return LastBlock;
582}
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000584CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
585 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000586 // Create the confluence block that will "merge" the results of the ternary
587 // expression.
588 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000589 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000590 if (!FinishBlock(ConfluenceBlock))
591 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Ted Kremenek51d40b02009-07-17 18:15:54 +0000593 // Create a block for the LHS expression if there is an LHS expression. A
594 // GCC extension allows LHS to be NULL, causing the condition to be the
595 // value that is returned instead.
596 // e.g: x ?: y is shorthand for: x ? x : y;
597 Succ = ConfluenceBlock;
598 Block = NULL;
599 CFGBlock* LHSBlock = NULL;
600 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000601 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000602 if (!FinishBlock(LHSBlock))
603 return 0;
604 Block = NULL;
605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
Ted Kremenek51d40b02009-07-17 18:15:54 +0000607 // Create the block for the RHS expression.
608 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000609 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000610 if (!FinishBlock(RHSBlock))
611 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Ted Kremenek51d40b02009-07-17 18:15:54 +0000613 // Create the block that will contain the condition.
614 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000615
Mike Stump773582d2009-07-23 23:25:26 +0000616 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000617 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000618 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000619 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000620 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000621 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000622 // If we know the condition is false, add NULL as the successor for
623 // the block containing the condition. In this case, the confluence
624 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000625 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000626 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000627 } else {
628 // If we have no LHS expression, add the ConfluenceBlock as a direct
629 // successor for the block containing the condition. Moreover, we need to
630 // reverse the order of the predecessors in the ConfluenceBlock because
631 // the RHSBlock will have been added to the succcessors already, and we
632 // want the first predecessor to the the block containing the expression
633 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000634 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000635 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000636 std::reverse(ConfluenceBlock->pred_begin(),
637 ConfluenceBlock->pred_end());
638 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000641 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000642 Block->setTerminator(C);
643 return addStmt(C->getCond());
644}
645
Ted Kremenek93668002009-07-17 22:18:43 +0000646CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
647 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000648
Ted Kremenek93668002009-07-17 22:18:43 +0000649 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000650 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000651 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000652 }
Mike Stump11289f42009-09-09 15:08:12 +0000653
Ted Kremenek93668002009-07-17 22:18:43 +0000654 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000655
Ted Kremenek93668002009-07-17 22:18:43 +0000656 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
657 typedef llvm::SmallVector<Decl*,10> BufTy;
658 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000659
Ted Kremenek93668002009-07-17 22:18:43 +0000660 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
661 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
662 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
663 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000664
Ted Kremenek93668002009-07-17 22:18:43 +0000665 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
666 // automatically freed with the CFG.
667 DeclGroupRef DG(*I);
668 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000669 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000670 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000673 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000674 B = VisitDeclSubExpr(D);
675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
677 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000678}
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenek93668002009-07-17 22:18:43 +0000680/// VisitDeclSubExpr - Utility method to add block-level expressions for
681/// initializers in Decls.
682CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
683 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000684
Ted Kremenek93668002009-07-17 22:18:43 +0000685 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000686
Ted Kremenek93668002009-07-17 22:18:43 +0000687 if (!VD)
688 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenek93668002009-07-17 22:18:43 +0000690 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000691
Ted Kremenek93668002009-07-17 22:18:43 +0000692 if (Init) {
693 // Optimization: Don't create separate block-level statements for literals.
694 switch (Init->getStmtClass()) {
695 case Stmt::IntegerLiteralClass:
696 case Stmt::CharacterLiteralClass:
697 case Stmt::StringLiteralClass:
698 break;
699 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000700 Block = addStmt(Init,
701 VD->getType()->isReferenceType()
702 ? AddStmtChoice::AlwaysAddAsLValue
703 : AddStmtChoice::AlwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000704 }
705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Ted Kremenek93668002009-07-17 22:18:43 +0000707 // If the type of VD is a VLA, then we must process its size expressions.
708 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
709 VA = FindVA(VA->getElementType().getTypePtr()))
710 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000711
Ted Kremenek93668002009-07-17 22:18:43 +0000712 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000713}
714
715CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000716 // We may see an if statement in the middle of a basic block, or it may be the
717 // first statement we are processing. In either case, we create a new basic
718 // block. First, we create the blocks for the then...else statements, and
719 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000720 // middle of a block, we stop processing that block. That block is then the
721 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000722
723 // The block we were proccessing is now finished. Make it the successor
724 // block.
725 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000726 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000727 if (!FinishBlock(Block))
728 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000729 }
Mike Stump31feda52009-07-17 01:31:16 +0000730
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000731 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000732 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000733
Ted Kremenek9aae5132007-08-23 21:42:29 +0000734 if (Stmt* Else = I->getElse()) {
735 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000736
Ted Kremenek9aae5132007-08-23 21:42:29 +0000737 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000738 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000739 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000740 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000741
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000742 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
743 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000744 else if (Block) {
745 if (!FinishBlock(ElseBlock))
746 return 0;
747 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000748 }
Mike Stump31feda52009-07-17 01:31:16 +0000749
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000750 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000751 CFGBlock* ThenBlock;
752 {
753 Stmt* Then = I->getThen();
754 assert (Then);
755 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000756 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000757 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000758
Ted Kremenek1b379512009-04-01 03:52:47 +0000759 if (!ThenBlock) {
760 // We can reach here if the "then" body has all NullStmts.
761 // Create an empty block so we can distinguish between true and false
762 // branches in path-sensitive analyses.
763 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000764 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000765 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000766 if (!FinishBlock(ThenBlock))
767 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000768 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000769 }
770
Mike Stump31feda52009-07-17 01:31:16 +0000771 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000772 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000773
Ted Kremenek9aae5132007-08-23 21:42:29 +0000774 // Set the terminator of the new block to the If statement.
775 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000776
Mike Stump773582d2009-07-23 23:25:26 +0000777 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000778 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000779
Ted Kremenek9aae5132007-08-23 21:42:29 +0000780 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000781 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
782 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000783
784 // Add the condition as the last statement in the new block. This may create
785 // new blocks as the condition may contain control-flow. Any newly created
786 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000787 Block = addStmt(I->getCond());
788
789 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
790 // and the condition variable initialization to the CFG.
791 if (VarDecl *VD = I->getConditionVariable()) {
792 if (Expr *Init = VD->getInit()) {
793 autoCreateBlock();
794 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
795 addStmt(Init);
796 }
797 }
798
799 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000800}
Mike Stump31feda52009-07-17 01:31:16 +0000801
802
Ted Kremenek9aae5132007-08-23 21:42:29 +0000803CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000804 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000805 //
Mike Stump31feda52009-07-17 01:31:16 +0000806 // NOTE: If a "return" appears in the middle of a block, this means that the
807 // code afterwards is DEAD (unreachable). We still keep a basic block
808 // for that code; a simple "mark-and-sweep" from the entry block will be
809 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000810 if (Block)
811 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000812
813 // Create the new block.
814 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000815
Ted Kremenek9aae5132007-08-23 21:42:29 +0000816 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000817 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000818
819 // Add the return statement to the block. This may create new blocks if R
820 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000821 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000822}
823
824CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
825 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000826 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000827 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000828
Ted Kremenek93668002009-07-17 22:18:43 +0000829 if (!LabelBlock) // This can happen when the body is empty, i.e.
830 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000831
Ted Kremenek93668002009-07-17 22:18:43 +0000832 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000833 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000834
835 // Labels partition blocks, so this is the end of the basic block we were
836 // processing (L is the block's label). Because this is label (and we have
837 // already processed the substatement) there is no extra control-flow to worry
838 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000839 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000840 if (!FinishBlock(LabelBlock))
841 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000842
843 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000844 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000845
Ted Kremenek9aae5132007-08-23 21:42:29 +0000846 // This block is now the implicit successor of other blocks.
847 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000848
Ted Kremenek9aae5132007-08-23 21:42:29 +0000849 return LabelBlock;
850}
851
852CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000853 // Goto is a control-flow statement. Thus we stop processing the current
854 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000855 if (Block)
856 FinishBlock(Block);
857
Ted Kremenek9aae5132007-08-23 21:42:29 +0000858 Block = createBlock(false);
859 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000860
861 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000862 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000863
Ted Kremenek9aae5132007-08-23 21:42:29 +0000864 if (I == LabelMap.end())
865 // We will need to backpatch this block later.
866 BackpatchBlocks.push_back(Block);
867 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000868 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000869
Mike Stump31feda52009-07-17 01:31:16 +0000870 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000871}
872
873CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000874 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000875
Mike Stump014b3ea2009-07-21 01:12:51 +0000876 // "for" is a control-flow statement. Thus we stop processing the current
877 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000878 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000879 if (!FinishBlock(Block))
880 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000881 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000882 } else
883 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000884
885 // Because of short-circuit evaluation, the condition of the loop can span
886 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
887 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000888 CFGBlock* ExitConditionBlock = createBlock(false);
889 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000890
Ted Kremenek81e14852007-08-27 19:46:09 +0000891 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000892 ExitConditionBlock->setTerminator(F);
893
894 // Now add the actual condition to the condition block. Because the condition
895 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000896 if (Stmt* C = F->getCond()) {
897 Block = ExitConditionBlock;
898 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +0000899 assert(Block == EntryConditionBlock);
900
901 // If this block contains a condition variable, add both the condition
902 // variable and initializer to the CFG.
903 if (VarDecl *VD = F->getConditionVariable()) {
904 if (Expr *Init = VD->getInit()) {
905 autoCreateBlock();
906 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
907 EntryConditionBlock = addStmt(Init);
908 assert(Block == EntryConditionBlock);
909 }
910 }
911
Ted Kremenek55957a82009-05-02 00:13:27 +0000912 if (Block) {
913 if (!FinishBlock(EntryConditionBlock))
914 return 0;
915 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000916 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000917
Mike Stump31feda52009-07-17 01:31:16 +0000918 // The condition block is the implicit successor for the loop body as well as
919 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000920 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000921
Mike Stump773582d2009-07-23 23:25:26 +0000922 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000923 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +0000924
Mike Stump773582d2009-07-23 23:25:26 +0000925 if (F->getCond())
926 KnownVal = TryEvaluateBool(F->getCond());
927
Ted Kremenek9aae5132007-08-23 21:42:29 +0000928 // Now create the loop body.
929 {
930 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000931
Ted Kremenek9aae5132007-08-23 21:42:29 +0000932 // Save the current values for Block, Succ, and continue and break targets
933 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000934 save_continue(ContinueTargetBlock),
935 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000936
Ted Kremeneke9610502007-08-30 18:39:40 +0000937 // Create a new block to contain the (bottom) of the loop body.
938 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000939
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000940 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000941 // Generate increment code in its own basic block. This is the target of
942 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000943 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000944 } else {
945 // No increment code. Create a special, empty, block that is used as the
946 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000947 assert(Succ == EntryConditionBlock);
948 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000949 }
Mike Stump31feda52009-07-17 01:31:16 +0000950
Ted Kremenek902393b2009-04-28 00:51:56 +0000951 // Finish up the increment (or empty) block if it hasn't been already.
952 if (Block) {
953 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000954 if (!FinishBlock(Block))
955 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000956 Block = 0;
957 }
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek902393b2009-04-28 00:51:56 +0000959 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000960
Ted Kremenek902393b2009-04-28 00:51:56 +0000961 // The starting block for the loop increment is the block that should
962 // represent the 'loop target' for looping back to the start of the loop.
963 ContinueTargetBlock->setLoopTarget(F);
964
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000965 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000966 BreakTargetBlock = LoopSuccessor;
967
968 // Now populate the body block, and in the process create new blocks as we
969 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000970 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000971
972 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000973 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000974 else if (Block && !FinishBlock(BodyBlock))
975 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000976
Ted Kremenek30754282009-07-24 04:47:11 +0000977 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000978 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000979 }
Mike Stump31feda52009-07-17 01:31:16 +0000980
Ted Kremenek30754282009-07-24 04:47:11 +0000981 // Link up the condition block with the code that follows the loop. (the
982 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000983 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000984
Ted Kremenek9aae5132007-08-23 21:42:29 +0000985 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000986 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000987 if (Stmt* I = F->getInit()) {
988 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000989 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000990 } else {
991 // There is no loop initialization. We are thus basically a while loop.
992 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000993 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000994 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000995 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000996 }
997}
998
Ted Kremenek9d56e642008-11-11 17:10:00 +0000999CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1000 // Objective-C fast enumeration 'for' statements:
1001 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1002 //
1003 // for ( Type newVariable in collection_expression ) { statements }
1004 //
1005 // becomes:
1006 //
1007 // prologue:
1008 // 1. collection_expression
1009 // T. jump to loop_entry
1010 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001011 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001012 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1013 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1014 // TB:
1015 // statements
1016 // T. jump to loop_entry
1017 // FB:
1018 // what comes after
1019 //
1020 // and
1021 //
1022 // Type existingItem;
1023 // for ( existingItem in expression ) { statements }
1024 //
1025 // becomes:
1026 //
Mike Stump31feda52009-07-17 01:31:16 +00001027 // the same with newVariable replaced with existingItem; the binding works
1028 // the same except that for one ObjCForCollectionStmt::getElement() returns
1029 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001030 //
Mike Stump31feda52009-07-17 01:31:16 +00001031
Ted Kremenek9d56e642008-11-11 17:10:00 +00001032 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek9d56e642008-11-11 17:10:00 +00001034 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001035 if (!FinishBlock(Block))
1036 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001037 LoopSuccessor = Block;
1038 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001039 } else
1040 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001041
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001042 // Build the condition blocks.
1043 CFGBlock* ExitConditionBlock = createBlock(false);
1044 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001045
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001046 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001047 ExitConditionBlock->setTerminator(S);
1048
1049 // The last statement in the block should be the ObjCForCollectionStmt, which
1050 // performs the actual binding to 'element' and determines if there are any
1051 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001052 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001053 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001054
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001055 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001056 // generate new blocks as necesary. We DON'T add the statement by default to
1057 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001058 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001059 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001060 if (!FinishBlock(EntryConditionBlock))
1061 return 0;
1062 Block = 0;
1063 }
Mike Stump31feda52009-07-17 01:31:16 +00001064
1065 // The condition block is the implicit successor for the loop body as well as
1066 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001067 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001068
Ted Kremenek9d56e642008-11-11 17:10:00 +00001069 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001070 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001071 // Save the current values for Succ, continue and break targets.
1072 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001073 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1074
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001075 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001076 ContinueTargetBlock = EntryConditionBlock;
1077
Ted Kremenek93668002009-07-17 22:18:43 +00001078 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001079
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001080 if (!BodyBlock)
1081 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001082 else if (Block) {
1083 if (!FinishBlock(BodyBlock))
1084 return 0;
1085 }
Mike Stump31feda52009-07-17 01:31:16 +00001086
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001087 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001088 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001089 }
Mike Stump31feda52009-07-17 01:31:16 +00001090
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001091 // Link up the condition block with the code that follows the loop.
1092 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001093 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001094
Ted Kremenek9d56e642008-11-11 17:10:00 +00001095 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001096 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001097 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001098}
1099
Ted Kremenek49805452009-05-02 01:49:13 +00001100CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1101 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001102
Ted Kremenek49805452009-05-02 01:49:13 +00001103 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001104 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001105
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001106 // The sync body starts its own basic block. This makes it a little easier
1107 // for diagnostic clients.
1108 if (SyncBlock) {
1109 if (!FinishBlock(SyncBlock))
1110 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001111
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001112 Block = 0;
1113 }
Mike Stump31feda52009-07-17 01:31:16 +00001114
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001115 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001116
Ted Kremenek49805452009-05-02 01:49:13 +00001117 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001118 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001119}
Mike Stump31feda52009-07-17 01:31:16 +00001120
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001121CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001122 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001123 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001124}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001125
Ted Kremenek9aae5132007-08-23 21:42:29 +00001126CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001127 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001128
Mike Stump014b3ea2009-07-21 01:12:51 +00001129 // "while" is a control-flow statement. Thus we stop processing the current
1130 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001131 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001132 if (!FinishBlock(Block))
1133 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001134 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001135 } else
1136 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001137
1138 // Because of short-circuit evaluation, the condition of the loop can span
1139 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1140 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001141 CFGBlock* ExitConditionBlock = createBlock(false);
1142 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001143
Ted Kremenek81e14852007-08-27 19:46:09 +00001144 // Set the terminator for the "exit" condition block.
1145 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001146
1147 // Now add the actual condition to the condition block. Because the condition
1148 // itself may contain control-flow, new blocks may be created. Thus we update
1149 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001150 if (Stmt* C = W->getCond()) {
1151 Block = ExitConditionBlock;
1152 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001153 assert(Block == EntryConditionBlock);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001154
1155 // If this block contains a condition variable, add both the condition
1156 // variable and initializer to the CFG.
1157 if (VarDecl *VD = W->getConditionVariable()) {
1158 if (Expr *Init = VD->getInit()) {
1159 autoCreateBlock();
1160 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1161 EntryConditionBlock = addStmt(Init);
1162 assert(Block == EntryConditionBlock);
1163 }
1164 }
1165
Ted Kremenek55957a82009-05-02 00:13:27 +00001166 if (Block) {
1167 if (!FinishBlock(EntryConditionBlock))
1168 return 0;
1169 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001170 }
Mike Stump31feda52009-07-17 01:31:16 +00001171
1172 // The condition block is the implicit successor for the loop body as well as
1173 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001174 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001175
Mike Stump773582d2009-07-23 23:25:26 +00001176 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001177 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001178
Ted Kremenek9aae5132007-08-23 21:42:29 +00001179 // Process the loop body.
1180 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001181 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001182
1183 // Save the current values for Block, Succ, and continue and break targets
1184 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1185 save_continue(ContinueTargetBlock),
1186 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001187
Mike Stump31feda52009-07-17 01:31:16 +00001188 // Create an empty block to represent the transition block for looping back
1189 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001190 Block = 0;
1191 assert(Succ == EntryConditionBlock);
1192 Succ = createBlock();
1193 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001194 ContinueTargetBlock = Succ;
1195
Ted Kremenek9aae5132007-08-23 21:42:29 +00001196 // All breaks should go to the code following the loop.
1197 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001198
Ted Kremenek9aae5132007-08-23 21:42:29 +00001199 // NULL out Block to force lazy instantiation of blocks for the body.
1200 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001201
Ted Kremenek9aae5132007-08-23 21:42:29 +00001202 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001203 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001204
Ted Kremeneke9610502007-08-30 18:39:40 +00001205 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001206 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001207 else if (Block) {
1208 if (!FinishBlock(BodyBlock))
1209 return 0;
1210 }
Mike Stump31feda52009-07-17 01:31:16 +00001211
Ted Kremenek30754282009-07-24 04:47:11 +00001212 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001213 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001214 }
Mike Stump31feda52009-07-17 01:31:16 +00001215
Ted Kremenek30754282009-07-24 04:47:11 +00001216 // Link up the condition block with the code that follows the loop. (the
1217 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001218 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001219
1220 // There can be no more statements in the condition block since we loop back
1221 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001222 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001223
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001224 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001225 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001226 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001227}
Mike Stump11289f42009-09-09 15:08:12 +00001228
1229
Ted Kremenek93668002009-07-17 22:18:43 +00001230CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1231 // FIXME: For now we pretend that @catch and the code it contains does not
1232 // exit.
1233 return Block;
1234}
Mike Stump31feda52009-07-17 01:31:16 +00001235
Ted Kremenek93041ba2008-12-09 20:20:09 +00001236CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1237 // FIXME: This isn't complete. We basically treat @throw like a return
1238 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001239
Ted Kremenek0868eea2009-09-24 18:45:41 +00001240 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001241 if (Block && !FinishBlock(Block))
1242 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001243
Ted Kremenek93041ba2008-12-09 20:20:09 +00001244 // Create the new block.
1245 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001246
Ted Kremenek93041ba2008-12-09 20:20:09 +00001247 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001248 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001249
1250 // Add the statement to the block. This may create new blocks if S contains
1251 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001252 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001253}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001254
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001255CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001256 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001257 if (Block && !FinishBlock(Block))
1258 return 0;
1259
1260 // Create the new block.
1261 Block = createBlock(false);
1262
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001263 if (TryTerminatedBlock)
1264 // The current try statement is the only successor.
1265 AddSuccessor(Block, TryTerminatedBlock);
1266 else
1267 // otherwise the Exit block is the only successor.
1268 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001269
1270 // Add the statement to the block. This may create new blocks if S contains
1271 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001272 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001273}
1274
Ted Kremenek93668002009-07-17 22:18:43 +00001275CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001276 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001277
Mike Stump8d50b6a2009-07-21 01:27:50 +00001278 // "do...while" is a control-flow statement. Thus we stop processing the
1279 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001280 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001281 if (!FinishBlock(Block))
1282 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001283 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001284 } else
1285 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001286
1287 // Because of short-circuit evaluation, the condition of the loop can span
1288 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1289 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001290 CFGBlock* ExitConditionBlock = createBlock(false);
1291 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001292
Ted Kremenek81e14852007-08-27 19:46:09 +00001293 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001294 ExitConditionBlock->setTerminator(D);
1295
1296 // Now add the actual condition to the condition block. Because the condition
1297 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001298 if (Stmt* C = D->getCond()) {
1299 Block = ExitConditionBlock;
1300 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001301 if (Block) {
1302 if (!FinishBlock(EntryConditionBlock))
1303 return 0;
1304 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001305 }
Mike Stump31feda52009-07-17 01:31:16 +00001306
Ted Kremeneka1523a32008-02-27 07:20:00 +00001307 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001308 Succ = EntryConditionBlock;
1309
Mike Stump773582d2009-07-23 23:25:26 +00001310 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001311 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001312
Ted Kremenek9aae5132007-08-23 21:42:29 +00001313 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001314 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001315 {
1316 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001317
Ted Kremenek9aae5132007-08-23 21:42:29 +00001318 // Save the current values for Block, Succ, and continue and break targets
1319 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001320 save_continue(ContinueTargetBlock),
1321 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001322
Ted Kremenek9aae5132007-08-23 21:42:29 +00001323 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001324 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001325
Ted Kremenek9aae5132007-08-23 21:42:29 +00001326 // All breaks should go to the code following the loop.
1327 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001328
Ted Kremenek9aae5132007-08-23 21:42:29 +00001329 // NULL out Block to force lazy instantiation of blocks for the body.
1330 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001331
Ted Kremenek9aae5132007-08-23 21:42:29 +00001332 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001333 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001334
Ted Kremeneke9610502007-08-30 18:39:40 +00001335 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001336 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001337 else if (Block) {
1338 if (!FinishBlock(BodyBlock))
1339 return 0;
1340 }
Mike Stump31feda52009-07-17 01:31:16 +00001341
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001342 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001343 // ExitConditionBlock to represent the "loop back" transition. Create an
1344 // empty block to represent the transition block for looping back to the
1345 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001346 // FIXME: Can we do this more efficiently without adding another block?
1347 Block = NULL;
1348 Succ = BodyBlock;
1349 CFGBlock *LoopBackBlock = createBlock();
1350 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenek30754282009-07-24 04:47:11 +00001352 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001353 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 }
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek30754282009-07-24 04:47:11 +00001356 // Link up the condition block with the code that follows the loop.
1357 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001358 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001359
1360 // There can be no more statements in the body block(s) since we loop back to
1361 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001362 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001363
Ted Kremenek9aae5132007-08-23 21:42:29 +00001364 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001365 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001366 return BodyBlock;
1367}
1368
1369CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1370 // "continue" is a control-flow statement. Thus we stop processing the
1371 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001372 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001373 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001374
Ted Kremenek9aae5132007-08-23 21:42:29 +00001375 // Now create a new block that ends with the continue statement.
1376 Block = createBlock(false);
1377 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenek9aae5132007-08-23 21:42:29 +00001379 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001380 // incomplete AST. This means the CFG cannot be constructed.
1381 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001382 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001383 else
1384 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001385
Ted Kremenek9aae5132007-08-23 21:42:29 +00001386 return Block;
1387}
Mike Stump11289f42009-09-09 15:08:12 +00001388
Ted Kremenek0747de62009-07-18 00:47:21 +00001389CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001390 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001391
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001392 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001393 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001394 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001395 }
Mike Stump11289f42009-09-09 15:08:12 +00001396
Ted Kremenek93668002009-07-17 22:18:43 +00001397 // VLA types have expressions that must be evaluated.
1398 if (E->isArgumentType()) {
1399 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1400 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1401 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001402 }
Mike Stump11289f42009-09-09 15:08:12 +00001403
Mike Stump31feda52009-07-17 01:31:16 +00001404 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001405}
Mike Stump11289f42009-09-09 15:08:12 +00001406
Ted Kremenek93668002009-07-17 22:18:43 +00001407/// VisitStmtExpr - Utility method to handle (nested) statement
1408/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001409CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1410 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001411 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001412 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001413 }
Ted Kremenek93668002009-07-17 22:18:43 +00001414 return VisitCompoundStmt(SE->getSubStmt());
1415}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001416
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001417CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001418 // "switch" is a control-flow statement. Thus we stop processing the current
1419 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001420 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001421
Ted Kremenek9aae5132007-08-23 21:42:29 +00001422 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001423 if (!FinishBlock(Block))
1424 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001425 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001426 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427
1428 // Save the current "switch" context.
1429 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001430 save_break(BreakTargetBlock),
1431 save_default(DefaultCaseBlock);
1432
Mike Stump31feda52009-07-17 01:31:16 +00001433 // Set the "default" case to be the block after the switch statement. If the
1434 // switch statement contains a "default:", this value will be overwritten with
1435 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001436 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001437
Ted Kremenek9aae5132007-08-23 21:42:29 +00001438 // Create a new block that will contain the switch statement.
1439 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001440
Ted Kremenek9aae5132007-08-23 21:42:29 +00001441 // Now process the switch body. The code after the switch is the implicit
1442 // successor.
1443 Succ = SwitchSuccessor;
1444 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001445
1446 // When visiting the body, the case statements should automatically get linked
1447 // up to the switch. We also don't keep a pointer to the body, since all
1448 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001449 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001450 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001451 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001452 if (Block) {
1453 if (!FinishBlock(BodyBlock))
1454 return 0;
1455 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001456
Mike Stump31feda52009-07-17 01:31:16 +00001457 // If we have no "default:" case, the default transition is to the code
1458 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001459 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001460
Ted Kremenek81e14852007-08-27 19:46:09 +00001461 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001462 SwitchTerminatedBlock->setTerminator(Terminator);
1463 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001464 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001465 Block = addStmt(Terminator->getCond());
1466
1467 // Finally, if the SwitchStmt contains a condition variable, add both the
1468 // SwitchStmt and the condition variable initialization to the CFG.
1469 if (VarDecl *VD = Terminator->getConditionVariable()) {
1470 if (Expr *Init = VD->getInit()) {
1471 autoCreateBlock();
1472 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1473 addStmt(Init);
1474 }
1475 }
1476
1477 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001478}
1479
Ted Kremenek93668002009-07-17 22:18:43 +00001480CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001481 // CaseStmts are essentially labels, so they are the first statement in a
1482 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001483
Ted Kremenek93668002009-07-17 22:18:43 +00001484 if (CS->getSubStmt())
1485 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001486
Ted Kremenek55e91e82007-08-30 18:48:11 +00001487 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001488 if (!CaseBlock)
1489 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001490
1491 // Cases statements partition blocks, so this is the top of the basic block we
1492 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001493 CaseBlock->setLabel(CS);
1494
Ted Kremenek55957a82009-05-02 00:13:27 +00001495 if (!FinishBlock(CaseBlock))
1496 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001497
1498 // Add this block to the list of successors for the block with the switch
1499 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001500 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001501 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001502
Ted Kremenek9aae5132007-08-23 21:42:29 +00001503 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1504 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001505
Ted Kremenek9aae5132007-08-23 21:42:29 +00001506 // This block is now the implicit successor of other blocks.
1507 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001508
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001509 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001510}
Mike Stump31feda52009-07-17 01:31:16 +00001511
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001512CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001513 if (Terminator->getSubStmt())
1514 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001515
Ted Kremenek654c78f2008-02-13 22:05:39 +00001516 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001517
1518 if (!DefaultCaseBlock)
1519 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001520
1521 // Default statements partition blocks, so this is the top of the basic block
1522 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001523 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001524
Ted Kremenek55957a82009-05-02 00:13:27 +00001525 if (!FinishBlock(DefaultCaseBlock))
1526 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001527
Mike Stump31feda52009-07-17 01:31:16 +00001528 // Unlike case statements, we don't add the default block to the successors
1529 // for the switch statement immediately. This is done when we finish
1530 // processing the switch statement. This allows for the default case
1531 // (including a fall-through to the code after the switch statement) to always
1532 // be the last successor of a switch-terminated block.
1533
Ted Kremenek654c78f2008-02-13 22:05:39 +00001534 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1535 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001536
Ted Kremenek654c78f2008-02-13 22:05:39 +00001537 // This block is now the implicit successor of other blocks.
1538 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001539
1540 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001541}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001542
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001543CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1544 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1545 // current block.
1546 CFGBlock* TrySuccessor = NULL;
1547
1548 if (Block) {
1549 if (!FinishBlock(Block))
1550 return 0;
1551 TrySuccessor = Block;
1552 } else TrySuccessor = Succ;
1553
1554 // Save the current "try" context.
1555 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1556
1557 // Create a new block that will contain the try statement.
1558 TryTerminatedBlock = createBlock(false);
1559 // Add the terminator in the try block.
1560 TryTerminatedBlock->setTerminator(Terminator);
1561
1562 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1563 // The code after the try is the implicit successor.
1564 Succ = TrySuccessor;
1565 CXXCatchStmt *CS = Terminator->getHandler(h);
1566 Block = NULL;
1567 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1568 if (CatchBlock == 0)
1569 return 0;
1570 // Add this block to the list of successors for the block with the try
1571 // statement.
1572 AddSuccessor(TryTerminatedBlock, CatchBlock);
1573 }
1574
1575 // The code after the try is the implicit successor.
1576 Succ = TrySuccessor;
1577
1578 // When visiting the body, the case statements should automatically get linked
1579 // up to the try.
1580 assert (Terminator->getTryBlock() && "try must contain a non-NULL body");
1581 Block = NULL;
1582 CFGBlock *BodyBlock = addStmt(Terminator->getTryBlock());
1583
1584 Block = BodyBlock;
1585
1586 return Block;
1587}
1588
1589CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1590 // CXXCatchStmt are treated like labels, so they are the first statement in a
1591 // block.
1592
1593 if (CS->getHandlerBlock())
1594 addStmt(CS->getHandlerBlock());
1595
1596 CFGBlock* CatchBlock = Block;
1597 if (!CatchBlock)
1598 CatchBlock = createBlock();
1599
1600 CatchBlock->setLabel(CS);
1601
1602 if (!FinishBlock(CatchBlock))
1603 return 0;
1604
1605 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1606 Block = NULL;
1607
1608 return CatchBlock;
1609}
1610
Ted Kremenekeda180e22007-08-28 19:26:49 +00001611CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001612 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001613 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001614
Ted Kremenekeda180e22007-08-28 19:26:49 +00001615 if (!IBlock) {
1616 IBlock = createBlock(false);
1617 cfg->setIndirectGotoBlock(IBlock);
1618 }
Mike Stump31feda52009-07-17 01:31:16 +00001619
Ted Kremenekeda180e22007-08-28 19:26:49 +00001620 // IndirectGoto is a control-flow statement. Thus we stop processing the
1621 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001622 if (Block && !FinishBlock(Block))
1623 return 0;
1624
Ted Kremenekeda180e22007-08-28 19:26:49 +00001625 Block = createBlock(false);
1626 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001627 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001628 return addStmt(I->getTarget());
1629}
1630
Ted Kremenek04cca642007-08-23 21:26:19 +00001631} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001632
Mike Stump31feda52009-07-17 01:31:16 +00001633/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1634/// no successors or predecessors. If this is the first block created in the
1635/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001636CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001637 bool first_block = begin() == end();
1638
1639 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001640 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1641 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1642 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001643
1644 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001645 if (first_block)
1646 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001647
1648 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001649 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001650}
1651
Ted Kremenek889073f2007-08-23 16:51:22 +00001652/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1653/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001654CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001655 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001656 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001657}
1658
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001659//===----------------------------------------------------------------------===//
1660// CFG: Queries for BlkExprs.
1661//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001662
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001663namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001664 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001665}
1666
Ted Kremenekbff98442009-12-23 23:37:10 +00001667static void FindSubExprAssignments(Stmt *S,
1668 llvm::SmallPtrSet<Expr*,50>& Set) {
1669 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001670 return;
Mike Stump31feda52009-07-17 01:31:16 +00001671
Ted Kremenekbff98442009-12-23 23:37:10 +00001672 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1673 Stmt *child = *I;
1674 if (!child)
1675 continue;
1676
1677 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001678 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001679
Ted Kremenekbff98442009-12-23 23:37:10 +00001680 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001681 }
1682}
1683
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001684static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1685 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001686
1687 // Look for assignments that are used as subexpressions. These are the only
1688 // assignments that we want to *possibly* register as a block-level
1689 // expression. Basically, if an assignment occurs both in a subexpression and
1690 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001691 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001692
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001693 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001694 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001695 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001696
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001697 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001698
1699 // Iterate over the statements again on identify the Expr* and Stmt* at the
1700 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001701
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001702 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001703 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001705 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001706 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001707 // expression are really "statements" whose value is never used by
1708 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001709 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001710 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001711 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1712 // Special handling for statement expressions. The last statement in
1713 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001714 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001715 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001716 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001717 (*M)[C->body_back()] = x;
1718 }
1719 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001720
Ted Kremenek95a123c2008-01-26 00:03:27 +00001721 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001722 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001723 }
Mike Stump31feda52009-07-17 01:31:16 +00001724
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001725 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001726
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001727 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001728
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001729 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001730 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001731 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001732 }
1733 }
Mike Stump31feda52009-07-17 01:31:16 +00001734
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001735 return M;
1736}
1737
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001738CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1739 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001740 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001741
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001742 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001743 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001744
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001745 if (I == M->end()) return CFG::BlkExprNumTy();
1746 else return CFG::BlkExprNumTy(I->second);
1747}
1748
1749unsigned CFG::getNumBlkExprs() {
1750 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1751 return M->size();
1752 else {
1753 // We assume callers interested in the number of BlkExprs will want
1754 // the map constructed if it doesn't already exist.
1755 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1756 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1757 }
1758}
1759
Ted Kremenek6065ef62008-04-28 18:00:46 +00001760//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001761// Cleanup: CFG dstor.
1762//===----------------------------------------------------------------------===//
1763
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001764CFG::~CFG() {
1765 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1766}
Mike Stump31feda52009-07-17 01:31:16 +00001767
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001768//===----------------------------------------------------------------------===//
1769// CFG pretty printing
1770//===----------------------------------------------------------------------===//
1771
Ted Kremenek7e776b12007-08-22 18:22:34 +00001772namespace {
1773
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001774class StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001775
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001776 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1777 StmtMapTy StmtMap;
1778 signed CurrentBlock;
1779 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001780 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001781public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001782
Chris Lattnerc61089a2009-06-30 01:26:17 +00001783 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1784 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001785 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1786 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001787 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001788 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001789 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001790 }
1791 }
Mike Stump31feda52009-07-17 01:31:16 +00001792
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001793 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001794
Chris Lattnerc61089a2009-06-30 01:26:17 +00001795 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001796 void setBlockID(signed i) { CurrentBlock = i; }
1797 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001798
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001799 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001800
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001801 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001802
1803 if (I == StmtMap.end())
1804 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001805
1806 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001807 && I->second.second == CurrentStmt)
1808 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001809
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001810 OS << "[B" << I->second.first << "." << I->second.second << "]";
1811 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001812 }
1813};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001814} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001815
Chris Lattnerc61089a2009-06-30 01:26:17 +00001816
1817namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001818class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001819 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001820
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001821 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001822 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001823 PrintingPolicy Policy;
1824
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001825public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001826 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001827 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001828 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001829
Ted Kremenek9aae5132007-08-23 21:42:29 +00001830 void VisitIfStmt(IfStmt* I) {
1831 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001832 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001833 }
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenek9aae5132007-08-23 21:42:29 +00001835 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001836 void VisitStmt(Stmt* Terminator) {
1837 Terminator->printPretty(OS, Helper, Policy);
1838 }
1839
Ted Kremenek9aae5132007-08-23 21:42:29 +00001840 void VisitForStmt(ForStmt* F) {
1841 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001842 if (F->getInit()) OS << "...";
1843 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001844 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001845 OS << "; ";
1846 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001847 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001848 }
Mike Stump31feda52009-07-17 01:31:16 +00001849
Ted Kremenek9aae5132007-08-23 21:42:29 +00001850 void VisitWhileStmt(WhileStmt* W) {
1851 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001852 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001853 }
Mike Stump31feda52009-07-17 01:31:16 +00001854
Ted Kremenek9aae5132007-08-23 21:42:29 +00001855 void VisitDoStmt(DoStmt* D) {
1856 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001857 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001858 }
Mike Stump31feda52009-07-17 01:31:16 +00001859
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001860 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001861 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001862 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001863 }
Mike Stump31feda52009-07-17 01:31:16 +00001864
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001865 void VisitCXXTryStmt(CXXTryStmt* CS) {
1866 OS << "try ...";
1867 }
1868
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001869 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001870 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001871 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001872 }
Mike Stump31feda52009-07-17 01:31:16 +00001873
Ted Kremenek391f94a2007-08-31 22:29:13 +00001874 void VisitChooseExpr(ChooseExpr* C) {
1875 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001876 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001877 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001878 }
Mike Stump31feda52009-07-17 01:31:16 +00001879
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001880 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1881 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001882 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001883 }
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001885 void VisitBinaryOperator(BinaryOperator* B) {
1886 if (!B->isLogicalOp()) {
1887 VisitExpr(B);
1888 return;
1889 }
Mike Stump31feda52009-07-17 01:31:16 +00001890
Douglas Gregor7de59662009-05-29 20:38:28 +00001891 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001892
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001893 switch (B->getOpcode()) {
1894 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001895 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001896 return;
1897 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001898 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001899 return;
1900 default:
1901 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001902 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001903 }
Mike Stump31feda52009-07-17 01:31:16 +00001904
Ted Kremenek12687ff2007-08-27 21:54:41 +00001905 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001906 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001907 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001908};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001909} // end anonymous namespace
1910
Mike Stump31feda52009-07-17 01:31:16 +00001911
Chris Lattnerc61089a2009-06-30 01:26:17 +00001912static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1913 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001914 if (Helper) {
1915 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001916 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001917 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001919 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001920 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001921 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001922 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001923 return;
1924 }
1925 }
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001927 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001928 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001929 if (B->getOpcode() == BinaryOperator::Comma) {
1930 OS << "... , ";
1931 Helper->handledStmt(B->getRHS(),OS);
1932 OS << '\n';
1933 return;
Mike Stump31feda52009-07-17 01:31:16 +00001934 }
1935 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001936 }
Mike Stump31feda52009-07-17 01:31:16 +00001937
Chris Lattnerc61089a2009-06-30 01:26:17 +00001938 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001939
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001940 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001941 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001942}
Mike Stump31feda52009-07-17 01:31:16 +00001943
Chris Lattnerc61089a2009-06-30 01:26:17 +00001944static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1945 const CFGBlock& B,
1946 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001947
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001948 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001949
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001950 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001951 OS << "\n [ B" << B.getBlockID();
1952
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001953 if (&B == &cfg->getEntry())
1954 OS << " (ENTRY) ]\n";
1955 else if (&B == &cfg->getExit())
1956 OS << " (EXIT) ]\n";
1957 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001958 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001959 else
1960 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenek71eca012007-08-29 23:20:49 +00001962 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001963 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001964
1965 if (print_edges)
1966 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001967
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001968 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001969 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001970 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001971 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001972 C->getLHS()->printPretty(OS, Helper,
1973 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001974 if (C->getRHS()) {
1975 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001976 C->getRHS()->printPretty(OS, Helper,
1977 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001978 }
Mike Stump31feda52009-07-17 01:31:16 +00001979 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001980 OS << "default";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001981 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Terminator)) {
1982 OS << "catch (";
1983 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
1984 0);
1985 OS << ")";
1986
1987 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001988 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenek71eca012007-08-29 23:20:49 +00001990 OS << ":\n";
1991 }
Mike Stump31feda52009-07-17 01:31:16 +00001992
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001993 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001994 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001995
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001996 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1997 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenek71eca012007-08-29 23:20:49 +00001999 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002000 if (print_edges)
2001 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002002
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002003 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002004
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002005 if (Helper)
2006 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002007
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002008 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002009 }
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenek71eca012007-08-29 23:20:49 +00002011 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002012 if (B.getTerminator()) {
2013 if (print_edges)
2014 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenek71eca012007-08-29 23:20:49 +00002016 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002017
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002018 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002019
Chris Lattnerc61089a2009-06-30 01:26:17 +00002020 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2021 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002022 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002023 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002024 }
Mike Stump31feda52009-07-17 01:31:16 +00002025
Ted Kremenek71eca012007-08-29 23:20:49 +00002026 if (print_edges) {
2027 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002028 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002029 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002030
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002031 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2032 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002033
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002034 if (i == 8 || (i-8) == 0)
2035 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002036
Ted Kremenek71eca012007-08-29 23:20:49 +00002037 OS << " B" << (*I)->getBlockID();
2038 }
Mike Stump31feda52009-07-17 01:31:16 +00002039
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002040 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002041
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002042 // Print the successors of this block.
2043 OS << " Successors (" << B.succ_size() << "):";
2044 i = 0;
2045
2046 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2047 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002048
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002049 if (i == 8 || (i-8) % 10 == 0)
2050 OS << "\n ";
2051
Mike Stump0d76d072009-07-20 23:24:15 +00002052 if (*I)
2053 OS << " B" << (*I)->getBlockID();
2054 else
2055 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002056 }
Mike Stump31feda52009-07-17 01:31:16 +00002057
Ted Kremenek71eca012007-08-29 23:20:49 +00002058 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002059 }
Mike Stump31feda52009-07-17 01:31:16 +00002060}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002061
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002062
2063/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002064void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002065
2066/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002067void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2068 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002069
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002070 // Print the entry block.
2071 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002072
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002073 // Iterate through the CFGBlocks and print them one by one.
2074 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2075 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002076 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002077 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002078
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002079 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002080 }
Mike Stump31feda52009-07-17 01:31:16 +00002081
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002082 // Print the exit block.
2083 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002084 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002085}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002086
2087/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002088void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2089 print(llvm::errs(), cfg, LO);
2090}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002091
2092/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2093/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002094void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2095 const LangOptions &LO) const {
2096 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002097 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002098}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002099
Ted Kremenek15647632008-01-30 23:02:42 +00002100/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002101void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002102 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002103 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002104 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2105}
2106
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002107Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002108
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002109 if (!Terminator)
2110 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002111
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002112 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002113
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002114 switch (Terminator->getStmtClass()) {
2115 default:
2116 break;
Mike Stump31feda52009-07-17 01:31:16 +00002117
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002118 case Stmt::ForStmtClass:
2119 E = cast<ForStmt>(Terminator)->getCond();
2120 break;
Mike Stump31feda52009-07-17 01:31:16 +00002121
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002122 case Stmt::WhileStmtClass:
2123 E = cast<WhileStmt>(Terminator)->getCond();
2124 break;
Mike Stump31feda52009-07-17 01:31:16 +00002125
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002126 case Stmt::DoStmtClass:
2127 E = cast<DoStmt>(Terminator)->getCond();
2128 break;
Mike Stump31feda52009-07-17 01:31:16 +00002129
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002130 case Stmt::IfStmtClass:
2131 E = cast<IfStmt>(Terminator)->getCond();
2132 break;
Mike Stump31feda52009-07-17 01:31:16 +00002133
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002134 case Stmt::ChooseExprClass:
2135 E = cast<ChooseExpr>(Terminator)->getCond();
2136 break;
Mike Stump31feda52009-07-17 01:31:16 +00002137
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002138 case Stmt::IndirectGotoStmtClass:
2139 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2140 break;
Mike Stump31feda52009-07-17 01:31:16 +00002141
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002142 case Stmt::SwitchStmtClass:
2143 E = cast<SwitchStmt>(Terminator)->getCond();
2144 break;
Mike Stump31feda52009-07-17 01:31:16 +00002145
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002146 case Stmt::ConditionalOperatorClass:
2147 E = cast<ConditionalOperator>(Terminator)->getCond();
2148 break;
Mike Stump31feda52009-07-17 01:31:16 +00002149
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002150 case Stmt::BinaryOperatorClass: // '&&' and '||'
2151 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002152 break;
Mike Stump31feda52009-07-17 01:31:16 +00002153
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002154 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002155 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002156 }
Mike Stump31feda52009-07-17 01:31:16 +00002157
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002158 return E ? E->IgnoreParens() : NULL;
2159}
2160
Ted Kremenek92137a32008-05-16 16:06:00 +00002161bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002162
Ted Kremenek92137a32008-05-16 16:06:00 +00002163 if (!Terminator)
2164 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002165
Ted Kremenek92137a32008-05-16 16:06:00 +00002166 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002167
Ted Kremenek92137a32008-05-16 16:06:00 +00002168 switch (Terminator->getStmtClass()) {
2169 default:
2170 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002171
2172 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002173 case Stmt::WhileStmtClass:
2174 case Stmt::DoStmtClass:
2175 case Stmt::IfStmtClass:
2176 case Stmt::ChooseExprClass:
2177 case Stmt::ConditionalOperatorClass:
2178 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002179 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002180 }
Mike Stump31feda52009-07-17 01:31:16 +00002181
Ted Kremenek92137a32008-05-16 16:06:00 +00002182 return E ? E->IgnoreParens() : NULL;
2183}
2184
Ted Kremenek15647632008-01-30 23:02:42 +00002185
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002186//===----------------------------------------------------------------------===//
2187// CFG Graphviz Visualization
2188//===----------------------------------------------------------------------===//
2189
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002190
2191#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002192static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002193#endif
2194
Chris Lattnerc61089a2009-06-30 01:26:17 +00002195void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002196#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002197 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002198 GraphHelper = &H;
2199 llvm::ViewGraph(this,"CFG");
2200 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002201#endif
2202}
2203
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002204namespace llvm {
2205template<>
2206struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002207
2208 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2209
2210 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002211
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002212#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002213 std::string OutSStr;
2214 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002215 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002216 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002217
2218 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2219
2220 // Process string output to make it nicer...
2221 for (unsigned i = 0; i != OutStr.length(); ++i)
2222 if (OutStr[i] == '\n') { // Left justify
2223 OutStr[i] = '\\';
2224 OutStr.insert(OutStr.begin()+i+1, 'l');
2225 }
Mike Stump31feda52009-07-17 01:31:16 +00002226
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002227 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002228#else
2229 return "";
2230#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002231 }
2232};
2233} // end namespace llvm