Skip to content

Conversation

jansvoboda11
Copy link
Contributor

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.

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 llvm#162903.
@llvmbot
Copy link
Member

llvmbot commented Oct 16, 2025

@llvm/pr-subscribers-llvm-support

Author: Jan Svoboda (jansvoboda11)

Changes

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 --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)

@ferasboulala
Copy link

Looking forward to getting this merged :). It's currently breaking one of our tools.

@nathanchance
Copy link
Member

Thanks, this obviously resolves the failure since it deletes it and the include still works.

@jansvoboda11 jansvoboda11 merged commit 8fe71e0 into llvm:main Oct 17, 2025
10 checks passed
@jansvoboda11 jansvoboda11 deleted the source-mgr-vfs-follow-up branch October 17, 2025 15:12
jansvoboda11 added a commit to jansvoboda11/llvm-project that referenced this pull request Oct 21, 2025
With llvm#163862, this is not really necessary and causes downstream issues.
jansvoboda11 added a commit that referenced this pull request Oct 21, 2025
With #163862, this is not really necessary and causes downstream issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants