This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Introduce FileSpec::GetComponents
ClosedPublic

Authored by bulbazord on May 24 2023, 7:45 PM.

Details

Summary

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.

Diff Detail

Event Timeline

bulbazord created this revision.May 24 2023, 7:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 24 2023, 7:45 PM
bulbazord requested review of this revision.May 24 2023, 7:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 24 2023, 7:45 PM
JDevlieghere added inline comments.May 24 2023, 8:24 PM
lldb/include/lldb/Utility/FileSpec.h
420

I'm surprised this returns a vector of std::strings and not llvm::StringRefs. I would expect all the components to be part of the FileSpec's stored path. Even with the file and directory stored as separate ConstStrings, that should be possible?

bulbazord added inline comments.May 24 2023, 8:29 PM
lldb/include/lldb/Utility/FileSpec.h
420

Yes, it is possible to do std::vector<llvm::StringRef> here, especially because they would be backed by ConstStrings which live forever. I chose std::string here because of the possibility that we one day no longer use ConstString to store the path, in which case the vector's StringRefs would only be valid for the lifetime of the FileSpec (or until it gets mutated).

Maybe I'm thinking too far ahead or planning for a future that will never come though. What do you think?

JDevlieghere added inline comments.May 24 2023, 9:20 PM
lldb/include/lldb/Utility/FileSpec.h
420

I think it's reasonable to expect the lifetime of things handed out by a FileSpec match the lifetime of the FileSpec, but that depends on how we want to deal with mutability. If we want to be able to mutate a FileSpec in place, then you're right, these things need to have their own lifetime.

bulbazord added inline comments.May 25 2023, 10:17 AM
lldb/include/lldb/Utility/FileSpec.h
420

I'd prefer to move towards FileSpec being immutable (or as close as possible), so I'll update this and change it to `StringRef. Thanks for the feedback!

Convert return type to std::vector<llvm::StringRef>
Actually fix code so this thing compiles correctly

bulbazord marked 2 inline comments as done.May 25 2023, 1:01 PM
mib accepted this revision.May 25 2023, 3:48 PM

LGTM!

lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
1241

Cool!

This revision is now accepted and ready to land.May 25 2023, 3:48 PM
This revision was automatically updated to reflect the committed changes.