[libcxx-commits] [libcxx] [libc++] Fix different slashes confuses lexically_proximate and lexically_relative (PR #99780)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jul 21 01:26:15 PDT 2024


https://github.com/RichardLuo0 updated https://github.com/llvm/llvm-project/pull/99780

>From 6e40c544bfa33923c766ff7459af3e377b79cbf9 Mon Sep 17 00:00:00 2001
From: RichardLuo <22096448+RichardLuo0 at users.noreply.github.com>
Date: Sun, 21 Jul 2024 03:12:56 +0800
Subject: [PATCH 1/4] [libc++] Use forward slash for root dir to fix
 lexically_relative on windows

---
 libcxx/src/filesystem/path_parser.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/libcxx/src/filesystem/path_parser.h b/libcxx/src/filesystem/path_parser.h
index 06623696452da..b38df13a3f7df 100644
--- a/libcxx/src/filesystem/path_parser.h
+++ b/libcxx/src/filesystem/path_parser.h
@@ -174,10 +174,7 @@ struct PathParser {
     case PS_AtEnd:
       return PATHSTR("");
     case PS_InRootDir:
-      if (RawEntry[0] == '\\')
-        return PATHSTR("\\");
-      else
-        return PATHSTR("/");
+      return PATHSTR("/");
     case PS_InTrailingSep:
       return PATHSTR("");
     case PS_InRootName:

>From 4efa8ffdabe5877941d334576275d37ccbcf3d43 Mon Sep 17 00:00:00 2001
From: RichardLuo <22096448+RichardLuo0 at users.noreply.github.com>
Date: Sun, 21 Jul 2024 13:59:26 +0800
Subject: [PATCH 2/4] [libc++] the root_directory() still returns the original
 root_directory

---
 libcxx/src/filesystem/path.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/src/filesystem/path.cpp b/libcxx/src/filesystem/path.cpp
index b2019521377ed..63b4c76bf5073 100644
--- a/libcxx/src/filesystem/path.cpp
+++ b/libcxx/src/filesystem/path.cpp
@@ -55,7 +55,7 @@ string_view_t path::__root_directory() const {
   if (PP.State_ == PathParser::PS_InRootName)
     ++PP;
   if (PP.State_ == PathParser::PS_InRootDir)
-    return *PP;
+    return PP.RawEntry;
   return {};
 }
 

>From 572ed17cb895614a7f9855fe5592a38b73f88c95 Mon Sep 17 00:00:00 2001
From: RichardLuo <22096448+RichardLuo0 at users.noreply.github.com>
Date: Sun, 21 Jul 2024 14:03:24 +0800
Subject: [PATCH 3/4] change the tests

---
 .../path.member/path.decompose/path.decompose.pass.cpp    | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp b/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp
index 6013172e0c671..9a1b581fa112c 100644
--- a/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp
@@ -128,11 +128,11 @@ const PathDecomposeTestcase PathTestCases[] =
 #endif
     , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
 #ifdef _WIN32
-    , {"c:\\", {"c:", "\\"}, "c:\\", "c:", "\\", "", "c:\\", ""}
-    , {"c:\\foo", {"c:", "\\", "foo"}, "c:\\", "c:", "\\", "foo", "c:\\", "foo"}
+    , {"c:\\", {"c:", "/"}, "c:\\", "c:", "\\", "", "c:\\", ""}
+    , {"c:\\foo", {"c:", "/", "foo"}, "c:\\", "c:", "\\", "foo", "c:\\", "foo"}
     , {"c:foo\\", {"c:", "foo", ""}, "c:", "c:", "", "foo\\", "c:foo", ""}
-    , {"c:\\foo\\", {"c:", "\\", "foo", ""}, "c:\\", "c:", "\\", "foo\\", "c:\\foo", ""}
-    , {"c:\\foo/",  {"c:", "\\", "foo", ""}, "c:\\", "c:", "\\", "foo/", "c:\\foo", ""}
+    , {"c:\\foo\\", {"c:", "/", "foo", ""}, "c:\\", "c:", "\\", "foo\\", "c:\\foo", ""}
+    , {"c:\\foo/",  {"c:", "/", "foo", ""}, "c:\\", "c:", "\\", "foo/", "c:\\foo", ""}
     , {"c:/foo\\bar", {"c:", "/", "foo", "bar"}, "c:\\", "c:", "\\", "foo\\bar", "c:/foo", "bar"}
 #else
     , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}

>From af388ed61dd46c81aff3d258bda12a8f811a10e1 Mon Sep 17 00:00:00 2001
From: RichardLuo <22096448+RichardLuo0 at users.noreply.github.com>
Date: Sun, 21 Jul 2024 16:16:11 +0800
Subject: [PATCH 4/4] fix format

---
 .../path.decompose/path.decompose.pass.cpp    | 184 ++++++++----------
 1 file changed, 85 insertions(+), 99 deletions(-)

diff --git a/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp b/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp
index 9a1b581fa112c..f98515a820fb0 100644
--- a/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp
+++ b/libcxx/test/std/input.output/filesystems/class.path/path.member/path.decompose/path.decompose.pass.cpp
@@ -43,7 +43,6 @@
 // iterator begin() const;
 // iterator end() const;
 
-
 #include <filesystem>
 #include <algorithm>
 #include <cassert>
@@ -59,96 +58,90 @@
 namespace fs = std::filesystem;
 
 struct ComparePathExact {
-  bool operator()(fs::path const& LHS, std::string const& RHS) const {
-    return LHS.string() == RHS;
-  }
+  bool operator()(fs::path const& LHS, std::string const& RHS) const { return LHS.string() == RHS; }
 };
 
-struct PathDecomposeTestcase
-{
-    std::string raw;
-    std::vector<std::string> elements;
-    std::string root_path;
-    std::string root_name;
-    std::string root_directory;
-    std::string relative_path;
-    std::string parent_path;
-    std::string filename;
+struct PathDecomposeTestcase {
+  std::string raw;
+  std::vector<std::string> elements;
+  std::string root_path;
+  std::string root_name;
+  std::string root_directory;
+  std::string relative_path;
+  std::string parent_path;
+  std::string filename;
 };
 
-const PathDecomposeTestcase PathTestCases[] =
-  {
-      {"", {}, "", "", "", "", "", ""}
-    , {".", {"."}, "", "", "", ".", "", "."}
-    , {"..", {".."}, "", "", "", "..", "", ".."}
-    , {"foo", {"foo"}, "", "", "", "foo", "", "foo"}
-    , {"/", {"/"}, "/", "", "/", "", "/", ""}
-    , {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"}
-    , {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""}
-    , {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""}
-    , {"foo/bar", {"foo","bar"}, "",  "", "",  "foo/bar", "foo", "bar"}
-    , {"/foo//bar", {"/","foo","bar"}, "/", "", "/", "foo/bar", "/foo", "bar"}
+const PathDecomposeTestcase PathTestCases[] = {
+    {"", {}, "", "", "", "", "", ""},
+    {".", {"."}, "", "", "", ".", "", "."},
+    {"..", {".."}, "", "", "", "..", "", ".."},
+    {"foo", {"foo"}, "", "", "", "foo", "", "foo"},
+    {"/", {"/"}, "/", "", "/", "", "/", ""},
+    {"/foo", {"/", "foo"}, "/", "", "/", "foo", "/", "foo"},
+    {"foo/", {"foo", ""}, "", "", "", "foo/", "foo", ""},
+    {"/foo/", {"/", "foo", ""}, "/", "", "/", "foo/", "/foo", ""},
+    {"foo/bar", {"foo", "bar"}, "", "", "", "foo/bar", "foo", "bar"},
+    {"/foo//bar", {"/", "foo", "bar"}, "/", "", "/", "foo/bar", "/foo", "bar"},
 #ifdef _WIN32
-    , {"//net", {"//net"}, "//net", "//net", "", "", "//net", ""}
-    , {"//net/", {"//net", "/"}, "//net/", "//net", "/", "", "//net/", ""}
-    , {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"}
+    {"//net", {"//net"}, "//net", "//net", "", "", "//net", ""},
+    {"//net/", {"//net", "/"}, "//net/", "//net", "/", "", "//net/", ""},
+    {"//net/foo", {"//net", "/", "foo"}, "//net/", "//net", "/", "foo", "//net/", "foo"},
 #else
-    , {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"}
-    , {"//net/", {"/", "net", ""}, "/", "", "/", "net/", "//net", ""}
-    , {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"}
+    {"//net", {"/", "net"}, "/", "", "/", "net", "/", "net"},
+    {"//net/", {"/", "net", ""}, "/", "", "/", "net/", "//net", ""},
+    {"//net/foo", {"/", "net", "foo"}, "/", "", "/", "net/foo", "/net", "foo"},
 #endif
-    , {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""}
-    , {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"}
-    , {"/.", {"/", "."}, "/", "", "/", ".", "/", "."}
-    , {"./", {".", ""}, "", "", "", "./", ".", ""}
-    , {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."}
-    , {"../", {"..", ""}, "", "", "", "../", "..", ""}
-    , {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."}
-    , {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."}
-    , {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""}
-    , {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"}
-    , {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""}
-    , {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"}
+    {"///foo///", {"/", "foo", ""}, "/", "", "/", "foo///", "///foo", ""},
+    {"///foo///bar", {"/", "foo", "bar"}, "/", "", "/", "foo///bar", "///foo", "bar"},
+    {"/.", {"/", "."}, "/", "", "/", ".", "/", "."},
+    {"./", {".", ""}, "", "", "", "./", ".", ""},
+    {"/..", {"/", ".."}, "/", "", "/", "..", "/", ".."},
+    {"../", {"..", ""}, "", "", "", "../", "..", ""},
+    {"foo/.", {"foo", "."}, "", "", "", "foo/.", "foo", "."},
+    {"foo/..", {"foo", ".."}, "", "", "", "foo/..", "foo", ".."},
+    {"foo/./", {"foo", ".", ""}, "", "", "", "foo/./", "foo/.", ""},
+    {"foo/./bar", {"foo", ".", "bar"}, "", "", "", "foo/./bar", "foo/.", "bar"},
+    {"foo/../", {"foo", "..", ""}, "", "", "", "foo/../", "foo/..", ""},
+    {"foo/../bar", {"foo", "..", "bar"}, "", "", "", "foo/../bar", "foo/..", "bar"},
 #ifdef _WIN32
-    , {"c:", {"c:"}, "c:", "c:", "", "", "c:", ""}
-    , {"c:/", {"c:", "/"}, "c:/", "c:", "/", "", "c:/", ""}
-    , {"c:foo", {"c:", "foo"}, "c:", "c:", "", "foo", "c:", "foo"}
-    , {"c:/foo", {"c:", "/", "foo"}, "c:/", "c:", "/", "foo", "c:/", "foo"}
-    , {"c:foo/", {"c:", "foo", ""}, "c:", "c:", "", "foo/", "c:foo", ""}
-    , {"c:/foo/", {"c:", "/", "foo", ""}, "c:/", "c:", "/", "foo/",  "c:/foo", ""}
-    , {"c:/foo/bar", {"c:", "/", "foo", "bar"}, "c:/", "c:", "/", "foo/bar", "c:/foo", "bar"}
+    {"c:", {"c:"}, "c:", "c:", "", "", "c:", ""},
+    {"c:/", {"c:", "/"}, "c:/", "c:", "/", "", "c:/", ""},
+    {"c:foo", {"c:", "foo"}, "c:", "c:", "", "foo", "c:", "foo"},
+    {"c:/foo", {"c:", "/", "foo"}, "c:/", "c:", "/", "foo", "c:/", "foo"},
+    {"c:foo/", {"c:", "foo", ""}, "c:", "c:", "", "foo/", "c:foo", ""},
+    {"c:/foo/", {"c:", "/", "foo", ""}, "c:/", "c:", "/", "foo/", "c:/foo", ""},
+    {"c:/foo/bar", {"c:", "/", "foo", "bar"}, "c:/", "c:", "/", "foo/bar", "c:/foo", "bar"},
 #else
-    , {"c:", {"c:"}, "", "", "", "c:", "", "c:"}
-    , {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""}
-    , {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"}
-    , {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"}
-    , {"c:foo/", {"c:foo", ""}, "", "", "", "c:foo/", "c:foo", ""}
-    , {"c:/foo/", {"c:", "foo", ""}, "", "", "", "c:/foo/",  "c:/foo", ""}
-    , {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"}
+    {"c:", {"c:"}, "", "", "", "c:", "", "c:"},
+    {"c:/", {"c:", ""}, "", "", "", "c:/", "c:", ""},
+    {"c:foo", {"c:foo"}, "", "", "", "c:foo", "", "c:foo"},
+    {"c:/foo", {"c:", "foo"}, "", "", "", "c:/foo", "c:", "foo"},
+    {"c:foo/", {"c:foo", ""}, "", "", "", "c:foo/", "c:foo", ""},
+    {"c:/foo/", {"c:", "foo", ""}, "", "", "", "c:/foo/", "c:/foo", ""},
+    {"c:/foo/bar", {"c:", "foo", "bar"}, "", "", "", "c:/foo/bar", "c:/foo", "bar"},
 #endif
-    , {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"}
+    {"prn:", {"prn:"}, "", "", "", "prn:", "", "prn:"},
 #ifdef _WIN32
-    , {"c:\\", {"c:", "/"}, "c:\\", "c:", "\\", "", "c:\\", ""}
-    , {"c:\\foo", {"c:", "/", "foo"}, "c:\\", "c:", "\\", "foo", "c:\\", "foo"}
-    , {"c:foo\\", {"c:", "foo", ""}, "c:", "c:", "", "foo\\", "c:foo", ""}
-    , {"c:\\foo\\", {"c:", "/", "foo", ""}, "c:\\", "c:", "\\", "foo\\", "c:\\foo", ""}
-    , {"c:\\foo/",  {"c:", "/", "foo", ""}, "c:\\", "c:", "\\", "foo/", "c:\\foo", ""}
-    , {"c:/foo\\bar", {"c:", "/", "foo", "bar"}, "c:\\", "c:", "\\", "foo\\bar", "c:/foo", "bar"}
+    {"c:\\", {"c:", "/"}, "c:\\", "c:", "\\", "", "c:\\", ""},
+    {"c:\\foo", {"c:", "/", "foo"}, "c:\\", "c:", "\\", "foo", "c:\\", "foo"},
+    {"c:foo\\", {"c:", "foo", ""}, "c:", "c:", "", "foo\\", "c:foo", ""},
+    {"c:\\foo\\", {"c:", "/", "foo", ""}, "c:\\", "c:", "\\", "foo\\", "c:\\foo", ""},
+    {"c:\\foo/", {"c:", "/", "foo", ""}, "c:\\", "c:", "\\", "foo/", "c:\\foo", ""},
+    {"c:/foo\\bar", {"c:", "/", "foo", "bar"}, "c:\\", "c:", "\\", "foo\\bar", "c:/foo", "bar"},
 #else
-    , {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"}
-    , {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"}
-    , {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"}
-    , {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"}
-    , {"c:\\foo/",  {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""}
-    , {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"}
+    {"c:\\", {"c:\\"}, "", "", "", "c:\\", "", "c:\\"},
+    {"c:\\foo", {"c:\\foo"}, "", "", "", "c:\\foo", "", "c:\\foo"},
+    {"c:foo\\", {"c:foo\\"}, "", "", "", "c:foo\\", "", "c:foo\\"},
+    {"c:\\foo\\", {"c:\\foo\\"}, "", "", "", "c:\\foo\\", "", "c:\\foo\\"},
+    {"c:\\foo/", {"c:\\foo", ""}, "", "", "", "c:\\foo/", "c:\\foo", ""},
+    {"c:/foo\\bar", {"c:", "foo\\bar"}, "", "", "", "c:/foo\\bar", "c:", "foo\\bar"},
 #endif
-    , {"//", {"/"}, "/", "", "/", "", "/", ""}
-  };
+    {"//", {"/"}, "/", "", "/", "", "/", ""}};
 
-void decompPathTest()
-{
+void decompPathTest() {
   using namespace fs;
-  for (auto const & TC : PathTestCases) {
+  for (auto const& TC : PathTestCases) {
     fs::path p(TC.raw);
     assert(p == TC.raw);
 
@@ -204,40 +197,34 @@ void decompPathTest()
 
     // check backwards
     std::vector<fs::path> Parts;
-    for (auto it = p.end(); it != p.begin(); )
+    for (auto it = p.end(); it != p.begin();)
       Parts.push_back(*--it);
     assert(static_cast<std::size_t>(std::distance(Parts.begin(), Parts.end())) == TC.elements.size());
     assert(std::equal(Parts.begin(), Parts.end(), TC.elements.rbegin(), ComparePathExact()));
   }
 }
 
-
-struct FilenameDecompTestcase
-{
+struct FilenameDecompTestcase {
   std::string raw;
   std::string filename;
   std::string stem;
   std::string extension;
 };
 
-const FilenameDecompTestcase FilenameTestCases[] =
-{
-    {"", "", "", ""}
-  , {".", ".", ".", ""}
-  , {"..", "..", "..", ""}
-  , {"/", "", "", ""}
-  , {"foo", "foo", "foo", ""}
-  , {"/foo/bar.txt", "bar.txt", "bar", ".txt"}
-  , {"foo..txt", "foo..txt", "foo.", ".txt"}
-  , {".profile", ".profile", ".profile", ""}
-  , {".profile.txt", ".profile.txt", ".profile", ".txt"}
-};
-
-
-void decompFilenameTest()
-{
+const FilenameDecompTestcase FilenameTestCases[] = {
+    {"", "", "", ""},
+    {".", ".", ".", ""},
+    {"..", "..", "..", ""},
+    {"/", "", "", ""},
+    {"foo", "foo", "foo", ""},
+    {"/foo/bar.txt", "bar.txt", "bar", ".txt"},
+    {"foo..txt", "foo..txt", "foo.", ".txt"},
+    {".profile", ".profile", ".profile", ""},
+    {".profile.txt", ".profile.txt", ".profile", ".txt"}};
+
+void decompFilenameTest() {
   using namespace fs;
-  for (auto const & TC : FilenameTestCases) {
+  for (auto const& TC : FilenameTestCases) {
     fs::path p(TC.raw);
     assert(p == TC.raw);
     ASSERT_NOEXCEPT(p.empty());
@@ -253,8 +240,7 @@ void decompFilenameTest()
   }
 }
 
-int main(int, char**)
-{
+int main(int, char**) {
   decompPathTest();
   decompFilenameTest();
 



More information about the libcxx-commits mailing list