Skip to content

Commit 46ec93a

Browse files
committed
[Support] [VirtualFileSystem] Detect the windows_slash path style
This fixes the following clang VFS tests, if `windows_slash` is the default style: Clang :: VFS/implicit-include.c Clang :: VFS/relative-path.c Clang-Unit :: Frontend/./FrontendTests.exe/CompilerInstance.DefaultVFSOverlayFromInvocation Also clarify a couple references to `Style::windows` into `Style::windows_backslash`, to make it clearer that each of them are opinionated in different directions (even if it doesn't matter for calls to e.g. `is_absolute`). Differential Revision: https://reviews.llvm.org/D113272
1 parent e2b1d32 commit 46ec93a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,10 @@ static llvm::sys::path::Style getExistingStyle(llvm::StringRef Path) {
10411041
// Detect the path style in use by checking the first separator.
10421042
llvm::sys::path::Style style = llvm::sys::path::Style::native;
10431043
const size_t n = Path.find_first_of("/\\");
1044+
// Can't distinguish between posix and windows_slash here.
10441045
if (n != static_cast<size_t>(-1))
10451046
style = (Path[n] == '/') ? llvm::sys::path::Style::posix
1046-
: llvm::sys::path::Style::windows;
1047+
: llvm::sys::path::Style::windows_backslash;
10471048
return style;
10481049
}
10491050

@@ -1189,8 +1190,10 @@ std::error_code RedirectingFileSystem::isLocal(const Twine &Path_,
11891190
}
11901191

11911192
std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
1193+
// is_absolute(..., Style::windows_*) accepts paths with both slash types.
11921194
if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) ||
1193-
llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::windows))
1195+
llvm::sys::path::is_absolute(Path,
1196+
llvm::sys::path::Style::windows_backslash))
11941197
return {};
11951198

11961199
auto WorkingDir = getCurrentWorkingDirectory();
@@ -1201,9 +1204,15 @@ std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path)
12011204
// is native and there is no way to override that. Since we know WorkingDir
12021205
// is absolute, we can use it to determine which style we actually have and
12031206
// append Path ourselves.
1204-
sys::path::Style style = sys::path::Style::windows;
1207+
sys::path::Style style = sys::path::Style::windows_backslash;
12051208
if (sys::path::is_absolute(WorkingDir.get(), sys::path::Style::posix)) {
12061209
style = sys::path::Style::posix;
1210+
} else {
1211+
// Distinguish between windows_backslash and windows_slash; getExistingStyle
1212+
// returns posix for a path with windows_slash.
1213+
if (getExistingStyle(WorkingDir.get()) !=
1214+
sys::path::Style::windows_backslash)
1215+
style = sys::path::Style::windows_slash;
12071216
}
12081217

12091218
std::string Result = WorkingDir.get();
@@ -1621,8 +1630,9 @@ class llvm::vfs::RedirectingFileSystemParser {
16211630
// which style we have, and use it consistently.
16221631
if (sys::path::is_absolute(Name, sys::path::Style::posix)) {
16231632
path_style = sys::path::Style::posix;
1624-
} else if (sys::path::is_absolute(Name, sys::path::Style::windows)) {
1625-
path_style = sys::path::Style::windows;
1633+
} else if (sys::path::is_absolute(Name,
1634+
sys::path::Style::windows_backslash)) {
1635+
path_style = sys::path::Style::windows_backslash;
16261636
} else {
16271637
assert(NameValueNode && "Name presence should be checked earlier");
16281638
error(NameValueNode,

0 commit comments

Comments
 (0)