Deleted Added
full compact
CFG.cpp (200583) CFG.cpp (201361)
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//===----------------------------------------------------------------------===//

--- 20 unchanged lines hidden (view full) ---

29
30static SourceLocation GetEndLoc(Decl* D) {
31 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
34
35 return D->getLocation();
36}
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//===----------------------------------------------------------------------===//

--- 20 unchanged lines hidden (view full) ---

29
30static SourceLocation GetEndLoc(Decl* D) {
31 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
34
35 return D->getLocation();
36}
37
38class AddStmtChoice {
39public:
40 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
41public:
42 AddStmtChoice(Kind kind) : k(kind) {}
43 bool alwaysAdd() const { return k != NotAlwaysAdd; }
44 bool asLValue() const { return k == AlwaysAddAsLValue; }
45private:
46 Kind k;
47};
37
38/// CFGBuilder - This class implements CFG construction from an AST.
39/// The builder is stateful: an instance of the builder should be used to only
40/// construct a single CFG.
41///
42/// Example usage:
43///
44/// CFGBuilder builder;

--- 34 unchanged lines hidden (view full) ---

79 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
80 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
81
82 // buildCFG - Used by external clients to construct the CFG.
83 CFG* buildCFG(Stmt *Statement, ASTContext *C);
84
85private:
86 // Visitors to walk an AST and construct the CFG.
48
49/// CFGBuilder - This class implements CFG construction from an AST.
50/// The builder is stateful: an instance of the builder should be used to only
51/// construct a single CFG.
52///
53/// Example usage:
54///
55/// CFGBuilder builder;

--- 34 unchanged lines hidden (view full) ---

90 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
91 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {}
92
93 // buildCFG - Used by external clients to construct the CFG.
94 CFG* buildCFG(Stmt *Statement, ASTContext *C);
95
96private:
97 // Visitors to walk an AST and construct the CFG.
87 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
88 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
89 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
98 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
99 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
100 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
90 CFGBlock *VisitBreakStmt(BreakStmt *B);
101 CFGBlock *VisitBreakStmt(BreakStmt *B);
91 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
102 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
92 CFGBlock *VisitCaseStmt(CaseStmt *C);
103 CFGBlock *VisitCaseStmt(CaseStmt *C);
93 CFGBlock *VisitChooseExpr(ChooseExpr *C);
104 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
94 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
105 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
95 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
106 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
107 AddStmtChoice asc);
96 CFGBlock *VisitContinueStmt(ContinueStmt *C);
97 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S) { return NYS(); }
98 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
99 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S) { return NYS(); }
100 CFGBlock *VisitDeclStmt(DeclStmt *DS);
101 CFGBlock *VisitDeclSubExpr(Decl* D);
102 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
103 CFGBlock *VisitDoStmt(DoStmt *D);
104 CFGBlock *VisitForStmt(ForStmt *F);
105 CFGBlock *VisitGotoStmt(GotoStmt* G);
106 CFGBlock *VisitIfStmt(IfStmt *I);
107 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
108 CFGBlock *VisitLabelStmt(LabelStmt *L);
109 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
110 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
111 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
112 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
113 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
114 CFGBlock *VisitReturnStmt(ReturnStmt* R);
108 CFGBlock *VisitContinueStmt(ContinueStmt *C);
109 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S) { return NYS(); }
110 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
111 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S) { return NYS(); }
112 CFGBlock *VisitDeclStmt(DeclStmt *DS);
113 CFGBlock *VisitDeclSubExpr(Decl* D);
114 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
115 CFGBlock *VisitDoStmt(DoStmt *D);
116 CFGBlock *VisitForStmt(ForStmt *F);
117 CFGBlock *VisitGotoStmt(GotoStmt* G);
118 CFGBlock *VisitIfStmt(IfStmt *I);
119 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
120 CFGBlock *VisitLabelStmt(LabelStmt *L);
121 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
122 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
123 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
124 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
125 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
126 CFGBlock *VisitReturnStmt(ReturnStmt* R);
115 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
116 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
127 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
128 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
117 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
118 CFGBlock *VisitWhileStmt(WhileStmt *W);
119
129 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
130 CFGBlock *VisitWhileStmt(WhileStmt *W);
131
120 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
121 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
132 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
133 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
122 CFGBlock *VisitChildren(Stmt* S);
123
124 // NYS == Not Yet Supported
125 CFGBlock* NYS() {
126 badCFG = true;
127 return Block;
128 }
129
130 void autoCreateBlock() { if (!Block) Block = createBlock(); }
131 CFGBlock *createBlock(bool add_successor = true);
132 bool FinishBlock(CFGBlock* B);
134 CFGBlock *VisitChildren(Stmt* S);
135
136 // NYS == Not Yet Supported
137 CFGBlock* NYS() {
138 badCFG = true;
139 return Block;
140 }
141
142 void autoCreateBlock() { if (!Block) Block = createBlock(); }
143 CFGBlock *createBlock(bool add_successor = true);
144 bool FinishBlock(CFGBlock* B);
133 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
145 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
146 return Visit(S, asc);
147 }
134
148
135 void AppendStmt(CFGBlock *B, Stmt *S) {
136 B->appendStmt(S, cfg->getBumpVectorContext());
149 void AppendStmt(CFGBlock *B, Stmt *S,
150 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
151 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
137 }
138
139 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
140 B->addSuccessor(S, cfg->getBumpVectorContext());
141 }
142
143 /// TryResult - a class representing a variant over the values
144 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,

--- 128 unchanged lines hidden (view full) ---

273
274 assert(B);
275 return true;
276}
277
278/// Visit - Walk the subtree of a statement and add extra
279/// blocks for ternary operators, &&, and ||. We also process "," and
280/// DeclStmts (which may contain nested control-flow).
152 }
153
154 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
155 B->addSuccessor(S, cfg->getBumpVectorContext());
156 }
157
158 /// TryResult - a class representing a variant over the values
159 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,

--- 128 unchanged lines hidden (view full) ---

288
289 assert(B);
290 return true;
291}
292
293/// Visit - Walk the subtree of a statement and add extra
294/// blocks for ternary operators, &&, and ||. We also process "," and
295/// DeclStmts (which may contain nested control-flow).
281CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
296CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
282tryAgain:
283 switch (S->getStmtClass()) {
284 default:
297tryAgain:
298 switch (S->getStmtClass()) {
299 default:
285 return VisitStmt(S, alwaysAdd);
300 return VisitStmt(S, asc);
286
287 case Stmt::AddrLabelExprClass:
301
302 case Stmt::AddrLabelExprClass:
288 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
303 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
289
290 case Stmt::BinaryOperatorClass:
304
305 case Stmt::BinaryOperatorClass:
291 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
306 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
292
293 case Stmt::BlockExprClass:
307
308 case Stmt::BlockExprClass:
294 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
309 return VisitBlockExpr(cast<BlockExpr>(S), asc);
295
296 case Stmt::BreakStmtClass:
297 return VisitBreakStmt(cast<BreakStmt>(S));
298
299 case Stmt::CallExprClass:
310
311 case Stmt::BreakStmtClass:
312 return VisitBreakStmt(cast<BreakStmt>(S));
313
314 case Stmt::CallExprClass:
300 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
315 return VisitCallExpr(cast<CallExpr>(S), asc);
301
302 case Stmt::CaseStmtClass:
303 return VisitCaseStmt(cast<CaseStmt>(S));
304
305 case Stmt::ChooseExprClass:
316
317 case Stmt::CaseStmtClass:
318 return VisitCaseStmt(cast<CaseStmt>(S));
319
320 case Stmt::ChooseExprClass:
306 return VisitChooseExpr(cast(S));
321 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
307
308 case Stmt::CompoundStmtClass:
309 return VisitCompoundStmt(cast<CompoundStmt>(S));
310
311 case Stmt::ConditionalOperatorClass:
322
323 case Stmt::CompoundStmtClass:
324 return VisitCompoundStmt(cast<CompoundStmt>(S));
325
326 case Stmt::ConditionalOperatorClass:
312 return VisitConditionalOperator(cast(S));
327 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
313
314 case Stmt::ContinueStmtClass:
315 return VisitContinueStmt(cast<ContinueStmt>(S));
316
317 case Stmt::DeclStmtClass:
318 return VisitDeclStmt(cast<DeclStmt>(S));
319
320 case Stmt::DefaultStmtClass:

--- 41 unchanged lines hidden (view full) ---

362
363 case Stmt::NullStmtClass:
364 return Block;
365
366 case Stmt::ReturnStmtClass:
367 return VisitReturnStmt(cast<ReturnStmt>(S));
368
369 case Stmt::SizeOfAlignOfExprClass:
328
329 case Stmt::ContinueStmtClass:
330 return VisitContinueStmt(cast<ContinueStmt>(S));
331
332 case Stmt::DeclStmtClass:
333 return VisitDeclStmt(cast<DeclStmt>(S));
334
335 case Stmt::DefaultStmtClass:

--- 41 unchanged lines hidden (view full) ---

377
378 case Stmt::NullStmtClass:
379 return Block;
380
381 case Stmt::ReturnStmtClass:
382 return VisitReturnStmt(cast<ReturnStmt>(S));
383
384 case Stmt::SizeOfAlignOfExprClass:
370 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
385 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
371
372 case Stmt::StmtExprClass:
386
387 case Stmt::StmtExprClass:
373 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
388 return VisitStmtExpr(cast<StmtExpr>(S), asc);
374
375 case Stmt::SwitchStmtClass:
376 return VisitSwitchStmt(cast<SwitchStmt>(S));
377
378 case Stmt::WhileStmtClass:
379 return VisitWhileStmt(cast<WhileStmt>(S));
380 }
381}
382
389
390 case Stmt::SwitchStmtClass:
391 return VisitSwitchStmt(cast<SwitchStmt>(S));
392
393 case Stmt::WhileStmtClass:
394 return VisitWhileStmt(cast<WhileStmt>(S));
395 }
396}
397
383CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
384 if (alwaysAdd) {
398CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
399 if (asc.alwaysAdd()) {
385 autoCreateBlock();
400 autoCreateBlock();
386 AppendStmt(Block, S);
401 AppendStmt(Block, S, asc);
387 }
388
389 return VisitChildren(S);
390}
391
392/// VisitChildren - Visit the children of a Stmt.
393CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
394 CFGBlock *B = Block;
395 for (Stmt::child_iterator I = Terminator->child_begin(),
396 E = Terminator->child_end(); I != E; ++I) {
397 if (*I) B = Visit(*I);
398 }
399 return B;
400}
401
402 }
403
404 return VisitChildren(S);
405}
406
407/// VisitChildren - Visit the children of a Stmt.
408CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
409 CFGBlock *B = Block;
410 for (Stmt::child_iterator I = Terminator->child_begin(),
411 E = Terminator->child_end(); I != E; ++I) {
412 if (*I) B = Visit(*I);
413 }
414 return B;
415}
416
402CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
417CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
418 AddStmtChoice asc) {
403 AddressTakenLabels.insert(A->getLabel());
404
419 AddressTakenLabels.insert(A->getLabel());
420
405 if (alwaysAdd) {
421 if (asc.alwaysAdd()) {
406 autoCreateBlock();
422 autoCreateBlock();
407 AppendStmt(Block, A);
423 AppendStmt(Block, A, asc);
408 }
409
410 return Block;
411}
412
424 }
425
426 return Block;
427}
428
413CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
429CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
430 AddStmtChoice asc) {
414 if (B->isLogicalOp()) { // && or ||
415 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
431 if (B->isLogicalOp()) { // && or ||
432 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
416 AppendStmt(ConfluenceBlock, B);
433 AppendStmt(ConfluenceBlock, B, asc);
417
418 if (!FinishBlock(ConfluenceBlock))
419 return 0;
420
421 // create the block evaluating the LHS
422 CFGBlock* LHSBlock = createBlock(false);
423 LHSBlock->setTerminator(B);
424

--- 20 unchanged lines hidden (view full) ---

445 }
446
447 // Generate the blocks for evaluating the LHS.
448 Block = LHSBlock;
449 return addStmt(B->getLHS());
450 }
451 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
452 autoCreateBlock();
434
435 if (!FinishBlock(ConfluenceBlock))
436 return 0;
437
438 // create the block evaluating the LHS
439 CFGBlock* LHSBlock = createBlock(false);
440 LHSBlock->setTerminator(B);
441

--- 20 unchanged lines hidden (view full) ---

462 }
463
464 // Generate the blocks for evaluating the LHS.
465 Block = LHSBlock;
466 return addStmt(B->getLHS());
467 }
468 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
469 autoCreateBlock();
453 AppendStmt(Block, B);
470 AppendStmt(Block, B, asc);
454 addStmt(B->getRHS());
455 return addStmt(B->getLHS());
456 }
457
471 addStmt(B->getRHS());
472 return addStmt(B->getLHS());
473 }
474
458 return VisitStmt(B, alwaysAdd);
475 return VisitStmt(B, asc);
459}
460
476}
477
461CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, bool alwaysAdd) {
462 if (alwaysAdd) {
478CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
479 if (asc.alwaysAdd()) {
463 autoCreateBlock();
480 autoCreateBlock();
464 AppendStmt(Block, E);
481 AppendStmt(Block, E, asc);
465 }
466 return Block;
467}
468
469CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
470 // "break" is a control-flow statement. Thus we stop processing the current
471 // block.
472 if (Block && !FinishBlock(Block))

--- 9 unchanged lines hidden (view full) ---

482 AddSuccessor(Block, BreakTargetBlock);
483 else
484 badCFG = true;
485
486
487 return Block;
488}
489
482 }
483 return Block;
484}
485
486CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
487 // "break" is a control-flow statement. Thus we stop processing the current
488 // block.
489 if (Block && !FinishBlock(Block))

