clang-tools  8.0.0
ClangTidyModule.h
Go to the documentation of this file.
1 //===--- ClangTidyModule.h - clang-tidy -------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
12 
13 #include "ClangTidy.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <functional>
16 #include <map>
17 #include <string>
18 #include <utility>
19 
20 namespace clang {
21 namespace tidy {
22 
23 /// \brief A collection of \c ClangTidyCheckFactory instances.
24 ///
25 /// All clang-tidy modules register their check factories with an instance of
26 /// this object.
28 public:
29  typedef std::function<ClangTidyCheck *(StringRef Name,
30  ClangTidyContext *Context)>
32 
33  /// \brief Registers check \p Factory with name \p Name.
34  ///
35  /// For all checks that have default constructors, use \c registerCheck.
36  void registerCheckFactory(StringRef Name, CheckFactory Factory);
37 
38  /// \brief 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(StringRef CheckName) {
60  registerCheckFactory(CheckName,
61  [](StringRef Name, ClangTidyContext *Context) {
62  return new CheckType(Name, Context);
63  });
64  }
65 
66  /// \brief Create instances of all checks matching \p CheckRegexString and
67  /// store them in \p Checks.
68  ///
69  /// The caller takes ownership of the return \c ClangTidyChecks.
70  void createChecks(ClangTidyContext *Context,
71  std::vector<std::unique_ptr<ClangTidyCheck>> &Checks);
72 
73  typedef std::map<std::string, 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(); }
77 
78 private:
79  FactoryMap Factories;
80 };
81 
82 /// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
83 /// them a prefixed name.
85 public:
86  virtual ~ClangTidyModule() {}
87 
88  /// \brief Implement this function in order to register all \c CheckFactories
89  /// belonging to this module.
90  virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
91 
92  /// \brief Gets default options for checks defined in this module.
93  virtual ClangTidyOptions getModuleOptions();
94 };
95 
96 } // end namespace tidy
97 } // end namespace clang
98 
99 #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.
Definition: ClangTidy.h:127
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