blob: c9c51438a0067a7d4d7db02c349dc7e7f41ca1cc [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"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include <utility>
17
18namespace clang {
19namespace tidy {
20
21/// \brief A factory, that can instantiate a specific clang-tidy check for
22/// processing a translation unit.
23///
24/// In order to register your check with the \c ClangTidyModule, create a
25/// subclass of \c CheckFactoryBase and implement \c createCheck(). Then, use
26/// this subclass in \c ClangTidyModule::addCheckFactories().
27class CheckFactoryBase {
28public:
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000029 virtual ~CheckFactoryBase() {}
Daniel Jasperd07c8402013-07-29 08:19:24 +000030 virtual ClangTidyCheck *createCheck() = 0;
31};
32
33/// \brief A subclass of \c CheckFactoryBase that should be used for all
34/// \c ClangTidyChecks that don't require constructor parameters.
35///
36/// For example, if have a clang-tidy check like:
37/// \code
38/// class MyTidyCheck : public ClangTidyCheck {
Craig Toppera3dbe842014-03-02 10:20:11 +000039/// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
Alexander Kornienkocb9272f2014-02-27 13:14:51 +000040/// ..
41/// }
Daniel Jasperd07c8402013-07-29 08:19:24 +000042/// };
43/// \endcode
44/// you can register it with:
45/// \code
46/// class MyModule : public ClangTidyModule {
Alexander Kornienko21f3b772014-03-05 13:01:24 +000047/// void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
Daniel Jasperd07c8402013-07-29 08:19:24 +000048/// CheckFactories.addCheckFactory(
49/// "myproject-my-check", new ClangTidyCheckFactory<MyTidyCheck>());
50/// }
51/// };
52/// \endcode
53template <typename T> class ClangTidyCheckFactory : public CheckFactoryBase {
54public:
Craig Toppera3dbe842014-03-02 10:20:11 +000055 ClangTidyCheck *createCheck() override { return new T; }
Daniel Jasperd07c8402013-07-29 08:19:24 +000056};
57
58class ClangTidyCheckFactories;
59
60/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
61/// them a prefixed name.
62class ClangTidyModule {
63public:
64 virtual ~ClangTidyModule() {}
65
66 /// \brief Implement this function in order to register all \c CheckFactories
67 /// belonging to this module.
68 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
69};
70
71/// \brief A collection of \c ClangTidyCheckFactory instances.
72///
73/// All clang-tidy modules register their check factories with an instance of
74/// this object.
75class ClangTidyCheckFactories {
76public:
77 ClangTidyCheckFactories() {}
78 ~ClangTidyCheckFactories();
79
80 /// \brief Register \p Factory with the name \p Name.
81 ///
82 /// The \c ClangTidyCheckFactories object takes ownership of the \p Factory.
83 void addCheckFactory(StringRef Name, CheckFactoryBase *Factory);
84
85 /// \brief Create instances of all checks matching \p CheckRegexString and
86 /// store them in \p Checks.
87 ///
88 /// The caller takes ownership of the return \c ClangTidyChecks.
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000089 void createChecks(GlobList &Filter,
Alexander Kornienkoa4695222014-06-05 13:31:45 +000090 std::vector<std::unique_ptr<ClangTidyCheck>> &Checks);
Daniel Jasperd07c8402013-07-29 08:19:24 +000091
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000092 typedef std::map<std::string, CheckFactoryBase *> FactoryMap;
93 FactoryMap::const_iterator begin() const { return Factories.begin(); }
94 FactoryMap::const_iterator end() const { return Factories.end(); }
Alexander Kornienko298b3822014-02-13 16:10:47 +000095 bool empty() const { return Factories.empty(); }
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000096
Daniel Jasperd07c8402013-07-29 08:19:24 +000097private:
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000098 FactoryMap Factories;
Daniel Jasperd07c8402013-07-29 08:19:24 +000099};
100
101} // end namespace tidy
102} // end namespace clang
103
104#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H