blob: f43631c4d0ab1d4e7f184b5fbc371ae34a3c6a5d [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000019#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4e5f99d2007-08-29 21:56:09 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremeneka59e0062007-12-17 19:35:20 +000022#include "llvm/Support/Streams.h"
Ted Kremenek83ebcef2008-01-08 18:15:10 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek6065ef62008-04-28 18:00:46 +000024#include <llvm/Support/Allocator.h>
Ted Kremenek2d470fc2008-09-13 05:16:45 +000025#include <llvm/Support/Format.h>
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Mike Stump31feda52009-07-17 01:31:16 +000038
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000039/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000040/// The builder is stateful: an instance of the builder should be used to only
41/// construct a single CFG.
42///
43/// Example usage:
44///
45/// CFGBuilder builder;
46/// CFG* cfg = builder.BuildAST(stmt1);
47///
Mike Stump31feda52009-07-17 01:31:16 +000048/// CFG construction is done via a recursive walk of an AST. We actually parse
49/// the AST in reverse order so that the successor of a basic block is
50/// constructed prior to its predecessor. This allows us to nicely capture
51/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000052///
Ted Kremenek93668002009-07-17 22:18:43 +000053class VISIBILITY_HIDDEN CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000054 ASTContext *Context;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000055 CFG* cfg;
56 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000062
Ted Kremenekeda180e22007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000071
Ted Kremenekeda180e22007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000075
76public:
Ted Kremenek889073f2007-08-23 16:51:22 +000077 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000078 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000079 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000080 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000081 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000082 }
Mike Stump31feda52009-07-17 01:31:16 +000083
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000084 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000085
Ted Kremenek9aae5132007-08-23 21:42:29 +000086 // buildCFG - Used by external clients to construct the CFG.
Mike Stump0d76d072009-07-20 23:24:15 +000087 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump31feda52009-07-17 01:31:16 +000088
Ted Kremenek93668002009-07-17 22:18:43 +000089private:
90 // Visitors to walk an AST and construct the CFG.
91 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
92 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek0747de62009-07-18 00:47:21 +000093 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
94 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +000095 CFGBlock *VisitBreakStmt(BreakStmt *B);
96 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
97 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +000098 CFGBlock *VisitChooseExpr(ChooseExpr *C);
99 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
100 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
101 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000102 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek93668002009-07-17 22:18:43 +0000103 CFGBlock *VisitDeclStmt(DeclStmt *DS);
104 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000105 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
106 CFGBlock *VisitDoStmt(DoStmt *D);
107 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000108 CFGBlock *VisitGotoStmt(GotoStmt* G);
109 CFGBlock *VisitIfStmt(IfStmt *I);
110 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
111 CFGBlock *VisitLabelStmt(LabelStmt *L);
112 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
113 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
114 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
115 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
116 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
117 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek0747de62009-07-18 00:47:21 +0000118 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
119 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000120 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
121 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000122
Ted Kremenek93668002009-07-17 22:18:43 +0000123 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
124 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
125 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000126
Ted Kremenek6065ef62008-04-28 18:00:46 +0000127 // NYS == Not Yet Supported
128 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000129 badCFG = true;
130 return Block;
131 }
Mike Stump31feda52009-07-17 01:31:16 +0000132
Ted Kremenek93668002009-07-17 22:18:43 +0000133 void autoCreateBlock() { if (!Block) Block = createBlock(); }
134 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000135 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000136 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
137
Ted Kremenekb64d1832008-03-13 03:04:22 +0000138 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000139};
Mike Stump31feda52009-07-17 01:31:16 +0000140
Douglas Gregor4619e432008-12-05 23:32:09 +0000141// FIXME: Add support for dependent-sized array types in C++?
142// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000143static VariableArrayType* FindVA(Type* t) {
144 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
145 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
146 if (vat->getSizeExpr())
147 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000148
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000149 t = vt->getElementType().getTypePtr();
150 }
Mike Stump31feda52009-07-17 01:31:16 +0000151
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000152 return 0;
153}
Mike Stump31feda52009-07-17 01:31:16 +0000154
155/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
156/// arbitrary statement. Examples include a single expression or a function
157/// body (compound statement). The ownership of the returned CFG is
158/// transferred to the caller. If CFG construction fails, this method returns
159/// NULL.
Mike Stump0d76d072009-07-20 23:24:15 +0000160CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
161 Context = C;
Ted Kremenek93668002009-07-17 22:18:43 +0000162 assert(cfg);
163 if (!Statement)
164 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000165
Ted Kremenekb64d1832008-03-13 03:04:22 +0000166 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000167
168 // Create an empty block that will serve as the exit block for the CFG. Since
169 // this is the first block added to the CFG, it will be implicitly registered
170 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000171 Succ = createBlock();
172 assert (Succ == &cfg->getExit());
173 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000174
Ted Kremenek9aae5132007-08-23 21:42:29 +0000175 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000176 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000177 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000178
Ted Kremenek40d87632008-02-27 17:33:02 +0000179 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000180 // Finalize the last constructed block. This usually involves reversing the
181 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000182 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000183
184 // Backpatch the gotos whose label -> block mappings we didn't know when we
185 // encountered them.
186 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000187 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000188
Ted Kremenek9aae5132007-08-23 21:42:29 +0000189 CFGBlock* B = *I;
190 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
191 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
192
193 // If there is no target for the goto, then we are looking at an
194 // incomplete AST. Handle this by not registering a successor.
195 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000196
197 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000198 }
Mike Stump31feda52009-07-17 01:31:16 +0000199
Ted Kremenekeda180e22007-08-28 19:26:49 +0000200 // Add successors to the Indirect Goto Dispatch block (if we have one).
201 if (CFGBlock* B = cfg->getIndirectGotoBlock())
202 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
203 E = AddressTakenLabels.end(); I != E; ++I ) {
204
205 // Lookup the target block.
206 LabelMapTy::iterator LI = LabelMap.find(*I);
207
208 // If there is no target block that contains label, then we are looking
209 // at an incomplete AST. Handle this by not registering a successor.
210 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000211
212 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000213 }
Mike Stump31feda52009-07-17 01:31:16 +0000214
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000215 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000216 }
Mike Stump31feda52009-07-17 01:31:16 +0000217
218 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000219 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000220
Ted Kremenekb64d1832008-03-13 03:04:22 +0000221 if (badCFG) {
222 delete cfg;
223 cfg = NULL;
224 return NULL;
225 }
Mike Stump31feda52009-07-17 01:31:16 +0000226
227 // NULL out cfg so that repeated calls to the builder will fail and that the
228 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000229 CFG* t = cfg;
230 cfg = NULL;
231 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000232}
Mike Stump31feda52009-07-17 01:31:16 +0000233
Ted Kremenek9aae5132007-08-23 21:42:29 +0000234/// createBlock - Used to lazily create blocks that are connected
235/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000236CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000237 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000238 if (add_successor && Succ)
239 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000240 return B;
241}
Mike Stump31feda52009-07-17 01:31:16 +0000242
243/// FinishBlock - When the last statement has been added to the block, we must
244/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000245bool CFGBuilder::FinishBlock(CFGBlock* B) {
246 if (badCFG)
247 return false;
248
Ted Kremenek93668002009-07-17 22:18:43 +0000249 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000250 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000251 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000252}
253
Ted Kremenek93668002009-07-17 22:18:43 +0000254/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000255/// blocks for ternary operators, &&, and ||. We also process "," and
256/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000257CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
258tryAgain:
259 switch (S->getStmtClass()) {
260 default:
261 return VisitStmt(S, alwaysAdd);
262
263 case Stmt::AddrLabelExprClass:
264 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
265
266 case Stmt::BinaryOperatorClass:
267 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
268
269 case Stmt::BlockExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000270 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000271
272 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000273 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000274
275 case Stmt::BreakStmtClass:
276 return VisitBreakStmt(cast<BreakStmt>(S));
277
278 case Stmt::CallExprClass:
279 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
280
281 case Stmt::CaseStmtClass:
282 return VisitCaseStmt(cast<CaseStmt>(S));
283
284 case Stmt::ChooseExprClass:
285 return VisitChooseExpr(cast<ChooseExpr>(S));
286
287 case Stmt::CompoundStmtClass:
288 return VisitCompoundStmt(cast<CompoundStmt>(S));
289
290 case Stmt::ConditionalOperatorClass:
291 return VisitConditionalOperator(cast<ConditionalOperator>(S));
292
293 case Stmt::ContinueStmtClass:
294 return VisitContinueStmt(cast<ContinueStmt>(S));
295
296 case Stmt::DeclStmtClass:
297 return VisitDeclStmt(cast<DeclStmt>(S));
298
299 case Stmt::DefaultStmtClass:
300 return VisitDefaultStmt(cast<DefaultStmt>(S));
301
302 case Stmt::DoStmtClass:
303 return VisitDoStmt(cast<DoStmt>(S));
304
305 case Stmt::ForStmtClass:
306 return VisitForStmt(cast<ForStmt>(S));
307
308 case Stmt::GotoStmtClass:
309 return VisitGotoStmt(cast<GotoStmt>(S));
310
311 case Stmt::IfStmtClass:
312 return VisitIfStmt(cast<IfStmt>(S));
313
314 case Stmt::IndirectGotoStmtClass:
315 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
316
317 case Stmt::LabelStmtClass:
318 return VisitLabelStmt(cast<LabelStmt>(S));
319
320 case Stmt::ObjCAtCatchStmtClass:
321 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
322
Mike Stump8dd1b6b2009-07-22 22:56:04 +0000323 case Stmt::CXXThrowExprClass:
324 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
325
Ted Kremenek93668002009-07-17 22:18:43 +0000326 case Stmt::ObjCAtSynchronizedStmtClass:
327 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
328
329 case Stmt::ObjCAtThrowStmtClass:
330 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
331
332 case Stmt::ObjCAtTryStmtClass:
333 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
334
335 case Stmt::ObjCForCollectionStmtClass:
336 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
337
338 case Stmt::ParenExprClass:
339 S = cast<ParenExpr>(S)->getSubExpr();
340 goto tryAgain;
341
342 case Stmt::NullStmtClass:
343 return Block;
344
345 case Stmt::ReturnStmtClass:
346 return VisitReturnStmt(cast<ReturnStmt>(S));
347
348 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000349 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000350
351 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000352 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000353
354 case Stmt::SwitchStmtClass:
355 return VisitSwitchStmt(cast<SwitchStmt>(S));
356
357 case Stmt::WhileStmtClass:
358 return VisitWhileStmt(cast<WhileStmt>(S));
359 }
360}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000361
Ted Kremenek93668002009-07-17 22:18:43 +0000362CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
363 if (alwaysAdd) {
364 autoCreateBlock();
365 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000366 }
Ted Kremenek93668002009-07-17 22:18:43 +0000367
368 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000369}
Mike Stump31feda52009-07-17 01:31:16 +0000370
Ted Kremenek93668002009-07-17 22:18:43 +0000371/// VisitChildren - Visit the children of a Stmt.
372CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
373 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000374 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000375 E = Terminator->child_end(); I != E; ++I) {
376 if (*I) B = Visit(*I);
377 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000378 return B;
379}
Ted Kremenek93668002009-07-17 22:18:43 +0000380
381CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
382 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000383
Ted Kremenek93668002009-07-17 22:18:43 +0000384 if (alwaysAdd) {
385 autoCreateBlock();
386 Block->appendStmt(A);
387 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000388
Ted Kremenek9aae5132007-08-23 21:42:29 +0000389 return Block;
390}
Ted Kremenek93668002009-07-17 22:18:43 +0000391
392CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
393 if (B->isLogicalOp()) { // && or ||
Ted Kremenek9aae5132007-08-23 21:42:29 +0000394
Mike Stump0d76d072009-07-20 23:24:15 +0000395 // See if this is a known constant.
396 bool KnownTrue = false;
397 bool KnownFalse = false;
398 Expr::EvalResult Result;
399 if (B->getLHS()->Evaluate(Result, *Context)
400 && Result.Val.isInt()) {
401 if ((B->getOpcode() == BinaryOperator::LAnd)
402 == Result.Val.getInt().getBoolValue())
403 KnownTrue = true;
404 else
405 KnownFalse = true;
406 }
407
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
425 // Now link the LHSBlock with RHSBlock.
426 if (B->getOpcode() == BinaryOperator::LOr) {
Mike Stump0d76d072009-07-20 23:24:15 +0000427 if (KnownTrue)
428 LHSBlock->addSuccessor(0);
429 else
430 LHSBlock->addSuccessor(ConfluenceBlock);
431 if (KnownFalse)
432 LHSBlock->addSuccessor(0);
433 else
434 LHSBlock->addSuccessor(RHSBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000435 } else {
436 assert (B->getOpcode() == BinaryOperator::LAnd);
Mike Stump0d76d072009-07-20 23:24:15 +0000437 if (KnownFalse)
438 LHSBlock->addSuccessor(0);
439 else
440 LHSBlock->addSuccessor(RHSBlock);
441 if (KnownTrue)
442 LHSBlock->addSuccessor(0);
443 else
444 LHSBlock->addSuccessor(ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000445 }
446
447 // Generate the blocks for evaluating the LHS.
448 Block = LHSBlock;
449 return addStmt(B->getLHS());
450 }
451 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000452 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000453 Block->appendStmt(B);
454 addStmt(B->getRHS());
455 return addStmt(B->getLHS());
456 }
457
458 return VisitStmt(B, alwaysAdd);
459}
460
Ted Kremenek0747de62009-07-18 00:47:21 +0000461CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000462 // FIXME
463 return NYS();
464}
465
Ted Kremenek0747de62009-07-18 00:47:21 +0000466CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
467 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000468 // FIXME
469 return NYS();
470}
471
472CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
473 // "break" is a control-flow statement. Thus we stop processing the current
474 // block.
475 if (Block && !FinishBlock(Block))
476 return 0;
477
478 // Now create a new block that ends with the break statement.
479 Block = createBlock(false);
480 Block->setTerminator(B);
481
482 // If there is no target for the break, then we are looking at an incomplete
483 // AST. This means that the CFG cannot be constructed.
484 if (BreakTargetBlock)
485 Block->addSuccessor(BreakTargetBlock);
486 else
487 badCFG = true;
488
489
Ted Kremenek9aae5132007-08-23 21:42:29 +0000490 return Block;
491}
Ted Kremenek93668002009-07-17 22:18:43 +0000492
493CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
494 // If this is a call to a no-return function, this stops the block here.
495 if (FunctionDecl *FD = C->getDirectCallee()) {
496
497 if (!FD->hasAttr<NoReturnAttr>())
498 return VisitStmt(C, alwaysAdd);
499
500 if (Block && !FinishBlock(Block))
501 return 0;
502
503 // Create new block with no successor for the remaining pieces.
504 Block = createBlock(false);
505 Block->appendStmt(C);
506
507 // Wire this to the exit block directly.
508 Block->addSuccessor(&cfg->getExit());
509
510 return VisitChildren(C);
511 }
512
513 return VisitStmt(C, alwaysAdd);
514}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000515
Ted Kremenek21822592009-07-17 18:20:32 +0000516CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
Mike Stump3557ea82009-07-21 01:46:17 +0000517 // See if this is a known constant.
518 bool KnownTrue = false;
519 bool KnownFalse = false;
520 Expr::EvalResult Result;
521 if (C->getCond()->Evaluate(Result, *Context)
522 && Result.Val.isInt()) {
523 if (Result.Val.getInt().getBoolValue())
524 KnownTrue = true;
525 else
526 KnownFalse = true;
527 }
528
Ted Kremenek21822592009-07-17 18:20:32 +0000529 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
530 ConfluenceBlock->appendStmt(C);
531 if (!FinishBlock(ConfluenceBlock))
532 return 0;
533
534 Succ = ConfluenceBlock;
535 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000536 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000537 if (!FinishBlock(LHSBlock))
538 return 0;
539
540 Succ = ConfluenceBlock;
541 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000542 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000543 if (!FinishBlock(RHSBlock))
544 return 0;
545
546 Block = createBlock(false);
Mike Stump3557ea82009-07-21 01:46:17 +0000547 if (KnownFalse)
548 Block->addSuccessor(0);
549 else
550 Block->addSuccessor(LHSBlock);
551 if (KnownTrue)
552 Block->addSuccessor(0);
553 else
554 Block->addSuccessor(RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000555 Block->setTerminator(C);
556 return addStmt(C->getCond());
557}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000558
Ted Kremenek93668002009-07-17 22:18:43 +0000559
560CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
561 CFGBlock* LastBlock = Block;
562
563 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
564 I != E; ++I ) {
565 LastBlock = addStmt(*I);
566 }
567 return LastBlock;
568}
569
Ted Kremenek51d40b02009-07-17 18:15:54 +0000570CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
Mike Stump0d76d072009-07-20 23:24:15 +0000571 // See if this is a known constant.
572 bool KnownTrue = false;
573 bool KnownFalse = false;
574 Expr::EvalResult Result;
575 if (C->getCond()->Evaluate(Result, *Context)
576 && Result.Val.isInt()) {
577 if (Result.Val.getInt().getBoolValue())
578 KnownTrue = true;
579 else
580 KnownFalse = true;
581 }
582
Ted Kremenek51d40b02009-07-17 18:15:54 +0000583 // Create the confluence block that will "merge" the results of the ternary
584 // expression.
585 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
586 ConfluenceBlock->appendStmt(C);
587 if (!FinishBlock(ConfluenceBlock))
588 return 0;
589
590 // Create a block for the LHS expression if there is an LHS expression. A
591 // GCC extension allows LHS to be NULL, causing the condition to be the
592 // value that is returned instead.
593 // e.g: x ?: y is shorthand for: x ? x : y;
594 Succ = ConfluenceBlock;
595 Block = NULL;
596 CFGBlock* LHSBlock = NULL;
597 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000598 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000599 if (!FinishBlock(LHSBlock))
600 return 0;
601 Block = NULL;
602 }
603
604 // Create the block for the RHS expression.
605 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000606 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000607 if (!FinishBlock(RHSBlock))
608 return 0;
609
610 // Create the block that will contain the condition.
611 Block = createBlock(false);
612
Mike Stump0d76d072009-07-20 23:24:15 +0000613 if (LHSBlock) {
614 if (KnownFalse)
615 Block->addSuccessor(0);
616 else
617 Block->addSuccessor(LHSBlock);
618 } else {
619 if (KnownFalse) {
620 // If we know the condition is false, add NULL as the successor for
621 // the block containing the condition. In this case, the confluence
622 // block will have just one predecessor.
623 Block->addSuccessor(0);
624 assert (ConfluenceBlock->pred_size() == 1);
625 } else {
626 // If we have no LHS expression, add the ConfluenceBlock as a direct
627 // successor for the block containing the condition. Moreover, we need to
628 // reverse the order of the predecessors in the ConfluenceBlock because
629 // the RHSBlock will have been added to the succcessors already, and we
630 // want the first predecessor to the the block containing the expression
631 // for the case when the ternary expression evaluates to true.
632 Block->addSuccessor(ConfluenceBlock);
633 assert (ConfluenceBlock->pred_size() == 2);
634 std::reverse(ConfluenceBlock->pred_begin(),
635 ConfluenceBlock->pred_end());
636 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000637 }
638
Mike Stump0d76d072009-07-20 23:24:15 +0000639 if (KnownTrue)
640 Block->addSuccessor(0);
641 else
642 Block->addSuccessor(RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000643
644 Block->setTerminator(C);
645 return addStmt(C->getCond());
646}
647
Ted Kremenek93668002009-07-17 22:18:43 +0000648CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
649 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000650
Ted Kremenek93668002009-07-17 22:18:43 +0000651 if (DS->isSingleDecl()) {
652 Block->appendStmt(DS);
653 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000654 }
Ted Kremenek93668002009-07-17 22:18:43 +0000655
656 CFGBlock *B = 0;
657
658 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
659 typedef llvm::SmallVector<Decl*,10> BufTy;
660 BufTy Buf(DS->decl_begin(), DS->decl_end());
661
662 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
663 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
664 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
665 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
666
667 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
668 // automatically freed with the CFG.
669 DeclGroupRef DG(*I);
670 Decl *D = *I;
671 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
672 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
673
674 // Append the fake DeclStmt to block.
675 Block->appendStmt(DSNew);
676 B = VisitDeclSubExpr(D);
677 }
678
679 return B;
680}
681
682/// VisitDeclSubExpr - Utility method to add block-level expressions for
683/// initializers in Decls.
684CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
685 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000686
Ted Kremenek93668002009-07-17 22:18:43 +0000687 VarDecl *VD = dyn_cast<VarDecl>(D);
688
689 if (!VD)
690 return Block;
691
692 Expr *Init = VD->getInit();
693
694 if (Init) {
695 // Optimization: Don't create separate block-level statements for literals.
696 switch (Init->getStmtClass()) {
697 case Stmt::IntegerLiteralClass:
698 case Stmt::CharacterLiteralClass:
699 case Stmt::StringLiteralClass:
700 break;
701 default:
702 Block = addStmt(Init);
703 }
704 }
705
706 // If the type of VD is a VLA, then we must process its size expressions.
707 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
708 VA = FindVA(VA->getElementType().getTypePtr()))
709 Block = addStmt(VA->getSizeExpr());
710
711 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000712}
713
714CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump23a443b2009-07-21 00:38:52 +0000715 // See if this is a known constant.
Mike Stump0d76d072009-07-20 23:24:15 +0000716 bool KnownTrue = false;
717 bool KnownFalse = false;
718 Expr::EvalResult Result;
719 if (I->getCond()->Evaluate(Result, *Context)
720 && Result.Val.isInt()) {
721 if (Result.Val.getInt().getBoolValue())
722 KnownTrue = true;
723 else
724 KnownFalse = true;
725 }
726
Mike Stump31feda52009-07-17 01:31:16 +0000727 // We may see an if statement in the middle of a basic block, or it may be the
728 // first statement we are processing. In either case, we create a new basic
729 // block. First, we create the blocks for the then...else statements, and
730 // then we create the block containing the if statement. If we were in the
731 // middle of a block, we stop processing that block and reverse its
732 // statements. That block is then the implicit successor for the "then" and
733 // "else" clauses.
734
735 // The block we were proccessing is now finished. Make it the successor
736 // block.
737 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000738 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000739 if (!FinishBlock(Block))
740 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000741 }
Mike Stump31feda52009-07-17 01:31:16 +0000742
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000743 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000744 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000745
Ted Kremenek9aae5132007-08-23 21:42:29 +0000746 if (Stmt* Else = I->getElse()) {
747 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000748
Ted Kremenek9aae5132007-08-23 21:42:29 +0000749 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000750 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000751 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000752 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000753
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000754 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
755 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000756 else if (Block) {
757 if (!FinishBlock(ElseBlock))
758 return 0;
759 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000760 }
Mike Stump31feda52009-07-17 01:31:16 +0000761
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000762 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000763 CFGBlock* ThenBlock;
764 {
765 Stmt* Then = I->getThen();
766 assert (Then);
767 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000768 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000769 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000770
Ted Kremenek1b379512009-04-01 03:52:47 +0000771 if (!ThenBlock) {
772 // We can reach here if the "then" body has all NullStmts.
773 // Create an empty block so we can distinguish between true and false
774 // branches in path-sensitive analyses.
775 ThenBlock = createBlock(false);
776 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000777 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000778 if (!FinishBlock(ThenBlock))
779 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000780 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000781 }
782
Mike Stump31feda52009-07-17 01:31:16 +0000783 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000784 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000785
Ted Kremenek9aae5132007-08-23 21:42:29 +0000786 // Set the terminator of the new block to the If statement.
787 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000788
Ted Kremenek9aae5132007-08-23 21:42:29 +0000789 // Now add the successors.
Mike Stump0d76d072009-07-20 23:24:15 +0000790 if (KnownFalse)
791 Block->addSuccessor(0);
792 else
793 Block->addSuccessor(ThenBlock);
794 if (KnownTrue)
795 Block->addSuccessor(0);
796 else
797 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000798
799 // Add the condition as the last statement in the new block. This may create
800 // new blocks as the condition may contain control-flow. Any newly created
801 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000802 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000803}
Mike Stump31feda52009-07-17 01:31:16 +0000804
805
Ted Kremenek9aae5132007-08-23 21:42:29 +0000806CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000807 // If we were in the middle of a block we stop processing that block and
808 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000809 //
Mike Stump31feda52009-07-17 01:31:16 +0000810 // NOTE: If a "return" appears in the middle of a block, this means that the
811 // code afterwards is DEAD (unreachable). We still keep a basic block
812 // for that code; a simple "mark-and-sweep" from the entry block will be
813 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000814 if (Block) FinishBlock(Block);
815
816 // Create the new block.
817 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000818
Ted Kremenek9aae5132007-08-23 21:42:29 +0000819 // The Exit block is the only successor.
820 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000821
822 // Add the return statement to the block. This may create new blocks if R
823 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000824 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825}
826
827CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
828 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000829 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000830 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000831
Ted Kremenek93668002009-07-17 22:18:43 +0000832 if (!LabelBlock) // This can happen when the body is empty, i.e.
833 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000834
Ted Kremenek93668002009-07-17 22:18:43 +0000835 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000836 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000837
838 // Labels partition blocks, so this is the end of the basic block we were
839 // processing (L is the block's label). Because this is label (and we have
840 // already processed the substatement) there is no extra control-flow to worry
841 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000842 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000843 if (!FinishBlock(LabelBlock))
844 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000845
846 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000847 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000848
Ted Kremenek9aae5132007-08-23 21:42:29 +0000849 // This block is now the implicit successor of other blocks.
850 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000851
Ted Kremenek9aae5132007-08-23 21:42:29 +0000852 return LabelBlock;
853}
854
855CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000856 // Goto is a control-flow statement. Thus we stop processing the current
857 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000858 if (Block)
859 FinishBlock(Block);
860
Ted Kremenek9aae5132007-08-23 21:42:29 +0000861 Block = createBlock(false);
862 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000863
864 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000865 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000866
Ted Kremenek9aae5132007-08-23 21:42:29 +0000867 if (I == LabelMap.end())
868 // We will need to backpatch this block later.
869 BackpatchBlocks.push_back(Block);
870 else
871 Block->addSuccessor(I->second);
872
Mike Stump31feda52009-07-17 01:31:16 +0000873 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000874}
875
876CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump014b3ea2009-07-21 01:12:51 +0000877 // See if this is a known constant.
878 bool KnownTrue = false;
879 bool KnownFalse = false;
880 Expr::EvalResult Result;
881 if (F->getCond() && F->getCond()->Evaluate(Result, *Context)
882 && Result.Val.isInt()) {
883 if (Result.Val.getInt().getBoolValue())
884 KnownTrue = true;
885 else
886 KnownFalse = true;
887 }
888 if (F->getCond() == 0)
889 KnownTrue = true;
890
Ted Kremenek9aae5132007-08-23 21:42:29 +0000891 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000892
Mike Stump014b3ea2009-07-21 01:12:51 +0000893 // "for" is a control-flow statement. Thus we stop processing the current
894 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000895 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000896 if (!FinishBlock(Block))
897 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000898 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000899 } else
900 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000901
902 // Because of short-circuit evaluation, the condition of the loop can span
903 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
904 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000905 CFGBlock* ExitConditionBlock = createBlock(false);
906 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000907
Ted Kremenek81e14852007-08-27 19:46:09 +0000908 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000909 ExitConditionBlock->setTerminator(F);
910
911 // Now add the actual condition to the condition block. Because the condition
912 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000913 if (Stmt* C = F->getCond()) {
914 Block = ExitConditionBlock;
915 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000916 if (Block) {
917 if (!FinishBlock(EntryConditionBlock))
918 return 0;
919 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000920 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000921
Mike Stump31feda52009-07-17 01:31:16 +0000922 // The condition block is the implicit successor for the loop body as well as
923 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000924 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000925
Ted Kremenek9aae5132007-08-23 21:42:29 +0000926 // Now create the loop body.
927 {
928 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000929
Ted Kremenek9aae5132007-08-23 21:42:29 +0000930 // Save the current values for Block, Succ, and continue and break targets
931 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
932 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000933 save_break(BreakTargetBlock);
934
Ted Kremeneke9610502007-08-30 18:39:40 +0000935 // Create a new block to contain the (bottom) of the loop body.
936 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000938 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000939 // Generate increment code in its own basic block. This is the target of
940 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000941 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000942 } else {
943 // No increment code. Create a special, empty, block that is used as the
944 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000945 assert(Succ == EntryConditionBlock);
946 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000947 }
Mike Stump31feda52009-07-17 01:31:16 +0000948
Ted Kremenek902393b2009-04-28 00:51:56 +0000949 // Finish up the increment (or empty) block if it hasn't been already.
950 if (Block) {
951 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000952 if (!FinishBlock(Block))
953 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000954 Block = 0;
955 }
Mike Stump31feda52009-07-17 01:31:16 +0000956
Ted Kremenek902393b2009-04-28 00:51:56 +0000957 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek902393b2009-04-28 00:51:56 +0000959 // The starting block for the loop increment is the block that should
960 // represent the 'loop target' for looping back to the start of the loop.
961 ContinueTargetBlock->setLoopTarget(F);
962
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000963 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000964 BreakTargetBlock = LoopSuccessor;
965
966 // Now populate the body block, and in the process create new blocks as we
967 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000968 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000969
970 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000971 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000972 else if (Block) {
973 if (!FinishBlock(BodyBlock))
974 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000975 }
976
Mike Stump014b3ea2009-07-21 01:12:51 +0000977 if (KnownFalse)
978 ExitConditionBlock->addSuccessor(0);
979 else {
980 // This new body block is a successor to our "exit" condition block.
981 ExitConditionBlock->addSuccessor(BodyBlock);
982 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000983 }
Mike Stump31feda52009-07-17 01:31:16 +0000984
Mike Stump014b3ea2009-07-21 01:12:51 +0000985 if (KnownTrue)
986 ExitConditionBlock->addSuccessor(0);
987 else {
988 // Link up the condition block with the code that follows the loop. (the
989 // false branch).
990 ExitConditionBlock->addSuccessor(LoopSuccessor);
991 }
Mike Stump31feda52009-07-17 01:31:16 +0000992
Ted Kremenek9aae5132007-08-23 21:42:29 +0000993 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000994 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000995 if (Stmt* I = F->getInit()) {
996 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000997 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000998 } else {
999 // There is no loop initialization. We are thus basically a while loop.
1000 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001001 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001002 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001003 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001004 }
1005}
1006
Ted Kremenek9d56e642008-11-11 17:10:00 +00001007CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1008 // Objective-C fast enumeration 'for' statements:
1009 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1010 //
1011 // for ( Type newVariable in collection_expression ) { statements }
1012 //
1013 // becomes:
1014 //
1015 // prologue:
1016 // 1. collection_expression
1017 // T. jump to loop_entry
1018 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001019 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001020 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1021 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1022 // TB:
1023 // statements
1024 // T. jump to loop_entry
1025 // FB:
1026 // what comes after
1027 //
1028 // and
1029 //
1030 // Type existingItem;
1031 // for ( existingItem in expression ) { statements }
1032 //
1033 // becomes:
1034 //
Mike Stump31feda52009-07-17 01:31:16 +00001035 // the same with newVariable replaced with existingItem; the binding works
1036 // the same except that for one ObjCForCollectionStmt::getElement() returns
1037 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001038 //
Mike Stump31feda52009-07-17 01:31:16 +00001039
Ted Kremenek9d56e642008-11-11 17:10:00 +00001040 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001041
Ted Kremenek9d56e642008-11-11 17:10:00 +00001042 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001043 if (!FinishBlock(Block))
1044 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001045 LoopSuccessor = Block;
1046 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001047 } else
1048 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001049
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001050 // Build the condition blocks.
1051 CFGBlock* ExitConditionBlock = createBlock(false);
1052 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001053
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001054 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001055 ExitConditionBlock->setTerminator(S);
1056
1057 // The last statement in the block should be the ObjCForCollectionStmt, which
1058 // performs the actual binding to 'element' and determines if there are any
1059 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001060 ExitConditionBlock->appendStmt(S);
1061 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001062
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001063 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001064 // generate new blocks as necesary. We DON'T add the statement by default to
1065 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +00001066 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +00001067 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001068 if (!FinishBlock(EntryConditionBlock))
1069 return 0;
1070 Block = 0;
1071 }
Mike Stump31feda52009-07-17 01:31:16 +00001072
1073 // The condition block is the implicit successor for the loop body as well as
1074 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001075 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001076
Ted Kremenek9d56e642008-11-11 17:10:00 +00001077 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001078 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001079 // Save the current values for Succ, continue and break targets.
1080 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001081 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1082
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001083 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001084 ContinueTargetBlock = EntryConditionBlock;
1085
Ted Kremenek93668002009-07-17 22:18:43 +00001086 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001087
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001088 if (!BodyBlock)
1089 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001090 else if (Block) {
1091 if (!FinishBlock(BodyBlock))
1092 return 0;
1093 }
Mike Stump31feda52009-07-17 01:31:16 +00001094
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001095 // This new body block is a successor to our "exit" condition block.
1096 ExitConditionBlock->addSuccessor(BodyBlock);
1097 }
Mike Stump31feda52009-07-17 01:31:16 +00001098
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001099 // Link up the condition block with the code that follows the loop.
1100 // (the false branch).
1101 ExitConditionBlock->addSuccessor(LoopSuccessor);
1102
Ted Kremenek9d56e642008-11-11 17:10:00 +00001103 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001104 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001105 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001106}
1107
Ted Kremenek49805452009-05-02 01:49:13 +00001108CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1109 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001110
Ted Kremenek49805452009-05-02 01:49:13 +00001111 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001112 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001113
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001114 // The sync body starts its own basic block. This makes it a little easier
1115 // for diagnostic clients.
1116 if (SyncBlock) {
1117 if (!FinishBlock(SyncBlock))
1118 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001119
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001120 Block = 0;
1121 }
Mike Stump31feda52009-07-17 01:31:16 +00001122
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001123 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001124
Ted Kremenek49805452009-05-02 01:49:13 +00001125 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001126 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001127}
Mike Stump31feda52009-07-17 01:31:16 +00001128
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001129CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001130 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001131 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001132}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001133
Ted Kremenek9aae5132007-08-23 21:42:29 +00001134CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump23a443b2009-07-21 00:38:52 +00001135 // See if this is a known constant.
1136 bool KnownTrue = false;
1137 bool KnownFalse = false;
1138 Expr::EvalResult Result;
1139 if (W->getCond()->Evaluate(Result, *Context)
1140 && Result.Val.isInt()) {
1141 if (Result.Val.getInt().getBoolValue())
1142 KnownTrue = true;
1143 else
1144 KnownFalse = true;
1145 }
1146
Ted Kremenek9aae5132007-08-23 21:42:29 +00001147 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001148
Mike Stump014b3ea2009-07-21 01:12:51 +00001149 // "while" is a control-flow statement. Thus we stop processing the current
1150 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001151 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001152 if (!FinishBlock(Block))
1153 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001154 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001155 } else
1156 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001157
1158 // Because of short-circuit evaluation, the condition of the loop can span
1159 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1160 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001161 CFGBlock* ExitConditionBlock = createBlock(false);
1162 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001163
Ted Kremenek81e14852007-08-27 19:46:09 +00001164 // Set the terminator for the "exit" condition block.
1165 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001166
1167 // Now add the actual condition to the condition block. Because the condition
1168 // itself may contain control-flow, new blocks may be created. Thus we update
1169 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001170 if (Stmt* C = W->getCond()) {
1171 Block = ExitConditionBlock;
1172 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001173 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001174 if (Block) {
1175 if (!FinishBlock(EntryConditionBlock))
1176 return 0;
1177 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001178 }
Mike Stump31feda52009-07-17 01:31:16 +00001179
1180 // The condition block is the implicit successor for the loop body as well as
1181 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001182 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001183
Ted Kremenek9aae5132007-08-23 21:42:29 +00001184 // Process the loop body.
1185 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001186 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001187
1188 // Save the current values for Block, Succ, and continue and break targets
1189 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1190 save_continue(ContinueTargetBlock),
1191 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001192
Mike Stump31feda52009-07-17 01:31:16 +00001193 // Create an empty block to represent the transition block for looping back
1194 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001195 Block = 0;
1196 assert(Succ == EntryConditionBlock);
1197 Succ = createBlock();
1198 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001199 ContinueTargetBlock = Succ;
1200
Ted Kremenek9aae5132007-08-23 21:42:29 +00001201 // All breaks should go to the code following the loop.
1202 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001203
Ted Kremenek9aae5132007-08-23 21:42:29 +00001204 // NULL out Block to force lazy instantiation of blocks for the body.
1205 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001206
Ted Kremenek9aae5132007-08-23 21:42:29 +00001207 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001208 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001209
Ted Kremeneke9610502007-08-30 18:39:40 +00001210 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001211 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001212 else if (Block) {
1213 if (!FinishBlock(BodyBlock))
1214 return 0;
1215 }
Mike Stump31feda52009-07-17 01:31:16 +00001216
Mike Stump23a443b2009-07-21 00:38:52 +00001217 if (KnownFalse)
1218 ExitConditionBlock->addSuccessor(0);
1219 else {
1220 // Add the loop body entry as a successor to the condition.
1221 ExitConditionBlock->addSuccessor(BodyBlock);
1222 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001223 }
Mike Stump31feda52009-07-17 01:31:16 +00001224
Mike Stump23a443b2009-07-21 00:38:52 +00001225 if (KnownTrue)
1226 ExitConditionBlock->addSuccessor(0);
1227 else {
1228 // Link up the condition block with the code that follows the loop. (the
1229 // false branch).
1230 ExitConditionBlock->addSuccessor(LoopSuccessor);
1231 }
Mike Stump31feda52009-07-17 01:31:16 +00001232
1233 // There can be no more statements in the condition block since we loop back
1234 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001235 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001236
Ted Kremenek9aae5132007-08-23 21:42:29 +00001237 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001238 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001239 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001240}
Ted Kremenek93668002009-07-17 22:18:43 +00001241
1242
1243CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1244 // FIXME: For now we pretend that @catch and the code it contains does not
1245 // exit.
1246 return Block;
1247}
Mike Stump31feda52009-07-17 01:31:16 +00001248
Ted Kremenek93041ba2008-12-09 20:20:09 +00001249CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1250 // FIXME: This isn't complete. We basically treat @throw like a return
1251 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001252
1253 // If we were in the middle of a block we stop processing that block and
1254 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001255 if (Block && !FinishBlock(Block))
1256 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001257
Ted Kremenek93041ba2008-12-09 20:20:09 +00001258 // Create the new block.
1259 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001260
Ted Kremenek93041ba2008-12-09 20:20:09 +00001261 // The Exit block is the only successor.
1262 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001263
1264 // Add the statement to the block. This may create new blocks if S contains
1265 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001266 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001267}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001268
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001269CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1270 // If we were in the middle of a block we stop processing that block and
1271 // reverse its statements.
1272 if (Block && !FinishBlock(Block))
1273 return 0;
1274
1275 // Create the new block.
1276 Block = createBlock(false);
1277
1278 // The Exit block is the only successor.
1279 Block->addSuccessor(&cfg->getExit());
1280
1281 // Add the statement to the block. This may create new blocks if S contains
1282 // control-flow (short-circuit operations).
1283 return VisitStmt(T, true);
1284}
1285
Ted Kremenek93668002009-07-17 22:18:43 +00001286CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Mike Stump8d50b6a2009-07-21 01:27:50 +00001287 // See if this is a known constant.
1288 bool KnownTrue = false;
1289 bool KnownFalse = false;
1290 Expr::EvalResult Result;
1291 if (D->getCond()->Evaluate(Result, *Context)
1292 && Result.Val.isInt()) {
1293 if (Result.Val.getInt().getBoolValue())
1294 KnownTrue = true;
1295 else
1296 KnownFalse = true;
1297 }
Mike Stump31feda52009-07-17 01:31:16 +00001298
Ted Kremenek9aae5132007-08-23 21:42:29 +00001299 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001300
Mike Stump8d50b6a2009-07-21 01:27:50 +00001301 // "do...while" is a control-flow statement. Thus we stop processing the
1302 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001303 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001304 if (!FinishBlock(Block))
1305 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001306 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001307 } else
1308 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001309
1310 // Because of short-circuit evaluation, the condition of the loop can span
1311 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1312 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001313 CFGBlock* ExitConditionBlock = createBlock(false);
1314 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001315
Ted Kremenek81e14852007-08-27 19:46:09 +00001316 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001317 ExitConditionBlock->setTerminator(D);
1318
1319 // Now add the actual condition to the condition block. Because the condition
1320 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001321 if (Stmt* C = D->getCond()) {
1322 Block = ExitConditionBlock;
1323 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001324 if (Block) {
1325 if (!FinishBlock(EntryConditionBlock))
1326 return 0;
1327 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001328 }
Mike Stump31feda52009-07-17 01:31:16 +00001329
Ted Kremeneka1523a32008-02-27 07:20:00 +00001330 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001331 Succ = EntryConditionBlock;
1332
Ted Kremenek9aae5132007-08-23 21:42:29 +00001333 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001334 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001335 {
1336 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001337
Ted Kremenek9aae5132007-08-23 21:42:29 +00001338 // Save the current values for Block, Succ, and continue and break targets
1339 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1340 save_continue(ContinueTargetBlock),
1341 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001342
Ted Kremenek9aae5132007-08-23 21:42:29 +00001343 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001344 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001345
Ted Kremenek9aae5132007-08-23 21:42:29 +00001346 // All breaks should go to the code following the loop.
1347 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001348
Ted Kremenek9aae5132007-08-23 21:42:29 +00001349 // NULL out Block to force lazy instantiation of blocks for the body.
1350 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001353 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001354
Ted Kremeneke9610502007-08-30 18:39:40 +00001355 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001356 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001357 else if (Block) {
1358 if (!FinishBlock(BodyBlock))
1359 return 0;
1360 }
Mike Stump31feda52009-07-17 01:31:16 +00001361
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001362 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001363 // ExitConditionBlock to represent the "loop back" transition. Create an
1364 // empty block to represent the transition block for looping back to the
1365 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001366 // FIXME: Can we do this more efficiently without adding another block?
1367 Block = NULL;
1368 Succ = BodyBlock;
1369 CFGBlock *LoopBackBlock = createBlock();
1370 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001371
Mike Stump8d50b6a2009-07-21 01:27:50 +00001372 if (KnownFalse)
1373 ExitConditionBlock->addSuccessor(0);
1374 else {
1375 // Add the loop body entry as a successor to the condition.
1376 ExitConditionBlock->addSuccessor(LoopBackBlock);
1377 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001378 }
Mike Stump31feda52009-07-17 01:31:16 +00001379
Mike Stump8d50b6a2009-07-21 01:27:50 +00001380 if (KnownTrue)
1381 ExitConditionBlock->addSuccessor(0);
1382 else {
1383 // Link up the condition block with the code that follows the loop. (the
1384 // false branch).
1385 ExitConditionBlock->addSuccessor(LoopSuccessor);
1386 }
Mike Stump31feda52009-07-17 01:31:16 +00001387
1388 // There can be no more statements in the body block(s) since we loop back to
1389 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001390 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001391
Ted Kremenek9aae5132007-08-23 21:42:29 +00001392 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001393 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001394 return BodyBlock;
1395}
1396
1397CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1398 // "continue" is a control-flow statement. Thus we stop processing the
1399 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001400 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001401 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001402
Ted Kremenek9aae5132007-08-23 21:42:29 +00001403 // Now create a new block that ends with the continue statement.
1404 Block = createBlock(false);
1405 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001406
Ted Kremenek9aae5132007-08-23 21:42:29 +00001407 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001408 // incomplete AST. This means the CFG cannot be constructed.
1409 if (ContinueTargetBlock)
1410 Block->addSuccessor(ContinueTargetBlock);
1411 else
1412 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001413
Ted Kremenek9aae5132007-08-23 21:42:29 +00001414 return Block;
1415}
Ted Kremenek93668002009-07-17 22:18:43 +00001416
Ted Kremenek0747de62009-07-18 00:47:21 +00001417CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1418 bool alwaysAdd) {
1419
1420 if (alwaysAdd) {
1421 autoCreateBlock();
1422 Block->appendStmt(E);
1423 }
1424
Ted Kremenek93668002009-07-17 22:18:43 +00001425 // VLA types have expressions that must be evaluated.
1426 if (E->isArgumentType()) {
1427 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1428 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1429 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001430 }
Ted Kremenek93668002009-07-17 22:18:43 +00001431
Mike Stump31feda52009-07-17 01:31:16 +00001432 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001433}
Ted Kremenek93668002009-07-17 22:18:43 +00001434
1435/// VisitStmtExpr - Utility method to handle (nested) statement
1436/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001437CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1438 if (alwaysAdd) {
1439 autoCreateBlock();
1440 Block->appendStmt(SE);
1441 }
Ted Kremenek93668002009-07-17 22:18:43 +00001442 return VisitCompoundStmt(SE->getSubStmt());
1443}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001444
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001445CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001446 // "switch" is a control-flow statement. Thus we stop processing the current
1447 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001448 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001449
Ted Kremenek9aae5132007-08-23 21:42:29 +00001450 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001451 if (!FinishBlock(Block))
1452 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001454 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001455
1456 // Save the current "switch" context.
1457 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001458 save_break(BreakTargetBlock),
1459 save_default(DefaultCaseBlock);
1460
Mike Stump31feda52009-07-17 01:31:16 +00001461 // Set the "default" case to be the block after the switch statement. If the
1462 // switch statement contains a "default:", this value will be overwritten with
1463 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001464 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001465
Ted Kremenek9aae5132007-08-23 21:42:29 +00001466 // Create a new block that will contain the switch statement.
1467 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001468
Ted Kremenek9aae5132007-08-23 21:42:29 +00001469 // Now process the switch body. The code after the switch is the implicit
1470 // successor.
1471 Succ = SwitchSuccessor;
1472 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001473
1474 // When visiting the body, the case statements should automatically get linked
1475 // up to the switch. We also don't keep a pointer to the body, since all
1476 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001477 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001478 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001479 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001480 if (Block) {
1481 if (!FinishBlock(BodyBlock))
1482 return 0;
1483 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001484
Mike Stump31feda52009-07-17 01:31:16 +00001485 // If we have no "default:" case, the default transition is to the code
1486 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001487 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001488
Ted Kremenek81e14852007-08-27 19:46:09 +00001489 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001490 SwitchTerminatedBlock->setTerminator(Terminator);
1491 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001492 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001493
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001494 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001495}
1496
Ted Kremenek93668002009-07-17 22:18:43 +00001497CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001498 // CaseStmts are essentially labels, so they are the first statement in a
1499 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001500
Ted Kremenek93668002009-07-17 22:18:43 +00001501 if (CS->getSubStmt())
1502 addStmt(CS->getSubStmt());
1503
Ted Kremenek55e91e82007-08-30 18:48:11 +00001504 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001505 if (!CaseBlock)
1506 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001507
1508 // Cases statements partition blocks, so this is the top of the basic block we
1509 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001510 CaseBlock->setLabel(CS);
1511
Ted Kremenek55957a82009-05-02 00:13:27 +00001512 if (!FinishBlock(CaseBlock))
1513 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001514
1515 // Add this block to the list of successors for the block with the switch
1516 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001517 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001518 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001519
Ted Kremenek9aae5132007-08-23 21:42:29 +00001520 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1521 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001522
Ted Kremenek9aae5132007-08-23 21:42:29 +00001523 // This block is now the implicit successor of other blocks.
1524 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001525
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001526 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001527}
Mike Stump31feda52009-07-17 01:31:16 +00001528
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001529CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001530 if (Terminator->getSubStmt())
1531 addStmt(Terminator->getSubStmt());
1532
Ted Kremenek654c78f2008-02-13 22:05:39 +00001533 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001534
1535 if (!DefaultCaseBlock)
1536 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001537
1538 // Default statements partition blocks, so this is the top of the basic block
1539 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001540 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001541
Ted Kremenek55957a82009-05-02 00:13:27 +00001542 if (!FinishBlock(DefaultCaseBlock))
1543 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001544
Mike Stump31feda52009-07-17 01:31:16 +00001545 // Unlike case statements, we don't add the default block to the successors
1546 // for the switch statement immediately. This is done when we finish
1547 // processing the switch statement. This allows for the default case
1548 // (including a fall-through to the code after the switch statement) to always
1549 // be the last successor of a switch-terminated block.
1550
Ted Kremenek654c78f2008-02-13 22:05:39 +00001551 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1552 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001553
Ted Kremenek654c78f2008-02-13 22:05:39 +00001554 // This block is now the implicit successor of other blocks.
1555 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001556
1557 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001558}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001559
Ted Kremenekeda180e22007-08-28 19:26:49 +00001560CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001561 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001562 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001563
Ted Kremenekeda180e22007-08-28 19:26:49 +00001564 if (!IBlock) {
1565 IBlock = createBlock(false);
1566 cfg->setIndirectGotoBlock(IBlock);
1567 }
Mike Stump31feda52009-07-17 01:31:16 +00001568
Ted Kremenekeda180e22007-08-28 19:26:49 +00001569 // IndirectGoto is a control-flow statement. Thus we stop processing the
1570 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001571 if (Block && !FinishBlock(Block))
1572 return 0;
1573
Ted Kremenekeda180e22007-08-28 19:26:49 +00001574 Block = createBlock(false);
1575 Block->setTerminator(I);
1576 Block->addSuccessor(IBlock);
1577 return addStmt(I->getTarget());
1578}
1579
Ted Kremenek04cca642007-08-23 21:26:19 +00001580} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001581
Mike Stump31feda52009-07-17 01:31:16 +00001582/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1583/// no successors or predecessors. If this is the first block created in the
1584/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001585CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001586 bool first_block = begin() == end();
1587
1588 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001589 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001590
1591 // If this is the first block, set it as the Entry and Exit.
1592 if (first_block) Entry = Exit = &front();
1593
1594 // Return the block.
1595 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001596}
1597
Ted Kremenek889073f2007-08-23 16:51:22 +00001598/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1599/// CFG is returned to the caller.
Mike Stump0d76d072009-07-20 23:24:15 +00001600CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001601 CFGBuilder Builder;
Mike Stump0d76d072009-07-20 23:24:15 +00001602 return Builder.buildCFG(Statement, C);
Ted Kremenek889073f2007-08-23 16:51:22 +00001603}
1604
1605/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001606void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1607
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001608//===----------------------------------------------------------------------===//
1609// CFG: Queries for BlkExprs.
1610//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001611
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001612namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001613 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001614}
1615
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001616static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1617 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001618 return;
Mike Stump31feda52009-07-17 01:31:16 +00001619
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001620 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001621 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001622
Ted Kremenek95a123c2008-01-26 00:03:27 +00001623 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1624 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001625
Ted Kremenek95a123c2008-01-26 00:03:27 +00001626 FindSubExprAssignments(*I, Set);
1627 }
1628}
1629
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001630static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1631 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001632
1633 // Look for assignments that are used as subexpressions. These are the only
1634 // assignments that we want to *possibly* register as a block-level
1635 // expression. Basically, if an assignment occurs both in a subexpression and
1636 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001637 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001638
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001639 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1640 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001641 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001642
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001643 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001644
1645 // Iterate over the statements again on identify the Expr* and Stmt* at the
1646 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001647
Ted Kremenek95a123c2008-01-26 00:03:27 +00001648 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001649 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001650
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001651 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001652 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001653 // expression are really "statements" whose value is never used by
1654 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001655 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001656 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001657 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1658 // Special handling for statement expressions. The last statement in
1659 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001660 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001661 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001662 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001663 (*M)[C->body_back()] = x;
1664 }
1665 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001666
Ted Kremenek95a123c2008-01-26 00:03:27 +00001667 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001668 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001669 }
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001671 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001672
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001673 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001674
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001675 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001676 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001677 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001678 }
1679 }
Mike Stump31feda52009-07-17 01:31:16 +00001680
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001681 return M;
1682}
1683
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001684CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1685 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001686 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001687
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001688 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001689 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001690
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001691 if (I == M->end()) return CFG::BlkExprNumTy();
1692 else return CFG::BlkExprNumTy(I->second);
1693}
1694
1695unsigned CFG::getNumBlkExprs() {
1696 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1697 return M->size();
1698 else {
1699 // We assume callers interested in the number of BlkExprs will want
1700 // the map constructed if it doesn't already exist.
1701 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1702 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1703 }
1704}
1705
Ted Kremenek6065ef62008-04-28 18:00:46 +00001706//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001707// Cleanup: CFG dstor.
1708//===----------------------------------------------------------------------===//
1709
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001710CFG::~CFG() {
1711 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1712}
Mike Stump31feda52009-07-17 01:31:16 +00001713
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001714//===----------------------------------------------------------------------===//
1715// CFG pretty printing
1716//===----------------------------------------------------------------------===//
1717
Ted Kremenek7e776b12007-08-22 18:22:34 +00001718namespace {
1719
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001720class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001721
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001722 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1723 StmtMapTy StmtMap;
1724 signed CurrentBlock;
1725 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001726 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001727public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001728
Chris Lattnerc61089a2009-06-30 01:26:17 +00001729 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1730 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001731 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1732 unsigned j = 1;
1733 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1734 BI != BEnd; ++BI, ++j )
1735 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1736 }
1737 }
Mike Stump31feda52009-07-17 01:31:16 +00001738
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001739 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001740
Chris Lattnerc61089a2009-06-30 01:26:17 +00001741 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001742 void setBlockID(signed i) { CurrentBlock = i; }
1743 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001744
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001745 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001746
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001747 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001748
1749 if (I == StmtMap.end())
1750 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001751
1752 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001753 && I->second.second == CurrentStmt)
1754 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001755
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001756 OS << "[B" << I->second.first << "." << I->second.second << "]";
1757 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001758 }
1759};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001760} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001761
Chris Lattnerc61089a2009-06-30 01:26:17 +00001762
1763namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001764class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1765 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001767 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001768 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001769 PrintingPolicy Policy;
1770
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001771public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001772 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001773 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001774 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001775
Ted Kremenek9aae5132007-08-23 21:42:29 +00001776 void VisitIfStmt(IfStmt* I) {
1777 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001778 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001779 }
Mike Stump31feda52009-07-17 01:31:16 +00001780
Ted Kremenek9aae5132007-08-23 21:42:29 +00001781 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001782 void VisitStmt(Stmt* Terminator) {
1783 Terminator->printPretty(OS, Helper, Policy);
1784 }
1785
Ted Kremenek9aae5132007-08-23 21:42:29 +00001786 void VisitForStmt(ForStmt* F) {
1787 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001788 if (F->getInit()) OS << "...";
1789 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001790 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001791 OS << "; ";
1792 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001793 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001794 }
Mike Stump31feda52009-07-17 01:31:16 +00001795
Ted Kremenek9aae5132007-08-23 21:42:29 +00001796 void VisitWhileStmt(WhileStmt* W) {
1797 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001798 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001799 }
Mike Stump31feda52009-07-17 01:31:16 +00001800
Ted Kremenek9aae5132007-08-23 21:42:29 +00001801 void VisitDoStmt(DoStmt* D) {
1802 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001803 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001804 }
Mike Stump31feda52009-07-17 01:31:16 +00001805
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001806 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001807 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001808 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001809 }
Mike Stump31feda52009-07-17 01:31:16 +00001810
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001811 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001812 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001813 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001814 }
Mike Stump31feda52009-07-17 01:31:16 +00001815
Ted Kremenek391f94a2007-08-31 22:29:13 +00001816 void VisitChooseExpr(ChooseExpr* C) {
1817 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001818 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001819 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001820 }
Mike Stump31feda52009-07-17 01:31:16 +00001821
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001822 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1823 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001824 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001825 }
Mike Stump31feda52009-07-17 01:31:16 +00001826
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001827 void VisitBinaryOperator(BinaryOperator* B) {
1828 if (!B->isLogicalOp()) {
1829 VisitExpr(B);
1830 return;
1831 }
Mike Stump31feda52009-07-17 01:31:16 +00001832
Douglas Gregor7de59662009-05-29 20:38:28 +00001833 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001834
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001835 switch (B->getOpcode()) {
1836 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001837 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001838 return;
1839 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001840 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001841 return;
1842 default:
1843 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001844 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001845 }
Mike Stump31feda52009-07-17 01:31:16 +00001846
Ted Kremenek12687ff2007-08-27 21:54:41 +00001847 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001848 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001849 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001850};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001851} // end anonymous namespace
1852
Mike Stump31feda52009-07-17 01:31:16 +00001853
Chris Lattnerc61089a2009-06-30 01:26:17 +00001854static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1855 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001856 if (Helper) {
1857 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001858 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001859 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001860
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001861 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001862 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001863 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001864 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001865 return;
1866 }
1867 }
Mike Stump31feda52009-07-17 01:31:16 +00001868
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001869 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001870 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001871 if (B->getOpcode() == BinaryOperator::Comma) {
1872 OS << "... , ";
1873 Helper->handledStmt(B->getRHS(),OS);
1874 OS << '\n';
1875 return;
Mike Stump31feda52009-07-17 01:31:16 +00001876 }
1877 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001878 }
Mike Stump31feda52009-07-17 01:31:16 +00001879
Chris Lattnerc61089a2009-06-30 01:26:17 +00001880 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001882 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001883 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001884}
Mike Stump31feda52009-07-17 01:31:16 +00001885
Chris Lattnerc61089a2009-06-30 01:26:17 +00001886static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1887 const CFGBlock& B,
1888 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001890 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001891
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001892 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001893 OS << "\n [ B" << B.getBlockID();
1894
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001895 if (&B == &cfg->getEntry())
1896 OS << " (ENTRY) ]\n";
1897 else if (&B == &cfg->getExit())
1898 OS << " (EXIT) ]\n";
1899 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001900 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001901 else
1902 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001903
Ted Kremenek71eca012007-08-29 23:20:49 +00001904 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001905 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001906
1907 if (print_edges)
1908 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001909
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001910 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001911 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001912 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001913 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001914 C->getLHS()->printPretty(OS, Helper,
1915 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001916 if (C->getRHS()) {
1917 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001918 C->getRHS()->printPretty(OS, Helper,
1919 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001920 }
Mike Stump31feda52009-07-17 01:31:16 +00001921 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001922 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001923 else
1924 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001925
Ted Kremenek71eca012007-08-29 23:20:49 +00001926 OS << ":\n";
1927 }
Mike Stump31feda52009-07-17 01:31:16 +00001928
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001929 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001930 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001931
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001932 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1933 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001934
Ted Kremenek71eca012007-08-29 23:20:49 +00001935 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001936 if (print_edges)
1937 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001938
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001939 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001940
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001941 if (Helper)
1942 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001943
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001944 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001945 }
Mike Stump31feda52009-07-17 01:31:16 +00001946
Ted Kremenek71eca012007-08-29 23:20:49 +00001947 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001948 if (B.getTerminator()) {
1949 if (print_edges)
1950 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001951
Ted Kremenek71eca012007-08-29 23:20:49 +00001952 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001953
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001954 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001955
Chris Lattnerc61089a2009-06-30 01:26:17 +00001956 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1957 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001958 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001959 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001960 }
Mike Stump31feda52009-07-17 01:31:16 +00001961
Ted Kremenek71eca012007-08-29 23:20:49 +00001962 if (print_edges) {
1963 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001964 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001965 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001966
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1968 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001969
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001970 if (i == 8 || (i-8) == 0)
1971 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001972
Ted Kremenek71eca012007-08-29 23:20:49 +00001973 OS << " B" << (*I)->getBlockID();
1974 }
Mike Stump31feda52009-07-17 01:31:16 +00001975
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001976 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001977
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001978 // Print the successors of this block.
1979 OS << " Successors (" << B.succ_size() << "):";
1980 i = 0;
1981
1982 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1983 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001984
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001985 if (i == 8 || (i-8) % 10 == 0)
1986 OS << "\n ";
1987
Mike Stump0d76d072009-07-20 23:24:15 +00001988 if (*I)
1989 OS << " B" << (*I)->getBlockID();
1990 else
1991 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001992 }
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenek71eca012007-08-29 23:20:49 +00001994 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001995 }
Mike Stump31feda52009-07-17 01:31:16 +00001996}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001997
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001998
1999/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002000void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002001
2002/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002003void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2004 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002006 // Print the entry block.
2007 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002008
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002009 // Iterate through the CFGBlocks and print them one by one.
2010 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2011 // Skip the entry block, because we already printed it.
2012 if (&(*I) == &getEntry() || &(*I) == &getExit())
2013 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002014
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002015 print_block(OS, this, *I, &Helper, true);
2016 }
Mike Stump31feda52009-07-17 01:31:16 +00002017
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002018 // Print the exit block.
2019 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002020 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002021}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002022
2023/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002024void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2025 print(llvm::errs(), cfg, LO);
2026}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002027
2028/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2029/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002030void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2031 const LangOptions &LO) const {
2032 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002033 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002034}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002035
Ted Kremenek15647632008-01-30 23:02:42 +00002036/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002037void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002038 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002039 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002040 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2041}
2042
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002043Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002044
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002045 if (!Terminator)
2046 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002047
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002048 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002049
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002050 switch (Terminator->getStmtClass()) {
2051 default:
2052 break;
Mike Stump31feda52009-07-17 01:31:16 +00002053
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002054 case Stmt::ForStmtClass:
2055 E = cast<ForStmt>(Terminator)->getCond();
2056 break;
Mike Stump31feda52009-07-17 01:31:16 +00002057
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002058 case Stmt::WhileStmtClass:
2059 E = cast<WhileStmt>(Terminator)->getCond();
2060 break;
Mike Stump31feda52009-07-17 01:31:16 +00002061
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002062 case Stmt::DoStmtClass:
2063 E = cast<DoStmt>(Terminator)->getCond();
2064 break;
Mike Stump31feda52009-07-17 01:31:16 +00002065
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002066 case Stmt::IfStmtClass:
2067 E = cast<IfStmt>(Terminator)->getCond();
2068 break;
Mike Stump31feda52009-07-17 01:31:16 +00002069
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002070 case Stmt::ChooseExprClass:
2071 E = cast<ChooseExpr>(Terminator)->getCond();
2072 break;
Mike Stump31feda52009-07-17 01:31:16 +00002073
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002074 case Stmt::IndirectGotoStmtClass:
2075 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2076 break;
Mike Stump31feda52009-07-17 01:31:16 +00002077
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002078 case Stmt::SwitchStmtClass:
2079 E = cast<SwitchStmt>(Terminator)->getCond();
2080 break;
Mike Stump31feda52009-07-17 01:31:16 +00002081
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002082 case Stmt::ConditionalOperatorClass:
2083 E = cast<ConditionalOperator>(Terminator)->getCond();
2084 break;
Mike Stump31feda52009-07-17 01:31:16 +00002085
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002086 case Stmt::BinaryOperatorClass: // '&&' and '||'
2087 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002088 break;
Mike Stump31feda52009-07-17 01:31:16 +00002089
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002090 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002091 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002092 }
Mike Stump31feda52009-07-17 01:31:16 +00002093
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002094 return E ? E->IgnoreParens() : NULL;
2095}
2096
Ted Kremenek92137a32008-05-16 16:06:00 +00002097bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002098
Ted Kremenek92137a32008-05-16 16:06:00 +00002099 if (!Terminator)
2100 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002101
Ted Kremenek92137a32008-05-16 16:06:00 +00002102 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002103
Ted Kremenek92137a32008-05-16 16:06:00 +00002104 switch (Terminator->getStmtClass()) {
2105 default:
2106 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002107
2108 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002109 case Stmt::WhileStmtClass:
2110 case Stmt::DoStmtClass:
2111 case Stmt::IfStmtClass:
2112 case Stmt::ChooseExprClass:
2113 case Stmt::ConditionalOperatorClass:
2114 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002115 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002116 }
Mike Stump31feda52009-07-17 01:31:16 +00002117
Ted Kremenek92137a32008-05-16 16:06:00 +00002118 return E ? E->IgnoreParens() : NULL;
2119}
2120
Ted Kremenek15647632008-01-30 23:02:42 +00002121
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002122//===----------------------------------------------------------------------===//
2123// CFG Graphviz Visualization
2124//===----------------------------------------------------------------------===//
2125
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002126
2127#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002128static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002129#endif
2130
Chris Lattnerc61089a2009-06-30 01:26:17 +00002131void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002132#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002133 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002134 GraphHelper = &H;
2135 llvm::ViewGraph(this,"CFG");
2136 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002137#endif
2138}
2139
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002140namespace llvm {
2141template<>
2142struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00002143 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2144 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002145
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002146#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002147 std::string OutSStr;
2148 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002149 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002150 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002151
2152 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2153
2154 // Process string output to make it nicer...
2155 for (unsigned i = 0; i != OutStr.length(); ++i)
2156 if (OutStr[i] == '\n') { // Left justify
2157 OutStr[i] = '\\';
2158 OutStr.insert(OutStr.begin()+i+1, 'l');
2159 }
Mike Stump31feda52009-07-17 01:31:16 +00002160
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002161 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002162#else
2163 return "";
2164#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002165 }
2166};
2167} // end namespace llvm