Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:23

0001 //===--- ClangTidyModule.h - clang-tidy -------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
0011 
0012 #include "ClangTidyOptions.h"
0013 #include "llvm/ADT/StringMap.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include <functional>
0016 #include <memory>
0017 
0018 namespace clang::tidy {
0019 
0020 class ClangTidyCheck;
0021 class ClangTidyContext;
0022 
0023 /// A collection of \c ClangTidyCheckFactory instances.
0024 ///
0025 /// All clang-tidy modules register their check factories with an instance of
0026 /// this object.
0027 class ClangTidyCheckFactories {
0028 public:
0029   using CheckFactory = std::function<std::unique_ptr<ClangTidyCheck>(
0030       llvm::StringRef Name, ClangTidyContext *Context)>;
0031 
0032   /// Registers check \p Factory with name \p Name.
0033   ///
0034   /// For all checks that have default constructors, use \c registerCheck.
0035   void registerCheckFactory(llvm::StringRef Name, CheckFactory Factory);
0036 
0037   /// Registers the \c CheckType with the name \p Name.
0038   ///
0039   /// This method should be used for all \c ClangTidyChecks that don't require
0040   /// constructor parameters.
0041   ///
0042   /// For example, if have a clang-tidy check like:
0043   /// \code
0044   /// class MyTidyCheck : public ClangTidyCheck {
0045   ///   void registerMatchers(ast_matchers::MatchFinder *Finder) override {
0046   ///     ..
0047   ///   }
0048   /// };
0049   /// \endcode
0050   /// you can register it with:
0051   /// \code
0052   /// class MyModule : public ClangTidyModule {
0053   ///   void addCheckFactories(ClangTidyCheckFactories &Factories) override {
0054   ///     Factories.registerCheck<MyTidyCheck>("myproject-my-check");
0055   ///   }
0056   /// };
0057   /// \endcode
0058   template <typename CheckType> void registerCheck(llvm::StringRef CheckName) {
0059     registerCheckFactory(CheckName,
0060                          [](llvm::StringRef Name, ClangTidyContext *Context) {
0061                            return std::make_unique<CheckType>(Name, Context);
0062                          });
0063   }
0064 
0065   /// Create instances of checks that are enabled.
0066   std::vector<std::unique_ptr<ClangTidyCheck>>
0067   createChecks(ClangTidyContext *Context) const;
0068 
0069   /// Create instances of checks that are enabled for the current Language.
0070   std::vector<std::unique_ptr<ClangTidyCheck>>
0071   createChecksForLanguage(ClangTidyContext *Context) const;
0072 
0073   using FactoryMap = llvm::StringMap<CheckFactory>;
0074   FactoryMap::const_iterator begin() const { return Factories.begin(); }
0075   FactoryMap::const_iterator end() const { return Factories.end(); }
0076   bool empty() const { return Factories.empty(); }
0077 
0078 private:
0079   FactoryMap Factories;
0080 };
0081 
0082 /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
0083 /// them a prefixed name.
0084 class ClangTidyModule {
0085 public:
0086   virtual ~ClangTidyModule() {}
0087 
0088   /// Implement this function in order to register all \c CheckFactories
0089   /// belonging to this module.
0090   virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
0091 
0092   /// Gets default options for checks defined in this module.
0093   virtual ClangTidyOptions getModuleOptions();
0094 };
0095 
0096 } // namespace clang::tidy
0097 
0098 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H