Reapply "[clang] NFCI: Adopt SourceManager::getFileEntryRefForID()"

This reapplies ddbcc10b9e26b18f6a70e23d0611b9da75ffa52f, except for a tiny part that was reverted separately: 65331da0032ab4253a4bc0ddcb2da67664bd86a9. That will be reapplied later on, since it turned out to be more involved.

This commit is enabled by 5523fefb01c282c4cbcaf6314a9aaf658c6c145f and f0f548a65a215c450d956dbcedb03656449705b9, specifically the part that makes 'clang-tidy/checkers/misc/header-include-cycle.cpp' separator agnostic.
This commit is contained in:
Jan Svoboda 2023-09-08 16:26:02 -07:00
parent a3ba9d697b
commit 523c471250
30 changed files with 72 additions and 58 deletions

View File

@ -300,7 +300,7 @@ AST_POLYMORPHIC_MATCHER_REGEX(isExpansionInFileMatching,
return false;
}
auto FileEntry =
SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
SourceManager.getFileEntryRefForID(SourceManager.getFileID(ExpansionLoc));
if (!FileEntry) {
return false;
}

View File

@ -14,6 +14,7 @@
#ifndef LLVM_CLANG_BASIC_SOURCELOCATION_H
#define LLVM_CLANG_BASIC_SOURCELOCATION_H
#include "clang/Basic/FileEntry.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
#include <cassert>
@ -356,8 +357,6 @@ public:
}
};
class FileEntry;
/// A SourceLocation and its associated SourceManager.
///
/// This is useful for argument passing to functions that expect both objects.
@ -413,6 +412,7 @@ public:
unsigned getColumnNumber(bool *Invalid = nullptr) const;
const FileEntry *getFileEntry() const;
OptionalFileEntryRef getFileEntryRef() const;
/// Return a StringRef to the source buffer data for the
/// specified FileID.

View File

