[clang] 3948aed - [clang] NFC: Modernize Decl iteration via IdentifierResolver
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Wed May 17 16:45:52 PDT 2023
Author: Jan Svoboda
Date: 2023-05-17T16:45:46-07:00
New Revision: 3948aedb7ab9a0626faccbe14624dbafeb2e3931
URL: https://github.com/llvm/llvm-project/commit/3948aedb7ab9a0626faccbe14624dbafeb2e3931
DIFF: https://github.com/llvm/llvm-project/commit/3948aedb7ab9a0626faccbe14624dbafeb2e3931.diff
LOG: [clang] NFC: Modernize Decl iteration via IdentifierResolver
Added:
Modified:
clang/include/clang/Sema/IdentifierResolver.h
clang/lib/Sema/IdentifierResolver.cpp
clang/lib/Serialization/ASTWriter.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Sema/IdentifierResolver.h b/clang/include/clang/Sema/IdentifierResolver.h
index 7c8dc46307d4f..1fbd6c48e5180 100644
--- a/clang/include/clang/Sema/IdentifierResolver.h
+++ b/clang/include/clang/Sema/IdentifierResolver.h
@@ -134,13 +134,14 @@ class IdentifierResolver {
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<iterator> 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 607dc3111e9d0..773cef65dcbdf 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::iterator>
+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 8816575ba60e7..749aaa4cd6e19 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -3558,12 +3558,8 @@ class ASTIdentifierTableTrait {
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 @@ class ASTIdentifierTableTrait {
// "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<NamedDecl *, 16> Decls(IdResolver.begin(II),
- IdResolver.end());
+ SmallVector<NamedDecl *, 16> Decls(IdResolver.decls(II));
for (NamedDecl *D : llvm::reverse(Decls))
LE.write<uint32_t>(
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<std::less<>>());
- 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,
More information about the cfe-commits
mailing list