[PATCH] D112288: Support: Expose sys::path::system_style()

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 21 18:32:04 PDT 2021


dexonsmith created this revision.
dexonsmith added a reviewer: Bigcheese.
Herald added a subscriber: hiraditya.
dexonsmith requested review of this revision.
Herald added a project: LLVM.

Expose sys::path::system_style(), matching the name of the analogous
sys::endian::system_endianness(). This is a constexpr function that
returns the "real" style that sys::path::Style::native is equivalent to,
and will allow a bunch of path-related code to stop checking `_WIN32`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112288

Files:
  llvm/include/llvm/Support/Path.h
  llvm/lib/Support/Path.cpp
  llvm/unittests/Support/Path.cpp


Index: llvm/unittests/Support/Path.cpp
===================================================================
--- llvm/unittests/Support/Path.cpp
+++ llvm/unittests/Support/Path.cpp
@@ -69,6 +69,10 @@
   int FD;
 };
 
+TEST(system_style, NotNative) {
+  EXPECT_NE(path::Style::native, path::system_style());
+}
+
 TEST(is_separator, Works) {
   EXPECT_TRUE(path::is_separator('/'));
   EXPECT_FALSE(path::is_separator('\0'));
@@ -78,11 +82,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::system_style() == path::Style::windows,
+            path::is_separator('\\'));
 }
 
 TEST(is_absolute_gnu, Works) {
@@ -107,6 +108,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 = path::system_style() == path::Style::posix ? 1 : 2;
+    EXPECT_EQ(path::is_absolute_gnu(std::get<0>(Path), path::Style::native),
+              std::get<Native>(Path));
   }
 }
 
Index: llvm/lib/Support/Path.cpp
===================================================================
--- llvm/lib/Support/Path.cpp
+++ llvm/lib/Support/Path.cpp
@@ -37,11 +37,7 @@
   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
+    return style == Style::native ? llvm::sys::path::system_style() : style;
   }
 
   inline const char *separators(Style style) {
Index: llvm/include/llvm/Support/Path.h
===================================================================
--- llvm/include/llvm/Support/Path.h
+++ llvm/include/llvm/Support/Path.h
@@ -27,6 +27,16 @@
 
 enum class Style { windows, posix, native };
 
+/// Return \a Style::windows or \a Style::posix, whichever one is equivalent to
+/// \a Style::native on the current host.
+constexpr Style system_style() {
+#if defined(_WIN32)
+  return Style::windows;
+#else
+  return Style::posix;
+#endif
+}
+
 /// @name Lexical Component Iterator
 /// @{
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112288.381452.patch
Type: text/x-patch
Size: 2310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211022/fd8675ee/attachment.bin>


More information about the llvm-commits mailing list