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"
18 namespace clang::tidy
{
21 class ClangTidyContext
;
23 /// A collection of \c ClangTidyCheckFactory instances.
25 /// All clang-tidy modules register their check factories with an instance of
27 class ClangTidyCheckFactories
{
29 using CheckFactory
= std::function
<std::unique_ptr
<ClangTidyCheck
>(
30 llvm::StringRef Name
, ClangTidyContext
*Context
)>;
32 /// Registers check \p Factory with name \p Name.
34 /// For all checks that have default constructors, use \c registerCheck.
35 void registerCheckFactory(llvm::StringRef Name
, CheckFactory Factory
);
37 /// Registers the \c CheckType with the name \p Name.
39 /// This method should be used for all \c ClangTidyChecks that don't require
40 /// constructor parameters.
42 /// For example, if have a clang-tidy check like:
44 /// class MyTidyCheck : public ClangTidyCheck {
45 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
50 /// you can register it with:
52 /// class MyModule : public ClangTidyModule {
53 /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
54 /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
58 template <typename CheckType
> void registerCheck(llvm::StringRef CheckName
) {
59 registerCheckFactory(CheckName
,
60 [](llvm::StringRef Name
, ClangTidyContext
*Context
) {
61 return std::make_unique
<CheckType
>(Name
, Context
);
65 /// Create instances of checks that are enabled.
66 std::vector
<std::unique_ptr
<ClangTidyCheck
>>
67 createChecks(ClangTidyContext
*Context
) const;
69 /// Create instances of checks that are enabled for the current Language.
70 std::vector
<std::unique_ptr
<ClangTidyCheck
>>
71 createChecksForLanguage(ClangTidyContext
*Context
) const;
73 typedef llvm::StringMap
<CheckFactory
> FactoryMap
;
74 FactoryMap::const_iterator
begin() const { return Factories
.begin(); }
75 FactoryMap::const_iterator
end() const { return Factories
.end(); }
76 bool empty() const { return Factories
.empty(); }
82 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
83 /// them a prefixed name.
84 class ClangTidyModule
{
86 virtual ~ClangTidyModule() {}
88 /// Implement this function in order to register all \c CheckFactories
89 /// belonging to this module.
90 virtual void addCheckFactories(ClangTidyCheckFactories
&CheckFactories
) = 0;
92 /// Gets default options for checks defined in this module.
93 virtual ClangTidyOptions
getModuleOptions();
96 } // namespace clang::tidy
98 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H