Skip to content

Commit

Permalink
[clang] NFCI: Use FileEntryRef in `SourceManager::overrideFileConte…
Browse files Browse the repository at this point in the history
…nts()`
  • Loading branch information
jansvoboda11 committed Sep 29, 2023
1 parent 1610311 commit 2da8f30
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 44 deletions.
8 changes: 2 additions & 6 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// \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));
}
Expand All @@ -972,12 +972,8 @@ class SourceManager : public RefCountedBase<SourceManager> {
///
/// \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.
///
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1935,8 +1935,8 @@ class Preprocessor {
/// (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; }
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 5 additions & 11 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Lex/Preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
6 changes: 3 additions & 3 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
4 changes: 2 additions & 2 deletions clang/unittests/Basic/SarifTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class SarifDocumentWriterTest : public ::testing::Test {
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)
Expand Down
32 changes: 16 additions & 16 deletions clang/unittests/Basic/SourceManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 =
Expand All @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2da8f30

Please sign in to comment.