Skip to content

Commit ddbcc10

Browse files
committed
[clang] NFCI: Adopt SourceManager::getFileEntryRefForID()
This commit replaces some calls to the deprecated `FileEntry::getName()` with `FileEntryRef::getName()` by swapping current usages of `SourceManager::getFileEntryForID()` with `SourceManager::getFileEntryRefForID()`. This lowers the number of usages of the deprecated `FileEntry::getName()` from 95 to 50.
1 parent 8271713 commit ddbcc10

31 files changed

+71
-59
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ AST_POLYMORPHIC_MATCHER_REGEX(isExpansionInFileMatching,
300300
return false;
301301
}
302302
auto FileEntry =
303-
SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
303+
SourceManager.getFileEntryRefForID(SourceManager.getFileID(ExpansionLoc));
304304
if (!FileEntry) {
305305
return false;
306306
}

clang/include/clang/Basic/SourceLocation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_BASIC_SOURCELOCATION_H
1515
#define LLVM_CLANG_BASIC_SOURCELOCATION_H
1616

17+
#include "clang/Basic/FileEntry.h"
1718
#include "clang/Basic/LLVM.h"
1819
#include "llvm/ADT/StringRef.h"
1920
#include <cassert>
@@ -356,8 +357,6 @@ class PresumedLoc {
356357
}
357358
};
358359

359-
class FileEntry;
360-
361360
/// A SourceLocation and its associated SourceManager.
362361
///
363362
/// This is useful for argument passing to functions that expect both objects.
@@ -413,6 +412,7 @@ class FullSourceLoc : public SourceLocation {
413412
unsigned getColumnNumber(bool *Invalid = nullptr) const;
414413

415414
const FileEntry *getFileEntry() const;
415+
OptionalFileEntryRef getFileEntryRef() const;
416416

417417
/// Return a StringRef to the source buffer data for the
418418
/// specified FileID.

clang/lib/ARCMigrate/ARCMT.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ bool MigrationProcess::applyTransform(TransformFn trans,
597597
I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
598598
FileID FID = I->first;
599599
RewriteBuffer &buf = I->second;
600-
const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
600+
OptionalFileEntryRef file =
601+
Ctx.getSourceManager().getFileEntryRefForID(FID);
601602
assert(file);
602603
std::string newFname = std::string(file->getName());
603604
newFname += "-trans";

clang/lib/ARCMigrate/ObjCMT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ class JSONEditWriter : public edit::EditsReceiver {
17851785
std::tie(FID, Offset) = SourceMgr.getDecomposedLoc(Loc);
17861786
assert(FID.isValid());
17871787
SmallString<200> Path =
1788-
StringRef(SourceMgr.getFileEntryForID(FID)->getName());
1788+
StringRef(SourceMgr.getFileEntryRefForID(FID)->getName());
17891789
llvm::sys::fs::make_absolute(Path);
17901790
OS << " \"file\": \"";
17911791
OS.write_escaped(Path.str()) << "\",\n";

clang/lib/ARCMigrate/PlistReporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void arcmt::writeARCDiagsToPlist(const std::string &outPath,
7272
" <array>\n";
7373

7474
for (FileID FID : Fids)
75-
EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
75+
EmitString(o << " ", SM.getFileEntryRefForID(FID)->getName()) << '\n';
7676

7777
o << " </array>\n"
7878
" <key>diagnostics</key>\n"

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ MicrosoftMangleContextImpl::MicrosoftMangleContextImpl(ASTContext &Context,
483483
// The generated names are intended to look similar to what MSVC generates,
484484
// which are something like "?A0x01234567@".
485485
SourceManager &SM = Context.getSourceManager();
486-
if (const FileEntry *FE = SM.getFileEntryForID(SM.getMainFileID())) {
486+
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID())) {
487487
// Truncate the hash so we get 8 characters of hexadecimal.
488488
uint32_t TruncatedHash = uint32_t(xxh3_64bits(FE->getName()));
489489
AnonymousNamespaceHash = llvm::utohexstr(TruncatedHash);

clang/lib/Analysis/PathDiagnostic.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,9 @@ static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) {
336336
std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs);
337337
if (InSameTU.first)
338338
return XL.isBeforeInTranslationUnitThan(YL);
339-
const FileEntry *XFE = SM.getFileEntryForID(XL.getSpellingLoc().getFileID());
340-
const FileEntry *YFE = SM.getFileEntryForID(YL.getSpellingLoc().getFileID());
339+
OptionalFileEntryRef XFE =
340+
SM.getFileEntryRefForID(XL.getSpellingLoc().getFileID());
341+
OptionalFileEntryRef YFE = SM.getFileEntryRefForID(YL.getSpellingLoc().getFileID());
341342
if (!XFE || !YFE)
342343
return XFE && !YFE;
343344
int NameCmp = XFE->getName().compare(YFE->getName());

clang/lib/Basic/SourceLocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ const FileEntry *FullSourceLoc::getFileEntry() const {
227227
return SrcMgr->getFileEntryForID(getFileID());
228228
}
229229

230+
OptionalFileEntryRef FullSourceLoc::getFileEntryRef() const {
231+
assert(isValid());
232+
return SrcMgr->getFileEntryRefForID(getFileID());
233+
}
234+
230235
unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
231236
assert(isValid());
232237
return SrcMgr->getExpansionLineNumber(*this, Invalid);

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
10181018

10191019
/// Return the filename of the file containing a SourceLocation.
10201020
StringRef SourceManager::getFilename(SourceLocation SpellingLoc) const {
1021-
if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
1021+
if (OptionalFileEntryRef F = getFileEntryRefForID(getFileID(SpellingLoc)))
10221022
return F->getName();
10231023
return StringRef();
10241024
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,7 +3215,7 @@ bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
32153215
return true;
32163216
// NoSanitize by location. Check "mainfile" prefix.
32173217
auto &SM = Context.getSourceManager();
3218-
const FileEntry &MainFile = *SM.getFileEntryForID(SM.getMainFileID());
3218+
FileEntryRef MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
32193219
if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
32203220
return true;
32213221

@@ -3236,7 +3236,7 @@ bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
32363236
return true;
32373237
auto &SM = Context.getSourceManager();
32383238
if (NoSanitizeL.containsMainFile(
3239-
Kind, SM.getFileEntryForID(SM.getMainFileID())->getName(), Category))
3239+
Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(), Category))
32403240
return true;
32413241
if (NoSanitizeL.containsLocation(Kind, Loc, Category))
32423242
return true;
@@ -3302,7 +3302,7 @@ CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
33023302
// If location is unknown, this may be a compiler-generated function. Assume
33033303
// it's located in the main file.
33043304
auto &SM = Context.getSourceManager();
3305-
if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
3305+
if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID()))
33063306
if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
33073307
return *V;
33083308
return ProfileList.getDefault(Kind);

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,8 @@ StringRef ASTUnit::getMainFileName() const {
14931493
}
14941494

14951495
if (SourceMgr) {
1496-
if (const FileEntry *
1497-
FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
1496+
if (OptionalFileEntryRef FE =
1497+
SourceMgr->getFileEntryRefForID(SourceMgr->getMainFileID()))
14981498
return FE->getName();
14991499
}
15001500

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
666666
} else {
667667
auto &OldSM = AST->getSourceManager();
668668
FileID ID = OldSM.getMainFileID();
669-
if (auto *File = OldSM.getFileEntryForID(ID))
669+
if (auto File = OldSM.getFileEntryRefForID(ID))
670670
Input = FrontendInputFile(File->getName(), Kind);
671671
else
672672
Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
@@ -844,7 +844,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
844844
return false;
845845
}
846846
// We now have the filename...
847-
FileName = FE->getFileEntry().getName();
847+
FileName = FE->getName();
848848
// ... still a header unit, but now use the path as written.
849849
Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
850850
Input = FrontendInputFile(FileName, Kind, Input.isSystem());

clang/lib/Frontend/HeaderIncludeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
259259
}
260260