--- 9 unchanged lines hidden (view full) ---

499 AddSuccessor(Block, BreakTargetBlock);
500 else
501 badCFG = true;
502
503
504 return Block;
505}
506
490CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
507CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
491 // If this is a call to a no-return function, this stops the block here.
492 bool NoReturn = false;
493 if (C->getCallee()->getType().getNoReturnAttr()) {
494 NoReturn = true;
495 }
496
497 if (FunctionDecl *FD = C->getDirectCallee())
498 if (FD->hasAttr<NoReturnAttr>())
499 NoReturn = true;
500
501 if (!NoReturn)
508 // If this is a call to a no-return function, this stops the block here.
509 bool NoReturn = false;
510 if (C->getCallee()->getType().getNoReturnAttr()) {
511 NoReturn = true;
512 }
513
514 if (FunctionDecl *FD = C->getDirectCallee())
515 if (FD->hasAttr<NoReturnAttr>())
516 NoReturn = true;
517
518 if (!NoReturn)
502 return VisitStmt(C, alwaysAdd);
519 return VisitStmt(C, asc);
503
504 if (Block && !FinishBlock(Block))
505 return 0;
506
507 // Create new block with no successor for the remaining pieces.
508 Block = createBlock(false);
520
521 if (Block && !FinishBlock(Block))
522 return 0;
523
524 // Create new block with no successor for the remaining pieces.
525 Block = createBlock(false);
509 AppendStmt(Block, C);
526 AppendStmt(Block, C, asc);
510
511 // Wire this to the exit block directly.
512 AddSuccessor(Block, &cfg->getExit());
513
514 return VisitChildren(C);
515}
516
527
528 // Wire this to the exit block directly.
529 AddSuccessor(Block, &cfg->getExit());
530
531 return VisitChildren(C);
532}
533
517CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
534CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
535 AddStmtChoice asc) {
518 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
536 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
519 AppendStmt(ConfluenceBlock, C);
537 AppendStmt(ConfluenceBlock, C, asc);
520 if (!FinishBlock(ConfluenceBlock))
521 return 0;
522
523 Succ = ConfluenceBlock;
524 Block = NULL;
525 CFGBlock* LHSBlock = addStmt(C->getLHS());
526 if (!FinishBlock(LHSBlock))
527 return 0;

--- 22 unchanged lines hidden (view full) ---

550 LastBlock = addStmt(*I);
551
552 if (badCFG)
553 return NULL;
554 }
555 return LastBlock;
556}
557
538 if (!FinishBlock(ConfluenceBlock))
539 return 0;
540
541 Succ = ConfluenceBlock;
542 Block = NULL;
543 CFGBlock* LHSBlock = addStmt(C->getLHS());
544 if (!FinishBlock(LHSBlock))
545 return 0;

