blob: 08a13c4d8ed3f4cdc420e64ef6a2f719f23a5d64 [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"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor6e6ad602009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Mike Stump31feda52009-07-17 01:31:16 +000037
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000038/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000039/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;
45/// CFG* cfg = builder.BuildAST(stmt1);
46///
Mike Stump31feda52009-07-17 01:31:16 +000047/// CFG construction is done via a recursive walk of an AST. We actually parse
48/// the AST in reverse order so that the successor of a basic block is
49/// constructed prior to its predecessor. This allows us to nicely capture
50/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000051///
Ted Kremenek93668002009-07-17 22:18:43 +000052class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000053 ASTContext *Context;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000054 CFG* cfg;
55 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000057 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000058 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000059 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000060 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000061
Ted Kremenekeda180e22007-08-28 19:26:49 +000062 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000063 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
64 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000065
66 // A list of blocks that end with a "goto" that must be backpatched to their
67 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000068 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000069 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000070
Ted Kremenekeda180e22007-08-28 19:26:49 +000071 // A list of labels whose address has been taken (for indirect gotos).
72 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
73 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000074
75public:
Ted Kremenek889073f2007-08-23 16:51:22 +000076 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000077 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000078 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000079 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000080 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000081 }
Mike Stump31feda52009-07-17 01:31:16 +000082
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000083 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000084
Ted Kremenek9aae5132007-08-23 21:42:29 +000085 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000086 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000087
Ted Kremenek93668002009-07-17 22:18:43 +000088private:
89 // Visitors to walk an AST and construct the CFG.
90 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
91 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000092 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
93 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000094 CFGBlock *VisitBreakStmt(BreakStmt *B);
95 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
96 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000097 CFGBlock *VisitChooseExpr(ChooseExpr *C);
98 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
99 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
100 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000101 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +0000102 CFGBlock *VisitDeclStmt(DeclStmt *DS);
103 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000104 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
105 CFGBlock *VisitDoStmt(DoStmt *D);
106 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000107 CFGBlock *VisitGotoStmt(GotoStmt* G);
108 CFGBlock *VisitIfStmt(IfStmt *I);
109 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
110 CFGBlock *VisitLabelStmt(LabelStmt *L);
111 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
112 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
113 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
114 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
115 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
116 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000117 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
118 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000119 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
120 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000121
Ted Kremenek93668002009-07-17 22:18:43 +0000122 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
123 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
124 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000125
Ted Kremenek6065ef62008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
Mike Stump31feda52009-07-17 01:31:16 +0000131
Ted Kremenek93668002009-07-17 22:18:43 +0000132 void autoCreateBlock() { if (!Block) Block = createBlock(); }
133 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000134 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000135 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
136
Ted Kremenek963cc312009-07-24 06:55:42 +0000137
138 /// TryResult - a class representing a variant over the values
139 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
140 /// and is used by the CFGBuilder to decide if a branch condition
141 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000142 class TryResult {
143 int X;
144 public:
145 TryResult(bool b) : X(b ? 1 : 0) {}
146 TryResult() : X(-1) {}
147
148 bool isTrue() const { return X == 1; }
149 bool isFalse() const { return X == 0; }
150 bool isKnown() const { return X >= 0; }
151 void negate() {
152 assert(isKnown());
153 X ^= 0x1;
154 }
155 };
156
Mike Stump773582d2009-07-23 23:25:26 +0000157 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
158 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000159 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000160 Expr::EvalResult Result;
Ted Kremenek30754282009-07-24 04:47:11 +0000161 if (S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000162 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000163
164 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000165 }
166
Ted Kremenekb64d1832008-03-13 03:04:22 +0000167 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000168};
Mike Stump31feda52009-07-17 01:31:16 +0000169
Douglas Gregor4619e432008-12-05 23:32:09 +0000170// FIXME: Add support for dependent-sized array types in C++?
171// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000172static VariableArrayType* FindVA(Type* t) {
173 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
174 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
175 if (vat->getSizeExpr())
176 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000177
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000178 t = vt->getElementType().getTypePtr();
179 }
Mike Stump31feda52009-07-17 01:31:16 +0000180
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000181 return 0;
182}
Mike Stump31feda52009-07-17 01:31:16 +0000183
184/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
185/// arbitrary statement. Examples include a single expression or a function
186/// body (compound statement). The ownership of the returned CFG is
187/// transferred to the caller. If CFG construction fails, this method returns
188/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000189CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
190 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000191 assert(cfg);
192 if (!Statement)
193 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000194
Ted Kremenekb64d1832008-03-13 03:04:22 +0000195 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000196
197 // Create an empty block that will serve as the exit block for the CFG. Since
198 // this is the first block added to the CFG, it will be implicitly registered
199 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000200 Succ = createBlock();
201 assert (Succ == &cfg->getExit());
202 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000203
Ted Kremenek9aae5132007-08-23 21:42:29 +0000204 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000205 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000206 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000207
Ted Kremenek40d87632008-02-27 17:33:02 +0000208 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000209 // Finalize the last constructed block. This usually involves reversing the
210 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000211 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000212
213 // Backpatch the gotos whose label -> block mappings we didn't know when we
214 // encountered them.
215 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000216 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000217
Ted Kremenek9aae5132007-08-23 21:42:29 +0000218 CFGBlock* B = *I;
219 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
220 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
221
222 // If there is no target for the goto, then we are looking at an
223 // incomplete AST. Handle this by not registering a successor.
224 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000225
226 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000227 }
Mike Stump31feda52009-07-17 01:31:16 +0000228
Ted Kremenekeda180e22007-08-28 19:26:49 +0000229 // Add successors to the Indirect Goto Dispatch block (if we have one).
230 if (CFGBlock* B = cfg->getIndirectGotoBlock())
231 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
232 E = AddressTakenLabels.end(); I != E; ++I ) {
233
234 // Lookup the target block.
235 LabelMapTy::iterator LI = LabelMap.find(*I);
236
237 // If there is no target block that contains label, then we are looking
238 // at an incomplete AST. Handle this by not registering a successor.
239 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000240
241 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000242 }
Mike Stump31feda52009-07-17 01:31:16 +0000243
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000244 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000245 }
Mike Stump31feda52009-07-17 01:31:16 +0000246
247 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000248 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenekb64d1832008-03-13 03:04:22 +0000250 if (badCFG) {
251 delete cfg;
252 cfg = NULL;
253 return NULL;
254 }
Mike Stump31feda52009-07-17 01:31:16 +0000255
256 // NULL out cfg so that repeated calls to the builder will fail and that the
257 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000258 CFG* t = cfg;
259 cfg = NULL;
260 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000261}
Mike Stump31feda52009-07-17 01:31:16 +0000262
Ted Kremenek9aae5132007-08-23 21:42:29 +0000263/// createBlock - Used to lazily create blocks that are connected
264/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000265CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000266 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000267 if (add_successor && Succ)
268 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000269 return B;
270}
Mike Stump31feda52009-07-17 01:31:16 +0000271
272/// FinishBlock - When the last statement has been added to the block, we must
273/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000274bool CFGBuilder::FinishBlock(CFGBlock* B) {
275 if (badCFG)
276 return false;
277
Ted Kremenek93668002009-07-17 22:18:43 +0000278 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000279 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000280 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000281}
282
Ted Kremenek93668002009-07-17 22:18:43 +0000283/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000284/// blocks for ternary operators, &&, and ||. We also process "," and
285/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000286CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
287tryAgain:
288 switch (S->getStmtClass()) {
289 default:
290 return VisitStmt(S, alwaysAdd);
291
292 case Stmt::AddrLabelExprClass:
293 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
294
295 case Stmt::BinaryOperatorClass:
296 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
297
298 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000299 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000300
301 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000302 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000303
304 case Stmt::BreakStmtClass:
305 return VisitBreakStmt(cast<BreakStmt>(S));
306
307 case Stmt::CallExprClass:
308 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
309
310 case Stmt::CaseStmtClass:
311 return VisitCaseStmt(cast<CaseStmt>(S));
312
313 case Stmt::ChooseExprClass:
314 return VisitChooseExpr(cast<ChooseExpr>(S));
315
316 case Stmt::CompoundStmtClass:
317 return VisitCompoundStmt(cast<CompoundStmt>(S));
318
319 case Stmt::ConditionalOperatorClass:
320 return VisitConditionalOperator(cast<ConditionalOperator>(S));
321
322 case Stmt::ContinueStmtClass:
323 return VisitContinueStmt(cast<ContinueStmt>(S));
324
325 case Stmt::DeclStmtClass:
326 return VisitDeclStmt(cast<DeclStmt>(S));
327
328 case Stmt::DefaultStmtClass:
329 return VisitDefaultStmt(cast<DefaultStmt>(S));
330
331 case Stmt::DoStmtClass:
332 return VisitDoStmt(cast<DoStmt>(S));
333
334 case Stmt::ForStmtClass:
335 return VisitForStmt(cast<ForStmt>(S));
336
337 case Stmt::GotoStmtClass:
338 return VisitGotoStmt(cast<GotoStmt>(S));
339
340 case Stmt::IfStmtClass:
341 return VisitIfStmt(cast<IfStmt>(S));
342
343 case Stmt::IndirectGotoStmtClass:
344 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
345
346 case Stmt::LabelStmtClass:
347 return VisitLabelStmt(cast<LabelStmt>(S));
348
349 case Stmt::ObjCAtCatchStmtClass:
350 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
351
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000352 case Stmt::CXXThrowExprClass:
353 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
354
Ted Kremenek93668002009-07-17 22:18:43 +0000355 case Stmt::ObjCAtSynchronizedStmtClass:
356 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
357
358 case Stmt::ObjCAtThrowStmtClass:
359 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
360
361 case Stmt::ObjCAtTryStmtClass:
362 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
363
364 case Stmt::ObjCForCollectionStmtClass:
365 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
366
367 case Stmt::ParenExprClass:
368 S = cast<ParenExpr>(S)->getSubExpr();
369 goto tryAgain;
370
371 case Stmt::NullStmtClass:
372 return Block;
373
374 case Stmt::ReturnStmtClass:
375 return VisitReturnStmt(cast<ReturnStmt>(S));
376
377 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000378 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000379
380 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000381 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000382
383 case Stmt::SwitchStmtClass:
384 return VisitSwitchStmt(cast<SwitchStmt>(S));
385
386 case Stmt::WhileStmtClass:
387 return VisitWhileStmt(cast<WhileStmt>(S));
388 }
389}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000390
Ted Kremenek93668002009-07-17 22:18:43 +0000391CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
392 if (alwaysAdd) {
393 autoCreateBlock();
394 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000395 }
Ted Kremenek93668002009-07-17 22:18:43 +0000396
397 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000398}
Mike Stump31feda52009-07-17 01:31:16 +0000399
Ted Kremenek93668002009-07-17 22:18:43 +0000400/// VisitChildren - Visit the children of a Stmt.
401CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
402 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000403 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000404 E = Terminator->child_end(); I != E; ++I) {
405 if (*I) B = Visit(*I);
406 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000407 return B;
408}
Ted Kremenek93668002009-07-17 22:18:43 +0000409
410CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
411 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000412
Ted Kremenek93668002009-07-17 22:18:43 +0000413 if (alwaysAdd) {
414 autoCreateBlock();
415 Block->appendStmt(A);
416 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000417
Ted Kremenek9aae5132007-08-23 21:42:29 +0000418 return Block;
419}
Ted Kremenek93668002009-07-17 22:18:43 +0000420
421CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
422 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000423 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
424 ConfluenceBlock->appendStmt(B);
425
426 if (!FinishBlock(ConfluenceBlock))
427 return 0;
428
429 // create the block evaluating the LHS
430 CFGBlock* LHSBlock = createBlock(false);
431 LHSBlock->setTerminator(B);
432
433 // create the block evaluating the RHS
434 Succ = ConfluenceBlock;
435 Block = NULL;
436 CFGBlock* RHSBlock = addStmt(B->getRHS());
437 if (!FinishBlock(RHSBlock))
438 return 0;
439
Mike Stump773582d2009-07-23 23:25:26 +0000440 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000441 TryResult KnownVal = TryEvaluateBool(B->getLHS());
442 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
443 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000444
Ted Kremenek93668002009-07-17 22:18:43 +0000445 // Now link the LHSBlock with RHSBlock.
446 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek30754282009-07-24 04:47:11 +0000447 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
448 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
449 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000450 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek30754282009-07-24 04:47:11 +0000451 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
452 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000453 }
454
455 // Generate the blocks for evaluating the LHS.
456 Block = LHSBlock;
457 return addStmt(B->getLHS());
458 }
459 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000460 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000461 Block->appendStmt(B);
462 addStmt(B->getRHS());
463 return addStmt(B->getLHS());
464 }
465
466 return VisitStmt(B, alwaysAdd);
467}
468
Ted Kremenek0747de62009-07-18 00:47:21 +0000469CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000470 // FIXME
471 return NYS();
472}
473
Ted Kremenek0747de62009-07-18 00:47:21 +0000474CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
475 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000476 // FIXME
477 return NYS();
478}
479
480CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
481 // "break" is a control-flow statement. Thus we stop processing the current
482 // block.
483 if (Block && !FinishBlock(Block))
484 return 0;
485
486 // Now create a new block that ends with the break statement.
487 Block = createBlock(false);
488 Block->setTerminator(B);
489
490 // If there is no target for the break, then we are looking at an incomplete
491 // AST. This means that the CFG cannot be constructed.
492 if (BreakTargetBlock)
493 Block->addSuccessor(BreakTargetBlock);
494 else
495 badCFG = true;
496
497
Ted Kremenek9aae5132007-08-23 21:42:29 +0000498 return Block;
499}
Ted Kremenek93668002009-07-17 22:18:43 +0000500
501CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
502 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000503 bool NoReturn = false;
504 if (C->getCallee()->getType().getNoReturnAttr()) {
505 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000506 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000507
508 if (FunctionDecl *FD = C->getDirectCallee())
509 if (FD->hasAttr<NoReturnAttr>())
510 NoReturn = true;
511
512 if (!NoReturn)
513 return VisitStmt(C, alwaysAdd);
514
515 if (Block && !FinishBlock(Block))
516 return 0;
517
518 // Create new block with no successor for the remaining pieces.
519 Block = createBlock(false);
520 Block->appendStmt(C);
521
522 // Wire this to the exit block directly.
523 Block->addSuccessor(&cfg->getExit());
Ted Kremenek93668002009-07-17 22:18:43 +0000524
Mike Stump8c5d7992009-07-25 21:26:53 +0000525 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000526}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000527
Ted Kremenek21822592009-07-17 18:20:32 +0000528CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
529 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
530 ConfluenceBlock->appendStmt(C);
531 if (!FinishBlock(ConfluenceBlock))
532 return 0;
533
534 Succ = ConfluenceBlock;
535 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000536 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000537 if (!FinishBlock(LHSBlock))
538 return 0;
539
540 Succ = ConfluenceBlock;
541 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000542 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000543 if (!FinishBlock(RHSBlock))
544 return 0;
545
546 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000547 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000548 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
549 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
550 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000551 Block->setTerminator(C);
552 return addStmt(C->getCond());
553}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000554
Ted Kremenek93668002009-07-17 22:18:43 +0000555
556CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
557 CFGBlock* LastBlock = Block;
558
559 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
560 I != E; ++I ) {
561 LastBlock = addStmt(*I);
562 }
563 return LastBlock;
564}
565
Ted Kremenek51d40b02009-07-17 18:15:54 +0000566CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
567 // Create the confluence block that will "merge" the results of the ternary
568 // expression.
569 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
570 ConfluenceBlock->appendStmt(C);
571 if (!FinishBlock(ConfluenceBlock))
572 return 0;
573
574 // Create a block for the LHS expression if there is an LHS expression. A
575 // GCC extension allows LHS to be NULL, causing the condition to be the
576 // value that is returned instead.
577 // e.g: x ?: y is shorthand for: x ? x : y;
578 Succ = ConfluenceBlock;
579 Block = NULL;
580 CFGBlock* LHSBlock = NULL;
581 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000582 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000583 if (!FinishBlock(LHSBlock))
584 return 0;
585 Block = NULL;
586 }
587
588 // Create the block for the RHS expression.
589 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000590 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000591 if (!FinishBlock(RHSBlock))
592 return 0;
593
594 // Create the block that will contain the condition.
595 Block = createBlock(false);
596
Mike Stump773582d2009-07-23 23:25:26 +0000597 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000598 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000599 if (LHSBlock) {
Ted Kremenek30754282009-07-24 04:47:11 +0000600 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000601 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000602 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000603 // If we know the condition is false, add NULL as the successor for
604 // the block containing the condition. In this case, the confluence
605 // block will have just one predecessor.
606 Block->addSuccessor(0);
Ted Kremenek30754282009-07-24 04:47:11 +0000607 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000608 } else {
609 // If we have no LHS expression, add the ConfluenceBlock as a direct
610 // successor for the block containing the condition. Moreover, we need to
611 // reverse the order of the predecessors in the ConfluenceBlock because
612 // the RHSBlock will have been added to the succcessors already, and we
613 // want the first predecessor to the the block containing the expression
614 // for the case when the ternary expression evaluates to true.
615 Block->addSuccessor(ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000616 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000617 std::reverse(ConfluenceBlock->pred_begin(),
618 ConfluenceBlock->pred_end());
619 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000620 }
621
Ted Kremenek30754282009-07-24 04:47:11 +0000622 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000623 Block->setTerminator(C);
624 return addStmt(C->getCond());
625}
626
Ted Kremenek93668002009-07-17 22:18:43 +0000627CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
628 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000629
Ted Kremenek93668002009-07-17 22:18:43 +0000630 if (DS->isSingleDecl()) {
631 Block->appendStmt(DS);
632 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000633 }
Ted Kremenek93668002009-07-17 22:18:43 +0000634
635 CFGBlock *B = 0;
636
637 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
638 typedef llvm::SmallVector<Decl*,10> BufTy;
639 BufTy Buf(DS->decl_begin(), DS->decl_end());
640
641 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
642 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
643 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
644 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
645
646 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
647 // automatically freed with the CFG.
648 DeclGroupRef DG(*I);
649 Decl *D = *I;
650 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
651 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
652
653 // Append the fake DeclStmt to block.
654 Block->appendStmt(DSNew);
655 B = VisitDeclSubExpr(D);
656 }
657
658 return B;
659}
660
661/// VisitDeclSubExpr - Utility method to add block-level expressions for
662/// initializers in Decls.
663CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
664 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000665
Ted Kremenek93668002009-07-17 22:18:43 +0000666 VarDecl *VD = dyn_cast<VarDecl>(D);
667
668 if (!VD)
669 return Block;
670
671 Expr *Init = VD->getInit();
672
673 if (Init) {
674 // Optimization: Don't create separate block-level statements for literals.
675 switch (Init->getStmtClass()) {
676 case Stmt::IntegerLiteralClass:
677 case Stmt::CharacterLiteralClass:
678 case Stmt::StringLiteralClass:
679 break;
680 default:
681 Block = addStmt(Init);
682 }
683 }
684
685 // If the type of VD is a VLA, then we must process its size expressions.
686 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
687 VA = FindVA(VA->getElementType().getTypePtr()))
688 Block = addStmt(VA->getSizeExpr());
689
690 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000691}
692
693CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000694 // We may see an if statement in the middle of a basic block, or it may be the
695 // first statement we are processing. In either case, we create a new basic
696 // block. First, we create the blocks for the then...else statements, and
697 // then we create the block containing the if statement. If we were in the
698 // middle of a block, we stop processing that block and reverse its
699 // statements. That block is then the implicit successor for the "then" and
700 // "else" clauses.
701
702 // The block we were proccessing is now finished. Make it the successor
703 // block.
704 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000706 if (!FinishBlock(Block))
707 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000708 }
Mike Stump31feda52009-07-17 01:31:16 +0000709
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000710 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000711 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000712
Ted Kremenek9aae5132007-08-23 21:42:29 +0000713 if (Stmt* Else = I->getElse()) {
714 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000715
Ted Kremenek9aae5132007-08-23 21:42:29 +0000716 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000717 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000719 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000720
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000721 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
722 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000723 else if (Block) {
724 if (!FinishBlock(ElseBlock))
725 return 0;
726 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000727 }
Mike Stump31feda52009-07-17 01:31:16 +0000728
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000729 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000730 CFGBlock* ThenBlock;
731 {
732 Stmt* Then = I->getThen();
733 assert (Then);
734 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000735 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000736 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000737
Ted Kremenek1b379512009-04-01 03:52:47 +0000738 if (!ThenBlock) {
739 // We can reach here if the "then" body has all NullStmts.
740 // Create an empty block so we can distinguish between true and false
741 // branches in path-sensitive analyses.
742 ThenBlock = createBlock(false);
743 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000744 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000745 if (!FinishBlock(ThenBlock))
746 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000747 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000748 }
749
Mike Stump31feda52009-07-17 01:31:16 +0000750 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000751 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000752
Ted Kremenek9aae5132007-08-23 21:42:29 +0000753 // Set the terminator of the new block to the If statement.
754 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000755
Mike Stump773582d2009-07-23 23:25:26 +0000756 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000757 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000758
Ted Kremenek9aae5132007-08-23 21:42:29 +0000759 // Now add the successors.
Ted Kremenek30754282009-07-24 04:47:11 +0000760 Block->addSuccessor(KnownVal.isFalse() ? NULL : ThenBlock);
761 Block->addSuccessor(KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000762
763 // Add the condition as the last statement in the new block. This may create
764 // new blocks as the condition may contain control-flow. Any newly created
765 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000766 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000767}
Mike Stump31feda52009-07-17 01:31:16 +0000768
769
Ted Kremenek9aae5132007-08-23 21:42:29 +0000770CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000771 // If we were in the middle of a block we stop processing that block and
772 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000773 //
Mike Stump31feda52009-07-17 01:31:16 +0000774 // NOTE: If a "return" appears in the middle of a block, this means that the
775 // code afterwards is DEAD (unreachable). We still keep a basic block
776 // for that code; a simple "mark-and-sweep" from the entry block will be
777 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000778 if (Block) FinishBlock(Block);
779
780 // Create the new block.
781 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000782
Ted Kremenek9aae5132007-08-23 21:42:29 +0000783 // The Exit block is the only successor.
784 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000785
786 // Add the return statement to the block. This may create new blocks if R
787 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000788 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000789}
790
791CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
792 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000793 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000794 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000795
Ted Kremenek93668002009-07-17 22:18:43 +0000796 if (!LabelBlock) // This can happen when the body is empty, i.e.
797 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000798
Ted Kremenek93668002009-07-17 22:18:43 +0000799 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000800 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000801
802 // Labels partition blocks, so this is the end of the basic block we were
803 // processing (L is the block's label). Because this is label (and we have
804 // already processed the substatement) there is no extra control-flow to worry
805 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000806 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000807 if (!FinishBlock(LabelBlock))
808 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000809
810 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000811 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000812
Ted Kremenek9aae5132007-08-23 21:42:29 +0000813 // This block is now the implicit successor of other blocks.
814 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000815
Ted Kremenek9aae5132007-08-23 21:42:29 +0000816 return LabelBlock;
817}
818
819CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000820 // Goto is a control-flow statement. Thus we stop processing the current
821 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000822 if (Block)
823 FinishBlock(Block);
824
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 Block = createBlock(false);
826 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000827
828 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000829 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000830
Ted Kremenek9aae5132007-08-23 21:42:29 +0000831 if (I == LabelMap.end())
832 // We will need to backpatch this block later.
833 BackpatchBlocks.push_back(Block);
834 else
835 Block->addSuccessor(I->second);
836
Mike Stump31feda52009-07-17 01:31:16 +0000837 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000838}
839
840CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000842
Mike Stump014b3ea2009-07-21 01:12:51 +0000843 // "for" is a control-flow statement. Thus we stop processing the current
844 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000845 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000846 if (!FinishBlock(Block))
847 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000848 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000849 } else
850 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000851
852 // Because of short-circuit evaluation, the condition of the loop can span
853 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
854 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000855 CFGBlock* ExitConditionBlock = createBlock(false);
856 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000857
Ted Kremenek81e14852007-08-27 19:46:09 +0000858 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000859 ExitConditionBlock->setTerminator(F);
860
861 // Now add the actual condition to the condition block. Because the condition
862 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000863 if (Stmt* C = F->getCond()) {
864 Block = ExitConditionBlock;
865 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000866 if (Block) {
867 if (!FinishBlock(EntryConditionBlock))
868 return 0;
869 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000870 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000871
Mike Stump31feda52009-07-17 01:31:16 +0000872 // The condition block is the implicit successor for the loop body as well as
873 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000874 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000875
Mike Stump773582d2009-07-23 23:25:26 +0000876 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000877 TryResult KnownVal(true);
878
Mike Stump773582d2009-07-23 23:25:26 +0000879 if (F->getCond())
880 KnownVal = TryEvaluateBool(F->getCond());
881
Ted Kremenek9aae5132007-08-23 21:42:29 +0000882 // Now create the loop body.
883 {
884 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000885
Ted Kremenek9aae5132007-08-23 21:42:29 +0000886 // Save the current values for Block, Succ, and continue and break targets
887 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
888 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000889 save_break(BreakTargetBlock);
890
Ted Kremeneke9610502007-08-30 18:39:40 +0000891 // Create a new block to contain the (bottom) of the loop body.
892 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000893
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000894 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000895 // Generate increment code in its own basic block. This is the target of
896 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000897 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000898 } else {
899 // No increment code. Create a special, empty, block that is used as the
900 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000901 assert(Succ == EntryConditionBlock);
902 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000903 }
Mike Stump31feda52009-07-17 01:31:16 +0000904
Ted Kremenek902393b2009-04-28 00:51:56 +0000905 // Finish up the increment (or empty) block if it hasn't been already.
906 if (Block) {
907 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000908 if (!FinishBlock(Block))
909 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000910 Block = 0;
911 }
Mike Stump31feda52009-07-17 01:31:16 +0000912
Ted Kremenek902393b2009-04-28 00:51:56 +0000913 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000914
Ted Kremenek902393b2009-04-28 00:51:56 +0000915 // The starting block for the loop increment is the block that should
916 // represent the 'loop target' for looping back to the start of the loop.
917 ContinueTargetBlock->setLoopTarget(F);
918
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000919 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000920 BreakTargetBlock = LoopSuccessor;
921
922 // Now populate the body block, and in the process create new blocks as we
923 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000924 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000925
926 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +0000927 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000928 else if (Block && !FinishBlock(BodyBlock))
929 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000930
Ted Kremenek30754282009-07-24 04:47:11 +0000931 // This new body block is a successor to our "exit" condition block.
932 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000933 }
Mike Stump31feda52009-07-17 01:31:16 +0000934
Ted Kremenek30754282009-07-24 04:47:11 +0000935 // Link up the condition block with the code that follows the loop. (the
936 // false branch).
937 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000938
Ted Kremenek9aae5132007-08-23 21:42:29 +0000939 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000940 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000941 if (Stmt* I = F->getInit()) {
942 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000943 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000944 } else {
945 // There is no loop initialization. We are thus basically a while loop.
946 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000947 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000948 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000949 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000950 }
951}
952
Ted Kremenek9d56e642008-11-11 17:10:00 +0000953CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
954 // Objective-C fast enumeration 'for' statements:
955 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
956 //
957 // for ( Type newVariable in collection_expression ) { statements }
958 //
959 // becomes:
960 //
961 // prologue:
962 // 1. collection_expression
963 // T. jump to loop_entry
964 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000965 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000966 // 1. ObjCForCollectionStmt [performs binding to newVariable]
967 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
968 // TB:
969 // statements
970 // T. jump to loop_entry
971 // FB:
972 // what comes after
973 //
974 // and
975 //
976 // Type existingItem;
977 // for ( existingItem in expression ) { statements }
978 //
979 // becomes:
980 //
Mike Stump31feda52009-07-17 01:31:16 +0000981 // the same with newVariable replaced with existingItem; the binding works
982 // the same except that for one ObjCForCollectionStmt::getElement() returns
983 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000984 //
Mike Stump31feda52009-07-17 01:31:16 +0000985
Ted Kremenek9d56e642008-11-11 17:10:00 +0000986 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000987
Ted Kremenek9d56e642008-11-11 17:10:00 +0000988 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000989 if (!FinishBlock(Block))
990 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000991 LoopSuccessor = Block;
992 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000993 } else
994 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000995
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000996 // Build the condition blocks.
997 CFGBlock* ExitConditionBlock = createBlock(false);
998 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000999
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001000 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001001 ExitConditionBlock->setTerminator(S);
1002
1003 // The last statement in the block should be the ObjCForCollectionStmt, which
1004 // performs the actual binding to 'element' and determines if there are any
1005 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001006 ExitConditionBlock->appendStmt(S);
1007 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001008
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001009 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001010 // generate new blocks as necesary. We DON'T add the statement by default to
1011 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001012 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001013 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001014 if (!FinishBlock(EntryConditionBlock))
1015 return 0;
1016 Block = 0;
1017 }
Mike Stump31feda52009-07-17 01:31:16 +00001018
1019 // The condition block is the implicit successor for the loop body as well as
1020 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001021 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001022
Ted Kremenek9d56e642008-11-11 17:10:00 +00001023 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001024 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001025 // Save the current values for Succ, continue and break targets.
1026 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001027 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1028
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001029 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001030 ContinueTargetBlock = EntryConditionBlock;
1031
Ted Kremenek93668002009-07-17 22:18:43 +00001032 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001034 if (!BodyBlock)
1035 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001036 else if (Block) {
1037 if (!FinishBlock(BodyBlock))
1038 return 0;
1039 }
Mike Stump31feda52009-07-17 01:31:16 +00001040
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001041 // This new body block is a successor to our "exit" condition block.
1042 ExitConditionBlock->addSuccessor(BodyBlock);
1043 }
Mike Stump31feda52009-07-17 01:31:16 +00001044
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001045 // Link up the condition block with the code that follows the loop.
1046 // (the false branch).
1047 ExitConditionBlock->addSuccessor(LoopSuccessor);
1048
Ted Kremenek9d56e642008-11-11 17:10:00 +00001049 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001051 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001052}
1053
Ted Kremenek49805452009-05-02 01:49:13 +00001054CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1055 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001056
Ted Kremenek49805452009-05-02 01:49:13 +00001057 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001058 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001059
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001060 // The sync body starts its own basic block. This makes it a little easier
1061 // for diagnostic clients.
1062 if (SyncBlock) {
1063 if (!FinishBlock(SyncBlock))
1064 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001065
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001066 Block = 0;
1067 }
Mike Stump31feda52009-07-17 01:31:16 +00001068
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001069 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001070
Ted Kremenek49805452009-05-02 01:49:13 +00001071 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001072 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001073}
Mike Stump31feda52009-07-17 01:31:16 +00001074
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001075CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001076 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001077 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001078}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001079
Ted Kremenek9aae5132007-08-23 21:42:29 +00001080CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001081 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001082
Mike Stump014b3ea2009-07-21 01:12:51 +00001083 // "while" is a control-flow statement. Thus we stop processing the current
1084 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001085 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001086 if (!FinishBlock(Block))
1087 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001088 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001089 } else
1090 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001091
1092 // Because of short-circuit evaluation, the condition of the loop can span
1093 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1094 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001095 CFGBlock* ExitConditionBlock = createBlock(false);
1096 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001097
Ted Kremenek81e14852007-08-27 19:46:09 +00001098 // Set the terminator for the "exit" condition block.
1099 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001100
1101 // Now add the actual condition to the condition block. Because the condition
1102 // itself may contain control-flow, new blocks may be created. Thus we update
1103 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001104 if (Stmt* C = W->getCond()) {
1105 Block = ExitConditionBlock;
1106 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001107 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001108 if (Block) {
1109 if (!FinishBlock(EntryConditionBlock))
1110 return 0;
1111 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001112 }
Mike Stump31feda52009-07-17 01:31:16 +00001113
1114 // The condition block is the implicit successor for the loop body as well as
1115 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001116 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001117
Mike Stump773582d2009-07-23 23:25:26 +00001118 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001119 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001120
Ted Kremenek9aae5132007-08-23 21:42:29 +00001121 // Process the loop body.
1122 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001123 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001124
1125 // Save the current values for Block, Succ, and continue and break targets
1126 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1127 save_continue(ContinueTargetBlock),
1128 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001129
Mike Stump31feda52009-07-17 01:31:16 +00001130 // Create an empty block to represent the transition block for looping back
1131 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001132 Block = 0;
1133 assert(Succ == EntryConditionBlock);
1134 Succ = createBlock();
1135 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001136 ContinueTargetBlock = Succ;
1137
Ted Kremenek9aae5132007-08-23 21:42:29 +00001138 // All breaks should go to the code following the loop.
1139 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001140
Ted Kremenek9aae5132007-08-23 21:42:29 +00001141 // NULL out Block to force lazy instantiation of blocks for the body.
1142 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001143
Ted Kremenek9aae5132007-08-23 21:42:29 +00001144 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001145 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremeneke9610502007-08-30 18:39:40 +00001147 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001148 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001149 else if (Block) {
1150 if (!FinishBlock(BodyBlock))
1151 return 0;
1152 }
Mike Stump31feda52009-07-17 01:31:16 +00001153
Ted Kremenek30754282009-07-24 04:47:11 +00001154 // Add the loop body entry as a successor to the condition.
1155 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001156 }
Mike Stump31feda52009-07-17 01:31:16 +00001157
Ted Kremenek30754282009-07-24 04:47:11 +00001158 // Link up the condition block with the code that follows the loop. (the
1159 // false branch).
1160 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001161
1162 // There can be no more statements in the condition block since we loop back
1163 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001164 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001165
Ted Kremenek9aae5132007-08-23 21:42:29 +00001166 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001167 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001168 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001169}
Ted Kremenek93668002009-07-17 22:18:43 +00001170
1171
1172CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1173 // FIXME: For now we pretend that @catch and the code it contains does not
1174 // exit.
1175 return Block;
1176}
Mike Stump31feda52009-07-17 01:31:16 +00001177
Ted Kremenek93041ba2008-12-09 20:20:09 +00001178CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1179 // FIXME: This isn't complete. We basically treat @throw like a return
1180 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001181
1182 // If we were in the middle of a block we stop processing that block and
1183 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001184 if (Block && !FinishBlock(Block))
1185 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001186
Ted Kremenek93041ba2008-12-09 20:20:09 +00001187 // Create the new block.
1188 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001189
Ted Kremenek93041ba2008-12-09 20:20:09 +00001190 // The Exit block is the only successor.
1191 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001192
1193 // Add the statement to the block. This may create new blocks if S contains
1194 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001195 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001196}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001197
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001198CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1199 // If we were in the middle of a block we stop processing that block and
1200 // reverse its statements.
1201 if (Block && !FinishBlock(Block))
1202 return 0;
1203
1204 // Create the new block.
1205 Block = createBlock(false);
1206
1207 // The Exit block is the only successor.
1208 Block->addSuccessor(&cfg->getExit());
1209
1210 // Add the statement to the block. This may create new blocks if S contains
1211 // control-flow (short-circuit operations).
1212 return VisitStmt(T, true);
1213}
1214
Ted Kremenek93668002009-07-17 22:18:43 +00001215CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001216 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001217
Mike Stump8d50b6a2009-07-21 01:27:50 +00001218 // "do...while" is a control-flow statement. Thus we stop processing the
1219 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001220 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001221 if (!FinishBlock(Block))
1222 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001223 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001224 } else
1225 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001226
1227 // Because of short-circuit evaluation, the condition of the loop can span
1228 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1229 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001230 CFGBlock* ExitConditionBlock = createBlock(false);
1231 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001232
Ted Kremenek81e14852007-08-27 19:46:09 +00001233 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001234 ExitConditionBlock->setTerminator(D);
1235
1236 // Now add the actual condition to the condition block. Because the condition
1237 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001238 if (Stmt* C = D->getCond()) {
1239 Block = ExitConditionBlock;
1240 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001241 if (Block) {
1242 if (!FinishBlock(EntryConditionBlock))
1243 return 0;
1244 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001245 }
Mike Stump31feda52009-07-17 01:31:16 +00001246
Ted Kremeneka1523a32008-02-27 07:20:00 +00001247 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001248 Succ = EntryConditionBlock;
1249
Mike Stump773582d2009-07-23 23:25:26 +00001250 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001251 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001252
Ted Kremenek9aae5132007-08-23 21:42:29 +00001253 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001254 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001255 {
1256 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001257
Ted Kremenek9aae5132007-08-23 21:42:29 +00001258 // Save the current values for Block, Succ, and continue and break targets
1259 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1260 save_continue(ContinueTargetBlock),
1261 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001262
Ted Kremenek9aae5132007-08-23 21:42:29 +00001263 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001264 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001265
Ted Kremenek9aae5132007-08-23 21:42:29 +00001266 // All breaks should go to the code following the loop.
1267 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001268
Ted Kremenek9aae5132007-08-23 21:42:29 +00001269 // NULL out Block to force lazy instantiation of blocks for the body.
1270 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001271
Ted Kremenek9aae5132007-08-23 21:42:29 +00001272 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001273 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001274
Ted Kremeneke9610502007-08-30 18:39:40 +00001275 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001276 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001277 else if (Block) {
1278 if (!FinishBlock(BodyBlock))
1279 return 0;
1280 }
Mike Stump31feda52009-07-17 01:31:16 +00001281
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001282 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001283 // ExitConditionBlock to represent the "loop back" transition. Create an
1284 // empty block to represent the transition block for looping back to the
1285 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001286 // FIXME: Can we do this more efficiently without adding another block?
1287 Block = NULL;
1288 Succ = BodyBlock;
1289 CFGBlock *LoopBackBlock = createBlock();
1290 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001291
Ted Kremenek30754282009-07-24 04:47:11 +00001292 // Add the loop body entry as a successor to the condition.
1293 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001294 }
Mike Stump31feda52009-07-17 01:31:16 +00001295
Ted Kremenek30754282009-07-24 04:47:11 +00001296 // Link up the condition block with the code that follows the loop.
1297 // (the false branch).
1298 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001299
1300 // There can be no more statements in the body block(s) since we loop back to
1301 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001302 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001303
Ted Kremenek9aae5132007-08-23 21:42:29 +00001304 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001305 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001306 return BodyBlock;
1307}
1308
1309CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1310 // "continue" is a control-flow statement. Thus we stop processing the
1311 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001312 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001313 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001314
Ted Kremenek9aae5132007-08-23 21:42:29 +00001315 // Now create a new block that ends with the continue statement.
1316 Block = createBlock(false);
1317 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001318
Ted Kremenek9aae5132007-08-23 21:42:29 +00001319 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001320 // incomplete AST. This means the CFG cannot be constructed.
1321 if (ContinueTargetBlock)
1322 Block->addSuccessor(ContinueTargetBlock);
1323 else
1324 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001325
Ted Kremenek9aae5132007-08-23 21:42:29 +00001326 return Block;
1327}
Ted Kremenek93668002009-07-17 22:18:43 +00001328
Ted Kremenek0747de62009-07-18 00:47:21 +00001329CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1330 bool alwaysAdd) {
1331
1332 if (alwaysAdd) {
1333 autoCreateBlock();
1334 Block->appendStmt(E);
1335 }
1336
Ted Kremenek93668002009-07-17 22:18:43 +00001337 // VLA types have expressions that must be evaluated.
1338 if (E->isArgumentType()) {
1339 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1340 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1341 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001342 }
Ted Kremenek93668002009-07-17 22:18:43 +00001343
Mike Stump31feda52009-07-17 01:31:16 +00001344 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001345}
Ted Kremenek93668002009-07-17 22:18:43 +00001346
1347/// VisitStmtExpr - Utility method to handle (nested) statement
1348/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001349CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1350 if (alwaysAdd) {
1351 autoCreateBlock();
1352 Block->appendStmt(SE);
1353 }
Ted Kremenek93668002009-07-17 22:18:43 +00001354 return VisitCompoundStmt(SE->getSubStmt());
1355}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001356
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001357CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001358 // "switch" is a control-flow statement. Thus we stop processing the current
1359 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001360 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001361
Ted Kremenek9aae5132007-08-23 21:42:29 +00001362 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001363 if (!FinishBlock(Block))
1364 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001365 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001366 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001367
1368 // Save the current "switch" context.
1369 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001370 save_break(BreakTargetBlock),
1371 save_default(DefaultCaseBlock);
1372
Mike Stump31feda52009-07-17 01:31:16 +00001373 // Set the "default" case to be the block after the switch statement. If the
1374 // switch statement contains a "default:", this value will be overwritten with
1375 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001376 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001377
Ted Kremenek9aae5132007-08-23 21:42:29 +00001378 // Create a new block that will contain the switch statement.
1379 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001380
Ted Kremenek9aae5132007-08-23 21:42:29 +00001381 // Now process the switch body. The code after the switch is the implicit
1382 // successor.
1383 Succ = SwitchSuccessor;
1384 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001385
1386 // When visiting the body, the case statements should automatically get linked
1387 // up to the switch. We also don't keep a pointer to the body, since all
1388 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001389 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001390 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001391 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001392 if (Block) {
1393 if (!FinishBlock(BodyBlock))
1394 return 0;
1395 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001396
Mike Stump31feda52009-07-17 01:31:16 +00001397 // If we have no "default:" case, the default transition is to the code
1398 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001399 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001400
Ted Kremenek81e14852007-08-27 19:46:09 +00001401 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001402 SwitchTerminatedBlock->setTerminator(Terminator);
1403 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001405
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001406 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001407}
1408
Ted Kremenek93668002009-07-17 22:18:43 +00001409CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001410 // CaseStmts are essentially labels, so they are the first statement in a
1411 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001412
Ted Kremenek93668002009-07-17 22:18:43 +00001413 if (CS->getSubStmt())
1414 addStmt(CS->getSubStmt());
1415
Ted Kremenek55e91e82007-08-30 18:48:11 +00001416 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001417 if (!CaseBlock)
1418 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001419
1420 // Cases statements partition blocks, so this is the top of the basic block we
1421 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001422 CaseBlock->setLabel(CS);
1423
Ted Kremenek55957a82009-05-02 00:13:27 +00001424 if (!FinishBlock(CaseBlock))
1425 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001426
1427 // Add this block to the list of successors for the block with the switch
1428 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001429 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001430 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001431
Ted Kremenek9aae5132007-08-23 21:42:29 +00001432 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1433 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001434
Ted Kremenek9aae5132007-08-23 21:42:29 +00001435 // This block is now the implicit successor of other blocks.
1436 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001437
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001438 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001439}
Mike Stump31feda52009-07-17 01:31:16 +00001440
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001441CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001442 if (Terminator->getSubStmt())
1443 addStmt(Terminator->getSubStmt());
1444
Ted Kremenek654c78f2008-02-13 22:05:39 +00001445 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001446
1447 if (!DefaultCaseBlock)
1448 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001449
1450 // Default statements partition blocks, so this is the top of the basic block
1451 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001452 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001453
Ted Kremenek55957a82009-05-02 00:13:27 +00001454 if (!FinishBlock(DefaultCaseBlock))
1455 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001456
Mike Stump31feda52009-07-17 01:31:16 +00001457 // Unlike case statements, we don't add the default block to the successors
1458 // for the switch statement immediately. This is done when we finish
1459 // processing the switch statement. This allows for the default case
1460 // (including a fall-through to the code after the switch statement) to always
1461 // be the last successor of a switch-terminated block.
1462
Ted Kremenek654c78f2008-02-13 22:05:39 +00001463 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1464 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001465
Ted Kremenek654c78f2008-02-13 22:05:39 +00001466 // This block is now the implicit successor of other blocks.
1467 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001468
1469 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001470}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001471
Ted Kremenekeda180e22007-08-28 19:26:49 +00001472CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001473 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001474 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001475
Ted Kremenekeda180e22007-08-28 19:26:49 +00001476 if (!IBlock) {
1477 IBlock = createBlock(false);
1478 cfg->setIndirectGotoBlock(IBlock);
1479 }
Mike Stump31feda52009-07-17 01:31:16 +00001480
Ted Kremenekeda180e22007-08-28 19:26:49 +00001481 // IndirectGoto is a control-flow statement. Thus we stop processing the
1482 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001483 if (Block && !FinishBlock(Block))
1484 return 0;
1485
Ted Kremenekeda180e22007-08-28 19:26:49 +00001486 Block = createBlock(false);
1487 Block->setTerminator(I);
1488 Block->addSuccessor(IBlock);
1489 return addStmt(I->getTarget());
1490}
1491
Ted Kremenek04cca642007-08-23 21:26:19 +00001492} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001493
Mike Stump31feda52009-07-17 01:31:16 +00001494/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1495/// no successors or predecessors. If this is the first block created in the
1496/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001497CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001498 bool first_block = begin() == end();
1499
1500 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001501 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001502
1503 // If this is the first block, set it as the Entry and Exit.
1504 if (first_block) Entry = Exit = &front();
1505
1506 // Return the block.
1507 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001508}
1509
Ted Kremenek889073f2007-08-23 16:51:22 +00001510/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1511/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001512CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001513 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001514 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001515}
1516
1517/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001518void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1519
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001520//===----------------------------------------------------------------------===//
1521// CFG: Queries for BlkExprs.
1522//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001523
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001524namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001525 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001526}
1527
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001528static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1529 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001530 return;
Mike Stump31feda52009-07-17 01:31:16 +00001531
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001532 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001533 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001534
Ted Kremenek95a123c2008-01-26 00:03:27 +00001535 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1536 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001537
Ted Kremenek95a123c2008-01-26 00:03:27 +00001538 FindSubExprAssignments(*I, Set);
1539 }
1540}
1541
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001542static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1543 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001544
1545 // Look for assignments that are used as subexpressions. These are the only
1546 // assignments that we want to *possibly* register as a block-level
1547 // expression. Basically, if an assignment occurs both in a subexpression and
1548 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001549 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001550
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001551 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1552 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001553 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001554
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001555 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001556
1557 // Iterate over the statements again on identify the Expr* and Stmt* at the
1558 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001559
Ted Kremenek95a123c2008-01-26 00:03:27 +00001560 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001561 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001562
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001563 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001564 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001565 // expression are really "statements" whose value is never used by
1566 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001567 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001568 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001569 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1570 // Special handling for statement expressions. The last statement in
1571 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001572 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001573 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001574 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001575 (*M)[C->body_back()] = x;
1576 }
1577 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001578
Ted Kremenek95a123c2008-01-26 00:03:27 +00001579 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001580 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001581 }
Mike Stump31feda52009-07-17 01:31:16 +00001582
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001583 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001584
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001585 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001586
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001587 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001588 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001589 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001590 }
1591 }
Mike Stump31feda52009-07-17 01:31:16 +00001592
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001593 return M;
1594}
1595
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001596CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1597 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001598 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001600 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001601 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001602
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001603 if (I == M->end()) return CFG::BlkExprNumTy();
1604 else return CFG::BlkExprNumTy(I->second);
1605}
1606
1607unsigned CFG::getNumBlkExprs() {
1608 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1609 return M->size();
1610 else {
1611 // We assume callers interested in the number of BlkExprs will want
1612 // the map constructed if it doesn't already exist.
1613 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1614 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1615 }
1616}
1617
Ted Kremenek6065ef62008-04-28 18:00:46 +00001618//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001619// Cleanup: CFG dstor.
1620//===----------------------------------------------------------------------===//
1621
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001622CFG::~CFG() {
1623 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1624}
Mike Stump31feda52009-07-17 01:31:16 +00001625
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001626//===----------------------------------------------------------------------===//
1627// CFG pretty printing
1628//===----------------------------------------------------------------------===//
1629
Ted Kremenek7e776b12007-08-22 18:22:34 +00001630namespace {
1631
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001632class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001633
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001634 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1635 StmtMapTy StmtMap;
1636 signed CurrentBlock;
1637 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001638 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001639public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001640
Chris Lattnerc61089a2009-06-30 01:26:17 +00001641 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1642 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001643 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1644 unsigned j = 1;
1645 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1646 BI != BEnd; ++BI, ++j )
1647 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1648 }
1649 }
Mike Stump31feda52009-07-17 01:31:16 +00001650
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001651 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001652
Chris Lattnerc61089a2009-06-30 01:26:17 +00001653 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001654 void setBlockID(signed i) { CurrentBlock = i; }
1655 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001656
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001657 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001658
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001659 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001660
1661 if (I == StmtMap.end())
1662 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001663
1664 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001665 && I->second.second == CurrentStmt)
1666 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001667
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001668 OS << "[B" << I->second.first << "." << I->second.second << "]";
1669 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001670 }
1671};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001672} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001673
Chris Lattnerc61089a2009-06-30 01:26:17 +00001674
1675namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001676class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1677 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001678
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001679 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001680 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001681 PrintingPolicy Policy;
1682
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001683public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001684 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001685 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001686 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001687
Ted Kremenek9aae5132007-08-23 21:42:29 +00001688 void VisitIfStmt(IfStmt* I) {
1689 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001690 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001691 }
Mike Stump31feda52009-07-17 01:31:16 +00001692
Ted Kremenek9aae5132007-08-23 21:42:29 +00001693 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001694 void VisitStmt(Stmt* Terminator) {
1695 Terminator->printPretty(OS, Helper, Policy);
1696 }
1697
Ted Kremenek9aae5132007-08-23 21:42:29 +00001698 void VisitForStmt(ForStmt* F) {
1699 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001700 if (F->getInit()) OS << "...";
1701 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001702 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001703 OS << "; ";
1704 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001705 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001706 }
Mike Stump31feda52009-07-17 01:31:16 +00001707
Ted Kremenek9aae5132007-08-23 21:42:29 +00001708 void VisitWhileStmt(WhileStmt* W) {
1709 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001710 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001711 }
Mike Stump31feda52009-07-17 01:31:16 +00001712
Ted Kremenek9aae5132007-08-23 21:42:29 +00001713 void VisitDoStmt(DoStmt* D) {
1714 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001715 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001716 }
Mike Stump31feda52009-07-17 01:31:16 +00001717
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001718 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001719 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001720 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001721 }
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001723 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001724 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001725 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001726 }
Mike Stump31feda52009-07-17 01:31:16 +00001727
Ted Kremenek391f94a2007-08-31 22:29:13 +00001728 void VisitChooseExpr(ChooseExpr* C) {
1729 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001730 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001731 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001732 }
Mike Stump31feda52009-07-17 01:31:16 +00001733
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001734 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1735 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001736 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001737 }
Mike Stump31feda52009-07-17 01:31:16 +00001738
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001739 void VisitBinaryOperator(BinaryOperator* B) {
1740 if (!B->isLogicalOp()) {
1741 VisitExpr(B);
1742 return;
1743 }
Mike Stump31feda52009-07-17 01:31:16 +00001744
Douglas Gregor7de59662009-05-29 20:38:28 +00001745 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001746
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001747 switch (B->getOpcode()) {
1748 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001749 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001750 return;
1751 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001752 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001753 return;
1754 default:
1755 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001756 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001757 }
Mike Stump31feda52009-07-17 01:31:16 +00001758
Ted Kremenek12687ff2007-08-27 21:54:41 +00001759 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001760 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001761 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001762};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001763} // end anonymous namespace
1764
Mike Stump31feda52009-07-17 01:31:16 +00001765
Chris Lattnerc61089a2009-06-30 01:26:17 +00001766static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1767 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001768 if (Helper) {
1769 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001770 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001771 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001772
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001773 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001774 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001775 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001776 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001777 return;
1778 }
1779 }
Mike Stump31feda52009-07-17 01:31:16 +00001780
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001781 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001782 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001783 if (B->getOpcode() == BinaryOperator::Comma) {
1784 OS << "... , ";
1785 Helper->handledStmt(B->getRHS(),OS);
1786 OS << '\n';
1787 return;
Mike Stump31feda52009-07-17 01:31:16 +00001788 }
1789 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001790 }
Mike Stump31feda52009-07-17 01:31:16 +00001791
Chris Lattnerc61089a2009-06-30 01:26:17 +00001792 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001793
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001794 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001795 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001796}
Mike Stump31feda52009-07-17 01:31:16 +00001797
Chris Lattnerc61089a2009-06-30 01:26:17 +00001798static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1799 const CFGBlock& B,
1800 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001801
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001802 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001803
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001804 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001805 OS << "\n [ B" << B.getBlockID();
1806
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001807 if (&B == &cfg->getEntry())
1808 OS << " (ENTRY) ]\n";
1809 else if (&B == &cfg->getExit())
1810 OS << " (EXIT) ]\n";
1811 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001812 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001813 else
1814 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001815
Ted Kremenek71eca012007-08-29 23:20:49 +00001816 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001817 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001818
1819 if (print_edges)
1820 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001821
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001822 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001823 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001824 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001825 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001826 C->getLHS()->printPretty(OS, Helper,
1827 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001828 if (C->getRHS()) {
1829 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001830 C->getRHS()->printPretty(OS, Helper,
1831 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001832 }
Mike Stump31feda52009-07-17 01:31:16 +00001833 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001834 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001835 else
1836 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001837
Ted Kremenek71eca012007-08-29 23:20:49 +00001838 OS << ":\n";
1839 }
Mike Stump31feda52009-07-17 01:31:16 +00001840
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001841 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001842 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001843
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001844 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1845 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001846
Ted Kremenek71eca012007-08-29 23:20:49 +00001847 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001848 if (print_edges)
1849 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001851 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001853 if (Helper)
1854 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001856 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001857 }
Mike Stump31feda52009-07-17 01:31:16 +00001858
Ted Kremenek71eca012007-08-29 23:20:49 +00001859 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001860 if (B.getTerminator()) {
1861 if (print_edges)
1862 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001863
Ted Kremenek71eca012007-08-29 23:20:49 +00001864 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001865
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001866 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001867
Chris Lattnerc61089a2009-06-30 01:26:17 +00001868 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1869 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001870 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001871 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001872 }
Mike Stump31feda52009-07-17 01:31:16 +00001873
Ted Kremenek71eca012007-08-29 23:20:49 +00001874 if (print_edges) {
1875 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001876 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001877 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001878
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001879 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1880 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001882 if (i == 8 || (i-8) == 0)
1883 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek71eca012007-08-29 23:20:49 +00001885 OS << " B" << (*I)->getBlockID();
1886 }
Mike Stump31feda52009-07-17 01:31:16 +00001887
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001888 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001890 // Print the successors of this block.
1891 OS << " Successors (" << B.succ_size() << "):";
1892 i = 0;
1893
1894 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1895 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001896
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001897 if (i == 8 || (i-8) % 10 == 0)
1898 OS << "\n ";
1899
Mike Stump0d76d072009-07-20 23:24:15 +00001900 if (*I)
1901 OS << " B" << (*I)->getBlockID();
1902 else
1903 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001904 }
Mike Stump31feda52009-07-17 01:31:16 +00001905
Ted Kremenek71eca012007-08-29 23:20:49 +00001906 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001907 }
Mike Stump31feda52009-07-17 01:31:16 +00001908}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001909
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001910
1911/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001912void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913
1914/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001915void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1916 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001917
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001918 // Print the entry block.
1919 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001920
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001921 // Iterate through the CFGBlocks and print them one by one.
1922 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1923 // Skip the entry block, because we already printed it.
1924 if (&(*I) == &getEntry() || &(*I) == &getExit())
1925 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001927 print_block(OS, this, *I, &Helper, true);
1928 }
Mike Stump31feda52009-07-17 01:31:16 +00001929
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001930 // Print the exit block.
1931 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001932 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001933}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001934
1935/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001936void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1937 print(llvm::errs(), cfg, LO);
1938}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001939
1940/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1941/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001942void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1943 const LangOptions &LO) const {
1944 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001945 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001946}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001947
Ted Kremenek15647632008-01-30 23:02:42 +00001948/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001949void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001950 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001951 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001952 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1953}
1954
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001955Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001956
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001957 if (!Terminator)
1958 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001959
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001960 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001962 switch (Terminator->getStmtClass()) {
1963 default:
1964 break;
Mike Stump31feda52009-07-17 01:31:16 +00001965
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001966 case Stmt::ForStmtClass:
1967 E = cast<ForStmt>(Terminator)->getCond();
1968 break;
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001970 case Stmt::WhileStmtClass:
1971 E = cast<WhileStmt>(Terminator)->getCond();
1972 break;
Mike Stump31feda52009-07-17 01:31:16 +00001973
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001974 case Stmt::DoStmtClass:
1975 E = cast<DoStmt>(Terminator)->getCond();
1976 break;
Mike Stump31feda52009-07-17 01:31:16 +00001977
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001978 case Stmt::IfStmtClass:
1979 E = cast<IfStmt>(Terminator)->getCond();
1980 break;
Mike Stump31feda52009-07-17 01:31:16 +00001981
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001982 case Stmt::ChooseExprClass:
1983 E = cast<ChooseExpr>(Terminator)->getCond();
1984 break;
Mike Stump31feda52009-07-17 01:31:16 +00001985
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001986 case Stmt::IndirectGotoStmtClass:
1987 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1988 break;
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001990 case Stmt::SwitchStmtClass:
1991 E = cast<SwitchStmt>(Terminator)->getCond();
1992 break;
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001994 case Stmt::ConditionalOperatorClass:
1995 E = cast<ConditionalOperator>(Terminator)->getCond();
1996 break;
Mike Stump31feda52009-07-17 01:31:16 +00001997
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001998 case Stmt::BinaryOperatorClass: // '&&' and '||'
1999 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002000 break;
Mike Stump31feda52009-07-17 01:31:16 +00002001
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002002 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002003 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002004 }
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002006 return E ? E->IgnoreParens() : NULL;
2007}
2008
Ted Kremenek92137a32008-05-16 16:06:00 +00002009bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenek92137a32008-05-16 16:06:00 +00002011 if (!Terminator)
2012 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002013
Ted Kremenek92137a32008-05-16 16:06:00 +00002014 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenek92137a32008-05-16 16:06:00 +00002016 switch (Terminator->getStmtClass()) {
2017 default:
2018 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002019
2020 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002021 case Stmt::WhileStmtClass:
2022 case Stmt::DoStmtClass:
2023 case Stmt::IfStmtClass:
2024 case Stmt::ChooseExprClass:
2025 case Stmt::ConditionalOperatorClass:
2026 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002027 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002028 }
Mike Stump31feda52009-07-17 01:31:16 +00002029
Ted Kremenek92137a32008-05-16 16:06:00 +00002030 return E ? E->IgnoreParens() : NULL;
2031}
2032
Ted Kremenek15647632008-01-30 23:02:42 +00002033
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002034//===----------------------------------------------------------------------===//
2035// CFG Graphviz Visualization
2036//===----------------------------------------------------------------------===//
2037
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002038
2039#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002040static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002041#endif
2042
Chris Lattnerc61089a2009-06-30 01:26:17 +00002043void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002044#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002045 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002046 GraphHelper = &H;
2047 llvm::ViewGraph(this,"CFG");
2048 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002049#endif
2050}
2051
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002052namespace llvm {
2053template<>
2054struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002055 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2056 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002057
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002058#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002059 std::string OutSStr;
2060 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002061 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002062 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002063
2064 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2065
2066 // Process string output to make it nicer...
2067 for (unsigned i = 0; i != OutStr.length(); ++i)
2068 if (OutStr[i] == '\n') { // Left justify
2069 OutStr[i] = '\\';
2070 OutStr.insert(OutStr.begin()+i+1, 'l');
2071 }
Mike Stump31feda52009-07-17 01:31:16 +00002072
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002073 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002074#else
2075 return "";
2076#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002077 }
2078};
2079} // end namespace llvm