diff options
author | Martin Storsjö <martin@martin.st> | 2021-10-04 17:17:00 +0300 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2021-11-05 10:41:51 +0200 |
commit | a8b54834a186f5570b49b614e31b961a9cf1cbfe (patch) | |
tree | cf9f9891831cdbffdc969e5c657c0a4a69a614b2 /llvm/lib/Support/Path.cpp | |
parent | f95bd18b5faa6a5af4b5786312c373c5b2dce687 (diff) | |
download | llvm-a8b54834a186f5570b49b614e31b961a9cf1cbfe.zip llvm-a8b54834a186f5570b49b614e31b961a9cf1cbfe.tar.gz llvm-a8b54834a186f5570b49b614e31b961a9cf1cbfe.tar.bz2 |
[Support] Add a new path style for Windows with forward slashes
This behaves just like the regular Windows style, with both separator
forms accepted, but with get_separator() returning forward slashes.
Add a more descriptive name for the existing style, keeping the old
name around as an alias initially.
Add a new function `make_preferred()` (like the C++17
`std::filesystem::path` function with the same name), which converts
windows paths to the preferred separator form (while this one works on
any platform and takes a `path::Style` argument).
Contrary to `native()` (just like `make_preferred()` in `std::filesystem`),
this doesn't do anything at all on Posix, it doesn't try to reinterpret
backslashes into forward slashes there.
Differential Revision: https://reviews.llvm.org/D111879
Diffstat (limited to 'llvm/lib/Support/Path.cpp')
-rw-r--r-- | llvm/lib/Support/Path.cpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index 536d114..cea6df8 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -37,13 +37,15 @@ namespace { using llvm::sys::path::Style; inline Style real_style(Style style) { + if (style != Style::native) + return style; if (is_style_posix(style)) return Style::posix; return Style::windows; } inline const char *separators(Style style) { - if (real_style(style) == Style::windows) + if (is_style_windows(style)) return "\\/"; return "/"; } @@ -547,7 +549,9 @@ void native(SmallVectorImpl<char> &Path, Style style) { if (Path.empty()) return; if (is_style_windows(style)) { - std::replace(Path.begin(), Path.end(), '/', '\\'); + for (char &Ch : Path) + if (is_separator(Ch, style)) + Ch = preferred_separator(style); if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) { SmallString<128> PathHome; home_directory(PathHome); @@ -601,7 +605,7 @@ bool is_separator(char value, Style style) { } StringRef get_separator(Style style) { - if (is_style_windows(style)) + if (real_style(style) == Style::windows) return "\\"; return "/"; } |