Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- ClangTidyModule.h - clang-tidy -------------------------*- C++ -*-===// |
| 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 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H |
| 12 | |
| 13 | #include "ClangTidy.h" |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Alexander Kornienko | d3fdcf8 | 2014-09-10 11:25:43 +0000 | [diff] [blame^] | 15 | #include <functional> |
| 16 | #include <map> |
| 17 | #include <string> |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 18 | #include <utility> |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | |
Alexander Kornienko | d3fdcf8 | 2014-09-10 11:25:43 +0000 | [diff] [blame^] | 23 | /// \brief A collection of \c ClangTidyCheckFactory instances. |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 24 | /// |
Alexander Kornienko | d3fdcf8 | 2014-09-10 11:25:43 +0000 | [diff] [blame^] | 25 | /// All clang-tidy modules register their check factories with an instance of |
| 26 | /// this object. |
| 27 | class ClangTidyCheckFactories { |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 28 | public: |
Alexander Kornienko | d3fdcf8 | 2014-09-10 11:25:43 +0000 | [diff] [blame^] | 29 | /// \brief Registers check \p Factory with name \p Name. |
| 30 | /// |
| 31 | /// For all checks that have default constructors, use \c registerCheck. |
| 32 | void registerCheckFactory(StringRef Name, |
| 33 | std::function<ClangTidyCheck *()> Factory); |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 34 | |
Alexander Kornienko | d3fdcf8 | 2014-09-10 11:25:43 +0000 | [diff] [blame^] | 35 | /// \brief Registers the \c CheckType with the name \p Name. |
| 36 | /// |
| 37 | /// This method should be used for all \c ClangTidyChecks that don't require |
| 38 | /// constructor parameters. |
| 39 | /// |
| 40 | /// For example, if have a clang-tidy check like: |
| 41 | /// \code |
| 42 | /// class MyTidyCheck : public ClangTidyCheck { |
| 43 | /// void registerMatchers(ast_matchers::MatchFinder *Finder) override { |
| 44 | /// .. |
| 45 | /// } |
| 46 | /// }; |
| 47 | /// \endcode |
| 48 | /// you can register it with: |
| 49 | /// \code |
| 50 | /// class MyModule : public ClangTidyModule { |
| 51 | /// void addCheckFactories(ClangTidyCheckFactories &Factories) override { |
| 52 | /// Factories.registerCheck<MyTidyCheck>("myproject-my-check"); |
| 53 | /// } |
| 54 | /// }; |
| 55 | /// \endcode |
| 56 | template<typename CheckType> |
| 57 | void registerCheck(StringRef Name) { |
| 58 | registerCheckFactory(Name, []() { return new CheckType(); }); |
| 59 | } |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 60 | |
Alexander Kornienko | d3fdcf8 | 2014-09-10 11:25:43 +0000 | [diff] [blame^] | 61 | /// \brief Create instances of all checks matching \p CheckRegexString and |
| 62 | /// store them in \p Checks. |
| 63 | /// |
| 64 | /// The caller takes ownership of the return \c ClangTidyChecks. |
| 65 | void createChecks(GlobList &Filter, |
| 66 | std::vector<std::unique_ptr<ClangTidyCheck>> &Checks); |
| 67 | |
| 68 | typedef std::map<std::string, std::function<ClangTidyCheck *()>> FactoryMap; |
| 69 | FactoryMap::const_iterator begin() const { return Factories.begin(); } |
| 70 | FactoryMap::const_iterator end() const { return Factories.end(); } |
| 71 | bool empty() const { return Factories.empty(); } |
| 72 | |
| 73 | private: |
| 74 | FactoryMap Factories; |
| 75 | }; |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 76 | |
| 77 | /// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives |
| 78 | /// them a prefixed name. |
| 79 | class ClangTidyModule { |
| 80 | public: |
| 81 | virtual ~ClangTidyModule() {} |
| 82 | |
| 83 | /// \brief Implement this function in order to register all \c CheckFactories |
| 84 | /// belonging to this module. |
| 85 | virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0; |
| 86 | }; |
| 87 | |
Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 88 | } // end namespace tidy |
| 89 | } // end namespace clang |
| 90 | |
| 91 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H |