Index: clang-tidy/ClangTidyModule.h =================================================================== --- clang-tidy/ClangTidyModule.h +++ clang-tidy/ClangTidyModule.h @@ -11,79 +11,51 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H #include "ClangTidy.h" -#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include +#include +#include #include namespace clang { namespace tidy { -/// \brief A factory, that can instantiate a specific clang-tidy check for -/// processing a translation unit. -/// -/// In order to register your check with the \c ClangTidyModule, create a -/// subclass of \c CheckFactoryBase and implement \c createCheck(). Then, use -/// this subclass in \c ClangTidyModule::addCheckFactories(). -class CheckFactoryBase { -public: - virtual ~CheckFactoryBase() {} - virtual ClangTidyCheck *createCheck() = 0; -}; - -/// \brief A subclass of \c CheckFactoryBase that should be used for all -/// \c ClangTidyChecks that don't require constructor parameters. -/// -/// For example, if have a clang-tidy check like: -/// \code -/// class MyTidyCheck : public ClangTidyCheck { -/// void registerMatchers(ast_matchers::MatchFinder *Finder) override { -/// .. -/// } -/// }; -/// \endcode -/// you can register it with: -/// \code -/// class MyModule : public ClangTidyModule { -/// void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override { -/// CheckFactories.addCheckFactory( -/// "myproject-my-check", new ClangTidyCheckFactory()); -/// } -/// }; -/// \endcode -template class ClangTidyCheckFactory : public CheckFactoryBase { -public: - ClangTidyCheck *createCheck() override { return new T; } -}; - -class ClangTidyCheckFactories; - -/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives -/// them a prefixed name. -class ClangTidyModule { -public: - virtual ~ClangTidyModule() {} - - /// \brief Implement this function in order to register all \c CheckFactories - /// belonging to this module. - virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0; -}; - /// \brief A collection of \c ClangTidyCheckFactory instances. /// /// All clang-tidy modules register their check factories with an instance of /// this object. class ClangTidyCheckFactories { public: - /// \brief Register \p Factory with the name \p Name. - void addCheckFactory(StringRef Name, - std::unique_ptr Factory); + /// \brief Registers check \p Factory with name \p Name. + /// + /// For all checks that have default constructors, use \c registerCheck. + void registerCheckFactory(StringRef Name, + std::function Factory); - /// \brief Registers the \c CheckType with the name \p Name by adding a - /// corresponding \c ClangTidyCheckFactory. + /// \brief Registers the \c CheckType with the name \p Name. + /// + /// This method should be used for all \c ClangTidyChecks that don't require + /// constructor parameters. + /// + /// For example, if have a clang-tidy check like: + /// \code + /// class MyTidyCheck : public ClangTidyCheck { + /// void registerMatchers(ast_matchers::MatchFinder *Finder) override { + /// .. + /// } + /// }; + /// \endcode + /// you can register it with: + /// \code + /// class MyModule : public ClangTidyModule { + /// void addCheckFactories(ClangTidyCheckFactories &Factories) override { + /// Factories.registerCheck("myproject-my-check"); + /// } + /// }; + /// \endcode template void registerCheck(StringRef Name) { - addCheckFactory(Name, - llvm::make_unique>()); + registerCheckFactory(Name, []() { return new CheckType(); }); } /// \brief Create instances of all checks matching \p CheckRegexString and @@ -93,7 +65,7 @@ void createChecks(GlobList &Filter, std::vector> &Checks); - typedef std::map> FactoryMap; + typedef std::map> FactoryMap; FactoryMap::const_iterator begin() const { return Factories.begin(); } FactoryMap::const_iterator end() const { return Factories.end(); } bool empty() const { return Factories.empty(); } @@ -102,6 +74,17 @@ FactoryMap Factories; }; +/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives +/// them a prefixed name. +class ClangTidyModule { +public: + virtual ~ClangTidyModule() {} + + /// \brief Implement this function in order to register all \c CheckFactories + /// belonging to this module. + virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0; +}; + } // end namespace tidy } // end namespace clang Index: clang-tidy/ClangTidyModule.cpp =================================================================== --- clang-tidy/ClangTidyModule.cpp +++ clang-tidy/ClangTidyModule.cpp @@ -16,16 +16,16 @@ namespace clang { namespace tidy { -void ClangTidyCheckFactories::addCheckFactory( - StringRef Name, std::unique_ptr Factory) { - Factories[Name] = std::move(Factory); +void ClangTidyCheckFactories::registerCheckFactory( + StringRef Name, std::function Factory) { + Factories[Name] = Factory; } void ClangTidyCheckFactories::createChecks( GlobList &Filter, std::vector> &Checks) { for (const auto &Factory : Factories) { if (Filter.contains(Factory.first)) { - ClangTidyCheck *Check = Factory.second->createCheck(); + ClangTidyCheck *Check = Factory.second(); Check->setName(Factory.first); Checks.emplace_back(Check); }