Skip to content

[clang][NFC] Refactor replaceExternalDecls to use llvm::any_of #143275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

snarang181
Copy link
Contributor

@snarang181 snarang181 commented Jun 7, 2025

This patch simplifies the declaration replacement logic in replaceExternalDecls by replacing a manual loop with an idiomatic use of llvm::any_of. This improves code readability and aligns with common LLVM coding style.

@snarang181 snarang181 changed the title Replace loop with llvm:any_of Refactor replaceExternalDecls to use llvm::any_of Jun 7, 2025
@snarang181 snarang181 changed the title Refactor replaceExternalDecls to use llvm::any_of [clang][NFC] Refactor replaceExternalDecls to use llvm::any_of Jun 7, 2025
@snarang181 snarang181 marked this pull request as ready for review June 7, 2025 19:03
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 7, 2025

@llvm/pr-subscribers-clang

Author: Samarth Narang (snarang181)

Changes

This patch simplifies the declaration replacement logic in replaceExternalDecls by replacing a manual loop with an idiomatic use of llvm::any_of. This improves code readability and aligns with common LLVM coding style.


Full diff: https://github.com/llvm/llvm-project/pull/143275.diff

1 Files Affected:

  • (modified) clang/include/clang/AST/DeclContextInternals.h (+4-8)
diff --git a/clang/include/clang/AST/DeclContextInternals.h b/clang/include/clang/AST/DeclContextInternals.h
index b17b7627ac90c..a1071f62d5fae 100644
--- a/clang/include/clang/AST/DeclContextInternals.h
+++ b/clang/include/clang/AST/DeclContextInternals.h
@@ -176,14 +176,10 @@ class StoredDeclsList {
     DeclListNode::Decls *Tail = erase_if([Decls](NamedDecl *ND) {
       if (ND->isFromASTFile())
         return true;
-      // FIXME: Can we get rid of this loop completely?
-      for (NamedDecl *D : Decls)
-        // Only replace the local declaration if the external declaration has
-        // higher visibilities.
-        if (D->getModuleOwnershipKind() <= ND->getModuleOwnershipKind() &&
-            D->declarationReplaces(ND, /*IsKnownNewer=*/false))
-          return true;
-      return false;
+      return llvm::any_of(Decls, [ND](NamedDecl *D) {
+        return D->getModuleOwnershipKind() <= ND->getModuleOwnershipKind() &&
+               D->declarationReplaces(ND, /*IsKnownNewer=*/false);
+      });
     });
 
     // Don't have any pending external decls any more.

@snarang181
Copy link
Contributor Author

@ChuanqiXu9, requesting your review here.

@zwuis
Copy link
Contributor

zwuis commented Jun 8, 2025

IIUC the FIXME comment means that the time complexity of this part of code is not good enough and we can make it faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants