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