blob: 1c1ca67be33d5884b49e1b12af281cf331cf1144 [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
Mike Stump773582d2009-07-23 23:25:26 +0000138 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
139 /// if we can evaluate to a known value, otherwise return -1.
140 int TryEvaluateBool(Expr *S) {
141 Expr::EvalResult Result;
142 if (S->Evaluate(Result, *Context)
143 && Result.Val.isInt()) {
144 if (Result.Val.getInt().getBoolValue())
145 return true;
146 else
147 return false;
148 }
149 return -1;
150 }
151
Ted Kremenekb64d1832008-03-13 03:04:22 +0000152 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000153};
Mike Stump31feda52009-07-17 01:31:16 +0000154
Douglas Gregor4619e432008-12-05 23:32:09 +0000155// FIXME: Add support for dependent-sized array types in C++?
156// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000157static VariableArrayType* FindVA(Type* t) {
158 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
159 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
160 if (vat->getSizeExpr())
161 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000162
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000163 t = vt->getElementType().getTypePtr();
164 }
Mike Stump31feda52009-07-17 01:31:16 +0000165
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000166 return 0;
167}
Mike Stump31feda52009-07-17 01:31:16 +0000168
169/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
170/// arbitrary statement. Examples include a single expression or a function
171/// body (compound statement). The ownership of the returned CFG is
172/// transferred to the caller. If CFG construction fails, this method returns
173/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000174CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
175 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000176 assert(cfg);
177 if (!Statement)
178 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000179
Ted Kremenekb64d1832008-03-13 03:04:22 +0000180 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000181
182 // Create an empty block that will serve as the exit block for the CFG. Since
183 // this is the first block added to the CFG, it will be implicitly registered
184 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000185 Succ = createBlock();
186 assert (Succ == &cfg->getExit());
187 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000188
Ted Kremenek9aae5132007-08-23 21:42:29 +0000189 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000190 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000191 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000192
Ted Kremenek40d87632008-02-27 17:33:02 +0000193 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000194 // Finalize the last constructed block. This usually involves reversing the
195 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000196 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000197
198 // Backpatch the gotos whose label -> block mappings we didn't know when we
199 // encountered them.
200 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000201 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000202
Ted Kremenek9aae5132007-08-23 21:42:29 +0000203 CFGBlock* B = *I;
204 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
205 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
206
207 // If there is no target for the goto, then we are looking at an
208 // incomplete AST. Handle this by not registering a successor.
209 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000210
211 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000212 }
Mike Stump31feda52009-07-17 01:31:16 +0000213
Ted Kremenekeda180e22007-08-28 19:26:49 +0000214 // Add successors to the Indirect Goto Dispatch block (if we have one).
215 if (CFGBlock* B = cfg->getIndirectGotoBlock())
216 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
217 E = AddressTakenLabels.end(); I != E; ++I ) {
218
219 // Lookup the target block.
220 LabelMapTy::iterator LI = LabelMap.find(*I);
221
222 // If there is no target block that contains label, then we are looking
223 // at an incomplete AST. Handle this by not registering a successor.
224 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000225
226 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000227 }
Mike Stump31feda52009-07-17 01:31:16 +0000228
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000229 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000230 }
Mike Stump31feda52009-07-17 01:31:16 +0000231
232 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000233 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000234
Ted Kremenekb64d1832008-03-13 03:04:22 +0000235 if (badCFG) {
236 delete cfg;
237 cfg = NULL;
238 return NULL;
239 }
Mike Stump31feda52009-07-17 01:31:16 +0000240
241 // NULL out cfg so that repeated calls to the builder will fail and that the
242 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000243 CFG* t = cfg;
244 cfg = NULL;
245 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000246}
Mike Stump31feda52009-07-17 01:31:16 +0000247
Ted Kremenek9aae5132007-08-23 21:42:29 +0000248/// createBlock - Used to lazily create blocks that are connected
249/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000250CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000251 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000252 if (add_successor && Succ)
253 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000254 return B;
255}
Mike Stump31feda52009-07-17 01:31:16 +0000256
257/// FinishBlock - When the last statement has been added to the block, we must
258/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000259bool CFGBuilder::FinishBlock(CFGBlock* B) {
260 if (badCFG)
261 return false;
262
Ted Kremenek93668002009-07-17 22:18:43 +0000263 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000264 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000265 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000266}
267
Ted Kremenek93668002009-07-17 22:18:43 +0000268/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000269/// blocks for ternary operators, &&, and ||. We also process "," and
270/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000271CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
272tryAgain:
273 switch (S->getStmtClass()) {
274 default:
275 return VisitStmt(S, alwaysAdd);
276
277 case Stmt::AddrLabelExprClass:
278 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
279
280 case Stmt::BinaryOperatorClass:
281 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
282
283 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000284 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000285
286 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000287 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000288
289 case Stmt::BreakStmtClass:
290 return VisitBreakStmt(cast<BreakStmt>(S));
291
292 case Stmt::CallExprClass:
293 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
294
295 case Stmt::CaseStmtClass:
296 return VisitCaseStmt(cast<CaseStmt>(S));
297
298 case Stmt::ChooseExprClass:
299 return VisitChooseExpr(cast<ChooseExpr>(S));
300
301 case Stmt::CompoundStmtClass:
302 return VisitCompoundStmt(cast<CompoundStmt>(S));
303
304 case Stmt::ConditionalOperatorClass:
305 return VisitConditionalOperator(cast<ConditionalOperator>(S));
306
307 case Stmt::ContinueStmtClass:
308 return VisitContinueStmt(cast<ContinueStmt>(S));
309
310 case Stmt::DeclStmtClass:
311 return VisitDeclStmt(cast<DeclStmt>(S));
312
313 case Stmt::DefaultStmtClass:
314 return VisitDefaultStmt(cast<DefaultStmt>(S));
315
316 case Stmt::DoStmtClass:
317 return VisitDoStmt(cast<DoStmt>(S));
318
319 case Stmt::ForStmtClass:
320 return VisitForStmt(cast<ForStmt>(S));
321
322 case Stmt::GotoStmtClass:
323 return VisitGotoStmt(cast<GotoStmt>(S));
324
325 case Stmt::IfStmtClass:
326 return VisitIfStmt(cast<IfStmt>(S));
327
328 case Stmt::IndirectGotoStmtClass:
329 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
330
331 case Stmt::LabelStmtClass:
332 return VisitLabelStmt(cast<LabelStmt>(S));
333
334 case Stmt::ObjCAtCatchStmtClass:
335 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
336
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000337 case Stmt::CXXThrowExprClass:
338 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
339
Ted Kremenek93668002009-07-17 22:18:43 +0000340 case Stmt::ObjCAtSynchronizedStmtClass:
341 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
342
343 case Stmt::ObjCAtThrowStmtClass:
344 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
345
346 case Stmt::ObjCAtTryStmtClass:
347 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
348
349 case Stmt::ObjCForCollectionStmtClass:
350 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
351
352 case Stmt::ParenExprClass:
353 S = cast<ParenExpr>(S)->getSubExpr();
354 goto tryAgain;
355
356 case Stmt::NullStmtClass:
357 return Block;
358
359 case Stmt::ReturnStmtClass:
360 return VisitReturnStmt(cast<ReturnStmt>(S));
361
362 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000363 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000364
365 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000366 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000367
368 case Stmt::SwitchStmtClass:
369 return VisitSwitchStmt(cast<SwitchStmt>(S));
370
371 case Stmt::WhileStmtClass:
372 return VisitWhileStmt(cast<WhileStmt>(S));
373 }
374}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000375
Ted Kremenek93668002009-07-17 22:18:43 +0000376CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
377 if (alwaysAdd) {
378 autoCreateBlock();
379 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000380 }
Ted Kremenek93668002009-07-17 22:18:43 +0000381
382 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000383}
Mike Stump31feda52009-07-17 01:31:16 +0000384
Ted Kremenek93668002009-07-17 22:18:43 +0000385/// VisitChildren - Visit the children of a Stmt.
386CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
387 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000388 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000389 E = Terminator->child_end(); I != E; ++I) {
390 if (*I) B = Visit(*I);
391 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000392 return B;
393}
Ted Kremenek93668002009-07-17 22:18:43 +0000394
395CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
396 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000397
Ted Kremenek93668002009-07-17 22:18:43 +0000398 if (alwaysAdd) {
399 autoCreateBlock();
400 Block->appendStmt(A);
401 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000402
Ted Kremenek9aae5132007-08-23 21:42:29 +0000403 return Block;
404}
Ted Kremenek93668002009-07-17 22:18:43 +0000405
406CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
407 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000408 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
409 ConfluenceBlock->appendStmt(B);
410
411 if (!FinishBlock(ConfluenceBlock))
412 return 0;
413
414 // create the block evaluating the LHS
415 CFGBlock* LHSBlock = createBlock(false);
416 LHSBlock->setTerminator(B);
417
418 // create the block evaluating the RHS
419 Succ = ConfluenceBlock;
420 Block = NULL;
421 CFGBlock* RHSBlock = addStmt(B->getRHS());
422 if (!FinishBlock(RHSBlock))
423 return 0;
424
Mike Stump773582d2009-07-23 23:25:26 +0000425 // See if this is a known constant.
426 int KnownVal = TryEvaluateBool(B->getLHS());
427 if (KnownVal != -1 && (B->getOpcode() == BinaryOperator::LOr))
428 KnownVal = !KnownVal;
429
Ted Kremenek93668002009-07-17 22:18:43 +0000430 // Now link the LHSBlock with RHSBlock.
431 if (B->getOpcode() == BinaryOperator::LOr) {
Mike Stump773582d2009-07-23 23:25:26 +0000432 if (KnownVal == true)
Mike Stump0d76d072009-07-20 23:24:15 +0000433 LHSBlock->addSuccessor(0);
434 else
435 LHSBlock->addSuccessor(ConfluenceBlock);
Mike Stump773582d2009-07-23 23:25:26 +0000436 if (KnownVal == false)
Mike Stump0d76d072009-07-20 23:24:15 +0000437 LHSBlock->addSuccessor(0);
438 else
439 LHSBlock->addSuccessor(RHSBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000440 } else {
441 assert (B->getOpcode() == BinaryOperator::LAnd);
Mike Stump773582d2009-07-23 23:25:26 +0000442 if (KnownVal == false)
Mike Stump0d76d072009-07-20 23:24:15 +0000443 LHSBlock->addSuccessor(0);
444 else
445 LHSBlock->addSuccessor(RHSBlock);
Mike Stump773582d2009-07-23 23:25:26 +0000446 if (KnownVal == true)
Mike Stump0d76d072009-07-20 23:24:15 +0000447 LHSBlock->addSuccessor(0);
448 else
449 LHSBlock->addSuccessor(ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000450 }
451
452 // Generate the blocks for evaluating the LHS.
453 Block = LHSBlock;
454 return addStmt(B->getLHS());
455 }
456 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000457 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000458 Block->appendStmt(B);
459 addStmt(B->getRHS());
460 return addStmt(B->getLHS());
461 }
462
463 return VisitStmt(B, alwaysAdd);
464}
465
Ted Kremenek0747de62009-07-18 00:47:21 +0000466CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000467 // FIXME
468 return NYS();
469}
470
Ted Kremenek0747de62009-07-18 00:47:21 +0000471CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
472 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000473 // FIXME
474 return NYS();
475}
476
477CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
478 // "break" is a control-flow statement. Thus we stop processing the current
479 // block.
480 if (Block && !FinishBlock(Block))
481 return 0;
482
483 // Now create a new block that ends with the break statement.
484 Block = createBlock(false);
485 Block->setTerminator(B);
486
487 // If there is no target for the break, then we are looking at an incomplete
488 // AST. This means that the CFG cannot be constructed.
489 if (BreakTargetBlock)
490 Block->addSuccessor(BreakTargetBlock);
491 else
492 badCFG = true;
493
494
Ted Kremenek9aae5132007-08-23 21:42:29 +0000495 return Block;
496}
Ted Kremenek93668002009-07-17 22:18:43 +0000497
498CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
499 // If this is a call to a no-return function, this stops the block here.
500 if (FunctionDecl *FD = C->getDirectCallee()) {
501
502 if (!FD->hasAttr<NoReturnAttr>())
503 return VisitStmt(C, alwaysAdd);
504
505 if (Block && !FinishBlock(Block))
506 return 0;
507
508 // Create new block with no successor for the remaining pieces.
509 Block = createBlock(false);
510 Block->appendStmt(C);
511
512 // Wire this to the exit block directly.
513 Block->addSuccessor(&cfg->getExit());
514
515 return VisitChildren(C);
516 }
517
518 return VisitStmt(C, alwaysAdd);
519}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000520
Ted Kremenek21822592009-07-17 18:20:32 +0000521CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
522 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
523 ConfluenceBlock->appendStmt(C);
524 if (!FinishBlock(ConfluenceBlock))
525 return 0;
526
527 Succ = ConfluenceBlock;
528 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000529 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000530 if (!FinishBlock(LHSBlock))
531 return 0;
532
533 Succ = ConfluenceBlock;
534 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000535 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000536 if (!FinishBlock(RHSBlock))
537 return 0;
538
539 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000540 // See if this is a known constant.
541 int KnownVal = TryEvaluateBool(C->getCond());
542 if (KnownVal == false)
Mike Stump3557ea82009-07-21 01:46:17 +0000543 Block->addSuccessor(0);
544 else
545 Block->addSuccessor(LHSBlock);
Mike Stump773582d2009-07-23 23:25:26 +0000546 if (KnownVal == true)
Mike Stump3557ea82009-07-21 01:46:17 +0000547 Block->addSuccessor(0);
548 else
549 Block->addSuccessor(RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000550 Block->setTerminator(C);
551 return addStmt(C->getCond());
552}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000553
Ted Kremenek93668002009-07-17 22:18:43 +0000554
555CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
556 CFGBlock* LastBlock = Block;
557
558 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
559 I != E; ++I ) {
560 LastBlock = addStmt(*I);
561 }
562 return LastBlock;
563}
564
Ted Kremenek51d40b02009-07-17 18:15:54 +0000565CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
566 // Create the confluence block that will "merge" the results of the ternary
567 // expression.
568 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
569 ConfluenceBlock->appendStmt(C);
570 if (!FinishBlock(ConfluenceBlock))
571 return 0;
572
573 // Create a block for the LHS expression if there is an LHS expression. A
574 // GCC extension allows LHS to be NULL, causing the condition to be the
575 // value that is returned instead.
576 // e.g: x ?: y is shorthand for: x ? x : y;
577 Succ = ConfluenceBlock;
578 Block = NULL;
579 CFGBlock* LHSBlock = NULL;
580 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000581 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000582 if (!FinishBlock(LHSBlock))
583 return 0;
584 Block = NULL;
585 }
586
587 // Create the block for the RHS expression.
588 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000589 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000590 if (!FinishBlock(RHSBlock))
591 return 0;
592
593 // Create the block that will contain the condition.
594 Block = createBlock(false);
595
Mike Stump773582d2009-07-23 23:25:26 +0000596 // See if this is a known constant.
597 int KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000598 if (LHSBlock) {
Mike Stump773582d2009-07-23 23:25:26 +0000599 if (KnownVal == false)
Mike Stump0d76d072009-07-20 23:24:15 +0000600 Block->addSuccessor(0);
601 else
602 Block->addSuccessor(LHSBlock);
603 } else {
Mike Stump773582d2009-07-23 23:25:26 +0000604 if (KnownVal == false) {
Mike Stump0d76d072009-07-20 23:24:15 +0000605 // If we know the condition is false, add NULL as the successor for
606 // the block containing the condition. In this case, the confluence
607 // block will have just one predecessor.
608 Block->addSuccessor(0);
609 assert (ConfluenceBlock->pred_size() == 1);
610 } else {
611 // If we have no LHS expression, add the ConfluenceBlock as a direct
612 // successor for the block containing the condition. Moreover, we need to
613 // reverse the order of the predecessors in the ConfluenceBlock because
614 // the RHSBlock will have been added to the succcessors already, and we
615 // want the first predecessor to the the block containing the expression
616 // for the case when the ternary expression evaluates to true.
617 Block->addSuccessor(ConfluenceBlock);
618 assert (ConfluenceBlock->pred_size() == 2);
619 std::reverse(ConfluenceBlock->pred_begin(),
620 ConfluenceBlock->pred_end());
621 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000622 }
623
Mike Stump773582d2009-07-23 23:25:26 +0000624 if (KnownVal == true)
Mike Stump0d76d072009-07-20 23:24:15 +0000625 Block->addSuccessor(0);
626 else
627 Block->addSuccessor(RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000628
629 Block->setTerminator(C);
630 return addStmt(C->getCond());
631}
632
Ted Kremenek93668002009-07-17 22:18:43 +0000633CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
634 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000635
Ted Kremenek93668002009-07-17 22:18:43 +0000636 if (DS->isSingleDecl()) {
637 Block->appendStmt(DS);
638 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000639 }
Ted Kremenek93668002009-07-17 22:18:43 +0000640
641 CFGBlock *B = 0;
642
643 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
644 typedef llvm::SmallVector<Decl*,10> BufTy;
645 BufTy Buf(DS->decl_begin(), DS->decl_end());
646
647 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
648 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
649 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
650 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
651
652 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
653 // automatically freed with the CFG.
654 DeclGroupRef DG(*I);
655 Decl *D = *I;
656 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
657 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
658
659 // Append the fake DeclStmt to block.
660 Block->appendStmt(DSNew);
661 B = VisitDeclSubExpr(D);
662 }
663
664 return B;
665}
666
667/// VisitDeclSubExpr - Utility method to add block-level expressions for
668/// initializers in Decls.
669CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
670 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000671
Ted Kremenek93668002009-07-17 22:18:43 +0000672 VarDecl *VD = dyn_cast<VarDecl>(D);
673
674 if (!VD)
675 return Block;
676
677 Expr *Init = VD->getInit();
678
679 if (Init) {
680 // Optimization: Don't create separate block-level statements for literals.
681 switch (Init->getStmtClass()) {
682 case Stmt::IntegerLiteralClass:
683 case Stmt::CharacterLiteralClass:
684 case Stmt::StringLiteralClass:
685 break;
686 default:
687 Block = addStmt(Init);
688 }
689 }
690
691 // If the type of VD is a VLA, then we must process its size expressions.
692 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
693 VA = FindVA(VA->getElementType().getTypePtr()))
694 Block = addStmt(VA->getSizeExpr());
695
696 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000697}
698
699CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000700 // We may see an if statement in the middle of a basic block, or it may be the
701 // first statement we are processing. In either case, we create a new basic
702 // block. First, we create the blocks for the then...else statements, and
703 // then we create the block containing the if statement. If we were in the
704 // middle of a block, we stop processing that block and reverse its
705 // statements. That block is then the implicit successor for the "then" and
706 // "else" clauses.
707
708 // The block we were proccessing is now finished. Make it the successor
709 // block.
710 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000711 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000712 if (!FinishBlock(Block))
713 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000714 }
Mike Stump31feda52009-07-17 01:31:16 +0000715
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000716 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000717 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000718
Ted Kremenek9aae5132007-08-23 21:42:29 +0000719 if (Stmt* Else = I->getElse()) {
720 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000721
Ted Kremenek9aae5132007-08-23 21:42:29 +0000722 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000723 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000724 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000725 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000726
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000727 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
728 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000729 else if (Block) {
730 if (!FinishBlock(ElseBlock))
731 return 0;
732 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000733 }
Mike Stump31feda52009-07-17 01:31:16 +0000734
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000735 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000736 CFGBlock* ThenBlock;
737 {
738 Stmt* Then = I->getThen();
739 assert (Then);
740 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000741 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000742 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000743
Ted Kremenek1b379512009-04-01 03:52:47 +0000744 if (!ThenBlock) {
745 // We can reach here if the "then" body has all NullStmts.
746 // Create an empty block so we can distinguish between true and false
747 // branches in path-sensitive analyses.
748 ThenBlock = createBlock(false);
749 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000750 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000751 if (!FinishBlock(ThenBlock))
752 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000753 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000754 }
755
Mike Stump31feda52009-07-17 01:31:16 +0000756 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000757 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000758
Ted Kremenek9aae5132007-08-23 21:42:29 +0000759 // Set the terminator of the new block to the If statement.
760 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000761
Mike Stump773582d2009-07-23 23:25:26 +0000762 // See if this is a known constant.
763 int KnownVal = TryEvaluateBool(I->getCond());
764
Ted Kremenek9aae5132007-08-23 21:42:29 +0000765 // Now add the successors.
Mike Stump773582d2009-07-23 23:25:26 +0000766 if (KnownVal == false)
Mike Stump0d76d072009-07-20 23:24:15 +0000767 Block->addSuccessor(0);
768 else
769 Block->addSuccessor(ThenBlock);
Mike Stump773582d2009-07-23 23:25:26 +0000770 if (KnownVal == true)
Mike Stump0d76d072009-07-20 23:24:15 +0000771 Block->addSuccessor(0);
772 else
773 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000774
775 // Add the condition as the last statement in the new block. This may create
776 // new blocks as the condition may contain control-flow. Any newly created
777 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000778 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000779}
Mike Stump31feda52009-07-17 01:31:16 +0000780
781
Ted Kremenek9aae5132007-08-23 21:42:29 +0000782CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000783 // If we were in the middle of a block we stop processing that block and
784 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000785 //
Mike Stump31feda52009-07-17 01:31:16 +0000786 // NOTE: If a "return" appears in the middle of a block, this means that the
787 // code afterwards is DEAD (unreachable). We still keep a basic block
788 // for that code; a simple "mark-and-sweep" from the entry block will be
789 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000790 if (Block) FinishBlock(Block);
791
792 // Create the new block.
793 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000794
Ted Kremenek9aae5132007-08-23 21:42:29 +0000795 // The Exit block is the only successor.
796 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000797
798 // Add the return statement to the block. This may create new blocks if R
799 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000800 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000801}
802
803CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
804 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000805 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000806 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000807
Ted Kremenek93668002009-07-17 22:18:43 +0000808 if (!LabelBlock) // This can happen when the body is empty, i.e.
809 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000810
Ted Kremenek93668002009-07-17 22:18:43 +0000811 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000812 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000813
814 // Labels partition blocks, so this is the end of the basic block we were
815 // processing (L is the block's label). Because this is label (and we have
816 // already processed the substatement) there is no extra control-flow to worry
817 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000818 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000819 if (!FinishBlock(LabelBlock))
820 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000821
822 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000823 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000824
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825 // This block is now the implicit successor of other blocks.
826 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000827
Ted Kremenek9aae5132007-08-23 21:42:29 +0000828 return LabelBlock;
829}
830
831CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000832 // Goto is a control-flow statement. Thus we stop processing the current
833 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000834 if (Block)
835 FinishBlock(Block);
836
Ted Kremenek9aae5132007-08-23 21:42:29 +0000837 Block = createBlock(false);
838 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000839
840 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000841 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000842
Ted Kremenek9aae5132007-08-23 21:42:29 +0000843 if (I == LabelMap.end())
844 // We will need to backpatch this block later.
845 BackpatchBlocks.push_back(Block);
846 else
847 Block->addSuccessor(I->second);
848
Mike Stump31feda52009-07-17 01:31:16 +0000849 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000850}
851
852CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000853 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000854
Mike Stump014b3ea2009-07-21 01:12:51 +0000855 // "for" is a control-flow statement. Thus we stop processing the current
856 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000857 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000858 if (!FinishBlock(Block))
859 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000860 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000861 } else
862 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000863
864 // Because of short-circuit evaluation, the condition of the loop can span
865 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
866 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000867 CFGBlock* ExitConditionBlock = createBlock(false);
868 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000869
Ted Kremenek81e14852007-08-27 19:46:09 +0000870 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000871 ExitConditionBlock->setTerminator(F);
872
873 // Now add the actual condition to the condition block. Because the condition
874 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000875 if (Stmt* C = F->getCond()) {
876 Block = ExitConditionBlock;
877 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000878 if (Block) {
879 if (!FinishBlock(EntryConditionBlock))
880 return 0;
881 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000882 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000883
Mike Stump31feda52009-07-17 01:31:16 +0000884 // The condition block is the implicit successor for the loop body as well as
885 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000886 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000887
Mike Stump773582d2009-07-23 23:25:26 +0000888 // See if this is a known constant.
889 int KnownVal = true;
890 if (F->getCond())
891 KnownVal = TryEvaluateBool(F->getCond());
892
Ted Kremenek9aae5132007-08-23 21:42:29 +0000893 // Now create the loop body.
894 {
895 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000896
Ted Kremenek9aae5132007-08-23 21:42:29 +0000897 // Save the current values for Block, Succ, and continue and break targets
898 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
899 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000900 save_break(BreakTargetBlock);
901
Ted Kremeneke9610502007-08-30 18:39:40 +0000902 // Create a new block to contain the (bottom) of the loop body.
903 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000904
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000905 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000906 // Generate increment code in its own basic block. This is the target of
907 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000908 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000909 } else {
910 // No increment code. Create a special, empty, block that is used as the
911 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000912 assert(Succ == EntryConditionBlock);
913 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000914 }
Mike Stump31feda52009-07-17 01:31:16 +0000915
Ted Kremenek902393b2009-04-28 00:51:56 +0000916 // Finish up the increment (or empty) block if it hasn't been already.
917 if (Block) {
918 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000919 if (!FinishBlock(Block))
920 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000921 Block = 0;
922 }
Mike Stump31feda52009-07-17 01:31:16 +0000923
Ted Kremenek902393b2009-04-28 00:51:56 +0000924 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000925
Ted Kremenek902393b2009-04-28 00:51:56 +0000926 // The starting block for the loop increment is the block that should
927 // represent the 'loop target' for looping back to the start of the loop.
928 ContinueTargetBlock->setLoopTarget(F);
929
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000930 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000931 BreakTargetBlock = LoopSuccessor;
932
933 // Now populate the body block, and in the process create new blocks as we
934 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000935 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000936
937 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000938 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000939 else if (Block) {
940 if (!FinishBlock(BodyBlock))
941 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000942 }
943
Mike Stump773582d2009-07-23 23:25:26 +0000944 if (KnownVal == false)
Mike Stump014b3ea2009-07-21 01:12:51 +0000945 ExitConditionBlock->addSuccessor(0);
946 else {
947 // This new body block is a successor to our "exit" condition block.
948 ExitConditionBlock->addSuccessor(BodyBlock);
949 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000950 }
Mike Stump31feda52009-07-17 01:31:16 +0000951
Mike Stump773582d2009-07-23 23:25:26 +0000952 if (KnownVal == true)
Mike Stump014b3ea2009-07-21 01:12:51 +0000953 ExitConditionBlock->addSuccessor(0);
954 else {
955 // Link up the condition block with the code that follows the loop. (the
956 // false branch).
957 ExitConditionBlock->addSuccessor(LoopSuccessor);
958 }
Mike Stump31feda52009-07-17 01:31:16 +0000959
Ted Kremenek9aae5132007-08-23 21:42:29 +0000960 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000961 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000962 if (Stmt* I = F->getInit()) {
963 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000964 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000965 } else {
966 // There is no loop initialization. We are thus basically a while loop.
967 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000968 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000969 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000970 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000971 }
972}
973
Ted Kremenek9d56e642008-11-11 17:10:00 +0000974CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
975 // Objective-C fast enumeration 'for' statements:
976 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
977 //
978 // for ( Type newVariable in collection_expression ) { statements }
979 //
980 // becomes:
981 //
982 // prologue:
983 // 1. collection_expression
984 // T. jump to loop_entry
985 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000986 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000987 // 1. ObjCForCollectionStmt [performs binding to newVariable]
988 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
989 // TB:
990 // statements
991 // T. jump to loop_entry
992 // FB:
993 // what comes after
994 //
995 // and
996 //
997 // Type existingItem;
998 // for ( existingItem in expression ) { statements }
999 //
1000 // becomes:
1001 //
Mike Stump31feda52009-07-17 01:31:16 +00001002 // the same with newVariable replaced with existingItem; the binding works
1003 // the same except that for one ObjCForCollectionStmt::getElement() returns
1004 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001005 //
Mike Stump31feda52009-07-17 01:31:16 +00001006
Ted Kremenek9d56e642008-11-11 17:10:00 +00001007 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001008
Ted Kremenek9d56e642008-11-11 17:10:00 +00001009 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001010 if (!FinishBlock(Block))
1011 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001012 LoopSuccessor = Block;
1013 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001014 } else
1015 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001016
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001017 // Build the condition blocks.
1018 CFGBlock* ExitConditionBlock = createBlock(false);
1019 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001020
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001021 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001022 ExitConditionBlock->setTerminator(S);
1023
1024 // The last statement in the block should be the ObjCForCollectionStmt, which
1025 // performs the actual binding to 'element' and determines if there are any
1026 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001027 ExitConditionBlock->appendStmt(S);
1028 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001029
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001030 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001031 // generate new blocks as necesary. We DON'T add the statement by default to
1032 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001033 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001034 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001035 if (!FinishBlock(EntryConditionBlock))
1036 return 0;
1037 Block = 0;
1038 }
Mike Stump31feda52009-07-17 01:31:16 +00001039
1040 // The condition block is the implicit successor for the loop body as well as
1041 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001042 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001043
Ted Kremenek9d56e642008-11-11 17:10:00 +00001044 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001045 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001046 // Save the current values for Succ, continue and break targets.
1047 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001048 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1049
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001051 ContinueTargetBlock = EntryConditionBlock;
1052
Ted Kremenek93668002009-07-17 22:18:43 +00001053 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001054
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001055 if (!BodyBlock)
1056 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001057 else if (Block) {
1058 if (!FinishBlock(BodyBlock))
1059 return 0;
1060 }
Mike Stump31feda52009-07-17 01:31:16 +00001061
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001062 // This new body block is a successor to our "exit" condition block.
1063 ExitConditionBlock->addSuccessor(BodyBlock);
1064 }
Mike Stump31feda52009-07-17 01:31:16 +00001065
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001066 // Link up the condition block with the code that follows the loop.
1067 // (the false branch).
1068 ExitConditionBlock->addSuccessor(LoopSuccessor);
1069
Ted Kremenek9d56e642008-11-11 17:10:00 +00001070 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001071 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001072 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001073}
1074
Ted Kremenek49805452009-05-02 01:49:13 +00001075CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1076 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001077
Ted Kremenek49805452009-05-02 01:49:13 +00001078 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001079 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001080
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001081 // The sync body starts its own basic block. This makes it a little easier
1082 // for diagnostic clients.
1083 if (SyncBlock) {
1084 if (!FinishBlock(SyncBlock))
1085 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001086
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001087 Block = 0;
1088 }
Mike Stump31feda52009-07-17 01:31:16 +00001089
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001090 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001091
Ted Kremenek49805452009-05-02 01:49:13 +00001092 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001093 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001094}
Mike Stump31feda52009-07-17 01:31:16 +00001095
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001096CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001097 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001098 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001099}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001100
Ted Kremenek9aae5132007-08-23 21:42:29 +00001101CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001102 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001103
Mike Stump014b3ea2009-07-21 01:12:51 +00001104 // "while" is a control-flow statement. Thus we stop processing the current
1105 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001106 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001107 if (!FinishBlock(Block))
1108 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001109 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001110 } else
1111 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001112
1113 // Because of short-circuit evaluation, the condition of the loop can span
1114 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1115 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001116 CFGBlock* ExitConditionBlock = createBlock(false);
1117 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001118
Ted Kremenek81e14852007-08-27 19:46:09 +00001119 // Set the terminator for the "exit" condition block.
1120 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001121
1122 // Now add the actual condition to the condition block. Because the condition
1123 // itself may contain control-flow, new blocks may be created. Thus we update
1124 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001125 if (Stmt* C = W->getCond()) {
1126 Block = ExitConditionBlock;
1127 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001128 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001129 if (Block) {
1130 if (!FinishBlock(EntryConditionBlock))
1131 return 0;
1132 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001133 }
Mike Stump31feda52009-07-17 01:31:16 +00001134
1135 // The condition block is the implicit successor for the loop body as well as
1136 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001137 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001138
Mike Stump773582d2009-07-23 23:25:26 +00001139 // See if this is a known constant.
1140 int KnownVal = TryEvaluateBool(W->getCond());
1141
Ted Kremenek9aae5132007-08-23 21:42:29 +00001142 // Process the loop body.
1143 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001144 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001145
1146 // Save the current values for Block, Succ, and continue and break targets
1147 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1148 save_continue(ContinueTargetBlock),
1149 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001150
Mike Stump31feda52009-07-17 01:31:16 +00001151 // Create an empty block to represent the transition block for looping back
1152 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001153 Block = 0;
1154 assert(Succ == EntryConditionBlock);
1155 Succ = createBlock();
1156 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001157 ContinueTargetBlock = Succ;
1158
Ted Kremenek9aae5132007-08-23 21:42:29 +00001159 // All breaks should go to the code following the loop.
1160 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001161
Ted Kremenek9aae5132007-08-23 21:42:29 +00001162 // NULL out Block to force lazy instantiation of blocks for the body.
1163 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek9aae5132007-08-23 21:42:29 +00001165 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001166 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001167
Ted Kremeneke9610502007-08-30 18:39:40 +00001168 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001169 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001170 else if (Block) {
1171 if (!FinishBlock(BodyBlock))
1172 return 0;
1173 }
Mike Stump31feda52009-07-17 01:31:16 +00001174
Mike Stump773582d2009-07-23 23:25:26 +00001175 if (KnownVal == false)
Mike Stump23a443b2009-07-21 00:38:52 +00001176 ExitConditionBlock->addSuccessor(0);
1177 else {
1178 // Add the loop body entry as a successor to the condition.
1179 ExitConditionBlock->addSuccessor(BodyBlock);
1180 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001181 }
Mike Stump31feda52009-07-17 01:31:16 +00001182
Mike Stump773582d2009-07-23 23:25:26 +00001183 if (KnownVal == true)
Mike Stump23a443b2009-07-21 00:38:52 +00001184 ExitConditionBlock->addSuccessor(0);
1185 else {
1186 // Link up the condition block with the code that follows the loop. (the
1187 // false branch).
1188 ExitConditionBlock->addSuccessor(LoopSuccessor);
1189 }
Mike Stump31feda52009-07-17 01:31:16 +00001190
1191 // There can be no more statements in the condition block since we loop back
1192 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001193 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001194
Ted Kremenek9aae5132007-08-23 21:42:29 +00001195 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001196 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001197 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001198}
Ted Kremenek93668002009-07-17 22:18:43 +00001199
1200
1201CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1202 // FIXME: For now we pretend that @catch and the code it contains does not
1203 // exit.
1204 return Block;
1205}
Mike Stump31feda52009-07-17 01:31:16 +00001206
Ted Kremenek93041ba2008-12-09 20:20:09 +00001207CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1208 // FIXME: This isn't complete. We basically treat @throw like a return
1209 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001210
1211 // If we were in the middle of a block we stop processing that block and
1212 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001213 if (Block && !FinishBlock(Block))
1214 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001215
Ted Kremenek93041ba2008-12-09 20:20:09 +00001216 // Create the new block.
1217 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001218
Ted Kremenek93041ba2008-12-09 20:20:09 +00001219 // The Exit block is the only successor.
1220 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001221
1222 // Add the statement to the block. This may create new blocks if S contains
1223 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001224 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001225}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001226
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001227CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1228 // If we were in the middle of a block we stop processing that block and
1229 // reverse its statements.
1230 if (Block && !FinishBlock(Block))
1231 return 0;
1232
1233 // Create the new block.
1234 Block = createBlock(false);
1235
1236 // The Exit block is the only successor.
1237 Block->addSuccessor(&cfg->getExit());
1238
1239 // Add the statement to the block. This may create new blocks if S contains
1240 // control-flow (short-circuit operations).
1241 return VisitStmt(T, true);
1242}
1243
Ted Kremenek93668002009-07-17 22:18:43 +00001244CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001245 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001246
Mike Stump8d50b6a2009-07-21 01:27:50 +00001247 // "do...while" is a control-flow statement. Thus we stop processing the
1248 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001249 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001250 if (!FinishBlock(Block))
1251 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001252 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001253 } else
1254 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001255
1256 // Because of short-circuit evaluation, the condition of the loop can span
1257 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1258 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001259 CFGBlock* ExitConditionBlock = createBlock(false);
1260 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001261
Ted Kremenek81e14852007-08-27 19:46:09 +00001262 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001263 ExitConditionBlock->setTerminator(D);
1264
1265 // Now add the actual condition to the condition block. Because the condition
1266 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001267 if (Stmt* C = D->getCond()) {
1268 Block = ExitConditionBlock;
1269 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001270 if (Block) {
1271 if (!FinishBlock(EntryConditionBlock))
1272 return 0;
1273 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001274 }
Mike Stump31feda52009-07-17 01:31:16 +00001275
Ted Kremeneka1523a32008-02-27 07:20:00 +00001276 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001277 Succ = EntryConditionBlock;
1278
Mike Stump773582d2009-07-23 23:25:26 +00001279 // See if this is a known constant.
1280 int KnownVal = TryEvaluateBool(D->getCond());
1281
Ted Kremenek9aae5132007-08-23 21:42:29 +00001282 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001283 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001284 {
1285 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001286
Ted Kremenek9aae5132007-08-23 21:42:29 +00001287 // Save the current values for Block, Succ, and continue and break targets
1288 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1289 save_continue(ContinueTargetBlock),
1290 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001291
Ted Kremenek9aae5132007-08-23 21:42:29 +00001292 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001293 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001294
Ted Kremenek9aae5132007-08-23 21:42:29 +00001295 // All breaks should go to the code following the loop.
1296 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001297
Ted Kremenek9aae5132007-08-23 21:42:29 +00001298 // NULL out Block to force lazy instantiation of blocks for the body.
1299 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001300
Ted Kremenek9aae5132007-08-23 21:42:29 +00001301 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001302 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001303
Ted Kremeneke9610502007-08-30 18:39:40 +00001304 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001305 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001306 else if (Block) {
1307 if (!FinishBlock(BodyBlock))
1308 return 0;
1309 }
Mike Stump31feda52009-07-17 01:31:16 +00001310
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001311 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001312 // ExitConditionBlock to represent the "loop back" transition. Create an
1313 // empty block to represent the transition block for looping back to the
1314 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001315 // FIXME: Can we do this more efficiently without adding another block?
1316 Block = NULL;
1317 Succ = BodyBlock;
1318 CFGBlock *LoopBackBlock = createBlock();
1319 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001320
Mike Stump773582d2009-07-23 23:25:26 +00001321 if (KnownVal == false)
Mike Stump8d50b6a2009-07-21 01:27:50 +00001322 ExitConditionBlock->addSuccessor(0);
1323 else {
1324 // Add the loop body entry as a successor to the condition.
1325 ExitConditionBlock->addSuccessor(LoopBackBlock);
1326 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001327 }
Mike Stump31feda52009-07-17 01:31:16 +00001328
Mike Stump773582d2009-07-23 23:25:26 +00001329 if (KnownVal == true)
Mike Stump8d50b6a2009-07-21 01:27:50 +00001330 ExitConditionBlock->addSuccessor(0);
1331 else {
1332 // Link up the condition block with the code that follows the loop. (the
1333 // false branch).
1334 ExitConditionBlock->addSuccessor(LoopSuccessor);
1335 }
Mike Stump31feda52009-07-17 01:31:16 +00001336
1337 // There can be no more statements in the body block(s) since we loop back to
1338 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001339 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001340
Ted Kremenek9aae5132007-08-23 21:42:29 +00001341 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001342 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001343 return BodyBlock;
1344}
1345
1346CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1347 // "continue" is a control-flow statement. Thus we stop processing the
1348 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001349 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001350 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 // Now create a new block that ends with the continue statement.
1353 Block = createBlock(false);
1354 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek9aae5132007-08-23 21:42:29 +00001356 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001357 // incomplete AST. This means the CFG cannot be constructed.
1358 if (ContinueTargetBlock)
1359 Block->addSuccessor(ContinueTargetBlock);
1360 else
1361 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001362
Ted Kremenek9aae5132007-08-23 21:42:29 +00001363 return Block;
1364}
Ted Kremenek93668002009-07-17 22:18:43 +00001365
Ted Kremenek0747de62009-07-18 00:47:21 +00001366CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1367 bool alwaysAdd) {
1368
1369 if (alwaysAdd) {
1370 autoCreateBlock();
1371 Block->appendStmt(E);
1372 }
1373
Ted Kremenek93668002009-07-17 22:18:43 +00001374 // VLA types have expressions that must be evaluated.
1375 if (E->isArgumentType()) {
1376 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1377 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1378 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001379 }
Ted Kremenek93668002009-07-17 22:18:43 +00001380
Mike Stump31feda52009-07-17 01:31:16 +00001381 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001382}
Ted Kremenek93668002009-07-17 22:18:43 +00001383
1384/// VisitStmtExpr - Utility method to handle (nested) statement
1385/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001386CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1387 if (alwaysAdd) {
1388 autoCreateBlock();
1389 Block->appendStmt(SE);
1390 }
Ted Kremenek93668002009-07-17 22:18:43 +00001391 return VisitCompoundStmt(SE->getSubStmt());
1392}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001393
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001394CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001395 // "switch" is a control-flow statement. Thus we stop processing the current
1396 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001397 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001398
Ted Kremenek9aae5132007-08-23 21:42:29 +00001399 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001400 if (!FinishBlock(Block))
1401 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001402 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001403 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404
1405 // Save the current "switch" context.
1406 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001407 save_break(BreakTargetBlock),
1408 save_default(DefaultCaseBlock);
1409
Mike Stump31feda52009-07-17 01:31:16 +00001410 // Set the "default" case to be the block after the switch statement. If the
1411 // switch statement contains a "default:", this value will be overwritten with
1412 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001413 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001414
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415 // Create a new block that will contain the switch statement.
1416 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001417
Ted Kremenek9aae5132007-08-23 21:42:29 +00001418 // Now process the switch body. The code after the switch is the implicit
1419 // successor.
1420 Succ = SwitchSuccessor;
1421 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001422
1423 // When visiting the body, the case statements should automatically get linked
1424 // up to the switch. We also don't keep a pointer to the body, since all
1425 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001426 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001427 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001428 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001429 if (Block) {
1430 if (!FinishBlock(BodyBlock))
1431 return 0;
1432 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001433
Mike Stump31feda52009-07-17 01:31:16 +00001434 // If we have no "default:" case, the default transition is to the code
1435 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001436 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001437
Ted Kremenek81e14852007-08-27 19:46:09 +00001438 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001439 SwitchTerminatedBlock->setTerminator(Terminator);
1440 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001441 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001442
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001443 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001444}
1445
Ted Kremenek93668002009-07-17 22:18:43 +00001446CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001447 // CaseStmts are essentially labels, so they are the first statement in a
1448 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001449
Ted Kremenek93668002009-07-17 22:18:43 +00001450 if (CS->getSubStmt())
1451 addStmt(CS->getSubStmt());
1452
Ted Kremenek55e91e82007-08-30 18:48:11 +00001453 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001454 if (!CaseBlock)
1455 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001456
1457 // Cases statements partition blocks, so this is the top of the basic block we
1458 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001459 CaseBlock->setLabel(CS);
1460
Ted Kremenek55957a82009-05-02 00:13:27 +00001461 if (!FinishBlock(CaseBlock))
1462 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001463
1464 // Add this block to the list of successors for the block with the switch
1465 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001466 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001467 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001468
Ted Kremenek9aae5132007-08-23 21:42:29 +00001469 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1470 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001471
Ted Kremenek9aae5132007-08-23 21:42:29 +00001472 // This block is now the implicit successor of other blocks.
1473 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001474
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001475 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001476}
Mike Stump31feda52009-07-17 01:31:16 +00001477
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001478CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001479 if (Terminator->getSubStmt())
1480 addStmt(Terminator->getSubStmt());
1481
Ted Kremenek654c78f2008-02-13 22:05:39 +00001482 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001483
1484 if (!DefaultCaseBlock)
1485 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001486
1487 // Default statements partition blocks, so this is the top of the basic block
1488 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001489 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001490
Ted Kremenek55957a82009-05-02 00:13:27 +00001491 if (!FinishBlock(DefaultCaseBlock))
1492 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001493
Mike Stump31feda52009-07-17 01:31:16 +00001494 // Unlike case statements, we don't add the default block to the successors
1495 // for the switch statement immediately. This is done when we finish
1496 // processing the switch statement. This allows for the default case
1497 // (including a fall-through to the code after the switch statement) to always
1498 // be the last successor of a switch-terminated block.
1499
Ted Kremenek654c78f2008-02-13 22:05:39 +00001500 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1501 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001502
Ted Kremenek654c78f2008-02-13 22:05:39 +00001503 // This block is now the implicit successor of other blocks.
1504 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001505
1506 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001507}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001508
Ted Kremenekeda180e22007-08-28 19:26:49 +00001509CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001510 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001511 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001512
Ted Kremenekeda180e22007-08-28 19:26:49 +00001513 if (!IBlock) {
1514 IBlock = createBlock(false);
1515 cfg->setIndirectGotoBlock(IBlock);
1516 }
Mike Stump31feda52009-07-17 01:31:16 +00001517
Ted Kremenekeda180e22007-08-28 19:26:49 +00001518 // IndirectGoto is a control-flow statement. Thus we stop processing the
1519 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001520 if (Block && !FinishBlock(Block))
1521 return 0;
1522
Ted Kremenekeda180e22007-08-28 19:26:49 +00001523 Block = createBlock(false);
1524 Block->setTerminator(I);
1525 Block->addSuccessor(IBlock);
1526 return addStmt(I->getTarget());
1527}
1528
Ted Kremenek04cca642007-08-23 21:26:19 +00001529} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001530
Mike Stump31feda52009-07-17 01:31:16 +00001531/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1532/// no successors or predecessors. If this is the first block created in the
1533/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001534CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001535 bool first_block = begin() == end();
1536
1537 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001538 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001539
1540 // If this is the first block, set it as the Entry and Exit.
1541 if (first_block) Entry = Exit = &front();
1542
1543 // Return the block.
1544 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001545}
1546
Ted Kremenek889073f2007-08-23 16:51:22 +00001547/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1548/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001549CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001550 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001551 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001552}
1553
1554/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001555void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1556
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001557//===----------------------------------------------------------------------===//
1558// CFG: Queries for BlkExprs.
1559//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001560
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001561namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001562 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001563}
1564
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001565static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1566 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001567 return;
Mike Stump31feda52009-07-17 01:31:16 +00001568
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001569 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001570 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001571
Ted Kremenek95a123c2008-01-26 00:03:27 +00001572 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1573 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001574
Ted Kremenek95a123c2008-01-26 00:03:27 +00001575 FindSubExprAssignments(*I, Set);
1576 }
1577}
1578
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001579static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1580 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001581
1582 // Look for assignments that are used as subexpressions. These are the only
1583 // assignments that we want to *possibly* register as a block-level
1584 // expression. Basically, if an assignment occurs both in a subexpression and
1585 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001586 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001587
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001588 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1589 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001590 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001591
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001592 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001593
1594 // Iterate over the statements again on identify the Expr* and Stmt* at the
1595 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001596
Ted Kremenek95a123c2008-01-26 00:03:27 +00001597 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001598 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001600 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001601 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001602 // expression are really "statements" whose value is never used by
1603 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001604 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001605 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001606 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1607 // Special handling for statement expressions. The last statement in
1608 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001609 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001610 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001611 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001612 (*M)[C->body_back()] = x;
1613 }
1614 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001615
Ted Kremenek95a123c2008-01-26 00:03:27 +00001616 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001617 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001618 }
Mike Stump31feda52009-07-17 01:31:16 +00001619
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001620 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001621
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001622 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001623
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001624 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001625 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001626 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001627 }
1628 }
Mike Stump31feda52009-07-17 01:31:16 +00001629
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001630 return M;
1631}
1632
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001633CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1634 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001635 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001636
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001637 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001638 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001639
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001640 if (I == M->end()) return CFG::BlkExprNumTy();
1641 else return CFG::BlkExprNumTy(I->second);
1642}
1643
1644unsigned CFG::getNumBlkExprs() {
1645 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1646 return M->size();
1647 else {
1648 // We assume callers interested in the number of BlkExprs will want
1649 // the map constructed if it doesn't already exist.
1650 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1651 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1652 }
1653}
1654
Ted Kremenek6065ef62008-04-28 18:00:46 +00001655//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001656// Cleanup: CFG dstor.
1657//===----------------------------------------------------------------------===//
1658
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001659CFG::~CFG() {
1660 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1661}
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001663//===----------------------------------------------------------------------===//
1664// CFG pretty printing
1665//===----------------------------------------------------------------------===//
1666
Ted Kremenek7e776b12007-08-22 18:22:34 +00001667namespace {
1668
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001669class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001671 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1672 StmtMapTy StmtMap;
1673 signed CurrentBlock;
1674 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001675 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001676public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001677
Chris Lattnerc61089a2009-06-30 01:26:17 +00001678 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1679 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001680 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1681 unsigned j = 1;
1682 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1683 BI != BEnd; ++BI, ++j )
1684 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1685 }
1686 }
Mike Stump31feda52009-07-17 01:31:16 +00001687
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001688 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001689
Chris Lattnerc61089a2009-06-30 01:26:17 +00001690 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001691 void setBlockID(signed i) { CurrentBlock = i; }
1692 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001693
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001694 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001695
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001696 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001697
1698 if (I == StmtMap.end())
1699 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001700
1701 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001702 && I->second.second == CurrentStmt)
1703 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001705 OS << "[B" << I->second.first << "." << I->second.second << "]";
1706 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001707 }
1708};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001709} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001710
Chris Lattnerc61089a2009-06-30 01:26:17 +00001711
1712namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001713class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1714 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001715
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001716 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001717 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001718 PrintingPolicy Policy;
1719
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001720public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001721 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001722 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001723 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001724
Ted Kremenek9aae5132007-08-23 21:42:29 +00001725 void VisitIfStmt(IfStmt* I) {
1726 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001727 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001728 }
Mike Stump31feda52009-07-17 01:31:16 +00001729
Ted Kremenek9aae5132007-08-23 21:42:29 +00001730 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001731 void VisitStmt(Stmt* Terminator) {
1732 Terminator->printPretty(OS, Helper, Policy);
1733 }
1734
Ted Kremenek9aae5132007-08-23 21:42:29 +00001735 void VisitForStmt(ForStmt* F) {
1736 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001737 if (F->getInit()) OS << "...";
1738 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001739 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001740 OS << "; ";
1741 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001742 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001743 }
Mike Stump31feda52009-07-17 01:31:16 +00001744
Ted Kremenek9aae5132007-08-23 21:42:29 +00001745 void VisitWhileStmt(WhileStmt* W) {
1746 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001747 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001748 }
Mike Stump31feda52009-07-17 01:31:16 +00001749
Ted Kremenek9aae5132007-08-23 21:42:29 +00001750 void VisitDoStmt(DoStmt* D) {
1751 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001752 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001753 }
Mike Stump31feda52009-07-17 01:31:16 +00001754
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001755 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001756 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001757 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001758 }
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001760 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001761 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001762 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001763 }
Mike Stump31feda52009-07-17 01:31:16 +00001764
Ted Kremenek391f94a2007-08-31 22:29:13 +00001765 void VisitChooseExpr(ChooseExpr* C) {
1766 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001767 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001768 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001769 }
Mike Stump31feda52009-07-17 01:31:16 +00001770
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001771 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1772 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001773 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001774 }
Mike Stump31feda52009-07-17 01:31:16 +00001775
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001776 void VisitBinaryOperator(BinaryOperator* B) {
1777 if (!B->isLogicalOp()) {
1778 VisitExpr(B);
1779 return;
1780 }
Mike Stump31feda52009-07-17 01:31:16 +00001781
Douglas Gregor7de59662009-05-29 20:38:28 +00001782 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001783
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001784 switch (B->getOpcode()) {
1785 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001786 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001787 return;
1788 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001789 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001790 return;
1791 default:
1792 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001793 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001794 }
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenek12687ff2007-08-27 21:54:41 +00001796 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001797 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001798 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001799};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001800} // end anonymous namespace
1801
Mike Stump31feda52009-07-17 01:31:16 +00001802
Chris Lattnerc61089a2009-06-30 01:26:17 +00001803static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1804 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001805 if (Helper) {
1806 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001807 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001808 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001809
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001810 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001811 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001812 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001813 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001814 return;
1815 }
1816 }
Mike Stump31feda52009-07-17 01:31:16 +00001817
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001818 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001819 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001820 if (B->getOpcode() == BinaryOperator::Comma) {
1821 OS << "... , ";
1822 Helper->handledStmt(B->getRHS(),OS);
1823 OS << '\n';
1824 return;
Mike Stump31feda52009-07-17 01:31:16 +00001825 }
1826 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001827 }
Mike Stump31feda52009-07-17 01:31:16 +00001828
Chris Lattnerc61089a2009-06-30 01:26:17 +00001829 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001830
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001831 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001832 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001833}
Mike Stump31feda52009-07-17 01:31:16 +00001834
Chris Lattnerc61089a2009-06-30 01:26:17 +00001835static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1836 const CFGBlock& B,
1837 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001838
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001839 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001840
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001841 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001842 OS << "\n [ B" << B.getBlockID();
1843
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001844 if (&B == &cfg->getEntry())
1845 OS << " (ENTRY) ]\n";
1846 else if (&B == &cfg->getExit())
1847 OS << " (EXIT) ]\n";
1848 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001849 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001850 else
1851 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek71eca012007-08-29 23:20:49 +00001853 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001854 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001855
1856 if (print_edges)
1857 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001858
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001859 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001860 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001861 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001862 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001863 C->getLHS()->printPretty(OS, Helper,
1864 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001865 if (C->getRHS()) {
1866 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001867 C->getRHS()->printPretty(OS, Helper,
1868 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001869 }
Mike Stump31feda52009-07-17 01:31:16 +00001870 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001871 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001872 else
1873 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001874
Ted Kremenek71eca012007-08-29 23:20:49 +00001875 OS << ":\n";
1876 }
Mike Stump31feda52009-07-17 01:31:16 +00001877
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001878 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001879 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001880
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001881 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1882 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001883
Ted Kremenek71eca012007-08-29 23:20:49 +00001884 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001885 if (print_edges)
1886 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001887
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001888 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001890 if (Helper)
1891 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001892
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001893 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001894 }
Mike Stump31feda52009-07-17 01:31:16 +00001895
Ted Kremenek71eca012007-08-29 23:20:49 +00001896 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001897 if (B.getTerminator()) {
1898 if (print_edges)
1899 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001900
Ted Kremenek71eca012007-08-29 23:20:49 +00001901 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001902
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001903 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001904
Chris Lattnerc61089a2009-06-30 01:26:17 +00001905 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1906 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001907 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001908 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001909 }
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenek71eca012007-08-29 23:20:49 +00001911 if (print_edges) {
1912 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001913 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001914 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001915
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001916 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1917 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001919 if (i == 8 || (i-8) == 0)
1920 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001921
Ted Kremenek71eca012007-08-29 23:20:49 +00001922 OS << " B" << (*I)->getBlockID();
1923 }
Mike Stump31feda52009-07-17 01:31:16 +00001924
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001925 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001927 // Print the successors of this block.
1928 OS << " Successors (" << B.succ_size() << "):";
1929 i = 0;
1930
1931 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1932 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001933
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001934 if (i == 8 || (i-8) % 10 == 0)
1935 OS << "\n ";
1936
Mike Stump0d76d072009-07-20 23:24:15 +00001937 if (*I)
1938 OS << " B" << (*I)->getBlockID();
1939 else
1940 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001941 }
Mike Stump31feda52009-07-17 01:31:16 +00001942
Ted Kremenek71eca012007-08-29 23:20:49 +00001943 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001944 }
Mike Stump31feda52009-07-17 01:31:16 +00001945}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001946
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001947
1948/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001949void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001950
1951/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001952void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1953 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001954
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001955 // Print the entry block.
1956 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001957
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001958 // Iterate through the CFGBlocks and print them one by one.
1959 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1960 // Skip the entry block, because we already printed it.
1961 if (&(*I) == &getEntry() || &(*I) == &getExit())
1962 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001963
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001964 print_block(OS, this, *I, &Helper, true);
1965 }
Mike Stump31feda52009-07-17 01:31:16 +00001966
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967 // Print the exit block.
1968 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001969 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001970}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001971
1972/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001973void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1974 print(llvm::errs(), cfg, LO);
1975}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001976
1977/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1978/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001979void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1980 const LangOptions &LO) const {
1981 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001982 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001983}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001984
Ted Kremenek15647632008-01-30 23:02:42 +00001985/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001986void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001987 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001988 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001989 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1990}
1991
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001992Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001994 if (!Terminator)
1995 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001996
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001997 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001999 switch (Terminator->getStmtClass()) {
2000 default:
2001 break;
Mike Stump31feda52009-07-17 01:31:16 +00002002
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002003 case Stmt::ForStmtClass:
2004 E = cast<ForStmt>(Terminator)->getCond();
2005 break;
Mike Stump31feda52009-07-17 01:31:16 +00002006
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002007 case Stmt::WhileStmtClass:
2008 E = cast<WhileStmt>(Terminator)->getCond();
2009 break;
Mike Stump31feda52009-07-17 01:31:16 +00002010
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002011 case Stmt::DoStmtClass:
2012 E = cast<DoStmt>(Terminator)->getCond();
2013 break;
Mike Stump31feda52009-07-17 01:31:16 +00002014
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002015 case Stmt::IfStmtClass:
2016 E = cast<IfStmt>(Terminator)->getCond();
2017 break;
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002019 case Stmt::ChooseExprClass:
2020 E = cast<ChooseExpr>(Terminator)->getCond();
2021 break;
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002023 case Stmt::IndirectGotoStmtClass:
2024 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2025 break;
Mike Stump31feda52009-07-17 01:31:16 +00002026
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002027 case Stmt::SwitchStmtClass:
2028 E = cast<SwitchStmt>(Terminator)->getCond();
2029 break;
Mike Stump31feda52009-07-17 01:31:16 +00002030
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002031 case Stmt::ConditionalOperatorClass:
2032 E = cast<ConditionalOperator>(Terminator)->getCond();
2033 break;
Mike Stump31feda52009-07-17 01:31:16 +00002034
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002035 case Stmt::BinaryOperatorClass: // '&&' and '||'
2036 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002037 break;
Mike Stump31feda52009-07-17 01:31:16 +00002038
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002039 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002040 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002041 }
Mike Stump31feda52009-07-17 01:31:16 +00002042
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002043 return E ? E->IgnoreParens() : NULL;
2044}
2045
Ted Kremenek92137a32008-05-16 16:06:00 +00002046bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002047
Ted Kremenek92137a32008-05-16 16:06:00 +00002048 if (!Terminator)
2049 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002050
Ted Kremenek92137a32008-05-16 16:06:00 +00002051 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002052
Ted Kremenek92137a32008-05-16 16:06:00 +00002053 switch (Terminator->getStmtClass()) {
2054 default:
2055 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002056
2057 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002058 case Stmt::WhileStmtClass:
2059 case Stmt::DoStmtClass:
2060 case Stmt::IfStmtClass:
2061 case Stmt::ChooseExprClass:
2062 case Stmt::ConditionalOperatorClass:
2063 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002064 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002065 }
Mike Stump31feda52009-07-17 01:31:16 +00002066
Ted Kremenek92137a32008-05-16 16:06:00 +00002067 return E ? E->IgnoreParens() : NULL;
2068}
2069
Ted Kremenek15647632008-01-30 23:02:42 +00002070
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002071//===----------------------------------------------------------------------===//
2072// CFG Graphviz Visualization
2073//===----------------------------------------------------------------------===//
2074
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002075
2076#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002077static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002078#endif
2079
Chris Lattnerc61089a2009-06-30 01:26:17 +00002080void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002081#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002082 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002083 GraphHelper = &H;
2084 llvm::ViewGraph(this,"CFG");
2085 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002086#endif
2087}
2088
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002089namespace llvm {
2090template<>
2091struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002092 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2093 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002094
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002095#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002096 std::string OutSStr;
2097 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002098 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002099 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002100
2101 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2102
2103 // Process string output to make it nicer...
2104 for (unsigned i = 0; i != OutStr.length(); ++i)
2105 if (OutStr[i] == '\n') { // Left justify
2106 OutStr[i] = '\\';
2107 OutStr.insert(OutStr.begin()+i+1, 'l');
2108 }
Mike Stump31feda52009-07-17 01:31:16 +00002109
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002110 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002111#else
2112 return "";
2113#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002114 }
2115};
2116} // end namespace llvm