Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 47aa3ca

Browse files
committed
[C++11] Replacing DeclStmt iterators decl_begin() and decl_end() with iterator_range decls(). Updating all of the usages of the iterators with range-based for loops.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203947 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7f99d73 commit 47aa3ca

23 files changed

+58
-82
lines changed

include/clang/AST/DataRecursiveASTVisitor.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -1935,9 +1935,8 @@ DEF_TRAVERSE_STMT(CXXCatchStmt, {
19351935
})
19361936

19371937
DEF_TRAVERSE_STMT(DeclStmt, {
1938-
for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1939-
I != E; ++I) {
1940-
TRY_TO(TraverseDecl(*I));
1938+
for (auto *I : S->decls()) {
1939+
TRY_TO(TraverseDecl(I));
19411940
}
19421941
// Suppress the default iteration over children() by
19431942
// returning. Here's why: A DeclStmt looks like 'type var [=

include/clang/AST/RecursiveASTVisitor.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -1950,9 +1950,8 @@ DEF_TRAVERSE_STMT(CXXCatchStmt, {
19501950
})
19511951

19521952
DEF_TRAVERSE_STMT(DeclStmt, {
1953-
for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();
1954-
I != E; ++I) {
1955-
TRY_TO(TraverseDecl(*I));
1953+
for (auto *I : S->decls()) {
1954+
TRY_TO(TraverseDecl(I));
19561955
}
19571956
// Suppress the default iteration over children() by
19581957
// returning. Here's why: A DeclStmt looks like 'type var [=

include/clang/AST/Stmt.h

+6
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,13 @@ class DeclStmt : public Stmt {
485485

486486
typedef DeclGroupRef::iterator decl_iterator;
487487
typedef DeclGroupRef::const_iterator const_decl_iterator;
488+
typedef llvm::iterator_range<decl_iterator> decl_range;
489+
typedef llvm::iterator_range<const_decl_iterator> decl_const_range;
488490

491+
decl_range decls() { return decl_range(decl_begin(), decl_end()); }
492+
decl_const_range decls() const {
493+
return decl_const_range(decl_begin(), decl_end());
494+
}
489495
decl_iterator decl_begin() { return DG.begin(); }
490496
decl_iterator decl_end() { return DG.end(); }
491497
const_decl_iterator decl_begin() const { return DG.begin(); }

lib/AST/ExprConstant.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -3318,13 +3318,12 @@ static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
33183318

33193319
case Stmt::DeclStmtClass: {
33203320
const DeclStmt *DS = cast<DeclStmt>(S);
3321-
for (DeclStmt::const_decl_iterator DclIt = DS->decl_begin(),
3322-
DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) {
3321+
for (const auto *DclIt : DS->decls()) {
33233322
// Each declaration initialization is its own full-expression.
33243323
// FIXME: This isn't quite right; if we're performing aggregate
33253324
// initialization, each braced subexpression is its own full-expression.
33263325
FullExpressionRAII Scope(Info);
3327-
if (!EvaluateDecl(Info, *DclIt) && !Info.keepEvaluatingAfterFailure())
3326+
if (!EvaluateDecl(Info, DclIt) && !Info.keepEvaluatingAfterFailure())
33283327
return ESR_Failed;
33293328
}
33303329
return ESR_Succeeded;

lib/AST/StmtPrinter.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,7 @@ void StmtPrinter::PrintRawDecl(Decl *D) {
126126
}
127127

128128
void StmtPrinter::PrintRawDeclStmt(const DeclStmt *S) {
129-
DeclStmt::const_decl_iterator Begin = S->decl_begin(), End = S->decl_end();
130-
SmallVector<Decl*, 2> Decls;
131-
for ( ; Begin != End; ++Begin)
132-
Decls.push_back(*Begin);
133-
129+
SmallVector<Decl*, 2> Decls(S->decls());
134130
Decl::printGroup(Decls.data(), Decls.size(), OS, Policy, IndentLevel);
135131
}
136132

lib/AST/StmtProfile.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ void StmtProfiler::VisitStmt(const Stmt *S) {
7979

8080
void StmtProfiler::VisitDeclStmt(const DeclStmt *S) {
8181
VisitStmt(S);
82-
for (DeclStmt::const_decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
83-
D != DEnd; ++D)
84-
VisitDecl(*D);
82+
for (const auto *D : S->decls())
83+
VisitDecl(D);
8584
}
8685

8786
void StmtProfiler::VisitNullStmt(const NullStmt *S) {

lib/Analysis/CFG.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -961,11 +961,9 @@ LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt *DS,
961961
if (!BuildOpts.AddImplicitDtors)
962962
return Scope;
963963

964-
for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
965-
; DI != DE; ++DI) {
966-
if (VarDecl *VD = dyn_cast<VarDecl>(*DI))
964+
for (auto *DI : DS->decls())
965+
if (VarDecl *VD = dyn_cast<VarDecl>(DI))
967966
Scope = addLocalScopeForVarDecl(VD, Scope);
968-
}
969967
return Scope;
970968
}
971969

lib/Analysis/Consumed.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,9 @@ void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
851851
}
852852

853853
void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
854-
for (DeclStmt::const_decl_iterator DI = DeclS->decl_begin(),
855-
DE = DeclS->decl_end(); DI != DE; ++DI) {
856-
857-
if (isa<VarDecl>(*DI)) VisitVarDecl(cast<VarDecl>(*DI));
858-
}
854+
for (const auto *DI : DeclS->decls())
855+
if (isa<VarDecl>(DI))
856+
VisitVarDecl(cast<VarDecl>(DI));
859857

860858
if (DeclS->isSingleDecl())
861859
if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))

lib/Analysis/LiveVariables.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,8 @@ void TransferFunctions::VisitDeclRefExpr(DeclRefExpr *DR) {
389389
}
390390

391391
void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
392-
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE = DS->decl_end();
393-
DI != DE; ++DI)
394-
if (VarDecl *VD = dyn_cast<VarDecl>(*DI)) {
392+
for (const auto *DI : DS->decls())
393+
if (const auto *VD = dyn_cast<VarDecl>(DI)) {
395394
if (!isAlwaysAlive(VD))
396395
val.liveDecls = LV.DSetFact.remove(val.liveDecls, VD);
397396
}

lib/Analysis/PseudoConstantAnalysis.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,9 @@ void PseudoConstantAnalysis::RunAnalysis() {
171171
case Stmt::DeclStmtClass: {
172172
const DeclStmt *DS = cast<DeclStmt>(Head);
173173
// Iterate over each decl and see if any of them contain reference decls
174-
for (DeclStmt::const_decl_iterator I = DS->decl_begin(),
175-
E = DS->decl_end(); I != E; ++I) {
174+
for (const auto *I : DS->decls()) {
176175
// We only care about VarDecls
177-
const VarDecl *VD = dyn_cast<VarDecl>(*I);
176+
const VarDecl *VD = dyn_cast<VarDecl>(I);
178177
if (!VD)
179178
continue;
180179

lib/Analysis/UninitializedValues.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,8 @@ void ClassifyRefs::classify(const Expr *E, Class C) {
373373
}
374374

375375
void ClassifyRefs::VisitDeclStmt(DeclStmt *DS) {
376-
for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
377-
DI != DE; ++DI) {
378-
VarDecl *VD = dyn_cast<VarDecl>(*DI);
376+
for (auto *DI : DS->decls()) {
377+
VarDecl *VD = dyn_cast<VarDecl>(DI);
379378
if (VD && isTrackedVar(VD))
380379
if (const DeclRefExpr *DRE = getSelfInitExpr(VD))
381380
Classification[DRE] = SelfInit;
@@ -694,9 +693,8 @@ void TransferFunctions::VisitBinaryOperator(BinaryOperator *BO) {
694693
}
695694

696695
void TransferFunctions::VisitDeclStmt(DeclStmt *DS) {
697-
for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
698-
DI != DE; ++DI) {
699-
VarDecl *VD = dyn_cast<VarDecl>(*DI);
696+
for (auto *DI : DS->decls()) {
697+
VarDecl *VD = dyn_cast<VarDecl>(DI);
700698
if (VD && isTrackedVar(VD)) {
701699
if (getSelfInitExpr(VD)) {
702700
// If the initializer consists solely of a reference to itself, we

lib/CodeGen/CGDecl.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -1013,10 +1013,9 @@ static bool isCapturedBy(const VarDecl &var, const Expr *e) {
10131013
}
10141014
else if (DeclStmt *DS = dyn_cast<DeclStmt>((*BI))) {
10151015
// special case declarations
1016-
for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1017-
I != E; ++I) {
1018-
if (VarDecl *VD = dyn_cast<VarDecl>((*I))) {
1019-
Expr *Init = VD->getInit();
1016+
for (const auto *I : DS->decls()) {
1017+
if (const auto *VD = dyn_cast<VarDecl>((I))) {
1018+
const Expr *Init = VD->getInit();
10201019
if (Init && isCapturedBy(var, Init))
10211020
return true;
10221021
}

lib/CodeGen/CGStmt.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,8 @@ void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
891891
if (HaveInsertPoint())
892892
EmitStopPoint(&S);
893893

894-
for (DeclStmt::const_decl_iterator I = S.decl_begin(), E = S.decl_end();
895-
I != E; ++I)
896-
EmitDecl(**I);
894+
for (const auto *I : S.decls())
895+
EmitDecl(*I);
897896
}
898897

899898
void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {

lib/Rewrite/Frontend/RewriteObjC.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -4753,9 +4753,7 @@ Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) {
47534753
RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin());
47544754

47554755
// Blocks rewrite rules.
4756-
for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end();
4757-
DI != DE; ++DI) {
4758-
Decl *SD = *DI;
4756+
for (auto *SD : DS->decls()) {
47594757
if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) {
47604758
if (isTopLevelBlockPointerType(ND->getType()))
47614759
RewriteBlockPointerDecl(ND);

lib/Sema/JumpDiagnostics.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,8 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope)
367367
if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
368368
// The decl statement creates a scope if any of the decls in it are VLAs
369369
// or have the cleanup attribute.
370-
for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
371-
I != E; ++I)
372-
BuildScopeInformation(*I, ParentScope);
370+
for (auto *I : DS->decls())
371+
BuildScopeInformation(I, ParentScope);
373372
continue;
374373
}
375374
// Disallow jumps into any part of an @try statement by pushing a scope and

lib/Sema/SemaDeclCXX.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,8 @@ static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl,
813813
// C++11 [dcl.constexpr]p3 and p4:
814814
// The definition of a constexpr function(p3) or constructor(p4) [...] shall
815815
// contain only
816-
for (DeclStmt::decl_iterator DclIt = DS->decl_begin(),
817-
DclEnd = DS->decl_end(); DclIt != DclEnd; ++DclIt) {
818-
switch ((*DclIt)->getKind()) {
816+
for (const auto *DclIt : DS->decls()) {
817+
switch (DclIt->getKind()) {
819818
case Decl::StaticAssert:
820819
case Decl::Using:
821820
case Decl::UsingShadow:
@@ -831,7 +830,7 @@ static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl,
831830
case Decl::TypeAlias: {
832831
// - typedef declarations and alias-declarations that do not define
833832
// classes or enumerations,
834-
TypedefNameDecl *TN = cast<TypedefNameDecl>(*DclIt);
833+
const auto *TN = cast<TypedefNameDecl>(DclIt);
835834
if (TN->getUnderlyingType()->isVariablyModifiedType()) {
836835
// Don't allow variably-modified types in constexpr functions.
837836
TypeLoc TL = TN->getTypeSourceInfo()->getTypeLoc();
@@ -846,7 +845,7 @@ static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl,
846845
case Decl::Enum:
847846
case Decl::CXXRecord:
848847
// C++1y allows types to be defined, not just declared.
849-
if (cast<TagDecl>(*DclIt)->isThisDeclarationADefinition())
848+
if (cast<TagDecl>(DclIt)->isThisDeclarationADefinition())
850849
SemaRef.Diag(DS->getLocStart(),
851850
SemaRef.getLangOpts().CPlusPlus1y
852851
? diag::warn_cxx11_compat_constexpr_type_definition
@@ -865,7 +864,7 @@ static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl,
865864
// C++1y [dcl.constexpr]p3 allows anything except:
866865
// a definition of a variable of non-literal type or of static or
867866
// thread storage duration or for which no initialization is performed.
868-
VarDecl *VD = cast<VarDecl>(*DclIt);
867+
const auto *VD = cast<VarDecl>(DclIt);
869868
if (VD->isThisDeclarationADefinition()) {
870869
if (VD->isStaticLocal()) {
871870
SemaRef.Diag(VD->getLocation(),

lib/Sema/SemaStmt.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -1594,14 +1594,13 @@ Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
15941594
// C99 6.8.5p3: The declaration part of a 'for' statement shall only
15951595
// declare identifiers for objects having storage class 'auto' or
15961596
// 'register'.
1597-
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
1598-
DI!=DE; ++DI) {
1599-
VarDecl *VD = dyn_cast<VarDecl>(*DI);
1597+
for (auto *DI : DS->decls()) {
1598+
VarDecl *VD = dyn_cast<VarDecl>(DI);
16001599
if (VD && VD->isLocalVarDecl() && !VD->hasLocalStorage())
16011600
VD = 0;
16021601
if (VD == 0) {
1603-
Diag((*DI)->getLocation(), diag::err_non_local_variable_decl_in_for);
1604-
(*DI)->setInvalidDecl();
1602+
Diag(DI->getLocation(), diag::err_non_local_variable_decl_in_for);
1603+
DI->setInvalidDecl();
16051604
}
16061605
}
16071606
}

lib/Sema/TreeTransform.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -5684,14 +5684,12 @@ StmtResult
56845684
TreeTransform<Derived>::TransformDeclStmt(DeclStmt *S) {
56855685
bool DeclChanged = false;
56865686
SmallVector<Decl *, 4> Decls;
5687-
for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
5688-
D != DEnd; ++D) {
5689-
Decl *Transformed = getDerived().TransformDefinition((*D)->getLocation(),
5690-
*D);
5687+
for (auto *D : S->decls()) {
5688+
Decl *Transformed = getDerived().TransformDefinition(D->getLocation(), D);
56915689
if (!Transformed)
56925690
return StmtError();
56935691

5694-
if (Transformed != *D)
5692+
if (Transformed != D)
56955693
DeclChanged = true;
56965694

56975695
Decls.push_back(Transformed);

lib/StaticAnalyzer/Checkers/CStringChecker.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1935,9 +1935,8 @@ void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
19351935
// Record string length for char a[] = "abc";
19361936
ProgramStateRef state = C.getState();
19371937

1938-
for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1939-
I != E; ++I) {
1940-
const VarDecl *D = dyn_cast<VarDecl>(*I);
1938+
for (const auto *I : DS->decls()) {
1939+
const VarDecl *D = dyn_cast<VarDecl>(I);
19411940
if (!D)
19421941
continue;
19431942

lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,8 @@ class DeadStoreObs : public LiveVariables::Observer {
313313
else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S))
314314
// Iterate through the decls. Warn if any initializers are complex
315315
// expressions that are not live (never used).
316-
for (DeclStmt::const_decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
317-
DI != DE; ++DI) {
318-
319-
VarDecl *V = dyn_cast<VarDecl>(*DI);
316+
for (const auto *DI : DS->decls()) {
317+
const auto *V = dyn_cast<VarDecl>(DI);
320318

321319
if (!V)
322320
continue;

lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR,
145145
void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) {
146146
VisitChildren(S);
147147

148-
for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();I!=E; ++I)
149-
if (VarDecl *VD = dyn_cast<VarDecl>(*I))
148+
for (auto *I : S->decls())
149+
if (VarDecl *VD = dyn_cast<VarDecl>(I))
150150
VisitVarDecl(VD);
151151
}
152152

lib/StaticAnalyzer/Checkers/MallocSizeofChecker.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ class CastedAllocFinder
101101
}
102102

103103
TypeCallPair VisitDeclStmt(const DeclStmt *S) {
104-
for (DeclStmt::const_decl_iterator I = S->decl_begin(), E = S->decl_end();
105-
I!=E; ++I)
106-
if (const VarDecl *VD = dyn_cast<VarDecl>(*I))
104+
for (const auto *I : S->decls())
105+
if (const VarDecl *VD = dyn_cast<VarDecl>(I))
107106
if (const Expr *Init = VD->getInit())
108107
VisitChild(VD, Init);
109108
return TypeCallPair();

tools/libclang/CIndex.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -2072,9 +2072,8 @@ void EnqueueVisitor::VisitDependentScopeDeclRefExpr(
20722072
void EnqueueVisitor::VisitDeclStmt(const DeclStmt *S) {
20732073
unsigned size = WL.size();
20742074
bool isFirst = true;
2075-
for (DeclStmt::const_decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
2076-
D != DEnd; ++D) {
2077-
AddDecl(*D, isFirst);
2075+
for (const auto *D : S->decls()) {
2076+
AddDecl(D, isFirst);
20782077
isFirst = false;
20792078
}
20802079
if (size == WL.size())

0 commit comments

Comments
 (0)