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