blob: af64fb8414432403541599c9ca16cdb15ae9e937 [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"
Ted Kremenek8a632182007-08-21 23:26:17 +000019#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4e5f99d2007-08-29 21:56:09 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremeneka59e0062007-12-17 19:35:20 +000022#include "llvm/Support/Streams.h"
Ted Kremenek83ebcef2008-01-08 18:15:10 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek6065ef62008-04-28 18:00:46 +000024#include <llvm/Support/Allocator.h>
Ted Kremenek2d470fc2008-09-13 05:16:45 +000025#include <llvm/Support/Format.h>
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Mike Stump31feda52009-07-17 01:31:16 +000038
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000039/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000040/// The builder is stateful: an instance of the builder should be used to only
41/// construct a single CFG.
42///
43/// Example usage:
44///
45/// CFGBuilder builder;
46/// CFG* cfg = builder.BuildAST(stmt1);
47///
Mike Stump31feda52009-07-17 01:31:16 +000048/// CFG construction is done via a recursive walk of an AST. We actually parse
49/// the AST in reverse order so that the successor of a basic block is
50/// constructed prior to its predecessor. This allows us to nicely capture
51/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000052///
Ted Kremenek93668002009-07-17 22:18:43 +000053class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000054 ASTContext *Context;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000055 CFG* cfg;
56 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000062
Ted Kremenekeda180e22007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000071
Ted Kremenekeda180e22007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000075
76public:
Ted Kremenek889073f2007-08-23 16:51:22 +000077 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000078 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000079 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000080 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000081 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000082 }
Mike Stump31feda52009-07-17 01:31:16 +000083
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000084 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000085
Ted Kremenek9aae5132007-08-23 21:42:29 +000086 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000087 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000088
Ted Kremenek93668002009-07-17 22:18:43 +000089private:
90 // Visitors to walk an AST and construct the CFG.
91 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
92 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000093 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
94 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000095 CFGBlock *VisitBreakStmt(BreakStmt *B);
96 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
97 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000098 CFGBlock *VisitChooseExpr(ChooseExpr *C);
99 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
100 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
101 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000102 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +0000103 CFGBlock *VisitDeclStmt(DeclStmt *DS);
104 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000105 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
106 CFGBlock *VisitDoStmt(DoStmt *D);
107 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000108 CFGBlock *VisitGotoStmt(GotoStmt* G);
109 CFGBlock *VisitIfStmt(IfStmt *I);
110 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
111 CFGBlock *VisitLabelStmt(LabelStmt *L);
112 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
113 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
114 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
115 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
116 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
117 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000118 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
119 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000120 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
121 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000122
Ted Kremenek93668002009-07-17 22:18:43 +0000123 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
124 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
125 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000126
Ted Kremenek6065ef62008-04-28 18:00:46 +0000127 // NYS == Not Yet Supported
128 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000129 badCFG = true;
130 return Block;
131 }
Mike Stump31feda52009-07-17 01:31:16 +0000132
Ted Kremenek93668002009-07-17 22:18:43 +0000133 void autoCreateBlock() { if (!Block) Block = createBlock(); }
134 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000135 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000136 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
137
Ted Kremenek30754282009-07-24 04:47:11 +0000138 class TryResult {
139 int X;
140 public:
141 TryResult(bool b) : X(b ? 1 : 0) {}
142 TryResult() : X(-1) {}
143
144 bool isTrue() const { return X == 1; }
145 bool isFalse() const { return X == 0; }
146 bool isKnown() const { return X >= 0; }
147 void negate() {
148 assert(isKnown());
149 X ^= 0x1;
150 }
151 };
152
Mike Stump773582d2009-07-23 23:25:26 +0000153 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
154 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000155 TryResult TryEvaluateBool(Expr *S) {
Mike Stump773582d2009-07-23 23:25:26 +0000156 Expr::EvalResult Result;
Ted Kremenek30754282009-07-24 04:47:11 +0000157 if (S->Evaluate(Result, *Context) && Result.Val.isInt())
158 return Result.Val.getInt().getBoolValue() ? true : false;
159
160 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000161 }
162
Ted Kremenekb64d1832008-03-13 03:04:22 +0000163 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000164};
Mike Stump31feda52009-07-17 01:31:16 +0000165
Douglas Gregor4619e432008-12-05 23:32:09 +0000166// FIXME: Add support for dependent-sized array types in C++?
167// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000168static VariableArrayType* FindVA(Type* t) {
169 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
170 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
171 if (vat->getSizeExpr())
172 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000173
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000174 t = vt->getElementType().getTypePtr();
175 }
Mike Stump31feda52009-07-17 01:31:16 +0000176
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000177 return 0;
178}
Mike Stump31feda52009-07-17 01:31:16 +0000179
180/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
181/// arbitrary statement. Examples include a single expression or a function
182/// body (compound statement). The ownership of the returned CFG is
183/// transferred to the caller. If CFG construction fails, this method returns
184/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000185CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
186 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000187 assert(cfg);
188 if (!Statement)
189 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000190
Ted Kremenekb64d1832008-03-13 03:04:22 +0000191 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000192
193 // Create an empty block that will serve as the exit block for the CFG. Since
194 // this is the first block added to the CFG, it will be implicitly registered
195 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000196 Succ = createBlock();
197 assert (Succ == &cfg->getExit());
198 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000199
Ted Kremenek9aae5132007-08-23 21:42:29 +0000200 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000201 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000202 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000203
Ted Kremenek40d87632008-02-27 17:33:02 +0000204 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000205 // Finalize the last constructed block. This usually involves reversing the
206 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000207 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000208
209 // Backpatch the gotos whose label -> block mappings we didn't know when we
210 // encountered them.
211 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000212 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000213
Ted Kremenek9aae5132007-08-23 21:42:29 +0000214 CFGBlock* B = *I;
215 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
216 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
217
218 // If there is no target for the goto, then we are looking at an
219 // incomplete AST. Handle this by not registering a successor.
220 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000221
222 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000223 }
Mike Stump31feda52009-07-17 01:31:16 +0000224
Ted Kremenekeda180e22007-08-28 19:26:49 +0000225 // Add successors to the Indirect Goto Dispatch block (if we have one).
226 if (CFGBlock* B = cfg->getIndirectGotoBlock())
227 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
228 E = AddressTakenLabels.end(); I != E; ++I ) {
229
230 // Lookup the target block.
231 LabelMapTy::iterator LI = LabelMap.find(*I);
232
233 // If there is no target block that contains label, then we are looking
234 // at an incomplete AST. Handle this by not registering a successor.
235 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000236
237 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000238 }
Mike Stump31feda52009-07-17 01:31:16 +0000239
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000240 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000241 }
Mike Stump31feda52009-07-17 01:31:16 +0000242
243 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000244 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000245
Ted Kremenekb64d1832008-03-13 03:04:22 +0000246 if (badCFG) {
247 delete cfg;
248 cfg = NULL;
249 return NULL;
250 }
Mike Stump31feda52009-07-17 01:31:16 +0000251
252 // NULL out cfg so that repeated calls to the builder will fail and that the
253 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000254 CFG* t = cfg;
255 cfg = NULL;
256 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000257}
Mike Stump31feda52009-07-17 01:31:16 +0000258
Ted Kremenek9aae5132007-08-23 21:42:29 +0000259/// createBlock - Used to lazily create blocks that are connected
260/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000261CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000262 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000263 if (add_successor && Succ)
264 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000265 return B;
266}
Mike Stump31feda52009-07-17 01:31:16 +0000267
268/// FinishBlock - When the last statement has been added to the block, we must
269/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000270bool CFGBuilder::FinishBlock(CFGBlock* B) {
271 if (badCFG)
272 return false;
273
Ted Kremenek93668002009-07-17 22:18:43 +0000274 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000275 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000276 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000277}
278
Ted Kremenek93668002009-07-17 22:18:43 +0000279/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000280/// blocks for ternary operators, &&, and ||. We also process "," and
281/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000282CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
283tryAgain:
284 switch (S->getStmtClass()) {
285 default:
286 return VisitStmt(S, alwaysAdd);
287
288 case Stmt::AddrLabelExprClass:
289 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
290
291 case Stmt::BinaryOperatorClass:
292 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
293
294 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000295 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000296
297 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000298 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000299
300 case Stmt::BreakStmtClass:
301 return VisitBreakStmt(cast<BreakStmt>(S));
302
303 case Stmt::CallExprClass:
304 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
305
306 case Stmt::CaseStmtClass:
307 return VisitCaseStmt(cast<CaseStmt>(S));
308
309 case Stmt::ChooseExprClass:
310 return VisitChooseExpr(cast<ChooseExpr>(S));
311
312 case Stmt::CompoundStmtClass:
313 return VisitCompoundStmt(cast<CompoundStmt>(S));
314
315 case Stmt::ConditionalOperatorClass:
316 return VisitConditionalOperator(cast<ConditionalOperator>(S));
317
318 case Stmt::ContinueStmtClass:
319 return VisitContinueStmt(cast<ContinueStmt>(S));
320
321 case Stmt::DeclStmtClass:
322 return VisitDeclStmt(cast<DeclStmt>(S));
323
324 case Stmt::DefaultStmtClass:
325 return VisitDefaultStmt(cast<DefaultStmt>(S));
326
327 case Stmt::DoStmtClass:
328 return VisitDoStmt(cast<DoStmt>(S));
329
330 case Stmt::ForStmtClass:
331 return VisitForStmt(cast<ForStmt>(S));
332
333 case Stmt::GotoStmtClass:
334 return VisitGotoStmt(cast<GotoStmt>(S));
335
336 case Stmt::IfStmtClass:
337 return VisitIfStmt(cast<IfStmt>(S));
338
339 case Stmt::IndirectGotoStmtClass:
340 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
341
342 case Stmt::LabelStmtClass:
343 return VisitLabelStmt(cast<LabelStmt>(S));
344
345 case Stmt::ObjCAtCatchStmtClass:
346 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
347
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000348 case Stmt::CXXThrowExprClass:
349 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
350
Ted Kremenek93668002009-07-17 22:18:43 +0000351 case Stmt::ObjCAtSynchronizedStmtClass:
352 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
353
354 case Stmt::ObjCAtThrowStmtClass:
355 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
356
357 case Stmt::ObjCAtTryStmtClass:
358 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
359
360 case Stmt::ObjCForCollectionStmtClass:
361 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
362
363 case Stmt::ParenExprClass:
364 S = cast<ParenExpr>(S)->getSubExpr();
365 goto tryAgain;
366
367 case Stmt::NullStmtClass:
368 return Block;
369
370 case Stmt::ReturnStmtClass:
371 return VisitReturnStmt(cast<ReturnStmt>(S));
372
373 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000374 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000375
376 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000377 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000378
379 case Stmt::SwitchStmtClass:
380 return VisitSwitchStmt(cast<SwitchStmt>(S));
381
382 case Stmt::WhileStmtClass:
383 return VisitWhileStmt(cast<WhileStmt>(S));
384 }
385}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000386
Ted Kremenek93668002009-07-17 22:18:43 +0000387CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
388 if (alwaysAdd) {
389 autoCreateBlock();
390 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000391 }
Ted Kremenek93668002009-07-17 22:18:43 +0000392
393 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000394}
Mike Stump31feda52009-07-17 01:31:16 +0000395
Ted Kremenek93668002009-07-17 22:18:43 +0000396/// VisitChildren - Visit the children of a Stmt.
397CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
398 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000399 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000400 E = Terminator->child_end(); I != E; ++I) {
401 if (*I) B = Visit(*I);
402 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000403 return B;
404}
Ted Kremenek93668002009-07-17 22:18:43 +0000405
406CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
407 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000408
Ted Kremenek93668002009-07-17 22:18:43 +0000409 if (alwaysAdd) {
410 autoCreateBlock();
411 Block->appendStmt(A);
412 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000413
Ted Kremenek9aae5132007-08-23 21:42:29 +0000414 return Block;
415}
Ted Kremenek93668002009-07-17 22:18:43 +0000416
417CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
418 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000419 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
420 ConfluenceBlock->appendStmt(B);
421
422 if (!FinishBlock(ConfluenceBlock))
423 return 0;
424
425 // create the block evaluating the LHS
426 CFGBlock* LHSBlock = createBlock(false);
427 LHSBlock->setTerminator(B);
428
429 // create the block evaluating the RHS
430 Succ = ConfluenceBlock;
431 Block = NULL;
432 CFGBlock* RHSBlock = addStmt(B->getRHS());
433 if (!FinishBlock(RHSBlock))
434 return 0;
435
Mike Stump773582d2009-07-23 23:25:26 +0000436 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000437 TryResult KnownVal = TryEvaluateBool(B->getLHS());
438 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
439 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000440
Ted Kremenek93668002009-07-17 22:18:43 +0000441 // Now link the LHSBlock with RHSBlock.
442 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek30754282009-07-24 04:47:11 +0000443 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
444 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
445 } else {
Ted Kremenek93668002009-07-17 22:18:43 +0000446 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek30754282009-07-24 04:47:11 +0000447 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
448 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000449 }
450
451 // Generate the blocks for evaluating the LHS.
452 Block = LHSBlock;
453 return addStmt(B->getLHS());
454 }
455 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000456 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000457 Block->appendStmt(B);
458 addStmt(B->getRHS());
459 return addStmt(B->getLHS());
460 }
461
462 return VisitStmt(B, alwaysAdd);
463}
464
Ted Kremenek0747de62009-07-18 00:47:21 +0000465CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000466 // FIXME
467 return NYS();
468}
469
Ted Kremenek0747de62009-07-18 00:47:21 +0000470CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
471 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000472 // FIXME
473 return NYS();
474}
475
476CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
477 // "break" is a control-flow statement. Thus we stop processing the current
478 // block.
479 if (Block && !FinishBlock(Block))
480 return 0;
481
482 // Now create a new block that ends with the break statement.
483 Block = createBlock(false);
484 Block->setTerminator(B);
485
486 // If there is no target for the break, then we are looking at an incomplete
487 // AST. This means that the CFG cannot be constructed.
488 if (BreakTargetBlock)
489 Block->addSuccessor(BreakTargetBlock);
490 else
491 badCFG = true;
492
493
Ted Kremenek9aae5132007-08-23 21:42:29 +0000494 return Block;
495}
Ted Kremenek93668002009-07-17 22:18:43 +0000496
497CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
498 // If this is a call to a no-return function, this stops the block here.
499 if (FunctionDecl *FD = C->getDirectCallee()) {
500
501 if (!FD->hasAttr<NoReturnAttr>())
502 return VisitStmt(C, alwaysAdd);
503
504 if (Block && !FinishBlock(Block))
505 return 0;
506
507 // Create new block with no successor for the remaining pieces.
508 Block = createBlock(false);
509 Block->appendStmt(C);
510
511 // Wire this to the exit block directly.
512 Block->addSuccessor(&cfg->getExit());
513
514 return VisitChildren(C);
515 }
516
517 return VisitStmt(C, alwaysAdd);
518}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000519
Ted Kremenek21822592009-07-17 18:20:32 +0000520CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
521 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
522 ConfluenceBlock->appendStmt(C);
523 if (!FinishBlock(ConfluenceBlock))
524 return 0;
525
526 Succ = ConfluenceBlock;
527 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000528 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000529 if (!FinishBlock(LHSBlock))
530 return 0;
531
532 Succ = ConfluenceBlock;
533 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000534 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000535 if (!FinishBlock(RHSBlock))
536 return 0;
537
538 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000539 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000540 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
541 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
542 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000543 Block->setTerminator(C);
544 return addStmt(C->getCond());
545}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000546
Ted Kremenek93668002009-07-17 22:18:43 +0000547
548CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
549 CFGBlock* LastBlock = Block;
550
551 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
552 I != E; ++I ) {
553 LastBlock = addStmt(*I);
554 }
555 return LastBlock;
556}
557
Ted Kremenek51d40b02009-07-17 18:15:54 +0000558CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
559 // Create the confluence block that will "merge" the results of the ternary
560 // expression.
561 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
562 ConfluenceBlock->appendStmt(C);
563 if (!FinishBlock(ConfluenceBlock))
564 return 0;
565
566 // Create a block for the LHS expression if there is an LHS expression. A
567 // GCC extension allows LHS to be NULL, causing the condition to be the
568 // value that is returned instead.
569 // e.g: x ?: y is shorthand for: x ? x : y;
570 Succ = ConfluenceBlock;
571 Block = NULL;
572 CFGBlock* LHSBlock = NULL;
573 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000574 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000575 if (!FinishBlock(LHSBlock))
576 return 0;
577 Block = NULL;
578 }
579
580 // Create the block for the RHS expression.
581 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000582 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000583 if (!FinishBlock(RHSBlock))
584 return 0;
585
586 // Create the block that will contain the condition.
587 Block = createBlock(false);
588
Mike Stump773582d2009-07-23 23:25:26 +0000589 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000590 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000591 if (LHSBlock) {
Ted Kremenek30754282009-07-24 04:47:11 +0000592 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000593 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000594 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000595 // If we know the condition is false, add NULL as the successor for
596 // the block containing the condition. In this case, the confluence
597 // block will have just one predecessor.
598 Block->addSuccessor(0);
Ted Kremenek30754282009-07-24 04:47:11 +0000599 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000600 } else {
601 // If we have no LHS expression, add the ConfluenceBlock as a direct
602 // successor for the block containing the condition. Moreover, we need to
603 // reverse the order of the predecessors in the ConfluenceBlock because
604 // the RHSBlock will have been added to the succcessors already, and we
605 // want the first predecessor to the the block containing the expression
606 // for the case when the ternary expression evaluates to true.
607 Block->addSuccessor(ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000608 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000609 std::reverse(ConfluenceBlock->pred_begin(),
610 ConfluenceBlock->pred_end());
611 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000612 }
613
Ted Kremenek30754282009-07-24 04:47:11 +0000614 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000615 Block->setTerminator(C);
616 return addStmt(C->getCond());
617}
618
Ted Kremenek93668002009-07-17 22:18:43 +0000619CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
620 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000621
Ted Kremenek93668002009-07-17 22:18:43 +0000622 if (DS->isSingleDecl()) {
623 Block->appendStmt(DS);
624 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000625 }
Ted Kremenek93668002009-07-17 22:18:43 +0000626
627 CFGBlock *B = 0;
628
629 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
630 typedef llvm::SmallVector<Decl*,10> BufTy;
631 BufTy Buf(DS->decl_begin(), DS->decl_end());
632
633 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
634 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
635 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
636 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
637
638 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
639 // automatically freed with the CFG.
640 DeclGroupRef DG(*I);
641 Decl *D = *I;
642 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
643 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
644
645 // Append the fake DeclStmt to block.
646 Block->appendStmt(DSNew);
647 B = VisitDeclSubExpr(D);
648 }
649
650 return B;
651}
652
653/// VisitDeclSubExpr - Utility method to add block-level expressions for
654/// initializers in Decls.
655CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
656 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000657
Ted Kremenek93668002009-07-17 22:18:43 +0000658 VarDecl *VD = dyn_cast<VarDecl>(D);
659
660 if (!VD)
661 return Block;
662
663 Expr *Init = VD->getInit();
664
665 if (Init) {
666 // Optimization: Don't create separate block-level statements for literals.
667 switch (Init->getStmtClass()) {
668 case Stmt::IntegerLiteralClass:
669 case Stmt::CharacterLiteralClass:
670 case Stmt::StringLiteralClass:
671 break;
672 default:
673 Block = addStmt(Init);
674 }
675 }
676
677 // If the type of VD is a VLA, then we must process its size expressions.
678 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
679 VA = FindVA(VA->getElementType().getTypePtr()))
680 Block = addStmt(VA->getSizeExpr());
681
682 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000683}
684
685CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000686 // We may see an if statement in the middle of a basic block, or it may be the
687 // first statement we are processing. In either case, we create a new basic
688 // block. First, we create the blocks for the then...else statements, and
689 // then we create the block containing the if statement. If we were in the
690 // middle of a block, we stop processing that block and reverse its
691 // statements. That block is then the implicit successor for the "then" and
692 // "else" clauses.
693
694 // The block we were proccessing is now finished. Make it the successor
695 // block.
696 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000697 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000698 if (!FinishBlock(Block))
699 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000700 }
Mike Stump31feda52009-07-17 01:31:16 +0000701
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000702 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000703 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000704
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705 if (Stmt* Else = I->getElse()) {
706 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000707
Ted Kremenek9aae5132007-08-23 21:42:29 +0000708 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000709 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000710 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000711 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000712
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000713 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
714 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000715 else if (Block) {
716 if (!FinishBlock(ElseBlock))
717 return 0;
718 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000719 }
Mike Stump31feda52009-07-17 01:31:16 +0000720
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000721 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000722 CFGBlock* ThenBlock;
723 {
724 Stmt* Then = I->getThen();
725 assert (Then);
726 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000727 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000728 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000729
Ted Kremenek1b379512009-04-01 03:52:47 +0000730 if (!ThenBlock) {
731 // We can reach here if the "then" body has all NullStmts.
732 // Create an empty block so we can distinguish between true and false
733 // branches in path-sensitive analyses.
734 ThenBlock = createBlock(false);
735 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000736 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000737 if (!FinishBlock(ThenBlock))
738 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000739 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000740 }
741
Mike Stump31feda52009-07-17 01:31:16 +0000742 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000743 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000744
Ted Kremenek9aae5132007-08-23 21:42:29 +0000745 // Set the terminator of the new block to the If statement.
746 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000747
Mike Stump773582d2009-07-23 23:25:26 +0000748 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000749 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000750
Ted Kremenek9aae5132007-08-23 21:42:29 +0000751 // Now add the successors.
Ted Kremenek30754282009-07-24 04:47:11 +0000752 Block->addSuccessor(KnownVal.isFalse() ? NULL : ThenBlock);
753 Block->addSuccessor(KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000754
755 // Add the condition as the last statement in the new block. This may create
756 // new blocks as the condition may contain control-flow. Any newly created
757 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000758 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000759}
Mike Stump31feda52009-07-17 01:31:16 +0000760
761
Ted Kremenek9aae5132007-08-23 21:42:29 +0000762CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000763 // If we were in the middle of a block we stop processing that block and
764 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000765 //
Mike Stump31feda52009-07-17 01:31:16 +0000766 // NOTE: If a "return" appears in the middle of a block, this means that the
767 // code afterwards is DEAD (unreachable). We still keep a basic block
768 // for that code; a simple "mark-and-sweep" from the entry block will be
769 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000770 if (Block) FinishBlock(Block);
771
772 // Create the new block.
773 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000774
Ted Kremenek9aae5132007-08-23 21:42:29 +0000775 // The Exit block is the only successor.
776 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000777
778 // Add the return statement to the block. This may create new blocks if R
779 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000780 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000781}
782
783CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
784 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000785 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000786 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000787
Ted Kremenek93668002009-07-17 22:18:43 +0000788 if (!LabelBlock) // This can happen when the body is empty, i.e.
789 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000792 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000793
794 // Labels partition blocks, so this is the end of the basic block we were
795 // processing (L is the block's label). Because this is label (and we have
796 // already processed the substatement) there is no extra control-flow to worry
797 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000798 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000799 if (!FinishBlock(LabelBlock))
800 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000801
802 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000803 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000804
Ted Kremenek9aae5132007-08-23 21:42:29 +0000805 // This block is now the implicit successor of other blocks.
806 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000807
Ted Kremenek9aae5132007-08-23 21:42:29 +0000808 return LabelBlock;
809}
810
811CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000812 // Goto is a control-flow statement. Thus we stop processing the current
813 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000814 if (Block)
815 FinishBlock(Block);
816
Ted Kremenek9aae5132007-08-23 21:42:29 +0000817 Block = createBlock(false);
818 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000819
820 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000821 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000822
Ted Kremenek9aae5132007-08-23 21:42:29 +0000823 if (I == LabelMap.end())
824 // We will need to backpatch this block later.
825 BackpatchBlocks.push_back(Block);
826 else
827 Block->addSuccessor(I->second);
828
Mike Stump31feda52009-07-17 01:31:16 +0000829 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000830}
831
832CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000833 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000834
Mike Stump014b3ea2009-07-21 01:12:51 +0000835 // "for" is a control-flow statement. Thus we stop processing the current
836 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000837 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000838 if (!FinishBlock(Block))
839 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000840 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000841 } else
842 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000843
844 // Because of short-circuit evaluation, the condition of the loop can span
845 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
846 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000847 CFGBlock* ExitConditionBlock = createBlock(false);
848 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000849
Ted Kremenek81e14852007-08-27 19:46:09 +0000850 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000851 ExitConditionBlock->setTerminator(F);
852
853 // Now add the actual condition to the condition block. Because the condition
854 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000855 if (Stmt* C = F->getCond()) {
856 Block = ExitConditionBlock;
857 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000858 if (Block) {
859 if (!FinishBlock(EntryConditionBlock))
860 return 0;
861 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000862 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000863
Mike Stump31feda52009-07-17 01:31:16 +0000864 // The condition block is the implicit successor for the loop body as well as
865 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000866 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000867
Mike Stump773582d2009-07-23 23:25:26 +0000868 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000869 TryResult KnownVal(true);
870
Mike Stump773582d2009-07-23 23:25:26 +0000871 if (F->getCond())
872 KnownVal = TryEvaluateBool(F->getCond());
873
Ted Kremenek9aae5132007-08-23 21:42:29 +0000874 // Now create the loop body.
875 {
876 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000877
Ted Kremenek9aae5132007-08-23 21:42:29 +0000878 // Save the current values for Block, Succ, and continue and break targets
879 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
880 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000881 save_break(BreakTargetBlock);
882
Ted Kremeneke9610502007-08-30 18:39:40 +0000883 // Create a new block to contain the (bottom) of the loop body.
884 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000885
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000886 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000887 // Generate increment code in its own basic block. This is the target of
888 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000889 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000890 } else {
891 // No increment code. Create a special, empty, block that is used as the
892 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000893 assert(Succ == EntryConditionBlock);
894 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000895 }
Mike Stump31feda52009-07-17 01:31:16 +0000896
Ted Kremenek902393b2009-04-28 00:51:56 +0000897 // Finish up the increment (or empty) block if it hasn't been already.
898 if (Block) {
899 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000900 if (!FinishBlock(Block))
901 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000902 Block = 0;
903 }
Mike Stump31feda52009-07-17 01:31:16 +0000904
Ted Kremenek902393b2009-04-28 00:51:56 +0000905 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000906
Ted Kremenek902393b2009-04-28 00:51:56 +0000907 // The starting block for the loop increment is the block that should
908 // represent the 'loop target' for looping back to the start of the loop.
909 ContinueTargetBlock->setLoopTarget(F);
910
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000911 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000912 BreakTargetBlock = LoopSuccessor;
913
914 // Now populate the body block, and in the process create new blocks as we
915 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000916 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000917
918 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000919 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek30754282009-07-24 04:47:11 +0000920 else if (Block && !FinishBlock(BodyBlock))
921 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000922
Ted Kremenek30754282009-07-24 04:47:11 +0000923 // This new body block is a successor to our "exit" condition block.
924 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000925 }
Mike Stump31feda52009-07-17 01:31:16 +0000926
Ted Kremenek30754282009-07-24 04:47:11 +0000927 // Link up the condition block with the code that follows the loop. (the
928 // false branch).
929 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000930
Ted Kremenek9aae5132007-08-23 21:42:29 +0000931 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000932 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000933 if (Stmt* I = F->getInit()) {
934 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000935 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000936 } else {
937 // There is no loop initialization. We are thus basically a while loop.
938 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000939 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000940 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000941 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000942 }
943}
944
Ted Kremenek9d56e642008-11-11 17:10:00 +0000945CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
946 // Objective-C fast enumeration 'for' statements:
947 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
948 //
949 // for ( Type newVariable in collection_expression ) { statements }
950 //
951 // becomes:
952 //
953 // prologue:
954 // 1. collection_expression
955 // T. jump to loop_entry
956 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000957 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000958 // 1. ObjCForCollectionStmt [performs binding to newVariable]
959 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
960 // TB:
961 // statements
962 // T. jump to loop_entry
963 // FB:
964 // what comes after
965 //
966 // and
967 //
968 // Type existingItem;
969 // for ( existingItem in expression ) { statements }
970 //
971 // becomes:
972 //
Mike Stump31feda52009-07-17 01:31:16 +0000973 // the same with newVariable replaced with existingItem; the binding works
974 // the same except that for one ObjCForCollectionStmt::getElement() returns
975 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000976 //
Mike Stump31feda52009-07-17 01:31:16 +0000977
Ted Kremenek9d56e642008-11-11 17:10:00 +0000978 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000979
Ted Kremenek9d56e642008-11-11 17:10:00 +0000980 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000981 if (!FinishBlock(Block))
982 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000983 LoopSuccessor = Block;
984 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000985 } else
986 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000987
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000988 // Build the condition blocks.
989 CFGBlock* ExitConditionBlock = createBlock(false);
990 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000991
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000992 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000993 ExitConditionBlock->setTerminator(S);
994
995 // The last statement in the block should be the ObjCForCollectionStmt, which
996 // performs the actual binding to 'element' and determines if there are any
997 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000998 ExitConditionBlock->appendStmt(S);
999 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001000
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001001 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001002 // generate new blocks as necesary. We DON'T add the statement by default to
1003 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001004 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001005 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001006 if (!FinishBlock(EntryConditionBlock))
1007 return 0;
1008 Block = 0;
1009 }
Mike Stump31feda52009-07-17 01:31:16 +00001010
1011 // The condition block is the implicit successor for the loop body as well as
1012 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001013 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001014
Ted Kremenek9d56e642008-11-11 17:10:00 +00001015 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001016 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001017 // Save the current values for Succ, continue and break targets.
1018 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001019 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1020
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001021 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001022 ContinueTargetBlock = EntryConditionBlock;
1023
Ted Kremenek93668002009-07-17 22:18:43 +00001024 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001025
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001026 if (!BodyBlock)
1027 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001028 else if (Block) {
1029 if (!FinishBlock(BodyBlock))
1030 return 0;
1031 }
Mike Stump31feda52009-07-17 01:31:16 +00001032
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001033 // This new body block is a successor to our "exit" condition block.
1034 ExitConditionBlock->addSuccessor(BodyBlock);
1035 }
Mike Stump31feda52009-07-17 01:31:16 +00001036
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001037 // Link up the condition block with the code that follows the loop.
1038 // (the false branch).
1039 ExitConditionBlock->addSuccessor(LoopSuccessor);
1040
Ted Kremenek9d56e642008-11-11 17:10:00 +00001041 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001042 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001043 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001044}
1045
Ted Kremenek49805452009-05-02 01:49:13 +00001046CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1047 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001048
Ted Kremenek49805452009-05-02 01:49:13 +00001049 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001050 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001051
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001052 // The sync body starts its own basic block. This makes it a little easier
1053 // for diagnostic clients.
1054 if (SyncBlock) {
1055 if (!FinishBlock(SyncBlock))
1056 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001057
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001058 Block = 0;
1059 }
Mike Stump31feda52009-07-17 01:31:16 +00001060
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001061 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001062
Ted Kremenek49805452009-05-02 01:49:13 +00001063 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001064 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001065}
Mike Stump31feda52009-07-17 01:31:16 +00001066
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001067CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001068 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001069 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001070}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001071
Ted Kremenek9aae5132007-08-23 21:42:29 +00001072CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001073 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001074
Mike Stump014b3ea2009-07-21 01:12:51 +00001075 // "while" is a control-flow statement. Thus we stop processing the current
1076 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001077 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001078 if (!FinishBlock(Block))
1079 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001080 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001081 } else
1082 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001083
1084 // Because of short-circuit evaluation, the condition of the loop can span
1085 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1086 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001087 CFGBlock* ExitConditionBlock = createBlock(false);
1088 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001089
Ted Kremenek81e14852007-08-27 19:46:09 +00001090 // Set the terminator for the "exit" condition block.
1091 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001092
1093 // Now add the actual condition to the condition block. Because the condition
1094 // itself may contain control-flow, new blocks may be created. Thus we update
1095 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001096 if (Stmt* C = W->getCond()) {
1097 Block = ExitConditionBlock;
1098 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001099 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001100 if (Block) {
1101 if (!FinishBlock(EntryConditionBlock))
1102 return 0;
1103 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001104 }
Mike Stump31feda52009-07-17 01:31:16 +00001105
1106 // The condition block is the implicit successor for the loop body as well as
1107 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001108 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001109
Mike Stump773582d2009-07-23 23:25:26 +00001110 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001111 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001112
Ted Kremenek9aae5132007-08-23 21:42:29 +00001113 // Process the loop body.
1114 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001115 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001116
1117 // Save the current values for Block, Succ, and continue and break targets
1118 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1119 save_continue(ContinueTargetBlock),
1120 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001121
Mike Stump31feda52009-07-17 01:31:16 +00001122 // Create an empty block to represent the transition block for looping back
1123 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001124 Block = 0;
1125 assert(Succ == EntryConditionBlock);
1126 Succ = createBlock();
1127 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001128 ContinueTargetBlock = Succ;
1129
Ted Kremenek9aae5132007-08-23 21:42:29 +00001130 // All breaks should go to the code following the loop.
1131 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001132
Ted Kremenek9aae5132007-08-23 21:42:29 +00001133 // NULL out Block to force lazy instantiation of blocks for the body.
1134 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001135
Ted Kremenek9aae5132007-08-23 21:42:29 +00001136 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001137 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001138
Ted Kremeneke9610502007-08-30 18:39:40 +00001139 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001140 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001141 else if (Block) {
1142 if (!FinishBlock(BodyBlock))
1143 return 0;
1144 }
Mike Stump31feda52009-07-17 01:31:16 +00001145
Ted Kremenek30754282009-07-24 04:47:11 +00001146 // Add the loop body entry as a successor to the condition.
1147 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001148 }
Mike Stump31feda52009-07-17 01:31:16 +00001149
Ted Kremenek30754282009-07-24 04:47:11 +00001150 // Link up the condition block with the code that follows the loop. (the
1151 // false branch).
1152 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001153
1154 // There can be no more statements in the condition block since we loop back
1155 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001156 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001157
Ted Kremenek9aae5132007-08-23 21:42:29 +00001158 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001159 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001160 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001161}
Ted Kremenek93668002009-07-17 22:18:43 +00001162
1163
1164CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1165 // FIXME: For now we pretend that @catch and the code it contains does not
1166 // exit.
1167 return Block;
1168}
Mike Stump31feda52009-07-17 01:31:16 +00001169
Ted Kremenek93041ba2008-12-09 20:20:09 +00001170CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1171 // FIXME: This isn't complete. We basically treat @throw like a return
1172 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001173
1174 // If we were in the middle of a block we stop processing that block and
1175 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001176 if (Block && !FinishBlock(Block))
1177 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001178
Ted Kremenek93041ba2008-12-09 20:20:09 +00001179 // Create the new block.
1180 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001181
Ted Kremenek93041ba2008-12-09 20:20:09 +00001182 // The Exit block is the only successor.
1183 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001184
1185 // Add the statement to the block. This may create new blocks if S contains
1186 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001187 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001188}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001189
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001190CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1191 // If we were in the middle of a block we stop processing that block and
1192 // reverse its statements.
1193 if (Block && !FinishBlock(Block))
1194 return 0;
1195
1196 // Create the new block.
1197 Block = createBlock(false);
1198
1199 // The Exit block is the only successor.
1200 Block->addSuccessor(&cfg->getExit());
1201
1202 // Add the statement to the block. This may create new blocks if S contains
1203 // control-flow (short-circuit operations).
1204 return VisitStmt(T, true);
1205}
1206
Ted Kremenek93668002009-07-17 22:18:43 +00001207CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001208 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001209
Mike Stump8d50b6a2009-07-21 01:27:50 +00001210 // "do...while" is a control-flow statement. Thus we stop processing the
1211 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001212 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001213 if (!FinishBlock(Block))
1214 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001215 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001216 } else
1217 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001218
1219 // Because of short-circuit evaluation, the condition of the loop can span
1220 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1221 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001222 CFGBlock* ExitConditionBlock = createBlock(false);
1223 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001224
Ted Kremenek81e14852007-08-27 19:46:09 +00001225 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001226 ExitConditionBlock->setTerminator(D);
1227
1228 // Now add the actual condition to the condition block. Because the condition
1229 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001230 if (Stmt* C = D->getCond()) {
1231 Block = ExitConditionBlock;
1232 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001233 if (Block) {
1234 if (!FinishBlock(EntryConditionBlock))
1235 return 0;
1236 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001237 }
Mike Stump31feda52009-07-17 01:31:16 +00001238
Ted Kremeneka1523a32008-02-27 07:20:00 +00001239 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001240 Succ = EntryConditionBlock;
1241
Mike Stump773582d2009-07-23 23:25:26 +00001242 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001243 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001244
Ted Kremenek9aae5132007-08-23 21:42:29 +00001245 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001246 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001247 {
1248 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001249
Ted Kremenek9aae5132007-08-23 21:42:29 +00001250 // Save the current values for Block, Succ, and continue and break targets
1251 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1252 save_continue(ContinueTargetBlock),
1253 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001254
Ted Kremenek9aae5132007-08-23 21:42:29 +00001255 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001256 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001257
Ted Kremenek9aae5132007-08-23 21:42:29 +00001258 // All breaks should go to the code following the loop.
1259 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001260
Ted Kremenek9aae5132007-08-23 21:42:29 +00001261 // NULL out Block to force lazy instantiation of blocks for the body.
1262 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001263
Ted Kremenek9aae5132007-08-23 21:42:29 +00001264 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001265 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001266
Ted Kremeneke9610502007-08-30 18:39:40 +00001267 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001268 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001269 else if (Block) {
1270 if (!FinishBlock(BodyBlock))
1271 return 0;
1272 }
Mike Stump31feda52009-07-17 01:31:16 +00001273
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001274 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001275 // ExitConditionBlock to represent the "loop back" transition. Create an
1276 // empty block to represent the transition block for looping back to the
1277 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001278 // FIXME: Can we do this more efficiently without adding another block?
1279 Block = NULL;
1280 Succ = BodyBlock;
1281 CFGBlock *LoopBackBlock = createBlock();
1282 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001283
Ted Kremenek30754282009-07-24 04:47:11 +00001284 // Add the loop body entry as a successor to the condition.
1285 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286 }
Mike Stump31feda52009-07-17 01:31:16 +00001287
Ted Kremenek30754282009-07-24 04:47:11 +00001288 // Link up the condition block with the code that follows the loop.
1289 // (the false branch).
1290 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001291
1292 // There can be no more statements in the body block(s) since we loop back to
1293 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001294 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001295
Ted Kremenek9aae5132007-08-23 21:42:29 +00001296 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001297 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001298 return BodyBlock;
1299}
1300
1301CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1302 // "continue" is a control-flow statement. Thus we stop processing the
1303 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001304 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001305 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001306
Ted Kremenek9aae5132007-08-23 21:42:29 +00001307 // Now create a new block that ends with the continue statement.
1308 Block = createBlock(false);
1309 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001310
Ted Kremenek9aae5132007-08-23 21:42:29 +00001311 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001312 // incomplete AST. This means the CFG cannot be constructed.
1313 if (ContinueTargetBlock)
1314 Block->addSuccessor(ContinueTargetBlock);
1315 else
1316 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001317
Ted Kremenek9aae5132007-08-23 21:42:29 +00001318 return Block;
1319}
Ted Kremenek93668002009-07-17 22:18:43 +00001320
Ted Kremenek0747de62009-07-18 00:47:21 +00001321CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1322 bool alwaysAdd) {
1323
1324 if (alwaysAdd) {
1325 autoCreateBlock();
1326 Block->appendStmt(E);
1327 }
1328
Ted Kremenek93668002009-07-17 22:18:43 +00001329 // VLA types have expressions that must be evaluated.
1330 if (E->isArgumentType()) {
1331 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1332 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1333 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001334 }
Ted Kremenek93668002009-07-17 22:18:43 +00001335
Mike Stump31feda52009-07-17 01:31:16 +00001336 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001337}
Ted Kremenek93668002009-07-17 22:18:43 +00001338
1339/// VisitStmtExpr - Utility method to handle (nested) statement
1340/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001341CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1342 if (alwaysAdd) {
1343 autoCreateBlock();
1344 Block->appendStmt(SE);
1345 }
Ted Kremenek93668002009-07-17 22:18:43 +00001346 return VisitCompoundStmt(SE->getSubStmt());
1347}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001348
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001349CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001350 // "switch" is a control-flow statement. Thus we stop processing the current
1351 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001353
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001355 if (!FinishBlock(Block))
1356 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001357 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001358 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001359
1360 // Save the current "switch" context.
1361 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001362 save_break(BreakTargetBlock),
1363 save_default(DefaultCaseBlock);
1364
Mike Stump31feda52009-07-17 01:31:16 +00001365 // Set the "default" case to be the block after the switch statement. If the
1366 // switch statement contains a "default:", this value will be overwritten with
1367 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001368 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001369
Ted Kremenek9aae5132007-08-23 21:42:29 +00001370 // Create a new block that will contain the switch statement.
1371 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001372
Ted Kremenek9aae5132007-08-23 21:42:29 +00001373 // Now process the switch body. The code after the switch is the implicit
1374 // successor.
1375 Succ = SwitchSuccessor;
1376 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001377
1378 // When visiting the body, the case statements should automatically get linked
1379 // up to the switch. We also don't keep a pointer to the body, since all
1380 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001381 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001382 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001383 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001384 if (Block) {
1385 if (!FinishBlock(BodyBlock))
1386 return 0;
1387 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001388
Mike Stump31feda52009-07-17 01:31:16 +00001389 // If we have no "default:" case, the default transition is to the code
1390 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001391 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001392
Ted Kremenek81e14852007-08-27 19:46:09 +00001393 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001394 SwitchTerminatedBlock->setTerminator(Terminator);
1395 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001396 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001397
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001398 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001399}
1400
Ted Kremenek93668002009-07-17 22:18:43 +00001401CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001402 // CaseStmts are essentially labels, so they are the first statement in a
1403 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001404
Ted Kremenek93668002009-07-17 22:18:43 +00001405 if (CS->getSubStmt())
1406 addStmt(CS->getSubStmt());
1407
Ted Kremenek55e91e82007-08-30 18:48:11 +00001408 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001409 if (!CaseBlock)
1410 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001411
1412 // Cases statements partition blocks, so this is the top of the basic block we
1413 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001414 CaseBlock->setLabel(CS);
1415
Ted Kremenek55957a82009-05-02 00:13:27 +00001416 if (!FinishBlock(CaseBlock))
1417 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001418
1419 // Add this block to the list of successors for the block with the switch
1420 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001421 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001422 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001423
Ted Kremenek9aae5132007-08-23 21:42:29 +00001424 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1425 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001426
Ted Kremenek9aae5132007-08-23 21:42:29 +00001427 // This block is now the implicit successor of other blocks.
1428 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001430 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001431}
Mike Stump31feda52009-07-17 01:31:16 +00001432
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001433CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001434 if (Terminator->getSubStmt())
1435 addStmt(Terminator->getSubStmt());
1436
Ted Kremenek654c78f2008-02-13 22:05:39 +00001437 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001438
1439 if (!DefaultCaseBlock)
1440 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001441
1442 // Default statements partition blocks, so this is the top of the basic block
1443 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001444 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001445
Ted Kremenek55957a82009-05-02 00:13:27 +00001446 if (!FinishBlock(DefaultCaseBlock))
1447 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001448
Mike Stump31feda52009-07-17 01:31:16 +00001449 // Unlike case statements, we don't add the default block to the successors
1450 // for the switch statement immediately. This is done when we finish
1451 // processing the switch statement. This allows for the default case
1452 // (including a fall-through to the code after the switch statement) to always
1453 // be the last successor of a switch-terminated block.
1454
Ted Kremenek654c78f2008-02-13 22:05:39 +00001455 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1456 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001457
Ted Kremenek654c78f2008-02-13 22:05:39 +00001458 // This block is now the implicit successor of other blocks.
1459 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001460
1461 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001462}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001463
Ted Kremenekeda180e22007-08-28 19:26:49 +00001464CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001465 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001466 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001467
Ted Kremenekeda180e22007-08-28 19:26:49 +00001468 if (!IBlock) {
1469 IBlock = createBlock(false);
1470 cfg->setIndirectGotoBlock(IBlock);
1471 }
Mike Stump31feda52009-07-17 01:31:16 +00001472
Ted Kremenekeda180e22007-08-28 19:26:49 +00001473 // IndirectGoto is a control-flow statement. Thus we stop processing the
1474 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001475 if (Block && !FinishBlock(Block))
1476 return 0;
1477
Ted Kremenekeda180e22007-08-28 19:26:49 +00001478 Block = createBlock(false);
1479 Block->setTerminator(I);
1480 Block->addSuccessor(IBlock);
1481 return addStmt(I->getTarget());
1482}
1483
Ted Kremenek04cca642007-08-23 21:26:19 +00001484} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001485
Mike Stump31feda52009-07-17 01:31:16 +00001486/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1487/// no successors or predecessors. If this is the first block created in the
1488/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001489CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001490 bool first_block = begin() == end();
1491
1492 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001493 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001494
1495 // If this is the first block, set it as the Entry and Exit.
1496 if (first_block) Entry = Exit = &front();
1497
1498 // Return the block.
1499 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001500}
1501
Ted Kremenek889073f2007-08-23 16:51:22 +00001502/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1503/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001504CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001505 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001506 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001507}
1508
1509/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001510void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1511
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001512//===----------------------------------------------------------------------===//
1513// CFG: Queries for BlkExprs.
1514//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001515
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001516namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001517 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001518}
1519
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001520static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1521 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001522 return;
Mike Stump31feda52009-07-17 01:31:16 +00001523
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001524 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001525 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001526
Ted Kremenek95a123c2008-01-26 00:03:27 +00001527 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1528 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001529
Ted Kremenek95a123c2008-01-26 00:03:27 +00001530 FindSubExprAssignments(*I, Set);
1531 }
1532}
1533
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001534static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1535 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001536
1537 // Look for assignments that are used as subexpressions. These are the only
1538 // assignments that we want to *possibly* register as a block-level
1539 // expression. Basically, if an assignment occurs both in a subexpression and
1540 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001541 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001542
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001543 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1544 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001545 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001546
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001547 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001548
1549 // Iterate over the statements again on identify the Expr* and Stmt* at the
1550 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001551
Ted Kremenek95a123c2008-01-26 00:03:27 +00001552 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001553 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001554
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001555 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001556 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001557 // expression are really "statements" whose value is never used by
1558 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001559 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001560 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001561 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1562 // Special handling for statement expressions. The last statement in
1563 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001564 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001565 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001566 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001567 (*M)[C->body_back()] = x;
1568 }
1569 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001570
Ted Kremenek95a123c2008-01-26 00:03:27 +00001571 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001572 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001573 }
Mike Stump31feda52009-07-17 01:31:16 +00001574
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001575 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001576
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001577 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001578
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001579 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001580 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001581 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001582 }
1583 }
Mike Stump31feda52009-07-17 01:31:16 +00001584
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001585 return M;
1586}
1587
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001588CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1589 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001590 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001592 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001593 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001594
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001595 if (I == M->end()) return CFG::BlkExprNumTy();
1596 else return CFG::BlkExprNumTy(I->second);
1597}
1598
1599unsigned CFG::getNumBlkExprs() {
1600 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1601 return M->size();
1602 else {
1603 // We assume callers interested in the number of BlkExprs will want
1604 // the map constructed if it doesn't already exist.
1605 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1606 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1607 }
1608}
1609
Ted Kremenek6065ef62008-04-28 18:00:46 +00001610//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001611// Cleanup: CFG dstor.
1612//===----------------------------------------------------------------------===//
1613
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001614CFG::~CFG() {
1615 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1616}
Mike Stump31feda52009-07-17 01:31:16 +00001617
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001618//===----------------------------------------------------------------------===//
1619// CFG pretty printing
1620//===----------------------------------------------------------------------===//
1621
Ted Kremenek7e776b12007-08-22 18:22:34 +00001622namespace {
1623
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001624class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001625
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001626 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1627 StmtMapTy StmtMap;
1628 signed CurrentBlock;
1629 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001630 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001631public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001632
Chris Lattnerc61089a2009-06-30 01:26:17 +00001633 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1634 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001635 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1636 unsigned j = 1;
1637 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1638 BI != BEnd; ++BI, ++j )
1639 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1640 }
1641 }
Mike Stump31feda52009-07-17 01:31:16 +00001642
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001643 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001644
Chris Lattnerc61089a2009-06-30 01:26:17 +00001645 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001646 void setBlockID(signed i) { CurrentBlock = i; }
1647 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001649 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001650
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001651 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001652
1653 if (I == StmtMap.end())
1654 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001655
1656 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001657 && I->second.second == CurrentStmt)
1658 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001659
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001660 OS << "[B" << I->second.first << "." << I->second.second << "]";
1661 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001662 }
1663};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001664} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001665
Chris Lattnerc61089a2009-06-30 01:26:17 +00001666
1667namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001668class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1669 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001671 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001672 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001673 PrintingPolicy Policy;
1674
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001675public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001676 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001677 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001678 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001679
Ted Kremenek9aae5132007-08-23 21:42:29 +00001680 void VisitIfStmt(IfStmt* I) {
1681 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001682 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001683 }
Mike Stump31feda52009-07-17 01:31:16 +00001684
Ted Kremenek9aae5132007-08-23 21:42:29 +00001685 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001686 void VisitStmt(Stmt* Terminator) {
1687 Terminator->printPretty(OS, Helper, Policy);
1688 }
1689
Ted Kremenek9aae5132007-08-23 21:42:29 +00001690 void VisitForStmt(ForStmt* F) {
1691 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001692 if (F->getInit()) OS << "...";
1693 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001694 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001695 OS << "; ";
1696 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001697 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001698 }
Mike Stump31feda52009-07-17 01:31:16 +00001699
Ted Kremenek9aae5132007-08-23 21:42:29 +00001700 void VisitWhileStmt(WhileStmt* W) {
1701 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001702 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001703 }
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenek9aae5132007-08-23 21:42:29 +00001705 void VisitDoStmt(DoStmt* D) {
1706 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001707 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001708 }
Mike Stump31feda52009-07-17 01:31:16 +00001709
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001710 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001711 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001712 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001713 }
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001715 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001716 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001717 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001718 }
Mike Stump31feda52009-07-17 01:31:16 +00001719
Ted Kremenek391f94a2007-08-31 22:29:13 +00001720 void VisitChooseExpr(ChooseExpr* C) {
1721 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001722 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001723 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001724 }
Mike Stump31feda52009-07-17 01:31:16 +00001725
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001726 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1727 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001728 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001729 }
Mike Stump31feda52009-07-17 01:31:16 +00001730
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001731 void VisitBinaryOperator(BinaryOperator* B) {
1732 if (!B->isLogicalOp()) {
1733 VisitExpr(B);
1734 return;
1735 }
Mike Stump31feda52009-07-17 01:31:16 +00001736
Douglas Gregor7de59662009-05-29 20:38:28 +00001737 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001738
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001739 switch (B->getOpcode()) {
1740 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001741 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001742 return;
1743 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001744 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001745 return;
1746 default:
1747 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001748 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001749 }
Mike Stump31feda52009-07-17 01:31:16 +00001750
Ted Kremenek12687ff2007-08-27 21:54:41 +00001751 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001752 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001753 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001754};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001755} // end anonymous namespace
1756
Mike Stump31feda52009-07-17 01:31:16 +00001757
Chris Lattnerc61089a2009-06-30 01:26:17 +00001758static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1759 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001760 if (Helper) {
1761 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001762 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001763 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001764
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001765 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001766 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001767 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001768 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001769 return;
1770 }
1771 }
Mike Stump31feda52009-07-17 01:31:16 +00001772
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001773 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001774 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001775 if (B->getOpcode() == BinaryOperator::Comma) {
1776 OS << "... , ";
1777 Helper->handledStmt(B->getRHS(),OS);
1778 OS << '\n';
1779 return;
Mike Stump31feda52009-07-17 01:31:16 +00001780 }
1781 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001782 }
Mike Stump31feda52009-07-17 01:31:16 +00001783
Chris Lattnerc61089a2009-06-30 01:26:17 +00001784 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001785
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001786 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001787 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001788}
Mike Stump31feda52009-07-17 01:31:16 +00001789
Chris Lattnerc61089a2009-06-30 01:26:17 +00001790static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1791 const CFGBlock& B,
1792 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001793
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001794 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001796 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001797 OS << "\n [ B" << B.getBlockID();
1798
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001799 if (&B == &cfg->getEntry())
1800 OS << " (ENTRY) ]\n";
1801 else if (&B == &cfg->getExit())
1802 OS << " (EXIT) ]\n";
1803 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001804 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001805 else
1806 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremenek71eca012007-08-29 23:20:49 +00001808 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001809 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001810
1811 if (print_edges)
1812 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001813
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001814 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001815 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001816 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001817 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001818 C->getLHS()->printPretty(OS, Helper,
1819 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001820 if (C->getRHS()) {
1821 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001822 C->getRHS()->printPretty(OS, Helper,
1823 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001824 }
Mike Stump31feda52009-07-17 01:31:16 +00001825 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001826 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001827 else
1828 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001829
Ted Kremenek71eca012007-08-29 23:20:49 +00001830 OS << ":\n";
1831 }
Mike Stump31feda52009-07-17 01:31:16 +00001832
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001833 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001834 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001835
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001836 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1837 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001838
Ted Kremenek71eca012007-08-29 23:20:49 +00001839 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001840 if (print_edges)
1841 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001842
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001843 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001844
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001845 if (Helper)
1846 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001847
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001848 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001849 }
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenek71eca012007-08-29 23:20:49 +00001851 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001852 if (B.getTerminator()) {
1853 if (print_edges)
1854 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenek71eca012007-08-29 23:20:49 +00001856 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001857
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001858 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001859
Chris Lattnerc61089a2009-06-30 01:26:17 +00001860 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1861 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001862 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001863 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001864 }
Mike Stump31feda52009-07-17 01:31:16 +00001865
Ted Kremenek71eca012007-08-29 23:20:49 +00001866 if (print_edges) {
1867 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001868 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001869 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001870
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001871 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1872 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001873
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001874 if (i == 8 || (i-8) == 0)
1875 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001876
Ted Kremenek71eca012007-08-29 23:20:49 +00001877 OS << " B" << (*I)->getBlockID();
1878 }
Mike Stump31feda52009-07-17 01:31:16 +00001879
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001880 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001882 // Print the successors of this block.
1883 OS << " Successors (" << B.succ_size() << "):";
1884 i = 0;
1885
1886 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1887 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001888
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001889 if (i == 8 || (i-8) % 10 == 0)
1890 OS << "\n ";
1891
Mike Stump0d76d072009-07-20 23:24:15 +00001892 if (*I)
1893 OS << " B" << (*I)->getBlockID();
1894 else
1895 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001896 }
Mike Stump31feda52009-07-17 01:31:16 +00001897
Ted Kremenek71eca012007-08-29 23:20:49 +00001898 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001899 }
Mike Stump31feda52009-07-17 01:31:16 +00001900}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001901
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001902
1903/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001904void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001905
1906/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001907void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1908 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001909
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001910 // Print the entry block.
1911 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001912
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913 // Iterate through the CFGBlocks and print them one by one.
1914 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1915 // Skip the entry block, because we already printed it.
1916 if (&(*I) == &getEntry() || &(*I) == &getExit())
1917 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001919 print_block(OS, this, *I, &Helper, true);
1920 }
Mike Stump31feda52009-07-17 01:31:16 +00001921
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001922 // Print the exit block.
1923 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001924 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001925}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001926
1927/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001928void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1929 print(llvm::errs(), cfg, LO);
1930}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001931
1932/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1933/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001934void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1935 const LangOptions &LO) const {
1936 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001937 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001938}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001939
Ted Kremenek15647632008-01-30 23:02:42 +00001940/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001941void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001942 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001943 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001944 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1945}
1946
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001947Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001948
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001949 if (!Terminator)
1950 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001951
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001952 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001953
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001954 switch (Terminator->getStmtClass()) {
1955 default:
1956 break;
Mike Stump31feda52009-07-17 01:31:16 +00001957
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001958 case Stmt::ForStmtClass:
1959 E = cast<ForStmt>(Terminator)->getCond();
1960 break;
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001962 case Stmt::WhileStmtClass:
1963 E = cast<WhileStmt>(Terminator)->getCond();
1964 break;
Mike Stump31feda52009-07-17 01:31:16 +00001965
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001966 case Stmt::DoStmtClass:
1967 E = cast<DoStmt>(Terminator)->getCond();
1968 break;
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001970 case Stmt::IfStmtClass:
1971 E = cast<IfStmt>(Terminator)->getCond();
1972 break;
Mike Stump31feda52009-07-17 01:31:16 +00001973
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001974 case Stmt::ChooseExprClass:
1975 E = cast<ChooseExpr>(Terminator)->getCond();
1976 break;
Mike Stump31feda52009-07-17 01:31:16 +00001977
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001978 case Stmt::IndirectGotoStmtClass:
1979 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1980 break;
Mike Stump31feda52009-07-17 01:31:16 +00001981
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001982 case Stmt::SwitchStmtClass:
1983 E = cast<SwitchStmt>(Terminator)->getCond();
1984 break;
Mike Stump31feda52009-07-17 01:31:16 +00001985
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001986 case Stmt::ConditionalOperatorClass:
1987 E = cast<ConditionalOperator>(Terminator)->getCond();
1988 break;
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001990 case Stmt::BinaryOperatorClass: // '&&' and '||'
1991 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001992 break;
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001994 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00001995 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001996 }
Mike Stump31feda52009-07-17 01:31:16 +00001997
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001998 return E ? E->IgnoreParens() : NULL;
1999}
2000
Ted Kremenek92137a32008-05-16 16:06:00 +00002001bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002002
Ted Kremenek92137a32008-05-16 16:06:00 +00002003 if (!Terminator)
2004 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenek92137a32008-05-16 16:06:00 +00002006 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002007
Ted Kremenek92137a32008-05-16 16:06:00 +00002008 switch (Terminator->getStmtClass()) {
2009 default:
2010 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002011
2012 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002013 case Stmt::WhileStmtClass:
2014 case Stmt::DoStmtClass:
2015 case Stmt::IfStmtClass:
2016 case Stmt::ChooseExprClass:
2017 case Stmt::ConditionalOperatorClass:
2018 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002019 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002020 }
Mike Stump31feda52009-07-17 01:31:16 +00002021
Ted Kremenek92137a32008-05-16 16:06:00 +00002022 return E ? E->IgnoreParens() : NULL;
2023}
2024
Ted Kremenek15647632008-01-30 23:02:42 +00002025
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002026//===----------------------------------------------------------------------===//
2027// CFG Graphviz Visualization
2028//===----------------------------------------------------------------------===//
2029
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002030
2031#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002032static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002033#endif
2034
Chris Lattnerc61089a2009-06-30 01:26:17 +00002035void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002036#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002037 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002038 GraphHelper = &H;
2039 llvm::ViewGraph(this,"CFG");
2040 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002041#endif
2042}
2043
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002044namespace llvm {
2045template<>
2046struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002047 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2048 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002049
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002050#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002051 std::string OutSStr;
2052 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002053 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002054 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002055
2056 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2057
2058 // Process string output to make it nicer...
2059 for (unsigned i = 0; i != OutStr.length(); ++i)
2060 if (OutStr[i] == '\n') { // Left justify
2061 OutStr[i] = '\\';
2062 OutStr.insert(OutStr.begin()+i+1, 'l');
2063 }
Mike Stump31feda52009-07-17 01:31:16 +00002064
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002065 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002066#else
2067 return "";
2068#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002069 }
2070};
2071} // end namespace llvm