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

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 16 13:47:46 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Jan Svoboda (jansvoboda11)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/163862.diff


1 Files Affected:

- (modified) llvm/lib/Support/SourceMgr.cpp (+5-5) 


``````````diff
diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp
index f2bbaab23ed7b..f2ceaa0c9a669 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)

``````````

</details>


https://github.com/llvm/llvm-project/pull/163862


More information about the llvm-commits mailing list