Skip to content

Commit d52af08

Browse files
jansvoboda11mahesh-attarde
authored andcommitted
[llvm] Use the VFS to make path absolute (llvm#161271)
For the redirecting VFS, the `'overlay-relative'` option controls whether external paths should be appended to the overlay directory. This didn't always work as expected: when the overlay file path itself was relative, its absolute path was decided by the real FS, not the underlying VFS, and the resulting external path didn't exist in the underlying VFS. This PR fixes this issue.
1 parent 0b8923e commit d52af08

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,12 @@ class llvm::vfs::RedirectingFileSystemParser {
19081908
FullPath = FS->getOverlayFileDir();
19091909
assert(!FullPath.empty() &&
19101910
"External contents prefix directory must exist");
1911-
llvm::sys::path::append(FullPath, Value);
1911+
SmallString<256> AbsFullPath = Value;
1912+
if (FS->makeAbsolute(FullPath, AbsFullPath)) {
1913+
error(N, "failed to make 'external-contents' absolute");
1914+
return nullptr;
1915+
}
1916+
FullPath = AbsFullPath;
19121917
} else {
19131918
FullPath = Value;
19141919
}
@@ -2204,7 +2209,7 @@ RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
22042209
// FS->OverlayFileDir => /<absolute_path_to>/dummy.cache/vfs
22052210
//
22062211
SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
2207-
std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
2212+
std::error_code EC = FS->makeAbsolute(OverlayAbsDir);
22082213
assert(!EC && "Overlay dir final path must be absolute");
22092214
(void)EC;
22102215
FS->setOverlayFileDir(OverlayAbsDir);

llvm/unittests/Support/VirtualFileSystemTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,29 @@ TEST_F(VFSFromYAMLTest, ReturnsExternalPathVFSHit) {
19411941
EXPECT_EQ(0, NumDiagnostics);
19421942
}
19431943

1944+
TEST_F(VFSFromYAMLTest, RelativeFileDirWithOverlayRelativeSetting) {
1945+
auto Lower = makeIntrusiveRefCnt<DummyFileSystem>();
1946+
Lower->addDirectory("//root/foo/bar");
1947+
Lower->addRegularFile("//root/foo/bar/a");
1948+
Lower->setCurrentWorkingDirectory("//root/foo");
1949+
IntrusiveRefCntPtr<vfs::FileSystem> FS =
1950+
getFromYAMLString("{\n"
1951+
" 'case-sensitive': false,\n"
1952+
" 'overlay-relative': true,\n"
1953+
" 'roots': [\n"
1954+
" { 'name': '//root/foo/bar/b', 'type': 'file',\n"
1955+
" 'external-contents': 'a'\n"
1956+
" }\n"
1957+
" ]\n"
1958+
"}",
1959+
Lower, "bar/overlay");
1960+
1961+
ASSERT_NE(FS.get(), nullptr);
1962+
ErrorOr<vfs::Status> S = FS->status("//root/foo/bar/b");
1963+
ASSERT_FALSE(S.getError());
1964+
EXPECT_EQ("//root/foo/bar/a", S->getName());
1965+
}
1966+
19441967
TEST_F(VFSFromYAMLTest, RootRelativeToOverlayDirTest) {
19451968
auto Lower = makeIntrusiveRefCnt<DummyFileSystem>();
19461969
Lower->addDirectory("//root/foo/bar");

0 commit comments

Comments
 (0)