- Notifications
You must be signed in to change notification settings - Fork 15.3k
[ASTMatchers] Extend hasName matcher when matching templates #100349
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -638,6 +638,39 @@ bool HasNameMatcher::matchesNodeFullFast(const NamedDecl &Node) const { | |
| return Patterns.foundMatch(/*AllowFullyQualified=*/true); | ||
| } | ||
| | ||
| static std::optional<StringRef> consumePatternBack(StringRef Pattern, | ||
| StringRef Target) { | ||
| while (!Pattern.empty()) { | ||
| auto Index = Pattern.rfind("<*>"); | ||
| if (Index == StringRef::npos) { | ||
| if (Target.consume_back(Pattern)) | ||
| return Target; | ||
| return {}; | ||
| } | ||
| auto Suffix = Pattern.substr(Index + 2); | ||
| if (!Target.consume_back(Suffix)) | ||
| Comment on lines +650 to +651 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I had to check this, could you please add a comment why only
| ||
| return {}; | ||
| auto BracketCount = 1; | ||
| Comment on lines +644 to +653 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're using
| ||
| while (BracketCount) { | ||
| if (Target.empty()) | ||
| return {}; | ||
| Comment on lines +648 to +656 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO returning | ||
| switch (Target.back()) { | ||
| case '<': | ||
| --BracketCount; | ||
| break; | ||
| case '>': | ||
| ++BracketCount; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| Target = Target.drop_back(); | ||
| } | ||
| Pattern = Pattern.take_front(Index); | ||
| } | ||
| return Target; | ||
| } | ||
| | ||
| bool HasNameMatcher::matchesNodeFullSlow(const NamedDecl &Node) const { | ||
| const bool SkipUnwrittenCases[] = {false, true}; | ||
| for (bool SkipUnwritten : SkipUnwrittenCases) { | ||
| | @@ -653,10 +686,12 @@ bool HasNameMatcher::matchesNodeFullSlow(const NamedDecl &Node) const { | |
| | ||
| for (const StringRef Pattern : Names) { | ||
| if (Pattern.starts_with("::")) { | ||
| if (FullName == Pattern) | ||
| if (auto Result = consumePatternBack(Pattern, FullName); | ||
| Result && Result->empty()) { | ||
| return true; | ||
| } else if (FullName.ends_with(Pattern) && | ||
| FullName.drop_back(Pattern.size()).ends_with("::")) { | ||
| } | ||
| } else if (auto Result = consumePatternBack(Pattern, FullName); | ||
| Result && Result->ends_with("::")) { | ||
| return true; | ||
| } | ||
| Comment on lines 688 to 696 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could write this loop body as const std::optional<StringRef> Result = consumePatternBack(Pattern, FullName); if (!Result) continue; if ((Pattern.starts_with("::") && Result->empty()) || Result->ends_with("::")) return true;which is more readable IMO | ||
| } | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably be clear that
<*>is the only syntax we support. e.g., we don't let you search based on arity, as inhasName("Foo<int, *>::Bar"). Might be worth an example as well, definitely worth some test coverage.