--- 22 unchanged lines hidden (view full) ---

568 LastBlock = addStmt(*I);
569
570 if (badCFG)
571 return NULL;
572 }
573 return LastBlock;
574}
575
558CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
576CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
577 AddStmtChoice asc) {
559 // Create the confluence block that will "merge" the results of the ternary
560 // expression.
561 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
578 // Create the confluence block that will "merge" the results of the ternary
579 // expression.
580 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
562 AppendStmt(ConfluenceBlock, C);
581 AppendStmt(ConfluenceBlock, C, asc);
563 if (!FinishBlock(ConfluenceBlock))
564 return 0;
565
566 // Create a block for the LHS expression if there is an LHS expression. A
567 // GCC extension allows LHS to be NULL, causing the condition to be the
568 // value that is returned instead.
569 // e.g: x ?: y is shorthand for: x ? x : y;
570 Succ = ConfluenceBlock;

--- 94 unchanged lines hidden (view full) ---

665 if (Init) {
666 // Optimization: Don't create separate block-level statements for literals.
667 switch (Init->getStmtClass()) {
668 case Stmt::IntegerLiteralClass:
669 case Stmt::CharacterLiteralClass:
670 case Stmt::StringLiteralClass:
671 break;
672 default:
582 if (!FinishBlock(ConfluenceBlock))
583 return 0;
584
585 // Create a block for the LHS expression if there is an LHS expression. A
586 // GCC extension allows LHS to be NULL, causing the condition to be the
587 // value that is returned instead.
588 // e.g: x ?: y is shorthand for: x ? x : y;
589 Succ = ConfluenceBlock;

--- 94 unchanged lines hidden (view full) ---

684 if (Init) {
685 // Optimization: Don't create separate block-level statements for literals.
686 switch (Init->getStmtClass()) {
687 case Stmt::IntegerLiteralClass:
688 case Stmt::CharacterLiteralClass:
689 case Stmt::StringLiteralClass:
690 break;
691 default:
673 Block = addStmt(Init);
692 Block = addStmt(Init,
693 VD->getType()->isReferenceType()
694 ? AddStmtChoice::AlwaysAddAsLValue
695 : AddStmtChoice::AlwaysAdd);
674 }
675 }
676
677 // If the type of VD is a VLA, then we must process its size expressions.
678 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
679 VA = FindVA(VA->getElementType().getTypePtr()))
680 Block = addStmt(VA->getSizeExpr());
681

--- 67 unchanged lines hidden (view full) ---

749
750 // Now add the successors.
751 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
752 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
753
754 // Add the condition as the last statement in the new block. This may create
755 // new blocks as the condition may contain control-flow. Any newly created
756 // blocks will be pointed to be "Block".
696 }
697 }
698
699 // If the type of VD is a VLA, then we must process its size expressions.
700 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
701 VA = FindVA(VA->getElementType().getTypePtr()))
702 Block = addStmt(VA->getSizeExpr());
703

