101 lines · c
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H11 12#include "ClangTidyOptions.h"13#include "llvm/ADT/StringMap.h"14#include "llvm/ADT/StringRef.h"15#include <functional>16#include <memory>17 18namespace clang::tidy {19 20class ClangTidyCheck;21class ClangTidyContext;22 23/// A collection of \c ClangTidyCheckFactory instances.24///25/// All clang-tidy modules register their check factories with an instance of26/// this object.27class ClangTidyCheckFactories {28public:29 using CheckFactory = std::function<std::unique_ptr<ClangTidyCheck>(30 llvm::StringRef Name, 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(llvm::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 require40 /// constructor parameters.41 ///42 /// For example, if have a clang-tidy check like:43 /// \code44 /// class MyTidyCheck : public ClangTidyCheck {45 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {46 /// ..47 /// }48 /// };49 /// \endcode50 /// you can register it with:51 /// \code52 /// class MyModule : public ClangTidyModule {53 /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {54 /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");55 /// }56 /// };57 /// \endcode58 template <typename CheckType> void registerCheck(llvm::StringRef CheckName) {59 registerCheckFactory(CheckName,60 [](llvm::StringRef Name, ClangTidyContext *Context) {61 return std::make_unique<CheckType>(Name, Context);62 });63 }64 65 void eraseCheck(llvm::StringRef CheckName) { Factories.erase(CheckName); }66 67 /// Create instances of checks that are enabled.68 std::vector<std::unique_ptr<ClangTidyCheck>>69 createChecks(ClangTidyContext *Context) const;70 71 /// Create instances of checks that are enabled for the current Language.72 std::vector<std::unique_ptr<ClangTidyCheck>>73 createChecksForLanguage(ClangTidyContext *Context) const;74 75 using FactoryMap = llvm::StringMap<CheckFactory>;76 FactoryMap::const_iterator begin() const { return Factories.begin(); }77 FactoryMap::const_iterator end() const { return Factories.end(); }78 bool empty() const { return Factories.empty(); }79 80private:81 FactoryMap Factories;82};83 84/// A clang-tidy module groups a number of \c ClangTidyChecks and gives85/// them a prefixed name.86class ClangTidyModule {87public:88 virtual ~ClangTidyModule() = default;89 90 /// Implement this function in order to register all \c CheckFactories91 /// belonging to this module.92 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;93 94 /// Gets default options for checks defined in this module.95 virtual ClangTidyOptions getModuleOptions();96};97 98} // namespace clang::tidy99 100#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H101