Skip to content

Commit 738b5c9

Browse files
Fix more VFS tests on Windows
Since VFS paths can be in either Posix or Windows style, we have to use a more flexible definition of "absolute" path. The key here is that FileSystem::makeAbsolute is now virtual, and the RedirectingFileSystem override checks for either concept of absolute before trying to make the path absolute by combining it with the current directory. Differential Revision: https://reviews.llvm.org/D70701
1 parent 12038be commit 738b5c9

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

clang/test/VFS/vfsroot-include.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// FIXME: PR43272
2-
// XFAIL: system-windows
3-
41
// RUN: rm -rf %t
52
// RUN: mkdir -p %t
63
// RUN: sed -e "s@TEST_DIR@%{/S:regex_replacement}@g" -e "s@OUT_DIR@%{/t:regex_replacement}@g" %S/Inputs/vfsroot.yaml > %t.yaml

clang/test/VFS/vfsroot-module.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// FIXME: PR43272
2-
// XFAIL: system-windows
3-
41
// RUN: rm -rf %t
52
// RUN: mkdir -p %t
63
// RUN: sed -e "s@TEST_DIR@%{/S:regex_replacement}@g" -e "s@OUT_DIR@%{/t:regex_replacement}@g" %S/Inputs/vfsroot.yaml > %t.yaml

clang/test/VFS/vfsroot-with-overlay.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// FIXME: PR43272
2-
// XFAIL: system-windows
3-
41
// RUN: rm -rf %t
52
// RUN: mkdir -p %t
63
// RUN: sed -e "s@TEST_DIR@%{/S:regex_replacement}@g" -e "s@OUT_DIR@%{/t:regex_replacement}@g" %S/Inputs/vfsroot.yaml > %t.yaml

llvm/include/llvm/Support/VirtualFileSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class FileSystem : public llvm::ThreadSafeRefCountedBase<FileSystem> {
293293
/// \param Path A path that is modified to be an absolute path.
294294
/// \returns success if \a path has been made absolute, otherwise a
295295
/// platform-specific error_code.
296-
std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
296+
virtual std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const;
297297
};
298298

299299
/// Gets an \p vfs::FileSystem for the 'real' file system, as seen by
@@ -749,6 +749,8 @@ class RedirectingFileSystem : public vfs::FileSystem {
749749

750750
std::error_code isLocal(const Twine &Path, bool &Result) override;
751751

752+
std::error_code makeAbsolute(SmallVectorImpl<char> &Path) const override;
753+
752754
directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
753755

754756
void setExternalContentsPrefixDir(StringRef PrefixDir);

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,19 @@ std::error_code RedirectingFileSystem::isLocal(const Twine &Path,
10731073
return ExternalFS->isLocal(Path, Result);
10741074
}
10751075

1076+
std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
1077+
if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) ||
1078+
llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::windows))
1079+
return {};
1080+
1081+
auto WorkingDir = getCurrentWorkingDirectory();
1082+
if (!WorkingDir)
1083+
return WorkingDir.getError();
1084+
1085+
llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
1086+
return {};
1087+
}
1088+
10761089
directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
10771090
std::error_code &EC) {
10781091
ErrorOr<RedirectingFileSystem::Entry *> E = lookupPath(Dir);
@@ -1428,21 +1441,31 @@ class llvm::vfs::RedirectingFileSystemParser {
14281441
return nullptr;
14291442
}
14301443

1431-
if (IsRootEntry && !sys::path::is_absolute(Name)) {
1432-
assert(NameValueNode && "Name presence should be checked earlier");
1433-
error(NameValueNode,
1434-
"entry with relative path at the root level is not discoverable");
1435-
return nullptr;
1444+
sys::path::Style path_style = sys::path::Style::native;
1445+
if (IsRootEntry) {
1446+
// VFS root entries may be in either Posix or Windows style. Figure out
1447+
// which style we have, and use it consistently.
1448+
if (sys::path::is_absolute(Name, sys::path::Style::posix)) {
1449+
path_style = sys::path::Style::posix;
1450+
} else if (sys::path::is_absolute(Name, sys::path::Style::windows)) {
1451+
path_style = sys::path::Style::windows;
1452+
} else {
1453+
assert(NameValueNode && "Name presence should be checked earlier");
1454+
error(NameValueNode,
1455+
"entry with relative path at the root level is not discoverable");
1456+
return nullptr;
1457+
}
14361458
}
14371459

14381460
// Remove trailing slash(es), being careful not to remove the root path
14391461
StringRef Trimmed(Name);
1440-
size_t RootPathLen = sys::path::root_path(Trimmed).size();
1462+
size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size();
14411463
while (Trimmed.size() > RootPathLen &&
1442-
sys::path::is_separator(Trimmed.back()))
1464+
sys::path::is_separator(Trimmed.back(), path_style))
14431465
Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
1466+
14441467
// Get the last component
1445-
StringRef LastComponent = sys::path::filename(Trimmed);
1468+
StringRef LastComponent = sys::path::filename(Trimmed, path_style);
14461469

14471470
std::unique_ptr<RedirectingFileSystem::Entry> Result;
14481471
switch (Kind) {
@@ -1460,12 +1483,12 @@ class llvm::vfs::RedirectingFileSystemParser {
14601483
break;
14611484
}
14621485

1463-
StringRef Parent = sys::path::parent_path(Trimmed);
1486+
StringRef Parent = sys::path::parent_path(Trimmed, path_style);
14641487
if (Parent.empty())
14651488
return Result;
14661489

14671490
// if 'name' contains multiple components, create implicit directory entries
1468-
for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
1491+
for (sys::path::reverse_iterator I = sys::path::rbegin(Parent, path_style),
14691492
E = sys::path::rend(Parent);
14701493
I != E; ++I) {
14711494
std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries;

0 commit comments

Comments
 (0)