[llvm] 4e4883e - Support: Expose sys::path::is_style_{posix, windows, native}()

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 29 11:47:02 PDT 2021


Author: Duncan P. N. Exon Smith
Date: 2021-10-29T11:46:44-07:00
New Revision: 4e4883e1f394f7c47ff3adee48039aa8374bb8d0

URL: https://github.com/llvm/llvm-project/commit/4e4883e1f394f7c47ff3adee48039aa8374bb8d0
DIFF: https://github.com/llvm/llvm-project/commit/4e4883e1f394f7c47ff3adee48039aa8374bb8d0.diff

LOG: 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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Path.h b/llvm/include/llvm/Support/Path.h
index a3869e4f86bd0..5a253ea1f3290 100644
--- a/llvm/include/llvm/Support/Path.h
+++ b/llvm/include/llvm/Support/Path.h
@@ -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
 /// @{
 

diff  --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 5e9456c0ca58c..89fe9233fa0a9 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -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) {

diff  --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index 7f95e6cae517d..59d3f21e64cdd 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -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));
   }
 }
 


        


More information about the llvm-commits mailing list