Skip to content

Commit 5b07de1

Browse files
authored
[clang][ASTMatcher] fix hasAnyBase not binding submatchers (#67939)
The BoundNodesTreeBuilder used in the BaseSpecMatcher was the original and was reset to its original state if a match occurred. The matcher now uses the local copy in the inner matcher. Fixes #65421
1 parent 342dca7 commit 5b07de1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ Bug Fixes to AST Handling
521521
computed RecordLayout is incorrect if fields are not completely imported and
522522
should not be cached.
523523
`Issue 64170 <https://github.com/llvm/llvm-project/issues/64170>`_
524+
- Fixed ``hasAnyBase`` not binding nodes in its submatcher.
525+
(`#65421 <https://github.com/llvm/llvm-project/issues/65421>`_)
524526

525527
Miscellaneous Bug Fixes
526528
^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool matchesAnyBase(const CXXRecordDecl &Node,
8787
[Finder, Builder, &BaseSpecMatcher](const CXXBaseSpecifier *BaseSpec,
8888
CXXBasePath &IgnoredParam) {
8989
BoundNodesTreeBuilder Result(*Builder);
90-
if (BaseSpecMatcher.matches(*BaseSpec, Finder, Builder)) {
90+
if (BaseSpecMatcher.matches(*BaseSpec, Finder, &Result)) {
9191
*Builder = std::move(Result);
9292
return true;
9393
}

clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "ASTMatchersTest.h"
1010
#include "clang/AST/Attrs.inc"
11+
#include "clang/AST/DeclCXX.h"
1112
#include "clang/AST/PrettyPrinter.h"
1213
#include "clang/ASTMatchers/ASTMatchFinder.h"
1314
#include "clang/ASTMatchers/ASTMatchers.h"
@@ -5457,6 +5458,18 @@ TEST(HasParent, NoDuplicateParents) {
54575458
stmt().bind("node"), std::make_unique<HasDuplicateParents>()));
54585459
}
54595460

5461+
TEST(HasAnyBase, BindsInnerBoundNodes) {
5462+
EXPECT_TRUE(matchAndVerifyResultTrue(
5463+
"struct Inner {}; struct Proxy : Inner {}; struct Main : public "
5464+
"Proxy {};",
5465+
cxxRecordDecl(hasName("Main"),
5466+
hasAnyBase(cxxBaseSpecifier(hasType(
5467+
cxxRecordDecl(hasName("Inner")).bind("base-class")))))
5468+
.bind("class"),
5469+
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("base-class",
5470+
"Inner")));
5471+
}
5472+
54605473
TEST(TypeMatching, PointeeTypes) {
54615474
EXPECT_TRUE(matches("int b; int &a = b;",
54625475
referenceType(pointee(builtinType()))));

0 commit comments

Comments
 (0)