@@ -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
182182private:
@@ -187,7 +187,7 @@ class RenamerClangTidyCheckPPCallbacks : public PPCallbacks {
187187class RenamerClangTidyVisitor
188188 : public RecursiveASTVisitor<RenamerClangTidyVisitor> {
189189public:
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
386386private:
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
416416void 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
450449void 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
495494void 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
521526void 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
534540static std::string
0 commit comments