Skip to content

Commit

Permalink
[lldb] Add Checksum to FileSpec (#71457)
Browse files Browse the repository at this point in the history
Store a Checksum in FileSpec. Its purpose is to store the MD5 hash that
was added to the DWARF 5 line table.

This increases the size of a FileSpec from 24 to 40 bytes. The
alternative is to introduce a new SupportFile abstraction for a FileSpec
+ Checksum but that would require a corresponding SupportFileList class.
During review we decided that wasn't worth it, but that's something we
can revisit in the future.
  • Loading branch information
JDevlieghere authored Nov 9, 2023
1 parent 8a454e1 commit 919f5ef
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
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 = {});

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();
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());
}

0 comments on commit 919f5ef

Please sign in to comment.