Skip to content

Commit 54a6798

Browse files
authored
[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.
1 parent 37575f5 commit 54a6798

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ class RenamerClangTidyCheckPPCallbacks : public PPCallbacks {
169169
return;
170170
if (SM.isWrittenInCommandLineFile(MacroNameTok.getLocation()))
171171
return;
172-
Check->checkMacro(SM, MacroNameTok, Info);
172+
Check->checkMacro(MacroNameTok, Info, SM);
173173
}
174174

175175
/// MacroExpands calls expandMacro for macros in the main file
176176
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
177177
SourceRange /*Range*/,
178178
const MacroArgs * /*Args*/) override {
179-
Check->expandMacro(MacroNameTok, MD.getMacroInfo());
179+
Check->expandMacro(MacroNameTok, MD.getMacroInfo(), SM);
180180
}
181181

182182
private:
@@ -187,7 +187,7 @@ class RenamerClangTidyCheckPPCallbacks : public PPCallbacks {
187187
class RenamerClangTidyVisitor
188188
: public RecursiveASTVisitor<RenamerClangTidyVisitor> {
189189
public:
190-
RenamerClangTidyVisitor(RenamerClangTidyCheck *Check, const SourceManager *SM,
190+
RenamerClangTidyVisitor(RenamerClangTidyCheck *Check, const SourceManager &SM,
191191
bool AggressiveDependentMemberLookup)
192192
: Check(Check), SM(SM),
193193
AggressiveDependentMemberLookup(AggressiveDependentMemberLookup) {}
@@ -258,7 +258,7 @@ class RenamerClangTidyVisitor
258258
// Fix overridden methods
259259
if (const auto *Method = dyn_cast<CXXMethodDecl>(Decl)) {
260260
if (const CXXMethodDecl *Overridden = getOverrideMethod(Method)) {
261-
Check->addUsage(Overridden, Method->getLocation());
261+
Check->addUsage(Overridden, Method->getLocation(), SM);
262262
return true; // Don't try to add the actual decl as a Failure.
263263
}
264264
}
@@ -268,7 +268,7 @@ class RenamerClangTidyVisitor
268268
if (isa<ClassTemplateSpecializationDecl>(Decl))
269269
return true;
270270

271-
Check->checkNamedDecl(Decl, *SM);
271+
Check->checkNamedDecl(Decl, SM);
272272
return true;
273273
}
274274

@@ -385,7 +385,7 @@ class RenamerClangTidyVisitor
385385

386386
private:
387387
RenamerClangTidyCheck *Check;
388-
const SourceManager *SM;
388+
const SourceManager &SM;
389389
const bool AggressiveDependentMemberLookup;
390390
};
391391

@@ -415,7 +415,7 @@ void RenamerClangTidyCheck::registerPPCallbacks(
415415

416416
void RenamerClangTidyCheck::addUsage(
417417
const RenamerClangTidyCheck::NamingCheckId &Decl, SourceRange Range,
418-
const SourceManager *SourceMgr) {
418+
const SourceManager &SourceMgr) {
419419
// Do nothing if the provided range is invalid.
420420
if (Range.isInvalid())
421421
return;
@@ -425,8 +425,7 @@ void RenamerClangTidyCheck::addUsage(
425425
// spelling location to different source locations, and we only want to fix
426426
// the token once, before it is expanded by the macro.
427427
SourceLocation FixLocation = Range.getBegin();
428-
if (SourceMgr)
429-
FixLocation = SourceMgr->getSpellingLoc(FixLocation);
428+
FixLocation = SourceMgr.getSpellingLoc(FixLocation);
430429
if (FixLocation.isInvalid())
431430
return;
432431

@@ -440,15 +439,15 @@ void RenamerClangTidyCheck::addUsage(
440439
if (!Failure.shouldFix())
441440
return;
442441

443-
if (SourceMgr && SourceMgr->isWrittenInScratchSpace(FixLocation))
442+
if (SourceMgr.isWrittenInScratchSpace(FixLocation))
444443
Failure.FixStatus = RenamerClangTidyCheck::ShouldFixStatus::InsideMacro;
445444

446-
if (!utils::rangeCanBeFixed(Range, SourceMgr))
445+
if (!utils::rangeCanBeFixed(Range, &SourceMgr))
447446
Failure.FixStatus = RenamerClangTidyCheck::ShouldFixStatus::InsideMacro;
448447
}
449448

450449
void RenamerClangTidyCheck::addUsage(const NamedDecl *Decl, SourceRange Range,
451-
const SourceManager *SourceMgr) {
450+
const SourceManager &SourceMgr) {
452451
// Don't keep track for non-identifier names.
453452
auto *II = Decl->getIdentifier();
454453
if (!II)
@@ -489,18 +488,24 @@ void RenamerClangTidyCheck::checkNamedDecl(const NamedDecl *Decl,
489488
}
490489

491490
Failure.Info = std::move(Info);
492-
addUsage(Decl, Range, &SourceMgr);
491+
addUsage(Decl, Range, SourceMgr);
493492
}
494493

495494
void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
496-
RenamerClangTidyVisitor Visitor(this, Result.SourceManager,
495+
if (!Result.SourceManager) {
496+
// In principle SourceManager is not null but going only by the definition
497+
// of MatchResult it must be handled. Cannot rename anything without a
498+
// SourceManager.
499+
return;
500+
}
501+
RenamerClangTidyVisitor Visitor(this, *Result.SourceManager,
497502
AggressiveDependentMemberLookup);
498503
Visitor.TraverseAST(*Result.Context);
499504
}
500505

501-
void RenamerClangTidyCheck::checkMacro(const SourceManager &SourceMgr,
502-
const Token &MacroNameTok,
503-
const MacroInfo *MI) {
506+
void RenamerClangTidyCheck::checkMacro(const Token &MacroNameTok,
507+
const MacroInfo *MI,
508+
const SourceManager &SourceMgr) {
504509
std::optional<FailureInfo> MaybeFailure =
505510
getMacroFailureInfo(MacroNameTok, SourceMgr);
506511
if (!MaybeFailure)
@@ -515,11 +520,12 @@ void RenamerClangTidyCheck::checkMacro(const SourceManager &SourceMgr,
515520
Failure.FixStatus = ShouldFixStatus::FixInvalidIdentifier;
516521

517522
Failure.Info = std::move(Info);
518-
addUsage(ID, Range);
523+
addUsage(ID, Range, SourceMgr);
519524
}
520525

521526
void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
522-
const MacroInfo *MI) {
527+
const MacroInfo *MI,
528+
const SourceManager &SourceMgr) {
523529
StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
524530
NamingCheckId ID(MI->getDefinitionLoc(), Name);
525531

@@ -528,7 +534,7 @@ void RenamerClangTidyCheck::expandMacro(const Token &MacroNameTok,
528534
return;
529535

530536
SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
531-
addUsage(ID, Range);
537+
addUsage(ID, Range, SourceMgr);
532538
}
533539

534540
static std::string

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,19 @@ class RenamerClangTidyCheck : public ClangTidyCheck {
108108
llvm::DenseMap<NamingCheckId, NamingCheckFailure>;
109109

110110
/// Check Macros for style violations.
111-
void checkMacro(const SourceManager &SourceMgr, const Token &MacroNameTok,
112-
const MacroInfo *MI);
111+
void checkMacro(const Token &MacroNameTok, const MacroInfo *MI,
112+
const SourceManager &SourceMgr);
113113

114114
/// Add a usage of a macro if it already has a violation.
115-
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
115+
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI,
116+
const SourceManager &SourceMgr);
116117

117118
void addUsage(const RenamerClangTidyCheck::NamingCheckId &Decl,
118-
SourceRange Range, const SourceManager *SourceMgr = nullptr);
119+
SourceRange Range, const SourceManager &SourceMgr);
119120

120121
/// Convenience method when the usage to be added is a NamedDecl.
121122
void addUsage(const NamedDecl *Decl, SourceRange Range,
122-
const SourceManager *SourceMgr = nullptr);
123+
const SourceManager &SourceMgr);
123124

124125
void checkNamedDecl(const NamedDecl *Decl, const SourceManager &SourceMgr);
125126

0 commit comments

Comments
 (0)