@ -597,7 +597,8 @@ bool MigrationProcess::applyTransform(TransformFn trans,
I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
FileID FID = I->first;
RewriteBuffer &buf = I->second;
const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
OptionalFileEntryRef file =
Ctx.getSourceManager().getFileEntryRefForID(FID);
assert(file);
std::string newFname = std::string(file->getName());
newFname += "-trans";

View File

@ -1785,7 +1785,7 @@ private:
std::tie(FID, Offset) = SourceMgr.getDecomposedLoc(Loc);
assert(FID.isValid());
SmallString<200> Path =
StringRef(SourceMgr.getFileEntryForID(FID)->getName());
StringRef(SourceMgr.getFileEntryRefForID(FID)->getName());
llvm::sys::fs::make_absolute(Path);
OS << " \"file\": \"";
OS.write_escaped(Path.str()) << "\",\n";

View File

@ -72,7 +72,7 @@ void arcmt::writeARCDiagsToPlist(const std::string &outPath,
" <array>\n";
for (FileID FID : Fids)
EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
EmitString(o << " ", SM.getFileEntryRefForID(FID)->getName()) << '\n';
o << " </array>\n"
" <key>diagnostics</key>\n"

View File

@ -483,7 +483,7 @@ MicrosoftMangleContextImpl::MicrosoftMangleContextImpl(ASTContext &Context,
// The generated names are intended to look similar to what MSVC generates,
// which are something like "?A0x01234567@".
SourceManager &SM = Context.getSourceManager();
if (const FileEntry *FE = SM.getFileEntryForID(SM.getMainFileID())) {
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID())) {
// Truncate the hash so we get 8 characters of hexadecimal.
uint32_t TruncatedHash = uint32_t(xxh3_64bits(FE->getName()));
AnonymousNamespaceHash = llvm::utohexstr(TruncatedHash);

View File

@ -336,8 +336,10 @@ static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) {
std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs);
if (InSameTU.first)
return XL.isBeforeInTranslationUnitThan(YL);
const FileEntry *XFE = SM.getFileEntryForID(XL.getSpellingLoc().getFileID());
const FileEntry *YFE = SM.getFileEntryForID(YL.getSpellingLoc().getFileID());
OptionalFileEntryRef XFE =
SM.getFileEntryRefForID(XL.getSpellingLoc().getFileID());
OptionalFileEntryRef YFE =
SM.getFileEntryRefForID(YL.getSpellingLoc().getFileID());
if (!XFE || !YFE)
return XFE && !YFE;
int NameCmp = XFE->getName().compare(YFE->getName());

View File

@ -227,6 +227,11 @@ const FileEntry *FullSourceLoc::getFileEntry() const {
return SrcMgr->getFileEntryForID(getFileID());
}
OptionalFileEntryRef FullSourceLoc::getFileEntryRef() const {
assert(isValid());
return SrcMgr->getFileEntryRefForID(getFileID());
}
unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
assert(isValid());
return SrcMgr->getExpansionLineNumber(*this, Invalid);

View File

@ -1018,7 +1018,7 @@ SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
/// Return the filename of the file containing a SourceLocation.
StringRef SourceManager::getFilename(SourceLocation SpellingLoc) const {
if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
if (OptionalFileEntryRef F = getFileEntryRefForID(getFileID(SpellingLoc)))
return F->getName();
return StringRef();
}

View File

@ -3215,7 +3215,7 @@ bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn,
return true;
// NoSanitize by location. Check "mainfile" prefix.
auto &SM = Context.getSourceManager();
const FileEntry &MainFile = *SM.getFileEntryForID(SM.getMainFileID());
FileEntryRef MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
return true;
@ -3236,7 +3236,8 @@ bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind,
return true;
auto &SM = Context.getSourceManager();
if (NoSanitizeL.containsMainFile(
Kind, SM.getFileEntryForID(SM.getMainFileID())->getName(), Category))
Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
Category))
return true;
if (NoSanitizeL.containsLocation(Kind, Loc, Category))
return true;
@ -3302,7 +3303,7 @@ CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn,
// If location is unknown, this may be a compiler-generated function. Assume
// it's located in the main file.
auto &SM = Context.getSourceManager();
if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID()))
if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
return *V;
return ProfileList.getDefault(Kind);

View File

@ -1497,8 +1497,8 @@ StringRef ASTUnit::getMainFileName() const {
}
if (SourceMgr) {
if (const FileEntry *
FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID()))
if (OptionalFileEntryRef FE =
SourceMgr->getFileEntryRefForID(SourceMgr->getMainFileID()))
return FE->getName();
}

View File

@ -666,7 +666,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
} else {
auto &OldSM = AST->getSourceManager();
FileID ID = OldSM.getMainFileID();
if (auto *File = OldSM.getFileEntryForID(ID))
if (auto File = OldSM.getFileEntryRefForID(ID))
Input = FrontendInputFile(File->getName(), Kind);
else
Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
@ -844,7 +844,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
return false;
}
// We now have the filename...
FileName = FE->getFileEntry().getName();
FileName = FE->getName();
// ... still a header unit, but now use the path as written.
Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
Input = FrontendInputFile(FileName, Kind, Input.isSystem());

View File

@ -259,7 +259,7 @@ void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
}
void HeaderIncludesJSONCallback::EndOfMainFile() {
const FileEntry *FE = SM.getFileEntryForID(SM.getMainFileID());
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID());
SmallString<256> MainFile(FE->getName());
SM.getFileManager().makeAbsolutePath(MainFile);

View File

