• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ASTMatchersInternal.cpp - Structural query framework ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Implements the base layer of the matcher framework.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/ASTMatchers/ASTMatchersInternal.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTTypeTraits.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/ParentMapContext.h"
19 #include "clang/AST/PrettyPrinter.h"
20 #include "clang/ASTMatchers/ASTMatchers.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Lex/Lexer.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/IntrusiveRefCntPtr.h"
25 #include "llvm/ADT/None.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ManagedStatic.h"
32 #include "llvm/Support/Regex.h"
33 #include "llvm/Support/WithColor.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <algorithm>
36 #include <cassert>
37 #include <cstddef>
38 #include <string>
39 #include <utility>
40 #include <vector>
41 
42 namespace clang {
43 namespace ast_matchers {
44 
AST_MATCHER_P(ObjCMessageExpr,hasAnySelectorMatcher,std::vector<std::string>,Matches)45 AST_MATCHER_P(ObjCMessageExpr, hasAnySelectorMatcher, std::vector<std::string>,
46               Matches) {
47   return llvm::is_contained(Matches, Node.getSelector().getAsString());
48 }
49 
50 namespace internal {
51 
52 static bool notUnaryOperator(const DynTypedNode &DynNode,
53                              ASTMatchFinder *Finder,
54                              BoundNodesTreeBuilder *Builder,
55                              ArrayRef<DynTypedMatcher> InnerMatchers);
56 
57 static bool allOfVariadicOperator(const DynTypedNode &DynNode,
58                                   ASTMatchFinder *Finder,
59                                   BoundNodesTreeBuilder *Builder,
60                                   ArrayRef<DynTypedMatcher> InnerMatchers);
61 
62 static bool eachOfVariadicOperator(const DynTypedNode &DynNode,
63                                    ASTMatchFinder *Finder,
64                                    BoundNodesTreeBuilder *Builder,
65                                    ArrayRef<DynTypedMatcher> InnerMatchers);
66 
67 static bool anyOfVariadicOperator(const DynTypedNode &DynNode,
68                                   ASTMatchFinder *Finder,
69                                   BoundNodesTreeBuilder *Builder,
70                                   ArrayRef<DynTypedMatcher> InnerMatchers);
71 
72 static bool optionallyVariadicOperator(const DynTypedNode &DynNode,
73                                        ASTMatchFinder *Finder,
74                                        BoundNodesTreeBuilder *Builder,
75                                        ArrayRef<DynTypedMatcher> InnerMatchers);
76 
matchesAnyBase(const CXXRecordDecl & Node,const Matcher<CXXBaseSpecifier> & BaseSpecMatcher,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder)77 bool matchesAnyBase(const CXXRecordDecl &Node,
78                     const Matcher<CXXBaseSpecifier> &BaseSpecMatcher,
79                     ASTMatchFinder *Finder, BoundNodesTreeBuilder *Builder) {
80   if (!Node.hasDefinition())
81     return false;
82 
83   CXXBasePaths Paths;
84   Paths.setOrigin(&Node);
85 
86   const auto basePredicate =
87       [Finder, Builder, &BaseSpecMatcher](const CXXBaseSpecifier *BaseSpec,
88                                           CXXBasePath &IgnoredParam) {
89         BoundNodesTreeBuilder Result(*Builder);
90         if (BaseSpecMatcher.matches(*BaseSpec, Finder, Builder)) {
91           *Builder = std::move(Result);
92           return true;
93         }
94         return false;
95       };
96 
97   return Node.lookupInBases(basePredicate, Paths,
98                             /*LookupInDependent =*/true);
99 }
100 
visitMatches(Visitor * ResultVisitor)101 void BoundNodesTreeBuilder::visitMatches(Visitor *ResultVisitor) {
102   if (Bindings.empty())
103     Bindings.push_back(BoundNodesMap());
104   for (BoundNodesMap &Binding : Bindings) {
105     ResultVisitor->visitMatch(BoundNodes(Binding));
106   }
107 }
108 
109 namespace {
110 
111 using VariadicOperatorFunction = bool (*)(
112     const DynTypedNode &DynNode, ASTMatchFinder *Finder,
113     BoundNodesTreeBuilder *Builder, ArrayRef<DynTypedMatcher> InnerMatchers);
114 
115 template <VariadicOperatorFunction Func>
116 class VariadicMatcher : public DynMatcherInterface {
117 public:
VariadicMatcher(std::vector<DynTypedMatcher> InnerMatchers)118   VariadicMatcher(std::vector<DynTypedMatcher> InnerMatchers)
119       : InnerMatchers(std::move(InnerMatchers)) {}
120 
dynMatches(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder) const121   bool dynMatches(const DynTypedNode &DynNode, ASTMatchFinder *Finder,
122                   BoundNodesTreeBuilder *Builder) const override {
123     return Func(DynNode, Finder, Builder, InnerMatchers);
124   }
125 
126 private:
127   std::vector<DynTypedMatcher> InnerMatchers;
128 };
129 
130 class IdDynMatcher : public DynMatcherInterface {
131 public:
IdDynMatcher(StringRef ID,IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher)132   IdDynMatcher(StringRef ID,
133                IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher)
134       : ID(ID), InnerMatcher(std::move(InnerMatcher)) {}
135 
dynMatches(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder) const136   bool dynMatches(const DynTypedNode &DynNode, ASTMatchFinder *Finder,
137                   BoundNodesTreeBuilder *Builder) const override {
138     bool Result = InnerMatcher->dynMatches(DynNode, Finder, Builder);
139     if (Result) Builder->setBinding(ID, DynNode);
140     return Result;
141   }
142 
TraversalKind() const143   llvm::Optional<clang::TraversalKind> TraversalKind() const override {
144     return InnerMatcher->TraversalKind();
145   }
146 
147 private:
148   const std::string ID;
149   const IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher;
150 };
151 
152 /// A matcher that always returns true.
153 class TrueMatcherImpl : public DynMatcherInterface {
154 public:
155   TrueMatcherImpl() = default;
156 
dynMatches(const DynTypedNode &,ASTMatchFinder *,BoundNodesTreeBuilder *) const157   bool dynMatches(const DynTypedNode &, ASTMatchFinder *,
158                   BoundNodesTreeBuilder *) const override {
159     return true;
160   }
161 };
162 
163 /// A matcher that specifies a particular \c TraversalKind.
164 ///
165 /// The kind provided to the constructor overrides any kind that may be
166 /// specified by the `InnerMatcher`.
167 class DynTraversalMatcherImpl : public DynMatcherInterface {
168 public:
DynTraversalMatcherImpl(clang::TraversalKind TK,IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher)169   explicit DynTraversalMatcherImpl(
170       clang::TraversalKind TK,
171       IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher)
172       : TK(TK), InnerMatcher(std::move(InnerMatcher)) {}
173 
dynMatches(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder) const174   bool dynMatches(const DynTypedNode &DynNode, ASTMatchFinder *Finder,
175                   BoundNodesTreeBuilder *Builder) const override {
176     return this->InnerMatcher->dynMatches(DynNode, Finder, Builder);
177   }
178 
TraversalKind() const179   llvm::Optional<clang::TraversalKind> TraversalKind() const override {
180     return TK;
181   }
182 
183 private:
184   clang::TraversalKind TK;
185   IntrusiveRefCntPtr<DynMatcherInterface> InnerMatcher;
186 };
187 
188 } // namespace
189 
isTraversalIgnoringImplicitNodes() const190 bool ASTMatchFinder::isTraversalIgnoringImplicitNodes() const {
191   return getASTContext().getParentMapContext().getTraversalKind() ==
192          TK_IgnoreUnlessSpelledInSource;
193 }
194 
195 DynTypedMatcher
constructVariadic(DynTypedMatcher::VariadicOperator Op,ASTNodeKind SupportedKind,std::vector<DynTypedMatcher> InnerMatchers)196 DynTypedMatcher::constructVariadic(DynTypedMatcher::VariadicOperator Op,
197                                    ASTNodeKind SupportedKind,
198                                    std::vector<DynTypedMatcher> InnerMatchers) {
199   assert(!InnerMatchers.empty() && "Array must not be empty.");
200   assert(llvm::all_of(InnerMatchers,
201                       [SupportedKind](const DynTypedMatcher &M) {
202                         return M.canConvertTo(SupportedKind);
203                       }) &&
204          "InnerMatchers must be convertible to SupportedKind!");
205 
206   // We must relax the restrict kind here.
207   // The different operators might deal differently with a mismatch.
208   // Make it the same as SupportedKind, since that is the broadest type we are
209   // allowed to accept.
210   auto RestrictKind = SupportedKind;
211 
212   switch (Op) {
213   case VO_AllOf:
214     // In the case of allOf() we must pass all the checks, so making
215     // RestrictKind the most restrictive can save us time. This way we reject
216     // invalid types earlier and we can elide the kind checks inside the
217     // matcher.
218     for (auto &IM : InnerMatchers) {
219       RestrictKind =
220           ASTNodeKind::getMostDerivedType(RestrictKind, IM.RestrictKind);
221     }
222     return DynTypedMatcher(
223         SupportedKind, RestrictKind,
224         new VariadicMatcher<allOfVariadicOperator>(std::move(InnerMatchers)));
225 
226   case VO_AnyOf:
227     return DynTypedMatcher(
228         SupportedKind, RestrictKind,
229         new VariadicMatcher<anyOfVariadicOperator>(std::move(InnerMatchers)));
230 
231   case VO_EachOf:
232     return DynTypedMatcher(
233         SupportedKind, RestrictKind,
234         new VariadicMatcher<eachOfVariadicOperator>(std::move(InnerMatchers)));
235 
236   case VO_Optionally:
237     return DynTypedMatcher(SupportedKind, RestrictKind,
238                            new VariadicMatcher<optionallyVariadicOperator>(
239                                std::move(InnerMatchers)));
240 
241   case VO_UnaryNot:
242     // FIXME: Implement the Not operator to take a single matcher instead of a
243     // vector.
244     return DynTypedMatcher(
245         SupportedKind, RestrictKind,
246         new VariadicMatcher<notUnaryOperator>(std::move(InnerMatchers)));
247   }
248   llvm_unreachable("Invalid Op value.");
249 }
250 
251 DynTypedMatcher
constructRestrictedWrapper(const DynTypedMatcher & InnerMatcher,ASTNodeKind RestrictKind)252 DynTypedMatcher::constructRestrictedWrapper(const DynTypedMatcher &InnerMatcher,
253                                             ASTNodeKind RestrictKind) {
254   DynTypedMatcher Copy = InnerMatcher;
255   Copy.RestrictKind = RestrictKind;
256   return Copy;
257 }
258 
259 DynTypedMatcher
withTraversalKind(ast_type_traits::TraversalKind TK)260 DynTypedMatcher::withTraversalKind(ast_type_traits::TraversalKind TK) {
261   auto Copy = *this;
262   Copy.Implementation =
263       new DynTraversalMatcherImpl(TK, std::move(Copy.Implementation));
264   return Copy;
265 }
266 
trueMatcher(ASTNodeKind NodeKind)267 DynTypedMatcher DynTypedMatcher::trueMatcher(ASTNodeKind NodeKind) {
268   // We only ever need one instance of TrueMatcherImpl, so we create a static
269   // instance and reuse it to reduce the overhead of the matcher and increase
270   // the chance of cache hits.
271   static const llvm::IntrusiveRefCntPtr<TrueMatcherImpl> Instance =
272       new TrueMatcherImpl();
273   return DynTypedMatcher(NodeKind, NodeKind, Instance);
274 }
275 
canMatchNodesOfKind(ASTNodeKind Kind) const276 bool DynTypedMatcher::canMatchNodesOfKind(ASTNodeKind Kind) const {
277   return RestrictKind.isBaseOf(Kind);
278 }
279 
dynCastTo(const ASTNodeKind Kind) const280 DynTypedMatcher DynTypedMatcher::dynCastTo(const ASTNodeKind Kind) const {
281   auto Copy = *this;
282   Copy.SupportedKind = Kind;
283   Copy.RestrictKind = ASTNodeKind::getMostDerivedType(Kind, RestrictKind);
284   return Copy;
285 }
286 
matches(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder) const287 bool DynTypedMatcher::matches(const DynTypedNode &DynNode,
288                               ASTMatchFinder *Finder,
289                               BoundNodesTreeBuilder *Builder) const {
290   TraversalKindScope RAII(Finder->getASTContext(),
291                           Implementation->TraversalKind());
292 
293   if (Finder->isTraversalIgnoringImplicitNodes() &&
294       Finder->IsMatchingInASTNodeNotSpelledInSource())
295     return false;
296 
297   auto N =
298       Finder->getASTContext().getParentMapContext().traverseIgnored(DynNode);
299 
300   if (RestrictKind.isBaseOf(N.getNodeKind()) &&
301       Implementation->dynMatches(N, Finder, Builder)) {
302     return true;
303   }
304   // Delete all bindings when a matcher does not match.
305   // This prevents unexpected exposure of bound nodes in unmatches
306   // branches of the match tree.
307   Builder->removeBindings([](const BoundNodesMap &) { return true; });
308   return false;
309 }
310 
matchesNoKindCheck(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder) const311 bool DynTypedMatcher::matchesNoKindCheck(const DynTypedNode &DynNode,
312                                          ASTMatchFinder *Finder,
313                                          BoundNodesTreeBuilder *Builder) const {
314   TraversalKindScope raii(Finder->getASTContext(),
315                           Implementation->TraversalKind());
316 
317   if (Finder->isTraversalIgnoringImplicitNodes() &&
318       Finder->IsMatchingInASTNodeNotSpelledInSource())
319     return false;
320 
321   auto N =
322       Finder->getASTContext().getParentMapContext().traverseIgnored(DynNode);
323 
324   assert(RestrictKind.isBaseOf(N.getNodeKind()));
325   if (Implementation->dynMatches(N, Finder, Builder)) {
326     return true;
327   }
328   // Delete all bindings when a matcher does not match.
329   // This prevents unexpected exposure of bound nodes in unmatches
330   // branches of the match tree.
331   Builder->removeBindings([](const BoundNodesMap &) { return true; });
332   return false;
333 }
334 
tryBind(StringRef ID) const335 llvm::Optional<DynTypedMatcher> DynTypedMatcher::tryBind(StringRef ID) const {
336   if (!AllowBind) return llvm::None;
337   auto Result = *this;
338   Result.Implementation =
339       new IdDynMatcher(ID, std::move(Result.Implementation));
340   return std::move(Result);
341 }
342 
canConvertTo(ASTNodeKind To) const343 bool DynTypedMatcher::canConvertTo(ASTNodeKind To) const {
344   const auto From = getSupportedKind();
345   auto QualKind = ASTNodeKind::getFromNodeKind<QualType>();
346   auto TypeKind = ASTNodeKind::getFromNodeKind<Type>();
347   /// Mimic the implicit conversions of Matcher<>.
348   /// - From Matcher<Type> to Matcher<QualType>
349   if (From.isSame(TypeKind) && To.isSame(QualKind)) return true;
350   /// - From Matcher<Base> to Matcher<Derived>
351   return From.isBaseOf(To);
352 }
353 
addMatch(const BoundNodesTreeBuilder & Other)354 void BoundNodesTreeBuilder::addMatch(const BoundNodesTreeBuilder &Other) {
355   Bindings.append(Other.Bindings.begin(), Other.Bindings.end());
356 }
357 
notUnaryOperator(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder,ArrayRef<DynTypedMatcher> InnerMatchers)358 static bool notUnaryOperator(const DynTypedNode &DynNode,
359                              ASTMatchFinder *Finder,
360                              BoundNodesTreeBuilder *Builder,
361                              ArrayRef<DynTypedMatcher> InnerMatchers) {
362   if (InnerMatchers.size() != 1)
363     return false;
364 
365   // The 'unless' matcher will always discard the result:
366   // If the inner matcher doesn't match, unless returns true,
367   // but the inner matcher cannot have bound anything.
368   // If the inner matcher matches, the result is false, and
369   // any possible binding will be discarded.
370   // We still need to hand in all the bound nodes up to this
371   // point so the inner matcher can depend on bound nodes,
372   // and we need to actively discard the bound nodes, otherwise
373   // the inner matcher will reset the bound nodes if it doesn't
374   // match, but this would be inversed by 'unless'.
375   BoundNodesTreeBuilder Discard(*Builder);
376   return !InnerMatchers[0].matches(DynNode, Finder, &Discard);
377 }
378 
allOfVariadicOperator(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder,ArrayRef<DynTypedMatcher> InnerMatchers)379 static bool allOfVariadicOperator(const DynTypedNode &DynNode,
380                                   ASTMatchFinder *Finder,
381                                   BoundNodesTreeBuilder *Builder,
382                                   ArrayRef<DynTypedMatcher> InnerMatchers) {
383   // allOf leads to one matcher for each alternative in the first
384   // matcher combined with each alternative in the second matcher.
385   // Thus, we can reuse the same Builder.
386   return llvm::all_of(InnerMatchers, [&](const DynTypedMatcher &InnerMatcher) {
387     return InnerMatcher.matchesNoKindCheck(DynNode, Finder, Builder);
388   });
389 }
390 
eachOfVariadicOperator(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder,ArrayRef<DynTypedMatcher> InnerMatchers)391 static bool eachOfVariadicOperator(const DynTypedNode &DynNode,
392                                    ASTMatchFinder *Finder,
393                                    BoundNodesTreeBuilder *Builder,
394                                    ArrayRef<DynTypedMatcher> InnerMatchers) {
395   BoundNodesTreeBuilder Result;
396   bool Matched = false;
397   for (const DynTypedMatcher &InnerMatcher : InnerMatchers) {
398     BoundNodesTreeBuilder BuilderInner(*Builder);
399     if (InnerMatcher.matches(DynNode, Finder, &BuilderInner)) {
400       Matched = true;
401       Result.addMatch(BuilderInner);
402     }
403   }
404   *Builder = std::move(Result);
405   return Matched;
406 }
407 
anyOfVariadicOperator(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder,ArrayRef<DynTypedMatcher> InnerMatchers)408 static bool anyOfVariadicOperator(const DynTypedNode &DynNode,
409                                   ASTMatchFinder *Finder,
410                                   BoundNodesTreeBuilder *Builder,
411                                   ArrayRef<DynTypedMatcher> InnerMatchers) {
412   for (const DynTypedMatcher &InnerMatcher : InnerMatchers) {
413     BoundNodesTreeBuilder Result = *Builder;
414     if (InnerMatcher.matches(DynNode, Finder, &Result)) {
415       *Builder = std::move(Result);
416       return true;
417     }
418   }
419   return false;
420 }
421 
422 static bool
optionallyVariadicOperator(const DynTypedNode & DynNode,ASTMatchFinder * Finder,BoundNodesTreeBuilder * Builder,ArrayRef<DynTypedMatcher> InnerMatchers)423 optionallyVariadicOperator(const DynTypedNode &DynNode, ASTMatchFinder *Finder,
424                            BoundNodesTreeBuilder *Builder,
425                            ArrayRef<DynTypedMatcher> InnerMatchers) {
426   if (InnerMatchers.size() != 1)
427     return false;
428 
429   BoundNodesTreeBuilder Result(*Builder);
430   if (InnerMatchers[0].matches(DynNode, Finder, &Result))
431     *Builder = std::move(Result);
432   return true;
433 }
434 
435 inline static
vectorFromRefs(ArrayRef<const StringRef * > NameRefs)436 std::vector<std::string> vectorFromRefs(ArrayRef<const StringRef *> NameRefs) {
437   std::vector<std::string> Names;
438   Names.reserve(NameRefs.size());
439   for (auto *Name : NameRefs)
440     Names.emplace_back(*Name);
441   return Names;
442 }
443 
hasAnyNameFunc(ArrayRef<const StringRef * > NameRefs)444 Matcher<NamedDecl> hasAnyNameFunc(ArrayRef<const StringRef *> NameRefs) {
445   return internal::Matcher<NamedDecl>(
446       new internal::HasNameMatcher(vectorFromRefs(NameRefs)));
447 }
448 
hasAnySelectorFunc(ArrayRef<const StringRef * > NameRefs)449 Matcher<ObjCMessageExpr> hasAnySelectorFunc(
450     ArrayRef<const StringRef *> NameRefs) {
451   return hasAnySelectorMatcher(vectorFromRefs(NameRefs));
452 }
453 
hasAnyOperatorNameFunc(ArrayRef<const StringRef * > NameRefs)454 HasOpNameMatcher hasAnyOperatorNameFunc(ArrayRef<const StringRef *> NameRefs) {
455   return HasOpNameMatcher(vectorFromRefs(NameRefs));
456 }
457 
458 HasOverloadOpNameMatcher
hasAnyOverloadedOperatorNameFunc(ArrayRef<const StringRef * > NameRefs)459 hasAnyOverloadedOperatorNameFunc(ArrayRef<const StringRef *> NameRefs) {
460   return HasOverloadOpNameMatcher(vectorFromRefs(NameRefs));
461 }
462 
HasNameMatcher(std::vector<std::string> N)463 HasNameMatcher::HasNameMatcher(std::vector<std::string> N)
464     : UseUnqualifiedMatch(llvm::all_of(
465           N, [](StringRef Name) { return Name.find("::") == Name.npos; })),
466       Names(std::move(N)) {
467 #ifndef NDEBUG
468   for (StringRef Name : Names)
469     assert(!Name.empty());
470 #endif
471 }
472 
consumeNameSuffix(StringRef & FullName,StringRef Suffix)473 static bool consumeNameSuffix(StringRef &FullName, StringRef Suffix) {
474   StringRef Name = FullName;
475   if (!Name.endswith(Suffix))
476     return false;
477   Name = Name.drop_back(Suffix.size());
478   if (!Name.empty()) {
479     if (!Name.endswith("::"))
480       return false;
481     Name = Name.drop_back(2);
482   }
483   FullName = Name;
484   return true;
485 }
486 
getNodeName(const NamedDecl & Node,llvm::SmallString<128> & Scratch)487 static StringRef getNodeName(const NamedDecl &Node,
488                              llvm::SmallString<128> &Scratch) {
489   // Simple name.
490   if (Node.getIdentifier())
491     return Node.getName();
492 
493   if (Node.getDeclName()) {
494     // Name needs to be constructed.
495     Scratch.clear();
496     llvm::raw_svector_ostream OS(Scratch);
497     Node.printName(OS);
498     return OS.str();
499   }
500 
501   return "(anonymous)";
502 }
503 
getNodeName(const RecordDecl & Node,llvm::SmallString<128> & Scratch)504 static StringRef getNodeName(const RecordDecl &Node,
505                              llvm::SmallString<128> &Scratch) {
506   if (Node.getIdentifier()) {
507     return Node.getName();
508   }
509   Scratch.clear();
510   return ("(anonymous " + Node.getKindName() + ")").toStringRef(Scratch);
511 }
512 
getNodeName(const NamespaceDecl & Node,llvm::SmallString<128> & Scratch)513 static StringRef getNodeName(const NamespaceDecl &Node,
514                              llvm::SmallString<128> &Scratch) {
515   return Node.isAnonymousNamespace() ? "(anonymous namespace)" : Node.getName();
516 }
517 
518 namespace {
519 
520 class PatternSet {
521 public:
PatternSet(ArrayRef<std::string> Names)522   PatternSet(ArrayRef<std::string> Names) {
523     Patterns.reserve(Names.size());
524     for (StringRef Name : Names)
525       Patterns.push_back({Name, Name.startswith("::")});
526   }
527 
528   /// Consumes the name suffix from each pattern in the set and removes the ones
529   /// that didn't match.
530   /// Return true if there are still any patterns left.
consumeNameSuffix(StringRef NodeName,bool CanSkip)531   bool consumeNameSuffix(StringRef NodeName, bool CanSkip) {
532     for (size_t I = 0; I < Patterns.size();) {
533       if (::clang::ast_matchers::internal::consumeNameSuffix(Patterns[I].P,
534                                                              NodeName) ||
535           CanSkip) {
536         ++I;
537       } else {
538         Patterns.erase(Patterns.begin() + I);
539       }
540     }
541     return !Patterns.empty();
542   }
543 
544   /// Check if any of the patterns are a match.
545   /// A match will be a pattern that was fully consumed, that also matches the
546   /// 'fully qualified' requirement.
foundMatch(bool AllowFullyQualified) const547   bool foundMatch(bool AllowFullyQualified) const {
548     return llvm::any_of(Patterns, [&](const Pattern &Pattern) {
549       return Pattern.P.empty() &&
550              (AllowFullyQualified || !Pattern.IsFullyQualified);
551     });
552   }
553 
554 private:
555   struct Pattern {
556     StringRef P;
557     bool IsFullyQualified;
558   };
559 
560   llvm::SmallVector<Pattern, 8> Patterns;
561 };
562 
563 } // namespace
564 
matchesNodeUnqualified(const NamedDecl & Node) const565 bool HasNameMatcher::matchesNodeUnqualified(const NamedDecl &Node) const {
566   assert(UseUnqualifiedMatch);
567   llvm::SmallString<128> Scratch;
568   StringRef NodeName = getNodeName(Node, Scratch);
569   return llvm::any_of(Names, [&](StringRef Name) {
570     return consumeNameSuffix(Name, NodeName) && Name.empty();
571   });
572 }
573 
matchesNodeFullFast(const NamedDecl & Node) const574 bool HasNameMatcher::matchesNodeFullFast(const NamedDecl &Node) const {
575   PatternSet Patterns(Names);
576   llvm::SmallString<128> Scratch;
577 
578   // This function is copied and adapted from NamedDecl::printQualifiedName()
579   // By matching each part individually we optimize in a couple of ways:
580   //  - We can exit early on the first failure.
581   //  - We can skip inline/anonymous namespaces without another pass.
582   //  - We print one name at a time, reducing the chance of overflowing the
583   //    inlined space of the SmallString.
584 
585   // First, match the name.
586   if (!Patterns.consumeNameSuffix(getNodeName(Node, Scratch),
587                                   /*CanSkip=*/false))
588     return false;
589 
590   // Try to match each declaration context.
591   // We are allowed to skip anonymous and inline namespaces if they don't match.
592   const DeclContext *Ctx = Node.getDeclContext();
593 
594   if (Ctx->isFunctionOrMethod())
595     return Patterns.foundMatch(/*AllowFullyQualified=*/false);
596 
597   for (; Ctx; Ctx = Ctx->getParent()) {
598     // Linkage Spec can just be ignored
599     // FIXME: Any other DeclContext kinds that can be safely disregarded
600     if (isa<LinkageSpecDecl>(Ctx))
601       continue;
602     if (!isa<NamedDecl>(Ctx))
603       break;
604     if (Patterns.foundMatch(/*AllowFullyQualified=*/false))
605       return true;
606 
607     if (const auto *ND = dyn_cast<NamespaceDecl>(Ctx)) {
608       // If it matches (or we can skip it), continue.
609       if (Patterns.consumeNameSuffix(getNodeName(*ND, Scratch),
610                                      /*CanSkip=*/ND->isAnonymousNamespace() ||
611                                          ND->isInline()))
612         continue;
613       return false;
614     }
615     if (const auto *RD = dyn_cast<RecordDecl>(Ctx)) {
616       if (!isa<ClassTemplateSpecializationDecl>(Ctx)) {
617         if (Patterns.consumeNameSuffix(getNodeName(*RD, Scratch),
618                                        /*CanSkip=*/false))
619           continue;
620 
621         return false;
622       }
623     }
624 
625     // We don't know how to deal with this DeclContext.
626     // Fallback to the slow version of the code.
627     return matchesNodeFullSlow(Node);
628   }
629 
630   return Patterns.foundMatch(/*AllowFullyQualified=*/true);
631 }
632 
matchesNodeFullSlow(const NamedDecl & Node) const633 bool HasNameMatcher::matchesNodeFullSlow(const NamedDecl &Node) const {
634   const bool SkipUnwrittenCases[] = {false, true};
635   for (bool SkipUnwritten : SkipUnwrittenCases) {
636     llvm::SmallString<128> NodeName = StringRef("::");
637     llvm::raw_svector_ostream OS(NodeName);
638 
639     PrintingPolicy Policy = Node.getASTContext().getPrintingPolicy();
640     Policy.SuppressUnwrittenScope = SkipUnwritten;
641     Policy.SuppressInlineNamespace = SkipUnwritten;
642     Node.printQualifiedName(OS, Policy);
643 
644     const StringRef FullName = OS.str();
645 
646     for (const StringRef Pattern : Names) {
647       if (Pattern.startswith("::")) {
648         if (FullName == Pattern)
649           return true;
650       } else if (FullName.endswith(Pattern) &&
651                  FullName.drop_back(Pattern.size()).endswith("::")) {
652         return true;
653       }
654     }
655   }
656 
657   return false;
658 }
659 
matchesNode(const NamedDecl & Node) const660 bool HasNameMatcher::matchesNode(const NamedDecl &Node) const {
661   assert(matchesNodeFullFast(Node) == matchesNodeFullSlow(Node));
662   if (UseUnqualifiedMatch) {
663     assert(matchesNodeUnqualified(Node) == matchesNodeFullFast(Node));
664     return matchesNodeUnqualified(Node);
665   }
666   return matchesNodeFullFast(Node);
667 }
668 
669 // Checks whether \p Loc points to a token with source text of \p TokenText.
isTokenAtLoc(const SourceManager & SM,const LangOptions & LangOpts,StringRef Text,SourceLocation Loc)670 static bool isTokenAtLoc(const SourceManager &SM, const LangOptions &LangOpts,
671                          StringRef Text, SourceLocation Loc) {
672   llvm::SmallString<16> Buffer;
673   bool Invalid = false;
674   // Since `Loc` may point into an expansion buffer, which has no corresponding
675   // source, we need to look at the spelling location to read the actual source.
676   StringRef TokenText = Lexer::getSpelling(SM.getSpellingLoc(Loc), Buffer, SM,
677                                            LangOpts, &Invalid);
678   return !Invalid && Text == TokenText;
679 }
680 
681 llvm::Optional<SourceLocation>
getExpansionLocOfMacro(StringRef MacroName,SourceLocation Loc,const ASTContext & Context)682 getExpansionLocOfMacro(StringRef MacroName, SourceLocation Loc,
683                        const ASTContext &Context) {
684   auto &SM = Context.getSourceManager();
685   const LangOptions &LangOpts = Context.getLangOpts();
686   while (Loc.isMacroID()) {
687     SrcMgr::ExpansionInfo Expansion =
688         SM.getSLocEntry(SM.getFileID(Loc)).getExpansion();
689     if (Expansion.isMacroArgExpansion())
690       // Check macro argument for an expansion of the given macro. For example,
691       // `F(G(3))`, where `MacroName` is `G`.
692       if (llvm::Optional<SourceLocation> ArgLoc = getExpansionLocOfMacro(
693               MacroName, Expansion.getSpellingLoc(), Context))
694         return ArgLoc;
695     Loc = Expansion.getExpansionLocStart();
696     if (isTokenAtLoc(SM, LangOpts, MacroName, Loc))
697       return Loc;
698   }
699   return llvm::None;
700 }
701 
createAndVerifyRegex(StringRef Regex,llvm::Regex::RegexFlags Flags,StringRef MatcherID)702 std::shared_ptr<llvm::Regex> createAndVerifyRegex(StringRef Regex,
703                                                   llvm::Regex::RegexFlags Flags,
704                                                   StringRef MatcherID) {
705   assert(!Regex.empty() && "Empty regex string");
706   auto SharedRegex = std::make_shared<llvm::Regex>(Regex, Flags);
707   std::string Error;
708   if (!SharedRegex->isValid(Error)) {
709     llvm::WithColor::error()
710         << "building matcher '" << MatcherID << "': " << Error << "\n";
711     llvm::WithColor::note() << " input was '" << Regex << "'\n";
712   }
713   return SharedRegex;
714 }
715 } // end namespace internal
716 
717 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAutoreleasePoolStmt>
718     autoreleasePoolStmt;
719 const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl>
720     translationUnitDecl;
721 const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl> typedefDecl;
722 const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl>
723     typedefNameDecl;
724 const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl> typeAliasDecl;
725 const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasTemplateDecl>
726     typeAliasTemplateDecl;
727 const internal::VariadicAllOfMatcher<Decl> decl;
728 const internal::VariadicAllOfMatcher<DecompositionDecl> decompositionDecl;
729 const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl>
730     linkageSpecDecl;
731 const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl;
732 const internal::VariadicDynCastAllOfMatcher<Decl, LabelDecl> labelDecl;
733 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl> namespaceDecl;
734 const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceAliasDecl>
735     namespaceAliasDecl;
736 const internal::VariadicDynCastAllOfMatcher<Decl, RecordDecl> recordDecl;
737 const internal::VariadicDynCastAllOfMatcher<Decl, CXXRecordDecl> cxxRecordDecl;
738 const internal::VariadicDynCastAllOfMatcher<Decl, ClassTemplateDecl>
739     classTemplateDecl;
740 const internal::VariadicDynCastAllOfMatcher<Decl,
741                                             ClassTemplateSpecializationDecl>
742     classTemplateSpecializationDecl;
743 const internal::VariadicDynCastAllOfMatcher<
744     Decl, ClassTemplatePartialSpecializationDecl>
745     classTemplatePartialSpecializationDecl;
746 const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl>
747     declaratorDecl;
748 const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl> parmVarDecl;
749 const internal::VariadicDynCastAllOfMatcher<Decl, AccessSpecDecl>
750     accessSpecDecl;
751 const internal::VariadicAllOfMatcher<CXXCtorInitializer> cxxCtorInitializer;
752 const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument;
753 const internal::VariadicAllOfMatcher<TemplateArgumentLoc> templateArgumentLoc;
754 const internal::VariadicAllOfMatcher<TemplateName> templateName;
755 const internal::VariadicDynCastAllOfMatcher<Decl, NonTypeTemplateParmDecl>
756     nonTypeTemplateParmDecl;
757 const internal::VariadicDynCastAllOfMatcher<Decl, TemplateTypeParmDecl>
758     templateTypeParmDecl;
759 const internal::VariadicDynCastAllOfMatcher<Decl, TemplateTemplateParmDecl>
760     templateTemplateParmDecl;
761 
762 const internal::VariadicAllOfMatcher<QualType> qualType;
763 const internal::VariadicAllOfMatcher<Type> type;
764 const internal::VariadicAllOfMatcher<TypeLoc> typeLoc;
765 const internal::VariadicDynCastAllOfMatcher<Stmt, UnaryExprOrTypeTraitExpr>
766     unaryExprOrTypeTraitExpr;
767 const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl;
768 const internal::VariadicDynCastAllOfMatcher<Decl, CXXConstructorDecl>
769     cxxConstructorDecl;
770 const internal::VariadicDynCastAllOfMatcher<Decl, CXXDestructorDecl>
771     cxxDestructorDecl;
772 const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl;
773 const internal::VariadicDynCastAllOfMatcher<Decl, EnumConstantDecl>
774     enumConstantDecl;
775 const internal::VariadicDynCastAllOfMatcher<Decl, TagDecl> tagDecl;
776 const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl> cxxMethodDecl;
777 const internal::VariadicDynCastAllOfMatcher<Decl, CXXConversionDecl>
778     cxxConversionDecl;
779 const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl;
780 const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl;
781 const internal::VariadicDynCastAllOfMatcher<Decl, IndirectFieldDecl>
782     indirectFieldDecl;
783 const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl> functionDecl;
784 const internal::VariadicDynCastAllOfMatcher<Decl, FunctionTemplateDecl>
785     functionTemplateDecl;
786 const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl;
787 const internal::VariadicAllOfMatcher<Stmt> stmt;
788 const internal::VariadicDynCastAllOfMatcher<Stmt, DeclStmt> declStmt;
789 const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr;
790 const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedMemberExpr>
791     unresolvedMemberExpr;
792 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDependentScopeMemberExpr>
793     cxxDependentScopeMemberExpr;
794 const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr;
795 const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr;
796 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXMemberCallExpr>
797     cxxMemberCallExpr;
798 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCMessageExpr>
799     objcMessageExpr;
800 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCInterfaceDecl>
801     objcInterfaceDecl;
802 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCImplementationDecl>
803     objcImplementationDecl;
804 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCProtocolDecl>
805     objcProtocolDecl;
806 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryDecl>
807     objcCategoryDecl;
808 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl>
809     objcCategoryImplDecl;
810 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl>
811     objcMethodDecl;
812 const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl>
813     blockDecl;
814 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCIvarDecl> objcIvarDecl;
815 const internal::VariadicDynCastAllOfMatcher<Decl, ObjCPropertyDecl>
816     objcPropertyDecl;
817 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtThrowStmt>
818     objcThrowStmt;
819 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtTryStmt> objcTryStmt;
820 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtCatchStmt>
821     objcCatchStmt;
822 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtFinallyStmt>
823     objcFinallyStmt;
824 const internal::VariadicDynCastAllOfMatcher<Stmt, ExprWithCleanups>
825     exprWithCleanups;
826 const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr> initListExpr;
827 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXStdInitializerListExpr>
828     cxxStdInitializerListExpr;
829 const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitValueInitExpr>
830     implicitValueInitExpr;
831 const internal::VariadicDynCastAllOfMatcher<Stmt, ParenListExpr> parenListExpr;
832 const internal::VariadicDynCastAllOfMatcher<Stmt, SubstNonTypeTemplateParmExpr>
833     substNonTypeTemplateParmExpr;
834 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl;
835 const internal::VariadicDynCastAllOfMatcher<Decl, UsingDirectiveDecl>
836     usingDirectiveDecl;
837 const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedLookupExpr>
838     unresolvedLookupExpr;
839 const internal::VariadicDynCastAllOfMatcher<Decl, UnresolvedUsingValueDecl>
840     unresolvedUsingValueDecl;
841 const internal::VariadicDynCastAllOfMatcher<Decl, UnresolvedUsingTypenameDecl>
842     unresolvedUsingTypenameDecl;
843 const internal::VariadicDynCastAllOfMatcher<Stmt, ConstantExpr> constantExpr;
844 const internal::VariadicDynCastAllOfMatcher<Stmt, ParenExpr> parenExpr;
845 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstructExpr>
846     cxxConstructExpr;
847 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXUnresolvedConstructExpr>
848     cxxUnresolvedConstructExpr;
849 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> cxxThisExpr;
850 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBindTemporaryExpr>
851     cxxBindTemporaryExpr;
852 const internal::VariadicDynCastAllOfMatcher<Stmt, MaterializeTemporaryExpr>
853     materializeTemporaryExpr;
854 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> cxxNewExpr;
855 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr> cxxDeleteExpr;
856 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNoexceptExpr>
857     cxxNoexceptExpr;
858 const internal::VariadicDynCastAllOfMatcher<Stmt, ArraySubscriptExpr>
859     arraySubscriptExpr;
860 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDefaultArgExpr>
861     cxxDefaultArgExpr;
862 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXOperatorCallExpr>
863     cxxOperatorCallExpr;
864 const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
865 const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr> declRefExpr;
866 const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr> objcIvarRefExpr;
867 const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr;
868 const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
869 const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
870 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt>
871     cxxForRangeStmt;
872 const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt;
873 const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt;
874 const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt;
875 const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt> continueStmt;
876 const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt;
877 const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt;
878 const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt;
879 const internal::VariadicDynCastAllOfMatcher<Stmt, AddrLabelExpr> addrLabelExpr;
880 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt;
881 const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase;
882 const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt;
883 const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt> defaultStmt;
884 const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt> compoundStmt;
885 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt> cxxCatchStmt;
886 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
887 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr> cxxThrowExpr;
888 const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
889 const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
890 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr>
891     cxxBoolLiteral;
892 const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral> stringLiteral;
893 const internal::VariadicDynCastAllOfMatcher<Stmt, CharacterLiteral>
894     characterLiteral;
895 const internal::VariadicDynCastAllOfMatcher<Stmt, IntegerLiteral>
896     integerLiteral;
897 const internal::VariadicDynCastAllOfMatcher<Stmt, FloatingLiteral> floatLiteral;
898 const internal::VariadicDynCastAllOfMatcher<Stmt, ImaginaryLiteral> imaginaryLiteral;
899 const internal::VariadicDynCastAllOfMatcher<Stmt, FixedPointLiteral>
900     fixedPointLiteral;
901 const internal::VariadicDynCastAllOfMatcher<Stmt, UserDefinedLiteral>
902     userDefinedLiteral;
903 const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundLiteralExpr>
904     compoundLiteralExpr;
905 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNullPtrLiteralExpr>
906     cxxNullPtrLiteralExpr;
907 const internal::VariadicDynCastAllOfMatcher<Stmt, ChooseExpr> chooseExpr;
908 const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr> gnuNullExpr;
909 const internal::VariadicDynCastAllOfMatcher<Stmt, AtomicExpr> atomicExpr;
910 const internal::VariadicDynCastAllOfMatcher<Stmt, StmtExpr> stmtExpr;
911 const internal::VariadicDynCastAllOfMatcher<Stmt, BinaryOperator>
912     binaryOperator;
913 const internal::VariadicDynCastAllOfMatcher<Stmt, UnaryOperator> unaryOperator;
914 const internal::VariadicDynCastAllOfMatcher<Stmt, ConditionalOperator>
915     conditionalOperator;
916 const internal::VariadicDynCastAllOfMatcher<Stmt, BinaryConditionalOperator>
917     binaryConditionalOperator;
918 const internal::VariadicDynCastAllOfMatcher<Stmt, OpaqueValueExpr>
919     opaqueValueExpr;
920 const internal::VariadicDynCastAllOfMatcher<Decl, StaticAssertDecl>
921     staticAssertDecl;
922 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXReinterpretCastExpr>
923     cxxReinterpretCastExpr;
924 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXStaticCastExpr>
925     cxxStaticCastExpr;
926 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDynamicCastExpr>
927     cxxDynamicCastExpr;
928 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstCastExpr>
929     cxxConstCastExpr;
930 const internal::VariadicDynCastAllOfMatcher<Stmt, CStyleCastExpr>
931     cStyleCastExpr;
932 const internal::VariadicDynCastAllOfMatcher<Stmt, ExplicitCastExpr>
933     explicitCastExpr;
934 const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitCastExpr>
935     implicitCastExpr;
936 const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr;
937 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFunctionalCastExpr>
938     cxxFunctionalCastExpr;
939 const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTemporaryObjectExpr>
940     cxxTemporaryObjectExpr;
941 const internal::VariadicDynCastAllOfMatcher<Stmt, PredefinedExpr>
942     predefinedExpr;
943 const internal::VariadicDynCastAllOfMatcher<Stmt, DesignatedInitExpr>
944     designatedInitExpr;
945 const internal::VariadicOperatorMatcherFunc<
946     2, std::numeric_limits<unsigned>::max()>
947     eachOf = {internal::DynTypedMatcher::VO_EachOf};
948 const internal::VariadicOperatorMatcherFunc<
949     2, std::numeric_limits<unsigned>::max()>
950     anyOf = {internal::DynTypedMatcher::VO_AnyOf};
951 const internal::VariadicOperatorMatcherFunc<
952     2, std::numeric_limits<unsigned>::max()>
953     allOf = {internal::DynTypedMatcher::VO_AllOf};
954 const internal::VariadicOperatorMatcherFunc<1, 1> optionally = {
955     internal::DynTypedMatcher::VO_Optionally};
956 const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
957                                  internal::hasAnyNameFunc>
958     hasAnyName = {};
959 
960 const internal::VariadicFunction<internal::HasOpNameMatcher, StringRef,
961                                  internal::hasAnyOperatorNameFunc>
962     hasAnyOperatorName = {};
963 const internal::VariadicFunction<internal::HasOverloadOpNameMatcher, StringRef,
964                                  internal::hasAnyOverloadedOperatorNameFunc>
965     hasAnyOverloadedOperatorName = {};
966 const internal::VariadicFunction<internal::Matcher<ObjCMessageExpr>, StringRef,
967                                  internal::hasAnySelectorFunc>
968     hasAnySelector = {};
969 const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> has = {};
970 const internal::ArgumentAdaptingMatcherFunc<internal::HasDescendantMatcher>
971     hasDescendant = {};
972 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher> forEach =
973     {};
974 const internal::ArgumentAdaptingMatcherFunc<internal::ForEachDescendantMatcher>
975     forEachDescendant = {};
976 const internal::ArgumentAdaptingMatcherFunc<
977     internal::HasParentMatcher,
978     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
979     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
980     hasParent = {};
981 const internal::ArgumentAdaptingMatcherFunc<
982     internal::HasAncestorMatcher,
983     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>,
984     internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>>
985     hasAncestor = {};
986 const internal::VariadicOperatorMatcherFunc<1, 1> unless = {
987     internal::DynTypedMatcher::VO_UnaryNot};
988 const internal::VariadicAllOfMatcher<NestedNameSpecifier> nestedNameSpecifier;
989 const internal::VariadicAllOfMatcher<NestedNameSpecifierLoc>
990     nestedNameSpecifierLoc;
991 const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr>
992     cudaKernelCallExpr;
993 const AstTypeMatcher<BuiltinType> builtinType;
994 const AstTypeMatcher<ArrayType> arrayType;
995 const AstTypeMatcher<ComplexType> complexType;
996 const AstTypeMatcher<ConstantArrayType> constantArrayType;
997 const AstTypeMatcher<DeducedTemplateSpecializationType>
998     deducedTemplateSpecializationType;
999 const AstTypeMatcher<DependentSizedArrayType> dependentSizedArrayType;
1000 const AstTypeMatcher<IncompleteArrayType> incompleteArrayType;
1001 const AstTypeMatcher<VariableArrayType> variableArrayType;
1002 const AstTypeMatcher<AtomicType> atomicType;
1003 const AstTypeMatcher<AutoType> autoType;
1004 const AstTypeMatcher<DecltypeType> decltypeType;
1005 const AstTypeMatcher<FunctionType> functionType;
1006 const AstTypeMatcher<FunctionProtoType> functionProtoType;
1007 const AstTypeMatcher<ParenType> parenType;
1008 const AstTypeMatcher<BlockPointerType> blockPointerType;
1009 const AstTypeMatcher<MemberPointerType> memberPointerType;
1010 const AstTypeMatcher<PointerType> pointerType;
1011 const AstTypeMatcher<ObjCObjectPointerType> objcObjectPointerType;
1012 const AstTypeMatcher<ReferenceType> referenceType;
1013 const AstTypeMatcher<LValueReferenceType> lValueReferenceType;
1014 const AstTypeMatcher<RValueReferenceType> rValueReferenceType;
1015 const AstTypeMatcher<TypedefType> typedefType;
1016 const AstTypeMatcher<EnumType> enumType;
1017 const AstTypeMatcher<TemplateSpecializationType> templateSpecializationType;
1018 const AstTypeMatcher<UnaryTransformType> unaryTransformType;
1019 const AstTypeMatcher<RecordType> recordType;
1020 const AstTypeMatcher<TagType> tagType;
1021 const AstTypeMatcher<ElaboratedType> elaboratedType;
1022 const AstTypeMatcher<SubstTemplateTypeParmType> substTemplateTypeParmType;
1023 const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType;
1024 const AstTypeMatcher<InjectedClassNameType> injectedClassNameType;
1025 const AstTypeMatcher<DecayedType> decayedType;
1026 AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasElementType,
1027                                  AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
1028                                                                  ComplexType));
1029 AST_TYPELOC_TRAVERSE_MATCHER_DEF(hasValueType,
1030                                  AST_POLYMORPHIC_SUPPORTED_TYPES(AtomicType));
1031 AST_TYPELOC_TRAVERSE_MATCHER_DEF(
1032     pointee,
1033     AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
1034                                     PointerType, ReferenceType));
1035 
1036 const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
1037     ompExecutableDirective;
1038 const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPDefaultClause>
1039     ompDefaultClause;
1040 const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl>
1041     cxxDeductionGuideDecl;
1042 
1043 } // end namespace ast_matchers
1044 } // end namespace clang
1045