Skip to content

Commit b063f5c

Browse files
committed
[ASTMatchers] Use provided target NodeKind instead of inferring it from the matchers.
Individual matchers might not be convertible to each other's kind, but they might still all be convertible to the target kind. All the callers already know the target kind, so just pass it down. llvm-svn: 242534
1 parent 2bec850 commit b063f5c

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class DynTypedMatcher {
281281
};
282282
static DynTypedMatcher
283283
constructVariadic(VariadicOperator Op,
284+
ast_type_traits::ASTNodeKind SupportedKind,
284285
std::vector<DynTypedMatcher> InnerMatchers);
285286

286287
/// \brief Get a "true" matcher for \p NodeKind.
@@ -1137,7 +1138,8 @@ template <typename... Ps> class VariadicOperatorMatcher {
11371138

11381139
template <typename T> operator Matcher<T>() const {
11391140
return DynTypedMatcher::constructVariadic(
1140-
Op, getMatchers<T>(llvm::index_sequence_for<Ps...>()))
1141+
Op, ast_type_traits::ASTNodeKind::getFromNodeKind<T>(),
1142+
getMatchers<T>(llvm::index_sequence_for<Ps...>()))
11411143
.template unconditionalConvertTo<T>();
11421144
}
11431145

@@ -1191,8 +1193,10 @@ BindableMatcher<T> makeAllOfComposite(
11911193
std::vector<DynTypedMatcher> DynMatchers(PI(InnerMatchers.begin()),
11921194
PI(InnerMatchers.end()));
11931195
return BindableMatcher<T>(
1194-
DynTypedMatcher::constructVariadic(DynTypedMatcher::VO_AllOf,
1195-
std::move(DynMatchers))
1196+
DynTypedMatcher::constructVariadic(
1197+
DynTypedMatcher::VO_AllOf,
1198+
ast_type_traits::ASTNodeKind::getFromNodeKind<T>(),
1199+
std::move(DynMatchers))
11961200
.template unconditionalConvertTo<T>());
11971201
}
11981202

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ static llvm::ManagedStatic<TrueMatcherImpl> TrueMatcherInstance;
110110

111111
DynTypedMatcher DynTypedMatcher::constructVariadic(
112112
DynTypedMatcher::VariadicOperator Op,
113+
ast_type_traits::ASTNodeKind SupportedKind,
113114
std::vector<DynTypedMatcher> InnerMatchers) {
114115
assert(InnerMatchers.size() > 0 && "Array must not be empty.");
115116
assert(std::all_of(InnerMatchers.begin(), InnerMatchers.end(),
116-
[&InnerMatchers](const DynTypedMatcher &M) {
117-
return InnerMatchers[0].canConvertTo(M.SupportedKind);
118-
}) &&
119-
"SupportedKind must be convertible to a common type!");
117+
[SupportedKind](const DynTypedMatcher &M) {
118+
return M.canConvertTo(SupportedKind);
119+
}) &&
120+
"InnerMatchers must be convertible to SupportedKind!");
120121

121-
auto SupportedKind = InnerMatchers[0].SupportedKind;
122122
// We must relax the restrict kind here.
123123
// The different operators might deal differently with a mismatch.
124124
// Make it the same as SupportedKind, since that is the broadest type we are

clang/lib/ASTMatchers/Dynamic/VariantValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ VariantMatcher::MatcherOps::constructVariadicOperator(
7272
return llvm::None;
7373
DynMatchers.push_back(*Inner);
7474
}
75-
return DynTypedMatcher::constructVariadic(Op, DynMatchers);
75+
return DynTypedMatcher::constructVariadic(Op, NodeKind, DynMatchers);
7676
}
7777

7878
VariantMatcher::Payload::~Payload() {}

clang/unittests/ASTMatchers/ASTMatchersTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ TEST(AllOf, AllOverloadsWork) {
453453
hasArgument(3, integerLiteral(equals(4)))))));
454454
}
455455

456+
TEST(ConstructVariadic, MismatchedTypes_Regression) {
457+
EXPECT_TRUE(
458+
matches("const int a = 0;",
459+
internal::DynTypedMatcher::constructVariadic(
460+
internal::DynTypedMatcher::VO_AnyOf,
461+
ast_type_traits::ASTNodeKind::getFromNodeKind<QualType>(),
462+
{isConstQualified(), arrayType()})
463+
.convertTo<QualType>()));
464+
}
465+
456466
TEST(DeclarationMatcher, MatchAnyOf) {
457467
DeclarationMatcher YOrZDerivedFromX =
458468
recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z"))));

0 commit comments

Comments
 (0)