diff --git a/llvm/include/llvm/Support/Path.h b/llvm/include/llvm/Support/Path.h --- a/llvm/include/llvm/Support/Path.h +++ b/llvm/include/llvm/Support/Path.h @@ -27,6 +27,27 @@ 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 /// @{ diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -37,11 +37,9 @@ 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) { diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -69,6 +69,29 @@ 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 @@ 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 @@ 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(Path)); } }