--- 67 unchanged lines hidden (view full) ---

771
772 // Now add the successors.
773 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
774 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
775
776 // Add the condition as the last statement in the new block. This may create
777 // new blocks as the condition may contain control-flow. Any newly created
778 // blocks will be pointed to be "Block".
757 return addStmt(I->getCond());
779 Block = addStmt(I->getCond());
780
781 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
782 // and the condition variable initialization to the CFG.
783 if (VarDecl *VD = I->getConditionVariable()) {
784 if (Expr *Init = VD->getInit()) {
785 autoCreateBlock();
786 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
787 addStmt(Init);
788 }
789 }
790
791 return Block;
758}
759
760
761CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
762 // If we were in the middle of a block we stop processing that block.
763 //
764 // NOTE: If a "return" appears in the middle of a block, this means that the
765 // code afterwards is DEAD (unreachable). We still keep a basic block

--- 5 unchanged lines hidden (view full) ---

771 // Create the new block.
772 Block = createBlock(false);
773
774 // The Exit block is the only successor.
775 AddSuccessor(Block, &cfg->getExit());
776
777 // Add the return statement to the block. This may create new blocks if R
778 // contains control-flow (short-circuit operations).
792}
793
794
795CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
796 // If we were in the middle of a block we stop processing that block.
797 //
798 // NOTE: If a "return" appears in the middle of a block, this means that the
799 // code afterwards is DEAD (unreachable). We still keep a basic block

--- 5 unchanged lines hidden (view full) ---

805 // Create the new block.
806 Block = createBlock(false);
807
808 // The Exit block is the only successor.
809 AddSuccessor(Block, &cfg->getExit());
810
811 // Add the return statement to the block. This may create new blocks if R
812 // contains control-flow (short-circuit operations).
779 return VisitStmt(R, true);
813 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
780}
781
782CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
783 // Get the block of the labeled statement. Add it to our map.
784 addStmt(L->getSubStmt());
785 CFGBlock* LabelBlock = Block;
786
787 if (!LabelBlock) // This can happen when the body is empty, i.e.

