clang-tools  14.0.0git
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 "ClangTidyOptions.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <functional>
16 #include <memory>
17 
18 namespace clang {
19 namespace tidy {
20 
21 class ClangTidyCheck;
22 class ClangTidyContext;
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.
29 public:
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  /// Create instances of checks that are enabled.
67  std::vector<std::unique_ptr<ClangTidyCheck>>
69 
70  typedef llvm::StringMap<CheckFactory> FactoryMap;
71  FactoryMap::const_iterator begin() const { return Factories.begin(); }
72  FactoryMap::const_iterator end() const { return Factories.end(); }
73  bool empty() const { return Factories.empty(); }
74 
75 private:
76  FactoryMap Factories;
77 };
78 
79 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
80 /// them a prefixed name.
82 public:
83  virtual ~ClangTidyModule() {}
84 
85  /// Implement this function in order to register all \c CheckFactories
86  /// belonging to this module.
87  virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
88 
89  /// Gets default options for checks defined in this module.
91 };
92 
93 } // end namespace tidy
94 } // end namespace clang
95 
96 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
clang::tidy::ClangTidyCheckFactories::registerCheckFactory
void registerCheckFactory(llvm::StringRef Name, CheckFactory Factory)
Registers check Factory with name Name.
Definition: ClangTidyModule.cpp:19
clang::tidy::ClangTidyCheckFactories::CheckFactory
std::function< std::unique_ptr< ClangTidyCheck >(llvm::StringRef Name, ClangTidyContext *Context)> CheckFactory
Definition: ClangTidyModule.h:31
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
clang::tidy::ClangTidyCheckFactories
A collection of ClangTidyCheckFactory instances.
Definition: ClangTidyModule.h:28
clang::tidy::ClangTidyCheckFactories::empty
bool empty() const
Definition: ClangTidyModule.h:73
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyModule::getModuleOptions
virtual ClangTidyOptions getModuleOptions()
Gets default options for checks defined in this module.
Definition: ClangTidyModule.cpp:34
clang::tidy::ClangTidyCheckFactories::end
FactoryMap::const_iterator end() const
Definition: ClangTidyModule.h:72
clang::tidy::ClangTidyCheckFactories::begin
FactoryMap::const_iterator begin() const
Definition: ClangTidyModule.h:71
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheckFactories::createChecks
std::vector< std::unique_ptr< ClangTidyCheck > > createChecks(ClangTidyContext *Context)
Create instances of checks that are enabled.
Definition: ClangTidyModule.cpp:25
ClangTidyOptions.h
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::ClangTidyModule
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
Definition: ClangTidyModule.h:81
clang::tidy::ClangTidyModule::~ClangTidyModule
virtual ~ClangTidyModule()
Definition: ClangTidyModule.h:83
clang::tidy::ClangTidyCheckFactories::registerCheck
void registerCheck(llvm::StringRef CheckName)
Registers the CheckType with the name Name.
Definition: ClangTidyModule.h:59
clang::tidy::ClangTidyCheckFactories::FactoryMap
llvm::StringMap< CheckFactory > FactoryMap
Definition: ClangTidyModule.h:70