Skip to content

[lldb] Add Checksum to FileSpec #71457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lldb/include/lldb/Utility/FileSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <optional>
#include <string>

#include "lldb/Utility/Checksum.h"
#include "lldb/Utility/ConstString.h"

#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -71,8 +72,12 @@ class FileSpec {
/// \param[in] style
/// The style of the path
///
/// \param[in] checksum
/// The MD5 checksum of the path.
///
/// \see FileSpec::SetFile (const char *path)
explicit FileSpec(llvm::StringRef path, Style style = Style::native);
explicit FileSpec(llvm::StringRef path, Style style = Style::native,
const Checksum &checksum = {});
Comment on lines +79 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the doxygen


explicit FileSpec(llvm::StringRef path, const llvm::Triple &triple);

Expand Down Expand Up @@ -362,7 +367,11 @@ class FileSpec {
///
/// \param[in] style
/// The style for the given path.
void SetFile(llvm::StringRef path, Style style);
///
/// \param[in] checksum
/// The checksum for the given path.
void SetFile(llvm::StringRef path, Style style,
const Checksum &checksum = {});

/// Change the file specified with a new path.
///
Expand Down Expand Up @@ -420,13 +429,17 @@ class FileSpec {
/// The lifetime of the StringRefs is tied to the lifetime of the FileSpec.
std::vector<llvm::StringRef> GetComponents() const;

/// Return the checksum for this FileSpec or all zeros if there is none.
const Checksum &GetChecksum() const { return m_checksum; };

protected:
// Convenience method for setting the file without changing the style.
void SetFile(llvm::StringRef path);

/// Called anytime m_directory or m_filename is changed to clear any cached
/// state in this object.
void PathWasModified() {
m_checksum = Checksum();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to recompute the checksum if the path was changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the FileSpec is a system independent path description (i.e. the file doesn't need to exist on the host) there's no way to recompute it automatically, although the user can specify a new checksum if the path changes.

m_is_resolved = false;
m_absolute = Absolute::Calculate;
}
Expand All @@ -443,6 +456,9 @@ class FileSpec {
/// The unique'd filename path.
ConstString m_filename;

/// The optional MD5 checksum of the file.
Checksum m_checksum;

/// True if this path has been resolved.
mutable bool m_is_resolved = false;

Expand Down
9 changes: 6 additions & 3 deletions lldb/source/Utility/FileSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
FileSpec::FileSpec() : m_style(GetNativeStyle()) {}

// Default constructor that can take an optional full path to a file on disk.
FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
SetFile(path, style);
FileSpec::FileSpec(llvm::StringRef path, Style style, const Checksum &checksum)
: m_checksum(checksum), m_style(style) {
SetFile(path, style, checksum);
}

FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &triple)
Expand Down Expand Up @@ -171,9 +172,11 @@ void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
// Update the contents of this object with a new path. The path will be split
// up into a directory and filename and stored as uniqued string values for
// quick comparison and efficient memory usage.
void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
void FileSpec::SetFile(llvm::StringRef pathname, Style style,
const Checksum &checksum) {
Clear();
m_style = (style == Style::native) ? GetNativeStyle() : style;
m_checksum = checksum;

if (pathname.empty())
return;
Expand Down
12 changes: 12 additions & 0 deletions lldb/unittests/Utility/FileSpecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,15 @@ TEST(FileSpecTest, TestGetComponents) {
EXPECT_EQ(file_spec.GetComponents(), pair.second);
}
}

TEST(FileSpecTest, TestChecksum) {
Checksum checksum({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
FileSpec file_spec("/foo/bar", FileSpec::Style::posix, checksum);
EXPECT_TRUE(static_cast<bool>(file_spec.GetChecksum()));
EXPECT_EQ(file_spec.GetChecksum(), checksum);

FileSpec copy = file_spec;

EXPECT_TRUE(static_cast<bool>(copy.GetChecksum()));
EXPECT_EQ(file_spec.GetChecksum(), copy.GetChecksum());
}