1 //===- Registry.cpp - Matcher registry ------------------------------------===//
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 /// \file
10 /// Registry map populated at static initialization time.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/ASTMatchers/Dynamic/Registry.h"
15 #include "Marshallers.h"
16 #include "clang/AST/ASTTypeTraits.h"
17 #include "clang/ASTMatchers/ASTMatchers.h"
18 #include "clang/ASTMatchers/Dynamic/Diagnostics.h"
19 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <cassert>
27 #include <iterator>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 using namespace clang::ast_type_traits;
35 
36 namespace clang {
37 namespace ast_matchers {
38 namespace dynamic {
39 
40 namespace {
41 
42 using internal::MatcherDescriptor;
43 
44 using ConstructorMap =
45     llvm::StringMap<std::unique_ptr<const MatcherDescriptor>>;
46 
47 class RegistryMaps {
48 public:
49   RegistryMaps();
50   ~RegistryMaps();
51 
constructors() const52   const ConstructorMap &constructors() const { return Constructors; }
53 
54 private:
55   void registerMatcher(StringRef MatcherName,
56                        std::unique_ptr<MatcherDescriptor> Callback);
57 
58   ConstructorMap Constructors;
59 };
60 
61 } // namespace
62 
registerMatcher(StringRef MatcherName,std::unique_ptr<MatcherDescriptor> Callback)63 void RegistryMaps::registerMatcher(
64     StringRef MatcherName, std::unique_ptr<MatcherDescriptor> Callback) {
65   assert(Constructors.find(MatcherName) == Constructors.end());
66   Constructors[MatcherName] = std::move(Callback);
67 }
68 
69 #define REGISTER_MATCHER(name)                                                 \
70   registerMatcher(#name, internal::makeMatcherAutoMarshall(                    \
71                              ::clang::ast_matchers::name, #name));
72 
73 #define REGISTER_MATCHER_OVERLOAD(name)                                        \
74   registerMatcher(#name,                                                       \
75       std::make_unique<internal::OverloadedMatcherDescriptor>(name##Callbacks))
76 
77 #define SPECIFIC_MATCHER_OVERLOAD(name, Id)                                    \
78   static_cast<::clang::ast_matchers::name##_Type##Id>(                         \
79       ::clang::ast_matchers::name)
80 
81 #define MATCHER_OVERLOAD_ENTRY(name, Id)                                       \
82         internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, Id), \
83                                           #name)
84 
85 #define REGISTER_OVERLOADED_2(name)                                            \
86   do {                                                                         \
87     std::unique_ptr<MatcherDescriptor> name##Callbacks[] = {                   \
88         MATCHER_OVERLOAD_ENTRY(name, 0),                                       \
89         MATCHER_OVERLOAD_ENTRY(name, 1)};                                      \
90     REGISTER_MATCHER_OVERLOAD(name);                                           \
91   } while (false)
92 
93 /// Generate a registry map with all the known matchers.
94 /// Please keep sorted alphabetically!
RegistryMaps()95 RegistryMaps::RegistryMaps() {
96   // TODO: Here is the list of the missing matchers, grouped by reason.
97   //
98   // Need Variant/Parser fixes:
99   // ofKind
100   //
101   // Polymorphic + argument overload:
102   // findAll
103   //
104   // Other:
105   // equalsNode
106 
107   REGISTER_OVERLOADED_2(callee);
108   REGISTER_OVERLOADED_2(hasAnyCapture);
109   REGISTER_OVERLOADED_2(hasPrefix);
110   REGISTER_OVERLOADED_2(hasType);
111   REGISTER_OVERLOADED_2(ignoringParens);
112   REGISTER_OVERLOADED_2(isDerivedFrom);
113   REGISTER_OVERLOADED_2(isDirectlyDerivedFrom);
114   REGISTER_OVERLOADED_2(isSameOrDerivedFrom);
115   REGISTER_OVERLOADED_2(loc);
116   REGISTER_OVERLOADED_2(pointsTo);
117   REGISTER_OVERLOADED_2(references);
118   REGISTER_OVERLOADED_2(thisPointerType);
119 
120   std::unique_ptr<MatcherDescriptor> equalsCallbacks[] = {
121       MATCHER_OVERLOAD_ENTRY(equals, 0),
122       MATCHER_OVERLOAD_ENTRY(equals, 1),
123       MATCHER_OVERLOAD_ENTRY(equals, 2),
124   };
125   REGISTER_MATCHER_OVERLOAD(equals);
126 
127   REGISTER_MATCHER(accessSpecDecl);
128   REGISTER_MATCHER(addrLabelExpr);
129   REGISTER_MATCHER(alignOfExpr);
130   REGISTER_MATCHER(allOf);
131   REGISTER_MATCHER(anyOf);
132   REGISTER_MATCHER(anything);
133   REGISTER_MATCHER(argumentCountIs);
134   REGISTER_MATCHER(arraySubscriptExpr);
135   REGISTER_MATCHER(arrayType);
136   REGISTER_MATCHER(asString);
137   REGISTER_MATCHER(asmStmt);
138   REGISTER_MATCHER(atomicExpr);
139   REGISTER_MATCHER(atomicType);
140   REGISTER_MATCHER(autoType);
141   REGISTER_MATCHER(autoreleasePoolStmt)
142   REGISTER_MATCHER(binaryConditionalOperator);
143   REGISTER_MATCHER(binaryOperator);
144   REGISTER_MATCHER(blockDecl);
145   REGISTER_MATCHER(blockExpr);
146   REGISTER_MATCHER(blockPointerType);
147   REGISTER_MATCHER(booleanType);
148   REGISTER_MATCHER(breakStmt);
149   REGISTER_MATCHER(builtinType);
150   REGISTER_MATCHER(cStyleCastExpr);
151   REGISTER_MATCHER(callExpr);
152   REGISTER_MATCHER(caseStmt);
153   REGISTER_MATCHER(castExpr);
154   REGISTER_MATCHER(characterLiteral);
155   REGISTER_MATCHER(chooseExpr);
156   REGISTER_MATCHER(classTemplateDecl);
157   REGISTER_MATCHER(classTemplateSpecializationDecl);
158   REGISTER_MATCHER(complexType);
159   REGISTER_MATCHER(compoundLiteralExpr);
160   REGISTER_MATCHER(compoundStmt);
161   REGISTER_MATCHER(conditionalOperator);
162   REGISTER_MATCHER(constantArrayType);
163   REGISTER_MATCHER(constantExpr);
164   REGISTER_MATCHER(containsDeclaration);
165   REGISTER_MATCHER(continueStmt);
166   REGISTER_MATCHER(cudaKernelCallExpr);
167   REGISTER_MATCHER(cxxBindTemporaryExpr);
168   REGISTER_MATCHER(cxxBoolLiteral);
169   REGISTER_MATCHER(cxxCatchStmt);
170   REGISTER_MATCHER(cxxConstCastExpr);
171   REGISTER_MATCHER(cxxConstructExpr);
172   REGISTER_MATCHER(cxxConstructorDecl);
173   REGISTER_MATCHER(cxxConversionDecl);
174   REGISTER_MATCHER(cxxCtorInitializer);
175   REGISTER_MATCHER(cxxDeductionGuideDecl);
176   REGISTER_MATCHER(cxxDefaultArgExpr);
177   REGISTER_MATCHER(cxxDeleteExpr);
178   REGISTER_MATCHER(cxxDependentScopeMemberExpr);
179   REGISTER_MATCHER(cxxDestructorDecl);
180   REGISTER_MATCHER(cxxDynamicCastExpr);
181   REGISTER_MATCHER(cxxForRangeStmt);
182   REGISTER_MATCHER(cxxFunctionalCastExpr);
183   REGISTER_MATCHER(cxxMemberCallExpr);
184   REGISTER_MATCHER(cxxMethodDecl);
185   REGISTER_MATCHER(cxxNewExpr);
186   REGISTER_MATCHER(cxxNullPtrLiteralExpr);
187   REGISTER_MATCHER(cxxOperatorCallExpr);
188   REGISTER_MATCHER(cxxRecordDecl);
189   REGISTER_MATCHER(cxxReinterpretCastExpr);
190   REGISTER_MATCHER(cxxStaticCastExpr);
191   REGISTER_MATCHER(cxxStdInitializerListExpr);
192   REGISTER_MATCHER(cxxTemporaryObjectExpr);
193   REGISTER_MATCHER(cxxThisExpr);
194   REGISTER_MATCHER(cxxThrowExpr);
195   REGISTER_MATCHER(cxxTryStmt);
196   REGISTER_MATCHER(cxxUnresolvedConstructExpr);
197   REGISTER_MATCHER(decayedType);
198   REGISTER_MATCHER(decl);
199   REGISTER_MATCHER(declCountIs);
200   REGISTER_MATCHER(declRefExpr);
201   REGISTER_MATCHER(declStmt);
202   REGISTER_MATCHER(declaratorDecl);
203   REGISTER_MATCHER(decltypeType);
204   REGISTER_MATCHER(defaultStmt);
205   REGISTER_MATCHER(dependentSizedArrayType);
206   REGISTER_MATCHER(designatedInitExpr);
207   REGISTER_MATCHER(designatorCountIs);
208   REGISTER_MATCHER(doStmt);
209   REGISTER_MATCHER(eachOf);
210   REGISTER_MATCHER(elaboratedType);
211   REGISTER_MATCHER(enumConstantDecl);
212   REGISTER_MATCHER(enumDecl);
213   REGISTER_MATCHER(enumType);
214   REGISTER_MATCHER(equalsBoundNode);
215   REGISTER_MATCHER(equalsIntegralValue);
216   REGISTER_MATCHER(explicitCastExpr);
217   REGISTER_MATCHER(expr);
218   REGISTER_MATCHER(exprWithCleanups);
219   REGISTER_MATCHER(fieldDecl);
220   REGISTER_MATCHER(floatLiteral);
221   REGISTER_MATCHER(forEach);
222   REGISTER_MATCHER(forEachArgumentWithParam);
223   REGISTER_MATCHER(forEachConstructorInitializer);
224   REGISTER_MATCHER(forEachDescendant);
225   REGISTER_MATCHER(forEachOverridden);
226   REGISTER_MATCHER(forEachSwitchCase);
227   REGISTER_MATCHER(forField);
228   REGISTER_MATCHER(forFunction);
229   REGISTER_MATCHER(forStmt);
230   REGISTER_MATCHER(friendDecl);
231   REGISTER_MATCHER(functionDecl);
232   REGISTER_MATCHER(functionProtoType);
233   REGISTER_MATCHER(functionTemplateDecl);
234   REGISTER_MATCHER(functionType);
235   REGISTER_MATCHER(gnuNullExpr);
236   REGISTER_MATCHER(gotoStmt);
237   REGISTER_MATCHER(has);
238   REGISTER_MATCHER(hasAncestor);
239   REGISTER_MATCHER(hasAnyArgument);
240   REGISTER_MATCHER(hasAnyClause);
241   REGISTER_MATCHER(hasAnyConstructorInitializer);
242   REGISTER_MATCHER(hasAnyDeclaration);
243   REGISTER_MATCHER(hasAnyName);
244   REGISTER_MATCHER(hasAnyParameter);
245   REGISTER_MATCHER(hasAnySelector);
246   REGISTER_MATCHER(hasAnySubstatement);
247   REGISTER_MATCHER(hasAnyTemplateArgument);
248   REGISTER_MATCHER(hasAnyUsingShadowDecl);
249   REGISTER_MATCHER(hasArgument);
250   REGISTER_MATCHER(hasArgumentOfType);
251   REGISTER_MATCHER(hasArraySize);
252   REGISTER_MATCHER(hasAttr);
253   REGISTER_MATCHER(hasAutomaticStorageDuration);
254   REGISTER_MATCHER(hasBase);
255   REGISTER_MATCHER(hasBitWidth);
256   REGISTER_MATCHER(hasBody);
257   REGISTER_MATCHER(hasCanonicalType);
258   REGISTER_MATCHER(hasCaseConstant);
259   REGISTER_MATCHER(hasCastKind);
260   REGISTER_MATCHER(hasCondition);
261   REGISTER_MATCHER(hasConditionVariableStatement);
262   REGISTER_MATCHER(hasDecayedType);
263   REGISTER_MATCHER(hasDeclContext);
264   REGISTER_MATCHER(hasDeclaration);
265   REGISTER_MATCHER(hasDeducedType);
266   REGISTER_MATCHER(hasDefaultArgument);
267   REGISTER_MATCHER(hasDefinition);
268   REGISTER_MATCHER(hasDescendant);
269   REGISTER_MATCHER(hasDestinationType);
270   REGISTER_MATCHER(hasDynamicExceptionSpec);
271   REGISTER_MATCHER(hasEitherOperand);
272   REGISTER_MATCHER(hasElementType);
273   REGISTER_MATCHER(hasElse);
274   REGISTER_MATCHER(hasExplicitSpecifier);
275   REGISTER_MATCHER(hasExternalFormalLinkage);
276   REGISTER_MATCHER(hasFalseExpression);
277   REGISTER_MATCHER(hasGlobalStorage);
278   REGISTER_MATCHER(hasImplicitDestinationType);
279   REGISTER_MATCHER(hasInClassInitializer);
280   REGISTER_MATCHER(hasIncrement);
281   REGISTER_MATCHER(hasIndex);
282   REGISTER_MATCHER(hasInit);
283   REGISTER_MATCHER(hasInitializer);
284   REGISTER_MATCHER(hasInitStatement);
285   REGISTER_MATCHER(hasKeywordSelector);
286   REGISTER_MATCHER(hasLHS);
287   REGISTER_MATCHER(hasLocalQualifiers);
288   REGISTER_MATCHER(hasLocalStorage);
289   REGISTER_MATCHER(hasLoopInit);
290   REGISTER_MATCHER(hasLoopVariable);
291   REGISTER_MATCHER(hasMethod);
292   REGISTER_MATCHER(hasName);
293   REGISTER_MATCHER(hasNullSelector);
294   REGISTER_MATCHER(hasObjectExpression);
295   REGISTER_MATCHER(hasOperatorName);
296   REGISTER_MATCHER(hasOverloadedOperatorName);
297   REGISTER_MATCHER(hasParameter);
298   REGISTER_MATCHER(hasParent);
299   REGISTER_MATCHER(hasQualifier);
300   REGISTER_MATCHER(hasRHS);
301   REGISTER_MATCHER(hasRangeInit);
302   REGISTER_MATCHER(hasReceiver);
303   REGISTER_MATCHER(hasReceiverType);
304   REGISTER_MATCHER(hasReplacementType);
305   REGISTER_MATCHER(hasReturnValue);
306   REGISTER_MATCHER(hasSelector);
307   REGISTER_MATCHER(hasSingleDecl);
308   REGISTER_MATCHER(hasSize);
309   REGISTER_MATCHER(hasSizeExpr);
310   REGISTER_MATCHER(hasSourceExpression);
311   REGISTER_MATCHER(hasSpecializedTemplate);
312   REGISTER_MATCHER(hasStaticStorageDuration);
313   REGISTER_MATCHER(hasStructuredBlock);
314   REGISTER_MATCHER(hasSyntacticForm);
315   REGISTER_MATCHER(hasTargetDecl);
316   REGISTER_MATCHER(hasTemplateArgument);
317   REGISTER_MATCHER(hasThen);
318   REGISTER_MATCHER(hasThreadStorageDuration);
319   REGISTER_MATCHER(hasTrailingReturn);
320   REGISTER_MATCHER(hasTrueExpression);
321   REGISTER_MATCHER(hasTypeLoc);
322   REGISTER_MATCHER(hasUnaryOperand);
323   REGISTER_MATCHER(hasUnarySelector);
324   REGISTER_MATCHER(hasUnderlyingDecl);
325   REGISTER_MATCHER(hasUnderlyingType);
326   REGISTER_MATCHER(hasUnqualifiedDesugaredType);
327   REGISTER_MATCHER(hasValueType);
328   REGISTER_MATCHER(ifStmt);
329   REGISTER_MATCHER(ignoringElidableConstructorCall);
330   REGISTER_MATCHER(ignoringImpCasts);
331   REGISTER_MATCHER(ignoringImplicit);
332   REGISTER_MATCHER(ignoringParenCasts);
333   REGISTER_MATCHER(ignoringParenImpCasts);
334   REGISTER_MATCHER(imaginaryLiteral);
335   REGISTER_MATCHER(implicitCastExpr);
336   REGISTER_MATCHER(implicitValueInitExpr);
337   REGISTER_MATCHER(incompleteArrayType);
338   REGISTER_MATCHER(indirectFieldDecl);
339   REGISTER_MATCHER(initListExpr);
340   REGISTER_MATCHER(injectedClassNameType);
341   REGISTER_MATCHER(innerType);
342   REGISTER_MATCHER(integerLiteral);
343   REGISTER_MATCHER(isAllowedToContainClauseKind);
344   REGISTER_MATCHER(isAnonymous);
345   REGISTER_MATCHER(isAnyCharacter);
346   REGISTER_MATCHER(isAnyPointer);
347   REGISTER_MATCHER(isArray);
348   REGISTER_MATCHER(isArrow);
349   REGISTER_MATCHER(isAssignmentOperator);
350   REGISTER_MATCHER(isBaseInitializer);
351   REGISTER_MATCHER(isBitField);
352   REGISTER_MATCHER(isCatchAll);
353   REGISTER_MATCHER(isClass);
354   REGISTER_MATCHER(isClassMessage);
355   REGISTER_MATCHER(isClassMethod);
356   REGISTER_MATCHER(isConst);
357   REGISTER_MATCHER(isConstQualified);
358   REGISTER_MATCHER(isConstexpr);
359   REGISTER_MATCHER(isCopyAssignmentOperator);
360   REGISTER_MATCHER(isCopyConstructor);
361   REGISTER_MATCHER(isDefaultConstructor);
362   REGISTER_MATCHER(isDefaulted);
363   REGISTER_MATCHER(isDefinition);
364   REGISTER_MATCHER(isDelegatingConstructor);
365   REGISTER_MATCHER(isDeleted);
366   REGISTER_MATCHER(isExceptionVariable);
367   REGISTER_MATCHER(isExpansionInFileMatching);
368   REGISTER_MATCHER(isExpansionInMainFile);
369   REGISTER_MATCHER(isExpansionInSystemHeader);
370   REGISTER_MATCHER(isExplicit);
371   REGISTER_MATCHER(isExplicitTemplateSpecialization);
372   REGISTER_MATCHER(isExpr);
373   REGISTER_MATCHER(isExternC);
374   REGISTER_MATCHER(isFinal);
375   REGISTER_MATCHER(isImplicit);
376   REGISTER_MATCHER(isInStdNamespace);
377   REGISTER_MATCHER(isInTemplateInstantiation);
378   REGISTER_MATCHER(isInline);
379   REGISTER_MATCHER(isInstanceMessage);
380   REGISTER_MATCHER(isInstanceMethod);
381   REGISTER_MATCHER(isInstantiated);
382   REGISTER_MATCHER(isInstantiationDependent);
383   REGISTER_MATCHER(isInteger);
384   REGISTER_MATCHER(isIntegral);
385   REGISTER_MATCHER(isLambda);
386   REGISTER_MATCHER(isListInitialization);
387   REGISTER_MATCHER(isMain);
388   REGISTER_MATCHER(isMemberInitializer);
389   REGISTER_MATCHER(isMoveAssignmentOperator);
390   REGISTER_MATCHER(isMoveConstructor);
391   REGISTER_MATCHER(isNoReturn);
392   REGISTER_MATCHER(isNoThrow);
393   REGISTER_MATCHER(isNoneKind);
394   REGISTER_MATCHER(isOMPStructuredBlock);
395   REGISTER_MATCHER(isOverride);
396   REGISTER_MATCHER(isPrivate);
397   REGISTER_MATCHER(isProtected);
398   REGISTER_MATCHER(isPublic);
399   REGISTER_MATCHER(isPure);
400   REGISTER_MATCHER(isScoped);
401   REGISTER_MATCHER(isSharedKind);
402   REGISTER_MATCHER(isSignedInteger);
403   REGISTER_MATCHER(isStandaloneDirective);
404   REGISTER_MATCHER(isStaticLocal);
405   REGISTER_MATCHER(isStaticStorageClass);
406   REGISTER_MATCHER(isStruct);
407   REGISTER_MATCHER(isTemplateInstantiation);
408   REGISTER_MATCHER(isTypeDependent);
409   REGISTER_MATCHER(isUnion);
410   REGISTER_MATCHER(isUnsignedInteger);
411   REGISTER_MATCHER(isUserProvided);
412   REGISTER_MATCHER(isValueDependent);
413   REGISTER_MATCHER(isVariadic);
414   REGISTER_MATCHER(isVirtual);
415   REGISTER_MATCHER(isVirtualAsWritten);
416   REGISTER_MATCHER(isVolatileQualified);
417   REGISTER_MATCHER(isWritten);
418   REGISTER_MATCHER(lValueReferenceType);
419   REGISTER_MATCHER(labelDecl);
420   REGISTER_MATCHER(labelStmt);
421   REGISTER_MATCHER(lambdaExpr);
422   REGISTER_MATCHER(linkageSpecDecl);
423   REGISTER_MATCHER(matchesName);
424   REGISTER_MATCHER(matchesSelector);
425   REGISTER_MATCHER(materializeTemporaryExpr);
426   REGISTER_MATCHER(member);
427   REGISTER_MATCHER(memberExpr);
428   REGISTER_MATCHER(memberPointerType);
429   REGISTER_MATCHER(namedDecl);
430   REGISTER_MATCHER(namesType);
431   REGISTER_MATCHER(namespaceAliasDecl);
432   REGISTER_MATCHER(namespaceDecl);
433   REGISTER_MATCHER(nestedNameSpecifier);
434   REGISTER_MATCHER(nestedNameSpecifierLoc);
435   REGISTER_MATCHER(nonTypeTemplateParmDecl);
436   REGISTER_MATCHER(nullPointerConstant);
437   REGISTER_MATCHER(nullStmt);
438   REGISTER_MATCHER(numSelectorArgs);
439   REGISTER_MATCHER(objcCatchStmt);
440   REGISTER_MATCHER(objcCategoryDecl);
441   REGISTER_MATCHER(objcCategoryImplDecl);
442   REGISTER_MATCHER(objcFinallyStmt);
443   REGISTER_MATCHER(objcImplementationDecl);
444   REGISTER_MATCHER(objcInterfaceDecl);
445   REGISTER_MATCHER(objcIvarDecl);
446   REGISTER_MATCHER(objcIvarRefExpr);
447   REGISTER_MATCHER(objcMessageExpr);
448   REGISTER_MATCHER(objcMethodDecl);
449   REGISTER_MATCHER(objcObjectPointerType);
450   REGISTER_MATCHER(objcPropertyDecl);
451   REGISTER_MATCHER(objcProtocolDecl);
452   REGISTER_MATCHER(objcThrowStmt);
453   REGISTER_MATCHER(objcTryStmt);
454   REGISTER_MATCHER(ofClass);
455   REGISTER_MATCHER(ompDefaultClause);
456   REGISTER_MATCHER(ompExecutableDirective);
457   REGISTER_MATCHER(on);
458   REGISTER_MATCHER(onImplicitObjectArgument);
459   REGISTER_MATCHER(opaqueValueExpr);
460   REGISTER_MATCHER(optionally);
461   REGISTER_MATCHER(parameterCountIs);
462   REGISTER_MATCHER(parenExpr);
463   REGISTER_MATCHER(parenListExpr);
464   REGISTER_MATCHER(parenType);
465   REGISTER_MATCHER(parmVarDecl);
466   REGISTER_MATCHER(pointee);
467   REGISTER_MATCHER(pointerType);
468   REGISTER_MATCHER(predefinedExpr);
469   REGISTER_MATCHER(qualType);
470   REGISTER_MATCHER(rValueReferenceType);
471   REGISTER_MATCHER(realFloatingPointType);
472   REGISTER_MATCHER(recordDecl);
473   REGISTER_MATCHER(recordType);
474   REGISTER_MATCHER(referenceType);
475   REGISTER_MATCHER(refersToDeclaration);
476   REGISTER_MATCHER(refersToIntegralType);
477   REGISTER_MATCHER(refersToTemplate);
478   REGISTER_MATCHER(refersToType);
479   REGISTER_MATCHER(requiresZeroInitialization);
480   REGISTER_MATCHER(returnStmt);
481   REGISTER_MATCHER(returns);
482   REGISTER_MATCHER(sizeOfExpr);
483   REGISTER_MATCHER(specifiesNamespace);
484   REGISTER_MATCHER(specifiesType);
485   REGISTER_MATCHER(specifiesTypeLoc);
486   REGISTER_MATCHER(statementCountIs);
487   REGISTER_MATCHER(staticAssertDecl);
488   REGISTER_MATCHER(stmt);
489   REGISTER_MATCHER(stmtExpr);
490   REGISTER_MATCHER(stringLiteral);
491   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
492   REGISTER_MATCHER(substTemplateTypeParmType);
493   REGISTER_MATCHER(switchCase);
494   REGISTER_MATCHER(switchStmt);
495   REGISTER_MATCHER(tagType);
496   REGISTER_MATCHER(templateArgument);
497   REGISTER_MATCHER(templateArgumentCountIs);
498   REGISTER_MATCHER(templateName);
499   REGISTER_MATCHER(templateSpecializationType);
500   REGISTER_MATCHER(templateTypeParmDecl);
501   REGISTER_MATCHER(templateTypeParmType);
502   REGISTER_MATCHER(throughUsingDecl);
503   REGISTER_MATCHER(to);
504   REGISTER_MATCHER(translationUnitDecl);
505   REGISTER_MATCHER(type);
506   REGISTER_MATCHER(typeAliasDecl);
507   REGISTER_MATCHER(typeAliasTemplateDecl);
508   REGISTER_MATCHER(typeLoc);
509   REGISTER_MATCHER(typedefDecl);
510   REGISTER_MATCHER(typedefNameDecl);
511   REGISTER_MATCHER(typedefType);
512   REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
513   REGISTER_MATCHER(unaryOperator);
514   REGISTER_MATCHER(unaryTransformType);
515   REGISTER_MATCHER(unless);
516   REGISTER_MATCHER(unresolvedLookupExpr);
517   REGISTER_MATCHER(unresolvedMemberExpr);
518   REGISTER_MATCHER(unresolvedUsingTypenameDecl);
519   REGISTER_MATCHER(unresolvedUsingValueDecl);
520   REGISTER_MATCHER(userDefinedLiteral);
521   REGISTER_MATCHER(usesADL);
522   REGISTER_MATCHER(usingDecl);
523   REGISTER_MATCHER(usingDirectiveDecl);
524   REGISTER_MATCHER(valueDecl);
525   REGISTER_MATCHER(varDecl);
526   REGISTER_MATCHER(variableArrayType);
527   REGISTER_MATCHER(voidType);
528   REGISTER_MATCHER(whileStmt);
529   REGISTER_MATCHER(withInitializer);
530 }
531 
532 RegistryMaps::~RegistryMaps() = default;
533 
534 static llvm::ManagedStatic<RegistryMaps> RegistryData;
535 
536 // static
lookupMatcherCtor(StringRef MatcherName)537 llvm::Optional<MatcherCtor> Registry::lookupMatcherCtor(StringRef MatcherName) {
538   auto it = RegistryData->constructors().find(MatcherName);
539   return it == RegistryData->constructors().end()
540              ? llvm::Optional<MatcherCtor>()
541              : it->second.get();
542 }
543 
operator <<(llvm::raw_ostream & OS,const std::set<ASTNodeKind> & KS)544 static llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
545                                      const std::set<ASTNodeKind> &KS) {
546   unsigned Count = 0;
547   for (std::set<ASTNodeKind>::const_iterator I = KS.begin(), E = KS.end();
548        I != E; ++I) {
549     if (I != KS.begin())
550       OS << "|";
551     if (Count++ == 3) {
552       OS << "...";
553       break;
554     }
555     OS << *I;
556   }
557   return OS;
558 }
559 
getAcceptedCompletionTypes(ArrayRef<std::pair<MatcherCtor,unsigned>> Context)560 std::vector<ArgKind> Registry::getAcceptedCompletionTypes(
561     ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
562   ASTNodeKind InitialTypes[] = {
563       ASTNodeKind::getFromNodeKind<Decl>(),
564       ASTNodeKind::getFromNodeKind<QualType>(),
565       ASTNodeKind::getFromNodeKind<Type>(),
566       ASTNodeKind::getFromNodeKind<Stmt>(),
567       ASTNodeKind::getFromNodeKind<NestedNameSpecifier>(),
568       ASTNodeKind::getFromNodeKind<NestedNameSpecifierLoc>(),
569       ASTNodeKind::getFromNodeKind<TypeLoc>()};
570 
571   // Starting with the above seed of acceptable top-level matcher types, compute
572   // the acceptable type set for the argument indicated by each context element.
573   std::set<ArgKind> TypeSet(std::begin(InitialTypes), std::end(InitialTypes));
574   for (const auto &CtxEntry : Context) {
575     MatcherCtor Ctor = CtxEntry.first;
576     unsigned ArgNumber = CtxEntry.second;
577     std::vector<ArgKind> NextTypeSet;
578     for (const ArgKind &Kind : TypeSet) {
579       if (Kind.getArgKind() == Kind.AK_Matcher &&
580           Ctor->isConvertibleTo(Kind.getMatcherKind()) &&
581           (Ctor->isVariadic() || ArgNumber < Ctor->getNumArgs()))
582         Ctor->getArgKinds(Kind.getMatcherKind(), ArgNumber, NextTypeSet);
583     }
584     TypeSet.clear();
585     TypeSet.insert(NextTypeSet.begin(), NextTypeSet.end());
586   }
587   return std::vector<ArgKind>(TypeSet.begin(), TypeSet.end());
588 }
589 
590 std::vector<MatcherCompletion>
getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes)591 Registry::getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes) {
592   std::vector<MatcherCompletion> Completions;
593 
594   // Search the registry for acceptable matchers.
595   for (const auto &M : RegistryData->constructors()) {
596     const MatcherDescriptor& Matcher = *M.getValue();
597     StringRef Name = M.getKey();
598 
599     std::set<ASTNodeKind> RetKinds;
600     unsigned NumArgs = Matcher.isVariadic() ? 1 : Matcher.getNumArgs();
601     bool IsPolymorphic = Matcher.isPolymorphic();
602     std::vector<std::vector<ArgKind>> ArgsKinds(NumArgs);
603     unsigned MaxSpecificity = 0;
604     for (const ArgKind& Kind : AcceptedTypes) {
605       if (Kind.getArgKind() != Kind.AK_Matcher)
606         continue;
607       unsigned Specificity;
608       ASTNodeKind LeastDerivedKind;
609       if (Matcher.isConvertibleTo(Kind.getMatcherKind(), &Specificity,
610                                   &LeastDerivedKind)) {
611         if (MaxSpecificity < Specificity)
612           MaxSpecificity = Specificity;
613         RetKinds.insert(LeastDerivedKind);
614         for (unsigned Arg = 0; Arg != NumArgs; ++Arg)
615           Matcher.getArgKinds(Kind.getMatcherKind(), Arg, ArgsKinds[Arg]);
616         if (IsPolymorphic)
617           break;
618       }
619     }
620 
621     if (!RetKinds.empty() && MaxSpecificity > 0) {
622       std::string Decl;
623       llvm::raw_string_ostream OS(Decl);
624 
625       if (IsPolymorphic) {
626         OS << "Matcher<T> " << Name << "(Matcher<T>";
627       } else {
628         OS << "Matcher<" << RetKinds << "> " << Name << "(";
629         for (const std::vector<ArgKind> &Arg : ArgsKinds) {
630           if (&Arg != &ArgsKinds[0])
631             OS << ", ";
632 
633           bool FirstArgKind = true;
634           std::set<ASTNodeKind> MatcherKinds;
635           // Two steps. First all non-matchers, then matchers only.
636           for (const ArgKind &AK : Arg) {
637             if (AK.getArgKind() == ArgKind::AK_Matcher) {
638               MatcherKinds.insert(AK.getMatcherKind());
639             } else {
640               if (!FirstArgKind) OS << "|";
641               FirstArgKind = false;
642               OS << AK.asString();
643             }
644           }
645           if (!MatcherKinds.empty()) {
646             if (!FirstArgKind) OS << "|";
647             OS << "Matcher<" << MatcherKinds << ">";
648           }
649         }
650       }
651       if (Matcher.isVariadic())
652         OS << "...";
653       OS << ")";
654 
655       std::string TypedText = Name;
656       TypedText += "(";
657       if (ArgsKinds.empty())
658         TypedText += ")";
659       else if (ArgsKinds[0][0].getArgKind() == ArgKind::AK_String)
660         TypedText += "\"";
661 
662       Completions.emplace_back(TypedText, OS.str(), MaxSpecificity);
663     }
664   }
665 
666   return Completions;
667 }
668 
constructMatcher(MatcherCtor Ctor,SourceRange NameRange,ArrayRef<ParserValue> Args,Diagnostics * Error)669 VariantMatcher Registry::constructMatcher(MatcherCtor Ctor,
670                                           SourceRange NameRange,
671                                           ArrayRef<ParserValue> Args,
672                                           Diagnostics *Error) {
673   return Ctor->create(NameRange, Args, Error);
674 }
675 
constructBoundMatcher(MatcherCtor Ctor,SourceRange NameRange,StringRef BindID,ArrayRef<ParserValue> Args,Diagnostics * Error)676 VariantMatcher Registry::constructBoundMatcher(MatcherCtor Ctor,
677                                                SourceRange NameRange,
678                                                StringRef BindID,
679                                                ArrayRef<ParserValue> Args,
680                                                Diagnostics *Error) {
681   VariantMatcher Out = constructMatcher(Ctor, NameRange, Args, Error);
682   if (Out.isNull()) return Out;
683 
684   llvm::Optional<DynTypedMatcher> Result = Out.getSingleMatcher();
685   if (Result.hasValue()) {
686     llvm::Optional<DynTypedMatcher> Bound = Result->tryBind(BindID);
687     if (Bound.hasValue()) {
688       return VariantMatcher::SingleMatcher(*Bound);
689     }
690   }
691   Error->addError(NameRange, Error->ET_RegistryNotBindable);
692   return VariantMatcher();
693 }
694 
695 } // namespace dynamic
696 } // namespace ast_matchers
697 } // namespace clang
698