[clang] NFCI: Use FileEntryRef in SourceManager::overrideFileContents()

This commit is contained in:
Jan Svoboda
2023-09-13 11:45:29 -07:00
parent 1610311a95
commit 2da8f30c5e
8 changed files with 33 additions and 44 deletions

View File

@@ -960,7 +960,7 @@ public:
///
/// \param Buffer the memory buffer whose contents will be used as the
/// data in the given source file.
void overrideFileContents(const FileEntry *SourceFile,
void overrideFileContents(FileEntryRef SourceFile,
const llvm::MemoryBufferRef &Buffer) {
overrideFileContents(SourceFile, llvm::MemoryBuffer::getMemBuffer(Buffer));
}
@@ -972,12 +972,8 @@ public:
///
/// \param Buffer the memory buffer whose contents will be used as the
/// data in the given source file.
void overrideFileContents(const FileEntry *SourceFile,
std::unique_ptr<llvm::MemoryBuffer> Buffer);
void overrideFileContents(FileEntryRef SourceFile,
std::unique_ptr<llvm::MemoryBuffer> Buffer) {
overrideFileContents(&SourceFile.getFileEntry(), std::move(Buffer));
}
std::unique_ptr<llvm::MemoryBuffer> Buffer);
/// Override the given source file with another one.
///

View File

@@ -1935,8 +1935,8 @@ public:
/// (1-based).
///
/// \returns true if an error occurred, false otherwise.
bool SetCodeCompletionPoint(const FileEntry *File,
unsigned Line, unsigned Column);
bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line,
unsigned Column);
/// Determine if we are performing code completion.
bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }

View File

@@ -687,8 +687,8 @@ SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) {
}
void SourceManager::overrideFileContents(
const FileEntry *SourceFile, std::unique_ptr<llvm::MemoryBuffer> Buffer) {
SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile->getLastRef());
FileEntryRef SourceFile, std::unique_ptr<llvm::MemoryBuffer> Buffer) {
SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile);
IR.setBuffer(std::move(Buffer));
IR.BufferOverridden = true;

View File

