From e7306b105eb7f0aeac48f380ffb7b855995dabfd Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 11 May 2018 11:55:34 +0000 Subject: [PATCH] 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 llvm-svn: 332088 --- lldb/source/Utility/FileSpec.cpp | 90 ++----------------------- lldb/unittests/Utility/FileSpecTest.cpp | 8 +-- 2 files changed, 9 insertions(+), 89 deletions(-) diff --git a/lldb/source/Utility/FileSpec.cpp b/lldb/source/Utility/FileSpec.cpp index 2168948fa81c..48b5702db991 100644 --- a/lldb/source/Utility/FileSpec.cpp +++ b/lldb/source/Utility/FileSpec.cpp @@ -82,67 +82,6 @@ void Denormalize(llvm::SmallVectorImpl &path, 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 &path) { @@ -312,10 +251,6 @@ const FileSpec &FileSpec::operator=(const FileSpec &rhs) { //------------------------------------------------------------------ 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 pathname, bool resolve, 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, diff --git a/lldb/unittests/Utility/FileSpecTest.cpp b/lldb/unittests/Utility/FileSpecTest.cpp index ba58d9ca706c..3e66474e4e7a 100644 --- a/lldb/unittests/Utility/FileSpecTest.cpp +++ b/lldb/unittests/Utility/FileSpecTest.cpp @@ -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.