--- 61 unchanged lines hidden (view full) ---

849 // Set the terminator for the "exit" condition block.
850 ExitConditionBlock->setTerminator(F);
851
852 // Now add the actual condition to the condition block. Because the condition
853 // itself may contain control-flow, new blocks may be created.
854 if (Stmt* C = F->getCond()) {
855 Block = ExitConditionBlock;
856 EntryConditionBlock = addStmt(C);
814}
815
816CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
817 // Get the block of the labeled statement. Add it to our map.
818 addStmt(L->getSubStmt());
819 CFGBlock* LabelBlock = Block;
820
821 if (!LabelBlock) // This can happen when the body is empty, i.e.

--- 61 unchanged lines hidden (view full) ---

883 // Set the terminator for the "exit" condition block.
884 ExitConditionBlock->setTerminator(F);
885
886 // Now add the actual condition to the condition block. Because the condition
887 // itself may contain control-flow, new blocks may be created.
888 if (Stmt* C = F->getCond()) {
889 Block = ExitConditionBlock;
890 EntryConditionBlock = addStmt(C);
891 assert(Block == EntryConditionBlock);
892
893 // If this block contains a condition variable, add both the condition
894 // variable and initializer to the CFG.
895 if (VarDecl *VD = F->getConditionVariable()) {
896 if (Expr *Init = VD->getInit()) {
897 autoCreateBlock();
898 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
899 EntryConditionBlock = addStmt(Init);
900 assert(Block == EntryConditionBlock);
901 }
902 }
903
857 if (Block) {
858 if (!FinishBlock(EntryConditionBlock))
859 return 0;
860 }
861 }
862
863 // The condition block is the implicit successor for the loop body as well as
864 // any code above the loop.

--- 130 unchanged lines hidden (view full) ---

995 // performs the actual binding to 'element' and determines if there are any
996 // more items in the collection.
997 AppendStmt(ExitConditionBlock, S);
998 Block = ExitConditionBlock;
999
1000 // Walk the 'element' expression to see if there are any side-effects. We
1001 // generate new blocks as necesary. We DON'T add the statement by default to
1002 // the CFG unless it contains control-flow.
904 if (Block) {
905 if (!FinishBlock(EntryConditionBlock))
906 return 0;
907 }
908 }
909
910 // The condition block is the implicit successor for the loop body as well as
911 // any code above the loop.

--- 130 unchanged lines hidden (view full) ---

1042 // performs the actual binding to 'element' and determines if there are any
1043 // more items in the collection.
1044 AppendStmt(ExitConditionBlock, S);
1045 Block = ExitConditionBlock;
1046
1047 // Walk the 'element' expression to see if there are any side-effects. We
1048 // generate new blocks as necesary. We DON'T add the statement by default to
1049 // the CFG unless it contains control-flow.
1003 EntryConditionBlock = Visit(S->getElement(), false);
1050 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
1004 if (Block) {
1005 if (!FinishBlock(EntryConditionBlock))
1006 return 0;
1007 Block = 0;
1008 }
1009
1010 // The condition block is the implicit successor for the loop body as well as
1011 // any code above the loop.

--- 79 unchanged lines hidden (view full) ---

