clang-tools 10.0.0

ClangTidyModule.h
Go to the documentation of this file.
1 //===--- ClangTidyModule.h - clang-tidy -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
11
12 #include "ClangTidy.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <functional>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <utility>
19
20 namespace clang {
21 namespace tidy {
22
23 /// 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 using CheckFactory = std::function<std::unique_ptr<ClangTidyCheck>(
30 StringRef Name, ClangTidyContext *Context)>;
31
32 /// 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 /// 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 std::make_unique<CheckType>(Name, Context);
62 });
63 }
64
65 /// Create instances of checks that are enabled.
66 std::vector<std::unique_ptr<ClangTidyCheck>>
67 createChecks(ClangTidyContext *Context);
68
69 typedef std::map<std::string, CheckFactory> FactoryMap;
70 FactoryMap::const_iterator begin() const { return Factories.begin(); }
71 FactoryMap::const_iterator end() const { return Factories.end(); }
72 bool empty() const { return Factories.empty(); }
73
74 private:
75 FactoryMap Factories;
76 };
77
78 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
79 /// them a prefixed name.
80 class ClangTidyModule {
81 public:
82 virtual ~ClangTidyModule() {}
83
84 /// Implement this function in order to register all \c CheckFactories
85 /// belonging to this module.
86 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
87
88 /// Gets default options for checks defined in this module.
89 virtual ClangTidyOptions getModuleOptions();
90 };
91
92 } // end namespace tidy
93 } // end namespace clang
94
95 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
ClangTidy.h
clang::tidy::ClangTidyCheckFactories::registerCheckFactory
void registerCheckFactory(StringRef Name, CheckFactory Factory)
Registers check Factory with name Name.
Definition: ClangTidyModule.cpp:18
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:50
clang::tidy::ClangTidyModule::~ClangTidyModule
virtual ~ClangTidyModule()
Definition: ClangTidyModule.h:82
clang::tidy::ClangTidyCheckFactories
A collection of ClangTidyCheckFactory instances.
Definition: ClangTidyModule.h:27
clang::tidy::ClangTidyCheckFactories::begin
FactoryMap::const_iterator begin() const
Definition: ClangTidyModule.h:70
clang::tidy::ClangTidyCheckFactories::empty
bool empty() const
Definition: ClangTidyModule.h:72
clang::tidy::ClangTidyCheckFactories::FactoryMap
std::map< std::string, CheckFactory > FactoryMap
Definition: ClangTidyModule.h:69
clang::tidy::ClangTidyModule
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
Definition: ClangTidyModule.h:80
clang::tidy::ClangTidyCheckFactories::CheckFactory
std::function< std::unique_ptr< ClangTidyCheck >(StringRef Name, ClangTidyContext *Context)> CheckFactory
Definition: ClangTidyModule.h:30
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::ClangTidyCheckFactories::end
FactoryMap::const_iterator end() const
Definition: ClangTidyModule.h:71
clang::tidy::ClangTidyCheckFactories::createChecks
std::vector< std::unique_ptr< ClangTidyCheck > > createChecks(ClangTidyContext *Context)
Create instances of checks that are enabled.
Definition: ClangTidyModule.cpp:24