aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwin Vane <revane@google.com>2024-04-12 12:49:25 -0400
committerGitHub <noreply@github.com>2024-04-12 12:49:25 -0400
commit54a6798e0a3630e705ed32dbbd63414a16331085 (patch)
tree2f94ada16c6929bf7884dd98d612d36a72131ac7
parent37575f5262d0e37a1e0b10c59fb8f59182cfe951 (diff)
downloadllvm-54a6798e0a3630e705ed32dbbd63414a16331085.zip
llvm-54a6798e0a3630e705ed32dbbd63414a16331085.tar.gz
llvm-54a6798e0a3630e705ed32dbbd63414a16331085.tar.bz2
[clang-tidy] Simplify RenamerClangTidyCheck API (#88268)
Some functions allow a null SourceManager, no SourceManager, or a SourceManager in an inconsistent argument position. Since SourceManager is generally not null and it doesn't make sense to apply renaming without one, these inconsistencies are now gone.
-rw-r--r--clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp46
-rw-r--r--clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h11
2 files changed, 32 insertions, 25 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
index ad8048e..962a243 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -169,14 +169,14 @@ public:
return;
if (SM.isWrittenInCommandLineFile(MacroNameTok.getLocation()))
return;
- Check->checkMacro(SM, MacroNameTok, Info);
+ Check->checkMacro(MacroNameTok, Info, SM);
}
/// MacroExpands calls expandMacro for macros in the main file
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
SourceRange /*Range*/,
const MacroArgs * /*Args*/) override {
- Check->expandMacro(MacroNameTok, MD.getMacroInfo());
+ Check->expandMacro(MacroNameTok, MD.getMacroInfo(), SM);
}
private:
@@ -187,7 +187,7 @@ private:
class RenamerClangTidyVisitor
: public RecursiveASTVisitor<RenamerClangTidyVisitor> {
public:
- RenamerClangTidyVisitor(RenamerClangTidyCheck *Check, const SourceManager *SM,
+ RenamerClangTidyVisitor(RenamerClangTidyCheck *Check, const SourceManager &SM,
bool AggressiveDependentMemberLookup)
: Check(Check), SM(SM),
AggressiveDependentMemberLookup(AggressiveDependentMemberLookup) {}
@@ -258,7 +258,7 @@ public:
// Fix overridden methods
if (const auto *Method = dyn_cast<CXXMethodDecl>(Decl)) {
if (const CXXMethodDecl *Overridden = getOverrideMethod(Method)) {
- Check->addUsage(Overridden, Method->getLocation());
+ Check->addUsage(Overridden, Method->getLocation(), SM);
return true; // Don't try to add the actual decl as a Failure.
}
}
@@ -268,7 +268,7 @@ public:
if (isa<ClassTemplateSpecializationDecl>(Decl))
return true;
- Check->checkNamedDecl(Decl, *SM);
+ Check->checkNamedDecl(Decl, SM);
return true;
}
@@ -385,7 +385,7 @@ public:
private:
RenamerClangTidyCheck *Check;
- const SourceManager *SM;
+ const SourceManager &SM;
const bool AggressiveDependentMemberLookup;
};
@@ -415,7 +415,7 @@ void RenamerClangTidyCheck::registerPPCallbacks(
void RenamerClangTidyCheck::addUsage(
const RenamerClangTidyCheck::NamingCheckId &Decl, SourceRange Range,
- const SourceManager *SourceMgr) {
+ const SourceManager &SourceMgr) {
// Do nothing if the provided range is invalid.
if (Range.isInvalid())
return;
@@ -425,8 +425,7 @@ void RenamerClangTidyCheck::addUsage(
// spelling location to different source locations, and we only want to fix
// the token once, before it is expanded by the macro.
SourceLocation FixLocation = Range.getBegin();
- if (SourceMgr)
- FixLocation = SourceMgr->getSpellingLoc(FixLocation);
+ FixLocation = SourceMgr.getSpellingLoc(FixLocation);
if (FixLocation.isInvalid())
return;
@@ -440,15 +439,15 @@ void RenamerClangTidyCheck::addUsage(
if (!Failure.shouldFix())
return;
- if (SourceMgr && SourceMgr->isWrittenInScratchSpace(FixLocation))
+ if (SourceMgr.isWrittenInScratchSpace(FixLocation))
Failure.FixStatus = RenamerClangTidyCheck::ShouldFixStatus::InsideMacro;
- if (!utils::rangeCanBeFixed(Range, SourceMgr))
+ if (!utils::rangeCanBeFixed(Range, &SourceMgr))
Failure.FixStatus = RenamerClangTidyCheck::ShouldFixStatus::InsideMacro;
}
void RenamerClangTidyCheck::addUsage(const NamedDecl *Decl, SourceRange Range,
- const SourceManager *SourceMgr) {
+ const SourceManager &SourceMgr) {
// Don't keep track for non-identifier names.
auto *II = Decl->getIdentifier();
if (!II)
@@ -489,18 +488,24 @@ void RenamerClangTidyCheck::checkNamedDecl(const NamedDecl *Decl,
}
Failure.Info = std::move(Info);
- addUsage(Decl, Range, &SourceMgr);
+ addUsage(Decl, Range, SourceMgr);
}
void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
- RenamerClangTidyVisitor Visitor(this, Result.SourceManager,
+ if (!Result.SourceManager) {
+ // In principle SourceManager is not null but going only by the definition
+ // of MatchResult it must be handled. Cannot rename anything without a
+ // SourceManager.
+ return;
+ }
+ RenamerClangTidyVisitor Visitor(this, *Result.SourceManager,
AggressiveDependentMemberLookup);
Visitor.TraverseAST(*Result.Context);
}
-void RenamerClangTidyCheck::checkMacro(const SourceManager &SourceMgr,
- const Token &MacroNameTok,
- const MacroInfo *MI) {
+void RenamerClangTidyCheck::checkMacro(const Token &MacroNameTok,
+ const MacroInfo *MI,
+ const SourceManager &SourceMgr) {
std::optional<FailureInfo> MaybeFailure =
getMacroFailureInfo(MacroNameTok, SourceMgr);
if (!MaybeFailure)
@@ -515,11 +520,12 @@ void RenamerClangTidyCheck::checkMacro(const SourceManager &SourceMgr,
Failure.FixStatus = ShouldFixStatus::FixInvalidIdentifier;
Failure.Info = std::move(Info);
- addUsage(ID, Range);
+ addUsage(ID, Range, SourceMgr);
}
void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
- const MacroInfo *MI) {
+ const MacroInfo *MI,
+ const SourceManager &SourceMgr) {
StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
NamingCheckId ID(MI->getDefinitionLoc(), Name);
@@ -528,7 +534,7 @@ void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
return;
SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
- addUsage(ID, Range);
+ addUsage(ID, Range, SourceMgr);
}
static std::string
diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
index 38228fb..be5b6f0 100644
--- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
+++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h
@@ -108,18 +108,19 @@ public:
llvm::DenseMap<NamingCheckId, NamingCheckFailure>;
/// Check Macros for style violations.
- void checkMacro(const SourceManager &SourceMgr, const Token &MacroNameTok,
- const MacroInfo *MI);
+ void checkMacro(const Token &MacroNameTok, const MacroInfo *MI,
+ const SourceManager &SourceMgr);
/// Add a usage of a macro if it already has a violation.
- void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
+ void expandMacro(const Token &MacroNameTok, const MacroInfo *MI,
+ const SourceManager &SourceMgr);
void addUsage(const RenamerClangTidyCheck::NamingCheckId &Decl,
- SourceRange Range, const SourceManager *SourceMgr = nullptr);
+ SourceRange Range, const SourceManager &SourceMgr);
/// Convenience method when the usage to be added is a NamedDecl.
void addUsage(const NamedDecl *Decl, SourceRange Range,
- const SourceManager *SourceMgr = nullptr);
+ const SourceManager &SourceMgr);
void checkNamedDecl(const NamedDecl *Decl, const SourceManager &SourceMgr);