[Lldb-commits] [lldb] r332088 - Remove custom path manipulation functions from FileSpec

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Fri May 11 04:55:35 PDT 2018


Author: labath
Date: Fri May 11 04:55:34 2018
New Revision: 332088

URL: http://llvm.org/viewvc/llvm-project?rev=332088&view=rev
Log:
Remove custom path manipulation functions from FileSpec

Summary:
now that llvm supports host-agnostic path manipulation functions (and
most of their kinks have been ironed out), we can remove our copies of
the path parsing functions in favour of the llvm ones.

This should be NFC except for the slight difference in handling of the
"//" path, which is now normalized to "/" (this only applies to the
literal "//" path; "//net" and friends still get to keep the two
slashes).

Reviewers: zturner, clayborg

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D46687

Modified:
    lldb/trunk/source/Utility/FileSpec.cpp
    lldb/trunk/unittests/Utility/FileSpecTest.cpp

Modified: lldb/trunk/source/Utility/FileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/FileSpec.cpp?rev=332088&r1=332087&r2=332088&view=diff
==============================================================================
--- lldb/trunk/source/Utility/FileSpec.cpp (original)
+++ lldb/trunk/source/Utility/FileSpec.cpp Fri May 11 04:55:34 2018
@@ -82,67 +82,6 @@ void Denormalize(llvm::SmallVectorImpl<c
 
   std::replace(path.begin(), path.end(), '/', '\\');
 }
-
-size_t FilenamePos(llvm::StringRef str, FileSpec::PathSyntax syntax) {
-  if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1])
-    return 0;
-
-  if (str.size() > 0 && IsPathSeparator(str.back(), syntax))
-    return str.size() - 1;
-
-  size_t pos = str.find_last_of(GetPathSeparators(syntax), str.size() - 1);
-
-  if (!PathSyntaxIsPosix(syntax) && pos == llvm::StringRef::npos)
-    pos = str.find_last_of(':', str.size() - 2);
-
-  if (pos == llvm::StringRef::npos ||
-      (pos == 1 && IsPathSeparator(str[0], syntax)))
-    return 0;
-
-  return pos + 1;
-}
-
-size_t RootDirStart(llvm::StringRef str, FileSpec::PathSyntax syntax) {
-  // case "c:/"
-  if (!PathSyntaxIsPosix(syntax) &&
-      (str.size() > 2 && str[1] == ':' && IsPathSeparator(str[2], syntax)))
-    return 2;
-
-  // case "//"
-  if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1])
-    return llvm::StringRef::npos;
-
-  // case "//net"
-  if (str.size() > 3 && IsPathSeparator(str[0], syntax) && str[0] == str[1] &&
-      !IsPathSeparator(str[2], syntax))
-    return str.find_first_of(GetPathSeparators(syntax), 2);
-
-  // case "/"
-  if (str.size() > 0 && IsPathSeparator(str[0], syntax))
-    return 0;
-
-  return llvm::StringRef::npos;
-}
-
-size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) {
-  size_t end_pos = FilenamePos(path, syntax);
-
-  bool filename_was_sep =
-      path.size() > 0 && IsPathSeparator(path[end_pos], syntax);
-
-  // Skip separators except for root dir.
-  size_t root_dir_pos = RootDirStart(path.substr(0, end_pos), syntax);
-
-  while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
-         IsPathSeparator(path[end_pos - 1], syntax))
-    --end_pos;
-
-  if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
-    return llvm::StringRef::npos;
-
-  return end_pos;
-}
-
 } // end anonymous namespace
 
 void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
@@ -312,10 +251,6 @@ const FileSpec &FileSpec::operator=(cons
 //------------------------------------------------------------------
 void FileSpec::SetFile(llvm::StringRef pathname, bool resolve,
                        PathSyntax syntax) {
-  // CLEANUP: Use StringRef for string handling.  This function is kind of a
-  // mess and the unclear semantics of RootDirStart and ParentPathEnd make it
-  // very difficult to understand this function.  There's no reason this
-  // function should be particularly complicated or difficult to understand.
   m_filename.Clear();
   m_directory.Clear();
   m_is_resolved = false;
@@ -339,26 +274,11 @@ void FileSpec::SetFile(llvm::StringRef p
   if (m_syntax == FileSpec::ePathSyntaxWindows)
     std::replace(resolved.begin(), resolved.end(), '\\', '/');
 
-  llvm::StringRef resolve_path_ref(resolved.c_str());
-  size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax);
-  if (dir_end == 0) {
-    m_filename.SetString(resolve_path_ref);
-    return;
-  }
-
-  m_directory.SetString(resolve_path_ref.substr(0, dir_end));
-
-  size_t filename_begin = dir_end;
-  size_t root_dir_start = RootDirStart(resolve_path_ref, m_syntax);
-  while (filename_begin != llvm::StringRef::npos &&
-         filename_begin < resolve_path_ref.size() &&
-         filename_begin != root_dir_start &&
-         IsPathSeparator(resolve_path_ref[filename_begin], m_syntax))
-    ++filename_begin;
-  m_filename.SetString((filename_begin == llvm::StringRef::npos ||
-                        filename_begin >= resolve_path_ref.size())
-                           ? "."
-                           : resolve_path_ref.substr(filename_begin));
+  auto style = LLVMPathSyntax(syntax);
+  m_filename.SetString(llvm::sys::path::filename(resolved, style));
+  llvm::StringRef dir = llvm::sys::path::parent_path(resolved, style);
+  if (!dir.empty())
+    m_directory.SetString(dir);
 }
 
 void FileSpec::SetFile(llvm::StringRef path, bool resolve,

Modified: lldb/trunk/unittests/Utility/FileSpecTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/FileSpecTest.cpp?rev=332088&r1=332087&r2=332088&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/FileSpecTest.cpp (original)
+++ lldb/trunk/unittests/Utility/FileSpecTest.cpp Fri May 11 04:55:34 2018
@@ -195,8 +195,7 @@ TEST(FileSpecTest, GetNormalizedPath) {
       {"/foo//bar/./baz", "/foo/bar/baz"},
       {"/./foo", "/foo"},
       {"/", "/"},
-      // TODO: fix llvm::sys::path::remove_dots() to return "//" below.
-      //{"//", "//"},
+      {"//", "/"},
       {"//net", "//net"},
       {"/..", "/"},
       {"/.", "/"},
@@ -212,6 +211,7 @@ TEST(FileSpecTest, GetNormalizedPath) {
       {"../../foo", "../../foo"},
   };
   for (auto test : posix_tests) {
+    SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
     EXPECT_EQ(test.second,
               FileSpec(test.first, false, FileSpec::ePathSyntaxPosix)
                   .GetPath());
@@ -224,8 +224,8 @@ TEST(FileSpecTest, GetNormalizedPath) {
       {R"(c:\bar\.)", R"(c:\bar)"},
       {R"(c:\.\bar)", R"(c:\bar)"},
       {R"(\)", R"(\)"},
-      //      {R"(\\)", R"(\\)"},
-      //      {R"(\\net)", R"(\\net)"},
+      {R"(\\)", R"(\)"},
+      {R"(\\net)", R"(\\net)"},
       {R"(c:\..)", R"(c:\)"},
       {R"(c:\.)", R"(c:\)"},
       // TODO: fix llvm::sys::path::remove_dots() to return "\" below.




More information about the lldb-commits mailing list