From bcbb92d12f871a8f927c7f1cfcb45fbd859c8369 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Mon, 17 Mar 2014 14:19:37 +0000 Subject: [PATCH] [C++11] Replacing CompoundStmt iterators body_begin() and body_end() with iterator_range body(). Updating all of the usages of the iterators with range-based for loops. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204040 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Stmt.h | 8 ++++++++ lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp | 15 ++++++--------- lib/ARCMigrate/Transforms.cpp | 5 ++--- lib/AST/ExprConstant.cpp | 5 ++--- lib/AST/StmtPrinter.cpp | 5 ++--- lib/Analysis/CFG.cpp | 5 ++--- lib/CodeGen/CGClass.cpp | 7 ++----- lib/CodeGen/CGDecl.cpp | 7 +++---- lib/CodeGen/CGObjC.cpp | 5 ++--- lib/Sema/SemaDeclCXX.cpp | 10 ++++------ lib/Sema/TreeTransform.h | 9 ++++----- lib/Serialization/ASTWriterStmt.cpp | 5 ++--- 12 files changed, 39 insertions(+), 47 deletions(-) diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index d1fb0640fc..ba4a1e39d3 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -571,6 +571,9 @@ public: unsigned size() const { return CompoundStmtBits.NumStmts; } typedef Stmt** body_iterator; + typedef llvm::iterator_range body_range; + + body_range body() { return body_range(body_begin(), body_end()); } body_iterator body_begin() { return Body; } body_iterator body_end() { return Body + size(); } Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; } @@ -581,6 +584,11 @@ public: } typedef Stmt* const * const_body_iterator; + typedef llvm::iterator_range body_const_range; + + body_const_range body() const { + return body_const_range(body_begin(), body_end()); + } const_body_iterator body_begin() const { return Body; } const_body_iterator body_end() const { return Body + size(); } const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; } diff --git a/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp b/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp index ccf0a91dab..31f19cebea 100644 --- a/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp +++ b/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp @@ -89,9 +89,8 @@ public: bool VisitCompoundStmt(CompoundStmt *S) { if (S->body_empty()) return false; // was already empty, not because of transformations. - for (CompoundStmt::body_iterator - I = S->body_begin(), E = S->body_end(); I != E; ++I) - if (!Visit(*I)) + for (auto *I : S->body()) + if (!Visit(I)) return false; return true; } @@ -167,9 +166,8 @@ public: } bool VisitCompoundStmt(CompoundStmt *S) { - for (CompoundStmt::body_iterator - I = S->body_begin(), E = S->body_end(); I != E; ++I) - check(*I); + for (auto *I : S->body()) + check(I); return true; } @@ -189,9 +187,8 @@ private: static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx, std::vector &MacroLocs) { - for (CompoundStmt::body_iterator - I = body->body_begin(), E = body->body_end(); I != E; ++I) - if (!EmptyChecker(Ctx, MacroLocs).Visit(*I)) + for (auto *I : body->body()) + if (!EmptyChecker(Ctx, MacroLocs).Visit(I)) return false; return true; diff --git a/lib/ARCMigrate/Transforms.cpp b/lib/ARCMigrate/Transforms.cpp index 3649d38852..c349cb59f8 100644 --- a/lib/ARCMigrate/Transforms.cpp +++ b/lib/ARCMigrate/Transforms.cpp @@ -264,9 +264,8 @@ public: } bool VisitCompoundStmt(CompoundStmt *S) { - for (CompoundStmt::body_iterator - I = S->body_begin(), E = S->body_end(); I != E; ++I) - mark(*I); + for (auto *I : S->body()) + mark(I); return true; } diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 4c98c00474..9c69080652 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -3341,9 +3341,8 @@ static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, BlockScopeRAII Scope(Info); const CompoundStmt *CS = cast(S); - for (CompoundStmt::const_body_iterator BI = CS->body_begin(), - BE = CS->body_end(); BI != BE; ++BI) { - EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI, Case); + for (const auto *BI : CS->body()) { + EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); if (ESR == ESR_Succeeded) Case = 0; else if (ESR != ESR_CaseNotFound) diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 625ab1624a..a1dec268d5 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -114,9 +114,8 @@ namespace { /// with no newline after the }. void StmtPrinter::PrintRawCompoundStmt(CompoundStmt *Node) { OS << "{\n"; - for (CompoundStmt::body_iterator I = Node->body_begin(), E = Node->body_end(); - I != E; ++I) - PrintStmt(*I); + for (auto *I : Node->body()) + PrintStmt(I); Indent() << "}"; } diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index 471b0e362e..ad5cf69cd7 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -939,9 +939,8 @@ void CFGBuilder::addLocalScopeForStmt(Stmt *S) { // For compound statement we will be creating explicit scope. if (CompoundStmt *CS = dyn_cast(S)) { - for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end() - ; BI != BE; ++BI) { - Stmt *SI = (*BI)->stripLabelLikeStatements(); + for (auto *BI : CS->body()) { + Stmt *SI = BI->stripLabelLikeStatements(); if (DeclStmt *DS = dyn_cast(SI)) Scope = addLocalScopeForDeclStmt(DS, Scope); } diff --git a/lib/CodeGen/CGClass.cpp b/lib/CodeGen/CGClass.cpp index 0bfc188c1a..071f8b3237 100644 --- a/lib/CodeGen/CGClass.cpp +++ b/lib/CodeGen/CGClass.cpp @@ -1351,11 +1351,8 @@ void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) LexicalScope Scope(*this, RootCS->getSourceRange()); AssignmentMemcpyizer AM(*this, AssignOp, Args); - for (CompoundStmt::const_body_iterator I = RootCS->body_begin(), - E = RootCS->body_end(); - I != E; ++I) { - AM.emitAssignment(*I); - } + for (auto *I : RootCS->body()) + AM.emitAssignment(I); AM.finish(); } diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index cf1c36675e..e16845c680 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -1003,13 +1003,12 @@ static bool isCapturedBy(const VarDecl &var, const Expr *e) { if (const StmtExpr *SE = dyn_cast(e)) { const CompoundStmt *CS = SE->getSubStmt(); - for (CompoundStmt::const_body_iterator BI = CS->body_begin(), - BE = CS->body_end(); BI != BE; ++BI) - if (Expr *E = dyn_cast((*BI))) { + for (const auto *BI : CS->body()) + if (const auto *E = dyn_cast(BI)) { if (isCapturedBy(var, E)) return true; } - else if (DeclStmt *DS = dyn_cast((*BI))) { + else if (const auto *DS = dyn_cast(BI)) { // special case declarations for (const auto *I : DS->decls()) { if (const auto *VD = dyn_cast((I))) { diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index 01ad1f22a2..d0a3f65328 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -2834,9 +2834,8 @@ void CodeGenFunction::EmitObjCAutoreleasePoolStmt( EHStack.pushCleanup(NormalCleanup, token); } - for (CompoundStmt::const_body_iterator I = S.body_begin(), - E = S.body_end(); I != E; ++I) - EmitStmt(*I); + for (const auto *I : S.body()) + EmitStmt(I); if (DI) DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index c9fee1e1f5..d056067835 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -997,9 +997,8 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S, Cxx1yLoc = S->getLocStart(); CompoundStmt *CompStmt = cast(S); - for (CompoundStmt::body_iterator BodyIt = CompStmt->body_begin(), - BodyEnd = CompStmt->body_end(); BodyIt != BodyEnd; ++BodyIt) { - if (!CheckConstexprFunctionStmt(SemaRef, Dcl, *BodyIt, ReturnStmts, + for (auto *BodyIt : CompStmt->body()) { + if (!CheckConstexprFunctionStmt(SemaRef, Dcl, BodyIt, ReturnStmts, Cxx1yLoc)) return false; } @@ -1101,9 +1100,8 @@ bool Sema::CheckConstexprFunctionBody(const FunctionDecl *Dcl, Stmt *Body) { // [... list of cases ...] CompoundStmt *CompBody = cast(Body); SourceLocation Cxx1yLoc; - for (CompoundStmt::body_iterator BodyIt = CompBody->body_begin(), - BodyEnd = CompBody->body_end(); BodyIt != BodyEnd; ++BodyIt) { - if (!CheckConstexprFunctionStmt(*this, Dcl, *BodyIt, ReturnStmts, Cxx1yLoc)) + for (auto *BodyIt : CompBody->body()) { + if (!CheckConstexprFunctionStmt(*this, Dcl, BodyIt, ReturnStmts, Cxx1yLoc)) return false; } diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 60dfb13334..5000d28098 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -5273,13 +5273,12 @@ TreeTransform::TransformCompoundStmt(CompoundStmt *S, bool SubStmtInvalid = false; bool SubStmtChanged = false; SmallVector Statements; - for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end(); - B != BEnd; ++B) { - StmtResult Result = getDerived().TransformStmt(*B); + for (auto *B : S->body()) { + StmtResult Result = getDerived().TransformStmt(B); if (Result.isInvalid()) { // Immediately fail if this was a DeclStmt, since it's very // likely that this will cause problems for future statements. - if (isa(*B)) + if (isa(B)) return StmtError(); // Otherwise, just keep processing substatements and fail later. @@ -5287,7 +5286,7 @@ TreeTransform::TransformCompoundStmt(CompoundStmt *S, continue; } - SubStmtChanged = SubStmtChanged || Result.get() != *B; + SubStmtChanged = SubStmtChanged || Result.get() != B; Statements.push_back(Result.takeAs()); } diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp index 05cbb5bd48..c55f0bca82 100644 --- a/lib/Serialization/ASTWriterStmt.cpp +++ b/lib/Serialization/ASTWriterStmt.cpp @@ -71,9 +71,8 @@ void ASTStmtWriter::VisitNullStmt(NullStmt *S) { void ASTStmtWriter::VisitCompoundStmt(CompoundStmt *S) { VisitStmt(S); Record.push_back(S->size()); - for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end(); - CS != CSEnd; ++CS) - Writer.AddStmt(*CS); + for (auto *CS : S->body()) + Writer.AddStmt(CS); Writer.AddSourceLocation(S->getLBracLoc(), Record); Writer.AddSourceLocation(S->getRBracLoc(), Record); Code = serialization::STMT_COMPOUND; -- 2.40.0