Skip to content

Commit 4e4883e

Browse files
committed
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
1 parent d0e9879 commit 4e4883e

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

llvm/include/llvm/Support/Path.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ namespace path {
2727

2828
enum class Style { windows, posix, native };
2929

30+
/// Check if \p S uses POSIX path rules.
31+
constexpr bool is_style_posix(Style S) {
32+
if (S == Style::posix)
33+
return true;
34+
if (S != Style::native)
35+
return false;
36+
#if defined(_WIN32)
37+
return false;
38+
#else
39+
return true;
40+
#endif
41+
}
42+
43+
/// Check if \p S uses Windows path rules.
44+
constexpr bool is_style_windows(Style S) { return !is_style_posix(S); }
45+
46+
/// Check if \p S uses the same path rules as Style::native.
47+
constexpr bool is_style_native(Style S) {
48+
return is_style_posix(S) == is_style_posix(Style::native);
49+
}
50+
3051
/// @name Lexical Component Iterator
3152
/// @{
3253

llvm/lib/Support/Path.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ namespace {
3737
using llvm::sys::path::Style;
3838

3939
inline Style real_style(Style style) {
40-
#ifdef _WIN32
41-
return (style == Style::posix) ? Style::posix : Style::windows;
42-
#else
43-
return (style == Style::windows) ? Style::windows : Style::posix;
44-
#endif
40+
if (is_style_posix(style))
41+
return Style::posix;
42+
return Style::windows;
4543
}
4644

4745
inline const char *separators(Style style) {

llvm/unittests/Support/Path.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ struct FileDescriptorCloser {
6969
int FD;
7070
};
7171

72+
TEST(is_style_Style, Works) {
73+
using namespace llvm::sys::path;
74+
// Check platform-independent results.
75+
EXPECT_TRUE(is_style_posix(Style::posix));
76+
EXPECT_TRUE(is_style_windows(Style::windows));
77+
EXPECT_FALSE(is_style_posix(Style::windows));
78+
EXPECT_FALSE(is_style_windows(Style::posix));
79+
80+
// Check platform-dependent results.
81+
#if defined(_WIN32)
82+
EXPECT_FALSE(is_style_posix(Style::native));
83+
EXPECT_TRUE(is_style_windows(Style::native));
84+
#else
85+
EXPECT_TRUE(is_style_posix(Style::native));
86+
EXPECT_FALSE(is_style_windows(Style::native));
87+
#endif
88+
89+
// Check is_style_native().
90+
EXPECT_TRUE(is_style_native(Style::native));
91+
EXPECT_EQ(is_style_posix(Style::native), is_style_native(Style::posix));
92+
EXPECT_EQ(is_style_windows(Style::native), is_style_native(Style::windows));
93+
}
94+
7295
TEST(is_separator, Works) {
7396
EXPECT_TRUE(path::is_separator('/'));
7497
EXPECT_FALSE(path::is_separator('\0'));
@@ -78,11 +101,8 @@ TEST(is_separator, Works) {
78101
EXPECT_TRUE(path::is_separator('\\', path::Style::windows));
79102
EXPECT_FALSE(path::is_separator('\\', path::Style::posix));
80103

81-
#ifdef _WIN32
82-
EXPECT_TRUE(path::is_separator('\\'));
83-
#else
84-
EXPECT_FALSE(path::is_separator('\\'));
85-
#endif
104+
EXPECT_EQ(path::is_style_windows(path::Style::native),
105+
path::is_separator('\\'));
86106
}
87107

88108
TEST(is_absolute_gnu, Works) {
@@ -107,6 +127,10 @@ TEST(is_absolute_gnu, Works) {
107127
std::get<1>(Path));
108128
EXPECT_EQ(path::is_absolute_gnu(std::get<0>(Path), path::Style::windows),
109129
std::get<2>(Path));
130+
131+
constexpr int Native = is_style_posix(path::Style::native) ? 1 : 2;
132+
EXPECT_EQ(path::is_absolute_gnu(std::get<0>(Path), path::Style::native),
133+
std::get<Native>(Path));
110134
}
111135
}
112136

0 commit comments

Comments
 (0)