Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 2 commits 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.

D->declarationReplaces(ND, /*IsKnownNewer=*/false))
return true;
return false;
return llvm::any_of(Decls, [ND](NamedDecl *D) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remain the comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the comment back. Is that what you meant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We should keep both.

@snarang181 snarang181 force-pushed the cleanup/replaceExternalDecls-loop branch from dc4e3c9 to 8446545 Compare June 9, 2025 02:08
@snarang181
Copy link
Contributor Author

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

OK. In any case, the usage of llvm_any_of seemed better than using an explicit for-loop.

@snarang181 snarang181 requested a review from ChuanqiXu9 June 9, 2025 02:10
@@ -177,13 +177,10 @@ class StoredDeclsList {
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another missing comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Should we keep the FIXME?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retain all now, I think.

@snarang181 snarang181 force-pushed the cleanup/replaceExternalDecls-loop branch 3 times, most recently from 9bee4e3 to ff09e5c Compare June 9, 2025 02:34
@snarang181 snarang181 force-pushed the cleanup/replaceExternalDecls-loop branch from ff09e5c to f849821 Compare June 9, 2025 02:40
# for free to join this conversation on GitHub. Already have an account? # 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.

4 participants