[C++11] Replacing Decl iterators attr_begin() and attr_end() with iterator_range attrs(). Updating all of the usages of the iterators with range-based for loops.

This is a reapplication of r203236 with modifications to the definition of attrs() and following the new style guidelines on auto usage.

llvm-svn: 203362
This commit is contained in:
Aaron Ballman 2014-03-08 22:19:01 +00:00
parent 4203039760
commit b97112e4bd
13 changed files with 62 additions and 77 deletions

View File

@ -598,8 +598,8 @@ bool DataRecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
}
// Visit any attributes attached to this declaration.
for (Decl::attr_iterator I=D->attr_begin(), E=D->attr_end(); I != E; ++I) {
if (!getDerived().TraverseAttr(*I))
for (auto *I : D->attrs()) {
if (!getDerived().TraverseAttr(I))
return false;
}
return true;

View File

@ -447,9 +447,12 @@ public:
}
typedef AttrVec::const_iterator attr_iterator;
typedef llvm::iterator_range<attr_iterator> attr_range;
attr_range attrs() const {
return attr_range(attr_begin(), attr_end());
}
// FIXME: Do not rely on iterators having comparable singular values.
// Note that this should error out if they do not.
attr_iterator attr_begin() const {
return hasAttrs() ? getAttrs().begin() : 0;
}

View File

