diff options
author | Vinicius Tinti <viniciustinti@gmail.com> | 2020-09-23 16:28:19 +0100 |
---|---|---|
committer | Renato Golin <rengolin@microsoft.com> | 2020-09-23 18:01:32 +0100 |
commit | 577adda54f075b256007ed4fa80c9988a0642a87 (patch) | |
tree | f500884a974282f5b301752ae169950080e028cc /llvm/lib/Support/Path.cpp | |
parent | 68358081585b160c38443425935457235a4b9d1a (diff) | |
download | llvm-577adda54f075b256007ed4fa80c9988a0642a87.zip llvm-577adda54f075b256007ed4fa80c9988a0642a87.tar.gz llvm-577adda54f075b256007ed4fa80c9988a0642a87.tar.bz2 |
[Support/Path] Add path::is_absolute_gnu
Implements IS_ABSOLUTE_PATH from GNU tools.
C++17 is_absolute behavior is different the from the behavior defined by GNU
tools.
According to cppreference.com, C++17 states: "An absolute path is a path
that unambiguously identifies the location of a file without reference
to an additional starting location."
In other words, the rules are:
1. POSIX style paths with nonempty root directory are absolute.
2. Windows style paths with nonempty root name and root directory are
absolute.
3. No other paths are absolute.
GNU rules are:
1. Paths starting with a path separator are absolute.
2. Windows style paths are also absolute if they start with a character
followed by ':'.
3. No other paths are absolute.
On Windows style the path "C:\Users\Default" has "C:" as root name and "\"
as root directory.
Hence "C:" on Windows is absolute under GNU rules and not absolute under
C++17 because it has no root directory. Likewise "/" and "\" on Windows are
absolute under GNU and are not absolute under C++17 due to empty root name.
Related to PR46368.
Differential Revision: https://reviews.llvm.org/D87667
Diffstat (limited to 'llvm/lib/Support/Path.cpp')
-rw-r--r-- | llvm/lib/Support/Path.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index 37b3086..bbc02a2 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -683,6 +683,24 @@ bool is_absolute(const Twine &path, Style style) { return rootDir && rootName; } +bool is_absolute_gnu(const Twine &path, Style style) { + SmallString<128> path_storage; + StringRef p = path.toStringRef(path_storage); + + // Handle '/' which is absolute for both Windows and POSIX systems. + // Handle '\\' on Windows. + if (!p.empty() && is_separator(p.front(), style)) + return true; + + if (real_style(style) == Style::windows) { + // Handle drive letter pattern (a character followed by ':') on Windows. + if (p.size() >= 2 && (p[0] && p[1] == ':')) + return true; + } + + return false; +} + bool is_relative(const Twine &path, Style style) { return !is_absolute(path, style); } |