Skip to content

[clang] Add hasAnyNameInVector matcher #139594

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

fmayer
Copy link
Contributor

@fmayer fmayer commented May 12, 2025

This is useful for matching functions whose name is given as a
parameter, e.g.

Created using spr 1.3.4
@llvmbot llvmbot added the clang Clang issues not falling into any other category label May 12, 2025
@llvmbot
Copy link
Member

llvmbot commented May 12, 2025

@llvm/pr-subscribers-clang

Author: Florian Mayer (fmayer)

Changes

This is useful for matching functions whose name is given as a
parameter, e.g.


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

1 Files Affected:

  • (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+18)
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index e6b684b24b080..255f7d3487f9d 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -3144,6 +3144,24 @@ extern const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
                                         internal::hasAnyNameFunc>
     hasAnyName;
 
+/// Matches NamedDecl nodes that have any of the names in vector.
+///
+/// This is useful for matching a list of names that are only known at runtime,
+/// e.g. through a command line argument.
+///
+/// \code
+///     hasAnyName({a, b, c})
+/// \endcode
+///  is equivalent to
+/// \code
+///     anyOf(hasName(a), hasName(b), hasName(c))
+/// \endcode
+inline internal::Matcher<NamedDecl>
+hasAnyNameInVector(std::vector<std::string> Names) {
+  return internal::Matcher<NamedDecl>(
+      new internal::HasNameMatcher(std::move(Names)));
+}
+
 /// Matches NamedDecl nodes whose fully qualified names contain
 /// a substring matched by the given RegExp.
 ///

@fmayer fmayer requested a review from ymand May 12, 2025 17:52
@AaronBallman AaronBallman self-requested a review May 12, 2025 17:58
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Are there any planned in-tree uses for this matcher? We don't typically add matchers without a need in tree due to the overhead of the matcher interfaces.

If there are in-tree uses planned, then some additional work is needed: regenerate the documentation (there's a script in the docs directory) and test coverage. I don't think Registry.cpp needs to be updated though as this cannot be used via the dynamic matchers, I believe.

@fmayer
Copy link
Contributor Author

fmayer commented May 12, 2025

Are there any planned in-tree uses for this matcher?

I had a downstream use planned. Does this mean I will have to reimplement internal::HasNameMatcher?

@AaronBallman
Copy link
Collaborator

Are there any planned in-tree uses for this matcher?

I had a downstream use planned. Does this mean I will have to reimplement internal::HasNameMatcher?

Can you adapt the code to use hasAnyName() instead, as that can accept a variadic number of StringRef arguments stored in an ArrayRef?

@fmayer
Copy link
Contributor Author

fmayer commented May 13, 2025

Are there any planned in-tree uses for this matcher?

I had a downstream use planned. Does this mean I will have to reimplement internal::HasNameMatcher?

Can you adapt the code to use hasAnyName() instead, as that can accept a variadic number of StringRef arguments stored in an ArrayRef?

The use is for a runtime list of names, so I don't think the variadic would work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants