Skip to content

Commit

Permalink
[clang][ASTMatcher] fix hasAnyBase not binding submatchers (#67939)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
5chmidti authored Oct 16, 2023
1 parent 342dca7 commit 5b07de1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ Bug Fixes to AST Handling
computed RecordLayout is incorrect if fields are not completely imported and
should not be cached.
`Issue 64170 <https://github.com/llvm/llvm-project/issues/64170>`_
- Fixed ``hasAnyBase`` not binding nodes in its submatcher.
(`#65421 <https://github.com/llvm/llvm-project/issues/65421>`_)

Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bool matchesAnyBase(const CXXRecordDecl &Node,
[Finder, Builder, &BaseSpecMatcher](const CXXBaseSpecifier *BaseSpec,
CXXBasePath &IgnoredParam) {
BoundNodesTreeBuilder Result(*Builder);
if (BaseSpecMatcher.matches(*BaseSpec, Finder, Builder)) {
if (BaseSpecMatcher.matches(*BaseSpec, Finder, &Result)) {
*Builder = std::move(Result);
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "ASTMatchersTest.h"
#include "clang/AST/Attrs.inc"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
Expand Down Expand Up @@ -5457,6 +5458,18 @@ TEST(HasParent, NoDuplicateParents) {
stmt().bind("node"), std::make_unique<HasDuplicateParents>()));
}

TEST(HasAnyBase, BindsInnerBoundNodes) {
EXPECT_TRUE(matchAndVerifyResultTrue(
"struct Inner {}; struct Proxy : Inner {}; struct Main : public "
"Proxy {};",
cxxRecordDecl(hasName("Main"),
hasAnyBase(cxxBaseSpecifier(hasType(
cxxRecordDecl(hasName("Inner")).bind("base-class")))))
.bind("class"),
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("base-class",
"Inner")));
}

TEST(TypeMatching, PointeeTypes) {
EXPECT_TRUE(matches("int b; int &a = b;",
referenceType(pointee(builtinType()))));
Expand Down

0 comments on commit 5b07de1

Please sign in to comment.