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