blob: 5a9e61143214c416de459ea6d89412c8a480e0f3 [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);
Ted Kremenek0747de62009-07-18 00:47:21 +0000103 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
104 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000105 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);
Ted Kremenek0747de62009-07-18 00:47:21 +0000127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
128 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000129 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:
Ted Kremenek0747de62009-07-18 00:47:21 +0000278 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000279
280 case Stmt::BlockDeclRefExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000281 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000282
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:
Ted Kremenek0747de62009-07-18 00:47:21 +0000354 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000355
356 case Stmt::StmtExprClass:
Ted Kremenek0747de62009-07-18 00:47:21 +0000357 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek93668002009-07-17 22:18:43 +0000358
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) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000432 autoCreateBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000433 Block->appendStmt(B);
434 addStmt(B->getRHS());
435 return addStmt(B->getLHS());
436 }
437
438 return VisitStmt(B, alwaysAdd);
439}
440
Ted Kremenek0747de62009-07-18 00:47:21 +0000441CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000442 // FIXME
443 return NYS();
444}
445
Ted Kremenek0747de62009-07-18 00:47:21 +0000446CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
447 bool alwaysAdd) {
Ted Kremenek93668002009-07-17 22:18:43 +0000448 // FIXME
449 return NYS();
450}
451
452CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
453 // "break" is a control-flow statement. Thus we stop processing the current
454 // block.
455 if (Block && !FinishBlock(Block))
456 return 0;
457
458 // Now create a new block that ends with the break statement.
459 Block = createBlock(false);
460 Block->setTerminator(B);
461
462 // If there is no target for the break, then we are looking at an incomplete
463 // AST. This means that the CFG cannot be constructed.
464 if (BreakTargetBlock)
465 Block->addSuccessor(BreakTargetBlock);
466 else
467 badCFG = true;
468
469
Ted Kremenek9aae5132007-08-23 21:42:29 +0000470 return Block;
471}
Ted Kremenek93668002009-07-17 22:18:43 +0000472
473CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
474 // If this is a call to a no-return function, this stops the block here.
475 if (FunctionDecl *FD = C->getDirectCallee()) {
476
477 if (!FD->hasAttr<NoReturnAttr>())
478 return VisitStmt(C, alwaysAdd);
479
480 if (Block && !FinishBlock(Block))
481 return 0;
482
483 // Create new block with no successor for the remaining pieces.
484 Block = createBlock(false);
485 Block->appendStmt(C);
486
487 // Wire this to the exit block directly.
488 Block->addSuccessor(&cfg->getExit());
489
490 return VisitChildren(C);
491 }
492
493 return VisitStmt(C, alwaysAdd);
494}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000495
Ted Kremenek21822592009-07-17 18:20:32 +0000496CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
497 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
498 ConfluenceBlock->appendStmt(C);
499 if (!FinishBlock(ConfluenceBlock))
500 return 0;
501
502 Succ = ConfluenceBlock;
503 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000504 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000505 if (!FinishBlock(LHSBlock))
506 return 0;
507
508 Succ = ConfluenceBlock;
509 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000510 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek21822592009-07-17 18:20:32 +0000511 if (!FinishBlock(RHSBlock))
512 return 0;
513
514 Block = createBlock(false);
515 Block->addSuccessor(LHSBlock);
516 Block->addSuccessor(RHSBlock);
517 Block->setTerminator(C);
518 return addStmt(C->getCond());
519}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000520
Ted Kremenek93668002009-07-17 22:18:43 +0000521
522CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
523 CFGBlock* LastBlock = Block;
524
525 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
526 I != E; ++I ) {
527 LastBlock = addStmt(*I);
528 }
529 return LastBlock;
530}
531
Ted Kremenek51d40b02009-07-17 18:15:54 +0000532CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
533 // Create the confluence block that will "merge" the results of the ternary
534 // expression.
535 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
536 ConfluenceBlock->appendStmt(C);
537 if (!FinishBlock(ConfluenceBlock))
538 return 0;
539
540 // Create a block for the LHS expression if there is an LHS expression. A
541 // GCC extension allows LHS to be NULL, causing the condition to be the
542 // value that is returned instead.
543 // e.g: x ?: y is shorthand for: x ? x : y;
544 Succ = ConfluenceBlock;
545 Block = NULL;
546 CFGBlock* LHSBlock = NULL;
547 if (C->getLHS()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000548 LHSBlock = addStmt(C->getLHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000549 if (!FinishBlock(LHSBlock))
550 return 0;
551 Block = NULL;
552 }
553
554 // Create the block for the RHS expression.
555 Succ = ConfluenceBlock;
Ted Kremenek93668002009-07-17 22:18:43 +0000556 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek51d40b02009-07-17 18:15:54 +0000557 if (!FinishBlock(RHSBlock))
558 return 0;
559
560 // Create the block that will contain the condition.
561 Block = createBlock(false);
562
563 if (LHSBlock)
564 Block->addSuccessor(LHSBlock);
565 else {
566 // If we have no LHS expression, add the ConfluenceBlock as a direct
567 // successor for the block containing the condition. Moreover, we need to
568 // reverse the order of the predecessors in the ConfluenceBlock because
569 // the RHSBlock will have been added to the succcessors already, and we
570 // want the first predecessor to the the block containing the expression
571 // for the case when the ternary expression evaluates to true.
572 Block->addSuccessor(ConfluenceBlock);
573 assert (ConfluenceBlock->pred_size() == 2);
574 std::reverse(ConfluenceBlock->pred_begin(),
575 ConfluenceBlock->pred_end());
576 }
577
578 Block->addSuccessor(RHSBlock);
579
580 Block->setTerminator(C);
581 return addStmt(C->getCond());
582}
583
Ted Kremenek93668002009-07-17 22:18:43 +0000584CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
585 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000586
Ted Kremenek93668002009-07-17 22:18:43 +0000587 if (DS->isSingleDecl()) {
588 Block->appendStmt(DS);
589 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000590 }
Ted Kremenek93668002009-07-17 22:18:43 +0000591
592 CFGBlock *B = 0;
593
594 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
595 typedef llvm::SmallVector<Decl*,10> BufTy;
596 BufTy Buf(DS->decl_begin(), DS->decl_end());
597
598 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
599 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
600 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
601 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
602
603 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
604 // automatically freed with the CFG.
605 DeclGroupRef DG(*I);
606 Decl *D = *I;
607 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
608 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
609
610 // Append the fake DeclStmt to block.
611 Block->appendStmt(DSNew);
612 B = VisitDeclSubExpr(D);
613 }
614
615 return B;
616}
617
618/// VisitDeclSubExpr - Utility method to add block-level expressions for
619/// initializers in Decls.
620CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
621 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000622
Ted Kremenek93668002009-07-17 22:18:43 +0000623 VarDecl *VD = dyn_cast<VarDecl>(D);
624
625 if (!VD)
626 return Block;
627
628 Expr *Init = VD->getInit();
629
630 if (Init) {
631 // Optimization: Don't create separate block-level statements for literals.
632 switch (Init->getStmtClass()) {
633 case Stmt::IntegerLiteralClass:
634 case Stmt::CharacterLiteralClass:
635 case Stmt::StringLiteralClass:
636 break;
637 default:
638 Block = addStmt(Init);
639 }
640 }
641
642 // If the type of VD is a VLA, then we must process its size expressions.
643 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
644 VA = FindVA(VA->getElementType().getTypePtr()))
645 Block = addStmt(VA->getSizeExpr());
646
647 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000648}
649
650CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000651 // We may see an if statement in the middle of a basic block, or it may be the
652 // first statement we are processing. In either case, we create a new basic
653 // block. First, we create the blocks for the then...else statements, and
654 // then we create the block containing the if statement. If we were in the
655 // middle of a block, we stop processing that block and reverse its
656 // statements. That block is then the implicit successor for the "then" and
657 // "else" clauses.
658
659 // The block we were proccessing is now finished. Make it the successor
660 // block.
661 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000662 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000663 if (!FinishBlock(Block))
664 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000665 }
Mike Stump31feda52009-07-17 01:31:16 +0000666
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000667 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000668 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000669
Ted Kremenek9aae5132007-08-23 21:42:29 +0000670 if (Stmt* Else = I->getElse()) {
671 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000672
Ted Kremenek9aae5132007-08-23 21:42:29 +0000673 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000674 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000675 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000676 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000677
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000678 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
679 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000680 else if (Block) {
681 if (!FinishBlock(ElseBlock))
682 return 0;
683 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000684 }
Mike Stump31feda52009-07-17 01:31:16 +0000685
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000686 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000687 CFGBlock* ThenBlock;
688 {
689 Stmt* Then = I->getThen();
690 assert (Then);
691 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000692 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000693 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000694
Ted Kremenek1b379512009-04-01 03:52:47 +0000695 if (!ThenBlock) {
696 // We can reach here if the "then" body has all NullStmts.
697 // Create an empty block so we can distinguish between true and false
698 // branches in path-sensitive analyses.
699 ThenBlock = createBlock(false);
700 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000701 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000702 if (!FinishBlock(ThenBlock))
703 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000704 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705 }
706
Mike Stump31feda52009-07-17 01:31:16 +0000707 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000708 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000709
Ted Kremenek9aae5132007-08-23 21:42:29 +0000710 // Set the terminator of the new block to the If statement.
711 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000712
Ted Kremenek9aae5132007-08-23 21:42:29 +0000713 // Now add the successors.
714 Block->addSuccessor(ThenBlock);
715 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000716
717 // Add the condition as the last statement in the new block. This may create
718 // new blocks as the condition may contain control-flow. Any newly created
719 // blocks will be pointed to be "Block".
Ted Kremenek93668002009-07-17 22:18:43 +0000720 return addStmt(I->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000721}
Mike Stump31feda52009-07-17 01:31:16 +0000722
723
Ted Kremenek9aae5132007-08-23 21:42:29 +0000724CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000725 // If we were in the middle of a block we stop processing that block and
726 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000727 //
Mike Stump31feda52009-07-17 01:31:16 +0000728 // NOTE: If a "return" appears in the middle of a block, this means that the
729 // code afterwards is DEAD (unreachable). We still keep a basic block
730 // for that code; a simple "mark-and-sweep" from the entry block will be
731 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000732 if (Block) FinishBlock(Block);
733
734 // Create the new block.
735 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000736
Ted Kremenek9aae5132007-08-23 21:42:29 +0000737 // The Exit block is the only successor.
738 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000739
740 // Add the return statement to the block. This may create new blocks if R
741 // contains control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +0000742 return VisitStmt(R, true);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000743}
744
745CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
746 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000747 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000748 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000749
Ted Kremenek93668002009-07-17 22:18:43 +0000750 if (!LabelBlock) // This can happen when the body is empty, i.e.
751 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000752
Ted Kremenek93668002009-07-17 22:18:43 +0000753 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000754 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000755
756 // Labels partition blocks, so this is the end of the basic block we were
757 // processing (L is the block's label). Because this is label (and we have
758 // already processed the substatement) there is no extra control-flow to worry
759 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000760 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000761 if (!FinishBlock(LabelBlock))
762 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000763
764 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000765 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000766
Ted Kremenek9aae5132007-08-23 21:42:29 +0000767 // This block is now the implicit successor of other blocks.
768 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000769
Ted Kremenek9aae5132007-08-23 21:42:29 +0000770 return LabelBlock;
771}
772
773CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000774 // Goto is a control-flow statement. Thus we stop processing the current
775 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000776 if (Block)
777 FinishBlock(Block);
778
Ted Kremenek9aae5132007-08-23 21:42:29 +0000779 Block = createBlock(false);
780 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000781
782 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000783 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000784
Ted Kremenek9aae5132007-08-23 21:42:29 +0000785 if (I == LabelMap.end())
786 // We will need to backpatch this block later.
787 BackpatchBlocks.push_back(Block);
788 else
789 Block->addSuccessor(I->second);
790
Mike Stump31feda52009-07-17 01:31:16 +0000791 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000792}
793
794CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump31feda52009-07-17 01:31:16 +0000795 // "for" is a control-flow statement. Thus we stop processing the current
796 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000797 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000798
Ted Kremenek9aae5132007-08-23 21:42:29 +0000799 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000800 if (!FinishBlock(Block))
801 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000802 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000803 } else
804 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000805
806 // Because of short-circuit evaluation, the condition of the loop can span
807 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
808 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000809 CFGBlock* ExitConditionBlock = createBlock(false);
810 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000811
Ted Kremenek81e14852007-08-27 19:46:09 +0000812 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000813 ExitConditionBlock->setTerminator(F);
814
815 // Now add the actual condition to the condition block. Because the condition
816 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000817 if (Stmt* C = F->getCond()) {
818 Block = ExitConditionBlock;
819 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000820 if (Block) {
821 if (!FinishBlock(EntryConditionBlock))
822 return 0;
823 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000824 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000825
Mike Stump31feda52009-07-17 01:31:16 +0000826 // The condition block is the implicit successor for the loop body as well as
827 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000828 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000829
Ted Kremenek9aae5132007-08-23 21:42:29 +0000830 // Now create the loop body.
831 {
832 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000833
Ted Kremenek9aae5132007-08-23 21:42:29 +0000834 // Save the current values for Block, Succ, and continue and break targets
835 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
836 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000837 save_break(BreakTargetBlock);
838
Ted Kremeneke9610502007-08-30 18:39:40 +0000839 // Create a new block to contain the (bottom) of the loop body.
840 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000841
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000842 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000843 // Generate increment code in its own basic block. This is the target of
844 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +0000845 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000846 } else {
847 // No increment code. Create a special, empty, block that is used as the
848 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000849 assert(Succ == EntryConditionBlock);
850 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000851 }
Mike Stump31feda52009-07-17 01:31:16 +0000852
Ted Kremenek902393b2009-04-28 00:51:56 +0000853 // Finish up the increment (or empty) block if it hasn't been already.
854 if (Block) {
855 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000856 if (!FinishBlock(Block))
857 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000858 Block = 0;
859 }
Mike Stump31feda52009-07-17 01:31:16 +0000860
Ted Kremenek902393b2009-04-28 00:51:56 +0000861 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000862
Ted Kremenek902393b2009-04-28 00:51:56 +0000863 // The starting block for the loop increment is the block that should
864 // represent the 'loop target' for looping back to the start of the loop.
865 ContinueTargetBlock->setLoopTarget(F);
866
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000867 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000868 BreakTargetBlock = LoopSuccessor;
869
870 // Now populate the body block, and in the process create new blocks as we
871 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +0000872 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000873
874 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000875 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000876 else if (Block) {
877 if (!FinishBlock(BodyBlock))
878 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000879 }
880
Ted Kremenek81e14852007-08-27 19:46:09 +0000881 // This new body block is a successor to our "exit" condition block.
882 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000883 }
Mike Stump31feda52009-07-17 01:31:16 +0000884
885 // Link up the condition block with the code that follows the loop. (the
886 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +0000887 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000888
Ted Kremenek9aae5132007-08-23 21:42:29 +0000889 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000890 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000891 if (Stmt* I = F->getInit()) {
892 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000893 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000894 } else {
895 // There is no loop initialization. We are thus basically a while loop.
896 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000897 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000898 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000899 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000900 }
901}
902
Ted Kremenek9d56e642008-11-11 17:10:00 +0000903CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
904 // Objective-C fast enumeration 'for' statements:
905 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
906 //
907 // for ( Type newVariable in collection_expression ) { statements }
908 //
909 // becomes:
910 //
911 // prologue:
912 // 1. collection_expression
913 // T. jump to loop_entry
914 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000915 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000916 // 1. ObjCForCollectionStmt [performs binding to newVariable]
917 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
918 // TB:
919 // statements
920 // T. jump to loop_entry
921 // FB:
922 // what comes after
923 //
924 // and
925 //
926 // Type existingItem;
927 // for ( existingItem in expression ) { statements }
928 //
929 // becomes:
930 //
Mike Stump31feda52009-07-17 01:31:16 +0000931 // the same with newVariable replaced with existingItem; the binding works
932 // the same except that for one ObjCForCollectionStmt::getElement() returns
933 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000934 //
Mike Stump31feda52009-07-17 01:31:16 +0000935
Ted Kremenek9d56e642008-11-11 17:10:00 +0000936 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000937
Ted Kremenek9d56e642008-11-11 17:10:00 +0000938 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000939 if (!FinishBlock(Block))
940 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000941 LoopSuccessor = Block;
942 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +0000943 } else
944 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000945
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000946 // Build the condition blocks.
947 CFGBlock* ExitConditionBlock = createBlock(false);
948 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000949
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000950 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000951 ExitConditionBlock->setTerminator(S);
952
953 // The last statement in the block should be the ObjCForCollectionStmt, which
954 // performs the actual binding to 'element' and determines if there are any
955 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000956 ExitConditionBlock->appendStmt(S);
957 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000959 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +0000960 // generate new blocks as necesary. We DON'T add the statement by default to
961 // the CFG unless it contains control-flow.
Ted Kremenek93668002009-07-17 22:18:43 +0000962 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +0000963 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000964 if (!FinishBlock(EntryConditionBlock))
965 return 0;
966 Block = 0;
967 }
Mike Stump31feda52009-07-17 01:31:16 +0000968
969 // The condition block is the implicit successor for the loop body as well as
970 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000971 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000972
Ted Kremenek9d56e642008-11-11 17:10:00 +0000973 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +0000974 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000975 // Save the current values for Succ, continue and break targets.
976 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +0000977 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
978
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000979 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +0000980 ContinueTargetBlock = EntryConditionBlock;
981
Ted Kremenek93668002009-07-17 22:18:43 +0000982 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000983
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000984 if (!BodyBlock)
985 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000986 else if (Block) {
987 if (!FinishBlock(BodyBlock))
988 return 0;
989 }
Mike Stump31feda52009-07-17 01:31:16 +0000990
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000991 // This new body block is a successor to our "exit" condition block.
992 ExitConditionBlock->addSuccessor(BodyBlock);
993 }
Mike Stump31feda52009-07-17 01:31:16 +0000994
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000995 // Link up the condition block with the code that follows the loop.
996 // (the false branch).
997 ExitConditionBlock->addSuccessor(LoopSuccessor);
998
Ted Kremenek9d56e642008-11-11 17:10:00 +0000999 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001000 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001001 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001002}
1003
Ted Kremenek49805452009-05-02 01:49:13 +00001004CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1005 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001006
Ted Kremenek49805452009-05-02 01:49:13 +00001007 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001008 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001009
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001010 // The sync body starts its own basic block. This makes it a little easier
1011 // for diagnostic clients.
1012 if (SyncBlock) {
1013 if (!FinishBlock(SyncBlock))
1014 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001015
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001016 Block = 0;
1017 }
Mike Stump31feda52009-07-17 01:31:16 +00001018
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001019 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001020
Ted Kremenek49805452009-05-02 01:49:13 +00001021 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001022 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001023}
Mike Stump31feda52009-07-17 01:31:16 +00001024
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001025CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001026 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001027 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001028}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001029
Ted Kremenek9aae5132007-08-23 21:42:29 +00001030CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump31feda52009-07-17 01:31:16 +00001031 // "while" is a control-flow statement. Thus we stop processing the current
1032 // block.
1033
Ted Kremenek9aae5132007-08-23 21:42:29 +00001034 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001035
Ted Kremenek9aae5132007-08-23 21:42:29 +00001036 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001037 if (!FinishBlock(Block))
1038 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001039 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001040 } else
1041 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001042
1043 // Because of short-circuit evaluation, the condition of the loop can span
1044 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1045 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001046 CFGBlock* ExitConditionBlock = createBlock(false);
1047 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001048
Ted Kremenek81e14852007-08-27 19:46:09 +00001049 // Set the terminator for the "exit" condition block.
1050 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001051
1052 // Now add the actual condition to the condition block. Because the condition
1053 // itself may contain control-flow, new blocks may be created. Thus we update
1054 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001055 if (Stmt* C = W->getCond()) {
1056 Block = ExitConditionBlock;
1057 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001058 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +00001059 if (Block) {
1060 if (!FinishBlock(EntryConditionBlock))
1061 return 0;
1062 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001063 }
Mike Stump31feda52009-07-17 01:31:16 +00001064
1065 // The condition block is the implicit successor for the loop body as well as
1066 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001067 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001068
Ted Kremenek9aae5132007-08-23 21:42:29 +00001069 // Process the loop body.
1070 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001071 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001072
1073 // Save the current values for Block, Succ, and continue and break targets
1074 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1075 save_continue(ContinueTargetBlock),
1076 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001077
Mike Stump31feda52009-07-17 01:31:16 +00001078 // Create an empty block to represent the transition block for looping back
1079 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001080 Block = 0;
1081 assert(Succ == EntryConditionBlock);
1082 Succ = createBlock();
1083 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001084 ContinueTargetBlock = Succ;
1085
Ted Kremenek9aae5132007-08-23 21:42:29 +00001086 // All breaks should go to the code following the loop.
1087 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001088
Ted Kremenek9aae5132007-08-23 21:42:29 +00001089 // NULL out Block to force lazy instantiation of blocks for the body.
1090 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001091
Ted Kremenek9aae5132007-08-23 21:42:29 +00001092 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001093 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001094
Ted Kremeneke9610502007-08-30 18:39:40 +00001095 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001096 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001097 else if (Block) {
1098 if (!FinishBlock(BodyBlock))
1099 return 0;
1100 }
Mike Stump31feda52009-07-17 01:31:16 +00001101
Ted Kremenek9aae5132007-08-23 21:42:29 +00001102 // Add the loop body entry as a successor to the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001103 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001104 }
Mike Stump31feda52009-07-17 01:31:16 +00001105
1106 // Link up the condition block with the code that follows the loop. (the
1107 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001108 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001109
1110 // There can be no more statements in the condition block since we loop back
1111 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001112 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001113
Ted Kremenek9aae5132007-08-23 21:42:29 +00001114 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001115 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001116 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001117}
Ted Kremenek93668002009-07-17 22:18:43 +00001118
1119
1120CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1121 // FIXME: For now we pretend that @catch and the code it contains does not
1122 // exit.
1123 return Block;
1124}
Mike Stump31feda52009-07-17 01:31:16 +00001125
Ted Kremenek93041ba2008-12-09 20:20:09 +00001126CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1127 // FIXME: This isn't complete. We basically treat @throw like a return
1128 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001129
1130 // If we were in the middle of a block we stop processing that block and
1131 // reverse its statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001132 if (Block && !FinishBlock(Block))
1133 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001134
Ted Kremenek93041ba2008-12-09 20:20:09 +00001135 // Create the new block.
1136 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001137
Ted Kremenek93041ba2008-12-09 20:20:09 +00001138 // The Exit block is the only successor.
1139 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001140
1141 // Add the statement to the block. This may create new blocks if S contains
1142 // control-flow (short-circuit operations).
Ted Kremenek93668002009-07-17 22:18:43 +00001143 return VisitStmt(S, true);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001144}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001145
Ted Kremenek93668002009-07-17 22:18:43 +00001146CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001147 // "do...while" is a control-flow statement. Thus we stop processing the
1148 // current block.
Mike Stump31feda52009-07-17 01:31:16 +00001149
Ted Kremenek9aae5132007-08-23 21:42:29 +00001150 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001151
Ted Kremenek9aae5132007-08-23 21:42:29 +00001152 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001153 if (!FinishBlock(Block))
1154 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001155 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001156 } else
1157 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001158
1159 // Because of short-circuit evaluation, the condition of the loop can span
1160 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1161 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001162 CFGBlock* ExitConditionBlock = createBlock(false);
1163 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001164
Ted Kremenek81e14852007-08-27 19:46:09 +00001165 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001166 ExitConditionBlock->setTerminator(D);
1167
1168 // Now add the actual condition to the condition block. Because the condition
1169 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001170 if (Stmt* C = D->getCond()) {
1171 Block = ExitConditionBlock;
1172 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001173 if (Block) {
1174 if (!FinishBlock(EntryConditionBlock))
1175 return 0;
1176 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001177 }
Mike Stump31feda52009-07-17 01:31:16 +00001178
Ted Kremeneka1523a32008-02-27 07:20:00 +00001179 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001180 Succ = EntryConditionBlock;
1181
Ted Kremenek9aae5132007-08-23 21:42:29 +00001182 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001183 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001184 {
1185 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001186
Ted Kremenek9aae5132007-08-23 21:42:29 +00001187 // Save the current values for Block, Succ, and continue and break targets
1188 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1189 save_continue(ContinueTargetBlock),
1190 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001191
Ted Kremenek9aae5132007-08-23 21:42:29 +00001192 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001193 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001194
Ted Kremenek9aae5132007-08-23 21:42:29 +00001195 // All breaks should go to the code following the loop.
1196 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001197
Ted Kremenek9aae5132007-08-23 21:42:29 +00001198 // NULL out Block to force lazy instantiation of blocks for the body.
1199 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001200
Ted Kremenek9aae5132007-08-23 21:42:29 +00001201 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001202 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001203
Ted Kremeneke9610502007-08-30 18:39:40 +00001204 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001205 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001206 else if (Block) {
1207 if (!FinishBlock(BodyBlock))
1208 return 0;
1209 }
Mike Stump31feda52009-07-17 01:31:16 +00001210
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001211 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001212 // ExitConditionBlock to represent the "loop back" transition. Create an
1213 // empty block to represent the transition block for looping back to the
1214 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001215 // FIXME: Can we do this more efficiently without adding another block?
1216 Block = NULL;
1217 Succ = BodyBlock;
1218 CFGBlock *LoopBackBlock = createBlock();
1219 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001220
Ted Kremenek9aae5132007-08-23 21:42:29 +00001221 // Add the loop body entry as a successor to the condition.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001222 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001223 }
Mike Stump31feda52009-07-17 01:31:16 +00001224
Ted Kremenek9aae5132007-08-23 21:42:29 +00001225 // Link up the condition block with the code that follows the loop.
1226 // (the false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001227 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001228
1229 // There can be no more statements in the body block(s) since we loop back to
1230 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001231 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001232
Ted Kremenek9aae5132007-08-23 21:42:29 +00001233 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001234 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001235 return BodyBlock;
1236}
1237
1238CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1239 // "continue" is a control-flow statement. Thus we stop processing the
1240 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001241 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001242 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001243
Ted Kremenek9aae5132007-08-23 21:42:29 +00001244 // Now create a new block that ends with the continue statement.
1245 Block = createBlock(false);
1246 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001247
Ted Kremenek9aae5132007-08-23 21:42:29 +00001248 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001249 // incomplete AST. This means the CFG cannot be constructed.
1250 if (ContinueTargetBlock)
1251 Block->addSuccessor(ContinueTargetBlock);
1252 else
1253 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001254
Ted Kremenek9aae5132007-08-23 21:42:29 +00001255 return Block;
1256}
Ted Kremenek93668002009-07-17 22:18:43 +00001257
Ted Kremenek0747de62009-07-18 00:47:21 +00001258CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1259 bool alwaysAdd) {
1260
1261 if (alwaysAdd) {
1262 autoCreateBlock();
1263 Block->appendStmt(E);
1264 }
1265
Ted Kremenek93668002009-07-17 22:18:43 +00001266 // VLA types have expressions that must be evaluated.
1267 if (E->isArgumentType()) {
1268 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1269 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1270 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001271 }
Ted Kremenek93668002009-07-17 22:18:43 +00001272
Mike Stump31feda52009-07-17 01:31:16 +00001273 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001274}
Ted Kremenek93668002009-07-17 22:18:43 +00001275
1276/// VisitStmtExpr - Utility method to handle (nested) statement
1277/// expressions (a GCC extension).
Ted Kremenek0747de62009-07-18 00:47:21 +00001278CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1279 if (alwaysAdd) {
1280 autoCreateBlock();
1281 Block->appendStmt(SE);
1282 }
Ted Kremenek93668002009-07-17 22:18:43 +00001283 return VisitCompoundStmt(SE->getSubStmt());
1284}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001285
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001286CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001287 // "switch" is a control-flow statement. Thus we stop processing the current
1288 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001289 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001290
Ted Kremenek9aae5132007-08-23 21:42:29 +00001291 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001292 if (!FinishBlock(Block))
1293 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001294 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001295 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001296
1297 // Save the current "switch" context.
1298 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001299 save_break(BreakTargetBlock),
1300 save_default(DefaultCaseBlock);
1301
Mike Stump31feda52009-07-17 01:31:16 +00001302 // Set the "default" case to be the block after the switch statement. If the
1303 // switch statement contains a "default:", this value will be overwritten with
1304 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001305 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001306
Ted Kremenek9aae5132007-08-23 21:42:29 +00001307 // Create a new block that will contain the switch statement.
1308 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001309
Ted Kremenek9aae5132007-08-23 21:42:29 +00001310 // Now process the switch body. The code after the switch is the implicit
1311 // successor.
1312 Succ = SwitchSuccessor;
1313 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001314
1315 // When visiting the body, the case statements should automatically get linked
1316 // up to the switch. We also don't keep a pointer to the body, since all
1317 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001318 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001319 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001320 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001321 if (Block) {
1322 if (!FinishBlock(BodyBlock))
1323 return 0;
1324 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001325
Mike Stump31feda52009-07-17 01:31:16 +00001326 // If we have no "default:" case, the default transition is to the code
1327 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001328 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001329
Ted Kremenek81e14852007-08-27 19:46:09 +00001330 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001331 SwitchTerminatedBlock->setTerminator(Terminator);
1332 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001333 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001334
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001335 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001336}
1337
Ted Kremenek93668002009-07-17 22:18:43 +00001338CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001339 // CaseStmts are essentially labels, so they are the first statement in a
1340 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001341
Ted Kremenek93668002009-07-17 22:18:43 +00001342 if (CS->getSubStmt())
1343 addStmt(CS->getSubStmt());
1344
Ted Kremenek55e91e82007-08-30 18:48:11 +00001345 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001346 if (!CaseBlock)
1347 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001348
1349 // Cases statements partition blocks, so this is the top of the basic block we
1350 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001351 CaseBlock->setLabel(CS);
1352
Ted Kremenek55957a82009-05-02 00:13:27 +00001353 if (!FinishBlock(CaseBlock))
1354 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001355
1356 // Add this block to the list of successors for the block with the switch
1357 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001358 assert(SwitchTerminatedBlock);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001359 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001360
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1362 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001363
Ted Kremenek9aae5132007-08-23 21:42:29 +00001364 // This block is now the implicit successor of other blocks.
1365 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001366
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001367 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001368}
Mike Stump31feda52009-07-17 01:31:16 +00001369
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001370CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001371 if (Terminator->getSubStmt())
1372 addStmt(Terminator->getSubStmt());
1373
Ted Kremenek654c78f2008-02-13 22:05:39 +00001374 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001375
1376 if (!DefaultCaseBlock)
1377 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001378
1379 // Default statements partition blocks, so this is the top of the basic block
1380 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001381 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek93668002009-07-17 22:18:43 +00001382
Ted Kremenek55957a82009-05-02 00:13:27 +00001383 if (!FinishBlock(DefaultCaseBlock))
1384 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001385
Mike Stump31feda52009-07-17 01:31:16 +00001386 // Unlike case statements, we don't add the default block to the successors
1387 // for the switch statement immediately. This is done when we finish
1388 // processing the switch statement. This allows for the default case
1389 // (including a fall-through to the code after the switch statement) to always
1390 // be the last successor of a switch-terminated block.
1391
Ted Kremenek654c78f2008-02-13 22:05:39 +00001392 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1393 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001394
Ted Kremenek654c78f2008-02-13 22:05:39 +00001395 // This block is now the implicit successor of other blocks.
1396 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001397
1398 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001399}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001400
Ted Kremenekeda180e22007-08-28 19:26:49 +00001401CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001402 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001403 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001404
Ted Kremenekeda180e22007-08-28 19:26:49 +00001405 if (!IBlock) {
1406 IBlock = createBlock(false);
1407 cfg->setIndirectGotoBlock(IBlock);
1408 }
Mike Stump31feda52009-07-17 01:31:16 +00001409
Ted Kremenekeda180e22007-08-28 19:26:49 +00001410 // IndirectGoto is a control-flow statement. Thus we stop processing the
1411 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001412 if (Block && !FinishBlock(Block))
1413 return 0;
1414
Ted Kremenekeda180e22007-08-28 19:26:49 +00001415 Block = createBlock(false);
1416 Block->setTerminator(I);
1417 Block->addSuccessor(IBlock);
1418 return addStmt(I->getTarget());
1419}
1420
Ted Kremenek04cca642007-08-23 21:26:19 +00001421} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001422
Mike Stump31feda52009-07-17 01:31:16 +00001423/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1424/// no successors or predecessors. If this is the first block created in the
1425/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001426CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001427 bool first_block = begin() == end();
1428
1429 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001430 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001431
1432 // If this is the first block, set it as the Entry and Exit.
1433 if (first_block) Entry = Exit = &front();
1434
1435 // Return the block.
1436 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001437}
1438
Ted Kremenek889073f2007-08-23 16:51:22 +00001439/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1440/// CFG is returned to the caller.
1441CFG* CFG::buildCFG(Stmt* Statement) {
1442 CFGBuilder Builder;
1443 return Builder.buildCFG(Statement);
1444}
1445
1446/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001447void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1448
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001449//===----------------------------------------------------------------------===//
1450// CFG: Queries for BlkExprs.
1451//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001452
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001453namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001454 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001455}
1456
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001457static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1458 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001459 return;
Mike Stump31feda52009-07-17 01:31:16 +00001460
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001461 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001462 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001463
Ted Kremenek95a123c2008-01-26 00:03:27 +00001464 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1465 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001466
Ted Kremenek95a123c2008-01-26 00:03:27 +00001467 FindSubExprAssignments(*I, Set);
1468 }
1469}
1470
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001471static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1472 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001473
1474 // Look for assignments that are used as subexpressions. These are the only
1475 // assignments that we want to *possibly* register as a block-level
1476 // expression. Basically, if an assignment occurs both in a subexpression and
1477 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001478 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001479
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001480 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1481 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001482 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001483
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001484 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001485
1486 // Iterate over the statements again on identify the Expr* and Stmt* at the
1487 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001488
Ted Kremenek95a123c2008-01-26 00:03:27 +00001489 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001490 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001492 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001493 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001494 // expression are really "statements" whose value is never used by
1495 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001496 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001497 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001498 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1499 // Special handling for statement expressions. The last statement in
1500 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001501 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001502 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001503 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001504 (*M)[C->body_back()] = x;
1505 }
1506 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001507
Ted Kremenek95a123c2008-01-26 00:03:27 +00001508 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001509 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001510 }
Mike Stump31feda52009-07-17 01:31:16 +00001511
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001512 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001513
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001514 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001515
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001516 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001517 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001518 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001519 }
1520 }
Mike Stump31feda52009-07-17 01:31:16 +00001521
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001522 return M;
1523}
1524
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001525CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1526 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001527 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001528
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001529 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001530 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001531
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001532 if (I == M->end()) return CFG::BlkExprNumTy();
1533 else return CFG::BlkExprNumTy(I->second);
1534}
1535
1536unsigned CFG::getNumBlkExprs() {
1537 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1538 return M->size();
1539 else {
1540 // We assume callers interested in the number of BlkExprs will want
1541 // the map constructed if it doesn't already exist.
1542 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1543 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1544 }
1545}
1546
Ted Kremenek6065ef62008-04-28 18:00:46 +00001547//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001548// Cleanup: CFG dstor.
1549//===----------------------------------------------------------------------===//
1550
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001551CFG::~CFG() {
1552 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1553}
Mike Stump31feda52009-07-17 01:31:16 +00001554
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001555//===----------------------------------------------------------------------===//
1556// CFG pretty printing
1557//===----------------------------------------------------------------------===//
1558
Ted Kremenek7e776b12007-08-22 18:22:34 +00001559namespace {
1560
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001561class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001562
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001563 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1564 StmtMapTy StmtMap;
1565 signed CurrentBlock;
1566 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001567 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001568public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001569
Chris Lattnerc61089a2009-06-30 01:26:17 +00001570 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1571 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001572 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1573 unsigned j = 1;
1574 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1575 BI != BEnd; ++BI, ++j )
1576 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1577 }
1578 }
Mike Stump31feda52009-07-17 01:31:16 +00001579
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001580 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001581
Chris Lattnerc61089a2009-06-30 01:26:17 +00001582 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001583 void setBlockID(signed i) { CurrentBlock = i; }
1584 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001585
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001586 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001587
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001588 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001589
1590 if (I == StmtMap.end())
1591 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001592
1593 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001594 && I->second.second == CurrentStmt)
1595 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001596
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001597 OS << "[B" << I->second.first << "." << I->second.second << "]";
1598 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001599 }
1600};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001601} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001602
Chris Lattnerc61089a2009-06-30 01:26:17 +00001603
1604namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001605class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1606 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001607
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001608 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001609 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001610 PrintingPolicy Policy;
1611
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001612public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001613 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001614 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001615 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001616
Ted Kremenek9aae5132007-08-23 21:42:29 +00001617 void VisitIfStmt(IfStmt* I) {
1618 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001619 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001620 }
Mike Stump31feda52009-07-17 01:31:16 +00001621
Ted Kremenek9aae5132007-08-23 21:42:29 +00001622 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001623 void VisitStmt(Stmt* Terminator) {
1624 Terminator->printPretty(OS, Helper, Policy);
1625 }
1626
Ted Kremenek9aae5132007-08-23 21:42:29 +00001627 void VisitForStmt(ForStmt* F) {
1628 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001629 if (F->getInit()) OS << "...";
1630 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001631 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001632 OS << "; ";
1633 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001634 OS << ")";
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 VisitWhileStmt(WhileStmt* W) {
1638 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001639 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001640 }
Mike Stump31feda52009-07-17 01:31:16 +00001641
Ted Kremenek9aae5132007-08-23 21:42:29 +00001642 void VisitDoStmt(DoStmt* D) {
1643 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001644 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001645 }
Mike Stump31feda52009-07-17 01:31:16 +00001646
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001647 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001648 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001649 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001650 }
Mike Stump31feda52009-07-17 01:31:16 +00001651
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001652 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001653 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001654 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001655 }
Mike Stump31feda52009-07-17 01:31:16 +00001656
Ted Kremenek391f94a2007-08-31 22:29:13 +00001657 void VisitChooseExpr(ChooseExpr* C) {
1658 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001659 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001660 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001661 }
Mike Stump31feda52009-07-17 01:31:16 +00001662
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001663 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1664 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001665 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001666 }
Mike Stump31feda52009-07-17 01:31:16 +00001667
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001668 void VisitBinaryOperator(BinaryOperator* B) {
1669 if (!B->isLogicalOp()) {
1670 VisitExpr(B);
1671 return;
1672 }
Mike Stump31feda52009-07-17 01:31:16 +00001673
Douglas Gregor7de59662009-05-29 20:38:28 +00001674 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001675
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001676 switch (B->getOpcode()) {
1677 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001678 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001679 return;
1680 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001681 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001682 return;
1683 default:
1684 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001685 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001686 }
Mike Stump31feda52009-07-17 01:31:16 +00001687
Ted Kremenek12687ff2007-08-27 21:54:41 +00001688 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001689 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001690 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001691};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001692} // end anonymous namespace
1693
Mike Stump31feda52009-07-17 01:31:16 +00001694
Chris Lattnerc61089a2009-06-30 01:26:17 +00001695static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1696 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001697 if (Helper) {
1698 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001699 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001700 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001701
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001702 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001703 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001704 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001705 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001706 return;
1707 }
1708 }
Mike Stump31feda52009-07-17 01:31:16 +00001709
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001710 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001711 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001712 if (B->getOpcode() == BinaryOperator::Comma) {
1713 OS << "... , ";
1714 Helper->handledStmt(B->getRHS(),OS);
1715 OS << '\n';
1716 return;
Mike Stump31feda52009-07-17 01:31:16 +00001717 }
1718 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001719 }
Mike Stump31feda52009-07-17 01:31:16 +00001720
Chris Lattnerc61089a2009-06-30 01:26:17 +00001721 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001723 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001724 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001725}
Mike Stump31feda52009-07-17 01:31:16 +00001726
Chris Lattnerc61089a2009-06-30 01:26:17 +00001727static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1728 const CFGBlock& B,
1729 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001730
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001731 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001732
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001733 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001734 OS << "\n [ B" << B.getBlockID();
1735
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001736 if (&B == &cfg->getEntry())
1737 OS << " (ENTRY) ]\n";
1738 else if (&B == &cfg->getExit())
1739 OS << " (EXIT) ]\n";
1740 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001741 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001742 else
1743 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001744
Ted Kremenek71eca012007-08-29 23:20:49 +00001745 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001746 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001747
1748 if (print_edges)
1749 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001750
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001751 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001752 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001753 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001754 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001755 C->getLHS()->printPretty(OS, Helper,
1756 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001757 if (C->getRHS()) {
1758 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001759 C->getRHS()->printPretty(OS, Helper,
1760 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001761 }
Mike Stump31feda52009-07-17 01:31:16 +00001762 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001763 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001764 else
1765 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001766
Ted Kremenek71eca012007-08-29 23:20:49 +00001767 OS << ":\n";
1768 }
Mike Stump31feda52009-07-17 01:31:16 +00001769
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001770 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001771 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001772
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001773 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1774 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001775
Ted Kremenek71eca012007-08-29 23:20:49 +00001776 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001777 if (print_edges)
1778 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001779
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001780 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001781
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001782 if (Helper)
1783 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001785 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001786 }
Mike Stump31feda52009-07-17 01:31:16 +00001787
Ted Kremenek71eca012007-08-29 23:20:49 +00001788 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001789 if (B.getTerminator()) {
1790 if (print_edges)
1791 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001792
Ted Kremenek71eca012007-08-29 23:20:49 +00001793 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001794
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001795 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001796
Chris Lattnerc61089a2009-06-30 01:26:17 +00001797 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1798 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001799 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001800 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001801 }
Mike Stump31feda52009-07-17 01:31:16 +00001802
Ted Kremenek71eca012007-08-29 23:20:49 +00001803 if (print_edges) {
1804 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001805 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001806 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001807
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001808 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1809 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001810
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001811 if (i == 8 || (i-8) == 0)
1812 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001813
Ted Kremenek71eca012007-08-29 23:20:49 +00001814 OS << " B" << (*I)->getBlockID();
1815 }
Mike Stump31feda52009-07-17 01:31:16 +00001816
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001817 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001818
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001819 // Print the successors of this block.
1820 OS << " Successors (" << B.succ_size() << "):";
1821 i = 0;
1822
1823 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1824 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001825
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001826 if (i == 8 || (i-8) % 10 == 0)
1827 OS << "\n ";
1828
1829 OS << " B" << (*I)->getBlockID();
1830 }
Mike Stump31feda52009-07-17 01:31:16 +00001831
Ted Kremenek71eca012007-08-29 23:20:49 +00001832 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001833 }
Mike Stump31feda52009-07-17 01:31:16 +00001834}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001835
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001836
1837/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001838void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001839
1840/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001841void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1842 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001843
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001844 // Print the entry block.
1845 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001846
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001847 // Iterate through the CFGBlocks and print them one by one.
1848 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1849 // Skip the entry block, because we already printed it.
1850 if (&(*I) == &getEntry() || &(*I) == &getExit())
1851 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001853 print_block(OS, this, *I, &Helper, true);
1854 }
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001856 // Print the exit block.
1857 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001858 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001859}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001860
1861/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001862void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1863 print(llvm::errs(), cfg, LO);
1864}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001865
1866/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1867/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001868void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1869 const LangOptions &LO) const {
1870 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001871 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001872}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001873
Ted Kremenek15647632008-01-30 23:02:42 +00001874/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001875void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001876 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001877 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001878 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1879}
1880
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001881Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001882
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001883 if (!Terminator)
1884 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001885
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001886 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001887
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001888 switch (Terminator->getStmtClass()) {
1889 default:
1890 break;
Mike Stump31feda52009-07-17 01:31:16 +00001891
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001892 case Stmt::ForStmtClass:
1893 E = cast<ForStmt>(Terminator)->getCond();
1894 break;
Mike Stump31feda52009-07-17 01:31:16 +00001895
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001896 case Stmt::WhileStmtClass:
1897 E = cast<WhileStmt>(Terminator)->getCond();
1898 break;
Mike Stump31feda52009-07-17 01:31:16 +00001899
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001900 case Stmt::DoStmtClass:
1901 E = cast<DoStmt>(Terminator)->getCond();
1902 break;
Mike Stump31feda52009-07-17 01:31:16 +00001903
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001904 case Stmt::IfStmtClass:
1905 E = cast<IfStmt>(Terminator)->getCond();
1906 break;
Mike Stump31feda52009-07-17 01:31:16 +00001907
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001908 case Stmt::ChooseExprClass:
1909 E = cast<ChooseExpr>(Terminator)->getCond();
1910 break;
Mike Stump31feda52009-07-17 01:31:16 +00001911
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001912 case Stmt::IndirectGotoStmtClass:
1913 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1914 break;
Mike Stump31feda52009-07-17 01:31:16 +00001915
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001916 case Stmt::SwitchStmtClass:
1917 E = cast<SwitchStmt>(Terminator)->getCond();
1918 break;
Mike Stump31feda52009-07-17 01:31:16 +00001919
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001920 case Stmt::ConditionalOperatorClass:
1921 E = cast<ConditionalOperator>(Terminator)->getCond();
1922 break;
Mike Stump31feda52009-07-17 01:31:16 +00001923
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001924 case Stmt::BinaryOperatorClass: // '&&' and '||'
1925 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001926 break;
Mike Stump31feda52009-07-17 01:31:16 +00001927
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001928 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00001929 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001930 }
Mike Stump31feda52009-07-17 01:31:16 +00001931
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001932 return E ? E->IgnoreParens() : NULL;
1933}
1934
Ted Kremenek92137a32008-05-16 16:06:00 +00001935bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek92137a32008-05-16 16:06:00 +00001937 if (!Terminator)
1938 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001939
Ted Kremenek92137a32008-05-16 16:06:00 +00001940 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001941
Ted Kremenek92137a32008-05-16 16:06:00 +00001942 switch (Terminator->getStmtClass()) {
1943 default:
1944 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001945
1946 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00001947 case Stmt::WhileStmtClass:
1948 case Stmt::DoStmtClass:
1949 case Stmt::IfStmtClass:
1950 case Stmt::ChooseExprClass:
1951 case Stmt::ConditionalOperatorClass:
1952 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00001953 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00001954 }
Mike Stump31feda52009-07-17 01:31:16 +00001955
Ted Kremenek92137a32008-05-16 16:06:00 +00001956 return E ? E->IgnoreParens() : NULL;
1957}
1958
Ted Kremenek15647632008-01-30 23:02:42 +00001959
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001960//===----------------------------------------------------------------------===//
1961// CFG Graphviz Visualization
1962//===----------------------------------------------------------------------===//
1963
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001964
1965#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00001966static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967#endif
1968
Chris Lattnerc61089a2009-06-30 01:26:17 +00001969void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001970#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00001971 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001972 GraphHelper = &H;
1973 llvm::ViewGraph(this,"CFG");
1974 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001975#endif
1976}
1977
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001978namespace llvm {
1979template<>
1980struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00001981 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1982 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001983
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001984#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001985 std::string OutSStr;
1986 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001987 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001988 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001989
1990 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1991
1992 // Process string output to make it nicer...
1993 for (unsigned i = 0; i != OutStr.length(); ++i)
1994 if (OutStr[i] == '\n') { // Left justify
1995 OutStr[i] = '\\';
1996 OutStr.insert(OutStr.begin()+i+1, 'l');
1997 }
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001999 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002000#else
2001 return "";
2002#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002003 }
2004};
2005} // end namespace llvm