Home | History | Annotate | Download | only in Dynamic
      1 //===--- Registry.cpp - Matcher registry -------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// \brief Registry map populated at static initialization time.
     12 ///
     13 //===------------------------------------------------------------===//
     14 
     15 #include "clang/ASTMatchers/Dynamic/Registry.h"
     16 
     17 #include <utility>
     18 
     19 #include "Marshallers.h"
     20 #include "clang/ASTMatchers/ASTMatchers.h"
     21 #include "llvm/ADT/StringMap.h"
     22 #include "llvm/ADT/StringRef.h"
     23 #include "llvm/Support/ManagedStatic.h"
     24 
     25 namespace clang {
     26 namespace ast_matchers {
     27 namespace dynamic {
     28 namespace {
     29 
     30 using internal::MatcherCreateCallback;
     31 
     32 typedef llvm::StringMap<const MatcherCreateCallback *> ConstructorMap;
     33 class RegistryMaps {
     34 public:
     35   RegistryMaps();
     36   ~RegistryMaps();
     37 
     38   const ConstructorMap &constructors() const { return Constructors; }
     39 
     40 private:
     41   void registerMatcher(StringRef MatcherName, MatcherCreateCallback *Callback);
     42   ConstructorMap Constructors;
     43 };
     44 
     45 void RegistryMaps::registerMatcher(StringRef MatcherName,
     46                                    MatcherCreateCallback *Callback) {
     47   assert(Constructors.find(MatcherName) == Constructors.end());
     48   Constructors[MatcherName] = Callback;
     49 }
     50 
     51 /// \brief MatcherCreateCallback that wraps multiple "overloads" of the same
     52 ///   matcher.
     53 ///
     54 /// It will try every overload and generate appropriate errors for when none or
     55 /// more than one overloads match the arguments.
     56 class OverloadedMatcherCreateCallback : public MatcherCreateCallback {
     57  public:
     58    OverloadedMatcherCreateCallback(ArrayRef<MatcherCreateCallback *> Callbacks)
     59        : Overloads(Callbacks) {}
     60 
     61   virtual ~OverloadedMatcherCreateCallback() {
     62     for (size_t i = 0, e = Overloads.size(); i != e; ++i)
     63       delete Overloads[i];
     64   }
     65 
     66   virtual MatcherList run(const SourceRange &NameRange,
     67                           ArrayRef<ParserValue> Args,
     68                           Diagnostics *Error) const {
     69     std::vector<MatcherList> Constructed;
     70     Diagnostics::OverloadContext Ctx(Error);
     71     for (size_t i = 0, e = Overloads.size(); i != e; ++i) {
     72       MatcherList SubMatcher = Overloads[i]->run(NameRange, Args, Error);
     73       if (!SubMatcher.empty()) {
     74         Constructed.push_back(SubMatcher);
     75       }
     76     }
     77 
     78     if (Constructed.empty()) return MatcherList();  // No overload matched.
     79     // We ignore the errors if any matcher succeeded.
     80     Ctx.revertErrors();
     81     if (Constructed.size() > 1) {
     82       // More than one constructed. It is ambiguous.
     83       Error->addError(NameRange, Error->ET_RegistryAmbiguousOverload);
     84       return MatcherList();
     85     }
     86     return Constructed[0];
     87   }
     88 
     89  private:
     90   std::vector<MatcherCreateCallback*> Overloads;
     91 };
     92 
     93 #define REGISTER_MATCHER(name)                                                 \
     94   registerMatcher(#name, internal::makeMatcherAutoMarshall(                    \
     95                              ::clang::ast_matchers::name, #name));
     96 
     97 #define SPECIFIC_MATCHER_OVERLOAD(name, Id)                                    \
     98   static_cast< ::clang::ast_matchers::name##_Type##Id>(                        \
     99       ::clang::ast_matchers::name)
    100 
    101 #define REGISTER_OVERLOADED_2(name)                                            \
    102   do {                                                                         \
    103     MatcherCreateCallback *Callbacks[] = {                                     \
    104       internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, 0),    \
    105                                         #name),                                \
    106       internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, 1),    \
    107                                         #name)                                 \
    108     };                                                                         \
    109     registerMatcher(#name, new OverloadedMatcherCreateCallback(Callbacks));    \
    110   } while (0)
    111 
    112 /// \brief Class that allows us to bind to the constructor of an
    113 ///   \c ArgumentAdaptingMatcher.
    114 /// This class, together with \c collectAdaptativeMatcherOverloads below, help
    115 /// us detect the Adapter class and create overload functions for the
    116 /// appropriate To/From types.
    117 /// We instantiate the \c createAdatingMatcher function for every type in
    118 /// \c FromTypes. \c ToTypes is handled on the marshaller side by using the
    119 /// \c ReturnTypes typedef in \c ArgumentAdaptingMatcher.
    120 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
    121           typename FromTypes, typename ToTypes>
    122 struct AdaptativeMatcherWrapper {
    123   template <typename FromArg>
    124   static ast_matchers::internal::ArgumentAdaptingMatcher<
    125       ArgumentAdapterT, FromArg, FromTypes, ToTypes>
    126   createAdatingMatcher(
    127       const ast_matchers::internal::Matcher<FromArg> &InnerMatcher) {
    128     return ast_matchers::internal::ArgumentAdaptingMatcher<
    129         ArgumentAdapterT, FromArg, FromTypes, ToTypes>(InnerMatcher);
    130   }
    131 
    132   static void collectOverloads(StringRef Name,
    133                                std::vector<MatcherCreateCallback *> &Out,
    134                                ast_matchers::internal::EmptyTypeList) {}
    135 
    136   template <typename FromTypeList>
    137   static void collectOverloads(StringRef Name,
    138                                std::vector<MatcherCreateCallback *> &Out,
    139                                FromTypeList TypeList) {
    140     Out.push_back(internal::makeMatcherAutoMarshall(
    141         &createAdatingMatcher<typename FromTypeList::head>, Name));
    142     collectOverloads(Name, Out, typename FromTypeList::tail());
    143   }
    144 
    145   static void collectOverloads(StringRef Name,
    146                                std::vector<MatcherCreateCallback *> &Out) {
    147     collectOverloads(Name, Out, FromTypes());
    148   }
    149 };
    150 
    151 template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
    152           typename DummyArg, typename FromTypes, typename ToTypes>
    153 void collectAdaptativeMatcherOverloads(
    154     StringRef Name,
    155     ast_matchers::internal::ArgumentAdaptingMatcher<ArgumentAdapterT, DummyArg,
    156                                                     FromTypes, ToTypes>(
    157         *func)(const ast_matchers::internal::Matcher<DummyArg> &),
    158     std::vector<MatcherCreateCallback *> &Out) {
    159   AdaptativeMatcherWrapper<ArgumentAdapterT, FromTypes,
    160                            ToTypes>::collectOverloads(Name, Out);
    161 }
    162 
    163 #define REGISTER_ADAPTATIVE(name)                                              \
    164   do {                                                                         \
    165     std::vector<MatcherCreateCallback *> Overloads;                            \
    166     collectAdaptativeMatcherOverloads(#name, &name<Decl>, Overloads);          \
    167     registerMatcher(#name, new OverloadedMatcherCreateCallback(Overloads));    \
    168   } while (0)
    169 
    170 /// \brief Generate a registry map with all the known matchers.
    171 RegistryMaps::RegistryMaps() {
    172   // TODO: Here is the list of the missing matchers, grouped by reason.
    173   //
    174   // Need Variant/Parser fixes:
    175   // ofKind
    176   //
    177   // Polymorphic + argument overload:
    178   // unless
    179   // eachOf
    180   // anyOf
    181   // allOf
    182   // findAll
    183   //
    184   // Other:
    185   // loc
    186   // equals
    187   // equalsNode
    188   // hasDeclaration
    189 
    190   REGISTER_OVERLOADED_2(callee);
    191   REGISTER_OVERLOADED_2(hasPrefix);
    192   REGISTER_OVERLOADED_2(hasType);
    193   REGISTER_OVERLOADED_2(isDerivedFrom);
    194   REGISTER_OVERLOADED_2(isSameOrDerivedFrom);
    195   REGISTER_OVERLOADED_2(pointsTo);
    196   REGISTER_OVERLOADED_2(references);
    197   REGISTER_OVERLOADED_2(thisPointerType);
    198 
    199   REGISTER_ADAPTATIVE(forEach);
    200   REGISTER_ADAPTATIVE(forEachDescendant);
    201   REGISTER_ADAPTATIVE(has);
    202   REGISTER_ADAPTATIVE(hasAncestor);
    203   REGISTER_ADAPTATIVE(hasDescendant);
    204   REGISTER_ADAPTATIVE(hasParent);
    205 
    206   REGISTER_MATCHER(accessSpecDecl);
    207   REGISTER_MATCHER(alignOfExpr);
    208   REGISTER_MATCHER(anything);
    209   REGISTER_MATCHER(argumentCountIs);
    210   REGISTER_MATCHER(arraySubscriptExpr);
    211   REGISTER_MATCHER(arrayType);
    212   REGISTER_MATCHER(asString);
    213   REGISTER_MATCHER(asmStmt);
    214   REGISTER_MATCHER(atomicType);
    215   REGISTER_MATCHER(autoType);
    216   REGISTER_MATCHER(binaryOperator);
    217   REGISTER_MATCHER(bindTemporaryExpr);
    218   REGISTER_MATCHER(blockPointerType);
    219   REGISTER_MATCHER(boolLiteral);
    220   REGISTER_MATCHER(breakStmt);
    221   REGISTER_MATCHER(builtinType);
    222   REGISTER_MATCHER(cStyleCastExpr);
    223   REGISTER_MATCHER(callExpr);
    224   REGISTER_MATCHER(castExpr);
    225   REGISTER_MATCHER(catchStmt);
    226   REGISTER_MATCHER(characterLiteral);
    227   REGISTER_MATCHER(classTemplateDecl);
    228   REGISTER_MATCHER(classTemplateSpecializationDecl);
    229   REGISTER_MATCHER(complexType);
    230   REGISTER_MATCHER(compoundLiteralExpr);
    231   REGISTER_MATCHER(compoundStmt);
    232   REGISTER_MATCHER(conditionalOperator);
    233   REGISTER_MATCHER(constCastExpr);
    234   REGISTER_MATCHER(constantArrayType);
    235   REGISTER_MATCHER(constructExpr);
    236   REGISTER_MATCHER(constructorDecl);
    237   REGISTER_MATCHER(containsDeclaration);
    238   REGISTER_MATCHER(continueStmt);
    239   REGISTER_MATCHER(decl);
    240   REGISTER_MATCHER(declCountIs);
    241   REGISTER_MATCHER(declRefExpr);
    242   REGISTER_MATCHER(declStmt);
    243   REGISTER_MATCHER(defaultArgExpr);
    244   REGISTER_MATCHER(deleteExpr);
    245   REGISTER_MATCHER(dependentSizedArrayType);
    246   REGISTER_MATCHER(destructorDecl);
    247   REGISTER_MATCHER(doStmt);
    248   REGISTER_MATCHER(dynamicCastExpr);
    249   REGISTER_MATCHER(elaboratedType);
    250   REGISTER_MATCHER(enumConstantDecl);
    251   REGISTER_MATCHER(enumDecl);
    252   REGISTER_MATCHER(explicitCastExpr);
    253   REGISTER_MATCHER(expr);
    254   REGISTER_MATCHER(fieldDecl);
    255   REGISTER_MATCHER(floatLiteral);
    256   REGISTER_MATCHER(forField);
    257   REGISTER_MATCHER(forRangeStmt);
    258   REGISTER_MATCHER(forStmt);
    259   REGISTER_MATCHER(functionDecl);
    260   REGISTER_MATCHER(functionTemplateDecl);
    261   REGISTER_MATCHER(functionType);
    262   REGISTER_MATCHER(functionalCastExpr);
    263   REGISTER_MATCHER(gotoStmt);
    264   REGISTER_MATCHER(hasAnyArgument);
    265   REGISTER_MATCHER(hasAnyConstructorInitializer);
    266   REGISTER_MATCHER(hasAnyParameter);
    267   REGISTER_MATCHER(hasAnySubstatement);
    268   REGISTER_MATCHER(hasAnyTemplateArgument);
    269   REGISTER_MATCHER(hasAnyUsingShadowDecl);
    270   REGISTER_MATCHER(hasArgument);
    271   REGISTER_MATCHER(hasArgumentOfType);
    272   REGISTER_MATCHER(hasBase);
    273   REGISTER_MATCHER(hasBody);
    274   REGISTER_MATCHER(hasCanonicalType);
    275   REGISTER_MATCHER(hasCondition);
    276   REGISTER_MATCHER(hasConditionVariableStatement);
    277   REGISTER_MATCHER(hasDeclContext);
    278   REGISTER_MATCHER(hasDeducedType);
    279   REGISTER_MATCHER(hasDestinationType);
    280   REGISTER_MATCHER(hasEitherOperand);
    281   REGISTER_MATCHER(hasElementType);
    282   REGISTER_MATCHER(hasFalseExpression);
    283   REGISTER_MATCHER(hasImplicitDestinationType);
    284   REGISTER_MATCHER(hasIncrement);
    285   REGISTER_MATCHER(hasIndex);
    286   REGISTER_MATCHER(hasInitializer);
    287   REGISTER_MATCHER(hasLHS);
    288   REGISTER_MATCHER(hasLocalQualifiers);
    289   REGISTER_MATCHER(hasLoopInit);
    290   REGISTER_MATCHER(hasMethod);
    291   REGISTER_MATCHER(hasName);
    292   REGISTER_MATCHER(hasObjectExpression);
    293   REGISTER_MATCHER(hasOperatorName);
    294   REGISTER_MATCHER(hasOverloadedOperatorName);
    295   REGISTER_MATCHER(hasParameter);
    296   REGISTER_MATCHER(hasQualifier);
    297   REGISTER_MATCHER(hasRHS);
    298   REGISTER_MATCHER(hasSingleDecl);
    299   REGISTER_MATCHER(hasSize);
    300   REGISTER_MATCHER(hasSizeExpr);
    301   REGISTER_MATCHER(hasSourceExpression);
    302   REGISTER_MATCHER(hasTargetDecl);
    303   REGISTER_MATCHER(hasTemplateArgument);
    304   REGISTER_MATCHER(hasTrueExpression);
    305   REGISTER_MATCHER(hasUnaryOperand);
    306   REGISTER_MATCHER(hasValueType);
    307   REGISTER_MATCHER(ifStmt);
    308   REGISTER_MATCHER(ignoringImpCasts);
    309   REGISTER_MATCHER(ignoringParenCasts);
    310   REGISTER_MATCHER(ignoringParenImpCasts);
    311   REGISTER_MATCHER(implicitCastExpr);
    312   REGISTER_MATCHER(incompleteArrayType);
    313   REGISTER_MATCHER(initListExpr);
    314   REGISTER_MATCHER(innerType);
    315   REGISTER_MATCHER(integerLiteral);
    316   REGISTER_MATCHER(isArrow);
    317   REGISTER_MATCHER(isConstQualified);
    318   REGISTER_MATCHER(isDefinition);
    319   REGISTER_MATCHER(isExplicitTemplateSpecialization);
    320   REGISTER_MATCHER(isExternC);
    321   REGISTER_MATCHER(isImplicit);
    322   REGISTER_MATCHER(isInteger);
    323   REGISTER_MATCHER(isOverride);
    324   REGISTER_MATCHER(isPrivate);
    325   REGISTER_MATCHER(isProtected);
    326   REGISTER_MATCHER(isPublic);
    327   REGISTER_MATCHER(isTemplateInstantiation);
    328   REGISTER_MATCHER(isVirtual);
    329   REGISTER_MATCHER(isWritten);
    330   REGISTER_MATCHER(lValueReferenceType);
    331   REGISTER_MATCHER(labelStmt);
    332   REGISTER_MATCHER(lambdaExpr);
    333   REGISTER_MATCHER(matchesName);
    334   REGISTER_MATCHER(materializeTemporaryExpr);
    335   REGISTER_MATCHER(member);
    336   REGISTER_MATCHER(memberCallExpr);
    337   REGISTER_MATCHER(memberExpr);
    338   REGISTER_MATCHER(memberPointerType);
    339   REGISTER_MATCHER(methodDecl);
    340   REGISTER_MATCHER(namedDecl);
    341   REGISTER_MATCHER(namesType);
    342   REGISTER_MATCHER(namespaceDecl);
    343   REGISTER_MATCHER(nestedNameSpecifier);
    344   REGISTER_MATCHER(nestedNameSpecifierLoc);
    345   REGISTER_MATCHER(newExpr);
    346   REGISTER_MATCHER(nullPtrLiteralExpr);
    347   REGISTER_MATCHER(nullStmt);
    348   REGISTER_MATCHER(ofClass);
    349   REGISTER_MATCHER(on);
    350   REGISTER_MATCHER(onImplicitObjectArgument);
    351   REGISTER_MATCHER(operatorCallExpr);
    352   REGISTER_MATCHER(parameterCountIs);
    353   REGISTER_MATCHER(parenType);
    354   REGISTER_MATCHER(pointee);
    355   REGISTER_MATCHER(pointerType);
    356   REGISTER_MATCHER(qualType);
    357   REGISTER_MATCHER(rValueReferenceType);
    358   REGISTER_MATCHER(recordDecl);
    359   REGISTER_MATCHER(recordType);
    360   REGISTER_MATCHER(referenceType);
    361   REGISTER_MATCHER(refersToDeclaration);
    362   REGISTER_MATCHER(refersToType);
    363   REGISTER_MATCHER(reinterpretCastExpr);
    364   REGISTER_MATCHER(returnStmt);
    365   REGISTER_MATCHER(returns);
    366   REGISTER_MATCHER(sizeOfExpr);
    367   REGISTER_MATCHER(specifiesNamespace);
    368   REGISTER_MATCHER(specifiesType);
    369   REGISTER_MATCHER(specifiesTypeLoc);
    370   REGISTER_MATCHER(statementCountIs);
    371   REGISTER_MATCHER(staticCastExpr);
    372   REGISTER_MATCHER(stmt);
    373   REGISTER_MATCHER(stringLiteral);
    374   REGISTER_MATCHER(switchCase);
    375   REGISTER_MATCHER(switchStmt);
    376   REGISTER_MATCHER(templateSpecializationType);
    377   REGISTER_MATCHER(thisExpr);
    378   REGISTER_MATCHER(throughUsingDecl);
    379   REGISTER_MATCHER(throwExpr);
    380   REGISTER_MATCHER(to);
    381   REGISTER_MATCHER(tryStmt);
    382   REGISTER_MATCHER(type);
    383   REGISTER_MATCHER(typeLoc);
    384   REGISTER_MATCHER(typedefType);
    385   REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
    386   REGISTER_MATCHER(unaryOperator);
    387   REGISTER_MATCHER(userDefinedLiteral);
    388   REGISTER_MATCHER(usingDecl);
    389   REGISTER_MATCHER(varDecl);
    390   REGISTER_MATCHER(variableArrayType);
    391   REGISTER_MATCHER(whileStmt);
    392   REGISTER_MATCHER(withInitializer);
    393 }
    394 
    395 RegistryMaps::~RegistryMaps() {
    396   for (ConstructorMap::iterator it = Constructors.begin(),
    397                                 end = Constructors.end();
    398        it != end; ++it) {
    399     delete it->second;
    400   }
    401 }
    402 
    403 static llvm::ManagedStatic<RegistryMaps> RegistryData;
    404 
    405 } // anonymous namespace
    406 
    407 // static
    408 MatcherList Registry::constructMatcher(StringRef MatcherName,
    409                                        const SourceRange &NameRange,
    410                                        ArrayRef<ParserValue> Args,
    411                                        Diagnostics *Error) {
    412   ConstructorMap::const_iterator it =
    413       RegistryData->constructors().find(MatcherName);
    414   if (it == RegistryData->constructors().end()) {
    415     Error->addError(NameRange, Error->ET_RegistryNotFound) << MatcherName;
    416     return MatcherList();
    417   }
    418 
    419   return it->second->run(NameRange, Args, Error);
    420 }
    421 
    422 // static
    423 MatcherList Registry::constructBoundMatcher(StringRef MatcherName,
    424                                             const SourceRange &NameRange,
    425                                             StringRef BindID,
    426                                             ArrayRef<ParserValue> Args,
    427                                             Diagnostics *Error) {
    428   MatcherList Out = constructMatcher(MatcherName, NameRange, Args, Error);
    429   if (Out.empty()) return Out;
    430 
    431   ArrayRef<const DynTypedMatcher*> Matchers = Out.matchers();
    432   if (Matchers.size() == 1) {
    433     OwningPtr<DynTypedMatcher> Bound(Matchers[0]->tryBind(BindID));
    434     if (Bound) {
    435       return *Bound;
    436     }
    437   }
    438   Error->addError(NameRange, Error->ET_RegistryNotBindable);
    439   return MatcherList();
    440 }
    441 
    442 }  // namespace dynamic
    443 }  // namespace ast_matchers
    444 }  // namespace clang
    445