blob: f85627e7d96b24baddb75f9c37b2e0d957c552b2 [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 Kremenek6796fbd2009-07-16 18:13:04 +000015#include "clang/Analysis/CFG.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4e5f99d2007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
Ted Kremeneka59e0062007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek83ebcef2008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek6065ef62008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremenek2d470fc2008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000025
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Ted Kremenek04cca642007-08-23 21:26:19 +000030// SaveAndRestore - A utility class that uses RIIA to save and restore
31// the value of a variable.
32template<typename T>
Ted Kremenek83ebcef2008-01-08 18:15:10 +000033struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenek04cca642007-08-23 21:26:19 +000034 SaveAndRestore(T& x) : X(x), old_value(x) {}
35 ~SaveAndRestore() { X = old_value; }
Ted Kremenekbbad8ce2007-08-30 18:13:31 +000036 T get() { return old_value; }
37
Ted Kremenek04cca642007-08-23 21:26:19 +000038 T& X;
39 T old_value;
40};
Mike Stump31feda52009-07-17 01:31:16 +000041
Douglas Gregor6e6ad602009-01-20 01:17:11 +000042static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000043 if (VarDecl* VD = dyn_cast<VarDecl>(D))
44 if (Expr* Ex = VD->getInit())
45 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000046
47 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000048}
Mike Stump31feda52009-07-17 01:31:16 +000049
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000050/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000051/// The builder is stateful: an instance of the builder should be used to only
52/// construct a single CFG.
53///
54/// Example usage:
55///
56/// CFGBuilder builder;
57/// CFG* cfg = builder.BuildAST(stmt1);
58///
Mike Stump31feda52009-07-17 01:31:16 +000059/// CFG construction is done via a recursive walk of an AST. We actually parse
60/// the AST in reverse order so that the successor of a basic block is
61/// constructed prior to its predecessor. This allows us to nicely capture
62/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000063///
Ted Kremenek93668002009-07-17 22:18:43 +000064class VISIBILITY_HIDDEN CFGBuilder {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000065 CFG* cfg;
66 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000067 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000068 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000069 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000070 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000071 CFGBlock* DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +000072
Ted Kremenekeda180e22007-08-28 19:26:49 +000073 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000074 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
75 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000076
77 // A list of blocks that end with a "goto" that must be backpatched to their
78 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000079 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000080 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000081
Ted Kremenekeda180e22007-08-28 19:26:49 +000082 // A list of labels whose address has been taken (for indirect gotos).
83 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
84 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000085
86public:
Ted Kremenek889073f2007-08-23 16:51:22 +000087 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000088 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek654c78f2008-02-13 22:05:39 +000089 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000090 // Create an empty CFG.
Mike Stump31feda52009-07-17 01:31:16 +000091 cfg = new CFG();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000092 }
Mike Stump31feda52009-07-17 01:31:16 +000093
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000094 ~CFGBuilder() { delete cfg; }
Mike Stump31feda52009-07-17 01:31:16 +000095
Ted Kremenek9aae5132007-08-23 21:42:29 +000096 // buildCFG - Used by external clients to construct the CFG.
97 CFG* buildCFG(Stmt* Statement);
Mike Stump31feda52009-07-17 01:31:16 +000098
Ted Kremenek93668002009-07-17 22:18:43 +000099private:
100 // Visitors to walk an AST and construct the CFG.
101 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
102 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
103 CFGBlock *VisitBlockExpr(BlockExpr* E);
104 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E);
105 CFGBlock *VisitBreakStmt(BreakStmt *B);
106 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
107 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek21822592009-07-17 18:20:32 +0000108 CFGBlock *VisitChooseExpr(ChooseExpr *C);
109 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
110 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
111 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000112 CFGBlock *VisitDeclStmt(DeclStmt *DS);
113 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000114 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
115 CFGBlock *VisitDoStmt(DoStmt *D);
116 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitGotoStmt(GotoStmt* G);
118 CFGBlock *VisitIfStmt(IfStmt *I);
119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
120 CFGBlock *VisitLabelStmt(LabelStmt *L);
121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
126 CFGBlock *VisitReturnStmt(ReturnStmt* R);
127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
128 CFGBlock *VisitStmtExpr(StmtExpr *S);
129 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
130 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000131
Ted Kremenek93668002009-07-17 22:18:43 +0000132 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
133 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
134 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000135
Ted Kremenek6065ef62008-04-28 18:00:46 +0000136 // NYS == Not Yet Supported
137 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000138 badCFG = true;
139 return Block;
140 }
Mike Stump31feda52009-07-17 01:31:16 +0000141
Ted Kremenek93668002009-07-17 22:18:43 +0000142 void autoCreateBlock() { if (!Block) Block = createBlock(); }
143 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000144 bool FinishBlock(CFGBlock* B);
Ted Kremenek93668002009-07-17 22:18:43 +0000145 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
146
Ted Kremenekb64d1832008-03-13 03:04:22 +0000147 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000148};
Mike Stump31feda52009-07-17 01:31:16 +0000149
Douglas Gregor4619e432008-12-05 23:32:09 +0000150// FIXME: Add support for dependent-sized array types in C++?
151// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000152static VariableArrayType* FindVA(Type* t) {
153 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
154 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
155 if (vat->getSizeExpr())
156 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000157
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000158 t = vt->getElementType().getTypePtr();
159 }
Mike Stump31feda52009-07-17 01:31:16 +0000160
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000161 return 0;
162}
Mike Stump31feda52009-07-17 01:31:16 +0000163
164/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
165/// arbitrary statement. Examples include a single expression or a function
166/// body (compound statement). The ownership of the returned CFG is
167/// transferred to the caller. If CFG construction fails, this method returns
168/// NULL.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000169CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek93668002009-07-17 22:18:43 +0000170 assert(cfg);
171 if (!Statement)
172 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000173
Ted Kremenekb64d1832008-03-13 03:04:22 +0000174 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000175
176 // Create an empty block that will serve as the exit block for the CFG. Since
177 // this is the first block added to the CFG, it will be implicitly registered
178 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000179 Succ = createBlock();
180 assert (Succ == &cfg->getExit());
181 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000182
Ted Kremenek9aae5132007-08-23 21:42:29 +0000183 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000184 CFGBlock* B = addStmt(Statement);
Ted Kremenek40d87632008-02-27 17:33:02 +0000185 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000186
Ted Kremenek40d87632008-02-27 17:33:02 +0000187 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000188 // Finalize the last constructed block. This usually involves reversing the
189 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000190 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000191
192 // Backpatch the gotos whose label -> block mappings we didn't know when we
193 // encountered them.
194 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000195 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000196
Ted Kremenek9aae5132007-08-23 21:42:29 +0000197 CFGBlock* B = *I;
198 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
199 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
200
201 // If there is no target for the goto, then we are looking at an
202 // incomplete AST. Handle this by not registering a successor.
203 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000204
205 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000206 }
Mike Stump31feda52009-07-17 01:31:16 +0000207
Ted Kremenekeda180e22007-08-28 19:26:49 +0000208 // Add successors to the Indirect Goto Dispatch block (if we have one).
209 if (CFGBlock* B = cfg->getIndirectGotoBlock())
210 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
211 E = AddressTakenLabels.end(); I != E; ++I ) {
212
213 // Lookup the target block.
214 LabelMapTy::iterator LI = LabelMap.find(*I);
215
216 // If there is no target block that contains label, then we are looking
217 // at an incomplete AST. Handle this by not registering a successor.
218 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000219
220 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000221 }
Mike Stump31feda52009-07-17 01:31:16 +0000222
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000223 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000224 }
Mike Stump31feda52009-07-17 01:31:16 +0000225
226 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000227 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000228
Ted Kremenekb64d1832008-03-13 03:04:22 +0000229 if (badCFG) {
230 delete cfg;
231 cfg = NULL;
232 return NULL;
233 }
Mike Stump31feda52009-07-17 01:31:16 +0000234
235 // NULL out cfg so that repeated calls to the builder will fail and that the
236 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000237 CFG* t = cfg;
238 cfg = NULL;
239 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000240}
Mike Stump31feda52009-07-17 01:31:16 +0000241
Ted Kremenek9aae5132007-08-23 21:42:29 +0000242/// createBlock - Used to lazily create blocks that are connected
243/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000244CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000245 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000246 if (add_successor && Succ)
247 B->addSuccessor(Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000248 return B;
249}
Mike Stump31feda52009-07-17 01:31:16 +0000250
251/// FinishBlock - When the last statement has been added to the block, we must
252/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000253bool CFGBuilder::FinishBlock(CFGBlock* B) {
254 if (badCFG)
255 return false;
256
Ted Kremenek93668002009-07-17 22:18:43 +0000257 assert(B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000258 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000259 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000260}
261
Ted Kremenek93668002009-07-17 22:18:43 +0000262/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000263/// blocks for ternary operators, &&, and ||. We also process "," and
264/// DeclStmts (which may contain nested control-flow).
Ted Kremenek93668002009-07-17 22:18:43 +0000265CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
266tryAgain:
267 switch (S->getStmtClass()) {
268 default:
269 return VisitStmt(S, alwaysAdd);
270
271 case Stmt::AddrLabelExprClass:
272 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
273
274 case Stmt::BinaryOperatorClass:
275 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
276
277 case Stmt::BlockExprClass:
278 return VisitBlockExpr(cast<BlockExpr>(S));
279
280 case Stmt::BlockDeclRefExprClass:
281 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S));
282
283 case Stmt::BreakStmtClass:
284 return VisitBreakStmt(cast<BreakStmt>(S));
285
286 case Stmt::CallExprClass:
287 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
288
289 case Stmt::CaseStmtClass:
290 return VisitCaseStmt(cast<CaseStmt>(S));
291
292 case Stmt::ChooseExprClass:
293 return VisitChooseExpr(cast<ChooseExpr>(S));
294
295 case Stmt::CompoundStmtClass:
296 return VisitCompoundStmt(cast<CompoundStmt>(S));
297
298 case Stmt::ConditionalOperatorClass:
299 return VisitConditionalOperator(cast<ConditionalOperator>(S));
300
301 case Stmt::ContinueStmtClass:
302 return VisitContinueStmt(cast<ContinueStmt>(S));
303
304 case Stmt::DeclStmtClass:
305 return VisitDeclStmt(cast<DeclStmt>(S));
306
307 case Stmt::DefaultStmtClass:
308 return VisitDefaultStmt(cast<DefaultStmt>(S));
309
310 case Stmt::DoStmtClass:
311 return VisitDoStmt(cast<DoStmt>(S));
312
313 case Stmt::ForStmtClass:
314 return VisitForStmt(cast<ForStmt>(S));
315
316 case Stmt::GotoStmtClass:
317 return VisitGotoStmt(cast<GotoStmt>(S));
318
319 case Stmt::IfStmtClass:
320 return VisitIfStmt(cast<IfStmt>(S));
321
322 case Stmt::IndirectGotoStmtClass:
323 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
324
325 case Stmt::LabelStmtClass:
326 return VisitLabelStmt(cast<LabelStmt>(S));
327
328 case Stmt::ObjCAtCatchStmtClass:
329 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
330
331 case Stmt::ObjCAtSynchronizedStmtClass:
332 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
333
334 case Stmt::ObjCAtThrowStmtClass:
335 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
336
337 case Stmt::ObjCAtTryStmtClass:
338 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
339
340 case Stmt::ObjCForCollectionStmtClass:
341 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
342
343 case Stmt::ParenExprClass:
344 S = cast<ParenExpr>(S)->getSubExpr();
345 goto tryAgain;
346
347 case Stmt::NullStmtClass:
348 return Block;
349
350 case Stmt::ReturnStmtClass:
351 return VisitReturnStmt(cast<ReturnStmt>(S));
352
353 case Stmt::SizeOfAlignOfExprClass:
354 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S));
355
356 case Stmt::StmtExprClass:
357 return VisitStmtExpr(cast<StmtExpr>(S));
358
359 case Stmt::SwitchStmtClass:
360 return VisitSwitchStmt(cast<SwitchStmt>(S));
361
362 case Stmt::WhileStmtClass:
363 return VisitWhileStmt(cast<WhileStmt>(S));
364 }
365}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000366
Ted Kremenek93668002009-07-17 22:18:43 +0000367CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
368 if (alwaysAdd) {
369 autoCreateBlock();
370 Block->appendStmt(S);
Mike Stump31feda52009-07-17 01:31:16 +0000371 }
Ted Kremenek93668002009-07-17 22:18:43 +0000372
373 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000374}
Mike Stump31feda52009-07-17 01:31:16 +0000375
Ted Kremenek93668002009-07-17 22:18:43 +0000376/// VisitChildren - Visit the children of a Stmt.
377CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
378 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000379 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000380 E = Terminator->child_end(); I != E; ++I) {
381 if (*I) B = Visit(*I);
382 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000383 return B;
384}
Ted Kremenek93668002009-07-17 22:18:43 +0000385
386CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
387 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000388
Ted Kremenek93668002009-07-17 22:18:43 +0000389 if (alwaysAdd) {
390 autoCreateBlock();
391 Block->appendStmt(A);
392 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000393
Ted Kremenek9aae5132007-08-23 21:42:29 +0000394 return Block;
395}
Ted Kremenek93668002009-07-17 22:18:43 +0000396
397CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
398 if (B->isLogicalOp()) { // && or ||
Ted Kremenek9aae5132007-08-23 21:42:29 +0000399
Ted Kremenek93668002009-07-17 22:18:43 +0000400 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
401 ConfluenceBlock->appendStmt(B);
402
403 if (!FinishBlock(ConfluenceBlock))
404 return 0;
405
406 // create the block evaluating the LHS
407 CFGBlock* LHSBlock = createBlock(false);
408 LHSBlock->setTerminator(B);
409
410 // create the block evaluating the RHS
411 Succ = ConfluenceBlock;
412 Block = NULL;
413 CFGBlock* RHSBlock = addStmt(B->getRHS());
414 if (!FinishBlock(RHSBlock))
415 return 0;
416
417 // Now link the LHSBlock with RHSBlock.
418 if (B->getOpcode() == BinaryOperator::LOr) {
419 LHSBlock->addSuccessor(ConfluenceBlock);
420 LHSBlock->addSuccessor(RHSBlock);
421 } else {
422 assert (B->getOpcode() == BinaryOperator::LAnd);
423 LHSBlock->addSuccessor(RHSBlock);
424 LHSBlock->addSuccessor(ConfluenceBlock);
425 }
426
427 // Generate the blocks for evaluating the LHS.
428 Block = LHSBlock;
429 return addStmt(B->getLHS());
430 }
431 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
432 Block->appendStmt(B);
433 addStmt(B->getRHS());
434 return addStmt(B->getLHS());
435 }
436
437 return VisitStmt(B, alwaysAdd);
438}
439
440CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E) {
441 // FIXME
442 return NYS();
443}
444
445CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E) {
446 // FIXME
447 return NYS();
448}
449
450CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
451 // "break" is a control-flow statement. Thus we stop processing the current
452 // block.
453 if (Block && !FinishBlock(Block))
454 return 0;
455
456 // Now create a new block that ends with the break statement.
457 Block = createBlock(false);
458 Block->setTerminator(B);
459
460 // If there is no target for the break, then we are looking at an incomplete
461 // AST. This means that the CFG cannot be constructed.
462 if (BreakTargetBlock)
463 Block->addSuccessor(BreakTargetBlock);
464 else
465 badCFG = true;
466
467
Ted Kremenek9aae5132007-08-23 21:42:29 +0000468 return Block;
469}
Ted Kremenek93668002009-07-17 22:18:43 +0000470
471CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
472 // If this is a call to a no-return function, this stops the block here.
473 if (FunctionDecl *FD = C->getDirectCallee()) {
474
475 if (!FD->hasAttr<NoReturnAttr>())
476 return VisitStmt(C, alwaysAdd);
477
478 if (Block && !FinishBlock(Block))
479 return 0;
480
481 // Create new block with no successor for the remaining pieces.
482 Block = createBlock(false);
483 Block->appendStmt(C);
484
485 // Wire this to the exit block directly.
486 Block->addSuccessor(&cfg->getExit());
487
488 return VisitChildren(C);
489 }
490
491 return VisitStmt(C, alwaysAdd);
492}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000493
Ted Kremenek21822592009-07-17 18:20:32 +0000494CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
495 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
496 ConfluenceBlock->appendStmt(C);
497 if (!FinishBlock(ConfluenceBlock))
498 return 0;
499
500 Succ = ConfluenceBlock;
501 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000502 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000503 if (!FinishBlock(LHSBlock))
504 return 0;
505
506 Succ = ConfluenceBlock;
507 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000508 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000509 if (!FinishBlock(RHSBlock))
510 return 0;
511
512 Block = createBlock(false);
513 Block->addSuccessor(LHSBlock);
514 Block->addSuccessor(RHSBlock);
515 Block->setTerminator(C);
516 return addStmt(C->getCond());
517}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000518
Ted Kremenek93668002009-07-17 22:18:43 +0000519
520CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
521 CFGBlock* LastBlock = Block;
522
523 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
524 I != E; ++I ) {
525 LastBlock = addStmt(*I);
526 }
527 return LastBlock;
528}
529
Ted Kremenek51d40b02009-07-17 18:15:54 +0000530CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
531 // Create the confluence block that will "merge" the results of the ternary
532 // expression.
533 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
534 ConfluenceBlock->appendStmt(C);
535 if (!FinishBlock(ConfluenceBlock))
536 return 0;
537
538 // Create a block for the LHS expression if there is an LHS expression. A
539 // GCC extension allows LHS to be NULL, causing the condition to be the
540 // value that is returned instead.
541 // e.g: x ?: y is shorthand for: x ? x : y;
542 Succ = ConfluenceBlock;
543 Block = NULL;
544 CFGBlock* LHSBlock = NULL;
545 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000546 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000547 if (!FinishBlock(LHSBlock))
548 return 0;
549 Block = NULL;
550 }
551
552 // Create the block for the RHS expression.
553 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000554 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000555 if (!FinishBlock(RHSBlock))
556 return 0;
557
558 // Create the block that will contain the condition.
559 Block = createBlock(false);
560
561 if (LHSBlock)
562 Block->addSuccessor(LHSBlock);
563 else {
564 // If we have no LHS expression, add the ConfluenceBlock as a direct
565 // successor for the block containing the condition. Moreover, we need to
566 // reverse the order of the predecessors in the ConfluenceBlock because
567 // the RHSBlock will have been added to the succcessors already, and we
568 // want the first predecessor to the the block containing the expression
569 // for the case when the ternary expression evaluates to true.
570 Block->addSuccessor(ConfluenceBlock);
571 assert (ConfluenceBlock->pred_size() == 2);
572 std::reverse(ConfluenceBlock->pred_begin(),
573 ConfluenceBlock->pred_end());
574 }
575
576 Block->addSuccessor(RHSBlock);
577
578 Block->setTerminator(C);
579 return addStmt(C->getCond());
580}
581
Ted Kremenek93668002009-07-17 22:18:43 +0000582CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
583 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000584
Ted Kremenek93668002009-07-17 22:18:43 +0000585 if (DS->isSingleDecl()) {
586 Block->appendStmt(DS);
587 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000588 }
Ted Kremenek93668002009-07-17 22:18:43 +0000589
590 CFGBlock *B = 0;
591
592 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
593 typedef llvm::SmallVector<Decl*,10> BufTy;
594 BufTy Buf(DS->decl_begin(), DS->decl_end());
595
596 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
597 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
598 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
599 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
600
601 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
602 // automatically freed with the CFG.
603 DeclGroupRef DG(*I);
604 Decl *D = *I;
605 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
606 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
607
608 // Append the fake DeclStmt to block.
609 Block->appendStmt(DSNew);
610 B = VisitDeclSubExpr(D);
611 }
612
613 return B;
614}
615
616/// VisitDeclSubExpr - Utility method to add block-level expressions for
617/// initializers in Decls.
618CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
619 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000620
Ted Kremenek93668002009-07-17 22:18:43 +0000621 VarDecl *VD = dyn_cast<VarDecl>(D);
622
623 if (!VD)
624 return Block;
625
626 Expr *Init = VD->getInit();
627
628 if (Init) {
629 // Optimization: Don't create separate block-level statements for literals.
630 switch (Init->getStmtClass()) {
631 case Stmt::IntegerLiteralClass:
632 case Stmt::CharacterLiteralClass:
633 case Stmt::StringLiteralClass:
634 break;
635 default:
636 Block = addStmt(Init);
637 }
638 }
639
640 // If the type of VD is a VLA, then we must process its size expressions.
641 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
642 VA = FindVA(VA->getElementType().getTypePtr()))
643 Block = addStmt(VA->getSizeExpr());
644
645 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000646}
647
648CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000649 // We may see an if statement in the middle of a basic block, or it may be the
650 // first statement we are processing. In either case, we create a new basic
651 // block. First, we create the blocks for the then...else statements, and
652 // then we create the block containing the if statement. If we were in the
653 // middle of a block, we stop processing that block and reverse its
654 // statements. That block is then the implicit successor for the "then" and
655 // "else" clauses.
656
657 // The block we were proccessing is now finished. Make it the successor
658 // block.
659 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000660 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000661 if (!FinishBlock(Block))
662 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000663 }
Mike Stump31feda52009-07-17 01:31:16 +0000664
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000665 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000666 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000667
Ted Kremenek9aae5132007-08-23 21:42:29 +0000668 if (Stmt* Else = I->getElse()) {
669 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000670
Ted Kremenek9aae5132007-08-23 21:42:29 +0000671 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000672 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000673 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000674 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000675
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000676 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
677 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000678 else if (Block) {
679 if (!FinishBlock(ElseBlock))
680 return 0;
681 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000682 }
Mike Stump31feda52009-07-17 01:31:16 +0000683
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000684 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000685 CFGBlock* ThenBlock;
686 {
687 Stmt* Then = I->getThen();
688 assert (Then);
689 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000690 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000691 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000692
Ted Kremenek1b379512009-04-01 03:52:47 +0000693 if (!ThenBlock) {
694 // We can reach here if the "then" body has all NullStmts.
695 // Create an empty block so we can distinguish between true and false
696 // branches in path-sensitive analyses.
697 ThenBlock = createBlock(false);
698 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000699 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000700 if (!FinishBlock(ThenBlock))
701 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000702 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000703 }
704
Mike Stump31feda52009-07-17 01:31:16 +0000705 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000706 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000707
Ted Kremenek9aae5132007-08-23 21:42:29 +0000708 // Set the terminator of the new block to the If statement.
709 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000710
Ted Kremenek9aae5132007-08-23 21:42:29 +0000711 // Now add the successors.
712 Block->addSuccessor(ThenBlock);
713 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000714
715 // Add the condition as the last statement in the new block. This may create
716 // new blocks as the condition may contain control-flow. Any newly created
717 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000718 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000719}
Mike Stump31feda52009-07-17 01:31:16 +0000720
721
Ted Kremenek9aae5132007-08-23 21:42:29 +0000722CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000723 // If we were in the middle of a block we stop processing that block and
724 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000725 //
Mike Stump31feda52009-07-17 01:31:16 +0000726 // NOTE: If a "return" appears in the middle of a block, this means that the
727 // code afterwards is DEAD (unreachable). We still keep a basic block
728 // for that code; a simple "mark-and-sweep" from the entry block will be
729 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000730 if (Block) FinishBlock(Block);
731
732 // Create the new block.
733 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000734
Ted Kremenek9aae5132007-08-23 21:42:29 +0000735 // The Exit block is the only successor.
736 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000737
738 // Add the return statement to the block. This may create new blocks if R
739 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000740 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000741}
742
743CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
744 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000745 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000746 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000747
Ted Kremenek93668002009-07-17 22:18:43 +0000748 if (!LabelBlock) // This can happen when the body is empty, i.e.
749 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000750
Ted Kremenek93668002009-07-17 22:18:43 +0000751 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000752 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000753
754 // Labels partition blocks, so this is the end of the basic block we were
755 // processing (L is the block's label). Because this is label (and we have
756 // already processed the substatement) there is no extra control-flow to worry
757 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000758 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000759 if (!FinishBlock(LabelBlock))
760 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000761
762 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000763 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000764
Ted Kremenek9aae5132007-08-23 21:42:29 +0000765 // This block is now the implicit successor of other blocks.
766 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000767
Ted Kremenek9aae5132007-08-23 21:42:29 +0000768 return LabelBlock;
769}
770
771CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000772 // Goto is a control-flow statement. Thus we stop processing the current
773 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000774 if (Block)
775 FinishBlock(Block);
776
Ted Kremenek9aae5132007-08-23 21:42:29 +0000777 Block = createBlock(false);
778 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000779
780 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000781 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000782
Ted Kremenek9aae5132007-08-23 21:42:29 +0000783 if (I == LabelMap.end())
784 // We will need to backpatch this block later.
785 BackpatchBlocks.push_back(Block);
786 else
787 Block->addSuccessor(I->second);
788
Mike Stump31feda52009-07-17 01:31:16 +0000789 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000790}
791
792CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump31feda52009-07-17 01:31:16 +0000793 // "for" is a control-flow statement. Thus we stop processing the current
794 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000795 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000796
Ted Kremenek9aae5132007-08-23 21:42:29 +0000797 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000798 if (!FinishBlock(Block))
799 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000800 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000801 } else
802 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000803
804 // Because of short-circuit evaluation, the condition of the loop can span
805 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
806 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000807 CFGBlock* ExitConditionBlock = createBlock(false);
808 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000809
Ted Kremenek81e14852007-08-27 19:46:09 +0000810 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000811 ExitConditionBlock->setTerminator(F);
812
813 // Now add the actual condition to the condition block. Because the condition
814 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000815 if (Stmt* C = F->getCond()) {
816 Block = ExitConditionBlock;
817 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000818 if (Block) {
819 if (!FinishBlock(EntryConditionBlock))
820 return 0;
821 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000822 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000823
Mike Stump31feda52009-07-17 01:31:16 +0000824 // The condition block is the implicit successor for the loop body as well as
825 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000826 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000827
Ted Kremenek9aae5132007-08-23 21:42:29 +0000828 // Now create the loop body.
829 {
830 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000831
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832 // Save the current values for Block, Succ, and continue and break targets
833 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
834 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000835 save_break(BreakTargetBlock);
836
Ted Kremeneke9610502007-08-30 18:39:40 +0000837 // Create a new block to contain the (bottom) of the loop body.
838 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000839
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000840 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000841 // Generate increment code in its own basic block. This is the target of
842 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000843 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000844 } else {
845 // No increment code. Create a special, empty, block that is used as the
846 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000847 assert(Succ == EntryConditionBlock);
848 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000849 }
Mike Stump31feda52009-07-17 01:31:16 +0000850
Ted Kremenek902393b2009-04-28 00:51:56 +0000851 // Finish up the increment (or empty) block if it hasn't been already.
852 if (Block) {
853 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000854 if (!FinishBlock(Block))
855 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000856 Block = 0;
857 }
Mike Stump31feda52009-07-17 01:31:16 +0000858
Ted Kremenek902393b2009-04-28 00:51:56 +0000859 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000860
Ted Kremenek902393b2009-04-28 00:51:56 +0000861 // The starting block for the loop increment is the block that should
862 // represent the 'loop target' for looping back to the start of the loop.
863 ContinueTargetBlock->setLoopTarget(F);
864
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000865 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000866 BreakTargetBlock = LoopSuccessor;
867
868 // Now populate the body block, and in the process create new blocks as we
869 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000870 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000871
872 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000873 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000874 else if (Block) {
875 if (!FinishBlock(BodyBlock))
876 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000877 }
878
Ted Kremenek81e14852007-08-27 19:46:09 +0000879 // This new body block is a successor to our "exit" condition block.
880 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000881 }
Mike Stump31feda52009-07-17 01:31:16 +0000882
883 // Link up the condition block with the code that follows the loop. (the
884 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +0000885 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000886
Ted Kremenek9aae5132007-08-23 21:42:29 +0000887 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000888 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000889 if (Stmt* I = F->getInit()) {
890 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000891 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000892 } else {
893 // There is no loop initialization. We are thus basically a while loop.
894 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000895 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000896 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000897 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000898 }
899}
900
Ted Kremenek9d56e642008-11-11 17:10:00 +0000901CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
902 // Objective-C fast enumeration 'for' statements:
903 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
904 //
905 // for ( Type newVariable in collection_expression ) { statements }
906 //
907 // becomes:
908 //
909 // prologue:
910 // 1. collection_expression
911 // T. jump to loop_entry
912 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000913 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000914 // 1. ObjCForCollectionStmt [performs binding to newVariable]
915 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
916 // TB:
917 // statements
918 // T. jump to loop_entry
919 // FB:
920 // what comes after
921 //
922 // and
923 //
924 // Type existingItem;
925 // for ( existingItem in expression ) { statements }
926 //
927 // becomes:
928 //
Mike Stump31feda52009-07-17 01:31:16 +0000929 // the same with newVariable replaced with existingItem; the binding works
930 // the same except that for one ObjCForCollectionStmt::getElement() returns
931 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000932 //
Mike Stump31feda52009-07-17 01:31:16 +0000933
Ted Kremenek9d56e642008-11-11 17:10:00 +0000934 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000935
Ted Kremenek9d56e642008-11-11 17:10:00 +0000936 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000937 if (!FinishBlock(Block))
938 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000939 LoopSuccessor = Block;
940 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000941 } else
942 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000943
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000944 // Build the condition blocks.
945 CFGBlock* ExitConditionBlock = createBlock(false);
946 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000947
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000948 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000949 ExitConditionBlock->setTerminator(S);
950
951 // The last statement in the block should be the ObjCForCollectionStmt, which
952 // performs the actual binding to 'element' and determines if there are any
953 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000954 ExitConditionBlock->appendStmt(S);
955 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000956
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000957 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +0000958 // generate new blocks as necesary. We DON'T add the statement by default to
959 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +0000960 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +0000961 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000962 if (!FinishBlock(EntryConditionBlock))
963 return 0;
964 Block = 0;
965 }
Mike Stump31feda52009-07-17 01:31:16 +0000966
967 // The condition block is the implicit successor for the loop body as well as
968 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000969 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000970
Ted Kremenek9d56e642008-11-11 17:10:00 +0000971 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +0000972 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000973 // Save the current values for Succ, continue and break targets.
974 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +0000975 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
976
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000977 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +0000978 ContinueTargetBlock = EntryConditionBlock;
979
Ted Kremenek93668002009-07-17 22:18:43 +0000980 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000981
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000982 if (!BodyBlock)
983 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000984 else if (Block) {
985 if (!FinishBlock(BodyBlock))
986 return 0;
987 }
Mike Stump31feda52009-07-17 01:31:16 +0000988
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000989 // This new body block is a successor to our "exit" condition block.
990 ExitConditionBlock->addSuccessor(BodyBlock);
991 }
Mike Stump31feda52009-07-17 01:31:16 +0000992
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000993 // Link up the condition block with the code that follows the loop.
994 // (the false branch).
995 ExitConditionBlock->addSuccessor(LoopSuccessor);
996
Ted Kremenek9d56e642008-11-11 17:10:00 +0000997 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000998 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +0000999 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001000}
1001
Ted Kremenek49805452009-05-02 01:49:13 +00001002CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1003 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001004
Ted Kremenek49805452009-05-02 01:49:13 +00001005 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001006 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001007
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001008 // The sync body starts its own basic block. This makes it a little easier
1009 // for diagnostic clients.
1010 if (SyncBlock) {
1011 if (!FinishBlock(SyncBlock))
1012 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001013
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001014 Block = 0;
1015 }
Mike Stump31feda52009-07-17 01:31:16 +00001016
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001017 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001018
Ted Kremenek49805452009-05-02 01:49:13 +00001019 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001020 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001021}
Mike Stump31feda52009-07-17 01:31:16 +00001022
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001023CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001024 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001025 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001026}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001027
Ted Kremenek9aae5132007-08-23 21:42:29 +00001028CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump31feda52009-07-17 01:31:16 +00001029 // "while" is a control-flow statement. Thus we stop processing the current
1030 // block.
1031
Ted Kremenek9aae5132007-08-23 21:42:29 +00001032 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek9aae5132007-08-23 21:42:29 +00001034 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001035 if (!FinishBlock(Block))
1036 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001037 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001038 } else
1039 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001040
1041 // Because of short-circuit evaluation, the condition of the loop can span
1042 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1043 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001044 CFGBlock* ExitConditionBlock = createBlock(false);
1045 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001046
Ted Kremenek81e14852007-08-27 19:46:09 +00001047 // Set the terminator for the "exit" condition block.
1048 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001049
1050 // Now add the actual condition to the condition block. Because the condition
1051 // itself may contain control-flow, new blocks may be created. Thus we update
1052 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001053 if (Stmt* C = W->getCond()) {
1054 Block = ExitConditionBlock;
1055 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001056 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001057 if (Block) {
1058 if (!FinishBlock(EntryConditionBlock))
1059 return 0;
1060 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001061 }
Mike Stump31feda52009-07-17 01:31:16 +00001062
1063 // The condition block is the implicit successor for the loop body as well as
1064 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001065 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001066
Ted Kremenek9aae5132007-08-23 21:42:29 +00001067 // Process the loop body.
1068 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001069 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001070
1071 // Save the current values for Block, Succ, and continue and break targets
1072 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1073 save_continue(ContinueTargetBlock),
1074 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001075
Mike Stump31feda52009-07-17 01:31:16 +00001076 // Create an empty block to represent the transition block for looping back
1077 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001078 Block = 0;
1079 assert(Succ == EntryConditionBlock);
1080 Succ = createBlock();
1081 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001082 ContinueTargetBlock = Succ;
1083
Ted Kremenek9aae5132007-08-23 21:42:29 +00001084 // All breaks should go to the code following the loop.
1085 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001086
Ted Kremenek9aae5132007-08-23 21:42:29 +00001087 // NULL out Block to force lazy instantiation of blocks for the body.
1088 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001089
Ted Kremenek9aae5132007-08-23 21:42:29 +00001090 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001091 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001092
Ted Kremeneke9610502007-08-30 18:39:40 +00001093 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001094 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001095 else if (Block) {
1096 if (!FinishBlock(BodyBlock))
1097 return 0;
1098 }
Mike Stump31feda52009-07-17 01:31:16 +00001099
Ted Kremenek9aae5132007-08-23 21:42:29 +00001100 // Add the loop body entry as a successor to the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001101 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001102 }
Mike Stump31feda52009-07-17 01:31:16 +00001103
1104 // Link up the condition block with the code that follows the loop. (the
1105 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001106 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001107
1108 // There can be no more statements in the condition block since we loop back
1109 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001110 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001111
Ted Kremenek9aae5132007-08-23 21:42:29 +00001112 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001113 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001114 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001115}
Ted Kremenek93668002009-07-17 22:18:43 +00001116
1117
1118CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1119 // FIXME: For now we pretend that @catch and the code it contains does not
1120 // exit.
1121 return Block;
1122}
Mike Stump31feda52009-07-17 01:31:16 +00001123
Ted Kremenek93041ba2008-12-09 20:20:09 +00001124CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1125 // FIXME: This isn't complete. We basically treat @throw like a return
1126 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001127
1128 // If we were in the middle of a block we stop processing that block and
1129 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001130 if (Block && !FinishBlock(Block))
1131 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001132
Ted Kremenek93041ba2008-12-09 20:20:09 +00001133 // Create the new block.
1134 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001135
Ted Kremenek93041ba2008-12-09 20:20:09 +00001136 // The Exit block is the only successor.
1137 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001138
1139 // Add the statement to the block. This may create new blocks if S contains
1140 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001141 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001142}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001143
Ted Kremenek93668002009-07-17 22:18:43 +00001144CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001145 // "do...while" is a control-flow statement. Thus we stop processing the
1146 // current block.
Mike Stump31feda52009-07-17 01:31:16 +00001147
Ted Kremenek9aae5132007-08-23 21:42:29 +00001148 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001149
Ted Kremenek9aae5132007-08-23 21:42:29 +00001150 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001151 if (!FinishBlock(Block))
1152 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001153 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001154 } else
1155 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001156
1157 // Because of short-circuit evaluation, the condition of the loop can span
1158 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1159 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001160 CFGBlock* ExitConditionBlock = createBlock(false);
1161 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001162
Ted Kremenek81e14852007-08-27 19:46:09 +00001163 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001164 ExitConditionBlock->setTerminator(D);
1165
1166 // Now add the actual condition to the condition block. Because the condition
1167 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001168 if (Stmt* C = D->getCond()) {
1169 Block = ExitConditionBlock;
1170 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001171 if (Block) {
1172 if (!FinishBlock(EntryConditionBlock))
1173 return 0;
1174 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001175 }
Mike Stump31feda52009-07-17 01:31:16 +00001176
Ted Kremeneka1523a32008-02-27 07:20:00 +00001177 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001178 Succ = EntryConditionBlock;
1179
Ted Kremenek9aae5132007-08-23 21:42:29 +00001180 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001181 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001182 {
1183 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001184
Ted Kremenek9aae5132007-08-23 21:42:29 +00001185 // Save the current values for Block, Succ, and continue and break targets
1186 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1187 save_continue(ContinueTargetBlock),
1188 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001189
Ted Kremenek9aae5132007-08-23 21:42:29 +00001190 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001191 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001192
Ted Kremenek9aae5132007-08-23 21:42:29 +00001193 // All breaks should go to the code following the loop.
1194 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001195
Ted Kremenek9aae5132007-08-23 21:42:29 +00001196 // NULL out Block to force lazy instantiation of blocks for the body.
1197 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001198
Ted Kremenek9aae5132007-08-23 21:42:29 +00001199 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001200 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001201
Ted Kremeneke9610502007-08-30 18:39:40 +00001202 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001203 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001204 else if (Block) {
1205 if (!FinishBlock(BodyBlock))
1206 return 0;
1207 }
Mike Stump31feda52009-07-17 01:31:16 +00001208
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001209 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001210 // ExitConditionBlock to represent the "loop back" transition. Create an
1211 // empty block to represent the transition block for looping back to the
1212 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001213 // FIXME: Can we do this more efficiently without adding another block?
1214 Block = NULL;
1215 Succ = BodyBlock;
1216 CFGBlock *LoopBackBlock = createBlock();
1217 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001218
Ted Kremenek9aae5132007-08-23 21:42:29 +00001219 // Add the loop body entry as a successor to the condition.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001220 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001221 }
Mike Stump31feda52009-07-17 01:31:16 +00001222
Ted Kremenek9aae5132007-08-23 21:42:29 +00001223 // Link up the condition block with the code that follows the loop.
1224 // (the false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001225 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001226
1227 // There can be no more statements in the body block(s) since we loop back to
1228 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001229 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001230
Ted Kremenek9aae5132007-08-23 21:42:29 +00001231 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001232 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001233 return BodyBlock;
1234}
1235
1236CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1237 // "continue" is a control-flow statement. Thus we stop processing the
1238 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001239 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001240 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001241
Ted Kremenek9aae5132007-08-23 21:42:29 +00001242 // Now create a new block that ends with the continue statement.
1243 Block = createBlock(false);
1244 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001245
Ted Kremenek9aae5132007-08-23 21:42:29 +00001246 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001247 // incomplete AST. This means the CFG cannot be constructed.
1248 if (ContinueTargetBlock)
1249 Block->addSuccessor(ContinueTargetBlock);
1250 else
1251 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001252
Ted Kremenek9aae5132007-08-23 21:42:29 +00001253 return Block;
1254}
Ted Kremenek93668002009-07-17 22:18:43 +00001255
1256CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
1257 // VLA types have expressions that must be evaluated.
1258 if (E->isArgumentType()) {
1259 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1260 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1261 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001262 }
Ted Kremenek93668002009-07-17 22:18:43 +00001263 // Expressions in sizeof/alignof are not evaluated and thus have no
1264 // control flow.
1265 else {
1266 autoCreateBlock();
1267 Block->appendStmt(E);
1268 }
1269
Mike Stump31feda52009-07-17 01:31:16 +00001270 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001271}
Ted Kremenek93668002009-07-17 22:18:43 +00001272
1273/// VisitStmtExpr - Utility method to handle (nested) statement
1274/// expressions (a GCC extension).
1275CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE) {
1276 autoCreateBlock();
1277 Block->appendStmt(SE);
1278 return VisitCompoundStmt(SE->getSubStmt());
1279}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001280
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001281CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001282 // "switch" is a control-flow statement. Thus we stop processing the current
1283 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001284 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001285
Ted Kremenek9aae5132007-08-23 21:42:29 +00001286 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001287 if (!FinishBlock(Block))
1288 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001289 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001290 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001291
1292 // Save the current "switch" context.
1293 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001294 save_break(BreakTargetBlock),
1295 save_default(DefaultCaseBlock);
1296
Mike Stump31feda52009-07-17 01:31:16 +00001297 // Set the "default" case to be the block after the switch statement. If the
1298 // switch statement contains a "default:", this value will be overwritten with
1299 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001300 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001301
Ted Kremenek9aae5132007-08-23 21:42:29 +00001302 // Create a new block that will contain the switch statement.
1303 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001304
Ted Kremenek9aae5132007-08-23 21:42:29 +00001305 // Now process the switch body. The code after the switch is the implicit
1306 // successor.
1307 Succ = SwitchSuccessor;
1308 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001309
1310 // When visiting the body, the case statements should automatically get linked
1311 // up to the switch. We also don't keep a pointer to the body, since all
1312 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001313 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001314 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001315 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001316 if (Block) {
1317 if (!FinishBlock(BodyBlock))
1318 return 0;
1319 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001320
Mike Stump31feda52009-07-17 01:31:16 +00001321 // If we have no "default:" case, the default transition is to the code
1322 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001323 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001324
Ted Kremenek81e14852007-08-27 19:46:09 +00001325 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001326 SwitchTerminatedBlock->setTerminator(Terminator);
1327 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001328 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001329
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001330 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001331}
1332
Ted Kremenek93668002009-07-17 22:18:43 +00001333CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001334 // CaseStmts are essentially labels, so they are the first statement in a
1335 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001336
Ted Kremenek93668002009-07-17 22:18:43 +00001337 if (CS->getSubStmt())
1338 addStmt(CS->getSubStmt());
1339
Ted Kremenek55e91e82007-08-30 18:48:11 +00001340 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001341 if (!CaseBlock)
1342 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001343
1344 // Cases statements partition blocks, so this is the top of the basic block we
1345 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001346 CaseBlock->setLabel(CS);
1347
Ted Kremenek55957a82009-05-02 00:13:27 +00001348 if (!FinishBlock(CaseBlock))
1349 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001350
1351 // Add this block to the list of successors for the block with the switch
1352 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001353 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001354 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek9aae5132007-08-23 21:42:29 +00001356 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1357 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremenek9aae5132007-08-23 21:42:29 +00001359 // This block is now the implicit successor of other blocks.
1360 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001361
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001362 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001363}
Mike Stump31feda52009-07-17 01:31:16 +00001364
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001365CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001366 if (Terminator->getSubStmt())
1367 addStmt(Terminator->getSubStmt());
1368
Ted Kremenek654c78f2008-02-13 22:05:39 +00001369 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001370
1371 if (!DefaultCaseBlock)
1372 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001373
1374 // Default statements partition blocks, so this is the top of the basic block
1375 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001376 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001377
Ted Kremenek55957a82009-05-02 00:13:27 +00001378 if (!FinishBlock(DefaultCaseBlock))
1379 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001380
Mike Stump31feda52009-07-17 01:31:16 +00001381 // Unlike case statements, we don't add the default block to the successors
1382 // for the switch statement immediately. This is done when we finish
1383 // processing the switch statement. This allows for the default case
1384 // (including a fall-through to the code after the switch statement) to always
1385 // be the last successor of a switch-terminated block.
1386
Ted Kremenek654c78f2008-02-13 22:05:39 +00001387 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1388 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001389
Ted Kremenek654c78f2008-02-13 22:05:39 +00001390 // This block is now the implicit successor of other blocks.
1391 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001392
1393 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001394}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001395
Ted Kremenekeda180e22007-08-28 19:26:49 +00001396CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001397 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001398 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001399
Ted Kremenekeda180e22007-08-28 19:26:49 +00001400 if (!IBlock) {
1401 IBlock = createBlock(false);
1402 cfg->setIndirectGotoBlock(IBlock);
1403 }
Mike Stump31feda52009-07-17 01:31:16 +00001404
Ted Kremenekeda180e22007-08-28 19:26:49 +00001405 // IndirectGoto is a control-flow statement. Thus we stop processing the
1406 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001407 if (Block && !FinishBlock(Block))
1408 return 0;
1409
Ted Kremenekeda180e22007-08-28 19:26:49 +00001410 Block = createBlock(false);
1411 Block->setTerminator(I);
1412 Block->addSuccessor(IBlock);
1413 return addStmt(I->getTarget());
1414}
1415
Ted Kremenek04cca642007-08-23 21:26:19 +00001416} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001417
Mike Stump31feda52009-07-17 01:31:16 +00001418/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1419/// no successors or predecessors. If this is the first block created in the
1420/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001421CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001422 bool first_block = begin() == end();
1423
1424 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001425 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001426
1427 // If this is the first block, set it as the Entry and Exit.
1428 if (first_block) Entry = Exit = &front();
1429
1430 // Return the block.
1431 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001432}
1433
Ted Kremenek889073f2007-08-23 16:51:22 +00001434/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1435/// CFG is returned to the caller.
1436CFG* CFG::buildCFG(Stmt* Statement) {
1437 CFGBuilder Builder;
1438 return Builder.buildCFG(Statement);
1439}
1440
1441/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001442void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1443
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001444//===----------------------------------------------------------------------===//
1445// CFG: Queries for BlkExprs.
1446//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001447
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001448namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001449 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001450}
1451
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001452static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1453 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001454 return;
Mike Stump31feda52009-07-17 01:31:16 +00001455
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001456 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001457 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001458
Ted Kremenek95a123c2008-01-26 00:03:27 +00001459 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1460 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001461
Ted Kremenek95a123c2008-01-26 00:03:27 +00001462 FindSubExprAssignments(*I, Set);
1463 }
1464}
1465
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001466static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1467 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001468
1469 // Look for assignments that are used as subexpressions. These are the only
1470 // assignments that we want to *possibly* register as a block-level
1471 // expression. Basically, if an assignment occurs both in a subexpression and
1472 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001473 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001474
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001475 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1476 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001477 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001478
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001479 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001480
1481 // Iterate over the statements again on identify the Expr* and Stmt* at the
1482 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001483
Ted Kremenek95a123c2008-01-26 00:03:27 +00001484 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001485 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001486
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001487 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001488 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001489 // expression are really "statements" whose value is never used by
1490 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001491 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001492 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001493 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1494 // Special handling for statement expressions. The last statement in
1495 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001496 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001497 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001498 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001499 (*M)[C->body_back()] = x;
1500 }
1501 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001502
Ted Kremenek95a123c2008-01-26 00:03:27 +00001503 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001504 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001505 }
Mike Stump31feda52009-07-17 01:31:16 +00001506
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001507 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001508
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001509 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001510
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001511 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001512 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001513 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001514 }
1515 }
Mike Stump31feda52009-07-17 01:31:16 +00001516
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001517 return M;
1518}
1519
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001520CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1521 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001522 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001523
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001524 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001525 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001526
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001527 if (I == M->end()) return CFG::BlkExprNumTy();
1528 else return CFG::BlkExprNumTy(I->second);
1529}
1530
1531unsigned CFG::getNumBlkExprs() {
1532 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1533 return M->size();
1534 else {
1535 // We assume callers interested in the number of BlkExprs will want
1536 // the map constructed if it doesn't already exist.
1537 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1538 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1539 }
1540}
1541
Ted Kremenek6065ef62008-04-28 18:00:46 +00001542//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001543// Cleanup: CFG dstor.
1544//===----------------------------------------------------------------------===//
1545
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001546CFG::~CFG() {
1547 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1548}
Mike Stump31feda52009-07-17 01:31:16 +00001549
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001550//===----------------------------------------------------------------------===//
1551// CFG pretty printing
1552//===----------------------------------------------------------------------===//
1553
Ted Kremenek7e776b12007-08-22 18:22:34 +00001554namespace {
1555
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001556class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001557
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001558 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1559 StmtMapTy StmtMap;
1560 signed CurrentBlock;
1561 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001562 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001563public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001564
Chris Lattnerc61089a2009-06-30 01:26:17 +00001565 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1566 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001567 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1568 unsigned j = 1;
1569 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1570 BI != BEnd; ++BI, ++j )
1571 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1572 }
1573 }
Mike Stump31feda52009-07-17 01:31:16 +00001574
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001575 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001576
Chris Lattnerc61089a2009-06-30 01:26:17 +00001577 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001578 void setBlockID(signed i) { CurrentBlock = i; }
1579 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001580
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001581 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001582
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001583 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001584
1585 if (I == StmtMap.end())
1586 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001587
1588 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001589 && I->second.second == CurrentStmt)
1590 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001592 OS << "[B" << I->second.first << "." << I->second.second << "]";
1593 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001594 }
1595};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001596} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001597
Chris Lattnerc61089a2009-06-30 01:26:17 +00001598
1599namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001600class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1601 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001602
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001603 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001604 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001605 PrintingPolicy Policy;
1606
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001607public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001608 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001609 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001610 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001611
Ted Kremenek9aae5132007-08-23 21:42:29 +00001612 void VisitIfStmt(IfStmt* I) {
1613 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001614 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001615 }
Mike Stump31feda52009-07-17 01:31:16 +00001616
Ted Kremenek9aae5132007-08-23 21:42:29 +00001617 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001618 void VisitStmt(Stmt* Terminator) {
1619 Terminator->printPretty(OS, Helper, Policy);
1620 }
1621
Ted Kremenek9aae5132007-08-23 21:42:29 +00001622 void VisitForStmt(ForStmt* F) {
1623 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001624 if (F->getInit()) OS << "...";
1625 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001626 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001627 OS << "; ";
1628 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001629 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001630 }
Mike Stump31feda52009-07-17 01:31:16 +00001631
Ted Kremenek9aae5132007-08-23 21:42:29 +00001632 void VisitWhileStmt(WhileStmt* W) {
1633 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001634 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001635 }
Mike Stump31feda52009-07-17 01:31:16 +00001636
Ted Kremenek9aae5132007-08-23 21:42:29 +00001637 void VisitDoStmt(DoStmt* D) {
1638 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001639 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001640 }
Mike Stump31feda52009-07-17 01:31:16 +00001641
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001642 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001643 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001644 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001645 }
Mike Stump31feda52009-07-17 01:31:16 +00001646
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001647 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001648 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001649 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001650 }
Mike Stump31feda52009-07-17 01:31:16 +00001651
Ted Kremenek391f94a2007-08-31 22:29:13 +00001652 void VisitChooseExpr(ChooseExpr* C) {
1653 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001654 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001655 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001656 }
Mike Stump31feda52009-07-17 01:31:16 +00001657
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001658 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1659 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001660 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001661 }
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001663 void VisitBinaryOperator(BinaryOperator* B) {
1664 if (!B->isLogicalOp()) {
1665 VisitExpr(B);
1666 return;
1667 }
Mike Stump31feda52009-07-17 01:31:16 +00001668
Douglas Gregor7de59662009-05-29 20:38:28 +00001669 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001671 switch (B->getOpcode()) {
1672 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001673 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001674 return;
1675 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001676 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001677 return;
1678 default:
1679 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001680 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001681 }
Mike Stump31feda52009-07-17 01:31:16 +00001682
Ted Kremenek12687ff2007-08-27 21:54:41 +00001683 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001684 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001685 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001686};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001687} // end anonymous namespace
1688
Mike Stump31feda52009-07-17 01:31:16 +00001689
Chris Lattnerc61089a2009-06-30 01:26:17 +00001690static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1691 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001692 if (Helper) {
1693 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001694 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001695 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001696
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001697 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001698 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001699 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001700 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001701 return;
1702 }
1703 }
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001705 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001706 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001707 if (B->getOpcode() == BinaryOperator::Comma) {
1708 OS << "... , ";
1709 Helper->handledStmt(B->getRHS(),OS);
1710 OS << '\n';
1711 return;
Mike Stump31feda52009-07-17 01:31:16 +00001712 }
1713 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001714 }
Mike Stump31feda52009-07-17 01:31:16 +00001715
Chris Lattnerc61089a2009-06-30 01:26:17 +00001716 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001717
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001718 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001719 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001720}
Mike Stump31feda52009-07-17 01:31:16 +00001721
Chris Lattnerc61089a2009-06-30 01:26:17 +00001722static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1723 const CFGBlock& B,
1724 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001725
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001726 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001727
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001728 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001729 OS << "\n [ B" << B.getBlockID();
1730
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001731 if (&B == &cfg->getEntry())
1732 OS << " (ENTRY) ]\n";
1733 else if (&B == &cfg->getExit())
1734 OS << " (EXIT) ]\n";
1735 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001736 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001737 else
1738 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001739
Ted Kremenek71eca012007-08-29 23:20:49 +00001740 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001741 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001742
1743 if (print_edges)
1744 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001745
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001746 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001747 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001748 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001749 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001750 C->getLHS()->printPretty(OS, Helper,
1751 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001752 if (C->getRHS()) {
1753 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001754 C->getRHS()->printPretty(OS, Helper,
1755 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001756 }
Mike Stump31feda52009-07-17 01:31:16 +00001757 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001758 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001759 else
1760 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001761
Ted Kremenek71eca012007-08-29 23:20:49 +00001762 OS << ":\n";
1763 }
Mike Stump31feda52009-07-17 01:31:16 +00001764
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001765 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001766 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001767
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001768 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1769 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001770
Ted Kremenek71eca012007-08-29 23:20:49 +00001771 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001772 if (print_edges)
1773 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001774
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001775 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001776
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001777 if (Helper)
1778 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001779
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001780 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001781 }
Mike Stump31feda52009-07-17 01:31:16 +00001782
Ted Kremenek71eca012007-08-29 23:20:49 +00001783 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001784 if (B.getTerminator()) {
1785 if (print_edges)
1786 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001787
Ted Kremenek71eca012007-08-29 23:20:49 +00001788 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001789
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001790 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001791
Chris Lattnerc61089a2009-06-30 01:26:17 +00001792 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1793 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001794 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001795 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001796 }
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenek71eca012007-08-29 23:20:49 +00001798 if (print_edges) {
1799 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001800 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001801 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001802
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001803 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1804 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001805
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001806 if (i == 8 || (i-8) == 0)
1807 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001808
Ted Kremenek71eca012007-08-29 23:20:49 +00001809 OS << " B" << (*I)->getBlockID();
1810 }
Mike Stump31feda52009-07-17 01:31:16 +00001811
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001812 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001813
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001814 // Print the successors of this block.
1815 OS << " Successors (" << B.succ_size() << "):";
1816 i = 0;
1817
1818 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1819 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001820
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001821 if (i == 8 || (i-8) % 10 == 0)
1822 OS << "\n ";
1823
1824 OS << " B" << (*I)->getBlockID();
1825 }
Mike Stump31feda52009-07-17 01:31:16 +00001826
Ted Kremenek71eca012007-08-29 23:20:49 +00001827 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001828 }
Mike Stump31feda52009-07-17 01:31:16 +00001829}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001830
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001831
1832/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001833void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001834
1835/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001836void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1837 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001838
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001839 // Print the entry block.
1840 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001841
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001842 // Iterate through the CFGBlocks and print them one by one.
1843 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1844 // Skip the entry block, because we already printed it.
1845 if (&(*I) == &getEntry() || &(*I) == &getExit())
1846 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001847
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001848 print_block(OS, this, *I, &Helper, true);
1849 }
Mike Stump31feda52009-07-17 01:31:16 +00001850
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001851 // Print the exit block.
1852 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001853 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001854}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001855
1856/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001857void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1858 print(llvm::errs(), cfg, LO);
1859}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001860
1861/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1862/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001863void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1864 const LangOptions &LO) const {
1865 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001866 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001867}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001868
Ted Kremenek15647632008-01-30 23:02:42 +00001869/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001870void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001871 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001872 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001873 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1874}
1875
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001876Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001877
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001878 if (!Terminator)
1879 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001880
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001881 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001882
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001883 switch (Terminator->getStmtClass()) {
1884 default:
1885 break;
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001887 case Stmt::ForStmtClass:
1888 E = cast<ForStmt>(Terminator)->getCond();
1889 break;
Mike Stump31feda52009-07-17 01:31:16 +00001890
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001891 case Stmt::WhileStmtClass:
1892 E = cast<WhileStmt>(Terminator)->getCond();
1893 break;
Mike Stump31feda52009-07-17 01:31:16 +00001894
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001895 case Stmt::DoStmtClass:
1896 E = cast<DoStmt>(Terminator)->getCond();
1897 break;
Mike Stump31feda52009-07-17 01:31:16 +00001898
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001899 case Stmt::IfStmtClass:
1900 E = cast<IfStmt>(Terminator)->getCond();
1901 break;
Mike Stump31feda52009-07-17 01:31:16 +00001902
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001903 case Stmt::ChooseExprClass:
1904 E = cast<ChooseExpr>(Terminator)->getCond();
1905 break;
Mike Stump31feda52009-07-17 01:31:16 +00001906
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001907 case Stmt::IndirectGotoStmtClass:
1908 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1909 break;
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001911 case Stmt::SwitchStmtClass:
1912 E = cast<SwitchStmt>(Terminator)->getCond();
1913 break;
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001915 case Stmt::ConditionalOperatorClass:
1916 E = cast<ConditionalOperator>(Terminator)->getCond();
1917 break;
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001919 case Stmt::BinaryOperatorClass: // '&&' and '||'
1920 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001921 break;
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001923 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00001924 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001925 }
Mike Stump31feda52009-07-17 01:31:16 +00001926
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001927 return E ? E->IgnoreParens() : NULL;
1928}
1929
Ted Kremenek92137a32008-05-16 16:06:00 +00001930bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00001931
Ted Kremenek92137a32008-05-16 16:06:00 +00001932 if (!Terminator)
1933 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001934
Ted Kremenek92137a32008-05-16 16:06:00 +00001935 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek92137a32008-05-16 16:06:00 +00001937 switch (Terminator->getStmtClass()) {
1938 default:
1939 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001940
1941 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00001942 case Stmt::WhileStmtClass:
1943 case Stmt::DoStmtClass:
1944 case Stmt::IfStmtClass:
1945 case Stmt::ChooseExprClass:
1946 case Stmt::ConditionalOperatorClass:
1947 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00001948 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00001949 }
Mike Stump31feda52009-07-17 01:31:16 +00001950
Ted Kremenek92137a32008-05-16 16:06:00 +00001951 return E ? E->IgnoreParens() : NULL;
1952}
1953
Ted Kremenek15647632008-01-30 23:02:42 +00001954
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001955//===----------------------------------------------------------------------===//
1956// CFG Graphviz Visualization
1957//===----------------------------------------------------------------------===//
1958
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001959
1960#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00001961static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001962#endif
1963
Chris Lattnerc61089a2009-06-30 01:26:17 +00001964void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001965#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00001966 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967 GraphHelper = &H;
1968 llvm::ViewGraph(this,"CFG");
1969 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001970#endif
1971}
1972
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001973namespace llvm {
1974template<>
1975struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00001976 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1977 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001978
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001979#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001980 std::string OutSStr;
1981 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001982 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001983 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001984
1985 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1986
1987 // Process string output to make it nicer...
1988 for (unsigned i = 0; i != OutStr.length(); ++i)
1989 if (OutStr[i] == '\n') { // Left justify
1990 OutStr[i] = '\\';
1991 OutStr.insert(OutStr.begin()+i+1, 'l');
1992 }
Mike Stump31feda52009-07-17 01:31:16 +00001993
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001994 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001995#else
1996 return "";
1997#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001998 }
1999};
2000} // end namespace llvm