RenamerClangTidyCheck.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //===--- RenamerClangTidyCheck.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_RENAMERCLANGTIDYCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/DenseSet.h"
  13. #include "llvm/ADT/FunctionExtras.h"
  14. #include <optional>
  15. #include <string>
  16. #include <utility>
  17. namespace clang {
  18. class MacroInfo;
  19. namespace tidy {
  20. /// Base class for clang-tidy checks that want to flag declarations and/or
  21. /// macros for renaming based on customizable criteria.
  22. class RenamerClangTidyCheck : public ClangTidyCheck {
  23. public:
  24. RenamerClangTidyCheck(StringRef CheckName, ClangTidyContext *Context);
  25. ~RenamerClangTidyCheck();
  26. /// Derived classes should not implement any matching logic themselves; this
  27. /// class will do the matching and call the derived class'
  28. /// getDeclFailureInfo() and getMacroFailureInfo() for determining whether a
  29. /// given identifier passes or fails the check.
  30. void registerMatchers(ast_matchers::MatchFinder *Finder) final;
  31. void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
  32. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  33. Preprocessor *ModuleExpanderPP) final;
  34. void onEndOfTranslationUnit() final;
  35. /// Derived classes that override this function should call this method from
  36. /// the overridden method.
  37. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  38. /// This enum will be used in %select of the diagnostic message.
  39. /// Each value below IgnoreFailureThreshold should have an error message.
  40. enum class ShouldFixStatus {
  41. ShouldFix,
  42. /// The fixup will conflict with a language keyword,
  43. /// so we can't fix it automatically.
  44. ConflictsWithKeyword,
  45. /// The fixup will conflict with a macro
  46. /// definition, so we can't fix it
  47. /// automatically.
  48. ConflictsWithMacroDefinition,
  49. /// The fixup results in an identifier that is not a valid c/c++ identifier.
  50. FixInvalidIdentifier,
  51. /// Values pass this threshold will be ignored completely
  52. /// i.e no message, no fixup.
  53. IgnoreFailureThreshold,
  54. /// If the identifier was used or declared within a macro we
  55. /// won't offer a fixup for safety reasons.
  56. InsideMacro,
  57. };
  58. /// Information describing a failed check
  59. struct FailureInfo {
  60. std::string KindName; // Tag or misc info to be used as derived classes need
  61. std::string Fixup; // The name that will be proposed as a fix-it hint
  62. };
  63. /// Holds an identifier name check failure, tracking the kind of the
  64. /// identifier, its possible fixup and the starting locations of all the
  65. /// identifier usages.
  66. struct NamingCheckFailure {
  67. FailureInfo Info;
  68. /// Whether the failure should be fixed or not.
  69. ///
  70. /// e.g.: if the identifier was used or declared within a macro we won't
  71. /// offer a fixup for safety reasons.
  72. bool shouldFix() const {
  73. return FixStatus == ShouldFixStatus::ShouldFix && !Info.Fixup.empty();
  74. }
  75. bool shouldNotify() const {
  76. return FixStatus < ShouldFixStatus::IgnoreFailureThreshold;
  77. }
  78. ShouldFixStatus FixStatus = ShouldFixStatus::ShouldFix;
  79. /// A set of all the identifier usages starting SourceLocation.
  80. llvm::DenseSet<SourceLocation> RawUsageLocs;
  81. NamingCheckFailure() = default;
  82. };
  83. using NamingCheckId = std::pair<SourceLocation, StringRef>;
  84. using NamingCheckFailureMap =
  85. llvm::DenseMap<NamingCheckId, NamingCheckFailure>;
  86. /// Check Macros for style violations.
  87. void checkMacro(const SourceManager &SourceMgr, const Token &MacroNameTok,
  88. const MacroInfo *MI);
  89. /// Add a usage of a macro if it already has a violation.
  90. void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
  91. void addUsage(const RenamerClangTidyCheck::NamingCheckId &Decl,
  92. SourceRange Range, const SourceManager *SourceMgr = nullptr);
  93. /// Convenience method when the usage to be added is a NamedDecl.
  94. void addUsage(const NamedDecl *Decl, SourceRange Range,
  95. const SourceManager *SourceMgr = nullptr);
  96. void checkNamedDecl(const NamedDecl *Decl, const SourceManager &SourceMgr);
  97. protected:
  98. /// Overridden by derived classes, returns information about if and how a Decl
  99. /// failed the check. A 'std::nullopt' result means the Decl did not fail the
  100. /// check.
  101. virtual std::optional<FailureInfo>
  102. getDeclFailureInfo(const NamedDecl *Decl, const SourceManager &SM) const = 0;
  103. /// Overridden by derived classes, returns information about if and how a
  104. /// macro failed the check. A 'std::nullopt' result means the macro did not
  105. /// fail the check.
  106. virtual std::optional<FailureInfo>
  107. getMacroFailureInfo(const Token &MacroNameTok,
  108. const SourceManager &SM) const = 0;
  109. /// Represents customized diagnostic text and how arguments should be applied.
  110. /// Example usage:
  111. ///
  112. /// return DiagInfo{"my %1 very %2 special %3 text",
  113. /// [=](DiagnosticBuilder &diag) {
  114. /// diag << arg1 << arg2 << arg3;
  115. /// }};
  116. struct DiagInfo {
  117. std::string Text;
  118. llvm::unique_function<void(DiagnosticBuilder &)> ApplyArgs;
  119. };
  120. /// Overridden by derived classes, returns a description of the diagnostic
  121. /// that should be emitted for the given failure. The base class will then
  122. /// further customize the diagnostic by adding info about whether the fix-it
  123. /// can be automatically applied or not.
  124. virtual DiagInfo getDiagInfo(const NamingCheckId &ID,
  125. const NamingCheckFailure &Failure) const = 0;
  126. private:
  127. NamingCheckFailureMap NamingCheckFailures;
  128. const bool AggressiveDependentMemberLookup;
  129. };
  130. } // namespace tidy
  131. } // namespace clang
  132. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H