Skip to content

Commit 8fe71e0

Browse files
authored
[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.
1 parent 9ab1677 commit 8fe71e0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

llvm/lib/Support/SourceMgr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
6969
ErrorOr<std::unique_ptr<MemoryBuffer>>
7070
SourceMgr::OpenIncludeFile(const std::string &Filename,
7171
std::string &IncludedFile) {
72-
if (!FS)
73-
reportFatalInternalError("Opening include file from SourceMgr without VFS");
72+
auto GetFile = [this](StringRef Path) {
73+
return FS ? FS->getBufferForFile(Path) : MemoryBuffer::getFile(Path);
74+
};
7475

75-
ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr =
76-
FS->getBufferForFile(Filename);
76+
ErrorOr<std::unique_ptr<MemoryBuffer>> NewBufOrErr = GetFile(Filename);
7777

7878
SmallString<64> Buffer(Filename);
7979
// If the file didn't exist directly, see if it's in an include path.
8080
for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr;
8181
++i) {
8282
Buffer = IncludeDirectories[i];
8383
sys::path::append(Buffer, Filename);
84-
NewBufOrErr = FS->getBufferForFile(Buffer);
84+
NewBufOrErr = GetFile(Buffer);
8585
}
8686

8787
if (NewBufOrErr)

0 commit comments

Comments
 (0)