TransformerClangTidyCheck.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //===---------- TransformerClangTidyCheck.h - clang-tidy ------------------===//
  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_TRANSFORMER_CLANG_TIDY_CHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "IncludeInserter.h"
  12. #include "IncludeSorter.h"
  13. #include "clang/ASTMatchers/ASTMatchFinder.h"
  14. #include "clang/Tooling/Transformer/Transformer.h"
  15. #include <optional>
  16. namespace clang::tidy::utils {
  17. /// A base class for defining a ClangTidy check based on a `RewriteRule`.
  18. //
  19. // For example, given a rule `MyCheckAsRewriteRule`, one can define a tidy check
  20. // as follows:
  21. //
  22. // class MyCheck : public TransformerClangTidyCheck {
  23. // public:
  24. // MyCheck(StringRef Name, ClangTidyContext *Context)
  25. // : TransformerClangTidyCheck(MyCheckAsRewriteRule, Name, Context) {}
  26. // };
  27. //
  28. // `TransformerClangTidyCheck` recognizes this clang-tidy option:
  29. //
  30. // * IncludeStyle. A string specifying which file naming convention is used by
  31. // the source code, 'llvm' or 'google'. Default is 'llvm'. The naming
  32. // convention influences how canonical headers are distinguished from other
  33. // includes.
  34. class TransformerClangTidyCheck : public ClangTidyCheck {
  35. public:
  36. TransformerClangTidyCheck(StringRef Name, ClangTidyContext *Context);
  37. /// DEPRECATED: prefer the two argument constructor in conjunction with
  38. /// \c setRule.
  39. ///
  40. /// \p MakeRule generates the rewrite rule to be used by the check, based on
  41. /// the given language and clang-tidy options. It can return \c std::nullopt
  42. /// to handle cases where the options disable the check.
  43. ///
  44. /// See \c setRule for constraints on the rule.
  45. TransformerClangTidyCheck(
  46. std::function<std::optional<transformer::RewriteRuleWith<std::string>>(
  47. const LangOptions &, const OptionsView &)>
  48. MakeRule,
  49. StringRef Name, ClangTidyContext *Context);
  50. /// Convenience overload of the constructor when the rule doesn't have any
  51. /// dependencies.
  52. TransformerClangTidyCheck(transformer::RewriteRuleWith<std::string> R,
  53. StringRef Name, ClangTidyContext *Context);
  54. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  55. Preprocessor *ModuleExpanderPP) override;
  56. void registerMatchers(ast_matchers::MatchFinder *Finder) final;
  57. void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
  58. /// Derived classes that override this function should call this method from
  59. /// the overridden method.
  60. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  61. /// Set the rule that this check implements. All cases in the rule must have
  62. /// a non-null \c Explanation, even though \c Explanation is optional for
  63. /// RewriteRule in general. Because the primary purpose of clang-tidy checks
  64. /// is to provide users with diagnostics, we assume that a missing explanation
  65. /// is a bug. If no explanation is desired, indicate that explicitly (for
  66. /// example, by passing `text("no explanation")` to `makeRule` as the
  67. /// `Explanation` argument).
  68. void setRule(transformer::RewriteRuleWith<std::string> R);
  69. private:
  70. transformer::RewriteRuleWith<std::string> Rule;
  71. IncludeInserter Inserter;
  72. };
  73. } // namespace clang::tidy::utils
  74. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H