clang-tools  14.0.0git
RenamerClangTidyCheck.h
Go to the documentation of this file.
1 //===--- RenamderClangTidyCheck.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 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/FunctionExtras.h"
16 #include "llvm/ADT/Optional.h"
17 #include <string>
18 #include <utility>
19 
20 namespace clang {
21 
22 class MacroInfo;
23 
24 namespace tidy {
25 
26 /// Base class for clang-tidy checks that want to flag declarations and/or
27 /// macros for renaming based on customizable criteria.
29 public:
30  RenamerClangTidyCheck(StringRef CheckName, ClangTidyContext *Context);
32 
33  /// Derived classes should not implement any matching logic themselves; this
34  /// class will do the matching and call the derived class'
35  /// GetDeclFailureInfo() and GetMacroFailureInfo() for determining whether a
36  /// given identifier passes or fails the check.
37  void registerMatchers(ast_matchers::MatchFinder *Finder) override final;
38  void
39  check(const ast_matchers::MatchFinder::MatchResult &Result) override final;
40  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
41  Preprocessor *ModuleExpanderPP) override final;
42  void onEndOfTranslationUnit() override final;
43 
44  /// Derived classes that override this function should call this method from
45  /// the overridden method.
46  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
47 
48  /// This enum will be used in %select of the diagnostic message.
49  /// Each value below IgnoreFailureThreshold should have an error message.
50  enum class ShouldFixStatus {
51  ShouldFix,
52 
53  /// The fixup will conflict with a language keyword,
54  /// so we can't fix it automatically.
56 
57  /// The fixup will conflict with a macro
58  /// definition, so we can't fix it
59  /// automatically.
61 
62  /// The fixup results in an identifier that is not a valid c/c++ identifier.
64 
65  /// Values pass this threshold will be ignored completely
66  /// i.e no message, no fixup.
68 
69  /// If the identifier was used or declared within a macro we
70  /// won't offer a fixup for safety reasons.
72  };
73 
74  /// Information describing a failed check
75  struct FailureInfo {
76  std::string KindName; // Tag or misc info to be used as derived classes need
77  std::string Fixup; // The name that will be proposed as a fix-it hint
78  };
79 
80  /// Holds an identifier name check failure, tracking the kind of the
81  /// identifier, its possible fixup and the starting locations of all the
82  /// identifier usages.
85 
86  /// Whether the failure should be fixed or not.
87  ///
88  /// e.g.: if the identifier was used or declared within a macro we won't
89  /// offer a fixup for safety reasons.
90  bool ShouldFix() const {
91  return FixStatus == ShouldFixStatus::ShouldFix && !Info.Fixup.empty();
92  }
93 
94  bool ShouldNotify() const {
96  }
97 
99 
100  /// A set of all the identifier usages starting SourceLocation.
101  llvm::DenseSet<SourceLocation> RawUsageLocs;
102 
103  NamingCheckFailure() = default;
104  };
105 
106  using NamingCheckId = std::pair<SourceLocation, std::string>;
107 
108  using NamingCheckFailureMap =
109  llvm::DenseMap<NamingCheckId, NamingCheckFailure>;
110 
111  /// Check Macros for style violations.
112  void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok,
113  const MacroInfo *MI);
114 
115  /// Add a usage of a macro if it already has a violation.
116  void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
117 
119  SourceRange Range, SourceManager *SourceMgr = nullptr);
120 
121  /// Convenience method when the usage to be added is a NamedDecl.
122  void addUsage(const NamedDecl *Decl, SourceRange Range,
123  SourceManager *SourceMgr = nullptr);
124 
125 protected:
126  /// Overridden by derived classes, returns information about if and how a Decl
127  /// failed the check. A 'None' result means the Decl did not fail the check.
128  virtual llvm::Optional<FailureInfo>
129  GetDeclFailureInfo(const NamedDecl *Decl, const SourceManager &SM) const = 0;
130 
131  /// Overridden by derived classes, returns information about if and how a
132  /// macro failed the check. A 'None' result means the macro did not fail the
133  /// check.
134  virtual llvm::Optional<FailureInfo>
135  GetMacroFailureInfo(const Token &MacroNameTok,
136  const SourceManager &SM) const = 0;
137 
138  /// Represents customized diagnostic text and how arguments should be applied.
139  /// Example usage:
140  ///
141  /// return DiagInfo{"my %1 very %2 special %3 text",
142  /// [=](DiagnosticBuilder &diag) {
143  /// diag << arg1 << arg2 << arg3;
144  /// }};
145  struct DiagInfo {
146  std::string Text;
147  llvm::unique_function<void(DiagnosticBuilder &)> ApplyArgs;
148  };
149 
150  /// Overridden by derived classes, returns a description of the diagnostic
151  /// that should be emitted for the given failure. The base class will then
152  /// further customize the diagnostic by adding info about whether the fix-it
153  /// can be automatically applied or not.
154  virtual DiagInfo GetDiagInfo(const NamingCheckId &ID,
155  const NamingCheckFailure &Failure) const = 0;
156 
157 private:
158  NamingCheckFailureMap NamingCheckFailures;
159  const bool AggressiveDependentMemberLookup;
160 };
161 
162 } // namespace tidy
163 } // namespace clang
164 
165 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_RENAMERCLANGTIDYCHECK_H
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::tidy::RenamerClangTidyCheck::RenamerClangTidyCheck
RenamerClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
Definition: RenamerClangTidyCheck.cpp:101
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::RenamerClangTidyCheck::FailureInfo::KindName
std::string KindName
Definition: RenamerClangTidyCheck.h:76
clang::tidy::RenamerClangTidyCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Derived classes that override this function should call this method from the overridden method.
Definition: RenamerClangTidyCheck.cpp:108
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure::ShouldFix
bool ShouldFix() const
Whether the failure should be fixed or not.
Definition: RenamerClangTidyCheck.h:90
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::RenamerClangTidyCheck::NamingCheckFailureMap
llvm::DenseMap< NamingCheckId, NamingCheckFailure > NamingCheckFailureMap
Definition: RenamerClangTidyCheck.h:109
clang::tidy::RenamerClangTidyCheck::~RenamerClangTidyCheck
~RenamerClangTidyCheck()
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure
Holds an identifier name check failure, tracking the kind of the identifier, its possible fixup and t...
Definition: RenamerClangTidyCheck.h:83
clang::tidy::RenamerClangTidyCheck::DiagInfo
Represents customized diagnostic text and how arguments should be applied.
Definition: RenamerClangTidyCheck.h:145
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:102
clang::tidy::RenamerClangTidyCheck::checkMacro
void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok, const MacroInfo *MI)
Check Macros for style violations.
Definition: RenamerClangTidyCheck.cpp:476
clang::tidy::RenamerClangTidyCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override final
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: RenamerClangTidyCheck.cpp:271
clang::tidy::RenamerClangTidyCheck::GetDiagInfo
virtual DiagInfo GetDiagInfo(const NamingCheckId &ID, const NamingCheckFailure &Failure) const =0
Overridden by derived classes, returns a description of the diagnostic that should be emitted for the...
clang::tidy::RenamerClangTidyCheck::GetDeclFailureInfo
virtual llvm::Optional< FailureInfo > GetDeclFailureInfo(const NamedDecl *Decl, const SourceManager &SM) const =0
Overridden by derived classes, returns information about if and how a Decl failed the check.
clang::tidy::RenamerClangTidyCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override final
Definition: RenamerClangTidyCheck.cpp:527
clang::tidy::RenamerClangTidyCheck::DiagInfo::Text
std::string Text
Definition: RenamerClangTidyCheck.h:146
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure::FixStatus
ShouldFixStatus FixStatus
Definition: RenamerClangTidyCheck.h:98
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus
ShouldFixStatus
This enum will be used in select of the diagnostic message.
Definition: RenamerClangTidyCheck.h:50
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure::ShouldNotify
bool ShouldNotify() const
Definition: RenamerClangTidyCheck.h:94
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus::InsideMacro
@ InsideMacro
If the identifier was used or declared within a macro we won't offer a fixup for safety reasons.
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus::IgnoreFailureThreshold
@ IgnoreFailureThreshold
Values pass this threshold will be ignored completely i.e no message, no fixup.
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure::RawUsageLocs
llvm::DenseSet< SourceLocation > RawUsageLocs
A set of all the identifier usages starting SourceLocation.
Definition: RenamerClangTidyCheck.h:101
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure::Info
FailureInfo Info
Definition: RenamerClangTidyCheck.h:84
clang::tidy::RenamerClangTidyCheck::FailureInfo::Fixup
std::string Fixup
Definition: RenamerClangTidyCheck.h:77
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus::ShouldFix
@ ShouldFix
clang::tidy::RenamerClangTidyCheck::addUsage
void addUsage(const RenamerClangTidyCheck::NamingCheckId &Decl, SourceRange Range, SourceManager *SourceMgr=nullptr)
Definition: RenamerClangTidyCheck.cpp:155
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::RenamerClangTidyCheck::expandMacro
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI)
Add a usage of a macro if it already has a violation.
Definition: RenamerClangTidyCheck.cpp:493
clang::tidy::RenamerClangTidyCheck
Base class for clang-tidy checks that want to flag declarations and/or macros for renaming based on c...
Definition: RenamerClangTidyCheck.h:28
clang::tidy::RenamerClangTidyCheck::NamingCheckId
std::pair< SourceLocation, std::string > NamingCheckId
Definition: RenamerClangTidyCheck.h:106
ID
static char ID
Definition: Logger.cpp:74
clang::tidy::RenamerClangTidyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override final
Derived classes should not implement any matching logic themselves; this class will do the matching a...
Definition: RenamerClangTidyCheck.cpp:113
clang::tidy::RenamerClangTidyCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override final
Override this to register PPCallbacks in the preprocessor.
Definition: RenamerClangTidyCheck.cpp:131
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::RenamerClangTidyCheck::NamingCheckFailure::NamingCheckFailure
NamingCheckFailure()=default
clang::tidy::RenamerClangTidyCheck::DiagInfo::ApplyArgs
llvm::unique_function< void(DiagnosticBuilder &)> ApplyArgs
Definition: RenamerClangTidyCheck.h:147
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus::ConflictsWithMacroDefinition
@ ConflictsWithMacroDefinition
The fixup will conflict with a macro definition, so we can't fix it automatically.
clang::tidy::RenamerClangTidyCheck::FailureInfo
Information describing a failed check.
Definition: RenamerClangTidyCheck.h:75
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus::FixInvalidIdentifier
@ FixInvalidIdentifier
The fixup results in an identifier that is not a valid c/c++ identifier.
clang::tidy::RenamerClangTidyCheck::GetMacroFailureInfo
virtual llvm::Optional< FailureInfo > GetMacroFailureInfo(const Token &MacroNameTok, const SourceManager &SM) const =0
Overridden by derived classes, returns information about if and how a macro failed the check.
clang::tidy::RenamerClangTidyCheck::ShouldFixStatus::ConflictsWithKeyword
@ ConflictsWithKeyword
The fixup will conflict with a language keyword, so we can't fix it automatically.