blob: 620a6917521d2fe7ee74d89c1cf9e053a9b147f2 [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///
Mike Stump31feda52009-07-17 01:31:16 +000064class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
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
99 // Visitors to walk an AST and construct the CFG. Called by buildCFG. Do not
100 // call directly!
101
Ted Kremenek21822592009-07-17 18:20:32 +0000102 CFGBlock *VisitBreakStmt(BreakStmt* B);
103 CFGBlock *VisitCaseStmt(CaseStmt* Terminator);
104 CFGBlock *VisitChooseExpr(ChooseExpr *C);
105 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
106 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
107 CFGBlock *VisitContinueStmt(ContinueStmt *C);
108 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
109 CFGBlock *VisitDoStmt(DoStmt *D);
110 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek9d56e642008-11-11 17:10:00 +0000111 CFGBlock* VisitGotoStmt(GotoStmt* G);
112 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000113 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek9d56e642008-11-11 17:10:00 +0000114 CFGBlock* VisitLabelStmt(LabelStmt* L);
115 CFGBlock* VisitNullStmt(NullStmt* Statement);
116 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
117 CFGBlock* VisitReturnStmt(ReturnStmt* R);
118 CFGBlock* VisitStmt(Stmt* Statement);
119 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
120 CFGBlock* VisitWhileStmt(WhileStmt* W);
Mike Stump48871a22009-07-17 01:04:31 +0000121
Ted Kremenekb64d1832008-03-13 03:04:22 +0000122 // FIXME: Add support for ObjC-specific control-flow structures.
Mike Stump48871a22009-07-17 01:04:31 +0000123
Ted Kremenek6065ef62008-04-28 18:00:46 +0000124 // NYS == Not Yet Supported
125 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000126 badCFG = true;
127 return Block;
128 }
Mike Stump31feda52009-07-17 01:31:16 +0000129
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000130 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S);
Mike Stump31feda52009-07-17 01:31:16 +0000131 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
132 // FIXME: For now we pretend that @catch and the code it contains does not
133 // exit.
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000134 return Block;
135 }
136
Mike Stump31feda52009-07-17 01:31:16 +0000137 // FIXME: This is not completely supported. We basically @throw like a
138 // 'return'.
Ted Kremenek93041ba2008-12-09 20:20:09 +0000139 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenek6065ef62008-04-28 18:00:46 +0000140
Ted Kremenek49805452009-05-02 01:49:13 +0000141 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S);
Mike Stump31feda52009-07-17 01:31:16 +0000142
Ted Kremenekac49ea62008-09-26 18:17:07 +0000143 // Blocks.
144 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
Mike Stump31feda52009-07-17 01:31:16 +0000145 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
146
Ted Kremenek9aae5132007-08-23 21:42:29 +0000147private:
148 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000149 CFGBlock* addStmt(Stmt* Terminator);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000150 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000151 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000152 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000153 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek55957a82009-05-02 00:13:27 +0000154 bool FinishBlock(CFGBlock* B);
Mike Stump31feda52009-07-17 01:31:16 +0000155
Ted Kremenekb64d1832008-03-13 03:04:22 +0000156 bool badCFG;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000157};
Mike Stump31feda52009-07-17 01:31:16 +0000158
Douglas Gregor4619e432008-12-05 23:32:09 +0000159// FIXME: Add support for dependent-sized array types in C++?
160// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000161static VariableArrayType* FindVA(Type* t) {
162 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
163 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
164 if (vat->getSizeExpr())
165 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000166
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000167 t = vt->getElementType().getTypePtr();
168 }
Mike Stump31feda52009-07-17 01:31:16 +0000169
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000170 return 0;
171}
Mike Stump31feda52009-07-17 01:31:16 +0000172
173/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
174/// arbitrary statement. Examples include a single expression or a function
175/// body (compound statement). The ownership of the returned CFG is
176/// transferred to the caller. If CFG construction fails, this method returns
177/// NULL.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000178CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenekeda180e22007-08-28 19:26:49 +0000179 assert (cfg);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000180 if (!Statement) return NULL;
181
Ted Kremenekb64d1832008-03-13 03:04:22 +0000182 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000183
184 // Create an empty block that will serve as the exit block for the CFG. Since
185 // this is the first block added to the CFG, it will be implicitly registered
186 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000187 Succ = createBlock();
188 assert (Succ == &cfg->getExit());
189 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000190
Ted Kremenek9aae5132007-08-23 21:42:29 +0000191 // Visit the statements and create the CFG.
Ted Kremenek40d87632008-02-27 17:33:02 +0000192 CFGBlock* B = Visit(Statement);
193 if (!B) B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000194
Ted Kremenek40d87632008-02-27 17:33:02 +0000195 if (B) {
Mike Stump31feda52009-07-17 01:31:16 +0000196 // Finalize the last constructed block. This usually involves reversing the
197 // order of the statements in the block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000198 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000199
200 // Backpatch the gotos whose label -> block mappings we didn't know when we
201 // encountered them.
202 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000203 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000204
Ted Kremenek9aae5132007-08-23 21:42:29 +0000205 CFGBlock* B = *I;
206 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
207 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
208
209 // If there is no target for the goto, then we are looking at an
210 // incomplete AST. Handle this by not registering a successor.
211 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000212
213 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000214 }
Mike Stump31feda52009-07-17 01:31:16 +0000215
Ted Kremenekeda180e22007-08-28 19:26:49 +0000216 // Add successors to the Indirect Goto Dispatch block (if we have one).
217 if (CFGBlock* B = cfg->getIndirectGotoBlock())
218 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
219 E = AddressTakenLabels.end(); I != E; ++I ) {
220
221 // Lookup the target block.
222 LabelMapTy::iterator LI = LabelMap.find(*I);
223
224 // If there is no target block that contains label, then we are looking
225 // at an incomplete AST. Handle this by not registering a successor.
226 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000227
228 B->addSuccessor(LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000229 }
Mike Stump31feda52009-07-17 01:31:16 +0000230
Ted Kremenekcdc0bc42007-09-17 16:18:02 +0000231 Succ = B;
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000232 }
Mike Stump31feda52009-07-17 01:31:16 +0000233
234 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000235 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000236
Ted Kremenekb64d1832008-03-13 03:04:22 +0000237 if (badCFG) {
238 delete cfg;
239 cfg = NULL;
240 return NULL;
241 }
Mike Stump31feda52009-07-17 01:31:16 +0000242
243 // NULL out cfg so that repeated calls to the builder will fail and that the
244 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000245 CFG* t = cfg;
246 cfg = NULL;
247 return t;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000248}
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenek9aae5132007-08-23 21:42:29 +0000250/// createBlock - Used to lazily create blocks that are connected
251/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000252CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000253 CFGBlock* B = cfg->createBlock();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000254 if (add_successor && Succ) B->addSuccessor(Succ);
255 return B;
256}
Mike Stump31feda52009-07-17 01:31:16 +0000257
258/// FinishBlock - When the last statement has been added to the block, we must
259/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek55957a82009-05-02 00:13:27 +0000260bool CFGBuilder::FinishBlock(CFGBlock* B) {
261 if (badCFG)
262 return false;
263
Ted Kremenek9aae5132007-08-23 21:42:29 +0000264 assert (B);
Ted Kremenek81e14852007-08-27 19:46:09 +0000265 B->reverseStmts();
Ted Kremenek55957a82009-05-02 00:13:27 +0000266 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000267}
268
Mike Stump31feda52009-07-17 01:31:16 +0000269/// addStmt - Used to add statements/expressions to the current CFGBlock
Ted Kremenek9e248872007-08-27 21:27:44 +0000270/// "Block". This method calls WalkAST on the passed statement to see if it
Mike Stump31feda52009-07-17 01:31:16 +0000271/// contains any short-circuit expressions. If so, it recursively creates the
272/// necessary blocks for such expressions. It returns the "topmost" block of
273/// the created blocks, or the original value of "Block" when this method was
274/// called if no additional blocks are created.
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000275CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremeneke9610502007-08-30 18:39:40 +0000276 if (!Block) Block = createBlock();
Ted Kremenek51d40b02009-07-17 18:15:54 +0000277 return WalkAST(Terminator, true);
Ted Kremenek9e248872007-08-27 21:27:44 +0000278}
279
Ted Kremenek51d40b02009-07-17 18:15:54 +0000280/// WalkAST - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000281/// blocks for ternary operators, &&, and ||. We also process "," and
282/// DeclStmts (which may contain nested control-flow).
Ted Kremenek51d40b02009-07-17 18:15:54 +0000283CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000284 switch (Terminator->getStmtClass()) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000285 case Stmt::ConditionalOperatorClass:
286 return VisitConditionalOperator(cast<ConditionalOperator>(Terminator));
287
Ted Kremenek21822592009-07-17 18:20:32 +0000288 case Stmt::ChooseExprClass:
289 return VisitChooseExpr(cast<ChooseExpr>(Terminator));
Mike Stump31feda52009-07-17 01:31:16 +0000290
291 case Stmt::DeclStmtClass: {
292 DeclStmt *DS = cast<DeclStmt>(Terminator);
293 if (DS->isSingleDecl()) {
294 Block->appendStmt(Terminator);
295 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
296 }
297
298 CFGBlock* B = 0;
299
300 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
301 typedef llvm::SmallVector<Decl*,10> BufTy;
302 BufTy Buf(DS->decl_begin(), DS->decl_end());
303
304 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
305 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
306 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
307 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
308
309 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
310 // automatically freed with the CFG.
311 DeclGroupRef DG(*I);
312 Decl* D = *I;
313 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
314
315 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
316
317 // Append the fake DeclStmt to block.
318 Block->appendStmt(DS);
319 B = WalkAST_VisitDeclSubExpr(D);
320 }
321 return B;
322 }
323
324 case Stmt::AddrLabelExprClass: {
325 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
326 AddressTakenLabels.insert(A->getLabel());
327
328 if (AlwaysAddStmt) Block->appendStmt(Terminator);
329 return Block;
330 }
331
332 case Stmt::StmtExprClass:
333 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
334
335 case Stmt::SizeOfAlignOfExprClass: {
336 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
337
338 // VLA types have expressions that must be evaluated.
339 if (E->isArgumentType()) {
340 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
341 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
342 addStmt(VA->getSizeExpr());
343 }
344 // Expressions in sizeof/alignof are not evaluated and thus have no
345 // control flow.
346 else
347 Block->appendStmt(Terminator);
348
349 return Block;
350 }
351
352 case Stmt::BinaryOperatorClass: {
353 BinaryOperator* B = cast<BinaryOperator>(Terminator);
354
355 if (B->isLogicalOp()) { // && or ||
356 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
357 ConfluenceBlock->appendStmt(B);
358 if (!FinishBlock(ConfluenceBlock))
359 return 0;
360
361 // create the block evaluating the LHS
362 CFGBlock* LHSBlock = createBlock(false);
363 LHSBlock->setTerminator(B);
364
365 // create the block evaluating the RHS
Ted Kremenek527ec812007-08-31 17:03:41 +0000366 Succ = ConfluenceBlock;
367 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000368 CFGBlock* RHSBlock = Visit(B->getRHS());
Ted Kremenek55957a82009-05-02 00:13:27 +0000369 if (!FinishBlock(RHSBlock))
370 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000371
372 // Now link the LHSBlock with RHSBlock.
373 if (B->getOpcode() == BinaryOperator::LOr) {
374 LHSBlock->addSuccessor(ConfluenceBlock);
375 LHSBlock->addSuccessor(RHSBlock);
376 } else {
377 assert (B->getOpcode() == BinaryOperator::LAnd);
378 LHSBlock->addSuccessor(RHSBlock);
379 LHSBlock->addSuccessor(ConfluenceBlock);
380 }
381
382 // Generate the blocks for evaluating the LHS.
383 Block = LHSBlock;
384 return addStmt(B->getLHS());
385 } else if (B->getOpcode() == BinaryOperator::Comma) { // ,
386 Block->appendStmt(B);
387 addStmt(B->getRHS());
388 return addStmt(B->getLHS());
Ted Kremenek527ec812007-08-31 17:03:41 +0000389 }
Ted Kremenek91e8d2a2007-08-28 16:18:58 +0000390
Mike Stump31feda52009-07-17 01:31:16 +0000391 break;
392 }
Ted Kremenekef52c642008-10-06 20:56:19 +0000393
Ted Kremenekac49ea62008-09-26 18:17:07 +0000394 // Blocks: No support for blocks ... yet
Mike Stump31feda52009-07-17 01:31:16 +0000395 case Stmt::BlockExprClass:
396 case Stmt::BlockDeclRefExprClass:
397 return NYS();
398
399 case Stmt::ParenExprClass:
400 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
401
Mike Stump48871a22009-07-17 01:04:31 +0000402 case Stmt::CallExprClass: {
Chris Lattnerc960b3e2009-07-17 15:50:19 +0000403 // If this is a call to a no-return function, this stops the block here.
404 FunctionDecl *FD = cast<CallExpr>(Terminator)->getDirectCallee();
405 if (FD == 0 || !FD->hasAttr<NoReturnAttr>())
Mike Stump48871a22009-07-17 01:04:31 +0000406 break;
407
Chris Lattnerc960b3e2009-07-17 15:50:19 +0000408 if (Block && !FinishBlock(Block))
409 return 0;
Mike Stump48871a22009-07-17 01:04:31 +0000410
411 // Create new block with no successor for the remaining pieces.
412 Block = createBlock(false);
413 Block->appendStmt(Terminator);
414
415 // Wire this to the exit block directly.
416 Block->addSuccessor(&cfg->getExit());
417
418 return WalkAST_VisitChildren(Terminator);
419 }
420
Mike Stump31feda52009-07-17 01:31:16 +0000421 default:
Chris Lattnerc960b3e2009-07-17 15:50:19 +0000422 // TODO: We can follow objective-c methods (message sends).
Mike Stump31feda52009-07-17 01:31:16 +0000423 break;
Ted Kremenek9e248872007-08-27 21:27:44 +0000424 };
Mike Stump31feda52009-07-17 01:31:16 +0000425
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000426 if (AlwaysAddStmt) Block->appendStmt(Terminator);
427 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9e248872007-08-27 21:27:44 +0000428}
Mike Stump31feda52009-07-17 01:31:16 +0000429
430/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions for
431/// initializers in Decls.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000432CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +0000433 VarDecl* VD = dyn_cast<VarDecl>(D);
434
435 if (!VD)
Ted Kremenek7016e4d2007-11-18 20:06:01 +0000436 return Block;
Mike Stump31feda52009-07-17 01:31:16 +0000437
Ted Kremenek8889bb32008-08-06 23:20:50 +0000438 Expr* Init = VD->getInit();
Mike Stump31feda52009-07-17 01:31:16 +0000439
Ted Kremenek61a625f2008-09-26 16:26:36 +0000440 if (Init) {
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000441 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenek61a625f2008-09-26 16:26:36 +0000442 switch (Init->getStmtClass()) {
443 case Stmt::IntegerLiteralClass:
444 case Stmt::CharacterLiteralClass:
445 case Stmt::StringLiteralClass:
446 break;
447 default:
448 Block = addStmt(Init);
449 }
Ted Kremenekfcf68632008-02-29 22:32:24 +0000450 }
Mike Stump31feda52009-07-17 01:31:16 +0000451
Ted Kremenek61a625f2008-09-26 16:26:36 +0000452 // If the type of VD is a VLA, then we must process its size expressions.
453 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
454 VA = FindVA(VA->getElementType().getTypePtr()))
Mike Stump31feda52009-07-17 01:31:16 +0000455 Block = addStmt(VA->getSizeExpr());
456
Ted Kremenek25fb6812007-08-28 18:14:37 +0000457 return Block;
458}
459
Mike Stump31feda52009-07-17 01:31:16 +0000460/// WalkAST_VisitChildren - Utility method to call WalkAST on the children of a
461/// Stmt.
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000462CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +0000463 CFGBlock* B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000464 for (Stmt::child_iterator I = Terminator->child_begin(),
465 E = Terminator->child_end();
Ted Kremenek9e248872007-08-27 21:27:44 +0000466 I != E; ++I)
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000467 if (*I) B = WalkAST(*I);
Mike Stump31feda52009-07-17 01:31:16 +0000468
Ted Kremenek9e248872007-08-27 21:27:44 +0000469 return B;
470}
471
Ted Kremenek105efce2007-08-28 18:30:10 +0000472/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
473/// expressions (a GCC extension).
Ted Kremenekc1f9a282008-04-16 21:10:48 +0000474CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
475 Block->appendStmt(Terminator);
Mike Stump31feda52009-07-17 01:31:16 +0000476 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek105efce2007-08-28 18:30:10 +0000477}
478
Ted Kremenek9aae5132007-08-23 21:42:29 +0000479/// VisitStmt - Handle statements with no branching control flow.
480CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
Mike Stump31feda52009-07-17 01:31:16 +0000481 // We cannot assume that we are in the middle of a basic block, since the CFG
482 // might only be constructed for this single statement. If we have no current
483 // basic block, just create one lazily.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000484 if (!Block) Block = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000485
486 // Simply add the statement to the current block. We actually insert
487 // statements in reverse order; this order is reversed later when processing
488 // the containing element in the AST.
Ted Kremenek81e14852007-08-27 19:46:09 +0000489 addStmt(Statement);
490
Ted Kremenek9aae5132007-08-23 21:42:29 +0000491 return Block;
492}
493
494CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
495 return Block;
496}
497
Ted Kremenek21822592009-07-17 18:20:32 +0000498CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
499 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
500 ConfluenceBlock->appendStmt(C);
501 if (!FinishBlock(ConfluenceBlock))
502 return 0;
503
504 Succ = ConfluenceBlock;
505 Block = NULL;
506 CFGBlock* LHSBlock = Visit(C->getLHS());
507 if (!FinishBlock(LHSBlock))
508 return 0;
509
510 Succ = ConfluenceBlock;
511 Block = NULL;
512 CFGBlock* RHSBlock = Visit(C->getRHS());
513 if (!FinishBlock(RHSBlock))
514 return 0;
515
516 Block = createBlock(false);
517 Block->addSuccessor(LHSBlock);
518 Block->addSuccessor(RHSBlock);
519 Block->setTerminator(C);
520 return addStmt(C->getCond());
521}
Ted Kremenek51d40b02009-07-17 18:15:54 +0000522
523CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
524 // Create the confluence block that will "merge" the results of the ternary
525 // expression.
526 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
527 ConfluenceBlock->appendStmt(C);
528 if (!FinishBlock(ConfluenceBlock))
529 return 0;
530
531 // Create a block for the LHS expression if there is an LHS expression. A
532 // GCC extension allows LHS to be NULL, causing the condition to be the
533 // value that is returned instead.
534 // e.g: x ?: y is shorthand for: x ? x : y;
535 Succ = ConfluenceBlock;
536 Block = NULL;
537 CFGBlock* LHSBlock = NULL;
538 if (C->getLHS()) {
539 LHSBlock = Visit(C->getLHS());
540 if (!FinishBlock(LHSBlock))
541 return 0;
542 Block = NULL;
543 }
544
545 // Create the block for the RHS expression.
546 Succ = ConfluenceBlock;
547 CFGBlock* RHSBlock = Visit(C->getRHS());
548 if (!FinishBlock(RHSBlock))
549 return 0;
550
551 // Create the block that will contain the condition.
552 Block = createBlock(false);
553
554 if (LHSBlock)
555 Block->addSuccessor(LHSBlock);
556 else {
557 // If we have no LHS expression, add the ConfluenceBlock as a direct
558 // successor for the block containing the condition. Moreover, we need to
559 // reverse the order of the predecessors in the ConfluenceBlock because
560 // the RHSBlock will have been added to the succcessors already, and we
561 // want the first predecessor to the the block containing the expression
562 // for the case when the ternary expression evaluates to true.
563 Block->addSuccessor(ConfluenceBlock);
564 assert (ConfluenceBlock->pred_size() == 2);
565 std::reverse(ConfluenceBlock->pred_begin(),
566 ConfluenceBlock->pred_end());
567 }
568
569 Block->addSuccessor(RHSBlock);
570
571 Block->setTerminator(C);
572 return addStmt(C->getCond());
573}
574
Ted Kremenek9aae5132007-08-23 21:42:29 +0000575CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump31feda52009-07-17 01:31:16 +0000576
Ted Kremenek0b0f2062009-07-03 00:10:50 +0000577 CFGBlock* LastBlock = Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000578
Ted Kremenekf6998822008-02-26 00:22:58 +0000579 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
580 I != E; ++I ) {
Ted Kremenekfff4fc72008-03-17 17:19:44 +0000581 LastBlock = Visit(*I);
Ted Kremenekf6998822008-02-26 00:22:58 +0000582 }
583
Ted Kremenekfff4fc72008-03-17 17:19:44 +0000584 return LastBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000585}
586
587CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000588 // We may see an if statement in the middle of a basic block, or it may be the
589 // first statement we are processing. In either case, we create a new basic
590 // block. First, we create the blocks for the then...else statements, and
591 // then we create the block containing the if statement. If we were in the
592 // middle of a block, we stop processing that block and reverse its
593 // statements. That block is then the implicit successor for the "then" and
594 // "else" clauses.
595
596 // The block we were proccessing is now finished. Make it the successor
597 // block.
598 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000599 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000600 if (!FinishBlock(Block))
601 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000602 }
Mike Stump31feda52009-07-17 01:31:16 +0000603
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000604 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000605 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000606
Ted Kremenek9aae5132007-08-23 21:42:29 +0000607 if (Stmt* Else = I->getElse()) {
608 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000609
Ted Kremenek9aae5132007-08-23 21:42:29 +0000610 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000611 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000612 Block = NULL;
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000613 ElseBlock = Visit(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000614
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000615 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
616 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000617 else if (Block) {
618 if (!FinishBlock(ElseBlock))
619 return 0;
620 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000621 }
Mike Stump31feda52009-07-17 01:31:16 +0000622
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000623 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000624 CFGBlock* ThenBlock;
625 {
626 Stmt* Then = I->getThen();
627 assert (Then);
628 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000629 Block = NULL;
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000630 ThenBlock = Visit(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000631
Ted Kremenek1b379512009-04-01 03:52:47 +0000632 if (!ThenBlock) {
633 // We can reach here if the "then" body has all NullStmts.
634 // Create an empty block so we can distinguish between true and false
635 // branches in path-sensitive analyses.
636 ThenBlock = createBlock(false);
637 ThenBlock->addSuccessor(sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000638 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000639 if (!FinishBlock(ThenBlock))
640 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000641 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000642 }
643
Mike Stump31feda52009-07-17 01:31:16 +0000644 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000645 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000646
Ted Kremenek9aae5132007-08-23 21:42:29 +0000647 // Set the terminator of the new block to the If statement.
648 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000649
Ted Kremenek9aae5132007-08-23 21:42:29 +0000650 // Now add the successors.
651 Block->addSuccessor(ThenBlock);
652 Block->addSuccessor(ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000653
654 // Add the condition as the last statement in the new block. This may create
655 // new blocks as the condition may contain control-flow. Any newly created
656 // blocks will be pointed to be "Block".
Ted Kremenek15647632008-01-30 23:02:42 +0000657 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000658}
Mike Stump31feda52009-07-17 01:31:16 +0000659
660
Ted Kremenek9aae5132007-08-23 21:42:29 +0000661CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump31feda52009-07-17 01:31:16 +0000662 // If we were in the middle of a block we stop processing that block and
663 // reverse its statements.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000664 //
Mike Stump31feda52009-07-17 01:31:16 +0000665 // NOTE: If a "return" appears in the middle of a block, this means that the
666 // code afterwards is DEAD (unreachable). We still keep a basic block
667 // for that code; a simple "mark-and-sweep" from the entry block will be
668 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000669 if (Block) FinishBlock(Block);
670
671 // Create the new block.
672 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000673
Ted Kremenek9aae5132007-08-23 21:42:29 +0000674 // The Exit block is the only successor.
675 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000676
677 // Add the return statement to the block. This may create new blocks if R
678 // contains control-flow (short-circuit operations).
Ted Kremenek81e14852007-08-27 19:46:09 +0000679 return addStmt(R);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000680}
681
682CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
683 // Get the block of the labeled statement. Add it to our map.
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000684 Visit(L->getSubStmt());
685 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000686
Ted Kremenekf9b17562007-08-30 18:20:57 +0000687 if (!LabelBlock) // This can happen when the body is empty, i.e.
688 LabelBlock=createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000689
Ted Kremenek9aae5132007-08-23 21:42:29 +0000690 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
691 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000692
693 // Labels partition blocks, so this is the end of the basic block we were
694 // processing (L is the block's label). Because this is label (and we have
695 // already processed the substatement) there is no extra control-flow to worry
696 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000697 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000698 if (!FinishBlock(LabelBlock))
699 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000700
701 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000702 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000703
Ted Kremenek9aae5132007-08-23 21:42:29 +0000704 // This block is now the implicit successor of other blocks.
705 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000706
Ted Kremenek9aae5132007-08-23 21:42:29 +0000707 return LabelBlock;
708}
709
710CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000711 // Goto is a control-flow statement. Thus we stop processing the current
712 // block and create a new one.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000713 if (Block) FinishBlock(Block);
714 Block = createBlock(false);
715 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000716
717 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000718 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000719
Ted Kremenek9aae5132007-08-23 21:42:29 +0000720 if (I == LabelMap.end())
721 // We will need to backpatch this block later.
722 BackpatchBlocks.push_back(Block);
723 else
724 Block->addSuccessor(I->second);
725
Mike Stump31feda52009-07-17 01:31:16 +0000726 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000727}
728
729CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump31feda52009-07-17 01:31:16 +0000730 // "for" is a control-flow statement. Thus we stop processing the current
731 // block.
732
Ted Kremenek9aae5132007-08-23 21:42:29 +0000733 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000734
Ted Kremenek9aae5132007-08-23 21:42:29 +0000735 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000736 if (!FinishBlock(Block))
737 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000738 LoopSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000739 } else LoopSuccessor = Succ;
740
741 // Because of short-circuit evaluation, the condition of the loop can span
742 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
743 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000744 CFGBlock* ExitConditionBlock = createBlock(false);
745 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000746
Ted Kremenek81e14852007-08-27 19:46:09 +0000747 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000748 ExitConditionBlock->setTerminator(F);
749
750 // Now add the actual condition to the condition block. Because the condition
751 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +0000752 if (Stmt* C = F->getCond()) {
753 Block = ExitConditionBlock;
754 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +0000755 if (Block) {
756 if (!FinishBlock(EntryConditionBlock))
757 return 0;
758 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000759 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000760
Mike Stump31feda52009-07-17 01:31:16 +0000761 // The condition block is the implicit successor for the loop body as well as
762 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000763 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000764
Ted Kremenek9aae5132007-08-23 21:42:29 +0000765 // Now create the loop body.
766 {
767 assert (F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000768
Ted Kremenek9aae5132007-08-23 21:42:29 +0000769 // Save the current values for Block, Succ, and continue and break targets
770 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
771 save_continue(ContinueTargetBlock),
Mike Stump31feda52009-07-17 01:31:16 +0000772 save_break(BreakTargetBlock);
773
Ted Kremeneke9610502007-08-30 18:39:40 +0000774 // Create a new block to contain the (bottom) of the loop body.
775 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000776
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000777 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +0000778 // Generate increment code in its own basic block. This is the target of
779 // continue statements.
Ted Kremeneke03879b2008-11-24 20:50:24 +0000780 Succ = Visit(I);
Mike Stump31feda52009-07-17 01:31:16 +0000781 } else {
782 // No increment code. Create a special, empty, block that is used as the
783 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +0000784 assert(Succ == EntryConditionBlock);
785 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000786 }
Mike Stump31feda52009-07-17 01:31:16 +0000787
Ted Kremenek902393b2009-04-28 00:51:56 +0000788 // Finish up the increment (or empty) block if it hasn't been already.
789 if (Block) {
790 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +0000791 if (!FinishBlock(Block))
792 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +0000793 Block = 0;
794 }
Mike Stump31feda52009-07-17 01:31:16 +0000795
Ted Kremenek902393b2009-04-28 00:51:56 +0000796 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000797
Ted Kremenek902393b2009-04-28 00:51:56 +0000798 // The starting block for the loop increment is the block that should
799 // represent the 'loop target' for looping back to the start of the loop.
800 ContinueTargetBlock->setLoopTarget(F);
801
Ted Kremenekb0746ca2008-09-04 21:48:47 +0000802 // All breaks should go to the code following the loop.
Mike Stump31feda52009-07-17 01:31:16 +0000803 BreakTargetBlock = LoopSuccessor;
804
805 // Now populate the body block, and in the process create new blocks as we
806 // walk the body of the loop.
807 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +0000808
809 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +0000810 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000811 else if (Block) {
812 if (!FinishBlock(BodyBlock))
813 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000814 }
815
Ted Kremenek81e14852007-08-27 19:46:09 +0000816 // This new body block is a successor to our "exit" condition block.
817 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000818 }
Mike Stump31feda52009-07-17 01:31:16 +0000819
820 // Link up the condition block with the code that follows the loop. (the
821 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +0000822 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +0000823
Ted Kremenek9aae5132007-08-23 21:42:29 +0000824 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +0000825 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000826 if (Stmt* I = F->getInit()) {
827 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +0000828 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +0000829 } else {
830 // There is no loop initialization. We are thus basically a while loop.
831 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000832 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +0000833 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +0000834 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000835 }
836}
837
Ted Kremenek9d56e642008-11-11 17:10:00 +0000838CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
839 // Objective-C fast enumeration 'for' statements:
840 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
841 //
842 // for ( Type newVariable in collection_expression ) { statements }
843 //
844 // becomes:
845 //
846 // prologue:
847 // 1. collection_expression
848 // T. jump to loop_entry
849 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000850 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +0000851 // 1. ObjCForCollectionStmt [performs binding to newVariable]
852 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
853 // TB:
854 // statements
855 // T. jump to loop_entry
856 // FB:
857 // what comes after
858 //
859 // and
860 //
861 // Type existingItem;
862 // for ( existingItem in expression ) { statements }
863 //
864 // becomes:
865 //
Mike Stump31feda52009-07-17 01:31:16 +0000866 // the same with newVariable replaced with existingItem; the binding works
867 // the same except that for one ObjCForCollectionStmt::getElement() returns
868 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +0000869 //
Mike Stump31feda52009-07-17 01:31:16 +0000870
Ted Kremenek9d56e642008-11-11 17:10:00 +0000871 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000872
Ted Kremenek9d56e642008-11-11 17:10:00 +0000873 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000874 if (!FinishBlock(Block))
875 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +0000876 LoopSuccessor = Block;
877 Block = 0;
Mike Stump31feda52009-07-17 01:31:16 +0000878 } else LoopSuccessor = Succ;
879
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000880 // Build the condition blocks.
881 CFGBlock* ExitConditionBlock = createBlock(false);
882 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000883
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000884 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +0000885 ExitConditionBlock->setTerminator(S);
886
887 // The last statement in the block should be the ObjCForCollectionStmt, which
888 // performs the actual binding to 'element' and determines if there are any
889 // more items in the collection.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000890 ExitConditionBlock->appendStmt(S);
891 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000892
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000893 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +0000894 // generate new blocks as necesary. We DON'T add the statement by default to
895 // the CFG unless it contains control-flow.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000896 EntryConditionBlock = WalkAST(S->getElement(), false);
Mike Stump31feda52009-07-17 01:31:16 +0000897 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000898 if (!FinishBlock(EntryConditionBlock))
899 return 0;
900 Block = 0;
901 }
Mike Stump31feda52009-07-17 01:31:16 +0000902
903 // The condition block is the implicit successor for the loop body as well as
904 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000905 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000906
Ted Kremenek9d56e642008-11-11 17:10:00 +0000907 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +0000908 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000909 // Save the current values for Succ, continue and break targets.
910 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +0000911 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
912
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000913 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +0000914 ContinueTargetBlock = EntryConditionBlock;
915
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000916 CFGBlock* BodyBlock = Visit(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +0000917
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000918 if (!BodyBlock)
919 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +0000920 else if (Block) {
921 if (!FinishBlock(BodyBlock))
922 return 0;
923 }
Mike Stump31feda52009-07-17 01:31:16 +0000924
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000925 // This new body block is a successor to our "exit" condition block.
926 ExitConditionBlock->addSuccessor(BodyBlock);
927 }
Mike Stump31feda52009-07-17 01:31:16 +0000928
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000929 // Link up the condition block with the code that follows the loop.
930 // (the false branch).
931 ExitConditionBlock->addSuccessor(LoopSuccessor);
932
Ted Kremenek9d56e642008-11-11 17:10:00 +0000933 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +0000934 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +0000935 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +0000936}
937
Ted Kremenek49805452009-05-02 01:49:13 +0000938CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
939 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +0000940
Ted Kremenek49805452009-05-02 01:49:13 +0000941 // Inline the body.
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000942 CFGBlock *SyncBlock = Visit(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +0000943
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000944 // The sync body starts its own basic block. This makes it a little easier
945 // for diagnostic clients.
946 if (SyncBlock) {
947 if (!FinishBlock(SyncBlock))
948 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000949
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000950 Block = 0;
951 }
Mike Stump31feda52009-07-17 01:31:16 +0000952
Ted Kremenekb3c657b2009-05-05 23:11:51 +0000953 Succ = SyncBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000954
Ted Kremenek49805452009-05-02 01:49:13 +0000955 // Inline the sync expression.
956 return Visit(S->getSynchExpr());
957}
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000959CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek89be6522009-04-07 04:26:02 +0000960 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +0000961}
Ted Kremenek9d56e642008-11-11 17:10:00 +0000962
Ted Kremenek9aae5132007-08-23 21:42:29 +0000963CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump31feda52009-07-17 01:31:16 +0000964 // "while" is a control-flow statement. Thus we stop processing the current
965 // block.
966
Ted Kremenek9aae5132007-08-23 21:42:29 +0000967 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000968
Ted Kremenek9aae5132007-08-23 21:42:29 +0000969 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000970 if (!FinishBlock(Block))
971 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000972 LoopSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000973 } else LoopSuccessor = Succ;
974
975 // Because of short-circuit evaluation, the condition of the loop can span
976 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
977 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000978 CFGBlock* ExitConditionBlock = createBlock(false);
979 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000980
Ted Kremenek81e14852007-08-27 19:46:09 +0000981 // Set the terminator for the "exit" condition block.
982 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +0000983
984 // Now add the actual condition to the condition block. Because the condition
985 // itself may contain control-flow, new blocks may be created. Thus we update
986 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +0000987 if (Stmt* C = W->getCond()) {
988 Block = ExitConditionBlock;
989 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +0000990 assert(Block == EntryConditionBlock);
Ted Kremenek55957a82009-05-02 00:13:27 +0000991 if (Block) {
992 if (!FinishBlock(EntryConditionBlock))
993 return 0;
994 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000995 }
Mike Stump31feda52009-07-17 01:31:16 +0000996
997 // The condition block is the implicit successor for the loop body as well as
998 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +0000999 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001000
Ted Kremenek9aae5132007-08-23 21:42:29 +00001001 // Process the loop body.
1002 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001003 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001004
1005 // Save the current values for Block, Succ, and continue and break targets
1006 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1007 save_continue(ContinueTargetBlock),
1008 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001009
Mike Stump31feda52009-07-17 01:31:16 +00001010 // Create an empty block to represent the transition block for looping back
1011 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001012 Block = 0;
1013 assert(Succ == EntryConditionBlock);
1014 Succ = createBlock();
1015 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001016 ContinueTargetBlock = Succ;
1017
Ted Kremenek9aae5132007-08-23 21:42:29 +00001018 // All breaks should go to the code following the loop.
1019 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001020
Ted Kremenek9aae5132007-08-23 21:42:29 +00001021 // NULL out Block to force lazy instantiation of blocks for the body.
1022 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001023
Ted Kremenek9aae5132007-08-23 21:42:29 +00001024 // Create the body. The returned block is the entry to the loop body.
1025 CFGBlock* BodyBlock = Visit(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001026
Ted Kremeneke9610502007-08-30 18:39:40 +00001027 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001028 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001029 else if (Block) {
1030 if (!FinishBlock(BodyBlock))
1031 return 0;
1032 }
Mike Stump31feda52009-07-17 01:31:16 +00001033
Ted Kremenek9aae5132007-08-23 21:42:29 +00001034 // Add the loop body entry as a successor to the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001035 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001036 }
Mike Stump31feda52009-07-17 01:31:16 +00001037
1038 // Link up the condition block with the code that follows the loop. (the
1039 // false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001040 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001041
1042 // There can be no more statements in the condition block since we loop back
1043 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001044 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001045
Ted Kremenek9aae5132007-08-23 21:42:29 +00001046 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001047 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001048 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001049}
Mike Stump31feda52009-07-17 01:31:16 +00001050
Ted Kremenek93041ba2008-12-09 20:20:09 +00001051CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1052 // FIXME: This isn't complete. We basically treat @throw like a return
1053 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001054
1055 // If we were in the middle of a block we stop processing that block and
1056 // reverse its statements.
Ted Kremenek55957a82009-05-02 00:13:27 +00001057 if (Block) {
1058 if (!FinishBlock(Block))
1059 return 0;
1060 }
Mike Stump31feda52009-07-17 01:31:16 +00001061
Ted Kremenek93041ba2008-12-09 20:20:09 +00001062 // Create the new block.
1063 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001064
Ted Kremenek93041ba2008-12-09 20:20:09 +00001065 // The Exit block is the only successor.
1066 Block->addSuccessor(&cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001067
1068 // Add the statement to the block. This may create new blocks if S contains
1069 // control-flow (short-circuit operations).
Ted Kremenek93041ba2008-12-09 20:20:09 +00001070 return addStmt(S);
1071}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001072
1073CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1074 // "do...while" is a control-flow statement. Thus we stop processing the
1075 // current block.
Mike Stump31feda52009-07-17 01:31:16 +00001076
Ted Kremenek9aae5132007-08-23 21:42:29 +00001077 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001078
Ted Kremenek9aae5132007-08-23 21:42:29 +00001079 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001080 if (!FinishBlock(Block))
1081 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001082 LoopSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001083 } else LoopSuccessor = Succ;
1084
1085 // Because of short-circuit evaluation, the condition of the loop can span
1086 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1087 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001088 CFGBlock* ExitConditionBlock = createBlock(false);
1089 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001090
Ted Kremenek81e14852007-08-27 19:46:09 +00001091 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001092 ExitConditionBlock->setTerminator(D);
1093
1094 // Now add the actual condition to the condition block. Because the condition
1095 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001096 if (Stmt* C = D->getCond()) {
1097 Block = ExitConditionBlock;
1098 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001099 if (Block) {
1100 if (!FinishBlock(EntryConditionBlock))
1101 return 0;
1102 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001103 }
Mike Stump31feda52009-07-17 01:31:16 +00001104
Ted Kremeneka1523a32008-02-27 07:20:00 +00001105 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001106 Succ = EntryConditionBlock;
1107
Ted Kremenek9aae5132007-08-23 21:42:29 +00001108 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001109 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001110 {
1111 assert (D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001112
Ted Kremenek9aae5132007-08-23 21:42:29 +00001113 // Save the current values for Block, Succ, and continue and break targets
1114 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1115 save_continue(ContinueTargetBlock),
1116 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001117
Ted Kremenek9aae5132007-08-23 21:42:29 +00001118 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001119 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001120
Ted Kremenek9aae5132007-08-23 21:42:29 +00001121 // All breaks should go to the code following the loop.
1122 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001123
Ted Kremenek9aae5132007-08-23 21:42:29 +00001124 // NULL out Block to force lazy instantiation of blocks for the body.
1125 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001126
Ted Kremenek9aae5132007-08-23 21:42:29 +00001127 // Create the body. The returned block is the entry to the loop body.
1128 BodyBlock = Visit(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001129
Ted Kremeneke9610502007-08-30 18:39:40 +00001130 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001131 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001132 else if (Block) {
1133 if (!FinishBlock(BodyBlock))
1134 return 0;
1135 }
Mike Stump31feda52009-07-17 01:31:16 +00001136
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001137 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001138 // ExitConditionBlock to represent the "loop back" transition. Create an
1139 // empty block to represent the transition block for looping back to the
1140 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001141 // FIXME: Can we do this more efficiently without adding another block?
1142 Block = NULL;
1143 Succ = BodyBlock;
1144 CFGBlock *LoopBackBlock = createBlock();
1145 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001146
Ted Kremenek9aae5132007-08-23 21:42:29 +00001147 // Add the loop body entry as a successor to the condition.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001148 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001149 }
Mike Stump31feda52009-07-17 01:31:16 +00001150
Ted Kremenek9aae5132007-08-23 21:42:29 +00001151 // Link up the condition block with the code that follows the loop.
1152 // (the false branch).
Ted Kremenek81e14852007-08-27 19:46:09 +00001153 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001154
1155 // There can be no more statements in the body block(s) since we loop back to
1156 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001157 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001158
Ted Kremenek9aae5132007-08-23 21:42:29 +00001159 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001160 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001161 return BodyBlock;
1162}
1163
1164CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1165 // "continue" is a control-flow statement. Thus we stop processing the
1166 // current block.
Ted Kremenek55957a82009-05-02 00:13:27 +00001167 if (Block) {
1168 if (!FinishBlock(Block))
1169 return 0;
1170 }
Mike Stump31feda52009-07-17 01:31:16 +00001171
Ted Kremenek9aae5132007-08-23 21:42:29 +00001172 // Now create a new block that ends with the continue statement.
1173 Block = createBlock(false);
1174 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001175
Ted Kremenek9aae5132007-08-23 21:42:29 +00001176 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001177 // incomplete AST. This means the CFG cannot be constructed.
1178 if (ContinueTargetBlock)
1179 Block->addSuccessor(ContinueTargetBlock);
1180 else
1181 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001182
Ted Kremenek9aae5132007-08-23 21:42:29 +00001183 return Block;
1184}
1185
1186CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
Mike Stump31feda52009-07-17 01:31:16 +00001187 // "break" is a control-flow statement. Thus we stop processing the current
1188 // block.
Ted Kremenek55957a82009-05-02 00:13:27 +00001189 if (Block) {
1190 if (!FinishBlock(Block))
1191 return 0;
1192 }
Mike Stump31feda52009-07-17 01:31:16 +00001193
Mike Stump48871a22009-07-17 01:04:31 +00001194 // Now create a new block that ends with the break statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001195 Block = createBlock(false);
1196 Block->setTerminator(B);
Mike Stump31feda52009-07-17 01:31:16 +00001197
1198 // If there is no target for the break, then we are looking at an incomplete
1199 // AST. This means that the CFG cannot be constructed.
Ted Kremenek882cf062009-04-07 18:53:24 +00001200 if (BreakTargetBlock)
1201 Block->addSuccessor(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001202 else
Ted Kremenek882cf062009-04-07 18:53:24 +00001203 badCFG = true;
1204
Ted Kremenek9aae5132007-08-23 21:42:29 +00001205
Mike Stump31feda52009-07-17 01:31:16 +00001206 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001207}
1208
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001209CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001210 // "switch" is a control-flow statement. Thus we stop processing the current
1211 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001212 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001213
Ted Kremenek9aae5132007-08-23 21:42:29 +00001214 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001215 if (!FinishBlock(Block))
1216 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001217 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001218 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001219
1220 // Save the current "switch" context.
1221 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001222 save_break(BreakTargetBlock),
1223 save_default(DefaultCaseBlock);
1224
Mike Stump31feda52009-07-17 01:31:16 +00001225 // Set the "default" case to be the block after the switch statement. If the
1226 // switch statement contains a "default:", this value will be overwritten with
1227 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001228 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001229
Ted Kremenek9aae5132007-08-23 21:42:29 +00001230 // Create a new block that will contain the switch statement.
1231 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001232
Ted Kremenek9aae5132007-08-23 21:42:29 +00001233 // Now process the switch body. The code after the switch is the implicit
1234 // successor.
1235 Succ = SwitchSuccessor;
1236 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001237
1238 // When visiting the body, the case statements should automatically get linked
1239 // up to the switch. We also don't keep a pointer to the body, since all
1240 // control-flow from the switch goes to case/default statements.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001241 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001242 Block = NULL;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001243 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001244 if (Block) {
1245 if (!FinishBlock(BodyBlock))
1246 return 0;
1247 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001248
Mike Stump31feda52009-07-17 01:31:16 +00001249 // If we have no "default:" case, the default transition is to the code
1250 // following the switch body.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001251 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001252
Ted Kremenek81e14852007-08-27 19:46:09 +00001253 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001254 SwitchTerminatedBlock->setTerminator(Terminator);
1255 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001256 Block = SwitchTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001257
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001258 return addStmt(Terminator->getCond());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001259}
1260
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001261CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001262 // CaseStmts are essentially labels, so they are the first statement in a
1263 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001264
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001265 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek55e91e82007-08-30 18:48:11 +00001266 CFGBlock* CaseBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001267 if (!CaseBlock) CaseBlock = createBlock();
1268
1269 // Cases statements partition blocks, so this is the top of the basic block we
1270 // were processing (the "case XXX:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001271 CaseBlock->setLabel(Terminator);
Ted Kremenek55957a82009-05-02 00:13:27 +00001272 if (!FinishBlock(CaseBlock))
1273 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001274
1275 // Add this block to the list of successors for the block with the switch
1276 // statement.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001277 assert (SwitchTerminatedBlock);
1278 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001279
Ted Kremenek9aae5132007-08-23 21:42:29 +00001280 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1281 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001282
Ted Kremenek9aae5132007-08-23 21:42:29 +00001283 // This block is now the implicit successor of other blocks.
1284 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001285
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001286 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001287}
Mike Stump31feda52009-07-17 01:31:16 +00001288
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001289CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1290 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek654c78f2008-02-13 22:05:39 +00001291 DefaultCaseBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001292 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1293
1294 // Default statements partition blocks, so this is the top of the basic block
1295 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001296 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek55957a82009-05-02 00:13:27 +00001297 if (!FinishBlock(DefaultCaseBlock))
1298 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001299
Mike Stump31feda52009-07-17 01:31:16 +00001300 // Unlike case statements, we don't add the default block to the successors
1301 // for the switch statement immediately. This is done when we finish
1302 // processing the switch statement. This allows for the default case
1303 // (including a fall-through to the code after the switch statement) to always
1304 // be the last successor of a switch-terminated block.
1305
Ted Kremenek654c78f2008-02-13 22:05:39 +00001306 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1307 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001308
Ted Kremenek654c78f2008-02-13 22:05:39 +00001309 // This block is now the implicit successor of other blocks.
1310 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001311
1312 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001313}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001314
Ted Kremenekeda180e22007-08-28 19:26:49 +00001315CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001316 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001317 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001318
Ted Kremenekeda180e22007-08-28 19:26:49 +00001319 if (!IBlock) {
1320 IBlock = createBlock(false);
1321 cfg->setIndirectGotoBlock(IBlock);
1322 }
Mike Stump31feda52009-07-17 01:31:16 +00001323
Ted Kremenekeda180e22007-08-28 19:26:49 +00001324 // IndirectGoto is a control-flow statement. Thus we stop processing the
1325 // current block and create a new one.
Ted Kremenek55957a82009-05-02 00:13:27 +00001326 if (Block) {
1327 if (!FinishBlock(Block))
1328 return 0;
1329 }
Ted Kremenekeda180e22007-08-28 19:26:49 +00001330 Block = createBlock(false);
1331 Block->setTerminator(I);
1332 Block->addSuccessor(IBlock);
1333 return addStmt(I->getTarget());
1334}
1335
Ted Kremenek9aae5132007-08-23 21:42:29 +00001336
Ted Kremenek04cca642007-08-23 21:26:19 +00001337} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001338
Mike Stump31feda52009-07-17 01:31:16 +00001339/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1340/// no successors or predecessors. If this is the first block created in the
1341/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001342CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001343 bool first_block = begin() == end();
1344
1345 // Create the block.
Ted Kremenek813dd672007-09-05 20:02:05 +00001346 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek889073f2007-08-23 16:51:22 +00001347
1348 // If this is the first block, set it as the Entry and Exit.
1349 if (first_block) Entry = Exit = &front();
1350
1351 // Return the block.
1352 return &front();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001353}
1354
Ted Kremenek889073f2007-08-23 16:51:22 +00001355/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1356/// CFG is returned to the caller.
1357CFG* CFG::buildCFG(Stmt* Statement) {
1358 CFGBuilder Builder;
1359 return Builder.buildCFG(Statement);
1360}
1361
1362/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001363void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1364
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001365//===----------------------------------------------------------------------===//
1366// CFG: Queries for BlkExprs.
1367//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001368
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001369namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001370 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001371}
1372
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001373static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1374 if (!Terminator)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001375 return;
Mike Stump31feda52009-07-17 01:31:16 +00001376
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001377 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001378 if (!*I) continue;
Mike Stump31feda52009-07-17 01:31:16 +00001379
Ted Kremenek95a123c2008-01-26 00:03:27 +00001380 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1381 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001382
Ted Kremenek95a123c2008-01-26 00:03:27 +00001383 FindSubExprAssignments(*I, Set);
1384 }
1385}
1386
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001387static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1388 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001389
1390 // Look for assignments that are used as subexpressions. These are the only
1391 // assignments that we want to *possibly* register as a block-level
1392 // expression. Basically, if an assignment occurs both in a subexpression and
1393 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001394 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001395
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001396 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1397 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001398 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001399
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001400 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001401
1402 // Iterate over the statements again on identify the Expr* and Stmt* at the
1403 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001404
Ted Kremenek95a123c2008-01-26 00:03:27 +00001405 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001406 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001407
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001408 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001409 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001410 // expression are really "statements" whose value is never used by
1411 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001412 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001413 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001414 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1415 // Special handling for statement expressions. The last statement in
1416 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001417 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001418 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001419 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001420 (*M)[C->body_back()] = x;
1421 }
1422 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001423
Ted Kremenek95a123c2008-01-26 00:03:27 +00001424 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001425 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001426 }
Mike Stump31feda52009-07-17 01:31:16 +00001427
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001428 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001430 Stmt* S = I->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001431
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001432 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001433 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001434 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001435 }
1436 }
Mike Stump31feda52009-07-17 01:31:16 +00001437
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001438 return M;
1439}
1440
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001441CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1442 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001443 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001444
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001445 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001446 BlkExprMapTy::iterator I = M->find(S);
Mike Stump31feda52009-07-17 01:31:16 +00001447
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001448 if (I == M->end()) return CFG::BlkExprNumTy();
1449 else return CFG::BlkExprNumTy(I->second);
1450}
1451
1452unsigned CFG::getNumBlkExprs() {
1453 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1454 return M->size();
1455 else {
1456 // We assume callers interested in the number of BlkExprs will want
1457 // the map constructed if it doesn't already exist.
1458 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1459 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1460 }
1461}
1462
Ted Kremenek6065ef62008-04-28 18:00:46 +00001463//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001464// Cleanup: CFG dstor.
1465//===----------------------------------------------------------------------===//
1466
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001467CFG::~CFG() {
1468 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1469}
Mike Stump31feda52009-07-17 01:31:16 +00001470
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001471//===----------------------------------------------------------------------===//
1472// CFG pretty printing
1473//===----------------------------------------------------------------------===//
1474
Ted Kremenek7e776b12007-08-22 18:22:34 +00001475namespace {
1476
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001477class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001479 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1480 StmtMapTy StmtMap;
1481 signed CurrentBlock;
1482 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001483 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001484public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001485
Chris Lattnerc61089a2009-06-30 01:26:17 +00001486 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1487 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001488 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1489 unsigned j = 1;
1490 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1491 BI != BEnd; ++BI, ++j )
1492 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1493 }
1494 }
Mike Stump31feda52009-07-17 01:31:16 +00001495
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001496 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001497
Chris Lattnerc61089a2009-06-30 01:26:17 +00001498 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001499 void setBlockID(signed i) { CurrentBlock = i; }
1500 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001501
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001502 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001503
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001504 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001505
1506 if (I == StmtMap.end())
1507 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001508
1509 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001510 && I->second.second == CurrentStmt)
1511 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001512
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001513 OS << "[B" << I->second.first << "." << I->second.second << "]";
1514 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001515 }
1516};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001517} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001518
Chris Lattnerc61089a2009-06-30 01:26:17 +00001519
1520namespace {
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001521class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1522 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001523
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001524 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001525 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001526 PrintingPolicy Policy;
1527
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001528public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001529 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001530 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001531 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001532
Ted Kremenek9aae5132007-08-23 21:42:29 +00001533 void VisitIfStmt(IfStmt* I) {
1534 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001535 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001536 }
Mike Stump31feda52009-07-17 01:31:16 +00001537
Ted Kremenek9aae5132007-08-23 21:42:29 +00001538 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001539 void VisitStmt(Stmt* Terminator) {
1540 Terminator->printPretty(OS, Helper, Policy);
1541 }
1542
Ted Kremenek9aae5132007-08-23 21:42:29 +00001543 void VisitForStmt(ForStmt* F) {
1544 OS << "for (" ;
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001545 if (F->getInit()) OS << "...";
1546 OS << "; ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001547 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001548 OS << "; ";
1549 if (F->getInc()) OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00001550 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00001551 }
Mike Stump31feda52009-07-17 01:31:16 +00001552
Ted Kremenek9aae5132007-08-23 21:42:29 +00001553 void VisitWhileStmt(WhileStmt* W) {
1554 OS << "while " ;
Douglas Gregor7de59662009-05-29 20:38:28 +00001555 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001556 }
Mike Stump31feda52009-07-17 01:31:16 +00001557
Ted Kremenek9aae5132007-08-23 21:42:29 +00001558 void VisitDoStmt(DoStmt* D) {
1559 OS << "do ... while ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001560 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001561 }
Mike Stump31feda52009-07-17 01:31:16 +00001562
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001563 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00001564 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001565 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00001566 }
Mike Stump31feda52009-07-17 01:31:16 +00001567
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001568 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001569 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001570 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001571 }
Mike Stump31feda52009-07-17 01:31:16 +00001572
Ted Kremenek391f94a2007-08-31 22:29:13 +00001573 void VisitChooseExpr(ChooseExpr* C) {
1574 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001575 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00001576 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00001577 }
Mike Stump31feda52009-07-17 01:31:16 +00001578
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001579 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1580 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00001581 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001582 }
Mike Stump31feda52009-07-17 01:31:16 +00001583
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001584 void VisitBinaryOperator(BinaryOperator* B) {
1585 if (!B->isLogicalOp()) {
1586 VisitExpr(B);
1587 return;
1588 }
Mike Stump31feda52009-07-17 01:31:16 +00001589
Douglas Gregor7de59662009-05-29 20:38:28 +00001590 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001592 switch (B->getOpcode()) {
1593 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00001594 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001595 return;
1596 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00001597 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001598 return;
1599 default:
1600 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00001601 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00001602 }
Mike Stump31feda52009-07-17 01:31:16 +00001603
Ted Kremenek12687ff2007-08-27 21:54:41 +00001604 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00001605 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00001606 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001607};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001608} // end anonymous namespace
1609
Mike Stump31feda52009-07-17 01:31:16 +00001610
Chris Lattnerc61089a2009-06-30 01:26:17 +00001611static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1612 Stmt* Terminator) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001613 if (Helper) {
1614 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001615 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001616 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00001617
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001618 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00001619 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00001620 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00001621 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001622 return;
1623 }
1624 }
Mike Stump31feda52009-07-17 01:31:16 +00001625
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001626 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001627 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001628 if (B->getOpcode() == BinaryOperator::Comma) {
1629 OS << "... , ";
1630 Helper->handledStmt(B->getRHS(),OS);
1631 OS << '\n';
1632 return;
Mike Stump31feda52009-07-17 01:31:16 +00001633 }
1634 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001635 }
Mike Stump31feda52009-07-17 01:31:16 +00001636
Chris Lattnerc61089a2009-06-30 01:26:17 +00001637 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00001638
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001639 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001640 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001641}
Mike Stump31feda52009-07-17 01:31:16 +00001642
Chris Lattnerc61089a2009-06-30 01:26:17 +00001643static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1644 const CFGBlock& B,
1645 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00001646
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001647 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001649 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00001650 OS << "\n [ B" << B.getBlockID();
1651
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001652 if (&B == &cfg->getEntry())
1653 OS << " (ENTRY) ]\n";
1654 else if (&B == &cfg->getExit())
1655 OS << " (EXIT) ]\n";
1656 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001657 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001658 else
1659 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00001660
Ted Kremenek71eca012007-08-29 23:20:49 +00001661 // Print the label of this block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001662 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001663
1664 if (print_edges)
1665 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001666
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001667 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001668 OS << L->getName();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001669 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00001670 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001671 C->getLHS()->printPretty(OS, Helper,
1672 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001673 if (C->getRHS()) {
1674 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00001675 C->getRHS()->printPretty(OS, Helper,
1676 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00001677 }
Mike Stump31feda52009-07-17 01:31:16 +00001678 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek71eca012007-08-29 23:20:49 +00001679 OS << "default";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001680 else
1681 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00001682
Ted Kremenek71eca012007-08-29 23:20:49 +00001683 OS << ":\n";
1684 }
Mike Stump31feda52009-07-17 01:31:16 +00001685
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001686 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001687 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00001688
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001689 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1690 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00001691
Ted Kremenek71eca012007-08-29 23:20:49 +00001692 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001693 if (print_edges)
1694 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001695
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001696 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00001697
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001698 if (Helper)
1699 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00001700
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001701 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001702 }
Mike Stump31feda52009-07-17 01:31:16 +00001703
Ted Kremenek71eca012007-08-29 23:20:49 +00001704 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001705 if (B.getTerminator()) {
1706 if (print_edges)
1707 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00001708
Ted Kremenek71eca012007-08-29 23:20:49 +00001709 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00001710
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001711 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00001712
Chris Lattnerc61089a2009-06-30 01:26:17 +00001713 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1714 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001715 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00001716 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001717 }
Mike Stump31feda52009-07-17 01:31:16 +00001718
Ted Kremenek71eca012007-08-29 23:20:49 +00001719 if (print_edges) {
1720 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001721 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00001722 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00001723
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001724 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1725 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001726
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001727 if (i == 8 || (i-8) == 0)
1728 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00001729
Ted Kremenek71eca012007-08-29 23:20:49 +00001730 OS << " B" << (*I)->getBlockID();
1731 }
Mike Stump31feda52009-07-17 01:31:16 +00001732
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001733 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00001734
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001735 // Print the successors of this block.
1736 OS << " Successors (" << B.succ_size() << "):";
1737 i = 0;
1738
1739 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1740 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00001741
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001742 if (i == 8 || (i-8) % 10 == 0)
1743 OS << "\n ";
1744
1745 OS << " B" << (*I)->getBlockID();
1746 }
Mike Stump31feda52009-07-17 01:31:16 +00001747
Ted Kremenek71eca012007-08-29 23:20:49 +00001748 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001749 }
Mike Stump31feda52009-07-17 01:31:16 +00001750}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001751
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001752
1753/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001754void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001755
1756/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001757void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1758 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001760 // Print the entry block.
1761 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00001762
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001763 // Iterate through the CFGBlocks and print them one by one.
1764 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1765 // Skip the entry block, because we already printed it.
1766 if (&(*I) == &getEntry() || &(*I) == &getExit())
1767 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001768
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001769 print_block(OS, this, *I, &Helper, true);
1770 }
Mike Stump31feda52009-07-17 01:31:16 +00001771
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001772 // Print the exit block.
1773 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00001774 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00001775}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001776
1777/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001778void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1779 print(llvm::errs(), cfg, LO);
1780}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001781
1782/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1783/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001784void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1785 const LangOptions &LO) const {
1786 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001787 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00001788}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001789
Ted Kremenek15647632008-01-30 23:02:42 +00001790/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00001791void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00001792 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00001793 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00001794 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1795}
1796
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001797Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00001798
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001799 if (!Terminator)
1800 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001801
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001802 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001803
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001804 switch (Terminator->getStmtClass()) {
1805 default:
1806 break;
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001808 case Stmt::ForStmtClass:
1809 E = cast<ForStmt>(Terminator)->getCond();
1810 break;
Mike Stump31feda52009-07-17 01:31:16 +00001811
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001812 case Stmt::WhileStmtClass:
1813 E = cast<WhileStmt>(Terminator)->getCond();
1814 break;
Mike Stump31feda52009-07-17 01:31:16 +00001815
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001816 case Stmt::DoStmtClass:
1817 E = cast<DoStmt>(Terminator)->getCond();
1818 break;
Mike Stump31feda52009-07-17 01:31:16 +00001819
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001820 case Stmt::IfStmtClass:
1821 E = cast<IfStmt>(Terminator)->getCond();
1822 break;
Mike Stump31feda52009-07-17 01:31:16 +00001823
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001824 case Stmt::ChooseExprClass:
1825 E = cast<ChooseExpr>(Terminator)->getCond();
1826 break;
Mike Stump31feda52009-07-17 01:31:16 +00001827
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001828 case Stmt::IndirectGotoStmtClass:
1829 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1830 break;
Mike Stump31feda52009-07-17 01:31:16 +00001831
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001832 case Stmt::SwitchStmtClass:
1833 E = cast<SwitchStmt>(Terminator)->getCond();
1834 break;
Mike Stump31feda52009-07-17 01:31:16 +00001835
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001836 case Stmt::ConditionalOperatorClass:
1837 E = cast<ConditionalOperator>(Terminator)->getCond();
1838 break;
Mike Stump31feda52009-07-17 01:31:16 +00001839
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001840 case Stmt::BinaryOperatorClass: // '&&' and '||'
1841 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001842 break;
Mike Stump31feda52009-07-17 01:31:16 +00001843
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001844 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00001845 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001846 }
Mike Stump31feda52009-07-17 01:31:16 +00001847
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001848 return E ? E->IgnoreParens() : NULL;
1849}
1850
Ted Kremenek92137a32008-05-16 16:06:00 +00001851bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00001852
Ted Kremenek92137a32008-05-16 16:06:00 +00001853 if (!Terminator)
1854 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001855
Ted Kremenek92137a32008-05-16 16:06:00 +00001856 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001857
Ted Kremenek92137a32008-05-16 16:06:00 +00001858 switch (Terminator->getStmtClass()) {
1859 default:
1860 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001861
1862 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00001863 case Stmt::WhileStmtClass:
1864 case Stmt::DoStmtClass:
1865 case Stmt::IfStmtClass:
1866 case Stmt::ChooseExprClass:
1867 case Stmt::ConditionalOperatorClass:
1868 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00001869 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00001870 }
Mike Stump31feda52009-07-17 01:31:16 +00001871
Ted Kremenek92137a32008-05-16 16:06:00 +00001872 return E ? E->IgnoreParens() : NULL;
1873}
1874
Ted Kremenek15647632008-01-30 23:02:42 +00001875
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001876//===----------------------------------------------------------------------===//
1877// CFG Graphviz Visualization
1878//===----------------------------------------------------------------------===//
1879
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001880
1881#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00001882static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001883#endif
1884
Chris Lattnerc61089a2009-06-30 01:26:17 +00001885void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001886#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00001887 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001888 GraphHelper = &H;
1889 llvm::ViewGraph(this,"CFG");
1890 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001891#endif
1892}
1893
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001894namespace llvm {
1895template<>
1896struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson4d9e93c2009-06-24 17:37:55 +00001897 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1898 bool ShortNames) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001899
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001900#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001901 std::string OutSStr;
1902 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001903 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001904 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001905
1906 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1907
1908 // Process string output to make it nicer...
1909 for (unsigned i = 0; i != OutStr.length(); ++i)
1910 if (OutStr[i] == '\n') { // Left justify
1911 OutStr[i] = '\\';
1912 OutStr.insert(OutStr.begin()+i+1, 'l');
1913 }
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001915 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00001916#else
1917 return "";
1918#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001919 }
1920};
1921} // end namespace llvm