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