1091
1092 // Now add the actual condition to the condition block. Because the condition
1093 // itself may contain control-flow, new blocks may be created. Thus we update
1094 // "Succ" after adding the condition.
1095 if (Stmt* C = W->getCond()) {
1096 Block = ExitConditionBlock;
1097 EntryConditionBlock = addStmt(C);
1098 assert(Block == EntryConditionBlock);
1051 if (Block) {
1052 if (!FinishBlock(EntryConditionBlock))
1053 return 0;
1054 Block = 0;
1055 }
1056
1057 // The condition block is the implicit successor for the loop body as well as
1058 // any code above the loop.

--- 79 unchanged lines hidden (view full) ---

1138
1139 // Now add the actual condition to the condition block. Because the condition
1140 // itself may contain control-flow, new blocks may be created. Thus we update
1141 // "Succ" after adding the condition.
1142 if (Stmt* C = W->getCond()) {
1143 Block = ExitConditionBlock;
1144 EntryConditionBlock = addStmt(C);
1145 assert(Block == EntryConditionBlock);
1146
1147 // If this block contains a condition variable, add both the condition
1148 // variable and initializer to the CFG.
1149 if (VarDecl *VD = W->getConditionVariable()) {
1150 if (Expr *Init = VD->getInit()) {
1151 autoCreateBlock();
1152 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1153 EntryConditionBlock = addStmt(Init);
1154 assert(Block == EntryConditionBlock);
1155 }
1156 }
1157
1099 if (Block) {
1100 if (!FinishBlock(EntryConditionBlock))
1101 return 0;
1102 }
1103 }
1104
1105 // The condition block is the implicit successor for the loop body as well as
1106 // any code above the loop.

--- 70 unchanged lines hidden (view full) ---

1177 // Create the new block.
1178 Block = createBlock(false);
1179
1180 // The Exit block is the only successor.
1181 AddSuccessor(Block, &cfg->getExit());
1182
1183 // Add the statement to the block. This may create new blocks if S contains
1184 // control-flow (short-circuit operations).
1158 if (Block) {
1159 if (!FinishBlock(EntryConditionBlock))
1160 return 0;
1161 }
1162 }
1163
1164 // The condition block is the implicit successor for the loop body as well as
1165 // any code above the loop.

--- 70 unchanged lines hidden (view full) ---

1236 // Create the new block.
1237 Block = createBlock(false);
1238
1239 // The Exit block is the only successor.
1240 AddSuccessor(Block, &cfg->getExit());
1241
1242 // Add the statement to the block. This may create new blocks if S contains
1243 // control-flow (short-circuit operations).
1185 return VisitStmt(S, true);
1244 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
1186}
1187
1188CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1189 // If we were in the middle of a block we stop processing that block.
1190 if (Block && !FinishBlock(Block))
1191 return 0;
1192
1193 // Create the new block.
1194 Block = createBlock(false);
1195
1196 // The Exit block is the only successor.
1197 AddSuccessor(Block, &cfg->getExit());
1198
1199 // Add the statement to the block. This may create new blocks if S contains
1200 // control-flow (short-circuit operations).
1245}
1246
1247CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1248 // If we were in the middle of a block we stop processing that block.
1249 if (Block && !FinishBlock(Block))
1250 return 0;
1251
1252 // Create the new block.
1253 Block = createBlock(false);
1254
1255 // The Exit block is the only successor.
1256 AddSuccessor(Block, &cfg->getExit());
1257
1258 // Add the statement to the block. This may create new blocks if S contains
1259 // control-flow (short-circuit operations).
1201 return VisitStmt(T, true);
1260 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
1202}
1203
1204CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
1205 CFGBlock* LoopSuccessor = NULL;
1206
1207 // "do...while" is a control-flow statement. Thus we stop processing the
1208 // current block.
1209 if (Block) {

--- 101 unchanged lines hidden (view full) ---

1311 AddSuccessor(Block, ContinueTargetBlock);
1312 else
1313 badCFG = true;
1314
1315 return Block;
1316}
1317
1318CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1261}
1262
1263CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
1264 CFGBlock* LoopSuccessor = NULL;
1265
1266 // "do...while" is a control-flow statement. Thus we stop processing the
1267 // current block.
1268 if (Block) {

--- 101 unchanged lines hidden (view full) ---

1370 AddSuccessor(Block, ContinueTargetBlock);
1371 else
1372 badCFG = true;
1373
1374 return Block;
1375}
1376
1377CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1319 bool alwaysAdd) {
1378 AddStmtChoice asc) {
1320
1379
1321 if (alwaysAdd) {
1380 if (asc.alwaysAdd()) {
1322 autoCreateBlock();
1323 AppendStmt(Block, E);
1324 }
1325
1326 // VLA types have expressions that must be evaluated.
1327 if (E->isArgumentType()) {
1328 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1329 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1330 addStmt(VA->getSizeExpr());
1331 }
1332
1333 return Block;
1334}
1335
1336/// VisitStmtExpr - Utility method to handle (nested) statement
1337/// expressions (a GCC extension).
1381 autoCreateBlock();
1382 AppendStmt(Block, E);
1383 }
1384
1385 // VLA types have expressions that must be evaluated.
1386 if (E->isArgumentType()) {
1387 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1388 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1389 addStmt(VA->getSizeExpr());
1390 }
1391
1392 return Block;
1393}
1394
1395/// VisitStmtExpr - Utility method to handle (nested) statement
1396/// expressions (a GCC extension).
1338CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1339 if (alwaysAdd) {
1397CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1398 if (asc.alwaysAdd()) {
1340 autoCreateBlock();
1341 AppendStmt(Block, SE);
1342 }
1343 return VisitCompoundStmt(SE->getSubStmt());
1344}
1345
1346CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
1347 // "switch" is a control-flow statement. Thus we stop processing the current

