[Lldb-commits] [lldb] 57154a6 - [lldb] Introduce FileSpec::GetComponents
Alex Langford via lldb-commits
lldb-commits at lists.llvm.org
Tue May 30 15:13:33 PDT 2023
Author: Alex Langford
Date: 2023-05-30T15:13:17-07:00
New Revision: 57154a63a07f732552968141136279350bcdf99d
URL: https://github.com/llvm/llvm-project/commit/57154a63a07f732552968141136279350bcdf99d
DIFF: https://github.com/llvm/llvm-project/commit/57154a63a07f732552968141136279350bcdf99d.diff
LOG: [lldb] Introduce FileSpec::GetComponents
This patch introduces FileSpec::GetComponents, a method that splits a
FileSpec's path into its individual components. For example, given
/foo/bar/baz, you'll get back a vector of strings {"foo", "bar", baz"}.
The motivation here is to reduce the use of
`FileSpec::RemoveLastPathComponent`. Mutating a FileSpec is expensive,
so providing a way of doing this without mutation is useful.
Differential Revision: https://reviews.llvm.org/D151399
Added:
Modified:
lldb/include/lldb/Utility/FileSpec.h
lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
lldb/source/Utility/FileSpec.cpp
lldb/unittests/Utility/FileSpecTest.cpp
Removed:
################################################################################
diff --git a/lldb/include/lldb/Utility/FileSpec.h b/lldb/include/lldb/Utility/FileSpec.h
index 919b5e8564583..6eb5b805d9d9f 100644
--- a/lldb/include/lldb/Utility/FileSpec.h
+++ b/lldb/include/lldb/Utility/FileSpec.h
@@ -408,6 +408,18 @@ class FileSpec {
/// A boolean value indicating whether the path was updated.
bool RemoveLastPathComponent();
+ /// Gets the components of the FileSpec's path.
+ /// For example, given the path:
+ /// /System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation
+ ///
+ /// This function returns:
+ /// {"System", "Library", "PrivateFrameworks", "UIFoundation.framework",
+ /// "UIFoundation"}
+ /// \return
+ /// A std::vector of llvm::StringRefs for each path component.
+ /// The lifetime of the StringRefs is tied to the lifetime of the FileSpec.
+ std::vector<llvm::StringRef> GetComponents() const;
+
protected:
// Convenience method for setting the file without changing the style.
void SetFile(llvm::StringRef path);
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index 76c6b535679a6..60327fbe3124f 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1236,13 +1236,9 @@ lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths(
// "UIFoundation" and "UIFoundation.framework" -- most likely the latter
// will be the one we find there.
- FileSpec platform_pull_upart(platform_file);
- std::vector<std::string> path_parts;
- path_parts.push_back(platform_pull_upart.GetFilename().AsCString());
- while (platform_pull_upart.RemoveLastPathComponent()) {
- ConstString part = platform_pull_upart.GetFilename();
- path_parts.push_back(part.AsCString());
- }
+ std::vector<llvm::StringRef> path_parts = platform_file.GetComponents();
+ // We want the components in reverse order.
+ std::reverse(path_parts.begin(), path_parts.end());
const size_t path_parts_size = path_parts.size();
size_t num_module_search_paths = module_search_paths_ptr->GetSize();
diff --git a/lldb/source/Utility/FileSpec.cpp b/lldb/source/Utility/FileSpec.cpp
index 6688e45650505..707033c1f8d6b 100644
--- a/lldb/source/Utility/FileSpec.cpp
+++ b/lldb/source/Utility/FileSpec.cpp
@@ -463,6 +463,26 @@ bool FileSpec::RemoveLastPathComponent() {
}
return false;
}
+
+std::vector<llvm::StringRef> FileSpec::GetComponents() const {
+ std::vector<llvm::StringRef> components;
+
+ auto dir_begin = llvm::sys::path::begin(m_directory.GetStringRef(), m_style);
+ auto dir_end = llvm::sys::path::end(m_directory.GetStringRef());
+
+ for (auto iter = dir_begin; iter != dir_end; ++iter) {
+ if (*iter == "/" || *iter == ".")
+ continue;
+
+ components.push_back(*iter);
+ }
+
+ if (!m_filename.IsEmpty() && m_filename != "/" && m_filename != ".")
+ components.push_back(m_filename.GetStringRef());
+
+ return components;
+}
+
/// Returns true if the filespec represents an implementation source
/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
/// extension).
diff --git a/lldb/unittests/Utility/FileSpecTest.cpp b/lldb/unittests/Utility/FileSpecTest.cpp
index ffd3d343dc188..2a62f6b1e7612 100644
--- a/lldb/unittests/Utility/FileSpecTest.cpp
+++ b/lldb/unittests/Utility/FileSpecTest.cpp
@@ -504,3 +504,33 @@ TEST(FileSpecTest, TestIsSourceImplementationFile) {
EXPECT_FALSE(win_noext.IsSourceImplementationFile());
EXPECT_FALSE(exe.IsSourceImplementationFile());
}
+
+TEST(FileSpecTest, TestGetComponents) {
+ std::pair<llvm::StringRef, std::vector<llvm::StringRef>> PosixTests[] = {
+ {"/", {}},
+ {"/foo", {"foo"}},
+ {"/foo/", {"foo"}},
+ {"/foo/bar", {"foo", "bar"}},
+ {"/llvm-project/lldb/unittests/Utility/FileSpecTest.cpp",
+ {"llvm-project", "lldb", "unittests", "Utility", "FileSpecTest.cpp"}},
+ };
+
+ for (const auto &pair : PosixTests) {
+ FileSpec file_spec = PosixSpec(pair.first);
+ EXPECT_EQ(file_spec.GetComponents(), pair.second);
+ }
+
+ std::pair<llvm::StringRef, std::vector<llvm::StringRef>> WindowsTests[] = {
+ {"C:\\", {"C:"}},
+ {"C:\\Windows\\", {"C:", "Windows"}},
+ {"C:\\Windows\\System32", {"C:", "Windows", "System32"}},
+ {"C:\\llvm-project\\lldb\\unittests\\Utility\\FileSpecTest.cpp",
+ {"C:", "llvm-project", "lldb", "unittests", "Utility",
+ "FileSpecTest.cpp"}},
+ };
+
+ for (const auto &pair : WindowsTests) {
+ FileSpec file_spec = WindowsSpec(pair.first);
+ EXPECT_EQ(file_spec.GetComponents(), pair.second);
+ }
+}
More information about the lldb-commits
mailing list