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