clang-tools 22.0.0git
ClangTidyModule.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "ClangTidyOptions.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Registry.h"
16#include <functional>
17#include <memory>
18
19namespace clang::tidy {
20
21class ClangTidyCheck;
23
24/// A collection of \c ClangTidyCheckFactory instances.
25///
26/// All clang-tidy modules register their check factories with an instance of
27/// this object.
29public:
30 using CheckFactory = std::function<std::unique_ptr<ClangTidyCheck>(
31 llvm::StringRef Name, ClangTidyContext *Context)>;
32
33 /// Registers check \p Factory with name \p Name.
34 ///
35 /// For all checks that have default constructors, use \c registerCheck.
36 void registerCheckFactory(llvm::StringRef Name, CheckFactory Factory);
37
38 /// Registers the \c CheckType with the name \p Name.
39 ///
40 /// This method should be used for all \c ClangTidyChecks that don't require
41 /// constructor parameters.
42 ///
43 /// For example, if have a clang-tidy check like:
44 /// \code
45 /// class MyTidyCheck : public ClangTidyCheck {
46 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
47 /// ..
48 /// }
49 /// };
50 /// \endcode
51 /// you can register it with:
52 /// \code
53 /// class MyModule : public ClangTidyModule {
54 /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
55 /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
56 /// }
57 /// };
58 /// \endcode
59 template <typename CheckType> void registerCheck(llvm::StringRef CheckName) {
60 registerCheckFactory(CheckName,
61 [](llvm::StringRef Name, ClangTidyContext *Context) {
62 return std::make_unique<CheckType>(Name, Context);
63 });
64 }
65
66 void eraseCheck(llvm::StringRef CheckName) { Factories.erase(CheckName); }
67
68 /// Create instances of checks that are enabled.
69 std::vector<std::unique_ptr<ClangTidyCheck>>
70 createChecks(ClangTidyContext *Context) const;
71
72 /// Create instances of checks that are enabled for the current Language.
73 std::vector<std::unique_ptr<ClangTidyCheck>>
75
76 using FactoryMap = llvm::StringMap<CheckFactory>;
77 FactoryMap::const_iterator begin() const { return Factories.begin(); }
78 FactoryMap::const_iterator end() const { return Factories.end(); }
79 bool empty() const { return Factories.empty(); }
80
81private:
82 FactoryMap Factories;
83};
84
85/// A clang-tidy module groups a number of \c ClangTidyChecks and gives
86/// them a prefixed name.
88public:
89 virtual ~ClangTidyModule() = default;
90
91 /// Implement this function in order to register all \c CheckFactories
92 /// belonging to this module.
93 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
94
95 /// Gets default options for checks defined in this module.
97};
98
99using ClangTidyModuleRegistry = llvm::Registry<ClangTidyModule>;
100
101} // namespace clang::tidy
102
103namespace llvm {
104extern template class Registry<clang::tidy::ClangTidyModule>;
105} // namespace llvm
106
107#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
A collection of ClangTidyCheckFactory instances.
std::vector< std::unique_ptr< ClangTidyCheck > > createChecksForLanguage(ClangTidyContext *Context) const
Create instances of checks that are enabled for the current Language.
void registerCheckFactory(llvm::StringRef Name, CheckFactory Factory)
Registers check Factory with name Name.
FactoryMap::const_iterator begin() const
std::function< std::unique_ptr< ClangTidyCheck >( llvm::StringRef Name, ClangTidyContext *Context)> CheckFactory
llvm::StringMap< CheckFactory > FactoryMap
void registerCheck(llvm::StringRef CheckName)
Registers the CheckType with the name Name.
FactoryMap::const_iterator end() const
void eraseCheck(llvm::StringRef CheckName)
std::vector< std::unique_ptr< ClangTidyCheck > > createChecks(ClangTidyContext *Context) const
Create instances of checks that are enabled.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories)=0
Implement this function in order to register all CheckFactories belonging to this module.
virtual ClangTidyOptions getModuleOptions()
Gets default options for checks defined in this module.
virtual ~ClangTidyModule()=default
llvm::Registry< ClangTidyModule > ClangTidyModuleRegistry
Some operations such as code completion produce a set of candidates.
Definition Generators.h:145
Contains options for clang-tidy.