[clang] 2da8f30 - [clang] NFCI: Use `FileEntryRef` in `SourceManager::overrideFileContents()`

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 29 09:30:28 PDT 2023


Author: Jan Svoboda
Date: 2023-09-29T09:30:21-07:00
New Revision: 2da8f30c5e9a26cd3ca7f5aa8489eb0f0f3c8559

URL: https://github.com/llvm/llvm-project/commit/2da8f30c5e9a26cd3ca7f5aa8489eb0f0f3c8559
DIFF: https://github.com/llvm/llvm-project/commit/2da8f30c5e9a26cd3ca7f5aa8489eb0f0f3c8559.diff

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

Added: 
    

Modified: 
    clang/include/clang/Basic/SourceManager.h
    clang/include/clang/Lex/Preprocessor.h
    clang/lib/Basic/SourceManager.cpp
    clang/lib/Frontend/CompilerInstance.cpp
    clang/lib/Lex/Preprocessor.cpp
    clang/unittests/AST/ASTImporterTest.cpp
    clang/unittests/Basic/SarifTest.cpp
    clang/unittests/Basic/SourceManagerTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h
index d6e65966c0eaecb..b882efcd8c57cdc 100644
--- a/clang/include/clang/Basic/SourceManager.h
+++ b/clang/include/clang/Basic/SourceManager.h
@@ -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));
   }
@@ -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.
   ///

diff  --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h
index ab6c3c68a94e477..e88164f196c1f7c 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -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; }

diff  --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 75855ade8865dba..3ca5dccb40e4f1e 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -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;

diff  --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index aacbb2050334411..d18371f21a9d86e 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -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));

diff  --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index 95e6a7a3771e516..73c37bc5161c4b6 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -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");
 

diff  --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index c90b5aaeb624306..e9e8cac2a15bc70 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -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"));

diff  --git a/clang/unittests/Basic/SarifTest.cpp b/clang/unittests/Basic/SarifTest.cpp
index b7ddd8701d56d6d..e87f4fa47aab26e 100644
--- a/clang/unittests/Basic/SarifTest.cpp
+++ b/clang/unittests/Basic/SarifTest.cpp
@@ -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)

diff  --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index db48dfd47c5e4b1..f451e43bb53d78e 100644
--- a/clang/unittests/Basic/SourceManagerTest.cpp
+++ b/clang/unittests/Basic/SourceManagerTest.cpp
@@ -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);


        


More information about the cfe-commits mailing list