ASTMatchersInternal.cpp 44 KB

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