1 //===--- Registry.cpp - Matcher registry -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Registry map populated at static initialization time.
12 ///
13 //===------------------------------------------------------------===//
14 
15 #include "clang/ASTMatchers/Dynamic/Registry.h"
16 #include "Marshallers.h"
17 #include "clang/ASTMatchers/ASTMatchers.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include <set>
23 #include <utility>
24 
25 using namespace clang::ast_type_traits;
26 
27 namespace clang {
28 namespace ast_matchers {
29 namespace dynamic {
30 namespace {
31 
32 using internal::MatcherDescriptor;
33 
34 typedef llvm::StringMap<const MatcherDescriptor *> ConstructorMap;
35 class RegistryMaps {
36 public:
37   RegistryMaps();
38   ~RegistryMaps();
39 
constructors() const40   const ConstructorMap &constructors() const { return Constructors; }
41 
42 private:
43   void registerMatcher(StringRef MatcherName, MatcherDescriptor *Callback);
44   ConstructorMap Constructors;
45 };
46 
registerMatcher(StringRef MatcherName,MatcherDescriptor * Callback)47 void RegistryMaps::registerMatcher(StringRef MatcherName,
48                                    MatcherDescriptor *Callback) {
49   assert(Constructors.find(MatcherName) == Constructors.end());
50   Constructors[MatcherName] = Callback;
51 }
52 
53 #define REGISTER_MATCHER(name)                                                 \
54   registerMatcher(#name, internal::makeMatcherAutoMarshall(                    \
55                              ::clang::ast_matchers::name, #name));
56 
57 #define SPECIFIC_MATCHER_OVERLOAD(name, Id)                                    \
58   static_cast< ::clang::ast_matchers::name##_Type##Id>(                        \
59       ::clang::ast_matchers::name)
60 
61 #define REGISTER_OVERLOADED_2(name)                                            \
62   do {                                                                         \
63     MatcherDescriptor *Callbacks[] = {                                         \
64       internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, 0),    \
65                                         #name),                                \
66       internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, 1),    \
67                                         #name)                                 \
68     };                                                                         \
69     registerMatcher(#name,                                                     \
70                     new internal::OverloadedMatcherDescriptor(Callbacks));     \
71   } while (0)
72 
73 /// \brief Generate a registry map with all the known matchers.
RegistryMaps()74 RegistryMaps::RegistryMaps() {
75   // TODO: Here is the list of the missing matchers, grouped by reason.
76   //
77   // Need Variant/Parser fixes:
78   // ofKind
79   //
80   // Polymorphic + argument overload:
81   // findAll
82   //
83   // Other:
84   // equals
85   // equalsNode
86 
87   REGISTER_OVERLOADED_2(callee);
88   REGISTER_OVERLOADED_2(hasPrefix);
89   REGISTER_OVERLOADED_2(hasType);
90   REGISTER_OVERLOADED_2(isDerivedFrom);
91   REGISTER_OVERLOADED_2(isSameOrDerivedFrom);
92   REGISTER_OVERLOADED_2(loc);
93   REGISTER_OVERLOADED_2(pointsTo);
94   REGISTER_OVERLOADED_2(references);
95   REGISTER_OVERLOADED_2(thisPointerType);
96 
97   REGISTER_MATCHER(accessSpecDecl);
98   REGISTER_MATCHER(alignOfExpr);
99   REGISTER_MATCHER(allOf);
100   REGISTER_MATCHER(anyOf);
101   REGISTER_MATCHER(anything);
102   REGISTER_MATCHER(argumentCountIs);
103   REGISTER_MATCHER(arraySubscriptExpr);
104   REGISTER_MATCHER(arrayType);
105   REGISTER_MATCHER(asmStmt);
106   REGISTER_MATCHER(asString);
107   REGISTER_MATCHER(atomicType);
108   REGISTER_MATCHER(autoType);
109   REGISTER_MATCHER(binaryOperator);
110   REGISTER_MATCHER(blockPointerType);
111   REGISTER_MATCHER(breakStmt);
112   REGISTER_MATCHER(builtinType);
113   REGISTER_MATCHER(callExpr);
114   REGISTER_MATCHER(caseStmt);
115   REGISTER_MATCHER(castExpr);
116   REGISTER_MATCHER(characterLiteral);
117   REGISTER_MATCHER(classTemplateDecl);
118   REGISTER_MATCHER(classTemplateSpecializationDecl);
119   REGISTER_MATCHER(complexType);
120   REGISTER_MATCHER(compoundLiteralExpr);
121   REGISTER_MATCHER(compoundStmt);
122   REGISTER_MATCHER(conditionalOperator);
123   REGISTER_MATCHER(constantArrayType);
124   REGISTER_MATCHER(containsDeclaration);
125   REGISTER_MATCHER(continueStmt);
126   REGISTER_MATCHER(cStyleCastExpr);
127   REGISTER_MATCHER(cudaKernelCallExpr);
128   REGISTER_MATCHER(cxxBindTemporaryExpr);
129   REGISTER_MATCHER(cxxBoolLiteral);
130   REGISTER_MATCHER(cxxCatchStmt);
131   REGISTER_MATCHER(cxxConstCastExpr);
132   REGISTER_MATCHER(cxxConstructExpr);
133   REGISTER_MATCHER(cxxConstructorDecl);
134   REGISTER_MATCHER(cxxConversionDecl);
135   REGISTER_MATCHER(cxxCtorInitializer);
136   REGISTER_MATCHER(cxxDefaultArgExpr);
137   REGISTER_MATCHER(cxxDeleteExpr);
138   REGISTER_MATCHER(cxxDestructorDecl);
139   REGISTER_MATCHER(cxxDynamicCastExpr);
140   REGISTER_MATCHER(cxxForRangeStmt);
141   REGISTER_MATCHER(cxxFunctionalCastExpr);
142   REGISTER_MATCHER(cxxMemberCallExpr);
143   REGISTER_MATCHER(cxxMethodDecl);
144   REGISTER_MATCHER(cxxNewExpr);
145   REGISTER_MATCHER(cxxNullPtrLiteralExpr);
146   REGISTER_MATCHER(cxxOperatorCallExpr);
147   REGISTER_MATCHER(cxxRecordDecl);
148   REGISTER_MATCHER(cxxReinterpretCastExpr);
149   REGISTER_MATCHER(cxxStaticCastExpr);
150   REGISTER_MATCHER(cxxTemporaryObjectExpr);
151   REGISTER_MATCHER(cxxThisExpr);
152   REGISTER_MATCHER(cxxThrowExpr);
153   REGISTER_MATCHER(cxxTryStmt);
154   REGISTER_MATCHER(cxxUnresolvedConstructExpr);
155   REGISTER_MATCHER(decayedType);
156   REGISTER_MATCHER(decl);
157   REGISTER_MATCHER(declaratorDecl);
158   REGISTER_MATCHER(declCountIs);
159   REGISTER_MATCHER(declRefExpr);
160   REGISTER_MATCHER(declStmt);
161   REGISTER_MATCHER(defaultStmt);
162   REGISTER_MATCHER(dependentSizedArrayType);
163   REGISTER_MATCHER(doStmt);
164   REGISTER_MATCHER(eachOf);
165   REGISTER_MATCHER(elaboratedType);
166   REGISTER_MATCHER(enumConstantDecl);
167   REGISTER_MATCHER(enumDecl);
168   REGISTER_MATCHER(equalsBoundNode);
169   REGISTER_MATCHER(equalsIntegralValue);
170   REGISTER_MATCHER(explicitCastExpr);
171   REGISTER_MATCHER(expr);
172   REGISTER_MATCHER(exprWithCleanups);
173   REGISTER_MATCHER(fieldDecl);
174   REGISTER_MATCHER(floatLiteral);
175   REGISTER_MATCHER(forEach);
176   REGISTER_MATCHER(forEachConstructorInitializer);
177   REGISTER_MATCHER(forEachDescendant);
178   REGISTER_MATCHER(forEachSwitchCase);
179   REGISTER_MATCHER(forField);
180   REGISTER_MATCHER(forStmt);
181   REGISTER_MATCHER(friendDecl);
182   REGISTER_MATCHER(functionDecl);
183   REGISTER_MATCHER(functionTemplateDecl);
184   REGISTER_MATCHER(functionType);
185   REGISTER_MATCHER(gotoStmt);
186   REGISTER_MATCHER(has);
187   REGISTER_MATCHER(hasAncestor);
188   REGISTER_MATCHER(hasAnyArgument);
189   REGISTER_MATCHER(hasAnyConstructorInitializer);
190   REGISTER_MATCHER(hasAnyParameter);
191   REGISTER_MATCHER(hasAnySubstatement);
192   REGISTER_MATCHER(hasAnyTemplateArgument);
193   REGISTER_MATCHER(hasAnyUsingShadowDecl);
194   REGISTER_MATCHER(hasArgument);
195   REGISTER_MATCHER(hasArgumentOfType);
196   REGISTER_MATCHER(hasAttr);
197   REGISTER_MATCHER(hasAutomaticStorageDuration);
198   REGISTER_MATCHER(hasBase);
199   REGISTER_MATCHER(hasBody);
200   REGISTER_MATCHER(hasCanonicalType);
201   REGISTER_MATCHER(hasCaseConstant);
202   REGISTER_MATCHER(hasCondition);
203   REGISTER_MATCHER(hasConditionVariableStatement);
204   REGISTER_MATCHER(hasDecayedType);
205   REGISTER_MATCHER(hasDeclaration);
206   REGISTER_MATCHER(hasDeclContext);
207   REGISTER_MATCHER(hasDeducedType);
208   REGISTER_MATCHER(hasDescendant);
209   REGISTER_MATCHER(hasDestinationType);
210   REGISTER_MATCHER(hasEitherOperand);
211   REGISTER_MATCHER(hasElementType);
212   REGISTER_MATCHER(hasElse);
213   REGISTER_MATCHER(hasFalseExpression);
214   REGISTER_MATCHER(hasGlobalStorage);
215   REGISTER_MATCHER(hasImplicitDestinationType);
216   REGISTER_MATCHER(hasIncrement);
217   REGISTER_MATCHER(hasIndex);
218   REGISTER_MATCHER(hasInitializer);
219   REGISTER_MATCHER(hasKeywordSelector);
220   REGISTER_MATCHER(hasLHS);
221   REGISTER_MATCHER(hasLocalQualifiers);
222   REGISTER_MATCHER(hasLocalStorage);
223   REGISTER_MATCHER(hasLoopInit);
224   REGISTER_MATCHER(hasLoopVariable);
225   REGISTER_MATCHER(hasMethod);
226   REGISTER_MATCHER(hasName);
227   REGISTER_MATCHER(hasNullSelector);
228   REGISTER_MATCHER(hasObjectExpression);
229   REGISTER_MATCHER(hasOperatorName);
230   REGISTER_MATCHER(hasOverloadedOperatorName);
231   REGISTER_MATCHER(hasParameter);
232   REGISTER_MATCHER(hasParent);
233   REGISTER_MATCHER(hasQualifier);
234   REGISTER_MATCHER(hasRangeInit);
235   REGISTER_MATCHER(hasReceiverType);
236   REGISTER_MATCHER(hasRHS);
237   REGISTER_MATCHER(hasSelector);
238   REGISTER_MATCHER(hasSingleDecl);
239   REGISTER_MATCHER(hasSize);
240   REGISTER_MATCHER(hasSizeExpr);
241   REGISTER_MATCHER(hasSourceExpression);
242   REGISTER_MATCHER(hasStaticStorageDuration);
243   REGISTER_MATCHER(hasTargetDecl);
244   REGISTER_MATCHER(hasTemplateArgument);
245   REGISTER_MATCHER(hasThen);
246   REGISTER_MATCHER(hasThreadStorageDuration);
247   REGISTER_MATCHER(hasTrueExpression);
248   REGISTER_MATCHER(hasTypeLoc);
249   REGISTER_MATCHER(hasUnaryOperand);
250   REGISTER_MATCHER(hasUnarySelector);
251   REGISTER_MATCHER(hasValueType);
252   REGISTER_MATCHER(ifStmt);
253   REGISTER_MATCHER(ignoringImpCasts);
254   REGISTER_MATCHER(ignoringParenCasts);
255   REGISTER_MATCHER(ignoringParenImpCasts);
256   REGISTER_MATCHER(implicitCastExpr);
257   REGISTER_MATCHER(incompleteArrayType);
258   REGISTER_MATCHER(initListExpr);
259   REGISTER_MATCHER(injectedClassNameType);
260   REGISTER_MATCHER(innerType);
261   REGISTER_MATCHER(integerLiteral);
262   REGISTER_MATCHER(isAnonymous);
263   REGISTER_MATCHER(isArrow);
264   REGISTER_MATCHER(isBaseInitializer);
265   REGISTER_MATCHER(isCatchAll);
266   REGISTER_MATCHER(isClass);
267   REGISTER_MATCHER(isConst);
268   REGISTER_MATCHER(isConstQualified);
269   REGISTER_MATCHER(isCopyConstructor);
270   REGISTER_MATCHER(isDefaultConstructor);
271   REGISTER_MATCHER(isDefinition);
272   REGISTER_MATCHER(isDeleted);
273   REGISTER_MATCHER(isExceptionVariable);
274   REGISTER_MATCHER(isExplicit);
275   REGISTER_MATCHER(isExplicitTemplateSpecialization);
276   REGISTER_MATCHER(isExpr);
277   REGISTER_MATCHER(isExternC);
278   REGISTER_MATCHER(isFinal);
279   REGISTER_MATCHER(isInline);
280   REGISTER_MATCHER(isImplicit);
281   REGISTER_MATCHER(isExpansionInFileMatching);
282   REGISTER_MATCHER(isExpansionInMainFile);
283   REGISTER_MATCHER(isInstantiated);
284   REGISTER_MATCHER(isExpansionInSystemHeader);
285   REGISTER_MATCHER(isInteger);
286   REGISTER_MATCHER(isIntegral);
287   REGISTER_MATCHER(isInTemplateInstantiation);
288   REGISTER_MATCHER(isListInitialization);
289   REGISTER_MATCHER(isMemberInitializer);
290   REGISTER_MATCHER(isMoveConstructor);
291   REGISTER_MATCHER(isNoThrow);
292   REGISTER_MATCHER(isOverride);
293   REGISTER_MATCHER(isPrivate);
294   REGISTER_MATCHER(isProtected);
295   REGISTER_MATCHER(isPublic);
296   REGISTER_MATCHER(isPure);
297   REGISTER_MATCHER(isStruct);
298   REGISTER_MATCHER(isTemplateInstantiation);
299   REGISTER_MATCHER(isUnion);
300   REGISTER_MATCHER(isVariadic);
301   REGISTER_MATCHER(isVirtual);
302   REGISTER_MATCHER(isVolatileQualified);
303   REGISTER_MATCHER(isWritten);
304   REGISTER_MATCHER(labelStmt);
305   REGISTER_MATCHER(lambdaExpr);
306   REGISTER_MATCHER(lValueReferenceType);
307   REGISTER_MATCHER(matchesName);
308   REGISTER_MATCHER(matchesSelector);
309   REGISTER_MATCHER(materializeTemporaryExpr);
310   REGISTER_MATCHER(member);
311   REGISTER_MATCHER(memberExpr);
312   REGISTER_MATCHER(memberPointerType);
313   REGISTER_MATCHER(namedDecl);
314   REGISTER_MATCHER(namespaceAliasDecl);
315   REGISTER_MATCHER(namespaceDecl);
316   REGISTER_MATCHER(namesType);
317   REGISTER_MATCHER(nestedNameSpecifier);
318   REGISTER_MATCHER(nestedNameSpecifierLoc);
319   REGISTER_MATCHER(nullStmt);
320   REGISTER_MATCHER(numSelectorArgs);
321   REGISTER_MATCHER(ofClass);
322   REGISTER_MATCHER(objcInterfaceDecl);
323   REGISTER_MATCHER(objcMessageExpr);
324   REGISTER_MATCHER(objcObjectPointerType);
325   REGISTER_MATCHER(on);
326   REGISTER_MATCHER(onImplicitObjectArgument);
327   REGISTER_MATCHER(parameterCountIs);
328   REGISTER_MATCHER(parenType);
329   REGISTER_MATCHER(parmVarDecl);
330   REGISTER_MATCHER(pointee);
331   REGISTER_MATCHER(pointerType);
332   REGISTER_MATCHER(qualType);
333   REGISTER_MATCHER(recordDecl);
334   REGISTER_MATCHER(recordType);
335   REGISTER_MATCHER(referenceType);
336   REGISTER_MATCHER(refersToDeclaration);
337   REGISTER_MATCHER(refersToIntegralType);
338   REGISTER_MATCHER(refersToType);
339   REGISTER_MATCHER(returns);
340   REGISTER_MATCHER(returnStmt);
341   REGISTER_MATCHER(rValueReferenceType);
342   REGISTER_MATCHER(sizeOfExpr);
343   REGISTER_MATCHER(specifiesNamespace);
344   REGISTER_MATCHER(specifiesType);
345   REGISTER_MATCHER(specifiesTypeLoc);
346   REGISTER_MATCHER(statementCountIs);
347   REGISTER_MATCHER(staticAssertDecl);
348   REGISTER_MATCHER(stmt);
349   REGISTER_MATCHER(stringLiteral);
350   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
351   REGISTER_MATCHER(substTemplateTypeParmType);
352   REGISTER_MATCHER(switchCase);
353   REGISTER_MATCHER(switchStmt);
354   REGISTER_MATCHER(templateArgument);
355   REGISTER_MATCHER(templateArgumentCountIs);
356   REGISTER_MATCHER(templateSpecializationType);
357   REGISTER_MATCHER(templateTypeParmType);
358   REGISTER_MATCHER(throughUsingDecl);
359   REGISTER_MATCHER(to);
360   REGISTER_MATCHER(translationUnitDecl);
361   REGISTER_MATCHER(type);
362   REGISTER_MATCHER(typedefDecl);
363   REGISTER_MATCHER(typedefType);
364   REGISTER_MATCHER(typeLoc);
365   REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
366   REGISTER_MATCHER(unaryOperator);
367   REGISTER_MATCHER(unaryTransformType);
368   REGISTER_MATCHER(unless);
369   REGISTER_MATCHER(unresolvedUsingTypenameDecl);
370   REGISTER_MATCHER(unresolvedUsingValueDecl);
371   REGISTER_MATCHER(userDefinedLiteral);
372   REGISTER_MATCHER(usingDecl);
373   REGISTER_MATCHER(usingDirectiveDecl);
374   REGISTER_MATCHER(valueDecl);
375   REGISTER_MATCHER(varDecl);
376   REGISTER_MATCHER(variableArrayType);
377   REGISTER_MATCHER(voidType);
378   REGISTER_MATCHER(whileStmt);
379   REGISTER_MATCHER(withInitializer);
380 }
381 
~RegistryMaps()382 RegistryMaps::~RegistryMaps() {
383   llvm::DeleteContainerSeconds(Constructors);
384 }
385 
386 static llvm::ManagedStatic<RegistryMaps> RegistryData;
387 
388 } // anonymous namespace
389 
390 // static
lookupMatcherCtor(StringRef MatcherName)391 llvm::Optional<MatcherCtor> Registry::lookupMatcherCtor(StringRef MatcherName) {
392   ConstructorMap::const_iterator it =
393       RegistryData->constructors().find(MatcherName);
394   return it == RegistryData->constructors().end()
395              ? llvm::Optional<MatcherCtor>()
396              : it->second;
397 }
398 
399 namespace {
400 
operator <<(llvm::raw_ostream & OS,const std::set<ASTNodeKind> & KS)401 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
402                               const std::set<ASTNodeKind> &KS) {
403   unsigned Count = 0;
404   for (std::set<ASTNodeKind>::const_iterator I = KS.begin(), E = KS.end();
405        I != E; ++I) {
406     if (I != KS.begin())
407       OS << "|";
408     if (Count++ == 3) {
409       OS << "...";
410       break;
411     }
412     OS << *I;
413   }
414   return OS;
415 }
416 
417 }  // namespace
418 
getAcceptedCompletionTypes(ArrayRef<std::pair<MatcherCtor,unsigned>> Context)419 std::vector<ArgKind> Registry::getAcceptedCompletionTypes(
420     ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
421   ASTNodeKind InitialTypes[] = {
422       ASTNodeKind::getFromNodeKind<Decl>(),
423       ASTNodeKind::getFromNodeKind<QualType>(),
424       ASTNodeKind::getFromNodeKind<Type>(),
425       ASTNodeKind::getFromNodeKind<Stmt>(),
426       ASTNodeKind::getFromNodeKind<NestedNameSpecifier>(),
427       ASTNodeKind::getFromNodeKind<NestedNameSpecifierLoc>(),
428       ASTNodeKind::getFromNodeKind<TypeLoc>()};
429 
430   // Starting with the above seed of acceptable top-level matcher types, compute
431   // the acceptable type set for the argument indicated by each context element.
432   std::set<ArgKind> TypeSet(std::begin(InitialTypes), std::end(InitialTypes));
433   for (const auto &CtxEntry : Context) {
434     MatcherCtor Ctor = CtxEntry.first;
435     unsigned ArgNumber = CtxEntry.second;
436     std::vector<ArgKind> NextTypeSet;
437     for (const ArgKind &Kind : TypeSet) {
438       if (Kind.getArgKind() == Kind.AK_Matcher &&
439           Ctor->isConvertibleTo(Kind.getMatcherKind()) &&
440           (Ctor->isVariadic() || ArgNumber < Ctor->getNumArgs()))
441         Ctor->getArgKinds(Kind.getMatcherKind(), ArgNumber, NextTypeSet);
442     }
443     TypeSet.clear();
444     TypeSet.insert(NextTypeSet.begin(), NextTypeSet.end());
445   }
446   return std::vector<ArgKind>(TypeSet.begin(), TypeSet.end());
447 }
448 
449 std::vector<MatcherCompletion>
getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes)450 Registry::getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes) {
451   std::vector<MatcherCompletion> Completions;
452 
453   // Search the registry for acceptable matchers.
454   for (const auto &M : RegistryData->constructors()) {
455     const auto *Matcher = M.getValue();
456     StringRef Name = M.getKey();
457 
458     std::set<ASTNodeKind> RetKinds;
459     unsigned NumArgs = Matcher->isVariadic() ? 1 : Matcher->getNumArgs();
460     bool IsPolymorphic = Matcher->isPolymorphic();
461     std::vector<std::vector<ArgKind>> ArgsKinds(NumArgs);
462     unsigned MaxSpecificity = 0;
463     for (const ArgKind& Kind : AcceptedTypes) {
464       if (Kind.getArgKind() != Kind.AK_Matcher)
465         continue;
466       unsigned Specificity;
467       ASTNodeKind LeastDerivedKind;
468       if (Matcher->isConvertibleTo(Kind.getMatcherKind(), &Specificity,
469                                    &LeastDerivedKind)) {
470         if (MaxSpecificity < Specificity)
471           MaxSpecificity = Specificity;
472         RetKinds.insert(LeastDerivedKind);
473         for (unsigned Arg = 0; Arg != NumArgs; ++Arg)
474           Matcher->getArgKinds(Kind.getMatcherKind(), Arg, ArgsKinds[Arg]);
475         if (IsPolymorphic)
476           break;
477       }
478     }
479 
480     if (!RetKinds.empty() && MaxSpecificity > 0) {
481       std::string Decl;
482       llvm::raw_string_ostream OS(Decl);
483 
484       if (IsPolymorphic) {
485         OS << "Matcher<T> " << Name << "(Matcher<T>";
486       } else {
487         OS << "Matcher<" << RetKinds << "> " << Name << "(";
488         for (const std::vector<ArgKind> &Arg : ArgsKinds) {
489           if (&Arg != &ArgsKinds[0])
490             OS << ", ";
491 
492           bool FirstArgKind = true;
493           std::set<ASTNodeKind> MatcherKinds;
494           // Two steps. First all non-matchers, then matchers only.
495           for (const ArgKind &AK : Arg) {
496             if (AK.getArgKind() == ArgKind::AK_Matcher) {
497               MatcherKinds.insert(AK.getMatcherKind());
498             } else {
499               if (!FirstArgKind) OS << "|";
500               FirstArgKind = false;
501               OS << AK.asString();
502             }
503           }
504           if (!MatcherKinds.empty()) {
505             if (!FirstArgKind) OS << "|";
506             OS << "Matcher<" << MatcherKinds << ">";
507           }
508         }
509       }
510       if (Matcher->isVariadic())
511         OS << "...";
512       OS << ")";
513 
514       std::string TypedText = Name;
515       TypedText += "(";
516       if (ArgsKinds.empty())
517         TypedText += ")";
518       else if (ArgsKinds[0][0].getArgKind() == ArgKind::AK_String)
519         TypedText += "\"";
520 
521       Completions.emplace_back(TypedText, OS.str(), MaxSpecificity);
522     }
523   }
524 
525   return Completions;
526 }
527 
528 // static
constructMatcher(MatcherCtor Ctor,SourceRange NameRange,ArrayRef<ParserValue> Args,Diagnostics * Error)529 VariantMatcher Registry::constructMatcher(MatcherCtor Ctor,
530                                           SourceRange NameRange,
531                                           ArrayRef<ParserValue> Args,
532                                           Diagnostics *Error) {
533   return Ctor->create(NameRange, Args, Error);
534 }
535 
536 // static
constructBoundMatcher(MatcherCtor Ctor,SourceRange NameRange,StringRef BindID,ArrayRef<ParserValue> Args,Diagnostics * Error)537 VariantMatcher Registry::constructBoundMatcher(MatcherCtor Ctor,
538                                                SourceRange NameRange,
539                                                StringRef BindID,
540                                                ArrayRef<ParserValue> Args,
541                                                Diagnostics *Error) {
542   VariantMatcher Out = constructMatcher(Ctor, NameRange, Args, Error);
543   if (Out.isNull()) return Out;
544 
545   llvm::Optional<DynTypedMatcher> Result = Out.getSingleMatcher();
546   if (Result.hasValue()) {
547     llvm::Optional<DynTypedMatcher> Bound = Result->tryBind(BindID);
548     if (Bound.hasValue()) {
549       return VariantMatcher::SingleMatcher(*Bound);
550     }
551   }
552   Error->addError(NameRange, Error->ET_RegistryNotBindable);
553   return VariantMatcher();
554 }
555 
556 }  // namespace dynamic
557 }  // namespace ast_matchers
558 }  // namespace clang
559