Skip to content

Commit b228145

Browse files
committed
[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 fe90297 commit b228145

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
@@ -1927,7 +1927,12 @@ class llvm::vfs::RedirectingFileSystemParser {
19271927
FullPath = FS->getOverlayFileDir();
19281928
assert(!FullPath.empty() &&
19291929
"External contents prefix directory must exist");
1930-
llvm::sys::path::append(FullPath, Value);
1930+
SmallString<256> AbsFullPath = Value;
1931+
if (FS->makeAbsolute(FullPath, AbsFullPath)) {
1932+
error(N, "failed to make 'external-contents' absolute");
1933+
return nullptr;
1934+
}
1935+
FullPath = AbsFullPath;
19311936
} else {
19321937
FullPath = Value;
19331938
}
@@ -2223,7 +2228,7 @@ RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
22232228
// FS->OverlayFileDir => /<absolute_path_to>/dummy.cache/vfs
22242229
//
22252230
SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
2226-
std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
2231+
std::error_code EC = FS->makeAbsolute(OverlayAbsDir);
22272232
assert(!EC && "Overlay dir final path must be absolute");
22282233
(void)EC;
22292234
FS->setOverlayFileDir(OverlayAbsDir);

llvm/unittests/Support/VirtualFileSystemTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,29 @@ TEST_F(VFSFromYAMLTest, ReturnsExternalPathVFSHit) {
19541954
EXPECT_EQ(0, NumDiagnostics);
19551955
}
19561956

1957+
TEST_F(VFSFromYAMLTest, RelativeFileDirWithOverlayRelativeSetting) {
1958+
auto Lower = makeIntrusiveRefCnt<DummyFileSystem>();
1959+
Lower->addDirectory("//root/foo/bar");
1960+
Lower->addRegularFile("//root/foo/bar/a");
1961+
Lower->setCurrentWorkingDirectory("//root/foo");
1962+
IntrusiveRefCntPtr<vfs::FileSystem> FS =
1963+
getFromYAMLString("{\n"
1964+
" 'case-sensitive': false,\n"
1965+
" 'overlay-relative': true,\n"
1966+
" 'roots': [\n"
1967+
" { 'name': '//root/foo/bar/b', 'type': 'file',\n"
1968+
" 'external-contents': 'a'\n"
1969+
" }\n"
1970+
" ]\n"
1971+
"}",
1972+
Lower, "bar/overlay");
1973+
1974+
ASSERT_NE(FS.get(), nullptr);
1975+
ErrorOr<vfs::Status> S = FS->status("//root/foo/bar/b");
1976+
ASSERT_FALSE(S.getError());
1977+
EXPECT_EQ("//root/foo/bar/a", S->getName());
1978+
}
1979+
19571980
TEST_F(VFSFromYAMLTest, RootRelativeToOverlayDirTest) {
19581981
IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
19591982
Lower->addDirectory("//root/foo/bar");

0 commit comments

Comments
 (0)