@@ -466,15 +466,14 @@ StmtDiff BaseForwardModeVisitor::VisitStmt(const Stmt* S) {
466466}
467467
468468StmtDiff BaseForwardModeVisitor::VisitCompoundStmt (const CompoundStmt* CS ) {
469- beginScope ( Scope::DeclScope);
469+ ScopeRAII compoundScope (* this , Scope::DeclScope);
470470 beginBlock ();
471471 for (Stmt* S : CS ->body ()) {
472472 StmtDiff SDiff = Visit (S);
473473 addToCurrentBlock (SDiff.getStmt_dx ());
474474 addToCurrentBlock (SDiff.getStmt ());
475475 }
476476 CompoundStmt* Result = endBlock ();
477- endScope ();
478477 // Differentation of CompundStmt produces another CompoundStmt with both
479478 // original and derived statements, i.e. Stmt() is Result and Stmt_dx() is
480479 // null.
@@ -484,7 +483,7 @@ StmtDiff BaseForwardModeVisitor::VisitCompoundStmt(const CompoundStmt* CS) {
484483StmtDiff BaseForwardModeVisitor::VisitIfStmt (const IfStmt* If) {
485484 // Control scope of the IfStmt. E.g., in if (double x = ...) {...}, x goes
486485 // to this scope.
487- beginScope ( Scope::DeclScope | Scope::ControlScope);
486+ ScopeRAII ifScope (* this , Scope::DeclScope | Scope::ControlScope);
488487 // Create a block "around" if statement, e.g:
489488 // {
490489 // ...
@@ -532,12 +531,11 @@ StmtDiff BaseForwardModeVisitor::VisitIfStmt(const IfStmt* If) {
532531 return BranchDiff.getStmt ();
533532 } else {
534533 beginBlock ();
535- beginScope ( Scope::DeclScope);
534+ ScopeRAII branchScope (* this , Scope::DeclScope);
536535 StmtDiff BranchDiff = Visit (Branch);
537536 for (Stmt* S : BranchDiff.getBothStmts ())
538537 addToCurrentBlock (S);
539538 CompoundStmt* Block = endBlock ();
540- endScope ();
541539 if (Block->size () == 1 )
542540 return Block->body_front ();
543541 else
@@ -554,7 +552,6 @@ StmtDiff BaseForwardModeVisitor::VisitIfStmt(const IfStmt* If) {
554552 addToCurrentBlock (ifDiff);
555553 CompoundStmt* Block = endBlock ();
556554 // If IfStmt is the only statement in the block, remove the block:
557- endScope ();
558555 // {
559556 // if (...) {...}
560557 // }
@@ -596,8 +593,8 @@ StmtDiff BaseForwardModeVisitor::VisitConditionalOperator(
596593
597594StmtDiff
598595BaseForwardModeVisitor::VisitCXXForRangeStmt (const CXXForRangeStmt* FRS ) {
599- beginScope ( Scope::DeclScope | Scope::ControlScope | Scope::BreakScope |
600- Scope::ContinueScope);
596+ ScopeRAII rangeScope (* this , Scope::DeclScope | Scope::ControlScope |
597+ Scope::BreakScope | Scope::ContinueScope);
601598 // Visiting for range-based ststement produces __range1, __begin1 and __end1
602599 // variables, so for(auto i: a){
603600 // ...
@@ -656,13 +653,12 @@ BaseForwardModeVisitor::VisitCXXForRangeStmt(const CXXForRangeStmt* FRS) {
656653 Stmt* forStmtDiff = new (m_Context)
657654 ForStmt (m_Context, nullptr , cond, /* condVar=*/ nullptr , Inc, bodyResult,
658655 FRS ->getForLoc (), FRS ->getBeginLoc (), FRS ->getEndLoc ());
659- endScope ();
660656 return StmtDiff (forStmtDiff);
661657}
662658
663659StmtDiff BaseForwardModeVisitor::VisitForStmt (const ForStmt* FS ) {
664- beginScope ( Scope::DeclScope | Scope::ControlScope | Scope::BreakScope |
665- Scope::ContinueScope);
660+ ScopeRAII forScope (* this , Scope::DeclScope | Scope::ControlScope |
661+ Scope::BreakScope | Scope::ContinueScope);
666662 beginBlock ();
667663 const Stmt* init = FS ->getInit ();
668664 StmtDiff initDiff = init ? Visit (init) : StmtDiff{};
@@ -760,22 +756,22 @@ StmtDiff BaseForwardModeVisitor::VisitForStmt(const ForStmt* FS) {
760756
761757 // Build the derived for loop body.
762758 const Stmt* body = FS ->getBody ();
763- beginScope (Scope::DeclScope);
764759 Stmt* bodyResult = nullptr ;
765- beginBlock ();
766- StmtDiff bodyVisited = Visit (body);
767- for (Stmt* S : bodyVisited.getBothStmts ())
768- addToCurrentBlock (S);
769- bodyResult = utils::unwrapIfSingleStmt (endBlock ());
770- endScope ();
760+ {
761+ ScopeRAII bodyScope (*this , Scope::DeclScope);
762+ beginBlock ();
763+ StmtDiff bodyVisited = Visit (body);
764+ for (Stmt* S : bodyVisited.getBothStmts ())
765+ addToCurrentBlock (S);
766+ bodyResult = utils::unwrapIfSingleStmt (endBlock ());
767+ }
771768
772769 Stmt* forStmtDiff = new (m_Context)
773770 ForStmt (m_Context, initDiff.getStmt (), cond, /* condVar=*/ nullptr ,
774771 incResult, bodyResult, noLoc, noLoc, noLoc);
775772
776773 addToCurrentBlock (forStmtDiff);
777774 CompoundStmt* Block = endBlock ();
778- endScope ();
779775
780776 StmtDiff Result =
781777 (Block->size () == 1 ) ? StmtDiff (forStmtDiff) : StmtDiff (Block);
@@ -1823,9 +1819,9 @@ StmtDiff BaseForwardModeVisitor::VisitStringLiteral(const StringLiteral* SL) {
18231819}
18241820
18251821StmtDiff BaseForwardModeVisitor::VisitWhileStmt (const WhileStmt* WS ) {
1826- // begin scope for while loop
1827- beginScope ( Scope::ContinueScope | Scope::BreakScope | Scope::DeclScope |
1828- Scope::ControlScope);
1822+ // Scope for the whole while loop.
1823+ ScopeRAII whileScope (* this , Scope::ContinueScope | Scope::BreakScope |
1824+ Scope::DeclScope | Scope::ControlScope);
18291825
18301826 const VarDecl* condVar = WS ->getConditionVariable ();
18311827 VarDecl* condVarClone = nullptr ;
@@ -1882,13 +1878,12 @@ StmtDiff BaseForwardModeVisitor::VisitWhileStmt(const WhileStmt* WS) {
18821878 if (isa<CompoundStmt>(body)) {
18831879 bodyResult = Visit (body).getStmt ();
18841880 } else {
1885- beginScope ( Scope::DeclScope);
1881+ ScopeRAII bodyScope (* this , Scope::DeclScope);
18861882 beginBlock ();
18871883 StmtDiff Result = Visit (body);
18881884 for (Stmt* S : Result.getBothStmts ())
18891885 addToCurrentBlock (S);
18901886 CompoundStmt* Block = endBlock ();
1891- endScope ();
18921887 bodyResult = Block;
18931888 }
18941889
@@ -1897,8 +1892,6 @@ StmtDiff BaseForwardModeVisitor::VisitWhileStmt(const WhileStmt* WS) {
18971892 .ActOnWhileStmt (/* WhileLoc=*/ noLoc, /* LParenLoc=*/ noLoc, condRes,
18981893 /* RParenLoc=*/ noLoc, bodyResult)
18991894 .get ();
1900- // end scope for while loop
1901- endScope ();
19021895 return StmtDiff (WSDiff);
19031896}
19041897
@@ -1908,22 +1901,21 @@ BaseForwardModeVisitor::VisitContinueStmt(const ContinueStmt* ContStmt) {
19081901}
19091902
19101903StmtDiff BaseForwardModeVisitor::VisitDoStmt (const DoStmt* DS ) {
1911- // begin scope for do-while statement
1912- beginScope ( Scope::ContinueScope | Scope::BreakScope);
1904+ // Scope for the whole do-while statement.
1905+ ScopeRAII doScope (* this , Scope::ContinueScope | Scope::BreakScope);
19131906 Expr* clonedCond = DS ->getCond () ? Clone (DS ->getCond ()) : nullptr ;
19141907 const Stmt* body = DS ->getBody ();
19151908
19161909 Stmt* bodyResult = nullptr ;
19171910 if (isa<CompoundStmt>(body)) {
19181911 bodyResult = Visit (body).getStmt ();
19191912 } else {
1920- beginScope ( Scope::DeclScope);
1913+ ScopeRAII bodyScope (* this , Scope::DeclScope);
19211914 beginBlock ();
19221915 StmtDiff Result = Visit (body);
19231916 for (Stmt* S : Result.getBothStmts ())
19241917 addToCurrentBlock (S);
19251918 CompoundStmt* Block = endBlock ();
1926- endScope ();
19271919 bodyResult = Block;
19281920 }
19291921
@@ -1933,8 +1925,6 @@ StmtDiff BaseForwardModeVisitor::VisitDoStmt(const DoStmt* DS) {
19331925 /* CondRParen=*/ noLoc)
19341926 .get ();
19351927
1936- // end scope for do-while statement
1937- endScope ();
19381928 return StmtDiff (S);
19391929}
19401930
0 commit comments