This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Remove FileSpec::GetLastPathComponent
ClosedPublic

Authored by bulbazord on May 2 2023, 10:09 AM.

Details

Summary

As far as I can tell, this just computes the filename of the FileSpec,
which is already conveniently stored in m_filename. We can use
FileSpec::GetFilename() instead.

Diff Detail

Event Timeline

bulbazord created this revision.May 2 2023, 10:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 10:09 AM
Herald added a subscriber: emaste. · View Herald Transcript
bulbazord requested review of this revision.May 2 2023, 10:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 10:09 AM
This revision is now accepted and ready to land.May 2 2023, 10:22 AM
mib added a comment.May 2 2023, 11:14 AM

@bulbazord What if the FileSpec is pointing to a directory instead of a file ? What would GetFilename return in that case compared to GetLastPathComponent ?

@bulbazord What if the FileSpec is pointing to a directory instead of a file ? What would GetFilename return in that case compared to GetLastPathComponent ?

FileSpec chops everything up into a directory and a filename, even if it's pointing to a directory. For example:

fspec = lldb.SBFileSpec("/foo/bar/baz/")
print(fspec.GetDirectory())
print(fspec.GetFilename())

This will print "/foo/bar" followed by "baz". baz is clearly a directory, but FileSpec will treat it as the filename. GetLastPathComponent uses llvm::sys::path::filename to get the last element of the path, which is the exact same mechanism we use when constructing FileSpec's internal m_directory and m_filename in the first place.

mib added a comment.May 2 2023, 11:58 AM

@bulbazord What if the FileSpec is pointing to a directory instead of a file ? What would GetFilename return in that case compared to GetLastPathComponent ?

FileSpec chops everything up into a directory and a filename, even if it's pointing to a directory. For example:

fspec = lldb.SBFileSpec("/foo/bar/baz/")
print(fspec.GetDirectory())
print(fspec.GetFilename())

This will print "/foo/bar" followed by "baz". baz is clearly a directory, but FileSpec will treat it as the filename. GetLastPathComponent uses llvm::sys::path::filename to get the last element of the path, which is the exact same mechanism we use when constructing FileSpec's internal m_directory and m_filename in the first place.

Here's some more bike-shedding: This is not related to this patch specifically, but I don't think GetFilename()should return baz since it's not a file. Similarly, GetDirectory() should /foo/bar/baz/. Then in order to get the pointed file or directory I'd use GetLastPathComponent(). And to get the parent (/foo/bar), I think we should expose a new GetParent method. TBH, I find the current behavior very confusing.

mib added a comment.May 2 2023, 4:42 PM

Given the current behavior, LGTM

mib accepted this revision.May 2 2023, 4:42 PM
This revision was automatically updated to reflect the committed changes.