clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ClangTidyModule.h
Go to the documentation of this file.
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_CLANGTIDYMODULE_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
12
13 #include "ClangTidy.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <functional>
16 #include <map>
17 #include <string>
18 #include <utility>
19
20 namespace clang {
21 namespace tidy {
22
23 /// \brief A collection of \c ClangTidyCheckFactory instances.
24 ///
25 /// All clang-tidy modules register their check factories with an instance of
26 /// this object.
27 class ClangTidyCheckFactories {
28 public:
29 typedef std::function<ClangTidyCheck *(
30 StringRef Name, ClangTidyContext *Context)> CheckFactory;
31
32 /// \brief Registers check \p Factory with name \p Name.
33 ///
34 /// For all checks that have default constructors, use \c registerCheck.
35 void registerCheckFactory(StringRef Name, CheckFactory Factory);
36
37 /// \brief Registers the \c CheckType with the name \p Name.
38 ///
39 /// This method should be used for all \c ClangTidyChecks that don't require
40 /// constructor parameters.
41 ///
42 /// For example, if have a clang-tidy check like:
43 /// \code
44 /// class MyTidyCheck : public ClangTidyCheck {
45 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
46 /// ..
47 /// }
48 /// };
49 /// \endcode
50 /// you can register it with:
51 /// \code
52 /// class MyModule : public ClangTidyModule {
53 /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
54 /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
55 /// }
56 /// };
57 /// \endcode
58 template <typename CheckType> void registerCheck(StringRef CheckName) {
59 registerCheckFactory(CheckName,
60 [](StringRef Name, ClangTidyContext *Context) {
61 return new CheckType(Name, Context);
62 });
63 }
64
65 /// \brief Create instances of all checks matching \p CheckRegexString and
66 /// store them in \p Checks.
67 ///
68 /// The caller takes ownership of the return \c ClangTidyChecks.
69 void createChecks(ClangTidyContext *Context,
70 std::vector<std::unique_ptr<ClangTidyCheck>> &Checks);
71
72 typedef std::map<std::string, CheckFactory> FactoryMap;
73 FactoryMap::const_iterator begin() const { return Factories.begin(); }
74 FactoryMap::const_iterator end() const { return Factories.end(); }
75 bool empty() const { return Factories.empty(); }
76
77 private:
78 FactoryMap Factories;
79 };
80
81 /// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
82 /// them a prefixed name.
83 class ClangTidyModule {
84 public:
85 virtual ~ClangTidyModule() {}
86
87 /// \brief Implement this function in order to register all \c CheckFactories
88 /// belonging to this module.
89 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
90
91 /// \brief Gets default options for checks defined in this module.
92 virtual ClangTidyOptions getModuleOptions();
93 };
94
95 } // end namespace tidy
96 } // end namespace clang
97
98 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
ClangTidy.h
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
Checks
std::vector< std::unique_ptr< ClangTidyCheck > > Checks
Definition: ClangTidy.cpp:189
clang::tidy::ClangTidyCheckFactories::registerCheckFactory
void registerCheckFactory(StringRef Name, CheckFactory Factory)
Registers check Factory with name Name.
Definition: ClangTidyModule.cpp:19
clang::tidy::ClangTidyCheckFactories::registerCheck
void registerCheck(StringRef CheckName)
Registers the CheckType with the name Name.
Definition: ClangTidyModule.h:58
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:49
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
clang::tidy::ClangTidyModule::~ClangTidyModule
virtual ~ClangTidyModule()
Definition: ClangTidyModule.h:85
clang::tidy::ClangTidyCheckFactories
A collection of ClangTidyCheckFactory instances.
Definition: ClangTidyModule.h:27
clang::tidy::ClangTidyCheckFactories::empty
bool empty() const
Definition: ClangTidyModule.h:75
clang::tidy::ClangTidyModule::getModuleOptions
virtual ClangTidyOptions getModuleOptions()
Gets default options for checks defined in this module.
Definition: ClangTidyModule.cpp:34
clang::tidy::ClangTidyCheckFactories::begin
FactoryMap::const_iterator begin() const
Definition: ClangTidyModule.h:73
clang::tidy::ClangTidyCheckFactories::FactoryMap
std::map< std::string, CheckFactory > FactoryMap
Definition: ClangTidyModule.h:72
clang::tidy::ClangTidyModule
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
Definition: ClangTidyModule.h:83
clang::tidy::ClangTidyCheckFactories::createChecks
void createChecks(ClangTidyContext *Context, std::vector< std::unique_ptr< ClangTidyCheck >> &Checks)
Create instances of all checks matching CheckRegexString and store them in Checks.
Definition: ClangTidyModule.cpp:24
clang::tidy::ClangTidyModule::addCheckFactories
virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories)=0
Implement this function in order to register all CheckFactories belonging to this module...
clang::tidy::ClangTidyCheckFactories::end
FactoryMap::const_iterator end() const
Definition: ClangTidyModule.h:74
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::ClangTidyCheckFactories::CheckFactory
std::function< ClangTidyCheck *(StringRef Name, ClangTidyContext *Context)> CheckFactory
Definition: ClangTidyModule.h:30