From 3948aedb7ab9a0626faccbe14624dbafeb2e3931 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Wed, 17 May 2023 16:45:18 -0700 Subject: [PATCH] [clang] NFC: Modernize Decl iteration via IdentifierResolver --- clang/include/clang/Sema/IdentifierResolver.h | 11 ++++++----- clang/lib/Sema/IdentifierResolver.cpp | 9 ++++++--- clang/lib/Serialization/ASTWriter.cpp | 21 ++++++--------------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/clang/include/clang/Sema/IdentifierResolver.h b/clang/include/clang/Sema/IdentifierResolver.h index 7c8dc46..1fbd6c4 100644 --- a/clang/include/clang/Sema/IdentifierResolver.h +++ b/clang/include/clang/Sema/IdentifierResolver.h @@ -134,13 +134,14 @@ public: explicit IdentifierResolver(Preprocessor &PP); ~IdentifierResolver(); - /// begin - Returns an iterator for decls with the name 'Name'. + /// Returns a range of decls with the name 'Name'. + llvm::iterator_range decls(DeclarationName Name); + + /// Returns an iterator over decls with the name 'Name'. iterator begin(DeclarationName Name); - /// end - Returns an iterator that has 'finished'. - iterator end() { - return iterator(); - } + /// Returns the end iterator. + iterator end() { return iterator(); } /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns diff --git a/clang/lib/Sema/IdentifierResolver.cpp b/clang/lib/Sema/IdentifierResolver.cpp index 607dc31..773cef6 100644 --- a/clang/lib/Sema/IdentifierResolver.cpp +++ b/clang/lib/Sema/IdentifierResolver.cpp @@ -231,9 +231,12 @@ void IdentifierResolver::RemoveDecl(NamedDecl *D) { return toIdDeclInfo(Ptr)->RemoveDecl(D); } -/// begin - Returns an iterator for decls with name 'Name'. -IdentifierResolver::iterator -IdentifierResolver::begin(DeclarationName Name) { +llvm::iterator_range +IdentifierResolver::decls(DeclarationName Name) { + return {begin(Name), end()}; +} + +IdentifierResolver::iterator IdentifierResolver::begin(DeclarationName Name) { if (IdentifierInfo *II = Name.getAsIdentifierInfo()) readingIdentifier(*II); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 8816575..749aaa4 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -3558,12 +3558,8 @@ public: if (MacroOffset) DataLen += 4; // MacroDirectives offset. - if (NeedDecls) { - for (IdentifierResolver::iterator D = IdResolver.begin(II), - DEnd = IdResolver.end(); - D != DEnd; ++D) - DataLen += 4; - } + if (NeedDecls) + DataLen += std::distance(IdResolver.begin(II), IdResolver.end()) * 4; } return emitULEBKeyDataLength(KeyLen, DataLen, Out); } @@ -3608,8 +3604,7 @@ public: // "stat"), but the ASTReader adds declarations to the end of the list // (so we need to see the struct "stat" before the function "stat"). // Only emit declarations that aren't from a chained PCH, though. - SmallVector Decls(IdResolver.begin(II), - IdResolver.end()); + SmallVector Decls(IdResolver.decls(II)); for (NamedDecl *D : llvm::reverse(Decls)) LE.write( Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), D))); @@ -4887,13 +4882,9 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot, } // Sort the identifiers to visit based on their name. llvm::sort(IIs, llvm::deref>()); - for (const IdentifierInfo *II : IIs) { - for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II), - DEnd = SemaRef.IdResolver.end(); - D != DEnd; ++D) { - GetDeclRef(*D); - } - } + for (const IdentifierInfo *II : IIs) + for (const Decl *D : SemaRef.IdResolver.decls(II)) + GetDeclRef(D); } // For method pool in the module, if it contains an entry for a selector, -- 2.7.4