[llvm] 8fe71e0 - [support] Don't require VFS in `SourceMgr` for loading includes (#163862)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 17 08:12:39 PDT 2025


Author: Jan Svoboda
Date: 2025-10-17T08:12:34-07:00
New Revision: 8fe71e0bdfb9102c607001289010698e51e38711

URL: https://github.com/llvm/llvm-project/commit/8fe71e0bdfb9102c607001289010698e51e38711
DIFF: https://github.com/llvm/llvm-project/commit/8fe71e0bdfb9102c607001289010698e51e38711.diff

LOG: [support] Don't require VFS in `SourceMgr` for loading includes (#163862)

This commit more gracefully handles situations where `SourceMgr` isn't
initialized with a VFS and tries to resolve an include. That's what
happens with the test case `clang -flto=thin -c test.c -o /dev/null`
where test.c contains `asm(" .incbin \"foo.i\" \n");`. Propagating the
actual VFS all the way is very difficult.

This is a follow-up to #162903.

Added: 
    

Modified: 
    llvm/lib/Support/SourceMgr.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp
index f2bbaab23ed7b..299615a6c8041 100644
--- a/llvm/lib/Support/SourceMgr.cpp
+++ b/llvm/lib/Support/SourceMgr.cpp
@@ -69,11 +69,11 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
 ErrorOr<std::unique_ptr<MemoryBuffer>>
 SourceMgr::OpenIncludeFile(const std::string &Filename,
                            std::string &IncludedFile) {
-  if (!FS)
-    reportFatalInternalError("Opening include file from SourceMgr without VFS");
+  auto GetFile = [this](StringRef Path) {
+    return FS ? FS->getBufferForFile(Path) : MemoryBuffer::getFile(Path);
+  };
 
-  ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
-      FS->getBufferForFile(Filename);
+  ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr = GetFile(Filename);
 
   SmallString<64> Buffer(Filename);
   // If the file didn't exist directly, see if it's in an include path.
@@ -81,7 +81,7 @@ SourceMgr::OpenIncludeFile(const std::string &Filename,
        ++i) {
     Buffer = IncludeDirectories[i];
     sys::path::append(Buffer, Filename);
-    NewBufOrErr = FS->getBufferForFile(Buffer);
+    NewBufOrErr = GetFile(Buffer);
   }
 
   if (NewBufOrErr)


        


More information about the llvm-commits mailing list