1 //===--- ClangTidyModule.h - clang-tidy -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
12 #include "ClangTidyOptions.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
22 class ClangTidyContext
;
24 /// A collection of \c ClangTidyCheckFactory instances.
26 /// All clang-tidy modules register their check factories with an instance of
28 class ClangTidyCheckFactories
{
30 using CheckFactory
= std::function
<std::unique_ptr
<ClangTidyCheck
>(
31 llvm::StringRef Name
, ClangTidyContext
*Context
)>;
33 /// Registers check \p Factory with name \p Name.
35 /// For all checks that have default constructors, use \c registerCheck.
36 void registerCheckFactory(llvm::StringRef Name
, CheckFactory Factory
);
38 /// Registers the \c CheckType with the name \p Name.
40 /// This method should be used for all \c ClangTidyChecks that don't require
41 /// constructor parameters.
43 /// For example, if have a clang-tidy check like:
45 /// class MyTidyCheck : public ClangTidyCheck {
46 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
51 /// you can register it with:
53 /// class MyModule : public ClangTidyModule {
54 /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
55 /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
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
);
66 /// Create instances of checks that are enabled.
67 std::vector
<std::unique_ptr
<ClangTidyCheck
>>
68 createChecks(ClangTidyContext
*Context
);
70 /// Create instances of checks that are enabled for the current Language.
71 std::vector
<std::unique_ptr
<ClangTidyCheck
>>
72 createChecksForLanguage(ClangTidyContext
*Context
);
74 typedef llvm::StringMap
<CheckFactory
> FactoryMap
;
75 FactoryMap::const_iterator
begin() const { return Factories
.begin(); }
76 FactoryMap::const_iterator
end() const { return Factories
.end(); }
77 bool empty() const { return Factories
.empty(); }
83 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
84 /// them a prefixed name.
85 class ClangTidyModule
{
87 virtual ~ClangTidyModule() {}
89 /// Implement this function in order to register all \c CheckFactories
90 /// belonging to this module.
91 virtual void addCheckFactories(ClangTidyCheckFactories
&CheckFactories
) = 0;
93 /// Gets default options for checks defined in this module.
94 virtual ClangTidyOptions
getModuleOptions();
97 } // end namespace tidy
98 } // end namespace clang
100 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H