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
This commit is contained in:
Pavel Labath 2018-05-11 11:55:34 +00:00
parent 5ad5c3c7ed
commit e7306b105e
2 changed files with 9 additions and 89 deletions

View File

@ -82,67 +82,6 @@ void Denormalize(llvm::SmallVectorImpl<char> &path,
std::replace(path.begin(), path.end(), '/', '\\'); 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 } // end anonymous namespace
void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) { void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
@ -312,10 +251,6 @@ const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
//------------------------------------------------------------------ //------------------------------------------------------------------
void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, void FileSpec::SetFile(llvm::StringRef pathname, bool resolve,
PathSyntax syntax) { 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_filename.Clear();
m_directory.Clear(); m_directory.Clear();
m_is_resolved = false; m_is_resolved = false;
@ -339,26 +274,11 @@ void FileSpec::SetFile(llvm::StringRef pathname, bool resolve,
if (m_syntax == FileSpec::ePathSyntaxWindows) if (m_syntax == FileSpec::ePathSyntaxWindows)
std::replace(resolved.begin(), resolved.end(), '\\', '/'); std::replace(resolved.begin(), resolved.end(), '\\', '/');
llvm::StringRef resolve_path_ref(resolved.c_str()); auto style = LLVMPathSyntax(syntax);
size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax); m_filename.SetString(llvm::sys::path::filename(resolved, style));
if (dir_end == 0) { llvm::StringRef dir = llvm::sys::path::parent_path(resolved, style);
m_filename.SetString(resolve_path_ref); if (!dir.empty())
return; m_directory.SetString(dir);
}
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));
} }
void FileSpec::SetFile(llvm::StringRef path, bool resolve, void FileSpec::SetFile(llvm::StringRef path, bool resolve,

View File

@ -195,8 +195,7 @@ TEST(FileSpecTest, GetNormalizedPath) {
{"/foo//bar/./baz", "/foo/bar/baz"}, {"/foo//bar/./baz", "/foo/bar/baz"},
{"/./foo", "/foo"}, {"/./foo", "/foo"},
{"/", "/"}, {"/", "/"},
// TODO: fix llvm::sys::path::remove_dots() to return "//" below. {"//", "/"},
//{"//", "//"},
{"//net", "//net"}, {"//net", "//net"},
{"/..", "/"}, {"/..", "/"},
{"/.", "/"}, {"/.", "/"},
@ -212,6 +211,7 @@ TEST(FileSpecTest, GetNormalizedPath) {
{"../../foo", "../../foo"}, {"../../foo", "../../foo"},
}; };
for (auto test : posix_tests) { for (auto test : posix_tests) {
SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
EXPECT_EQ(test.second, EXPECT_EQ(test.second,
FileSpec(test.first, false, FileSpec::ePathSyntaxPosix) FileSpec(test.first, false, FileSpec::ePathSyntaxPosix)
.GetPath()); .GetPath());
@ -224,8 +224,8 @@ TEST(FileSpecTest, GetNormalizedPath) {
{R"(c:\bar\.)", R"(c:\bar)"}, {R"(c:\bar\.)", R"(c:\bar)"},
{R"(c:\.\bar)", R"(c:\bar)"}, {R"(c:\.\bar)", R"(c:\bar)"},
{R"(\)", R"(\)"}, {R"(\)", R"(\)"},
// {R"(\\)", R"(\\)"}, {R"(\\)", R"(\)"},
// {R"(\\net)", R"(\\net)"}, {R"(\\net)", R"(\\net)"},
{R"(c:\..)", R"(c:\)"}, {R"(c:\..)", R"(c:\)"},
{R"(c:\.)", R"(c:\)"}, {R"(c:\.)", R"(c:\)"},
// TODO: fix llvm::sys::path::remove_dots() to return "\" below. // TODO: fix llvm::sys::path::remove_dots() to return "\" below.