From: Aaron Ballman Date: Mon, 10 Mar 2014 17:08:28 +0000 (+0000) Subject: [C++11] Replacing DeclBase iterators specific_attr_begin() and specific_attr_end... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=15fd658d179dfa842e067e34d4442ead6b1e85e4;p=clang [C++11] Replacing DeclBase iterators specific_attr_begin() and specific_attr_end() with iterator_range specific_attrs(). Updating all of the usages of the iterators with range-based for loops. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@203474 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 49e0de11f4..cf027387cb 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -471,6 +471,12 @@ public: HasAttrs = false; } + template + llvm::iterator_range> specific_attrs() const { + return llvm::iterator_range>( + specific_attr_begin(), specific_attr_end()); + } + template specific_attr_iterator specific_attr_begin() const { return specific_attr_iterator(attr_begin()); diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index eaaf927751..89f7291c4d 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -207,11 +207,8 @@ static Optional getVisibilityOf(const NamedDecl *D, // If we're on Mac OS X, an 'availability' for Mac OS X attribute // implies visibility(default). if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) { - for (specific_attr_iterator - A = D->specific_attr_begin(), - AEnd = D->specific_attr_end(); - A != AEnd; ++A) - if ((*A)->getPlatform()->getName().equals("macosx")) + for (const auto *A : D->specific_attrs()) + if (A->getPlatform()->getName().equals("macosx")) return DefaultVisibility; } diff --git a/lib/Analysis/ThreadSafety.cpp b/lib/Analysis/ThreadSafety.cpp index 6e9845db34..cf8b289207 100644 --- a/lib/Analysis/ThreadSafety.cpp +++ b/lib/Analysis/ThreadSafety.cpp @@ -1878,10 +1878,8 @@ void BuildLockset::checkAccess(const Expr *Exp, AccessKind AK) { Analyzer->Handler.handleNoMutexHeld(D, POK_VarAccess, AK, Exp->getExprLoc()); - for (specific_attr_iterator - I = D->specific_attr_begin(), - E = D->specific_attr_end(); I != E; ++I) - warnIfMutexNotHeld(D, Exp, AK, (*I)->getArg(), POK_VarAccess); + for (const auto *I : D->specific_attrs()) + warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarAccess); } /// \brief Checks pt_guarded_by and pt_guarded_var attributes. @@ -1916,10 +1914,8 @@ void BuildLockset::checkPtAccess(const Expr *Exp, AccessKind AK) { Analyzer->Handler.handleNoMutexHeld(D, POK_VarDereference, AK, Exp->getExprLoc()); - for (specific_attr_iterator - I = D->specific_attr_begin(), - E = D->specific_attr_end(); I != E; ++I) - warnIfMutexNotHeld(D, Exp, AK, (*I)->getArg(), POK_VarDereference); + for (auto const *I : D->specific_attrs()) + warnIfMutexNotHeld(D, Exp, AK, I->getArg(), POK_VarDereference); } diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index 5028d5ddec..2d8389f020 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -1569,12 +1569,10 @@ void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) { assert(D->hasAttr() && "no annotate attribute"); // FIXME We create a new bitcast for every annotation because that's what // llvm-gcc was doing. - for (specific_attr_iterator - ai = D->specific_attr_begin(), - ae = D->specific_attr_end(); ai != ae; ++ai) + for (const auto *I : D->specific_attrs()) EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation), Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()), - (*ai)->getAnnotation(), D->getLocation()); + I->getAnnotation(), D->getLocation()); } llvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D, @@ -1584,15 +1582,13 @@ llvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D, llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, CGM.Int8PtrTy); - for (specific_attr_iterator - ai = D->specific_attr_begin(), - ae = D->specific_attr_end(); ai != ae; ++ai) { + for (const auto *I : D->specific_attrs()) { // FIXME Always emit the cast inst so we can differentiate between // annotation on the first field of a struct and annotation on the struct // itself. if (VTy != CGM.Int8PtrTy) V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy)); - V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation()); + V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation()); V = Builder.CreateBitCast(V, VTy); } diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index c8dd8136fd..4f040e2780 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -1051,10 +1051,8 @@ void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV) { assert(D->hasAttr() && "no annotate attribute"); // Get the struct elements for these annotations. - for (specific_attr_iterator - ai = D->specific_attr_begin(), - ae = D->specific_attr_end(); ai != ae; ++ai) - Annotations.push_back(EmitAnnotateAttr(GV, *ai, D->getLocation())); + for (const auto *I : D->specific_attrs()) + Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); } bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index de82ea8bd1..512916e9a8 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -730,11 +730,7 @@ static void CheckNonNullArguments(Sema &S, const Expr * const *ExprArgs, SourceLocation CallSiteLoc) { // Check the attributes attached to the method/function itself. - for (specific_attr_iterator - I = FDecl->specific_attr_begin(), - E = FDecl->specific_attr_end(); I != E; ++I) { - - const NonNullAttr *NonNull = *I; + for (const auto *NonNull : FDecl->specific_attrs()) { for (NonNullAttr::args_iterator i = NonNull->args_begin(), e = NonNull->args_end(); i != e; ++i) { @@ -771,14 +767,11 @@ void Sema::checkCall(NamedDecl *FDecl, ArrayRef Args, // Printf and scanf checking. llvm::SmallBitVector CheckedVarArgs; if (FDecl) { - for (specific_attr_iterator - I = FDecl->specific_attr_begin(), - E = FDecl->specific_attr_end(); - I != E; ++I) { + for (const auto *I : FDecl->specific_attrs()) { // Only create vector if there are format attributes. CheckedVarArgs.resize(Args.size()); - CheckFormatArguments(*I, Args, IsMemberFunction, CallType, Loc, Range, + CheckFormatArguments(I, Args, IsMemberFunction, CallType, Loc, Range, CheckedVarArgs); } } @@ -799,12 +792,8 @@ void Sema::checkCall(NamedDecl *FDecl, ArrayRef Args, CheckNonNullArguments(*this, FDecl, Args.data(), Loc); // Type safety checking. - for (specific_attr_iterator - i = FDecl->specific_attr_begin(), - e = FDecl->specific_attr_end(); - i != e; ++i) { - CheckArgumentWithTypeTag(*i, Args.data()); - } + for (const auto *I : FDecl->specific_attrs()) + CheckArgumentWithTypeTag(I, Args.data()); } } @@ -2141,10 +2130,7 @@ checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef Args, if (const ParmVarDecl *PV = dyn_cast(VD)) { if (const NamedDecl *ND = dyn_cast(PV->getDeclContext())) { int PVIndex = PV->getFunctionScopeIndex() + 1; - for (specific_attr_iterator - i = ND->specific_attr_begin(), - e = ND->specific_attr_end(); i != e ; ++i) { - FormatAttr *PVFormat = *i; + for (const auto *PVFormat : ND->specific_attrs()) { // adjust for implicit parameter if (const CXXMethodDecl *MD = dyn_cast(ND)) if (MD->isInstance()) diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 6a0c4c4d04..2724c646be 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -2638,12 +2638,8 @@ CodeCompletionResult::CreateCodeCompletionString(ASTContext &Ctx, return Result.TakeString(); } - for (specific_attr_iterator - i = ND->specific_attr_begin(), - e = ND->specific_attr_end(); - i != e; ++i) - Result.AddAnnotation( - Result.getAllocator().CopyString((*i)->getAnnotation())); + for (const auto *I : ND->specific_attrs()) + Result.AddAnnotation(Result.getAllocator().CopyString(I->getAnnotation())); AddResultTypeChunk(Ctx, Policy, ND, Result); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 820f908e35..8e796e6be9 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1825,9 +1825,7 @@ static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { AlignedAttr *OldAlignasAttr = 0; AlignedAttr *OldStrictestAlignAttr = 0; unsigned OldAlign = 0; - for (specific_attr_iterator - I = Old->specific_attr_begin(), - E = Old->specific_attr_end(); I != E; ++I) { + for (auto *I : Old->specific_attrs()) { // FIXME: We have no way of representing inherited dependent alignments // in a case like: // template struct alignas(A) X; @@ -1838,26 +1836,24 @@ static bool mergeAlignedAttrs(Sema &S, NamedDecl *New, Decl *Old) { return false; if (I->isAlignas()) - OldAlignasAttr = *I; + OldAlignasAttr = I; unsigned Align = I->getAlignment(S.Context); if (Align > OldAlign) { OldAlign = Align; - OldStrictestAlignAttr = *I; + OldStrictestAlignAttr = I; } } // Look for alignas attributes on New. AlignedAttr *NewAlignasAttr = 0; unsigned NewAlign = 0; - for (specific_attr_iterator - I = New->specific_attr_begin(), - E = New->specific_attr_end(); I != E; ++I) { + for (auto *I : New->specific_attrs()) { if (I->isAlignmentDependent()) return false; if (I->isAlignas()) - NewAlignasAttr = *I; + NewAlignasAttr = I; unsigned Align = I->getAlignment(S.Context); if (Align > NewAlign) @@ -2103,15 +2099,12 @@ void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, // we process them. if (!foundAny) New->setAttrs(AttrVec()); - for (specific_attr_iterator - i = Old->specific_attr_begin(), - e = Old->specific_attr_end(); - i != e; ++i) { + for (auto *I : Old->specific_attrs()) { bool Override = false; // Ignore deprecated/unavailable/availability attributes if requested. - if (isa(*i) || - isa(*i) || - isa(*i)) { + if (isa(I) || + isa(I) || + isa(I)) { switch (AMK) { case AMK_None: continue; @@ -2126,10 +2119,10 @@ void Sema::mergeDeclAttributes(NamedDecl *New, Decl *Old, } // Already handled. - if (isa(*i)) + if (isa(I)) continue; - if (mergeDeclAttribute(*this, New, *i, Override)) + if (mergeDeclAttribute(*this, New, I, Override)) foundAny = true; } @@ -2171,12 +2164,10 @@ static void mergeParamDeclAttributes(ParmVarDecl *newDecl, // done before we process them. if (!foundAny) newDecl->setAttrs(AttrVec()); - for (specific_attr_iterator - i = oldDecl->specific_attr_begin(), - e = oldDecl->specific_attr_end(); i != e; ++i) { - if (!DeclHasAttr(newDecl, *i)) { + for (const auto *I : oldDecl->specific_attrs()) { + if (!DeclHasAttr(newDecl, I)) { InheritableAttr *newAttr = - cast((*i)->clone(S.Context)); + cast(I->clone(S.Context)); newAttr->setInherited(true); newDecl->addAttr(newAttr); foundAny = true; @@ -8942,10 +8933,7 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) { !VD->getType()->isIntegralOrEnumerationType()) return; - for (specific_attr_iterator - I = ThisDecl->specific_attr_begin(), - E = ThisDecl->specific_attr_end(); - I != E; ++I) { + for (const auto *I : ThisDecl->specific_attrs()) { const Expr *MagicValueExpr = VD->getInit(); if (!MagicValueExpr) { continue; diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 7817ca9efc..26fa4cddd3 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1284,15 +1284,13 @@ static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { } // Check we don't have a conflict with another ownership attribute. - for (specific_attr_iterator - i = D->specific_attr_begin(), - e = D->specific_attr_end(); i != e; ++i) { + for (const auto *I : D->specific_attrs()) { // FIXME: A returns attribute should conflict with any returns attribute // with a different index too. - if ((*i)->getOwnKind() != K && (*i)->args_end() != - std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { + if (I->getOwnKind() != K && I->args_end() != + std::find(I->args_begin(), I->args_end(), Idx)) { S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) - << AL.getName() << *i; + << AL.getName() << I; return; } } @@ -2445,18 +2443,14 @@ FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, int FirstArg, unsigned AttrSpellingListIndex) { // Check whether we already have an equivalent format attribute. - for (specific_attr_iterator - i = D->specific_attr_begin(), - e = D->specific_attr_end(); - i != e ; ++i) { - FormatAttr *f = *i; - if (f->getType() == Format && - f->getFormatIdx() == FormatIdx && - f->getFirstArg() == FirstArg) { + for (auto *F : D->specific_attrs()) { + if (F->getType() == Format && + F->getFormatIdx() == FormatIdx && + F->getFirstArg() == FirstArg) { // If we don't have a valid location for this attribute, adopt the // location. - if (f->getLocation().isInvalid()) - f->setRange(Range); + if (F->getLocation().isInvalid()) + F->setRange(Range); return NULL; } } @@ -2667,10 +2661,8 @@ static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { return; // Don't duplicate annotations that are already set. - for (specific_attr_iterator - i = D->specific_attr_begin(), - e = D->specific_attr_end(); i != e; ++i) { - if ((*i)->getAnnotation() == Str) + for (const auto *I : D->specific_attrs()) { + if (I->getAnnotation() == Str) return; } @@ -2819,13 +2811,11 @@ void Sema::CheckAlignasUnderalignment(Decl *D) { // would otherwise be required for the entity being declared. AlignedAttr *AlignasAttr = 0; unsigned Align = 0; - for (specific_attr_iterator - I = D->specific_attr_begin(), - E = D->specific_attr_end(); I != E; ++I) { + for (auto *I : D->specific_attrs()) { if (I->isAlignmentDependent()) return; if (I->isAlignas()) - AlignasAttr = *I; + AlignasAttr = I; Align = std::max(Align, I->getAlignment(Context)); } diff --git a/lib/StaticAnalyzer/Checkers/DirectIvarAssignment.cpp b/lib/StaticAnalyzer/Checkers/DirectIvarAssignment.cpp index 4c2047a8ff..f200dac1c6 100644 --- a/lib/StaticAnalyzer/Checkers/DirectIvarAssignment.cpp +++ b/lib/StaticAnalyzer/Checkers/DirectIvarAssignment.cpp @@ -161,14 +161,10 @@ void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D, } static bool isAnnotatedToAllowDirectAssignment(const Decl *D) { - for (specific_attr_iterator - AI = D->specific_attr_begin(), - AE = D->specific_attr_end(); AI != AE; ++AI) { - const AnnotateAttr *Ann = *AI; + for (const auto *Ann : D->specific_attrs()) if (Ann->getAnnotation() == "objc_allow_direct_instance_variable_assignment") return true; - } return false; } @@ -226,14 +222,9 @@ void ento::registerDirectIvarAssignment(CheckerManager &mgr) { // Register the checker that checks for direct accesses in functions annotated // with __attribute__((annotate("objc_no_direct_instance_variable_assignment"))). static bool AttrFilter(const ObjCMethodDecl *M) { - for (specific_attr_iterator - AI = M->specific_attr_begin(), - AE = M->specific_attr_end(); - AI != AE; ++AI) { - const AnnotateAttr *Ann = *AI; + for (const auto *Ann : M->specific_attrs()) if (Ann->getAnnotation() == "objc_no_direct_instance_variable_assignment") return false; - } return true; } diff --git a/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp b/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp index a55f8112d0..4154d34438 100644 --- a/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp @@ -613,11 +613,7 @@ static bool getPrintfFormatArgumentNum(const CallExpr *CE, const FunctionDecl *FDecl = C.getCalleeDecl(CE); if (!FDecl) return false; - for (specific_attr_iterator - i = FDecl->specific_attr_begin(), - e = FDecl->specific_attr_end(); i != e ; ++i) { - - const FormatAttr *Format = *i; + for (const auto *Format : FDecl->specific_attrs()) { ArgNum = Format->getFormatIdx() - 1; if ((Format->getType()->getName() == "printf") && CE->getNumArgs() > ArgNum) diff --git a/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp b/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp index c95654cb21..3b794f2325 100644 --- a/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/IvarInvalidationChecker.cpp @@ -227,10 +227,7 @@ public: }; static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) { - for (specific_attr_iterator - AI = M->specific_attr_begin(), - AE = M->specific_attr_end(); AI != AE; ++AI) { - const AnnotateAttr *Ann = *AI; + for (const auto *Ann : M->specific_attrs()) { if (!LookForPartial && Ann->getAnnotation() == "objc_instance_variable_invalidator") return true; diff --git a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp index 51d26d8a64..ca40bebc99 100644 --- a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp @@ -512,11 +512,8 @@ bool MallocChecker::isAllocationFunction(const FunctionDecl *FD, } if (ChecksEnabled[CK_MallocOptimistic] && FD->hasAttrs()) - for (specific_attr_iterator - i = FD->specific_attr_begin(), - e = FD->specific_attr_end(); - i != e; ++i) - if ((*i)->getOwnKind() == OwnershipAttr::Returns) + for (const auto *I : FD->specific_attrs()) + if (I->getOwnKind() == OwnershipAttr::Returns) return true; return false; } @@ -534,12 +531,9 @@ bool MallocChecker::isFreeFunction(const FunctionDecl *FD, ASTContext &C) const } if (ChecksEnabled[CK_MallocOptimistic] && FD->hasAttrs()) - for (specific_attr_iterator - i = FD->specific_attr_begin(), - e = FD->specific_attr_end(); - i != e; ++i) - if ((*i)->getOwnKind() == OwnershipAttr::Takes || - (*i)->getOwnKind() == OwnershipAttr::Holds) + for (const auto *I : FD->specific_attrs()) + if (I->getOwnKind() == OwnershipAttr::Takes || + I->getOwnKind() == OwnershipAttr::Holds) return true; return false; } @@ -633,17 +627,14 @@ void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const { // Check all the attributes, if there are any. // There can be multiple of these attributes. if (FD->hasAttrs()) - for (specific_attr_iterator - i = FD->specific_attr_begin(), - e = FD->specific_attr_end(); - i != e; ++i) { - switch ((*i)->getOwnKind()) { + for (const auto *I : FD->specific_attrs()) { + switch (I->getOwnKind()) { case OwnershipAttr::Returns: - State = MallocMemReturnsAttr(C, CE, *i); + State = MallocMemReturnsAttr(C, CE, I); break; case OwnershipAttr::Takes: case OwnershipAttr::Holds: - State = FreeMemAttr(C, CE, *i); + State = FreeMemAttr(C, CE, I); break; } }