@@ -400,14 +400,8 @@ static void InitializeFileRemapping(DiagnosticsEngine &Diags,
// Remap files in the source manager (with buffers).
for (const auto &RB : InitOpts.RemappedFileBuffers) {
// Create the file entry for the file that we're mapping from.
const FileEntry *FromFile =
FileMgr.getVirtualFile(RB.first, RB.second->getBufferSize(), 0);
if (!FromFile) {
Diags.Report(diag::err_fe_remap_missing_from_file) << RB.first;
if (!InitOpts.RetainRemappedFileBuffers)
delete RB.second;
continue;
}
FileEntryRef FromFile =
FileMgr.getVirtualFileRef(RB.first, RB.second->getBufferSize(), 0);
// Override the contents of the "from" file with the contents of the
// "to" file. If the caller owns the buffers, then pass a MemoryBufferRef;
@@ -701,7 +695,7 @@ static bool EnableCodeCompletion(Preprocessor &PP,
unsigned Column) {
// Tell the source manager to chop off the given file at a specific
// line and column.
auto Entry = PP.getFileManager().getFile(Filename);
auto Entry = PP.getFileManager().getOptionalFileRef(Filename);
if (!Entry) {
PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
<< Filename;
@@ -1357,7 +1351,7 @@ static bool compileModule(CompilerInstance &ImportingInstance,
[&](CompilerInstance &Instance) {
std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
llvm::MemoryBuffer::getMemBuffer(InferredModuleMapContent);
const FileEntry *ModuleMapFile = Instance.getFileManager().getVirtualFile(
FileEntryRef ModuleMapFile = Instance.getFileManager().getVirtualFileRef(
FakeModuleMapFile, InferredModuleMapContent.size(), 0);
Instance.getSourceManager().overrideFileContents(
ModuleMapFile, std::move(ModuleMapBuffer));
@@ -2182,7 +2176,7 @@ void CompilerInstance::createModuleFromSource(SourceLocation ImportLoc,
auto PreBuildStep = [&](CompilerInstance &Other) {
// Create a virtual file containing our desired source.
// FIXME: We shouldn't need to do this.
const FileEntry *ModuleMapFile = Other.getFileManager().getVirtualFile(
FileEntryRef ModuleMapFile = Other.getFileManager().getVirtualFileRef(
ModuleMapFileName, NullTerminatedSource.size(), 0);
Other.getSourceManager().overrideFileContents(
ModuleMapFile, llvm::MemoryBuffer::getMemBuffer(NullTerminatedSource));

View File

@@ -391,10 +391,9 @@ void Preprocessor::recomputeCurLexerKind() {
CurLexerKind = CLK_CachingLexer;
}
bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
bool Preprocessor::SetCodeCompletionPoint(FileEntryRef File,
unsigned CompleteLine,
unsigned CompleteColumn) {
assert(File);
assert(CompleteLine && CompleteColumn && "Starts from 1:1");
assert(!CodeCompletionFile && "Already set");

View File

@@ -7256,14 +7256,14 @@ TEST_P(ImportSourceLocations, OverwrittenFileBuffer) {
{
SourceManager &FromSM = FromTU->getASTContext().getSourceManager();
clang::FileManager &FM = FromSM.getFileManager();
const clang::FileEntry &FE =
*FM.getVirtualFile(Path, static_cast<off_t>(Contents.size()), 0);
clang::FileEntryRef FE =
FM.getVirtualFileRef(Path, static_cast<off_t>(Contents.size()), 0);
llvm::SmallVector<char, 64> Buffer;
Buffer.append(Contents.begin(), Contents.end());
auto FileContents = std::make_unique<llvm::SmallVectorMemoryBuffer>(
std::move(Buffer), Path, /*RequiresNullTerminator=*/false);
FromSM.overrideFileContents(&FE, std::move(FileContents));
FromSM.overrideFileContents(FE, std::move(FileContents));
// Import the VarDecl to trigger the importing of the FileID.
auto Pattern = varDecl(hasName("X"));

View File

@@ -60,8 +60,8 @@ protected:
bool IsMainFile = false) {
std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
llvm::MemoryBuffer::getMemBuffer(SourceText);
const FileEntry *SourceFile =
FileMgr.getVirtualFile(Name, SourceBuf->getBufferSize(), 0);
FileEntryRef SourceFile =
FileMgr.getVirtualFileRef(Name, SourceBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(SourceFile, std::move(SourceBuf));
FileID FID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);
if (IsMainFile)

View File

@@ -69,8 +69,8 @@ TEST_F(SourceManagerTest, isInMemoryBuffersNoSourceLocationInfo) {
std::unique_ptr<llvm::MemoryBuffer> BuiltInBuf =
llvm::MemoryBuffer::getMemBuffer(Source);
const FileEntry *BuiltInFile =
FileMgr.getVirtualFile("<built-in>", BuiltInBuf->getBufferSize(), 0);
FileEntryRef BuiltInFile =
FileMgr.getVirtualFileRef("<built-in>", BuiltInBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(BuiltInFile, std::move(BuiltInBuf));
FileID BuiltInFileID =
SourceMgr.getOrCreateFileID(BuiltInFile, SrcMgr::C_User);
@@ -82,7 +82,7 @@ TEST_F(SourceManagerTest, isInMemoryBuffersNoSourceLocationInfo) {
std::unique_ptr<llvm::MemoryBuffer> CommandLineBuf =
llvm::MemoryBuffer::getMemBuffer(Source);
const FileEntry *CommandLineFile = FileMgr.getVirtualFile(
FileEntryRef CommandLineFile = FileMgr.getVirtualFileRef(
"<command line>", CommandLineBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(CommandLineFile, std::move(CommandLineBuf));
FileID CommandLineFileID =
@@ -95,7 +95,7 @@ TEST_F(SourceManagerTest, isInMemoryBuffersNoSourceLocationInfo) {
std::unique_ptr<llvm::MemoryBuffer> ScratchSpaceBuf =
llvm::MemoryBuffer::getMemBuffer(Source);
const FileEntry *ScratchSpaceFile = FileMgr.getVirtualFile(
FileEntryRef ScratchSpaceFile = FileMgr.getVirtualFileRef(
"<scratch space>", ScratchSpaceBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(ScratchSpaceFile, std::move(ScratchSpaceBuf));
FileID ScratchSpaceFileID =
@@ -311,12 +311,12 @@ TEST_F(SourceManagerTest, locationPrintTest) {
std::unique_ptr<llvm::MemoryBuffer> Buf =
llvm::MemoryBuffer::getMemBuffer(Source);
const FileEntry *SourceFile =
FileMgr.getVirtualFile("/mainFile.cpp", Buf->getBufferSize(), 0);
FileEntryRef SourceFile =
FileMgr.getVirtualFileRef("/mainFile.cpp", Buf->getBufferSize(), 0);
SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
const FileEntry *HeaderFile =
FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0);
FileEntryRef HeaderFile = FileMgr.getVirtualFileRef(
"/test-header.h", HeaderBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf));
FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);
@@ -431,8 +431,8 @@ TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
SourceMgr.setMainFileID(mainFileID);
const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
HeaderBuf->getBufferSize(), 0);
FileEntryRef headerFile = FileMgr.getVirtualFileRef(
"/test-header.h", HeaderBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
TrivialModuleLoader ModLoader;
@@ -555,8 +555,8 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
llvm::MemoryBuffer::getMemBuffer(main);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
HeaderBuf->getBufferSize(), 0);
FileEntryRef headerFile = FileMgr.getVirtualFileRef(
"/test-header.h", HeaderBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
TrivialModuleLoader ModLoader;
@@ -640,14 +640,14 @@ TEST_F(SourceManagerTest, isMainFile) {
std::unique_ptr<llvm::MemoryBuffer> Buf =
llvm::MemoryBuffer::getMemBuffer(Source);
const FileEntry *SourceFile =
FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0);
FileEntryRef SourceFile =
FileMgr.getVirtualFileRef("mainFile.cpp", Buf->getBufferSize(), 0);
SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
std::unique_ptr<llvm::MemoryBuffer> Buf2 =
llvm::MemoryBuffer::getMemBuffer(Source);
const FileEntry *SecondFile =
FileMgr.getVirtualFile("file2.cpp", Buf2->getBufferSize(), 0);
FileEntryRef SecondFile =
FileMgr.getVirtualFileRef("file2.cpp", Buf2->getBufferSize(), 0);
SourceMgr.overrideFileContents(SecondFile, std::move(Buf2));
FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);