--- 38 unchanged lines hidden (view full) ---

1386 // If we have no "default:" case, the default transition is to the code
1387 // following the switch body.
1388 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
1389
1390 // Add the terminator and condition in the switch block.
1391 SwitchTerminatedBlock->setTerminator(Terminator);
1392 assert (Terminator->getCond() && "switch condition must be non-NULL");
1393 Block = SwitchTerminatedBlock;
1399 autoCreateBlock();
1400 AppendStmt(Block, SE);
1401 }
1402 return VisitCompoundStmt(SE->getSubStmt());
1403}
1404
1405CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
1406 // "switch" is a control-flow statement. Thus we stop processing the current

--- 38 unchanged lines hidden (view full) ---

1445 // If we have no "default:" case, the default transition is to the code
1446 // following the switch body.
1447 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
1448
1449 // Add the terminator and condition in the switch block.
1450 SwitchTerminatedBlock->setTerminator(Terminator);
1451 assert (Terminator->getCond() && "switch condition must be non-NULL");
1452 Block = SwitchTerminatedBlock;
1394
1395 return addStmt(Terminator->getCond());
1453 Block = addStmt(Terminator->getCond());
1454
1455 // Finally, if the SwitchStmt contains a condition variable, add both the
1456 // SwitchStmt and the condition variable initialization to the CFG.
1457 if (VarDecl *VD = Terminator->getConditionVariable()) {
1458 if (Expr *Init = VD->getInit()) {
1459 autoCreateBlock();
1460 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1461 addStmt(Init);
1462 }
1463 }
1464
1465 return Block;
1396}
1397
1398CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
1399 // CaseStmts are essentially labels, so they are the first statement in a
1400 // block.
1401
1402 if (CS->getSubStmt())
1403 addStmt(CS->getSubStmt());

--- 105 unchanged lines hidden (view full) ---

1509//===----------------------------------------------------------------------===//
1510// CFG: Queries for BlkExprs.
1511//===----------------------------------------------------------------------===//
1512
1513namespace {
1514 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
1515}
1516
1466}
1467
1468CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
1469 // CaseStmts are essentially labels, so they are the first statement in a
1470 // block.
1471
1472 if (CS->getSubStmt())
1473 addStmt(CS->getSubStmt());

--- 105 unchanged lines hidden (view full) ---

1579//===----------------------------------------------------------------------===//
1580// CFG: Queries for BlkExprs.
1581//===----------------------------------------------------------------------===//
1582
1583namespace {
1584 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
1585}
1586
1517static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1518 if (!Terminator)
1587static void FindSubExprAssignments(Stmt *S,
1588 llvm::SmallPtrSet<Expr*,50>& Set) {
1589 if (!S)
1519 return;
1520
1590 return;
1591
1521 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
1522 if (!*I) continue;
1523
1524 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1592 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1593 Stmt *child = *I;
1594 if (!child)
1595 continue;
1596
1597 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
1525 if (B->isAssignmentOp()) Set.insert(B);
1526
1598 if (B->isAssignmentOp()) Set.insert(B);
1599
1527 FindSubExprAssignments(*I, Set);
1600 FindSubExprAssignments(child, Set);
1528 }
1529}
1530
1531static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1532 BlkExprMapTy* M = new BlkExprMapTy();
1533
1534 // Look for assignments that are used as subexpressions. These are the only
1535 // assignments that we want to *possibly* register as a block-level

--- 535 unchanged lines hidden ---
1601 }
1602}
1603
1604static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1605 BlkExprMapTy* M = new BlkExprMapTy();
1606
1607 // Look for assignments that are used as subexpressions. These are the only
1608 // assignments that we want to *possibly* register as a block-level

--- 535 unchanged lines hidden ---