 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
62.4% |
443 / 710 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
81.3% |
577 / 710 |
| |
|
Line Coverage: |
72.5% |
755 / 1041 |
| |
 |
|
 |
1 : //===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
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 :
15 : #include "clang/Analysis/Support/SaveAndRestore.h"
16 : #include "clang/Analysis/CFG.h"
17 : #include "clang/AST/DeclCXX.h"
18 : #include "clang/AST/StmtVisitor.h"
19 : #include "clang/AST/PrettyPrinter.h"
20 : #include "llvm/Support/GraphWriter.h"
21 : #include "llvm/Support/Allocator.h"
22 : #include "llvm/Support/Format.h"
23 : #include "llvm/ADT/DenseMap.h"
24 : #include "llvm/ADT/SmallPtrSet.h"
25 : #include "llvm/ADT/OwningPtr.h"
26 :
27 : using namespace clang;
28 :
29 : namespace {
30 :
31 250: static SourceLocation GetEndLoc(Decl* D) {
201: branch 1 taken
49: branch 2 taken
32 250: if (VarDecl* VD = dyn_cast<VarDecl>(D))
60: branch 1 taken
141: branch 2 taken
33 201: if (Expr* Ex = VD->getInit())
34 60: return Ex->getSourceRange().getEnd();
35 :
36 190: return D->getLocation();
37 : }
38 :
39 : class AddStmtChoice {
40 : public:
41 : enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
42 : public:
43 81668: AddStmtChoice(Kind kind) : k(kind) {}
44 59759: bool alwaysAdd() const { return k != NotAlwaysAdd; }
45 18481: bool asLValue() const { return k == AlwaysAddAsLValue; }
46 : private:
47 : Kind k;
48 : };
49 :
50 : /// CFGBuilder - This class implements CFG construction from an AST.
51 : /// 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 : ///
59 : /// 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.
63 : ///
64 5486: class CFGBuilder {
65 : ASTContext *Context;
66 : llvm::OwningPtr<CFG> cfg;
67 :
68 : CFGBlock* Block;
69 : CFGBlock* Succ;
70 : CFGBlock* ContinueTargetBlock;
71 : CFGBlock* BreakTargetBlock;
72 : CFGBlock* SwitchTerminatedBlock;
73 : CFGBlock* DefaultCaseBlock;
74 : CFGBlock* TryTerminatedBlock;
75 :
76 : // LabelMap records the mapping from Label expressions to their blocks.
77 : typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
78 : LabelMapTy LabelMap;
79 :
80 : // A list of blocks that end with a "goto" that must be backpatched to their
81 : // resolved targets upon completion of CFG construction.
82 : typedef std::vector<CFGBlock*> BackpatchBlocksTy;
83 : BackpatchBlocksTy BackpatchBlocks;
84 :
85 : // A list of labels whose address has been taken (for indirect gotos).
86 : typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
87 : LabelSetTy AddressTakenLabels;
88 :
89 : public:
90 5486: explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
91 : Block(NULL), Succ(NULL),
92 : ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
93 : SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
94 5486: TryTerminatedBlock(NULL) {}
95 :
96 : // buildCFG - Used by external clients to construct the CFG.
97 : CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges,
98 : bool AddScopes);
99 :
100 : private:
101 : // Visitors to walk an AST and construct the CFG.
102 : CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
103 : CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
104 : CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
105 : CFGBlock *VisitBreakStmt(BreakStmt *B);
106 : CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
107 : CFGBlock *VisitCaseStmt(CaseStmt *C);
108 : CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
109 : CFGBlock *VisitCompoundStmt(CompoundStmt *C);
110 : CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
111 : AddStmtChoice asc);
112 : CFGBlock *VisitContinueStmt(ContinueStmt *C);
113 : CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
114 : CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
115 : CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
116 : CFGBlock *VisitDeclStmt(DeclStmt *DS);
117 : CFGBlock *VisitDeclSubExpr(Decl* D);
118 : CFGBlock *VisitDefaultStmt(DefaultStmt *D);
119 : CFGBlock *VisitDoStmt(DoStmt *D);
120 : CFGBlock *VisitForStmt(ForStmt *F);
121 : CFGBlock *VisitGotoStmt(GotoStmt* G);
122 : CFGBlock *VisitIfStmt(IfStmt *I);
123 : CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
124 : CFGBlock *VisitLabelStmt(LabelStmt *L);
125 : CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
126 : CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
127 : CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
128 : CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
129 : CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
130 : CFGBlock *VisitReturnStmt(ReturnStmt* R);
131 : CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
132 : CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
133 : CFGBlock *VisitSwitchStmt(SwitchStmt *S);
134 : CFGBlock *VisitWhileStmt(WhileStmt *W);
135 :
136 : CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
137 : CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
138 : CFGBlock *VisitChildren(Stmt* S);
139 :
140 : // NYS == Not Yet Supported
141 8: CFGBlock* NYS() {
142 8: badCFG = true;
143 8: return Block;
144 : }
145 :
146 6426: CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
6426: branch 0 taken
0: branch 1 not taken
147 6426: if (!AddScopes)
148 6426: return B;
149 :
0: branch 0 not taken
0: branch 1 not taken
150 0: if (B == 0)
151 0: B = createBlock();
152 0: B->StartScope(S, cfg->getBumpVectorContext());
153 0: return B;
154 : }
155 :
156 6434: void EndScope(Stmt *S) {
6434: branch 0 taken
0: branch 1 not taken
157 6434: if (!AddScopes)
158 6434: return;
159 :
0: branch 0 not taken
0: branch 1 not taken
160 0: if (Block == 0)
161 0: Block = createBlock();
162 0: Block->EndScope(S, cfg->getBumpVectorContext());
163 : }
164 :
2475: branch 0 taken
15428: branch 1 taken
165 17903: void autoCreateBlock() { if (!Block) Block = createBlock(); }
166 : CFGBlock *createBlock(bool add_successor = true);
167 : bool FinishBlock(CFGBlock* B);
168 26898: CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
169 26898: return Visit(S, asc);
170 : }
171 :
172 : void AppendStmt(CFGBlock *B, Stmt *S,
173 18481: AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
174 18481: B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
175 18481: }
176 :
177 18377: void AddSuccessor(CFGBlock *B, CFGBlock *S) {
178 18377: B->addSuccessor(S, cfg->getBumpVectorContext());
179 18377: }
180 :
181 : /// TryResult - a class representing a variant over the values
182 : /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
183 : /// and is used by the CFGBuilder to decide if a branch condition
184 : /// can be decided up front during CFG construction.
185 : class TryResult {
186 : int X;
187 : public:
369: branch 0 taken
98: branch 1 taken
188 467: TryResult(bool b) : X(b ? 1 : 0) {}
189 1859: TryResult() : X(-1) {}
190 :
191 2185: bool isTrue() const { return X == 1; }
192 2185: bool isFalse() const { return X == 0; }
193 110: bool isKnown() const { return X >= 0; }
194 13: void negate() {
0: branch 1 not taken
13: branch 2 taken
195 13: assert(isKnown());
196 13: X ^= 0x1;
197 13: }
198 : };
199 :
200 : /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
201 : /// if we can evaluate to a known value, otherwise return -1.
202 2176: TryResult TryEvaluateBool(Expr *S) {
203 2176: Expr::EvalResult Result;
2175: branch 1 taken
1: branch 2 taken
2175: branch 4 taken
0: branch 5 not taken
319: branch 7 taken
1856: branch 8 taken
317: branch 10 taken
2: branch 11 taken
317: branch 12 taken
1859: branch 13 taken
204 2176: if (!S->isTypeDependent() && !S->isValueDependent() &&
205 : S->Evaluate(Result, *Context) && Result.Val.isInt())
206 317: return Result.Val.getInt().getBoolValue();
207 :
208 1859: return TryResult();
209 : }
210 :
211 : bool badCFG;
212 :
213 : // True iff EH edges on CallExprs should be added to the CFG.
214 : bool AddEHEdges;
215 :
216 : // True iff scope start and scope end notes should be added to the CFG.
217 : bool AddScopes;
218 : };
219 :
220 : // FIXME: Add support for dependent-sized array types in C++?
221 : // Does it even make sense to build a CFG for an uninstantiated template?
222 3847: static VariableArrayType* FindVA(Type* t) {
233: branch 1 taken
3614: branch 2 taken
223 4023: while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
57: branch 1 taken
176: branch 2 taken
224 233: if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
57: branch 1 taken
0: branch 2 not taken
225 57: if (vat->getSizeExpr())
226 57: return vat;
227 :
228 176: t = vt->getElementType().getTypePtr();
229 : }
230 :
231 3614: return 0;
232 : }
233 :
234 : /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
235 : /// arbitrary statement. Examples include a single expression or a function
236 : /// body (compound statement). The ownership of the returned CFG is
237 : /// transferred to the caller. If CFG construction fails, this method returns
238 : /// NULL.
239 : CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
240 5486: bool addehedges, bool AddScopes) {
241 5486: AddEHEdges = addehedges;
242 5486: Context = C;
0: branch 1 not taken
5486: branch 2 taken
243 5486: assert(cfg.get());
0: branch 0 not taken
5486: branch 1 taken
244 5486: if (!Statement)
245 0: return NULL;
246 :
247 5486: this->AddScopes = AddScopes;
248 5486: badCFG = false;
249 :
250 : // Create an empty block that will serve as the exit block for the CFG. Since
251 : // this is the first block added to the CFG, it will be implicitly registered
252 : // as the exit block.
253 5486: Succ = createBlock();
0: branch 2 not taken
5486: branch 3 taken
254 5486: assert(Succ == &cfg->getExit());
255 5486: Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
256 :
257 : // Visit the statements and create the CFG.
258 5486: CFGBlock* B = addStmt(Statement);
259 :
260 5486: if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
261 : // FIXME: Add code for base initializers and member initializers.
262 : (void)CD;
263 : }
55: branch 0 taken
5431: branch 1 taken
264 5486: if (!B)
265 55: B = Succ;
266 :
5486: branch 0 taken
0: branch 1 not taken
267 5486: if (B) {
268 : // Finalize the last constructed block. This usually involves reversing the
269 : // order of the statements in the block.
5335: branch 0 taken
151: branch 1 taken
270 5486: if (Block) FinishBlock(B);
271 :
272 : // Backpatch the gotos whose label -> block mappings we didn't know when we
273 : // encountered them.
14: branch 3 taken
5486: branch 4 taken
274 10986: for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
275 5486: E = BackpatchBlocks.end(); I != E; ++I ) {
276 :
277 14: CFGBlock* B = *I;
278 14: GotoStmt* G = cast<GotoStmt>(B->getTerminator());
279 14: LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
280 :
281 : // If there is no target for the goto, then we are looking at an
282 : // incomplete AST. Handle this by not registering a successor.
13: branch 3 taken
1: branch 4 taken
283 14: if (LI == LabelMap.end()) continue;
284 :
285 13: AddSuccessor(B, LI->second);
286 : }
287 :
288 : // Add successors to the Indirect Goto Dispatch block (if we have one).
13: branch 2 taken
5473: branch 3 taken
289 5486: if (CFGBlock* B = cfg->getIndirectGotoBlock())
14: branch 3 taken
13: branch 4 taken
290 40: for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
291 13: E = AddressTakenLabels.end(); I != E; ++I ) {
292 :
293 : // Lookup the target block.
294 14: LabelMapTy::iterator LI = LabelMap.find(*I);
295 :
296 : // If there is no target block that contains label, then we are looking
297 : // at an incomplete AST. Handle this by not registering a successor.
14: branch 3 taken
0: branch 4 not taken
298 14: if (LI == LabelMap.end()) continue;
299 :
300 14: AddSuccessor(B, LI->second);
301 : }
302 :
303 5486: Succ = B;
304 : }
305 :
306 : // Create an empty entry block that has no predecessors.
307 5486: cfg->setEntry(createBlock());
308 :
8: branch 0 taken
5478: branch 1 taken
309 5486: return badCFG ? NULL : cfg.take();
310 : }
311 :
312 : /// createBlock - Used to lazily create blocks that are connected
313 : /// to the current (global) succcessor.
314 21483: CFGBlock* CFGBuilder::createBlock(bool add_successor) {
315 21483: CFGBlock* B = cfg->createBlock();
13849: branch 0 taken
7634: branch 1 taken
8363: branch 2 taken
5486: branch 3 taken
316 21483: if (add_successor && Succ)
317 8363: AddSuccessor(B, Succ);
318 21483: return B;
319 : }
320 :
321 : /// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
322 10228: bool CFGBuilder::FinishBlock(CFGBlock* B) {
6: branch 0 taken
10222: branch 1 taken
323 10228: if (badCFG)
324 6: return false;
325 :
0: branch 0 not taken
10222: branch 1 taken
326 10222: assert(B);
327 10222: return true;
328 : }
329 :
330 : /// Visit - Walk the subtree of a statement and add extra
331 : /// blocks for ternary operators, &&, and ||. We also process "," and
332 : /// DeclStmts (which may contain nested control-flow).
333 74799: CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
334 74799: tryAgain:
46475: branch 1 taken
15: branch 2 taken
5125: branch 3 taken
140: branch 4 taken
165: branch 5 taken
3127: branch 6 taken
171: branch 7 taken
10: branch 8 taken
6385: branch 9 taken
230: branch 10 taken
15: branch 11 taken
0: branch 12 not taken
9: branch 13 taken
4: branch 14 taken
3679: branch 15 taken
28: branch 16 taken
51: branch 17 taken
150: branch 18 taken
30: branch 19 taken
1529: branch 20 taken
13: branch 21 taken
66: branch 22 taken
0: branch 23 not taken
5: branch 24 taken
2: branch 25 taken
8: branch 26 taken
20: branch 27 taken
2010: branch 28 taken
29: branch 29 taken
4965: branch 30 taken
125: branch 31 taken
49: branch 32 taken
51: branch 33 taken
118: branch 34 taken
335 74799: switch (S->getStmtClass()) {
336 : default:
337 46475: return VisitStmt(S, asc);
338 :
339 : case Stmt::AddrLabelExprClass:
340 15: return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
341 :
342 : case Stmt::BinaryOperatorClass:
343 5125: return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
344 :
345 : case Stmt::BlockExprClass:
346 140: return VisitBlockExpr(cast<BlockExpr>(S), asc);
347 :
348 : case Stmt::BreakStmtClass:
349 165: return VisitBreakStmt(cast<BreakStmt>(S));
350 :
351 : case Stmt::CallExprClass:
352 3127: return VisitCallExpr(cast<CallExpr>(S), asc);
353 :
354 : case Stmt::CaseStmtClass:
355 171: return VisitCaseStmt(cast<CaseStmt>(S));
356 :
357 : case Stmt::ChooseExprClass:
358 10: return VisitChooseExpr(cast<ChooseExpr>(S), asc);
359 :
360 : case Stmt::CompoundStmtClass:
361 6385: return VisitCompoundStmt(cast<CompoundStmt>(S));
362 :
363 : case Stmt::ConditionalOperatorClass:
364 230: return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
365 :
366 : case Stmt::ContinueStmtClass:
367 15: return VisitContinueStmt(cast<ContinueStmt>(S));
368 :
369 : case Stmt::CXXCatchStmtClass:
370 0: return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
371 :
372 : case Stmt::CXXThrowExprClass:
373 9: return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
374 :
375 : case Stmt::CXXTryStmtClass:
376 4: return VisitCXXTryStmt(cast<CXXTryStmt>(S));
377 :
378 : case Stmt::DeclStmtClass:
379 3679: return VisitDeclStmt(cast<DeclStmt>(S));
380 :
381 : case Stmt::DefaultStmtClass:
382 28: return VisitDefaultStmt(cast<DefaultStmt>(S));
383 :
384 : case Stmt::DoStmtClass:
385 51: return VisitDoStmt(cast<DoStmt>(S));
386 :
387 : case Stmt::ForStmtClass:
388 150: return VisitForStmt(cast<ForStmt>(S));
389 :
390 : case Stmt::GotoStmtClass:
391 30: return VisitGotoStmt(cast<GotoStmt>(S));
392 :
393 : case Stmt::IfStmtClass:
394 1529: return VisitIfStmt(cast<IfStmt>(S));
395 :
396 : case Stmt::IndirectGotoStmtClass:
397 13: return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
398 :
399 : case Stmt::LabelStmtClass:
400 66: return VisitLabelStmt(cast<LabelStmt>(S));
401 :
402 : case Stmt::ObjCAtCatchStmtClass:
403 0: return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
404 :
405 : case Stmt::ObjCAtSynchronizedStmtClass:
406 5: return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
407 :
408 : case Stmt::ObjCAtThrowStmtClass:
409 2: return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
410 :
411 : case Stmt::ObjCAtTryStmtClass:
412 8: return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
413 :
414 : case Stmt::ObjCForCollectionStmtClass:
415 20: return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
416 :
417 : case Stmt::ParenExprClass:
418 2010: S = cast<ParenExpr>(S)->getSubExpr();
419 2010: goto tryAgain;
420 :
421 : case Stmt::NullStmtClass:
422 29: return Block;
423 :
424 : case Stmt::ReturnStmtClass:
425 4965: return VisitReturnStmt(cast<ReturnStmt>(S));
426 :
427 : case Stmt::SizeOfAlignOfExprClass:
428 125: return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
429 :
430 : case Stmt::StmtExprClass:
431 49: return VisitStmtExpr(cast<StmtExpr>(S), asc);
432 :
433 : case Stmt::SwitchStmtClass:
434 51: return VisitSwitchStmt(cast<SwitchStmt>(S));
435 :
436 : case Stmt::WhileStmtClass:
437 118: return VisitWhileStmt(cast<WhileStmt>(S));
438 : }
439 : }
440 :
441 59430: CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
14051: branch 1 taken
45379: branch 2 taken
442 59430: if (asc.alwaysAdd()) {
443 14051: autoCreateBlock();
444 14051: AppendStmt(Block, S, asc);
445 : }
446 :
447 59430: return VisitChildren(S);
448 : }
449 :
450 : /// VisitChildren - Visit the children of a Stmt.
451 59525: CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
452 59525: CFGBlock *B = Block;
45871: branch 3 taken
59525: branch 4 taken
453 164921: for (Stmt::child_iterator I = Terminator->child_begin(),
454 59525: E = Terminator->child_end(); I != E; ++I) {
45871: branch 1 taken
0: branch 2 not taken
455 45871: if (*I) B = Visit(*I);
456 : }
457 59525: return B;
458 : }
459 :
460 : CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
461 15: AddStmtChoice asc) {
462 15: AddressTakenLabels.insert(A->getLabel());
463 :
3: branch 1 taken
12: branch 2 taken
464 15: if (asc.alwaysAdd()) {
465 3: autoCreateBlock();
466 3: AppendStmt(Block, A, asc);
467 : }
468 :
469 15: return Block;
470 : }
471 :
472 : CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
473 5125: AddStmtChoice asc) {
97: branch 1 taken
5028: branch 2 taken
474 5125: if (B->isLogicalOp()) { // && or ||
96: branch 0 taken
1: branch 1 taken
475 97: CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
476 97: AppendStmt(ConfluenceBlock, B, asc);
477 :
0: branch 1 not taken
97: branch 2 taken
478 97: if (!FinishBlock(ConfluenceBlock))
479 0: return 0;
480 :
481 : // create the block evaluating the LHS
482 97: CFGBlock* LHSBlock = createBlock(false);
483 97: LHSBlock->setTerminator(B);
484 :
485 : // create the block evaluating the RHS
486 97: Succ = ConfluenceBlock;
487 97: Block = NULL;
488 97: CFGBlock* RHSBlock = addStmt(B->getRHS());
0: branch 1 not taken
97: branch 2 taken
489 97: if (!FinishBlock(RHSBlock))
490 0: return 0;
491 :
492 : // See if this is a known constant.
493 97: TryResult KnownVal = TryEvaluateBool(B->getLHS());
25: branch 1 taken
72: branch 2 taken
13: branch 4 taken
12: branch 5 taken
13: branch 6 taken
84: branch 7 taken
494 97: if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
495 13: KnownVal.negate();
496 :
497 : // Now link the LHSBlock with RHSBlock.
39: branch 1 taken
58: branch 2 taken
498 97: if (B->getOpcode() == BinaryOperator::LOr) {
6: branch 1 taken
33: branch 2 taken
499 39: AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
7: branch 1 taken
32: branch 2 taken
500 39: AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
501 : } else {
0: branch 1 not taken
58: branch 2 taken
502 58: assert(B->getOpcode() == BinaryOperator::LAnd);
6: branch 1 taken
52: branch 2 taken
503 58: AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
6: branch 1 taken
52: branch 2 taken
504 58: AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
505 : }
506 :
507 : // Generate the blocks for evaluating the LHS.
508 97: Block = LHSBlock;
509 97: return addStmt(B->getLHS());
510 : }
81: branch 1 taken
4947: branch 2 taken
511 5028: else if (B->getOpcode() == BinaryOperator::Comma) { // ,
512 81: autoCreateBlock();
513 81: AppendStmt(Block, B, asc);
514 81: addStmt(B->getRHS());
515 81: return addStmt(B->getLHS());
516 : }
517 :
518 4947: return VisitStmt(B, asc);
519 : }
520 :
521 140: CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
11: branch 1 taken
129: branch 2 taken
522 140: if (asc.alwaysAdd()) {
523 11: autoCreateBlock();
524 11: AppendStmt(Block, E, asc);
525 : }
526 140: return Block;
527 : }
528 :
529 165: CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
530 : // "break" is a control-flow statement. Thus we stop processing the current
531 : // block.
1: branch 0 taken
164: branch 1 taken
0: branch 3 not taken
1: branch 4 taken
0: branch 5 not taken
165: branch 6 taken
532 165: if (Block && !FinishBlock(Block))
533 0: return 0;
534 :
535 : // Now create a new block that ends with the break statement.
536 165: Block = createBlock(false);
537 165: Block->setTerminator(B);
538 :
539 : // If there is no target for the break, then we are looking at an incomplete
540 : // AST. This means that the CFG cannot be constructed.
165: branch 0 taken
0: branch 1 not taken
541 165: if (BreakTargetBlock)
542 165: AddSuccessor(Block, BreakTargetBlock);
543 : else
544 0: badCFG = true;
545 :
546 :
547 165: return Block;
548 : }
549 :
550 3127: static bool CanThrow(Expr *E) {
551 3127: QualType Ty = E->getType();
3028: branch 2 taken
99: branch 3 taken
552 3127: if (Ty->isFunctionPointerType())
553 3028: Ty = Ty->getAs<PointerType>()->getPointeeType();
99: branch 2 taken
0: branch 3 not taken
554 99: else if (Ty->isBlockPointerType())
555 99: Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
556 :
557 3127: const FunctionType *FT = Ty->getAs<FunctionType>();
3127: branch 0 taken
0: branch 1 not taken
558 3127: if (FT) {
2579: branch 1 taken
548: branch 2 taken
559 3127: if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
4: branch 1 taken
2575: branch 2 taken
560 2579: if (Proto->hasEmptyExceptionSpec())
561 4: return false;
562 : }
563 3123: return true;
564 : }
565 :
566 3127: CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
567 : // If this is a call to a no-return function, this stops the block here.
568 3127: bool NoReturn = false;
95: branch 3 taken
3032: branch 4 taken
569 3127: if (C->getCallee()->getType().getNoReturnAttr()) {
570 95: NoReturn = true;
571 : }
572 :
573 3127: bool AddEHEdge = false;
574 :
575 : // Languages without exceptions are assumed to not throw.
251: branch 1 taken
2876: branch 2 taken
576 3127: if (Context->getLangOptions().Exceptions) {
0: branch 0 not taken
251: branch 1 taken
577 251: if (AddEHEdges)
578 0: AddEHEdge = true;
579 : }
580 :
2977: branch 1 taken
150: branch 2 taken
581 3127: if (FunctionDecl *FD = C->getDirectCallee()) {
0: branch 1 not taken
2977: branch 2 taken
582 2977: if (FD->hasAttr<NoReturnAttr>())
583 0: NoReturn = true;
132: branch 1 taken
2845: branch 2 taken
584 2977: if (FD->hasAttr<NoThrowAttr>())
585 132: AddEHEdge = false;
586 : }
587 :
4: branch 2 taken
3123: branch 3 taken
588 3127: if (!CanThrow(C->getCallee()))
589 4: AddEHEdge = false;
590 :
3032: branch 0 taken
95: branch 1 taken
3032: branch 2 taken
0: branch 3 not taken
591 3127: if (!NoReturn && !AddEHEdge)
592 3032: return VisitStmt(C, asc);
593 :
51: branch 0 taken
44: branch 1 taken
594 95: if (Block) {
595 51: Succ = Block;
0: branch 1 not taken
51: branch 2 taken
596 51: if (!FinishBlock(Block))
597 0: return 0;
598 : }
599 :
600 95: Block = createBlock(!NoReturn);
601 95: AppendStmt(Block, C, asc);
602 :
95: branch 0 taken
0: branch 1 not taken
603 95: if (NoReturn) {
604 : // Wire this to the exit block directly.
605 95: AddSuccessor(Block, &cfg->getExit());
606 : }
0: branch 0 not taken
95: branch 1 taken
607 95: if (AddEHEdge) {
608 : // Add exceptional edges.
0: branch 0 not taken
0: branch 1 not taken
609 0: if (TryTerminatedBlock)
610 0: AddSuccessor(Block, TryTerminatedBlock);
611 : else
612 0: AddSuccessor(Block, &cfg->getExit());
613 : }
614 :
615 95: return VisitChildren(C);
616 : }
617 :
618 : CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
619 10: AddStmtChoice asc) {
10: branch 0 taken
0: branch 1 not taken
620 10: CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
621 10: AppendStmt(ConfluenceBlock, C, asc);
0: branch 1 not taken
10: branch 2 taken
622 10: if (!FinishBlock(ConfluenceBlock))
623 0: return 0;
624 :
625 10: Succ = ConfluenceBlock;
626 10: Block = NULL;
627 10: CFGBlock* LHSBlock = addStmt(C->getLHS());
0: branch 1 not taken
10: branch 2 taken
628 10: if (!FinishBlock(LHSBlock))
629 0: return 0;
630 :
631 10: Succ = ConfluenceBlock;
632 10: Block = NULL;
633 10: CFGBlock* RHSBlock = addStmt(C->getRHS());
0: branch 1 not taken
10: branch 2 taken
634 10: if (!FinishBlock(RHSBlock))
635 0: return 0;
636 :
637 10: Block = createBlock(false);
638 : // See if this is a known constant.
639 10: const TryResult& KnownVal = TryEvaluateBool(C->getCond());
5: branch 1 taken
5: branch 2 taken
640 10: AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
5: branch 1 taken
5: branch 2 taken
641 10: AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
642 10: Block->setTerminator(C);
643 10: return addStmt(C->getCond());
644 : }
645 :
646 :
647 6434: CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
648 6434: EndScope(C);
649 :
650 6434: CFGBlock* LastBlock = Block;
651 :
13563: branch 4 taken
6426: branch 5 taken
652 19989: for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
653 : I != E; ++I ) {
654 13563: LastBlock = addStmt(*I);
655 :
8: branch 0 taken
13555: branch 1 taken
656 13563: if (badCFG)
657 8: return NULL;
658 : }
659 :
660 6426: LastBlock = StartScope(C, LastBlock);
661 :
662 6426: return LastBlock;
663 : }
664 :
665 : CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
666 230: AddStmtChoice asc) {
667 : // Create the confluence block that will "merge" the results of the ternary
668 : // expression.
218: branch 0 taken
12: branch 1 taken
669 230: CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
670 230: AppendStmt(ConfluenceBlock, C, asc);
0: branch 1 not taken
230: branch 2 taken
671 230: if (!FinishBlock(ConfluenceBlock))
672 0: return 0;
673 :
674 : // Create a block for the LHS expression if there is an LHS expression. A
675 : // GCC extension allows LHS to be NULL, causing the condition to be the
676 : // value that is returned instead.
677 : // e.g: x ?: y is shorthand for: x ? x : y;
678 230: Succ = ConfluenceBlock;
679 230: Block = NULL;
680 230: CFGBlock* LHSBlock = NULL;
212: branch 1 taken
18: branch 2 taken
681 230: if (C->getLHS()) {
682 212: LHSBlock = addStmt(C->getLHS());
0: branch 1 not taken
212: branch 2 taken
683 212: if (!FinishBlock(LHSBlock))
684 0: return 0;
685 212: Block = NULL;
686 : }
687 :
688 : // Create the block for the RHS expression.
689 230: Succ = ConfluenceBlock;
690 230: CFGBlock* RHSBlock = addStmt(C->getRHS());
0: branch 1 not taken
230: branch 2 taken
691 230: if (!FinishBlock(RHSBlock))
692 0: return 0;
693 :
694 : // Create the block that will contain the condition.
695 230: Block = createBlock(false);
696 :
697 : // See if this is a known constant.
698 230: const TryResult& KnownVal = TryEvaluateBool(C->getCond());
212: branch 0 taken
18: branch 1 taken
699 230: if (LHSBlock) {
7: branch 1 taken
205: branch 2 taken
700 212: AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
701 : } else {
5: branch 1 taken
13: branch 2 taken
702 18: if (KnownVal.isFalse()) {
703 : // If we know the condition is false, add NULL as the successor for
704 : // the block containing the condition. In this case, the confluence
705 : // block will have just one predecessor.
706 5: AddSuccessor(Block, 0);
0: branch 1 not taken
5: branch 2 taken
707 5: assert(ConfluenceBlock->pred_size() == 1);
708 : } else {
709 : // If we have no LHS expression, add the ConfluenceBlock as a direct
710 : // successor for the block containing the condition. Moreover, we need to
711 : // reverse the order of the predecessors in the ConfluenceBlock because
712 : // the RHSBlock will have been added to the succcessors already, and we
713 : // want the first predecessor to the the block containing the expression
714 : // for the case when the ternary expression evaluates to true.
715 13: AddSuccessor(Block, ConfluenceBlock);
0: branch 1 not taken
13: branch 2 taken
716 13: assert(ConfluenceBlock->pred_size() == 2);
717 : std::reverse(ConfluenceBlock->pred_begin(),
718 13: ConfluenceBlock->pred_end());
719 : }
720 : }
721 :
13: branch 1 taken
217: branch 2 taken
722 230: AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
723 230: Block->setTerminator(C);
724 230: return addStmt(C->getCond());
725 : }
726 :
727 3679: CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
728 3679: autoCreateBlock();
729 :
3555: branch 1 taken
124: branch 2 taken
730 3679: if (DS->isSingleDecl()) {
731 3555: AppendStmt(Block, DS);
732 3555: return VisitDeclSubExpr(DS->getSingleDecl());
733 : }
734 :
735 124: CFGBlock *B = 0;
736 :
737 : // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
738 : typedef llvm::SmallVector<Decl*,10> BufTy;
739 124: BufTy Buf(DS->decl_begin(), DS->decl_end());
740 :
250: branch 4 taken
124: branch 5 taken
741 374: for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
742 : // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
743 : unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
744 250: ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
745 :
746 : // Allocate the DeclStmt using the BumpPtrAllocator. It will get
747 : // automatically freed with the CFG.
748 250: DeclGroupRef DG(*I);
749 250: Decl *D = *I;
750 250: void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
250: branch 3 taken
0: branch 4 not taken
751 250: DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
752 :
753 : // Append the fake DeclStmt to block.
754 250: AppendStmt(Block, DSNew);
755 250: B = VisitDeclSubExpr(D);
756 : }
757 :
758 124: return B;
759 : }
760 :
761 : /// VisitDeclSubExpr - Utility method to add block-level expressions for
762 : /// initializers in Decls.
763 3805: CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
0: branch 0 not taken
3805: branch 1 taken
764 3805: assert(Block);
765 :
766 3805: VarDecl *VD = dyn_cast<VarDecl>(D);
767 :
230: branch 0 taken
3575: branch 1 taken
768 3805: if (!VD)
769 230: return Block;
770 :
771 3575: Expr *Init = VD->getInit();
772 :
2567: branch 0 taken
1008: branch 1 taken
773 3575: if (Init) {
774 : // Optimization: Don't create separate block-level statements for literals.
374: branch 1 taken
2193: branch 2 taken
775 2567: switch (Init->getStmtClass()) {
776 : case Stmt::IntegerLiteralClass:
777 : case Stmt::CharacterLiteralClass:
778 : case Stmt::StringLiteralClass:
779 374: break;
780 : default:
781 : Block = addStmt(Init,
782 : VD->getType()->isReferenceType()
783 : ? AddStmtChoice::AlwaysAddAsLValue
19: branch 3 taken
2174: branch 4 taken
784 2193: : AddStmtChoice::AlwaysAdd);
785 : }
786 : }
787 :
788 : // If the type of VD is a VLA, then we must process its size expressions.
44: branch 6 taken
3575: branch 7 taken
789 3619: for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
790 : VA = FindVA(VA->getElementType().getTypePtr()))
791 44: Block = addStmt(VA->getSizeExpr());
792 :
793 3575: return Block;
794 : }
795 :
796 1529: CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
797 : // We may see an if statement in the middle of a basic block, or it may be the
798 : // first statement we are processing. In either case, we create a new basic
799 : // block. First, we create the blocks for the then...else statements, and
800 : // then we create the block containing the if statement. If we were in the
801 : // middle of a block, we stop processing that block. That block is then the
802 : // implicit successor for the "then" and "else" clauses.
803 :
804 : // The block we were proccessing is now finished. Make it the successor
805 : // block.
1042: branch 0 taken
487: branch 1 taken
806 1529: if (Block) {
807 1042: Succ = Block;
0: branch 1 not taken
1042: branch 2 taken
808 1042: if (!FinishBlock(Block))
809 0: return 0;
810 : }
811 :
812 : // Process the false branch.
813 1529: CFGBlock* ElseBlock = Succ;
814 :
132: branch 1 taken
1397: branch 2 taken
815 1529: if (Stmt* Else = I->getElse()) {
816 132: SaveAndRestore<CFGBlock*> sv(Succ);
817 :
818 : // NULL out Block so that the recursive call to Visit will
819 : // create a new basic block.
820 132: Block = NULL;
821 132: ElseBlock = addStmt(Else);
822 :
0: branch 0 not taken
132: branch 1 taken
823 132: if (!ElseBlock) // Can occur when the Else body has all NullStmts.
824 0: ElseBlock = sv.get();
132: branch 0 taken
0: branch 1 not taken
825 132: else if (Block) {
0: branch 1 not taken
132: branch 2 taken
826 132: if (!FinishBlock(ElseBlock))
827 0: return 0;
132: branch 1 taken
0: branch 2 not taken
828 132: }
829 : }
830 :
831 : // Process the true branch.
832 : CFGBlock* ThenBlock;
833 : {
834 1529: Stmt* Then = I->getThen();
0: branch 0 not taken
1529: branch 1 taken
835 1529: assert(Then);
836 1529: SaveAndRestore<CFGBlock*> sv(Succ);
837 1529: Block = NULL;
838 1529: ThenBlock = addStmt(Then);
839 :
67: branch 0 taken
1462: branch 1 taken
840 1529: if (!ThenBlock) {
841 : // We can reach here if the "then" body has all NullStmts.
842 : // Create an empty block so we can distinguish between true and false
843 : // branches in path-sensitive analyses.
844 67: ThenBlock = createBlock(false);
845 67: AddSuccessor(ThenBlock, sv.get());
1453: branch 0 taken
9: branch 1 taken
846 1462: } else if (Block) {
0: branch 1 not taken
1453: branch 2 taken
847 1453: if (!FinishBlock(ThenBlock))
848 0: return 0;
1529: branch 1 taken
0: branch 2 not taken
849 1529: }
850 : }
851 :
852 : // Now create a new block containing the if statement.
853 1529: Block = createBlock(false);
854 :
855 : // Set the terminator of the new block to the If statement.
856 1529: Block->setTerminator(I);
857 :
858 : // See if this is a known constant.
859 1529: const TryResult &KnownVal = TryEvaluateBool(I->getCond());
860 :
861 : // Now add the successors.
23: branch 1 taken
1506: branch 2 taken
862 1552: AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
72: branch 1 taken
1457: branch 2 taken
863 1529: AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
864 :
865 : // Add the condition as the last statement in the new block. This may create
866 : // new blocks as the condition may contain control-flow. Any newly created
867 : // blocks will be pointed to be "Block".
868 1529: Block = addStmt(I->getCond());
869 :
870 : // Finally, if the IfStmt contains a condition variable, add both the IfStmt
871 : // and the condition variable initialization to the CFG.
11: branch 1 taken
1518: branch 2 taken
872 1529: if (VarDecl *VD = I->getConditionVariable()) {
11: branch 1 taken
0: branch 2 not taken
873 11: if (Expr *Init = VD->getInit()) {
874 11: autoCreateBlock();
875 11: AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
876 11: addStmt(Init);
877 : }
878 : }
879 :
880 1529: return Block;
881 : }
882 :
883 :
884 4965: CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
885 : // If we were in the middle of a block we stop processing that block.
886 : //
887 : // NOTE: If a "return" appears in the middle of a block, this means that the
888 : // code afterwards is DEAD (unreachable). We still keep a basic block
889 : // for that code; a simple "mark-and-sweep" from the entry block will be
890 : // able to report such dead blocks.
94: branch 0 taken
4871: branch 1 taken
891 4965: if (Block)
892 94: FinishBlock(Block);
893 :
894 : // Create the new block.
895 4965: Block = createBlock(false);
896 :
897 : // The Exit block is the only successor.
898 4965: AddSuccessor(Block, &cfg->getExit());
899 :
900 : // Add the return statement to the block. This may create new blocks if R
901 : // contains control-flow (short-circuit operations).
902 4965: return VisitStmt(R, AddStmtChoice::AlwaysAdd);
903 : }
904 :
905 66: CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
906 : // Get the block of the labeled statement. Add it to our map.
907 66: addStmt(L->getSubStmt());
908 66: CFGBlock* LabelBlock = Block;
909 :
3: branch 0 taken
63: branch 1 taken
910 66: if (!LabelBlock) // This can happen when the body is empty, i.e.
911 3: LabelBlock = createBlock(); // scopes that only contains NullStmts.
912 :
66: branch 4 taken
0: branch 5 not taken
913 66: assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
914 66: LabelMap[ L ] = LabelBlock;
915 :
916 : // Labels partition blocks, so this is the end of the basic block we were
917 : // processing (L is the block's label). Because this is label (and we have
918 : // already processed the substatement) there is no extra control-flow to worry
919 : // about.
920 66: LabelBlock->setLabel(L);
0: branch 1 not taken
66: branch 2 taken
921 66: if (!FinishBlock(LabelBlock))
922 0: return 0;
923 :
924 : // We set Block to NULL to allow lazy creation of a new block (if necessary);
925 66: Block = NULL;
926 :
927 : // This block is now the implicit successor of other blocks.
928 66: Succ = LabelBlock;
929 :
930 66: return LabelBlock;
931 : }
932 :
933 30: CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
934 : // Goto is a control-flow statement. Thus we stop processing the current
935 : // block and create a new one.
6: branch 0 taken
24: branch 1 taken
936 30: if (Block)
937 6: FinishBlock(Block);
938 :
939 30: Block = createBlock(false);
940 30: Block->setTerminator(G);
941 :
942 : // If we already know the mapping to the label block add the successor now.
943 30: LabelMapTy::iterator I = LabelMap.find(G->getLabel());
944 :
14: branch 3 taken
16: branch 4 taken
945 30: if (I == LabelMap.end())
946 : // We will need to backpatch this block later.
947 14: BackpatchBlocks.push_back(Block);
948 : else
949 16: AddSuccessor(Block, I->second);
950 :
951 30: return Block;
952 : }
953 :
954 150: CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
955 150: CFGBlock* LoopSuccessor = NULL;
956 :
957 : // "for" is a control-flow statement. Thus we stop processing the current
958 : // block.
118: branch 0 taken
32: branch 1 taken
959 150: if (Block) {
0: branch 1 not taken
118: branch 2 taken
960 118: if (!FinishBlock(Block))
961 0: return 0;
962 118: LoopSuccessor = Block;
963 : } else
964 32: LoopSuccessor = Succ;
965 :
966 : // Because of short-circuit evaluation, the condition of the loop can span
967 : // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
968 : // evaluate the condition.
969 150: CFGBlock* ExitConditionBlock = createBlock(false);
970 150: CFGBlock* EntryConditionBlock = ExitConditionBlock;
971 :
972 : // Set the terminator for the "exit" condition block.
973 150: ExitConditionBlock->setTerminator(F);
974 :
975 : // Now add the actual condition to the condition block. Because the condition
976 : // itself may contain control-flow, new blocks may be created.
141: branch 1 taken
9: branch 2 taken
977 150: if (Stmt* C = F->getCond()) {
978 141: Block = ExitConditionBlock;
979 141: EntryConditionBlock = addStmt(C);
0: branch 0 not taken
141: branch 1 taken
980 141: assert(Block == EntryConditionBlock);
981 :
982 : // If this block contains a condition variable, add both the condition
983 : // variable and initializer to the CFG.
4: branch 1 taken
137: branch 2 taken
984 141: if (VarDecl *VD = F->getConditionVariable()) {
4: branch 1 taken
0: branch 2 not taken
985 4: if (Expr *Init = VD->getInit()) {
986 4: autoCreateBlock();
987 4: AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
988 4: EntryConditionBlock = addStmt(Init);
0: branch 0 not taken
4: branch 1 taken
989 4: assert(Block == EntryConditionBlock);
990 : }
991 : }
992 :
141: branch 0 taken
0: branch 1 not taken
993 141: if (Block) {
0: branch 1 not taken
141: branch 2 taken
994 141: if (!FinishBlock(EntryConditionBlock))
995 0: return 0;
996 : }
997 : }
998 :
999 : // The condition block is the implicit successor for the loop body as well as
1000 : // any code above the loop.
1001 150: Succ = EntryConditionBlock;
1002 :
1003 : // See if this is a known constant.
1004 150: TryResult KnownVal(true);
1005 :
141: branch 1 taken
9: branch 2 taken
1006 150: if (F->getCond())
1007 141: KnownVal = TryEvaluateBool(F->getCond());
1008 :
1009 : // Now create the loop body.
1010 : {
0: branch 1 not taken
150: branch 2 taken
1011 150: assert(F->getBody());
1012 :
1013 : // Save the current values for Block, Succ, and continue and break targets
1014 150: SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1015 150: save_continue(ContinueTargetBlock),
1016 150: save_break(BreakTargetBlock);
1017 :
1018 : // Create a new block to contain the (bottom) of the loop body.
1019 150: Block = NULL;
1020 :
129: branch 1 taken
21: branch 2 taken
1021 150: if (Stmt* I = F->getInc()) {
1022 : // Generate increment code in its own basic block. This is the target of
1023 : // continue statements.
1024 129: Succ = addStmt(I);
1025 : } else {
1026 : // No increment code. Create a special, empty, block that is used as the
1027 : // target block for "looping back" to the start of the loop.
0: branch 0 not taken
21: branch 1 taken
1028 21: assert(Succ == EntryConditionBlock);
1029 21: Succ = createBlock();
1030 : }
1031 :
1032 : // Finish up the increment (or empty) block if it hasn't been already.
129: branch 0 taken
21: branch 1 taken
1033 150: if (Block) {
0: branch 0 not taken
129: branch 1 taken
1034 129: assert(Block == Succ);
0: branch 1 not taken
129: branch 2 taken
1035 129: if (!FinishBlock(Block))
1036 0: return 0;
1037 129: Block = 0;
1038 : }
1039 :
1040 150: ContinueTargetBlock = Succ;
1041 :
1042 : // The starting block for the loop increment is the block that should
1043 : // represent the 'loop target' for looping back to the start of the loop.
1044 150: ContinueTargetBlock->setLoopTarget(F);
1045 :
1046 : // All breaks should go to the code following the loop.
1047 150: BreakTargetBlock = LoopSuccessor;
1048 :
1049 : // Now populate the body block, and in the process create new blocks as we
1050 : // walk the body of the loop.
1051 150: CFGBlock* BodyBlock = addStmt(F->getBody());
1052 :
10: branch 0 taken
140: branch 1 taken
1053 150: if (!BodyBlock)
1054 10: BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
136: branch 0 taken
4: branch 1 taken
0: branch 3 not taken
136: branch 4 taken
0: branch 5 not taken
140: branch 6 taken
1055 140: else if (Block && !FinishBlock(BodyBlock))
1056 0: return 0;
1057 :
1058 : // This new body block is a successor to our "exit" condition block.
6: branch 1 taken
144: branch 2 taken
150: branch 5 taken
0: branch 6 not taken
150: branch 8 taken
0: branch 9 not taken
150: branch 11 taken
0: branch 12 not taken
150: branch 14 taken
0: branch 15 not taken
1059 150: AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
1060 : }
1061 :
1062 : // Link up the condition block with the code that follows the loop. (the
1063 : // false branch).
15: branch 1 taken
135: branch 2 taken
1064 165: AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
1065 :
1066 : // If the loop contains initialization, create a new block for those
1067 : // statements. This block can also contain statements that precede the loop.
130: branch 1 taken
20: branch 2 taken
1068 150: if (Stmt* I = F->getInit()) {
1069 130: Block = createBlock();
1070 130: return addStmt(I);
1071 : } else {
1072 : // There is no loop initialization. We are thus basically a while loop.
1073 : // NULL out Block to force lazy block construction.
1074 20: Block = NULL;
1075 20: Succ = EntryConditionBlock;
1076 20: return EntryConditionBlock;
1077 : }
1078 : }
1079 :
1080 20: CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1081 : // Objective-C fast enumeration 'for' statements:
1082 : // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1083 : //
1084 : // for ( Type newVariable in collection_expression ) { statements }
1085 : //
1086 : // becomes:
1087 : //
1088 : // prologue:
1089 : // 1. collection_expression
1090 : // T. jump to loop_entry
1091 : // loop_entry:
1092 : // 1. side-effects of element expression
1093 : // 1. ObjCForCollectionStmt [performs binding to newVariable]
1094 : // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1095 : // TB:
1096 : // statements
1097 : // T. jump to loop_entry
1098 : // FB:
1099 : // what comes after
1100 : //
1101 : // and
1102 : //
1103 : // Type existingItem;
1104 : // for ( existingItem in expression ) { statements }
1105 : //
1106 : // becomes:
1107 : //
1108 : // the same with newVariable replaced with existingItem; the binding works
1109 : // the same except that for one ObjCForCollectionStmt::getElement() returns
1110 : // a DeclStmt and the other returns a DeclRefExpr.
1111 : //
1112 :
1113 20: CFGBlock* LoopSuccessor = 0;
1114 :
9: branch 0 taken
11: branch 1 taken
1115 20: if (Block) {
0: branch 1 not taken
9: branch 2 taken
1116 9: if (!FinishBlock(Block))
1117 0: return 0;
1118 9: LoopSuccessor = Block;
1119 9: Block = 0;
1120 : } else
1121 11: LoopSuccessor = Succ;
1122 :
1123 : // Build the condition blocks.
1124 20: CFGBlock* ExitConditionBlock = createBlock(false);
1125 20: CFGBlock* EntryConditionBlock = ExitConditionBlock;
1126 :
1127 : // Set the terminator for the "exit" condition block.
1128 20: ExitConditionBlock->setTerminator(S);
1129 :
1130 : // The last statement in the block should be the ObjCForCollectionStmt, which
1131 : // performs the actual binding to 'element' and determines if there are any
1132 : // more items in the collection.
1133 20: AppendStmt(ExitConditionBlock, S);
1134 20: Block = ExitConditionBlock;
1135 :
1136 : // Walk the 'element' expression to see if there are any side-effects. We
1137 : // generate new blocks as necesary. We DON'T add the statement by default to
1138 : // the CFG unless it contains control-flow.
1139 20: EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
20: branch 0 taken
0: branch 1 not taken
1140 20: if (Block) {
0: branch 1 not taken
20: branch 2 taken
1141 20: if (!FinishBlock(EntryConditionBlock))
1142 0: return 0;
1143 20: Block = 0;
1144 : }
1145 :
1146 : // The condition block is the implicit successor for the loop body as well as
1147 : // any code above the loop.
1148 20: Succ = EntryConditionBlock;
1149 :
1150 : // Now create the true branch.
1151 : {
1152 : // Save the current values for Succ, continue and break targets.
1153 20: SaveAndRestore<CFGBlock*> save_Succ(Succ),
1154 20: save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1155 :
1156 20: BreakTargetBlock = LoopSuccessor;
1157 20: ContinueTargetBlock = EntryConditionBlock;
1158 :
1159 20: CFGBlock* BodyBlock = addStmt(S->getBody());
1160 :
8: branch 0 taken
12: branch 1 taken
1161 20: if (!BodyBlock)
1162 8: BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
12: branch 0 taken
0: branch 1 not taken
1163 12: else if (Block) {
0: branch 1 not taken
12: branch 2 taken
1164 12: if (!FinishBlock(BodyBlock))
1165 0: return 0;
1166 : }
1167 :
1168 : // This new body block is a successor to our "exit" condition block.
20: branch 2 taken
0: branch 3 not taken
20: branch 5 taken
0: branch 6 not taken
20: branch 8 taken
0: branch 9 not taken
1169 20: AddSuccessor(ExitConditionBlock, BodyBlock);
1170 : }
1171 :
1172 : // Link up the condition block with the code that follows the loop.
1173 : // (the false branch).
1174 20: AddSuccessor(ExitConditionBlock, LoopSuccessor);
1175 :
1176 : // Now create a prologue block to contain the collection expression.
1177 20: Block = createBlock();
1178 20: return addStmt(S->getCollection());
1179 : }
1180 :
1181 5: CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1182 : // FIXME: Add locking 'primitives' to CFG for @synchronized.
1183 :
1184 : // Inline the body.
1185 5: CFGBlock *SyncBlock = addStmt(S->getSynchBody());
1186 :
1187 : // The sync body starts its own basic block. This makes it a little easier
1188 : // for diagnostic clients.
5: branch 0 taken
0: branch 1 not taken
1189 5: if (SyncBlock) {
0: branch 1 not taken
5: branch 2 taken
1190 5: if (!FinishBlock(SyncBlock))
1191 0: return 0;
1192 :
1193 5: Block = 0;
1194 : }
1195 :
1196 5: Succ = SyncBlock;
1197 :
1198 : // Inline the sync expression.
1199 5: return addStmt(S->getSynchExpr());
1200 : }
1201 :
1202 8: CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
1203 : // FIXME
1204 8: return NYS();
1205 : }
1206 :
1207 118: CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
1208 118: CFGBlock* LoopSuccessor = NULL;
1209 :
1210 : // "while" is a control-flow statement. Thus we stop processing the current
1211 : // block.
27: branch 0 taken
91: branch 1 taken
1212 118: if (Block) {
0: branch 1 not taken
27: branch 2 taken
1213 27: if (!FinishBlock(Block))
1214 0: return 0;
1215 27: LoopSuccessor = Block;
1216 : } else
1217 91: LoopSuccessor = Succ;
1218 :
1219 : // Because of short-circuit evaluation, the condition of the loop can span
1220 : // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1221 : // evaluate the condition.
1222 118: CFGBlock* ExitConditionBlock = createBlock(false);
1223 118: CFGBlock* EntryConditionBlock = ExitConditionBlock;
1224 :
1225 : // Set the terminator for the "exit" condition block.
1226 118: ExitConditionBlock->setTerminator(W);
1227 :
1228 : // Now add the actual condition to the condition block. Because the condition
1229 : // itself may contain control-flow, new blocks may be created. Thus we update
1230 : // "Succ" after adding the condition.
118: branch 1 taken
0: branch 2 not taken
1231 118: if (Stmt* C = W->getCond()) {
1232 118: Block = ExitConditionBlock;
1233 118: EntryConditionBlock = addStmt(C);
0: branch 0 not taken
118: branch 1 taken
1234 118: assert(Block == EntryConditionBlock);
1235 :
1236 : // If this block contains a condition variable, add both the condition
1237 : // variable and initializer to the CFG.
6: branch 1 taken
112: branch 2 taken
1238 118: if (VarDecl *VD = W->getConditionVariable()) {
6: branch 1 taken
0: branch 2 not taken
1239 6: if (Expr *Init = VD->getInit()) {
1240 6: autoCreateBlock();
1241 6: AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1242 6: EntryConditionBlock = addStmt(Init);
0: branch 0 not taken
6: branch 1 taken
1243 6: assert(Block == EntryConditionBlock);
1244 : }
1245 : }
1246 :
118: branch 0 taken
0: branch 1 not taken
1247 118: if (Block) {
0: branch 1 not taken
118: branch 2 taken
1248 118: if (!FinishBlock(EntryConditionBlock))
1249 0: return 0;
1250 : }
1251 : }
1252 :
1253 : // The condition block is the implicit successor for the loop body as well as
1254 : // any code above the loop.
1255 118: Succ = EntryConditionBlock;
1256 :
1257 : // See if this is a known constant.
1258 118: const TryResult& KnownVal = TryEvaluateBool(W->getCond());
1259 :
1260 : // Process the loop body.
1261 : {
0: branch 1 not taken
118: branch 2 taken
1262 118: assert(W->getBody());
1263 :
1264 : // Save the current values for Block, Succ, and continue and break targets
1265 118: SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1266 118: save_continue(ContinueTargetBlock),
1267 118: save_break(BreakTargetBlock);
1268 :
1269 : // Create an empty block to represent the transition block for looping back
1270 : // to the head of the loop.
1271 118: Block = 0;
0: branch 0 not taken
118: branch 1 taken
1272 118: assert(Succ == EntryConditionBlock);
1273 118: Succ = createBlock();
1274 118: Succ->setLoopTarget(W);
1275 118: ContinueTargetBlock = Succ;
1276 :
1277 : // All breaks should go to the code following the loop.
1278 118: BreakTargetBlock = LoopSuccessor;
1279 :
1280 : // NULL out Block to force lazy instantiation of blocks for the body.
1281 118: Block = NULL;
1282 :
1283 : // Create the body. The returned block is the entry to the loop body.
1284 118: CFGBlock* BodyBlock = addStmt(W->getBody());
1285 :
63: branch 0 taken
55: branch 1 taken
1286 118: if (!BodyBlock)
1287 63: BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
55: branch 0 taken
0: branch 1 not taken
1288 55: else if (Block) {
0: branch 1 not taken
55: branch 2 taken
1289 55: if (!FinishBlock(BodyBlock))
1290 0: return 0;
1291 : }
1292 :
1293 : // Add the loop body entry as a successor to the condition.
7: branch 1 taken
111: branch 2 taken
118: branch 5 taken
0: branch 6 not taken
118: branch 8 taken
0: branch 9 not taken
118: branch 11 taken
0: branch 12 not taken
118: branch 14 taken
0: branch 15 not taken
1294 118: AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
1295 : }
1296 :
1297 : // Link up the condition block with the code that follows the loop. (the
1298 : // false branch).
92: branch 1 taken
26: branch 2 taken
1299 210: AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
1300 :
1301 : // There can be no more statements in the condition block since we loop back
1302 : // to this block. NULL out Block to force lazy creation of another block.
1303 118: Block = NULL;
1304 :
1305 : // Return the condition block, which is the dominating block for the loop.
1306 118: Succ = EntryConditionBlock;
1307 118: return EntryConditionBlock;
1308 : }
1309 :
1310 :
1311 0: CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1312 : // FIXME: For now we pretend that @catch and the code it contains does not
1313 : // exit.
1314 0: return Block;
1315 : }
1316 :
1317 2: CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1318 : // FIXME: This isn't complete. We basically treat @throw like a return
1319 : // statement.
1320 :
1321 : // If we were in the middle of a block we stop processing that block.
0: branch 0 not taken
2: branch 1 taken
0: branch 3 not taken
0: branch 4 not taken
0: branch 5 not taken
2: branch 6 taken
1322 2: if (Block && !FinishBlock(Block))
1323 0: return 0;
1324 :
1325 : // Create the new block.
1326 2: Block = createBlock(false);
1327 :
1328 : // The Exit block is the only successor.
1329 2: AddSuccessor(Block, &cfg->getExit());
1330 :
1331 : // Add the statement to the block. This may create new blocks if S contains
1332 : // control-flow (short-circuit operations).
1333 2: return VisitStmt(S, AddStmtChoice::AlwaysAdd);
1334 : }
1335 :
1336 9: CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1337 : // If we were in the middle of a block we stop processing that block.
7: branch 0 taken
2: branch 1 taken
0: branch 3 not taken
7: branch 4 taken
0: branch 5 not taken
9: branch 6 taken
1338 9: if (Block && !FinishBlock(Block))
1339 0: return 0;
1340 :
1341 : // Create the new block.
1342 9: Block = createBlock(false);
1343 :
0: branch 0 not taken
9: branch 1 taken
1344 9: if (TryTerminatedBlock)
1345 : // The current try statement is the only successor.
1346 0: AddSuccessor(Block, TryTerminatedBlock);
1347 : else
1348 : // otherwise the Exit block is the only successor.
1349 9: AddSuccessor(Block, &cfg->getExit());
1350 :
1351 : // Add the statement to the block. This may create new blocks if S contains
1352 : // control-flow (short-circuit operations).
1353 9: return VisitStmt(T, AddStmtChoice::AlwaysAdd);
1354 : }
1355 :
1356 51: CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
1357 51: CFGBlock* LoopSuccessor = NULL;
1358 :
1359 : // "do...while" is a control-flow statement. Thus we stop processing the
1360 : // current block.
43: branch 0 taken
8: branch 1 taken
1361 51: if (Block) {
0: branch 1 not taken
43: branch 2 taken
1362 43: if (!FinishBlock(Block))
1363 0: return 0;
1364 43: LoopSuccessor = Block;
1365 : } else
1366 8: LoopSuccessor = Succ;
1367 :
1368 : // Because of short-circuit evaluation, the condition of the loop can span
1369 : // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1370 : // evaluate the condition.
1371 51: CFGBlock* ExitConditionBlock = createBlock(false);
1372 51: CFGBlock* EntryConditionBlock = ExitConditionBlock;
1373 :
1374 : // Set the terminator for the "exit" condition block.
1375 51: ExitConditionBlock->setTerminator(D);
1376 :
1377 : // Now add the actual condition to the condition block. Because the condition
1378 : // itself may contain control-flow, new blocks may be created.
51: branch 1 taken
0: branch 2 not taken
1379 51: if (Stmt* C = D->getCond()) {
1380 51: Block = ExitConditionBlock;
1381 51: EntryConditionBlock = addStmt(C);
51: branch 0 taken
0: branch 1 not taken
1382 51: if (Block) {
0: branch 1 not taken
51: branch 2 taken
1383 51: if (!FinishBlock(EntryConditionBlock))
1384 0: return 0;
1385 : }
1386 : }
1387 :
1388 : // The condition block is the implicit successor for the loop body.
1389 51: Succ = EntryConditionBlock;
1390 :
1391 : // See if this is a known constant.
1392 51: const TryResult &KnownVal = TryEvaluateBool(D->getCond());
1393 :
1394 : // Process the loop body.
1395 51: CFGBlock* BodyBlock = NULL;
1396 : {
0: branch 1 not taken
51: branch 2 taken
1397 51: assert(D->getBody());
1398 :
1399 : // Save the current values for Block, Succ, and continue and break targets
1400 51: SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1401 51: save_continue(ContinueTargetBlock),
1402 51: save_break(BreakTargetBlock);
1403 :
1404 : // All continues within this loop should go to the condition block
1405 51: ContinueTargetBlock = EntryConditionBlock;
1406 :
1407 : // All breaks should go to the code following the loop.
1408 51: BreakTargetBlock = LoopSuccessor;
1409 :
1410 : // NULL out Block to force lazy instantiation of blocks for the body.
1411 51: Block = NULL;
1412 :
1413 : // Create the body. The returned block is the entry to the loop body.
1414 51: BodyBlock = addStmt(D->getBody());
1415 :
1: branch 0 taken
50: branch 1 taken
1416 51: if (!BodyBlock)
1417 1: BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
50: branch 0 taken
0: branch 1 not taken
1418 50: else if (Block) {
0: branch 1 not taken
50: branch 2 taken
1419 50: if (!FinishBlock(BodyBlock))
1420 0: return 0;
1421 : }
1422 :
1423 : // Add an intermediate block between the BodyBlock and the
1424 : // ExitConditionBlock to represent the "loop back" transition. Create an
1425 : // empty block to represent the transition block for looping back to the
1426 : // head of the loop.
1427 : // FIXME: Can we do this more efficiently without adding another block?
1428 51: Block = NULL;
1429 51: Succ = BodyBlock;
1430 51: CFGBlock *LoopBackBlock = createBlock();
1431 51: LoopBackBlock->setLoopTarget(D);
1432 :
1433 : // Add the loop body entry as a successor to the condition.
33: branch 1 taken
18: branch 2 taken
51: branch 5 taken
0: branch 6 not taken
51: branch 8 taken
0: branch 9 not taken
51: branch 11 taken
0: branch 12 not taken
51: branch 14 taken
0: branch 15 not taken
1434 51: AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
1435 : }
1436 :
1437 : // Link up the condition block with the code that follows the loop.
1438 : // (the false branch).
18: branch 1 taken
33: branch 2 taken
1439 69: AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
1440 :
1441 : // There can be no more statements in the body block(s) since we loop back to
1442 : // the body. NULL out Block to force lazy creation of another block.
1443 51: Block = NULL;
1444 :
1445 : // Return the loop body, which is the dominating block for the loop.
1446 51: Succ = BodyBlock;
1447 51: return BodyBlock;
1448 : }
1449 :
1450 15: CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1451 : // "continue" is a control-flow statement. Thus we stop processing the
1452 : // current block.
1: branch 0 taken
14: branch 1 taken
0: branch 3 not taken
1: branch 4 taken
0: branch 5 not taken
15: branch 6 taken
1453 15: if (Block && !FinishBlock(Block))
1454 0: return 0;
1455 :
1456 : // Now create a new block that ends with the continue statement.
1457 15: Block = createBlock(false);
1458 15: Block->setTerminator(C);
1459 :
1460 : // If there is no target for the continue, then we are looking at an
1461 : // incomplete AST. This means the CFG cannot be constructed.
15: branch 0 taken
0: branch 1 not taken
1462 15: if (ContinueTargetBlock)
1463 15: AddSuccessor(Block, ContinueTargetBlock);
1464 : else
1465 0: badCFG = true;
1466 :
1467 15: return Block;
1468 : }
1469 :
1470 : CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1471 125: AddStmtChoice asc) {
1472 :
10: branch 1 taken
115: branch 2 taken
1473 125: if (asc.alwaysAdd()) {
1474 10: autoCreateBlock();
1475 10: AppendStmt(Block, E);
1476 : }
1477 :
1478 : // VLA types have expressions that must be evaluated.
39: branch 1 taken
86: branch 2 taken
1479 125: if (E->isArgumentType()) {
13: branch 6 taken
39: branch 7 taken
1480 52: for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1481 : VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1482 13: addStmt(VA->getSizeExpr());
1483 : }
1484 :
1485 125: return Block;
1486 : }
1487 :
1488 : /// VisitStmtExpr - Utility method to handle (nested) statement
1489 : /// expressions (a GCC extension).
1490 49: CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
43: branch 1 taken
6: branch 2 taken
1491 49: if (asc.alwaysAdd()) {
1492 43: autoCreateBlock();
1493 43: AppendStmt(Block, SE);
1494 : }
1495 49: return VisitCompoundStmt(SE->getSubStmt());
1496 : }
1497 :
1498 51: CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
1499 : // "switch" is a control-flow statement. Thus we stop processing the current
1500 : // block.
1501 51: CFGBlock* SwitchSuccessor = NULL;
1502 :
23: branch 0 taken
28: branch 1 taken
1503 51: if (Block) {
0: branch 1 not taken
23: branch 2 taken
1504 23: if (!FinishBlock(Block))
1505 0: return 0;
1506 23: SwitchSuccessor = Block;
1507 28: } else SwitchSuccessor = Succ;
1508 :
1509 : // Save the current "switch" context.
1510 51: SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
1511 51: save_break(BreakTargetBlock),
1512 51: save_default(DefaultCaseBlock);
1513 :
1514 : // Set the "default" case to be the block after the switch statement. If the
1515 : // switch statement contains a "default:", this value will be overwritten with
1516 : // the block for that code.
1517 51: DefaultCaseBlock = SwitchSuccessor;
1518 :
1519 : // Create a new block that will contain the switch statement.
1520 51: SwitchTerminatedBlock = createBlock(false);
1521 :
1522 : // Now process the switch body. The code after the switch is the implicit
1523 : // successor.
1524 51: Succ = SwitchSuccessor;
1525 51: BreakTargetBlock = SwitchSuccessor;
1526 :
1527 : // When visiting the body, the case statements should automatically get linked
1528 : // up to the switch. We also don't keep a pointer to the body, since all
1529 : // control-flow from the switch goes to case/default statements.
51: branch 1 taken
0: branch 2 not taken
1530 51: assert(Terminator->getBody() && "switch must contain a non-NULL body");
1531 51: Block = NULL;
1532 51: CFGBlock *BodyBlock = addStmt(Terminator->getBody());
0: branch 0 not taken
51: branch 1 taken
1533 51: if (Block) {
0: branch 1 not taken
0: branch 2 not taken
1534 0: if (!FinishBlock(BodyBlock))
1535 0: return 0;
1536 : }
1537 :
1538 : // If we have no "default:" case, the default transition is to the code
1539 : // following the switch body.
1540 51: AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
1541 :
1542 : // Add the terminator and condition in the switch block.
1543 51: SwitchTerminatedBlock->setTerminator(Terminator);
51: branch 1 taken
0: branch 2 not taken
1544 51: assert(Terminator->getCond() && "switch condition must be non-NULL");
1545 51: Block = SwitchTerminatedBlock;
1546 51: Block = addStmt(Terminator->getCond());
1547 :
1548 : // Finally, if the SwitchStmt contains a condition variable, add both the
1549 : // SwitchStmt and the condition variable initialization to the CFG.
4: branch 1 taken
47: branch 2 taken
1550 51: if (VarDecl *VD = Terminator->getConditionVariable()) {
4: branch 1 taken
0: branch 2 not taken
1551 4: if (Expr *Init = VD->getInit()) {
1552 4: autoCreateBlock();
1553 4: AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1554 4: addStmt(Init);
1555 : }
1556 : }
1557 :
1558 51: return Block;
1559 : }
1560 :
1561 171: CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
1562 : // CaseStmts are essentially labels, so they are the first statement in a
1563 : // block.
1564 :
171: branch 1 taken
0: branch 2 not taken
1565 171: if (CS->getSubStmt())
1566 171: addStmt(CS->getSubStmt());
1567 :
1568 171: CFGBlock* CaseBlock = Block;
42: branch 0 taken
129: branch 1 taken
1569 171: if (!CaseBlock)
1570 42: CaseBlock = createBlock();
1571 :
1572 : // Cases statements partition blocks, so this is the top of the basic block we
1573 : // were processing (the "case XXX:" is the label).
1574 171: CaseBlock->setLabel(CS);
1575 :
0: branch 1 not taken
171: branch 2 taken
1576 171: if (!FinishBlock(CaseBlock))
1577 0: return 0;
1578 :
1579 : // Add this block to the list of successors for the block with the switch
1580 : // statement.
0: branch 0 not taken
171: branch 1 taken
1581 171: assert(SwitchTerminatedBlock);
1582 171: AddSuccessor(SwitchTerminatedBlock, CaseBlock);
1583 :
1584 : // We set Block to NULL to allow lazy creation of a new block (if necessary)
1585 171: Block = NULL;
1586 :
1587 : // This block is now the implicit successor of other blocks.
1588 171: Succ = CaseBlock;
1589 :
1590 171: return CaseBlock;
1591 : }
1592 :
1593 28: CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
28: branch 1 taken
0: branch 2 not taken
1594 28: if (Terminator->getSubStmt())
1595 28: addStmt(Terminator->getSubStmt());
1596 :
1597 28: DefaultCaseBlock = Block;
1598 :
4: branch 0 taken
24: branch 1 taken
1599 28: if (!DefaultCaseBlock)
1600 4: DefaultCaseBlock = createBlock();
1601 :
1602 : // Default statements partition blocks, so this is the top of the basic block
1603 : // we were processing (the "default:" is the label).
1604 28: DefaultCaseBlock->setLabel(Terminator);
1605 :
0: branch 1 not taken
28: branch 2 taken
1606 28: if (!FinishBlock(DefaultCaseBlock))
1607 0: return 0;
1608 :
1609 : // Unlike case statements, we don't add the default block to the successors
1610 : // for the switch statement immediately. This is done when we finish
1611 : // processing the switch statement. This allows for the default case
1612 : // (including a fall-through to the code after the switch statement) to always
1613 : // be the last successor of a switch-terminated block.
1614 :
1615 : // We set Block to NULL to allow lazy creation of a new block (if necessary)
1616 28: Block = NULL;
1617 :
1618 : // This block is now the implicit successor of other blocks.
1619 28: Succ = DefaultCaseBlock;
1620 :
1621 28: return DefaultCaseBlock;
1622 : }
1623 :
1624 4: CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1625 : // "try"/"catch" is a control-flow statement. Thus we stop processing the
1626 : // current block.
1627 4: CFGBlock* TrySuccessor = NULL;
1628 :
4: branch 0 taken
0: branch 1 not taken
1629 4: if (Block) {
0: branch 1 not taken
4: branch 2 taken
1630 4: if (!FinishBlock(Block))
1631 0: return 0;
1632 4: TrySuccessor = Block;
1633 0: } else TrySuccessor = Succ;
1634 :
1635 4: CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
1636 :
1637 : // Create a new block that will contain the try statement.
1638 4: CFGBlock *NewTryTerminatedBlock = createBlock(false);
1639 : // Add the terminator in the try block.
1640 4: NewTryTerminatedBlock->setTerminator(Terminator);
1641 :
1642 4: bool HasCatchAll = false;
4: branch 1 taken
4: branch 2 taken
1643 8: for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1644 : // The code after the try is the implicit successor.
1645 4: Succ = TrySuccessor;
1646 4: CXXCatchStmt *CS = Terminator->getHandler(h);
0: branch 1 not taken
4: branch 2 taken
1647 4: if (CS->getExceptionDecl() == 0) {
1648 0: HasCatchAll = true;
1649 : }
1650 4: Block = NULL;
1651 4: CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
0: branch 0 not taken
4: branch 1 taken
1652 4: if (CatchBlock == 0)
1653 0: return 0;
1654 : // Add this block to the list of successors for the block with the try
1655 : // statement.
1656 4: AddSuccessor(NewTryTerminatedBlock, CatchBlock);
1657 : }
4: branch 0 taken
0: branch 1 not taken
1658 4: if (!HasCatchAll) {
0: branch 0 not taken
4: branch 1 taken
1659 4: if (PrevTryTerminatedBlock)
1660 0: AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
1661 : else
1662 4: AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
1663 : }
1664 :
1665 : // The code after the try is the implicit successor.
1666 4: Succ = TrySuccessor;
1667 :
1668 : // Save the current "try" context.
1669 4: SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1670 4: TryTerminatedBlock = NewTryTerminatedBlock;
1671 :
4: branch 1 taken
0: branch 2 not taken
1672 4: assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
1673 4: Block = NULL;
1674 4: Block = addStmt(Terminator->getTryBlock());
1675 4: return Block;
1676 : }
1677 :
1678 4: CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1679 : // CXXCatchStmt are treated like labels, so they are the first statement in a
1680 : // block.
1681 :
4: branch 1 taken
0: branch 2 not taken
1682 4: if (CS->getHandlerBlock())
1683 4: addStmt(CS->getHandlerBlock());
1684 :
1685 4: CFGBlock* CatchBlock = Block;
0: branch 0 not taken
4: branch 1 taken
1686 4: if (!CatchBlock)
1687 0: CatchBlock = createBlock();
1688 :
1689 4: CatchBlock->setLabel(CS);
1690 :
0: branch 1 not taken
4: branch 2 taken
1691 4: if (!FinishBlock(CatchBlock))
1692 0: return 0;
1693 :
1694 : // We set Block to NULL to allow lazy creation of a new block (if necessary)
1695 4: Block = NULL;
1696 :
1697 4: return CatchBlock;
1698 : }
1699 :
1700 13: CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1701 : // Lazily create the indirect-goto dispatch block if there isn't one already.
1702 13: CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1703 :
13: branch 0 taken
0: branch 1 not taken
1704 13: if (!IBlock) {
1705 13: IBlock = createBlock(false);
1706 13: cfg->setIndirectGotoBlock(IBlock);
1707 : }
1708 :
1709 : // IndirectGoto is a control-flow statement. Thus we stop processing the
1710 : // current block and create a new one.
0: branch 0 not taken
13: branch 1 taken
0: branch 3 not taken
0: branch 4 not taken
0: branch 5 not taken
13: branch 6 taken
1711 13: if (Block && !FinishBlock(Block))
1712 0: return 0;
1713 :
1714 13: Block = createBlock(false);
1715 13: Block->setTerminator(I);
1716 13: AddSuccessor(Block, IBlock);
1717 13: return addStmt(I->getTarget());
1718 : }
1719 :
1720 : } // end anonymous namespace
1721 :
1722 : /// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1723 : /// no successors or predecessors. If this is the first block created in the
1724 : /// CFG, it is automatically set to be the Entry and Exit of the CFG.
1725 21483: CFGBlock* CFG::createBlock() {
1726 21483: bool first_block = begin() == end();
1727 :
1728 : // Create the block.
1729 21483: CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
21483: branch 1 taken
0: branch 2 not taken
1730 21483: new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1731 21483: Blocks.push_back(Mem, BlkBVC);
1732 :
1733 : // If this is the first block, set it as the Entry and Exit.
5486: branch 0 taken
15997: branch 1 taken
1734 21483: if (first_block)
1735 5486: Entry = Exit = &back();
1736 :
1737 : // Return the block.
1738 21483: return &back();
1739 : }
1740 :
1741 : /// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1742 : /// CFG is returned to the caller.
1743 : CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
1744 5486: bool AddEHEdges, bool AddScopes) {
1745 5486: CFGBuilder Builder;
1746 5486: return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
1747 : }
1748 :
1749 : //===----------------------------------------------------------------------===//
1750 : // CFG: Queries for BlkExprs.
1751 : //===----------------------------------------------------------------------===//
1752 :
1753 : namespace {
1754 : typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
1755 : }
1756 :
1757 : static void FindSubExprAssignments(Stmt *S,
1758 40090: llvm::SmallPtrSet<Expr*,50>& Set) {
0: branch 0 not taken
40090: branch 1 taken
1759 40090: if (!S)
1760 0: return;
1761 :
30186: branch 4 taken
40090: branch 5 taken
1762 70276: for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1763 30186: Stmt *child = *I;
26: branch 0 taken
30160: branch 1 taken
1764 30186: if (!child)
1765 26: continue;
1766 :
1013: branch 1 taken
29147: branch 2 taken
1767 30160: if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
77: branch 1 taken
936: branch 2 taken
1768 1013: if (B->isAssignmentOp()) Set.insert(B);
1769 :
1770 30160: FindSubExprAssignments(child, Set);
1771 : }
1772 : }
1773 :
1774 2006: static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1775 2006: BlkExprMapTy* M = new BlkExprMapTy();
1776 :
1777 : // Look for assignments that are used as subexpressions. These are the only
1778 : // assignments that we want to *possibly* register as a block-level
1779 : // expression. Basically, if an assignment occurs both in a subexpression and
1780 : // at the block-level, it is a block-level expression.
1781 2006: llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1782 :
8924: branch 2 taken
2006: branch 3 taken
1783 10930: for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
9930: branch 4 taken
8924: branch 5 taken
1784 18854: for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
1785 9930: FindSubExprAssignments(*BI, SubExprAssignments);
1786 :
8924: branch 2 taken
2006: branch 3 taken
1787 10930: for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1788 :
1789 : // Iterate over the statements again on identify the Expr* and Stmt* at the
1790 : // block-level that are block-level expressions.
1791 :
9930: branch 4 taken
8924: branch 5 taken
1792 18854: for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
6088: branch 2 taken
3842: branch 3 taken
1793 9930: if (Expr* Exp = dyn_cast<Expr>(*BI)) {
1794 :
1995: branch 1 taken
4093: branch 2 taken
1795 6088: if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
1796 : // Assignment expressions that are not nested within another
1797 : // expression are really "statements" whose value is never used by
1798 : // another expression.
1171: branch 1 taken
824: branch 2 taken
1158: branch 4 taken
13: branch 5 taken
1158: branch 6 taken
837: branch 7 taken
1799 1995: if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
1800 1158: continue;
43: branch 1 taken
4050: branch 2 taken
1801 4093: } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1802 : // Special handling for statement expressions. The last statement in
1803 : // the statement expression is also a block-level expr.
1804 43: const CompoundStmt* C = Terminator->getSubStmt();
35: branch 1 taken
8: branch 2 taken
1805 43: if (!C->body_empty()) {
1806 35: unsigned x = M->size();
1807 35: (*M)[C->body_back()] = x;
1808 : }
1809 : }
1810 :
1811 4930: unsigned x = M->size();
1812 4930: (*M)[Exp] = x;
1813 : }
1814 :
1815 : // Look at terminators. The condition is a block-level expression.
1816 :
1817 8924: Stmt* S = (*I)->getTerminatorCondition();
1818 :
1364: branch 0 taken
7560: branch 1 taken
19: branch 6 taken
1345: branch 7 taken
19: branch 8 taken
8905: branch 9 taken
1819 8924: if (S && M->find(S) == M->end()) {
1820 19: unsigned x = M->size();
1821 19: (*M)[S] = x;
1822 : }
1823 : }
1824 :
1825 2006: return M;
1826 : }
1827 :
1828 212356: CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
0: branch 0 not taken
212356: branch 1 taken
1829 212356: assert(S != NULL);
1935: branch 0 taken
210421: branch 1 taken
1830 212356: if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1831 :
1832 212356: BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
1833 212356: BlkExprMapTy::iterator I = M->find(S);
134579: branch 3 taken
77777: branch 4 taken
1834 212356: return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
1835 : }
1836 :
1837 2067: unsigned CFG::getNumBlkExprs() {
1996: branch 0 taken
71: branch 1 taken
1838 2067: if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1839 1996: return M->size();
1840 : else {
1841 : // We assume callers interested in the number of BlkExprs will want
1842 : // the map constructed if it doesn't already exist.
1843 71: BlkExprMap = (void*) PopulateBlkExprMap(*this);
1844 71: return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1845 : }
1846 : }
1847 :
1848 : //===----------------------------------------------------------------------===//
1849 : // Cleanup: CFG dstor.
1850 : //===----------------------------------------------------------------------===//
1851 :
1852 5486: CFG::~CFG() {
2006: branch 0 taken
3480: branch 1 taken
2006: branch 4 taken
2006: branch 5 taken
1853 5486: delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1854 5486: }
1855 :
1856 : //===----------------------------------------------------------------------===//
1857 : // CFG pretty printing
1858 : //===----------------------------------------------------------------------===//
1859 :
1860 : namespace {
1861 :
1862 : class StmtPrinterHelper : public PrinterHelper {
1863 : typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1864 : StmtMapTy StmtMap;
1865 : signed CurrentBlock;
1866 : unsigned CurrentStmt;
1867 : const LangOptions &LangOpts;
1868 : public:
1869 :
1870 0: StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1871 0: : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
0: branch 2 not taken
0: branch 3 not taken
1872 0: for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1873 0: unsigned j = 1;
0: branch 6 not taken
0: branch 7 not taken
1874 0: for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
1875 : BI != BEnd; ++BI, ++j )
1876 0: StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
1877 : }
1878 0: }
1879 :
0: branch 2 not taken
0: branch 3 not taken
0: branch 7 not taken
0: branch 8 not taken
1880 0: virtual ~StmtPrinterHelper() {}
1881 :
1882 0: const LangOptions &getLangOpts() const { return LangOpts; }
1883 0: void setBlockID(signed i) { CurrentBlock = i; }
1884 0: void setStmtID(unsigned i) { CurrentStmt = i; }
1885 :
1886 0: virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
1887 :
1888 0: StmtMapTy::iterator I = StmtMap.find(Terminator);
1889 :
0: branch 3 not taken
0: branch 4 not taken
1890 0: if (I == StmtMap.end())
1891 0: return false;
1892 :
0: branch 0 not taken
0: branch 1 not taken
0: branch 3 not taken
0: branch 4 not taken
0: branch 6 not taken
0: branch 7 not taken
0: branch 8 not taken
0: branch 9 not taken
1893 0: if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1894 : && I->second.second == CurrentStmt) {
1895 0: return false;
1896 : }
1897 :
1898 0: OS << "[B" << I->second.first << "." << I->second.second << "]";
1899 0: return true;
1900 : }
1901 : };
1902 : } // end anonymous namespace
1903 :
1904 :
1905 : namespace {
1906 : class CFGBlockTerminatorPrint
1907 : : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1908 :
1909 : llvm::raw_ostream& OS;
1910 : StmtPrinterHelper* Helper;
1911 : PrintingPolicy Policy;
1912 : public:
1913 : CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
1914 0: const PrintingPolicy &Policy)
1915 0: : OS(os), Helper(helper), Policy(Policy) {}
1916 :
1917 0: void VisitIfStmt(IfStmt* I) {
1918 0: OS << "if ";
1919 0: I->getCond()->printPretty(OS,Helper,Policy);
1920 0: }
1921 :
1922 : // Default case.
1923 0: void VisitStmt(Stmt* Terminator) {
1924 0: Terminator->printPretty(OS, Helper, Policy);
1925 0: }
1926 :
1927 0: void VisitForStmt(ForStmt* F) {
1928 0: OS << "for (" ;
0: branch 1 not taken
0: branch 2 not taken
1929 0: if (F->getInit())
1930 0: OS << "...";
1931 0: OS << "; ";
0: branch 1 not taken
0: branch 2 not taken
1932 0: if (Stmt* C = F->getCond())
1933 0: C->printPretty(OS, Helper, Policy);
1934 0: OS << "; ";
0: branch 1 not taken
0: branch 2 not taken
1935 0: if (F->getInc())
1936 0: OS << "...";
1937 0: OS << ")";
1938 0: }
1939 :
1940 0: void VisitWhileStmt(WhileStmt* W) {
1941 0: OS << "while " ;
0: branch 1 not taken
0: branch 2 not taken
1942 0: if (Stmt* C = W->getCond())
1943 0: C->printPretty(OS, Helper, Policy);
1944 0: }
1945 :
1946 0: void VisitDoStmt(DoStmt* D) {
1947 0: OS << "do ... while ";
0: branch 1 not taken
0: branch 2 not taken
1948 0: if (Stmt* C = D->getCond())
1949 0: C->printPretty(OS, Helper, Policy);
1950 0: }
1951 :
1952 0: void VisitSwitchStmt(SwitchStmt* Terminator) {
1953 0: OS << "switch ";
1954 0: Terminator->getCond()->printPretty(OS, Helper, Policy);
1955 0: }
1956 :
1957 0: void VisitCXXTryStmt(CXXTryStmt* CS) {
1958 0: OS << "try ...";
1959 0: }
1960 :
1961 0: void VisitConditionalOperator(ConditionalOperator* C) {
1962 0: C->getCond()->printPretty(OS, Helper, Policy);
1963 0: OS << " ? ... : ...";
1964 0: }
1965 :
1966 0: void VisitChooseExpr(ChooseExpr* C) {
1967 0: OS << "__builtin_choose_expr( ";
1968 0: C->getCond()->printPretty(OS, Helper, Policy);
1969 0: OS << " )";
1970 0: }
1971 :
1972 0: void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1973 0: OS << "goto *";
1974 0: I->getTarget()->printPretty(OS, Helper, Policy);
1975 0: }
1976 :
1977 0: void VisitBinaryOperator(BinaryOperator* B) {
0: branch 1 not taken
0: branch 2 not taken
1978 0: if (!B->isLogicalOp()) {
1979 0: VisitExpr(B);
1980 0: return;
1981 : }
1982 :
1983 0: B->getLHS()->printPretty(OS, Helper, Policy);
1984 :
0: branch 1 not taken
0: branch 2 not taken
0: branch 3 not taken
1985 0: switch (B->getOpcode()) {
1986 : case BinaryOperator::LOr:
1987 0: OS << " || ...";
1988 0: return;
1989 : case BinaryOperator::LAnd:
1990 0: OS << " && ...";
1991 0: return;
1992 : default:
1993 0: assert(false && "Invalid logical operator.");
1994 : }
1995 : }
1996 :
1997 0: void VisitExpr(Expr* E) {
1998 0: E->printPretty(OS, Helper, Policy);
1999 0: }
2000 : };
2001 : } // end anonymous namespace
2002 :
2003 :
2004 : static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
2005 0: const CFGElement &E) {
2006 0: Stmt *Terminator = E;
2007 :
0: branch 1 not taken
0: branch 2 not taken
2008 0: if (E.asStartScope()) {
2009 0: OS << "start scope\n";
2010 0: return;
2011 : }
0: branch 1 not taken
0: branch 2 not taken
2012 0: if (E.asEndScope()) {
2013 0: OS << "end scope\n";
2014 0: return;
2015 : }
2016 :
0: branch 0 not taken
0: branch 1 not taken
2017 0: if (Helper) {
2018 : // special printing for statement-expressions.
0: branch 1 not taken
0: branch 2 not taken
2019 0: if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
2020 0: CompoundStmt* Sub = SE->getSubStmt();
2021 :
0: branch 3 not taken
0: branch 4 not taken
2022 0: if (Sub->child_begin() != Sub->child_end()) {
2023 0: OS << "({ ... ; ";
2024 0: Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2025 0: OS << " })\n";
2026 0: return;
2027 : }
2028 : }
2029 :
2030 : // special printing for comma expressions.
0: branch 1 not taken
0: branch 2 not taken
2031 0: if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
0: branch 1 not taken
0: branch 2 not taken
2032 0: if (B->getOpcode() == BinaryOperator::Comma) {
2033 0: OS << "... , ";
2034 0: Helper->handledStmt(B->getRHS(),OS);
2035 0: OS << '\n';
2036 0: return;
2037 : }
2038 : }
2039 : }
2040 :
2041 0: Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2042 :
2043 : // Expressions need a newline.
0: branch 1 not taken
0: branch 2 not taken
2044 0: if (isa<Expr>(Terminator)) OS << '\n';
2045 : }
2046 :
2047 : static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2048 : const CFGBlock& B,
2049 0: StmtPrinterHelper* Helper, bool print_edges) {
2050 :
0: branch 0 not taken
0: branch 1 not taken
2051 0: if (Helper) Helper->setBlockID(B.getBlockID());
2052 :
2053 : // Print the header.
2054 0: OS << "\n [ B" << B.getBlockID();
2055 :
0: branch 1 not taken
0: branch 2 not taken
2056 0: if (&B == &cfg->getEntry())
2057 0: OS << " (ENTRY) ]\n";
0: branch 1 not taken
0: branch 2 not taken
2058 0: else if (&B == &cfg->getExit())
2059 0: OS << " (EXIT) ]\n";
0: branch 1 not taken
0: branch 2 not taken
2060 0: else if (&B == cfg->getIndirectGotoBlock())
2061 0: OS << " (INDIRECT GOTO DISPATCH) ]\n";
2062 : else
2063 0: OS << " ]\n";
2064 :
2065 : // Print the label of this block.
0: branch 1 not taken
0: branch 2 not taken
2066 0: if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
2067 :
0: branch 0 not taken
0: branch 1 not taken
2068 0: if (print_edges)
2069 0: OS << " ";
2070 :
0: branch 1 not taken
0: branch 2 not taken
2071 0: if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
2072 0: OS << L->getName();
0: branch 1 not taken
0: branch 2 not taken
2073 0: else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
2074 0: OS << "case ";
2075 : C->getLHS()->printPretty(OS, Helper,
2076 0: PrintingPolicy(Helper->getLangOpts()));
0: branch 1 not taken
0: branch 2 not taken
2077 0: if (C->getRHS()) {
2078 0: OS << " ... ";
2079 : C->getRHS()->printPretty(OS, Helper,
2080 0: PrintingPolicy(Helper->getLangOpts()));
2081 : }
0: branch 1 not taken
0: branch 2 not taken
2082 0: } else if (isa<DefaultStmt>(Label))
2083 0: OS << "default";
0: branch 1 not taken
0: branch 2 not taken
2084 0: else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
2085 0: OS << "catch (";
0: branch 1 not taken
0: branch 2 not taken
2086 0: if (CS->getExceptionDecl())
2087 : CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2088 0: 0);
2089 : else
2090 0: OS << "...";
2091 0: OS << ")";
2092 :
2093 : } else
2094 0: assert(false && "Invalid label statement in CFGBlock.");
2095 :
2096 0: OS << ":\n";
2097 : }
2098 :
2099 : // Iterate through the statements in the block and print them.
2100 0: unsigned j = 1;
2101 :
0: branch 4 not taken
0: branch 5 not taken
2102 0: for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2103 : I != E ; ++I, ++j ) {
2104 :
2105 : // Print the statement # in the basic block and the statement itself.
0: branch 0 not taken
0: branch 1 not taken
2106 0: if (print_edges)
2107 0: OS << " ";
2108 :
2109 0: OS << llvm::format("%3d", j) << ": ";
2110 :
0: branch 0 not taken
0: branch 1 not taken
2111 0: if (Helper)
2112 0: Helper->setStmtID(j);
2113 :
2114 0: print_stmt(OS,Helper,*I);
2115 : }
2116 :
2117 : // Print the terminator of this block.
0: branch 1 not taken
0: branch 2 not taken
2118 0: if (B.getTerminator()) {
0: branch 0 not taken
0: branch 1 not taken
2119 0: if (print_edges)
2120 0: OS << " ";
2121 :
2122 0: OS << " T: ";
2123 :
0: branch 0 not taken
0: branch 1 not taken
2124 0: if (Helper) Helper->setBlockID(-1);
2125 :
2126 : CFGBlockTerminatorPrint TPrinter(OS, Helper,
2127 0: PrintingPolicy(Helper->getLangOpts()));
2128 0: TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
2129 0: OS << '\n';
2130 : }
2131 :
0: branch 0 not taken
0: branch 1 not taken
2132 0: if (print_edges) {
2133 : // Print the predecessors of this block.
2134 0: OS << " Predecessors (" << B.pred_size() << "):";
2135 0: unsigned i = 0;
2136 :
0: branch 2 not taken
0: branch 3 not taken
2137 0: for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2138 : I != E; ++I, ++i) {
2139 :
0: branch 0 not taken
0: branch 1 not taken
0: branch 2 not taken
0: branch 3 not taken
2140 0: if (i == 8 || (i-8) == 0)
2141 0: OS << "\n ";
2142 :
2143 0: OS << " B" << (*I)->getBlockID();
2144 : }
2145 :
2146 0: OS << '\n';
2147 :
2148 : // Print the successors of this block.
2149 0: OS << " Successors (" << B.succ_size() << "):";
2150 0: i = 0;
2151 :
0: branch 2 not taken
0: branch 3 not taken
2152 0: for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2153 : I != E; ++I, ++i) {
2154 :
0: branch 0 not taken
0: branch 1 not taken
0: branch 2 not taken
0: branch 3 not taken
2155 0: if (i == 8 || (i-8) % 10 == 0)
2156 0: OS << "\n ";
2157 :
0: branch 0 not taken
0: branch 1 not taken
2158 0: if (*I)
2159 0: OS << " B" << (*I)->getBlockID();
2160 : else
2161 0: OS << " NULL";
2162 : }
2163 :
2164 0: OS << '\n';
2165 : }
2166 0: }
2167 :
2168 :
2169 : /// dump - A simple pretty printer of a CFG that outputs to stderr.
2170 0: void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
2171 :
2172 : /// print - A simple pretty printer of a CFG that outputs to an ostream.
2173 0: void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2174 0: StmtPrinterHelper Helper(this, LO);
2175 :
2176 : // Print the entry block.
2177 0: print_block(OS, this, getEntry(), &Helper, true);
2178 :
2179 : // Iterate through the CFGBlocks and print them one by one.
0: branch 2 not taken
0: branch 3 not taken
2180 0: for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2181 : // Skip the entry block, because we already printed it.
0: branch 1 not taken
0: branch 2 not taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 6 not taken
0: branch 7 not taken
2182 0: if (&(**I) == &getEntry() || &(**I) == &getExit())
2183 0: continue;
2184 :
2185 0: print_block(OS, this, **I, &Helper, true);
2186 : }
2187 :
2188 : // Print the exit block.
2189 0: print_block(OS, this, getExit(), &Helper, true);
2190 0: OS.flush();
2191 0: }
2192 :
2193 : /// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
2194 0: void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2195 0: print(llvm::errs(), cfg, LO);
2196 0: }
2197 :
2198 : /// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2199 : /// Generally this will only be called from CFG::print.
2200 : void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2201 0: const LangOptions &LO) const {
2202 0: StmtPrinterHelper Helper(cfg, LO);
2203 0: print_block(OS, cfg, *this, &Helper, true);
2204 0: }
2205 :
2206 : /// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
2207 : void CFGBlock::printTerminator(llvm::raw_ostream &OS,
2208 0: const LangOptions &LO) const {
2209 0: CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
2210 0: TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2211 0: }
2212 :
2213 34184: Stmt* CFGBlock::getTerminatorCondition() {
2214 :
26654: branch 0 taken
7530: branch 1 taken
2215 34184: if (!Terminator)
2216 26654: return NULL;
2217 :
2218 7530: Expr* E = NULL;
2219 :
550: branch 1 taken
427: branch 2 taken
149: branch 3 taken
127: branch 4 taken
5030: branch 5 taken
43: branch 6 taken
32: branch 7 taken
126: branch 8 taken
645: branch 9 taken
348: branch 10 taken
53: branch 11 taken
2220 7530: switch (Terminator->getStmtClass()) {
2221 : default:
2222 550: break;
2223 :
2224 : case Stmt::ForStmtClass:
2225 427: E = cast<ForStmt>(Terminator)->getCond();
2226 427: break;
2227 :
2228 : case Stmt::WhileStmtClass:
2229 149: E = cast<WhileStmt>(Terminator)->getCond();
2230 149: break;
2231 :
2232 : case Stmt::DoStmtClass:
2233 127: E = cast<DoStmt>(Terminator)->getCond();
2234 127: break;
2235 :
2236 : case Stmt::IfStmtClass:
2237 5030: E = cast<IfStmt>(Terminator)->getCond();
2238 5030: break;
2239 :
2240 : case Stmt::ChooseExprClass:
2241 43: E = cast<ChooseExpr>(Terminator)->getCond();
2242 43: break;
2243 :
2244 : case Stmt::IndirectGotoStmtClass:
2245 32: E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2246 32: break;
2247 :
2248 : case Stmt::SwitchStmtClass:
2249 126: E = cast<SwitchStmt>(Terminator)->getCond();
2250 126: break;
2251 :
2252 : case Stmt::ConditionalOperatorClass:
2253 645: E = cast<ConditionalOperator>(Terminator)->getCond();
2254 645: break;
2255 :
2256 : case Stmt::BinaryOperatorClass: // '&&' and '||'
2257 348: E = cast<BinaryOperator>(Terminator)->getLHS();
2258 348: break;
2259 :
2260 : case Stmt::ObjCForCollectionStmtClass:
2261 53: return Terminator;
2262 : }
2263 :
6896: branch 0 taken
581: branch 1 taken
2264 7477: return E ? E->IgnoreParens() : NULL;
2265 : }
2266 :
2267 0: bool CFGBlock::hasBinaryBranchTerminator() const {
2268 :
0: branch 0 not taken
0: branch 1 not taken
2269 0: if (!Terminator)
2270 0: return false;
2271 :
2272 0: Expr* E = NULL;
2273 :
0: branch 1 not taken
0: branch 2 not taken
2274 0: switch (Terminator->getStmtClass()) {
2275 : default:
2276 0: return false;
2277 :
2278 : case Stmt::ForStmtClass:
2279 : case Stmt::WhileStmtClass:
2280 : case Stmt::DoStmtClass:
2281 : case Stmt::IfStmtClass:
2282 : case Stmt::ChooseExprClass:
2283 : case Stmt::ConditionalOperatorClass:
2284 : case Stmt::BinaryOperatorClass:
2285 0: return true;
2286 : }
2287 :
2288 : return E ? E->IgnoreParens() : NULL;
2289 : }
2290 :
2291 :
2292 : //===----------------------------------------------------------------------===//
2293 : // CFG Graphviz Visualization
2294 : //===----------------------------------------------------------------------===//
2295 :
2296 :
2297 : #ifndef NDEBUG
2298 : static StmtPrinterHelper* GraphHelper;
2299 : #endif
2300 :
2301 0: void CFG::viewCFG(const LangOptions &LO) const {
2302 : #ifndef NDEBUG
2303 0: StmtPrinterHelper H(this, LO);
2304 0: GraphHelper = &H;
2305 0: llvm::ViewGraph(this,"CFG");
2306 0: GraphHelper = NULL;
2307 : #endif
2308 0: }
2309 :
2310 : namespace llvm {
2311 : template<>
2312 : struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
2313 :
2314 0: DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2315 :
2316 0: static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
2317 :
2318 : #ifndef NDEBUG
2319 0: std::string OutSStr;
2320 0: llvm::raw_string_ostream Out(OutSStr);
2321 0: print_block(Out,Graph, *Node, GraphHelper, false);
2322 0: std::string& OutStr = Out.str();
2323 :
0: branch 1 not taken
0: branch 2 not taken
2324 0: if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2325 :
2326 : // Process string output to make it nicer...
0: branch 1 not taken
0: branch 2 not taken
2327 0: for (unsigned i = 0; i != OutStr.length(); ++i)
0: branch 1 not taken
0: branch 2 not taken
2328 0: if (OutStr[i] == '\n') { // Left justify
2329 0: OutStr[i] = '\\';
2330 0: OutStr.insert(OutStr.begin()+i+1, 'l');
2331 : }
2332 :
2333 0: return OutStr;
2334 : #else
2335 : return "";
2336 : #endif
2337 : }
2338 : };
2339 : } // end namespace llvm
Generated: 2010-02-10 01:31 by zcov