clang-tools  10.0.0svn
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 "ClangTidy.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <functional>
15 #include <map>
16 #include <string>
17 #include <utility>
18 
19 namespace clang {
20 namespace tidy {
21 
22 /// A collection of \c ClangTidyCheckFactory instances.
23 ///
24 /// All clang-tidy modules register their check factories with an instance of
25 /// this object.
27 public:
28  typedef std::function<ClangTidyCheck *(StringRef Name,
29  ClangTidyContext *Context)>
31 
32  /// Registers check \p Factory with name \p Name.
33  ///
34  /// For all checks that have default constructors, use \c registerCheck.
35  void registerCheckFactory(StringRef Name, CheckFactory Factory);
36 
37  /// Registers the \c CheckType with the name \p Name.
38  ///
39  /// This method should be used for all \c ClangTidyChecks that don't require
40  /// constructor parameters.
41  ///
42  /// For example, if have a clang-tidy check like:
43  /// \code
44  /// class MyTidyCheck : public ClangTidyCheck {
45  /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
46  /// ..
47  /// }
48  /// };
49  /// \endcode
50  /// you can register it with:
51  /// \code
52  /// class MyModule : public ClangTidyModule {
53  /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
54  /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
55  /// }
56  /// };
57  /// \endcode
58  template <typename CheckType> void registerCheck(StringRef CheckName) {
59  registerCheckFactory(CheckName,
60  [](StringRef Name, ClangTidyContext *Context) {
61  return new CheckType(Name, Context);
62  });
63  }
64 
65  /// Create instances of all checks matching \p CheckRegexString and
66  /// store them in \p Checks.
67  ///
68  /// The caller takes ownership of the return \c ClangTidyChecks.
69  void createChecks(ClangTidyContext *Context,
70  std::vector<std::unique_ptr<ClangTidyCheck>> &Checks);
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.
92  virtual ClangTidyOptions getModuleOptions();
93 };
94 
95 } // end namespace tidy
96 } // end namespace clang
97 
98 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
void registerCheckFactory(StringRef Name, CheckFactory Factory)
Registers check Factory with name Name.
void registerCheck(StringRef CheckName)
Registers the CheckType with the name Name.
Contains options for clang-tidy.
Base class for all clang-tidy checks.
A collection of ClangTidyCheckFactory instances.
FactoryMap::const_iterator begin() const
std::function< ClangTidyCheck *(StringRef Name, ClangTidyContext *Context)> CheckFactory
std::map< std::string, CheckFactory > FactoryMap
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
void createChecks(ClangTidyContext *Context, std::vector< std::unique_ptr< ClangTidyCheck >> &Checks)
Create instances of all checks matching CheckRegexString and store them in Checks.
static constexpr llvm::StringLiteral Name
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static cl::opt< std::string > Checks("checks", cl::desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
FactoryMap::const_iterator end() const