@ -669,8 +669,8 @@ bool RecursiveASTVisitor<Derived>::TraverseDecl(Decl *D) {
}
// Visit any attributes attached to this declaration.
for (Decl::attr_iterator I=D->attr_begin(), E=D->attr_end(); I != E; ++I) {
if (!getDerived().TraverseAttr(*I))
for (auto *I : D->attrs()) {
if (!getDerived().TraverseAttr(I))
return false;
}
return true;

View File

@ -762,7 +762,7 @@ void ASTDumper::dumpDecl(const Decl *D) {
if (ND->isHidden())
OS << " hidden";
bool HasAttrs = D->attr_begin() != D->attr_end();
bool HasAttrs = D->hasAttrs();
const FullComment *Comment =
D->getASTContext().getLocalCommentForDeclUncached(D);
// Decls within functions are visited by the body

View File

@ -3499,8 +3499,8 @@ LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
void ValueDecl::anchor() { }
bool ValueDecl::isWeak() const {
for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
for (const auto *I : attrs())
if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I))
return true;
return isWeakImported();

View File

@ -408,8 +408,8 @@ AvailabilityResult Decl::getAvailability(std::string *Message) const {
AvailabilityResult Result = AR_Available;
std::string ResultMessage;
for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
for (const auto *A : attrs()) {
if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
if (Result >= AR_Deprecated)
continue;
@ -420,13 +420,13 @@ AvailabilityResult Decl::getAvailability(std::string *Message) const {
continue;
}
if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
if (Message)
*Message = Unavailable->getMessage();
return AR_Unavailable;
}
if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
Message);
@ -482,11 +482,11 @@ bool Decl::isWeakImported() const {
if (!canBeWeakImported(IsDefinition))
return false;
for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
if (isa<WeakImportAttr>(*A))
for (const auto *A : attrs()) {
if (isa<WeakImportAttr>(A))
return true;
if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
if (CheckAvailability(getASTContext(), Availability, 0)
== AR_NotYetIntroduced)
return true;

View File

@ -2638,12 +2638,13 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx,
return Result.TakeString();
}
for (Decl::attr_iterator i = ND->attr_begin(); i != ND->attr_end(); ++i) {
if (AnnotateAttr *Attr = dyn_cast_or_null<AnnotateAttr>(*i)) {
Result.AddAnnotation(Result.getAllocator().CopyString(Attr->getAnnotation()));
}
}
for (specific_attr_iterator<AnnotateAttr>
i = ND->specific_attr_begin<AnnotateAttr>(),
e = ND->specific_attr_end<AnnotateAttr>();
i != e; ++i)
Result.AddAnnotation(
Result.getAllocator().CopyString((*i)->getAnnotation()));
AddResultTypeChunk(Ctx, Policy, ND, Result);
if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {

View File

@ -1791,16 +1791,16 @@ void Sema::MergeTypedefNameDecl(TypedefNameDecl *New, LookupResult &OldDecls) {
static bool DeclHasAttr(const Decl *D, const Attr *A) {
const OwnershipAttr *OA = dyn_cast<OwnershipAttr>(A);
const AnnotateAttr *Ann = dyn_cast<AnnotateAttr>(A);
for (Decl::attr_iterator i = D->attr_begin(), e = D->attr_end(); i != e; ++i)
if ((*i)->getKind() == A->getKind()) {
for (const auto *i : D->attrs())
if (i->getKind() == A->getKind()) {
if (Ann) {
if (Ann->getAnnotation() == cast<AnnotateAttr>(*i)->getAnnotation())
if (Ann->getAnnotation() == cast<AnnotateAttr>(i)->getAnnotation())
return true;
continue;
}
// FIXME: Don't hardcode this check
if (OA && isa<OwnershipAttr>(*i))
return OA->getOwnKind() == cast<OwnershipAttr>(*i)->getOwnKind();
if (OA && isa<OwnershipAttr>(i))
return OA->getOwnKind() == cast<OwnershipAttr>(i)->getOwnKind();
return true;
}
@ -1997,12 +1997,9 @@ static const Decl *getDefinition(const Decl *D) {
}
static bool hasAttribute(const Decl *D, attr::Kind Kind) {
for (Decl::attr_iterator I = D->attr_begin(), E = D->attr_end();
I != E; ++I) {
Attr *Attribute = *I;
for (const auto *Attribute : D->attrs())
if (Attribute->getKind() == Kind)
return true;
}
return false;
}

View File

@ -12673,48 +12673,41 @@ bool Sema::checkThisInStaticMemberFunctionAttributes(CXXMethodDecl *Method) {
FindCXXThisExpr Finder(*this);
// Check attributes.
for (Decl::attr_iterator A = Method->attr_begin(), AEnd = Method->attr_end();
A != AEnd; ++A) {
for (const auto *A : Method->attrs()) {
// FIXME: This should be emitted by tblgen.
Expr *Arg = 0;
ArrayRef<Expr *> Args;
if (GuardedByAttr *G = dyn_cast<GuardedByAttr>(*A))
if (const auto *G = dyn_cast<GuardedByAttr>(A))
Arg = G->getArg();
else if (PtGuardedByAttr *G = dyn_cast<PtGuardedByAttr>(*A))
else if (const auto *G = dyn_cast<PtGuardedByAttr>(A))
Arg = G->getArg();
else if (AcquiredAfterAttr *AA = dyn_cast<AcquiredAfterAttr>(*A))
else if (const auto *AA = dyn_cast<AcquiredAfterAttr>(A))
Args = ArrayRef<Expr *>(AA->args_begin(), AA->args_size());
else if (AcquiredBeforeAttr *AB = dyn_cast<AcquiredBeforeAttr>(*A))
else if (const auto *AB = dyn_cast<AcquiredBeforeAttr>(A))
Args = ArrayRef<Expr *>(AB->args_begin(), AB->args_size());
else if (ExclusiveLockFunctionAttr *ELF
= dyn_cast<ExclusiveLockFunctionAttr>(*A))
else if (const auto *ELF = dyn_cast<ExclusiveLockFunctionAttr>(A))
Args = ArrayRef<Expr *>(ELF->args_begin(), ELF->args_size());
else if (SharedLockFunctionAttr *SLF
= dyn_cast<SharedLockFunctionAttr>(*A))
else if (const auto *SLF = dyn_cast<SharedLockFunctionAttr>(A))
Args = ArrayRef<Expr *>(SLF->args_begin(), SLF->args_size());
else if (ExclusiveTrylockFunctionAttr *ETLF
= dyn_cast<ExclusiveTrylockFunctionAttr>(*A)) {
else if (const auto *ETLF = dyn_cast<ExclusiveTrylockFunctionAttr>(A)) {
Arg = ETLF->getSuccessValue();
Args = ArrayRef<Expr *>(ETLF->args_begin(), ETLF->args_size());
} else if (SharedTrylockFunctionAttr *STLF
= dyn_cast<SharedTrylockFunctionAttr>(*A)) {
} else if (const auto *STLF = dyn_cast<SharedTrylockFunctionAttr>(A)) {
Arg = STLF->getSuccessValue();
Args = ArrayRef<Expr *>(STLF->args_begin(), STLF->args_size());
} else if (UnlockFunctionAttr *UF = dyn_cast<UnlockFunctionAttr>(*A))
} else if (const auto *UF = dyn_cast<UnlockFunctionAttr>(A))
Args = ArrayRef<Expr *>(UF->args_begin(), UF->args_size());
else if (LockReturnedAttr *LR = dyn_cast<LockReturnedAttr>(*A))
else if (const auto *LR = dyn_cast<LockReturnedAttr>(A))
Arg = LR->getArg();
else if (LocksExcludedAttr *LE = dyn_cast<LocksExcludedAttr>(*A))
else if (const auto *LE = dyn_cast<LocksExcludedAttr>(A))
Args = ArrayRef<Expr *>(LE->args_begin(), LE->args_size());
else if (RequiresCapabilityAttr *RC
= dyn_cast<RequiresCapabilityAttr>(*A))
else if (const auto *RC = dyn_cast<RequiresCapabilityAttr>(A))
Args = ArrayRef<Expr *>(RC->args_begin(), RC->args_size());
else if (AcquireCapabilityAttr *AC = dyn_cast<AcquireCapabilityAttr>(*A))
else if (const auto *AC = dyn_cast<AcquireCapabilityAttr>(A))
Args = ArrayRef<Expr *>(AC->args_begin(), AC->args_size());
else if (TryAcquireCapabilityAttr *AC
= dyn_cast<TryAcquireCapabilityAttr>(*A))
Args = ArrayRef<Expr *>(AC->args_begin(), AC->args_size());
else if (ReleaseCapabilityAttr *RC = dyn_cast<ReleaseCapabilityAttr>(*A))
else if (const auto *AC = dyn_cast<TryAcquireCapabilityAttr>(A))
Args = ArrayRef<Expr *>(AC->args_begin(), AC->args_size());
else if (const auto *RC = dyn_cast<ReleaseCapabilityAttr>(A))
Args = ArrayRef<Expr *>(RC->args_begin(), RC->args_size());
if (Arg && !Finder.TraverseStmt(Arg))

View File

@ -1940,13 +1940,11 @@ void Sema::DiagnoseMissingDesignatedInitOverrides(
static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
ObjCPropertyDecl *Property) {
// Should we just clone all attributes over?
for (Decl::attr_iterator A = Property->attr_begin(),
AEnd = Property->attr_end();
A != AEnd; ++A) {
if (isa<DeprecatedAttr>(*A) ||
isa<UnavailableAttr>(*A) ||
isa<AvailabilityAttr>(*A))
PropertyMethod->addAttr((*A)->clone(S.Context));
for (const auto *A : Property->attrs()) {
if (isa<DeprecatedAttr>(A) ||
isa<UnavailableAttr>(A) ||
isa<AvailabilityAttr>(A))
PropertyMethod->addAttr(A->clone(S.Context));
}
}

View File

@ -168,10 +168,7 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
const Decl *Tmpl, Decl *New,
LateInstantiatedAttrVec *LateAttrs,
LocalInstantiationScope *OuterMostScope) {
for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
i != e; ++i) {
const Attr *TmplAttr = *i;
for (const auto *TmplAttr : Tmpl->attrs()) {
// FIXME: This should be generalized to more than just the AlignedAttr.
const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
if (Aligned && Aligned->isAlignmentDependent()) {

View File

@ -1671,9 +1671,8 @@ bool CursorVisitor::VisitCXXRecordDecl(CXXRecordDecl *D) {
}
bool CursorVisitor::VisitAttributes(Decl *D) {
for (AttrVec::const_iterator i = D->attr_begin(), e = D->attr_end();
i != e; ++i)
if (Visit(MakeCXCursor(*i, D, TU)))
for (const auto *I : D->attrs())
if (Visit(MakeCXCursor(I, D, TU)))
return true;
return false;
@ -6026,9 +6025,8 @@ static int getCursorPlatformAvailabilityForDecl(const Decl *D,
int availability_size) {
bool HadAvailAttr = false;
int N = 0;
for (Decl::attr_iterator A = D->attr_begin(), AEnd = D->attr_end(); A != AEnd;
++A) {
if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
for (auto A : D->attrs()) {
if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
HadAvailAttr = true;
if (always_deprecated)
*always_deprecated = 1;
@ -6037,7 +6035,7 @@ static int getCursorPlatformAvailabilityForDecl(const Decl *D,
continue;
}
if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(A)) {
HadAvailAttr = true;
if (always_unavailable)
*always_unavailable = 1;
@ -6047,7 +6045,7 @@ static int getCursorPlatformAvailabilityForDecl(const Decl *D,
continue;
}
if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(*A)) {
if (AvailabilityAttr *Avail = dyn_cast<AvailabilityAttr>(A)) {
HadAvailAttr = true;
if (N < availability_size) {
availability[N].Platform

View File

@ -67,9 +67,7 @@ AttrListInfo::AttrListInfo(const Decl *D, IndexingContext &IdxCtx)
if (!D->hasAttrs())
return;
for (AttrVec::const_iterator AttrI = D->attr_begin(), AttrE = D->attr_end();
AttrI != AttrE; ++AttrI) {
const Attr *A = *AttrI;
for (const auto *A : D->attrs()) {
CXCursor C = MakeCXCursor(A, D, IdxCtx.CXTU);
CXIdxLoc Loc = IdxCtx.getIndexLoc(A->getLocation());
switch (C.kind) {