ClangTidyModule.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
  10. #include "ClangTidyOptions.h"
  11. #include "llvm/ADT/StringMap.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include <functional>
  14. #include <memory>
  15. namespace clang::tidy {
  16. class ClangTidyCheck;
  17. class ClangTidyContext;
  18. /// A collection of \c ClangTidyCheckFactory instances.
  19. ///
  20. /// All clang-tidy modules register their check factories with an instance of
  21. /// this object.
  22. class ClangTidyCheckFactories {
  23. public:
  24. using CheckFactory = std::function<std::unique_ptr<ClangTidyCheck>(
  25. llvm::StringRef Name, ClangTidyContext *Context)>;
  26. /// Registers check \p Factory with name \p Name.
  27. ///
  28. /// For all checks that have default constructors, use \c registerCheck.
  29. void registerCheckFactory(llvm::StringRef Name, CheckFactory Factory);
  30. /// Registers the \c CheckType with the name \p Name.
  31. ///
  32. /// This method should be used for all \c ClangTidyChecks that don't require
  33. /// constructor parameters.
  34. ///
  35. /// For example, if have a clang-tidy check like:
  36. /// \code
  37. /// class MyTidyCheck : public ClangTidyCheck {
  38. /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
  39. /// ..
  40. /// }
  41. /// };
  42. /// \endcode
  43. /// you can register it with:
  44. /// \code
  45. /// class MyModule : public ClangTidyModule {
  46. /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
  47. /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
  48. /// }
  49. /// };
  50. /// \endcode
  51. template <typename CheckType> void registerCheck(llvm::StringRef CheckName) {
  52. registerCheckFactory(CheckName,
  53. [](llvm::StringRef Name, ClangTidyContext *Context) {
  54. return std::make_unique<CheckType>(Name, Context);
  55. });
  56. }
  57. /// Create instances of checks that are enabled.
  58. std::vector<std::unique_ptr<ClangTidyCheck>>
  59. createChecks(ClangTidyContext *Context) const;
  60. /// Create instances of checks that are enabled for the current Language.
  61. std::vector<std::unique_ptr<ClangTidyCheck>>
  62. createChecksForLanguage(ClangTidyContext *Context) const;
  63. typedef llvm::StringMap<CheckFactory> FactoryMap;
  64. FactoryMap::const_iterator begin() const { return Factories.begin(); }
  65. FactoryMap::const_iterator end() const { return Factories.end(); }
  66. bool empty() const { return Factories.empty(); }
  67. private:
  68. FactoryMap Factories;
  69. };
  70. /// A clang-tidy module groups a number of \c ClangTidyChecks and gives
  71. /// them a prefixed name.
  72. class ClangTidyModule {
  73. public:
  74. virtual ~ClangTidyModule() {}
  75. /// Implement this function in order to register all \c CheckFactories
  76. /// belonging to this module.
  77. virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
  78. /// Gets default options for checks defined in this module.
  79. virtual ClangTidyOptions getModuleOptions();
  80. };
  81. } // namespace clang::tidy
  82. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H