Skip to content

Commit 9743c9d

Browse files
committed
Remove VariadicOperatorMatcherInterface as it is redundant with logic from DynTypedMatcher.
Summary: The generic variadic matcher is faster (one less virtual function call per match) and doesn't require template instantiations which reduces compile time and binary size. Registry.cpp.o generates ~14% less symbols and compiles ~7.5% faster. The change also speeds up our clang-tidy benchmark by ~2%. Reviewers: klimek Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D6278 llvm-svn: 222131
1 parent 4c0ef37 commit 9743c9d

File tree

5 files changed

+26
-50
lines changed

5 files changed

+26
-50
lines changed

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,36 +1121,11 @@ class ForEachMatcher : public MatcherInterface<T> {
11211121
/// \brief VariadicOperatorMatcher related types.
11221122
/// @{
11231123

1124-
/// \brief Function signature for any variadic operator. It takes the inner
1125-
/// matchers as an array of DynTypedMatcher.
1126-
typedef bool (*VariadicOperatorFunction)(
1127-
const ast_type_traits::DynTypedNode DynNode, ASTMatchFinder *Finder,
1128-
BoundNodesTreeBuilder *Builder, ArrayRef<DynTypedMatcher> InnerMatchers);
1129-
1130-
/// \brief \c MatcherInterface<T> implementation for an variadic operator.
1131-
template <typename T>
1132-
class VariadicOperatorMatcherInterface : public MatcherInterface<T> {
1133-
public:
1134-
VariadicOperatorMatcherInterface(VariadicOperatorFunction Func,
1135-
std::vector<DynTypedMatcher> InnerMatchers)
1136-
: Func(Func), InnerMatchers(std::move(InnerMatchers)) {}
1137-
1138-
bool matches(const T &Node, ASTMatchFinder *Finder,
1139-
BoundNodesTreeBuilder *Builder) const override {
1140-
return Func(ast_type_traits::DynTypedNode::create(Node), Finder, Builder,
1141-
InnerMatchers);
1142-
}
1143-
1144-
private:
1145-
const VariadicOperatorFunction Func;
1146-
const std::vector<DynTypedMatcher> InnerMatchers;
1147-
};
1148-
11491124
/// \brief "No argument" placeholder to use as template paratemers.
11501125
struct VariadicOperatorNoArg {};
11511126

1152-
/// \brief Polymorphic matcher object that uses a \c VariadicOperatorFunction
1153-
/// operator.
1127+
/// \brief Polymorphic matcher object that uses a \c
1128+
/// DynTypedMatcher::VariadicOperatorFunction operator.
11541129
///
11551130
/// Input matchers can have any type (including other polymorphic matcher
11561131
/// types), and the actual Matcher<T> is generated on demand with an implicit
@@ -1165,7 +1140,8 @@ template <typename P1, typename P2 = VariadicOperatorNoArg,
11651140
typename P9 = VariadicOperatorNoArg>
11661141
class VariadicOperatorMatcher {
11671142
public:
1168-
VariadicOperatorMatcher(VariadicOperatorFunction Func, const P1 &Param1,
1143+
VariadicOperatorMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
1144+
const P1 &Param1,
11691145
const P2 &Param2 = VariadicOperatorNoArg(),
11701146
const P3 &Param3 = VariadicOperatorNoArg(),
11711147
const P4 &Param4 = VariadicOperatorNoArg(),
@@ -1189,9 +1165,8 @@ class VariadicOperatorMatcher {
11891165
addMatcher<T>(Param7, Matchers);
11901166
addMatcher<T>(Param8, Matchers);
11911167
addMatcher<T>(Param9, Matchers);
1192-
// FIXME: Use DynTypedMatcher::constructVariadic() instead.
1193-
return Matcher<T>(
1194-
new VariadicOperatorMatcherInterface<T>(Func, std::move(Matchers)));
1168+
return DynTypedMatcher::constructVariadic(Func, std::move(Matchers))
1169+
.template unconditionalConvertTo<T>();
11951170
}
11961171

11971172
private:
@@ -1206,7 +1181,7 @@ class VariadicOperatorMatcher {
12061181
static void addMatcher(VariadicOperatorNoArg,
12071182
std::vector<DynTypedMatcher> &Matchers) {}
12081183

1209-
const VariadicOperatorFunction Func;
1184+
const DynTypedMatcher::VariadicOperatorFunction Func;
12101185
const P1 Param1;
12111186
const P2 Param2;
12121187
const P3 Param3;
@@ -1224,7 +1199,7 @@ class VariadicOperatorMatcher {
12241199
/// It supports 1-9 argument overloaded operator(). More can be added if needed.
12251200
template <unsigned MinCount, unsigned MaxCount>
12261201
struct VariadicOperatorMatcherFunc {
1227-
VariadicOperatorFunction Func;
1202+
DynTypedMatcher::VariadicOperatorFunction Func;
12281203

12291204
template <unsigned Count, typename T>
12301205
struct EnableIfValidArity
@@ -1350,9 +1325,9 @@ BindableMatcher<T> makeAllOfComposite(
13501325
for (const auto *InnerMatcher : InnerMatchers) {
13511326
DynMatchers.push_back(*InnerMatcher);
13521327
}
1353-
// FIXME: Use DynTypedMatcher::constructVariadic() instead.
1354-
return BindableMatcher<T>(new VariadicOperatorMatcherInterface<T>(
1355-
AllOfVariadicOperator, std::move(DynMatchers)));
1328+
return BindableMatcher<T>(DynTypedMatcher::constructVariadic(
1329+
AllOfVariadicOperator, std::move(DynMatchers))
1330+
.template unconditionalConvertTo<T>());
13561331
}
13571332

13581333
/// \brief Creates a Matcher<T> that matches if

clang/include/clang/ASTMatchers/Dynamic/VariantValue.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ class VariantMatcher {
106106
/// \brief Constructs a variadic typed matcher from \p InnerMatchers.
107107
/// Will try to convert each inner matcher to the destination type and
108108
/// return llvm::None if it fails to do so.
109-
llvm::Optional<DynTypedMatcher> constructVariadicOperator(
110-
ast_matchers::internal::VariadicOperatorFunction Func,
111-
ArrayRef<VariantMatcher> InnerMatchers) const;
109+
llvm::Optional<DynTypedMatcher>
110+
constructVariadicOperator(DynTypedMatcher::VariadicOperatorFunction Func,
111+
ArrayRef<VariantMatcher> InnerMatchers) const;
112112

113113
protected:
114114
~MatcherOps() {}
@@ -147,9 +147,9 @@ class VariantMatcher {
147147
/// \brief Creates a 'variadic' operator matcher.
148148
///
149149
/// It will bind to the appropriate type on getTypedMatcher<T>().
150-
static VariantMatcher VariadicOperatorMatcher(
151-
ast_matchers::internal::VariadicOperatorFunction Func,
152-
std::vector<VariantMatcher> Args);
150+
static VariantMatcher
151+
VariadicOperatorMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
152+
std::vector<VariantMatcher> Args);
153153

154154
/// \brief Makes the matcher the "null" matcher.
155155
void reset();

clang/lib/ASTMatchers/ASTMatchersInternal.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace {
3232

3333
class VariadicMatcher : public DynMatcherInterface {
3434
public:
35-
VariadicMatcher(VariadicOperatorFunction Func,
35+
VariadicMatcher(DynTypedMatcher::VariadicOperatorFunction Func,
3636
std::vector<DynTypedMatcher> InnerMatchers)
3737
: Func(Func), InnerMatchers(std::move(InnerMatchers)) {}
3838

@@ -43,7 +43,7 @@ class VariadicMatcher : public DynMatcherInterface {
4343
}
4444

4545
private:
46-
VariadicOperatorFunction Func;
46+
DynTypedMatcher::VariadicOperatorFunction Func;
4747
std::vector<DynTypedMatcher> InnerMatchers;
4848
};
4949

@@ -86,7 +86,8 @@ static llvm::ManagedStatic<TrueMatcherImpl> TrueMatcherInstance;
8686
} // namespace
8787

8888
DynTypedMatcher DynTypedMatcher::constructVariadic(
89-
VariadicOperatorFunction Func, std::vector<DynTypedMatcher> InnerMatchers) {
89+
DynTypedMatcher::VariadicOperatorFunction Func,
90+
std::vector<DynTypedMatcher> InnerMatchers) {
9091
assert(InnerMatchers.size() > 0 && "Array must not be empty.");
9192
assert(std::all_of(InnerMatchers.begin(), InnerMatchers.end(),
9293
[&InnerMatchers](const DynTypedMatcher &M) {

clang/lib/ASTMatchers/Dynamic/Marshallers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ class OverloadedMatcherDescriptor : public MatcherDescriptor {
556556
/// \brief Variadic operator marshaller function.
557557
class VariadicOperatorMatcherDescriptor : public MatcherDescriptor {
558558
public:
559-
typedef ast_matchers::internal::VariadicOperatorFunction VarFunc;
559+
typedef DynTypedMatcher::VariadicOperatorFunction VarFunc;
560560
VariadicOperatorMatcherDescriptor(unsigned MinCount, unsigned MaxCount,
561561
VarFunc Func, StringRef MatcherName)
562562
: MinCount(MinCount), MaxCount(MaxCount), Func(Func),

clang/lib/ASTMatchers/Dynamic/VariantValue.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ VariantMatcher::MatcherOps::canConstructFrom(const DynTypedMatcher &Matcher,
5858

5959
llvm::Optional<DynTypedMatcher>
6060
VariantMatcher::MatcherOps::constructVariadicOperator(
61-
ast_matchers::internal::VariadicOperatorFunction Func,
61+
DynTypedMatcher::VariadicOperatorFunction Func,
6262
ArrayRef<VariantMatcher> InnerMatchers) const {
6363
std::vector<DynTypedMatcher> DynMatchers;
6464
for (const auto &InnerMatcher : InnerMatchers) {
@@ -176,7 +176,7 @@ class VariantMatcher::PolymorphicPayload : public VariantMatcher::Payload {
176176

177177
class VariantMatcher::VariadicOpPayload : public VariantMatcher::Payload {
178178
public:
179-
VariadicOpPayload(ast_matchers::internal::VariadicOperatorFunction Func,
179+
VariadicOpPayload(DynTypedMatcher::VariadicOperatorFunction Func,
180180
std::vector<VariantMatcher> Args)
181181
: Func(Func), Args(std::move(Args)) {}
182182

@@ -209,7 +209,7 @@ class VariantMatcher::VariadicOpPayload : public VariantMatcher::Payload {
209209
}
210210

211211
private:
212-
const ast_matchers::internal::VariadicOperatorFunction Func;
212+
const DynTypedMatcher::VariadicOperatorFunction Func;
213213
const std::vector<VariantMatcher> Args;
214214
};
215215

@@ -225,7 +225,7 @@ VariantMatcher::PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers) {
225225
}
226226

227227
VariantMatcher VariantMatcher::VariadicOperatorMatcher(
228-
ast_matchers::internal::VariadicOperatorFunction Func,
228+
DynTypedMatcher::VariadicOperatorFunction Func,
229229
std::vector<VariantMatcher> Args) {
230230
return VariantMatcher(new VariadicOpPayload(Func, std::move(Args)));
231231
}

0 commit comments

Comments
 (0)