261261
void HeaderIncludesJSONCallback::EndOfMainFile() {
262-
const FileEntry *FE = SM.getFileEntryForID(SM.getMainFileID());
262+
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID());
263263
SmallString<256> MainFile(FE->getName());
264264
SM.getFileManager().makeAbsolutePath(MainFile);
265265

clang/lib/Frontend/LogDiagnosticPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
118118
const SourceManager &SM = Info.getSourceManager();
119119
FileID FID = SM.getMainFileID();
120120
if (FID.isValid()) {
121-
if (const FileEntry *FE = SM.getFileEntryForID(FID))
121+
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID))
122122
MainFilename = std::string(FE->getName());
123123
}
124124
}
@@ -147,7 +147,7 @@ void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
147147
// At least print the file name if available:
148148
FileID FID = SM.getFileID(Info.getLocation());
149149
if (FID.isValid()) {
150-
if (const FileEntry *FE = SM.getFileEntryForID(FID))
150+
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID))
151151
DE.Filename = std::string(FE->getName());
152152
}
153153
} else {

clang/lib/Frontend/PrecompiledPreamble.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,19 +550,19 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
550550

551551
SourceManager &SourceMgr = Clang->getSourceManager();
552552
for (auto &Filename : PreambleDepCollector->getDependencies()) {
553-
auto FileOrErr = Clang->getFileManager().getFile(Filename);
554-
if (!FileOrErr ||
555-
*FileOrErr == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
553+
auto MaybeFile = Clang->getFileManager().getOptionalFileRef(Filename);
554+
if (!MaybeFile ||
555+
MaybeFile == SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID()))
556556
continue;
557-
auto File = *FileOrErr;
558-
if (time_t ModTime = File->getModificationTime()) {
559-
FilesInPreamble[File->getName()] =
560-
PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
557+
auto File = *MaybeFile;
558+
if (time_t ModTime = File.getModificationTime()) {
559+
FilesInPreamble[File.getName()] =
560+
PrecompiledPreamble::PreambleFileHash::createForFile(File.getSize(),
561561
ModTime);
562562
} else {
563563
llvm::MemoryBufferRef Buffer =
564564
SourceMgr.getMemoryBufferForFileOrFake(File);
565-
FilesInPreamble[File->getName()] =
565+
FilesInPreamble[File.getName()] =
566566
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
567567
}
568568
}

clang/lib/Frontend/Rewrite/FixItRewriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ bool FixItRewriter::WriteFixedFiles(
9393
}
9494

9595
for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
96-
const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
96+
OptionalFileEntryRef Entry =
97+
Rewrite.getSourceMgr().getFileEntryRefForID(I->first);
9798
int fd;
9899
std::string Filename =
99100
FixItOpts->RewriteFilename(std::string(Entry->getName()), fd);

clang/lib/Frontend/Rewrite/HTMLPrint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
6262

6363
// Format the file.
6464
FileID FID = R.getSourceMgr().getMainFileID();
65-
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
65+
OptionalFileEntryRef Entry = R.getSourceMgr().getFileEntryRefForID(FID);
6666
StringRef Name;
6767
// In some cases, in particular the case where the input is from stdin,
6868
// there is no entry. Fall back to the memory buffer for a name in those

clang/lib/Frontend/SARIFDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SarifResult SARIFDiagnostic::addLocationToResult(
7070
// At least add the file name if available:
7171
FileID FID = Loc.getFileID();
7272
if (FID.isValid()) {
73-
if (const FileEntry *FE = Loc.getFileEntry()) {
73+
if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
7474
emitFilename(FE->getName(), Loc.getManager());
7575
// FIXME(llvm-project/issues/57366): File-only locations
7676
}

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
779779
if (PLoc.isInvalid()) {
780780
// At least print the file name if available:
781781
if (FileID FID = Loc.getFileID(); FID.isValid()) {
782-
if (const FileEntry *FE = Loc.getFileEntry()) {
782+
if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
783783
emitFilename(FE->getName(), Loc.getManager());
784784
OS << ": ";
785785
}

clang/lib/Frontend/VerifyDiagnosticConsumer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,8 @@ static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceM
868868
OS << "\n (frontend)";
869869
else {
870870
OS << "\n ";
871-
if (const FileEntry *File = SourceMgr->getFileEntryForID(
872-
SourceMgr->getFileID(I->first)))
871+
if (OptionalFileEntryRef File =
872+
SourceMgr->getFileEntryRefForID(SourceMgr->getFileID(I->first)))
873873
OS << " File " << File->getName();
874874
OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
875875
}

clang/lib/Index/CommentToXML.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) {
891891
unsigned FileOffset = LocInfo.second;
892892

893893
if (FID.isValid()) {
894-
if (const FileEntry *FE = SM.getFileEntryForID(FID)) {
894+
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID)) {
895895
Result << " file=\"";
896896
appendToResultWithXMLEscaping(FE->getName());
897897
Result << "\"";

clang/lib/Index/USRGeneration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
3131
}
3232
Loc = SM.getExpansionLoc(Loc);
3333
const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
34-
const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
34+
OptionalFileEntryRef FE = SM.getFileEntryRefForID(Decomposed.first);
3535
if (FE) {
3636
OS << llvm::sys::path::filename(FE->getName());
3737
} else {

clang/lib/Rewrite/Rewriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ bool Rewriter::overwriteChangedFiles() {
412412
unsigned OverwriteFailure = Diag.getCustomDiagID(
413413
DiagnosticsEngine::Error, "unable to overwrite file %0: %1");
414414
for (buffer_iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
415-
const FileEntry *Entry = getSourceMgr().getFileEntryForID(I->first);
415+
OptionalFileEntryRef Entry = getSourceMgr().getFileEntryRefForID(I->first);
416416
if (auto Error =
417417
llvm::writeToOutput(Entry->getName(), [&](llvm::raw_ostream &OS) {
418418
I->second.write(OS);

clang/lib/Sema/Sema.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ class SemaPPCallbacks : public PPCallbacks {
152152
SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc));
153153
if (IncludeLoc.isValid()) {
154154
if (llvm::timeTraceProfilerEnabled()) {
155-
const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc));
156-
llvm::timeTraceProfilerBegin(
157-
"Source", FE != nullptr ? FE->getName() : StringRef("<unknown>"));
155+
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getFileID(Loc));
156+
llvm::timeTraceProfilerBegin("Source", FE ? FE->getName()
157+
: StringRef("<unknown>"));
158158
}
159159

160160
IncludeStack.push_back(IncludeLoc);

clang/lib/Sema/SemaModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ void Sema::HandleStartOfHeaderUnit() {
103103

104104
StringRef HUName = getLangOpts().CurrentModule;
105105
if (HUName.empty()) {
106-
HUName = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())->getName();
106+
HUName =
107+
SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName();
107108
const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
108109
}
109110

0 commit comments

Comments
 (0)