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 <memory> 17 18 namespace clang { 19 namespace tidy { 20 21 class ClangTidyCheck; 22 class ClangTidyContext; 23 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)>; 32 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); 37 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 registerCheck(llvm::StringRef CheckName)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 }); 64 } 65 66 /// Create instances of checks that are enabled. 67 std::vector<std::unique_ptr<ClangTidyCheck>> 68 createChecks(ClangTidyContext *Context); 69 70 /// Create instances of checks that are enabled for the current Language. 71 std::vector<std::unique_ptr<ClangTidyCheck>> 72 createChecksForLanguage(ClangTidyContext *Context); 73 74 typedef llvm::StringMap<CheckFactory> FactoryMap; begin()75 FactoryMap::const_iterator begin() const { return Factories.begin(); } end()76 FactoryMap::const_iterator end() const { return Factories.end(); } empty()77 bool empty() const { return Factories.empty(); } 78 79 private: 80 FactoryMap Factories; 81 }; 82 83 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives 84 /// them a prefixed name. 85 class ClangTidyModule { 86 public: ~ClangTidyModule()87 virtual ~ClangTidyModule() {} 88 89 /// Implement this function in order to register all \c CheckFactories 90 /// belonging to this module. 91 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0; 92 93 /// Gets default options for checks defined in this module. 94 virtual ClangTidyOptions getModuleOptions(); 95 }; 96 97 } // end namespace tidy 98 } // end namespace clang 99 100 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H 101