diff options
author | Alexander Kornienko <alexfh@google.com> | 2014-09-10 11:25:43 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2014-09-10 11:25:43 +0000 |
commit | 92b46944ac205b8d27e21b9db27527fb3553ae4d (patch) | |
tree | ad021b7cf468c2d1f514ad14a5252623d9e8749b /clang-tidy/ClangTidyModule.h | |
parent | 5787fcf8445673ead21f77d64bfe9a39f38b22de (diff) |
std::function-ize ClangTidyCheckFactories.
Reviewers: djasper
Reviewed By: djasper
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D5289
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@217492 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy/ClangTidyModule.h')
-rw-r--r-- | clang-tidy/ClangTidyModule.h | 101 |
1 files changed, 42 insertions, 59 deletions
diff --git a/clang-tidy/ClangTidyModule.h b/clang-tidy/ClangTidyModule.h index 9580ce9a..1134d380 100644 --- a/clang-tidy/ClangTidyModule.h +++ b/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 <functional> +#include <map> +#include <string> #include <utility> 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<MyTidyCheck>()); -/// } -/// }; -/// \endcode -template <typename T> 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<CheckFactoryBase> 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<ClangTidyCheck *()> 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<MyTidyCheck>("myproject-my-check"); + /// } + /// }; + /// \endcode template<typename CheckType> void registerCheck(StringRef Name) { - addCheckFactory(Name, - llvm::make_unique<ClangTidyCheckFactory<CheckType>>()); + registerCheckFactory(Name, []() { return new CheckType(); }); } /// \brief Create instances of all checks matching \p CheckRegexString and @@ -93,7 +65,7 @@ public: void createChecks(GlobList &Filter, std::vector<std::unique_ptr<ClangTidyCheck>> &Checks); - typedef std::map<std::string, std::unique_ptr<CheckFactoryBase>> FactoryMap; + typedef std::map<std::string, std::function<ClangTidyCheck *()>> 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 @@ private: 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 |