• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
30 class ClangTidyCheckFactories {
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
registerCheck(llvm::StringRef CheckName)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>>
70   createChecks(ClangTidyContext *Context);
71 
72   typedef llvm::StringMap<CheckFactory> FactoryMap;
begin()73   FactoryMap::const_iterator begin() const { return Factories.begin(); }
end()74   FactoryMap::const_iterator end() const { return Factories.end(); }
empty()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.
83 class ClangTidyModule {
84 public:
~ClangTidyModule()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
99