blob: 1134d380d49ef62900e33526be40eda8a3e684d9 [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- 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 Jasperd07c8402013-07-29 08:19:24 +000014#include "llvm/ADT/StringRef.h"
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000015#include <functional>
16#include <map>
17#include <string>
Daniel Jasperd07c8402013-07-29 08:19:24 +000018#include <utility>
19
20namespace clang {
21namespace tidy {
22
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000023/// \brief A collection of \c ClangTidyCheckFactory instances.
Daniel Jasperd07c8402013-07-29 08:19:24 +000024///
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000025/// All clang-tidy modules register their check factories with an instance of
26/// this object.
27class ClangTidyCheckFactories {
Daniel Jasperd07c8402013-07-29 08:19:24 +000028public:
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000029 /// \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 Jasperd07c8402013-07-29 08:19:24 +000034
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000035 /// \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 Jasperd07c8402013-07-29 08:19:24 +000060
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000061 /// \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
73private:
74 FactoryMap Factories;
75};
Daniel Jasperd07c8402013-07-29 08:19:24 +000076
77/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
78/// them a prefixed name.
79class ClangTidyModule {
80public:
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 Jasperd07c8402013-07-29 08:19:24 +000088} // end namespace tidy
89} // end namespace clang
90
91#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H