@ -118,7 +118,7 @@ void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
const SourceManager &SM = Info.getSourceManager();
FileID FID = SM.getMainFileID();
if (FID.isValid()) {
if (const FileEntry *FE = SM.getFileEntryForID(FID))
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID))
MainFilename = std::string(FE->getName());
}
}
@ -147,7 +147,7 @@ void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
// At least print the file name if available:
FileID FID = SM.getFileID(Info.getLocation());
if (FID.isValid()) {
if (const FileEntry *FE = SM.getFileEntryForID(FID))
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID))
DE.Filename = std::string(FE->getName());
}
} else {

View File

@ -550,19 +550,19 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build(
SourceManager &SourceMgr = Clang->getSourceManager();
for (auto &Filename : PreambleDepCollector->getDependencies()) {
auto FileOrErr = Clang->getFileManager().getFile(Filename);
if (!FileOrErr ||
*FileOrErr == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()))
auto MaybeFile = Clang->getFileManager().getOptionalFileRef(Filename);
if (!MaybeFile ||
MaybeFile == SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID()))
continue;
auto File = *FileOrErr;
if (time_t ModTime = File->getModificationTime()) {
FilesInPreamble[File->getName()] =
PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(),
auto File = *MaybeFile;
if (time_t ModTime = File.getModificationTime()) {
FilesInPreamble[File.getName()] =
PrecompiledPreamble::PreambleFileHash::createForFile(File.getSize(),
ModTime);
} else {
llvm::MemoryBufferRef Buffer =
SourceMgr.getMemoryBufferForFileOrFake(File);
FilesInPreamble[File->getName()] =
FilesInPreamble[File.getName()] =
PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer);
}
}

View File

@ -93,7 +93,8 @@ bool FixItRewriter::WriteFixedFiles(
}
for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
OptionalFileEntryRef Entry =
Rewrite.getSourceMgr().getFileEntryRefForID(I->first);
int fd;
std::string Filename =
FixItOpts->RewriteFilename(std::string(Entry->getName()), fd);

View File

@ -62,7 +62,7 @@ void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) {
// Format the file.
FileID FID = R.getSourceMgr().getMainFileID();
const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID);
OptionalFileEntryRef Entry = R.getSourceMgr().getFileEntryRefForID(FID);
StringRef Name;
// In some cases, in particular the case where the input is from stdin,
// there is no entry. Fall back to the memory buffer for a name in those

View File

