File indexing completed on 2026-05-10 08:36:22
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H
0011
0012 #include "../ClangTidyCheck.h"
0013 #include "llvm/ADT/DenseMap.h"
0014 #include "llvm/ADT/DenseSet.h"
0015 #include "llvm/ADT/FunctionExtras.h"
0016 #include <optional>
0017 #include <string>
0018 #include <utility>
0019
0020 namespace clang {
0021
0022 class MacroInfo;
0023
0024 namespace tidy {
0025
0026
0027
0028 class RenamerClangTidyCheck : public ClangTidyCheck {
0029 public:
0030 RenamerClangTidyCheck(StringRef CheckName, ClangTidyContext *Context);
0031 ~RenamerClangTidyCheck();
0032
0033
0034
0035
0036
0037 void registerMatchers(ast_matchers::MatchFinder *Finder) final;
0038 void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
0039 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0040 Preprocessor *ModuleExpanderPP) final;
0041 void onEndOfTranslationUnit() final;
0042
0043
0044
0045 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0046
0047
0048
0049 enum class ShouldFixStatus {
0050 ShouldFix,
0051
0052
0053
0054 ConflictsWithKeyword,
0055
0056
0057
0058
0059 ConflictsWithMacroDefinition,
0060
0061
0062 FixInvalidIdentifier,
0063
0064
0065
0066 IgnoreFailureThreshold,
0067
0068
0069
0070 InsideMacro,
0071 };
0072
0073
0074 struct FailureInfo {
0075 std::string KindName;
0076 std::string Fixup;
0077 };
0078
0079
0080
0081
0082 struct NamingCheckFailure {
0083 FailureInfo Info;
0084
0085
0086
0087
0088
0089 bool shouldFix() const {
0090 return FixStatus == ShouldFixStatus::ShouldFix && !Info.Fixup.empty();
0091 }
0092
0093 bool shouldNotify() const {
0094 return FixStatus < ShouldFixStatus::IgnoreFailureThreshold;
0095 }
0096
0097 ShouldFixStatus FixStatus = ShouldFixStatus::ShouldFix;
0098
0099
0100 llvm::DenseSet<SourceLocation> RawUsageLocs;
0101
0102 NamingCheckFailure() = default;
0103 };
0104
0105 using NamingCheckId = std::pair<SourceLocation, StringRef>;
0106
0107 using NamingCheckFailureMap =
0108 llvm::DenseMap<NamingCheckId, NamingCheckFailure>;
0109
0110
0111 void checkMacro(const Token &MacroNameTok, const MacroInfo *MI,
0112 const SourceManager &SourceMgr);
0113
0114
0115 void expandMacro(const Token &MacroNameTok, const MacroInfo *MI,
0116 const SourceManager &SourceMgr);
0117
0118 void addUsage(const NamedDecl *Decl, SourceRange Range,
0119 const SourceManager &SourceMgr);
0120
0121 protected:
0122
0123
0124
0125 virtual std::optional<FailureInfo>
0126 getDeclFailureInfo(const NamedDecl *Decl, const SourceManager &SM) const = 0;
0127
0128
0129
0130
0131 virtual std::optional<FailureInfo>
0132 getMacroFailureInfo(const Token &MacroNameTok,
0133 const SourceManager &SM) const = 0;
0134
0135
0136
0137
0138
0139
0140
0141
0142 struct DiagInfo {
0143 std::string Text;
0144 llvm::unique_function<void(DiagnosticBuilder &)> ApplyArgs;
0145 };
0146
0147
0148
0149
0150
0151 virtual DiagInfo getDiagInfo(const NamingCheckId &ID,
0152 const NamingCheckFailure &Failure) const = 0;
0153
0154 private:
0155
0156
0157
0158
0159 std::pair<NamingCheckFailureMap::iterator, bool>
0160 addUsage(const RenamerClangTidyCheck::NamingCheckId &FailureId,
0161 SourceRange UsageRange, const SourceManager &SourceMgr);
0162
0163 NamingCheckFailureMap NamingCheckFailures;
0164 const bool AggressiveDependentMemberLookup;
0165 };
0166
0167 }
0168 }
0169
0170 #endif