[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clang-tidy / ClangTidyModule.h
blobf44457d3ae8f8fb8f2d563f13cc31e70c245d86c
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
12 #include "ClangTidyOptions.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <functional>
16 #include <memory>
18 namespace clang {
19 namespace tidy {
21 class ClangTidyCheck;
22 class ClangTidyContext;
24 /// A collection of \c ClangTidyCheckFactory instances.
25 ///
26 /// All clang-tidy modules register their check factories with an instance of
27 /// this object.
28 class ClangTidyCheckFactories {
29 public:
30 using CheckFactory = std::function<std::unique_ptr<ClangTidyCheck>(
31 llvm::StringRef Name, ClangTidyContext *Context)>;
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);
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 });
66 /// Create instances of checks that are enabled.
67 std::vector<std::unique_ptr<ClangTidyCheck>>
68 createChecks(ClangTidyContext *Context);
70 /// Create instances of checks that are enabled for the current Language.
71 std::vector<std::unique_ptr<ClangTidyCheck>>
72 createChecksForLanguage(ClangTidyContext *Context);
74 typedef llvm::StringMap<CheckFactory> FactoryMap;
75 FactoryMap::const_iterator begin() const { return Factories.begin(); }
76 FactoryMap::const_iterator end() const { return Factories.end(); }
77 bool empty() const { return Factories.empty(); }
79 private:
80 FactoryMap Factories;
83 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
84 /// them a prefixed name.
85 class ClangTidyModule {
86 public:
87 virtual ~ClangTidyModule() {}
89 /// Implement this function in order to register all \c CheckFactories
90 /// belonging to this module.
91 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
93 /// Gets default options for checks defined in this module.
94 virtual ClangTidyOptions getModuleOptions();
97 } // end namespace tidy
98 } // end namespace clang
100 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H