@ -70,7 +70,7 @@ SarifResult SARIFDiagnostic::addLocationToResult(
// At least add the file name if available:
FileID FID = Loc.getFileID();
if (FID.isValid()) {
if (const FileEntry *FE = Loc.getFileEntry()) {
if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
emitFilename(FE->getName(), Loc.getManager());
// FIXME(llvm-project/issues/57366): File-only locations
}

View File

@ -779,7 +779,7 @@ void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
if (PLoc.isInvalid()) {
// At least print the file name if available:
if (FileID FID = Loc.getFileID(); FID.isValid()) {
if (const FileEntry *FE = Loc.getFileEntry()) {
if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
emitFilename(FE->getName(), Loc.getManager());
OS << ": ";
}

View File

@ -868,8 +868,8 @@ static unsigned PrintUnexpected(DiagnosticsEngine &Diags, SourceManager *SourceM
OS << "\n (frontend)";
else {
OS << "\n ";
if (const FileEntry *File = SourceMgr->getFileEntryForID(
SourceMgr->getFileID(I->first)))
if (OptionalFileEntryRef File =
SourceMgr->getFileEntryRefForID(SourceMgr->getFileID(I->first)))
OS << " File " << File->getName();
OS << " Line " << SourceMgr->getPresumedLineNumber(I->first);
}

View File

@ -891,7 +891,7 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) {
unsigned FileOffset = LocInfo.second;
if (FID.isValid()) {
if (const FileEntry *FE = SM.getFileEntryForID(FID)) {
if (OptionalFileEntryRef FE = SM.getFileEntryRefForID(FID)) {
Result << " file=\"";
appendToResultWithXMLEscaping(FE->getName());
Result << "\"";

View File

@ -31,7 +31,7 @@ static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc,
}
Loc = SM.getExpansionLoc(Loc);
const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc);
const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
OptionalFileEntryRef FE = SM.getFileEntryRefForID(Decomposed.first);
if (FE) {
OS << llvm::sys::path::filename(FE->getName());
} else {

View File

@ -152,9 +152,9 @@ public:
SourceLocation IncludeLoc = SM.getIncludeLoc(SM.getFileID(Loc));
if (IncludeLoc.isValid()) {
if (llvm::timeTraceProfilerEnabled()) {
const FileEntry *FE = SM.getFileEntryForID(SM.getFileID(Loc));
llvm::timeTraceProfilerBegin(
"Source", FE != nullptr ? FE->getName() : StringRef("<unknown>"));
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getFileID(Loc));
llvm::timeTraceProfilerBegin("Source", FE ? FE->getName()
: StringRef("<unknown>"));
}
IncludeStack.push_back(IncludeLoc);

View File

@ -103,7 +103,8 @@ void Sema::HandleStartOfHeaderUnit() {
StringRef HUName = getLangOpts().CurrentModule;
if (HUName.empty()) {
HUName = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())->getName();
HUName =
SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID())->getName();
const_cast<LangOptions &>(getLangOpts()).CurrentModule = HUName.str();
}

View File

@ -112,7 +112,7 @@ public:
// Add HTML header/footers to file specified by FID
void FinalizeHTML(const PathDiagnostic &D, Rewriter &R,
const SourceManager &SMgr, const PathPieces &path,
FileID FID, const FileEntry *Entry, const char *declName);
FileID FID, FileEntryRef Entry, const char *declName);
// Rewrite the file specified by FID with HTML formatting.
void RewriteFile(Rewriter &R, const PathPieces &path, FileID FID);
@ -326,7 +326,7 @@ void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D,
FileID ReportFile =
path.back()->getLocation().asLocation().getExpansionLoc().getFileID();
const FileEntry *Entry = SMgr.getFileEntryForID(ReportFile);
OptionalFileEntryRef Entry = SMgr.getFileEntryRefForID(ReportFile);
FileName << llvm::sys::path::filename(Entry->getName()).str() << "-"
<< declName.c_str() << "-" << offsetDecl << "-";
@ -396,7 +396,7 @@ std::string HTMLDiagnostics::GenerateHTML(const PathDiagnostic& D, Rewriter &R,
os << "<div class=FileNav><a href=\"#File" << (I - 1)->getHashValue()
<< "\">&#x2190;</a></div>";
os << "<h4 class=FileName>" << SMgr.getFileEntryForID(*I)->getName()
os << "<h4 class=FileName>" << SMgr.getFileEntryRefForID(*I)->getName()
<< "</h4>\n";
// Right nav arrow
@ -429,8 +429,8 @@ std::string HTMLDiagnostics::GenerateHTML(const PathDiagnostic& D, Rewriter &R,
// Add CSS, header, and footer.
FileID FID =
path.back()->getLocation().asLocation().getExpansionLoc().getFileID();
const FileEntry* Entry = SMgr.getFileEntryForID(FID);
FinalizeHTML(D, R, SMgr, path, FileIDs[0], Entry, declName);
OptionalFileEntryRef Entry = SMgr.getFileEntryRefForID(FID);
FinalizeHTML(D, R, SMgr, path, FileIDs[0], *Entry, declName);
std::string file;
llvm::raw_string_ostream os(file);
@ -537,16 +537,17 @@ document.addEventListener("DOMContentLoaded", function() {
return s;
}
void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
const SourceManager& SMgr, const PathPieces& path, FileID FID,
const FileEntry *Entry, const char *declName) {
void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic &D, Rewriter &R,
const SourceManager &SMgr,
const PathPieces &path, FileID FID,
FileEntryRef Entry, const char *declName) {
// This is a cludge; basically we want to append either the full
// working directory if we have no directory information. This is
// a work in progress.
llvm::SmallString<0> DirName;
if (llvm::sys::path::is_relative(Entry->getName())) {
if (llvm::sys::path::is_relative(Entry.getName())) {
llvm::sys::fs::current_path(DirName);
DirName += '/';
}
@ -575,7 +576,7 @@ void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
<< "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n"
"<tr><td class=\"rowname\">File:</td><td>"
<< html::EscapeText(DirName)
<< html::EscapeText(Entry->getName())
<< html::EscapeText(Entry.getName())
<< "</td></tr>\n<tr><td class=\"rowname\">Warning:</td><td>"
"<a href=\"#EndPath\">line "
<< LineNumber
@ -656,9 +657,9 @@ void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
if (!BugCategory.empty())
os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n";
os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n";
os << "\n<!-- BUGFILE " << DirName << Entry.getName() << " -->\n";
os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry->getName()) << " -->\n";
os << "\n<!-- FILENAME " << llvm::sys::path::filename(Entry.getName()) << " -->\n";
os << "\n<!-- FUNCTIONNAME " << declName << " -->\n";
@ -682,7 +683,7 @@ void HTMLDiagnostics::FinalizeHTML(const PathDiagnostic& D, Rewriter &R,
R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str());
}
html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName());
html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry.getName());
}
StringRef HTMLDiagnostics::showHelpJavascript() {

View File

@ -804,7 +804,7 @@ void PlistDiagnostics::FlushDiagnosticsImpl(
o << " <key>files</key>\n"
" <array>\n";
for (FileID FID : Fids)
EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n';
EmitString(o << " ", SM.getFileEntryRefForID(FID)->getName()) << '\n';
o << " </array>\n";
if (llvm::AreStatisticsEnabled() && DiagOpts.ShouldSerializeStats) {

View File

@ -122,7 +122,8 @@ void Replacement::setFromSourceLocation(const SourceManager &Sources,
StringRef ReplacementText) {
const std::pair<FileID, unsigned> DecomposedLocation =
Sources.getDecomposedLoc(Start);
const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first);
OptionalFileEntryRef Entry =
Sources.getFileEntryRefForID(DecomposedLocation.first);
this->FilePath = std::string(Entry ? Entry->getName() : InvalidLocation);
this->ReplacementRange = Range(DecomposedLocation.second, Length);
this->ReplacementText = std::string(ReplacementText);

View File

@ -198,7 +198,7 @@ AtomicChange::AtomicChange(const SourceManager &SM,
const FullSourceLoc FullKeyPosition(KeyPosition, SM);
std::pair<FileID, unsigned> FileIDAndOffset =
FullKeyPosition.getSpellingLoc().getDecomposedLoc();
const FileEntry *FE = SM.getFileEntryForID(FileIDAndOffset.first);
OptionalFileEntryRef FE = SM.getFileEntryRefForID(FileIDAndOffset.first);
assert(FE && "Cannot create AtomicChange with invalid location.");
FilePath = std::string(FE->getName());
Key = FilePath + ":" + std::to_string(FileIDAndOffset.second);

View File

@ -243,7 +243,8 @@ private:
DiagnosticsEngine::Error,
"SourceLocation in file %0 at offset %1 is invalid");
Engine.Report(SourceLocation(), InvalidOffset)
<< SourceMgr.getFileEntryForID(MainFileID)->getName() << SymbolOffset;
<< SourceMgr.getFileEntryRefForID(MainFileID)->getName()
<< SymbolOffset;
return false;
}

View File

@ -940,7 +940,7 @@ std::string TokenBuffer::dumpForTests() const {
for (FileID ID : Keys) {
const MarkedFile &File = Files.find(ID)->second;
auto *Entry = SourceMgr->getFileEntryForID(ID);
auto Entry = SourceMgr->getFileEntryRefForID(ID);
if (!Entry)
continue; // Skip builtin files.
std::string Path = llvm::sys::path::convert_to_slash(Entry->getName());