blob: 47a69331314437c6737b91f096054b78328642b3 [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 {
39/// virtual void registerMatchers(ast_matchers::MatchFinder *Finder) { .. }
40/// };
41/// \endcode
42/// you can register it with:
43/// \code
44/// class MyModule : public ClangTidyModule {
45/// virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) {
46/// CheckFactories.addCheckFactory(
47/// "myproject-my-check", new ClangTidyCheckFactory<MyTidyCheck>());
48/// }
49/// };
50/// \endcode
51template <typename T> class ClangTidyCheckFactory : public CheckFactoryBase {
52public:
53 virtual ClangTidyCheck *createCheck() { return new T; }
54};
55
56class ClangTidyCheckFactories;
57
58/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
59/// them a prefixed name.
60class ClangTidyModule {
61public:
62 virtual ~ClangTidyModule() {}
63
64 /// \brief Implement this function in order to register all \c CheckFactories
65 /// belonging to this module.
66 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
67};
68
69/// \brief A collection of \c ClangTidyCheckFactory instances.
70///
71/// All clang-tidy modules register their check factories with an instance of
72/// this object.
73class ClangTidyCheckFactories {
74public:
75 ClangTidyCheckFactories() {}
76 ~ClangTidyCheckFactories();
77
78 /// \brief Register \p Factory with the name \p Name.
79 ///
80 /// The \c ClangTidyCheckFactories object takes ownership of the \p Factory.
81 void addCheckFactory(StringRef Name, CheckFactoryBase *Factory);
82
83 /// \brief Create instances of all checks matching \p CheckRegexString and
84 /// store them in \p Checks.
85 ///
86 /// The caller takes ownership of the return \c ClangTidyChecks.
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000087 void createChecks(ChecksFilter &Filter,
Daniel Jasperd07c8402013-07-29 08:19:24 +000088 SmallVectorImpl<ClangTidyCheck *> &Checks);
89
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000090 typedef std::map<std::string, CheckFactoryBase *> FactoryMap;
91 FactoryMap::const_iterator begin() const { return Factories.begin(); }
92 FactoryMap::const_iterator end() const { return Factories.end(); }
Alexander Kornienko298b3822014-02-13 16:10:47 +000093 bool empty() const { return Factories.empty(); }
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000094
Daniel Jasperd07c8402013-07-29 08:19:24 +000095private:
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000096 FactoryMap Factories;
Daniel Jasperd07c8402013-07-29 08:19:24 +000097};
98
99} // end namespace tidy
100} // end namespace clang
101
102#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H