forked from OSchip/llvm-project
Support: Expose sys::path::is_style_{posix,windows,native}()
Expose three helpers in namespace llvm::sys::path to detect the path rules followed by sys::path::Style. - is_style_posix() - is_style_windows() - is_style_native() This are constexpr functions that that will allow a bunch of path-related code to stop checking `_WIN32`. Originally I looked at adding system_style(), analogous to sys::endian::system_endianness(), but future patches (from others) will add more Windows style variants for slash preferences. These helpers should be resilient to that change, allowing callers to detect basic path rules. Differential Revision: https://reviews.llvm.org/D112288
This commit is contained in:
parent
d0e9879d96
commit
4e4883e1f3
|
@ -27,6 +27,27 @@ namespace path {
|
|||
|
||||
enum class Style { windows, posix, native };
|
||||
|
||||
/// Check if \p S uses POSIX path rules.
|
||||
constexpr bool is_style_posix(Style S) {
|
||||
if (S == Style::posix)
|
||||
return true;
|
||||
if (S != Style::native)
|
||||
return false;
|
||||
#if defined(_WIN32)
|
||||
return false;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Check if \p S uses Windows path rules.
|
||||
constexpr bool is_style_windows(Style S) { return !is_style_posix(S); }
|
||||
|
||||
/// Check if \p S uses the same path rules as Style::native.
|
||||
constexpr bool is_style_native(Style S) {
|
||||
return is_style_posix(S) == is_style_posix(Style::native);
|
||||
}
|
||||
|
||||
/// @name Lexical Component Iterator
|
||||
/// @{
|
||||
|
||||
|
|
|
@ -37,11 +37,9 @@ namespace {
|
|||
using llvm::sys::path::Style;
|
||||
|
||||
inline Style real_style(Style style) {
|
||||
#ifdef _WIN32
|
||||
return (style == Style::posix) ? Style::posix : Style::windows;
|
||||
#else
|
||||
return (style == Style::windows) ? Style::windows : Style::posix;
|
||||
#endif
|
||||
if (is_style_posix(style))
|
||||
return Style::posix;
|
||||
return Style::windows;
|
||||
}
|
||||
|
||||
inline const char *separators(Style style) {
|
||||
|
|
|
@ -69,6 +69,29 @@ struct FileDescriptorCloser {
|
|||
int FD;
|
||||
};
|
||||
|
||||
TEST(is_style_Style, Works) {
|
||||
using namespace llvm::sys::path;
|
||||
// Check platform-independent results.
|
||||
EXPECT_TRUE(is_style_posix(Style::posix));
|
||||
EXPECT_TRUE(is_style_windows(Style::windows));
|
||||
EXPECT_FALSE(is_style_posix(Style::windows));
|
||||
EXPECT_FALSE(is_style_windows(Style::posix));
|
||||
|
||||
// Check platform-dependent results.
|
||||
#if defined(_WIN32)
|
||||
EXPECT_FALSE(is_style_posix(Style::native));
|
||||
EXPECT_TRUE(is_style_windows(Style::native));
|
||||
#else
|
||||
EXPECT_TRUE(is_style_posix(Style::native));
|
||||
EXPECT_FALSE(is_style_windows(Style::native));
|
||||
#endif
|
||||
|
||||
// Check is_style_native().
|
||||
EXPECT_TRUE(is_style_native(Style::native));
|
||||
EXPECT_EQ(is_style_posix(Style::native), is_style_native(Style::posix));
|
||||
EXPECT_EQ(is_style_windows(Style::native), is_style_native(Style::windows));
|
||||
}
|
||||
|
||||
TEST(is_separator, Works) {
|
||||
EXPECT_TRUE(path::is_separator('/'));
|
||||
EXPECT_FALSE(path::is_separator('\0'));
|
||||
|
@ -78,11 +101,8 @@ TEST(is_separator, Works) {
|
|||
EXPECT_TRUE(path::is_separator('\\', path::Style::windows));
|
||||
EXPECT_FALSE(path::is_separator('\\', path::Style::posix));
|
||||
|
||||
#ifdef _WIN32
|
||||
EXPECT_TRUE(path::is_separator('\\'));
|
||||
#else
|
||||
EXPECT_FALSE(path::is_separator('\\'));
|
||||
#endif
|
||||
EXPECT_EQ(path::is_style_windows(path::Style::native),
|
||||
path::is_separator('\\'));
|
||||
}
|
||||
|
||||
TEST(is_absolute_gnu, Works) {
|
||||
|
@ -107,6 +127,10 @@ TEST(is_absolute_gnu, Works) {
|
|||
std::get<1>(Path));
|
||||
EXPECT_EQ(path::is_absolute_gnu(std::get<0>(Path), path::Style::windows),
|
||||
std::get<2>(Path));
|
||||
|
||||
constexpr int Native = is_style_posix(path::Style::native) ? 1 : 2;
|
||||
EXPECT_EQ(path::is_absolute_gnu(std::get<0>(Path), path::Style::native),
|
||||
std::get<Native>(Path));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue