[Lldb-commits] [lldb] r333540 - [FileSpec] Re-implmenet removeLastPathComponent
Jonas Devlieghere via lldb-commits
lldb-commits at lists.llvm.org
Wed May 30 06:03:16 PDT 2018
Author: jdevlieghere
Date: Wed May 30 06:03:16 2018
New Revision: 333540
URL: http://llvm.org/viewvc/llvm-project?rev=333540&view=rev
Log:
[FileSpec] Re-implmenet removeLastPathComponent
When reading DBGSourcePathRemapping from a dSYM, we remove the last two
path components to make the source lookup more general. However, when
dealing with a relative path that has less than 2 components, we ended
up with an invalid (empty) FileSpec.
This patch changes the behavior of removeLastPathComponent to remove the
last path component, if possible. It does this by checking whether a
parent path exists, and if so using that as the new path. We rely
entirely on LLVM's path implementation to do the heavy lifting.
We now also return a boolean which indicates whether the operator was
successful or not.
Differential revision: https://reviews.llvm.org/D47495
rdar://37791687
Modified:
lldb/trunk/include/lldb/Utility/FileSpec.h
lldb/trunk/source/Utility/FileSpec.cpp
lldb/trunk/unittests/Utility/FileSpecTest.cpp
Modified: lldb/trunk/include/lldb/Utility/FileSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/FileSpec.h?rev=333540&r1=333539&r2=333540&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/FileSpec.h (original)
+++ lldb/trunk/include/lldb/Utility/FileSpec.h Wed May 30 06:03:16 2018
@@ -532,7 +532,14 @@ public:
void AppendPathComponent(llvm::StringRef component);
void AppendPathComponent(const FileSpec &new_path);
- void RemoveLastPathComponent();
+ //------------------------------------------------------------------
+ /// Removes the last path component by replacing the current path with its
+ /// parent. When the current path has no parent, this is a no-op.
+ ///
+ /// @return
+ /// A boolean value indicating whether the path was updated.
+ //------------------------------------------------------------------
+ bool RemoveLastPathComponent();
ConstString GetLastPathComponent() const;
Modified: lldb/trunk/source/Utility/FileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/FileSpec.cpp?rev=333540&r1=333539&r2=333540&view=diff
==============================================================================
--- lldb/trunk/source/Utility/FileSpec.cpp (original)
+++ lldb/trunk/source/Utility/FileSpec.cpp Wed May 30 06:03:16 2018
@@ -785,36 +785,15 @@ void FileSpec::AppendPathComponent(const
return AppendPathComponent(new_path.GetPath(false));
}
-void FileSpec::RemoveLastPathComponent() {
- // CLEANUP: Use StringRef for string handling.
-
- const bool resolve = false;
- if (m_filename.IsEmpty() && m_directory.IsEmpty()) {
- SetFile("", resolve);
- return;
- }
- if (m_directory.IsEmpty()) {
- SetFile("", resolve);
- return;
+bool FileSpec::RemoveLastPathComponent() {
+ llvm::SmallString<64> current_path;
+ GetPath(current_path, false);
+ if (llvm::sys::path::has_parent_path(current_path, m_style)) {
+ SetFile(llvm::sys::path::parent_path(current_path, m_style), false,
+ m_style);
+ return true;
}
- if (m_filename.IsEmpty()) {
- const char *dir_cstr = m_directory.GetCString();
- const char *last_slash_ptr = ::strrchr(dir_cstr, '/');
-
- // check for obvious cases before doing the full thing
- if (!last_slash_ptr) {
- SetFile("", resolve);
- return;
- }
- if (last_slash_ptr == dir_cstr) {
- SetFile("/", resolve);
- return;
- }
- size_t last_slash_pos = last_slash_ptr - dir_cstr + 1;
- ConstString new_path(dir_cstr, last_slash_pos);
- SetFile(new_path.GetCString(), resolve);
- } else
- SetFile(m_directory.GetCString(), resolve);
+ return false;
}
//------------------------------------------------------------------
/// Returns true if the filespec represents an implementation source
Modified: lldb/trunk/unittests/Utility/FileSpecTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/FileSpecTest.cpp?rev=333540&r1=333539&r2=333540&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/FileSpecTest.cpp (original)
+++ lldb/trunk/unittests/Utility/FileSpecTest.cpp Wed May 30 06:03:16 2018
@@ -320,3 +320,44 @@ TEST(FileSpecTest, IsRelative) {
}
}
+TEST(FileSpecTest, RemoveLastPathComponent) {
+ FileSpec fs_posix("/foo/bar/baz", false, FileSpec::Style::posix);
+ EXPECT_STREQ("/foo/bar/baz", fs_posix.GetCString());
+ EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
+ EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
+ EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
+ EXPECT_STREQ("/foo", fs_posix.GetCString());
+ EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
+ EXPECT_STREQ("/", fs_posix.GetCString());
+ EXPECT_FALSE(fs_posix.RemoveLastPathComponent());
+ EXPECT_STREQ("/", fs_posix.GetCString());
+
+ FileSpec fs_posix_relative("./foo/bar/baz", false, FileSpec::Style::posix);
+ EXPECT_STREQ("foo/bar/baz", fs_posix_relative.GetCString());
+ EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
+ EXPECT_STREQ("foo/bar", fs_posix_relative.GetCString());
+ EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
+ EXPECT_STREQ("foo", fs_posix_relative.GetCString());
+ EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
+ EXPECT_STREQ("foo", fs_posix_relative.GetCString());
+
+ FileSpec fs_posix_relative2("./", false, FileSpec::Style::posix);
+ EXPECT_STREQ(".", fs_posix_relative2.GetCString());
+ EXPECT_FALSE(fs_posix_relative2.RemoveLastPathComponent());
+ EXPECT_STREQ(".", fs_posix_relative2.GetCString());
+ EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
+ EXPECT_STREQ(".", fs_posix_relative2.GetCString());
+
+ FileSpec fs_windows("C:\\foo\\bar\\baz", false, FileSpec::Style::windows);
+ EXPECT_STREQ("C:\\foo\\bar\\baz", fs_windows.GetCString());
+ EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
+ EXPECT_STREQ("C:\\foo\\bar", fs_windows.GetCString());
+ EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
+ EXPECT_STREQ("C:\\foo", fs_windows.GetCString());
+ EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
+ EXPECT_STREQ("C:\\", fs_windows.GetCString());
+ EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
+ EXPECT_STREQ("C:", fs_windows.GetCString());
+ EXPECT_FALSE(fs_windows.RemoveLastPathComponent());
+ EXPECT_STREQ("C:", fs_windows.GetCString());
+}
More information about the lldb-commits
mailing list