Skip to content

Fix hasName matcher assertion with inline namespaces #100975

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

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ Fixed Point Support in Clang
AST Matchers
------------

- Fixed an issue with the `hasName` and `hasAnyName` matcher when matching
inline namespaces with an enclosing namespace of the same name.

clang-format
------------

Expand Down
23 changes: 16 additions & 7 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,23 @@ class PatternSet {
/// that didn't match.
/// Return true if there are still any patterns left.
bool consumeNameSuffix(StringRef NodeName, bool CanSkip) {
for (size_t I = 0; I < Patterns.size();) {
if (::clang::ast_matchers::internal::consumeNameSuffix(Patterns[I].P,
NodeName) ||
CanSkip) {
++I;
} else {
Patterns.erase(Patterns.begin() + I);
if (CanSkip) {
// If we can skip the node, then we need to handle the case where a
// skipped node has the same name as its parent.
// namespace a { inline namespace a { class A; } }
// cxxRecordDecl(hasName("::a::A"))
// To do this, any patterns that match should be duplicated in our set,
// one of them with the tail removed.
for (size_t I = 0, E = Patterns.size(); I != E; ++I) {
StringRef Pattern = Patterns[I].P;
if (ast_matchers::internal::consumeNameSuffix(Patterns[I].P, NodeName))
Patterns.push_back({Pattern, Patterns[I].IsFullyQualified});
}
} else {
llvm::erase_if(Patterns, [&NodeName](auto &Pattern) {
return !::clang::ast_matchers::internal::consumeNameSuffix(Pattern.P,
NodeName);
});
}
return !Patterns.empty();
}
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,10 @@ TEST_P(ASTMatchersTest, HasName_MatchesNamespaces) {
recordDecl(hasName("a+b::C"))));
EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }",
recordDecl(hasName("C"))));
EXPECT_TRUE(matches("namespace a { inline namespace a { class C; } }",
recordDecl(hasName("::a::C"))));
EXPECT_TRUE(matches("namespace a { inline namespace a { class C; } }",
recordDecl(hasName("::a::a::C"))));
}

TEST_P(ASTMatchersTest, HasName_MatchesOuterClasses) {
Expand Down
Loading