Skip to content

Commit 919f5ef

Browse files
authored
[lldb] Add Checksum to FileSpec (#71457)
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.
1 parent 8a454e1 commit 919f5ef

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

lldb/include/lldb/Utility/FileSpec.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <optional>
1414
#include <string>
1515

16+
#include "lldb/Utility/Checksum.h"
1617
#include "lldb/Utility/ConstString.h"
1718

1819
#include "llvm/ADT/StringRef.h"
@@ -71,8 +72,12 @@ class FileSpec {
7172
/// \param[in] style
7273
/// The style of the path
7374
///
75+
/// \param[in] checksum
76+
/// The MD5 checksum of the path.
77+
///
7478
/// \see FileSpec::SetFile (const char *path)
75-
explicit FileSpec(llvm::StringRef path, Style style = Style::native);
79+
explicit FileSpec(llvm::StringRef path, Style style = Style::native,
80+
const Checksum &checksum = {});
7681

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

@@ -362,7 +367,11 @@ class FileSpec {
362367
///
363368
/// \param[in] style
364369
/// The style for the given path.
365-
void SetFile(llvm::StringRef path, Style style);
370+
///
371+
/// \param[in] checksum
372+
/// The checksum for the given path.
373+
void SetFile(llvm::StringRef path, Style style,
374+
const Checksum &checksum = {});
366375

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

432+
/// Return the checksum for this FileSpec or all zeros if there is none.
433+
const Checksum &GetChecksum() const { return m_checksum; };
434+
423435
protected:
424436
// Convenience method for setting the file without changing the style.
425437
void SetFile(llvm::StringRef path);
426438

427439
/// Called anytime m_directory or m_filename is changed to clear any cached
428440
/// state in this object.
429441
void PathWasModified() {
442+
m_checksum = Checksum();
430443
m_is_resolved = false;
431444
m_absolute = Absolute::Calculate;
432445
}
@@ -443,6 +456,9 @@ class FileSpec {
443456
/// The unique'd filename path.
444457
ConstString m_filename;
445458

459+
/// The optional MD5 checksum of the file.
460+
Checksum m_checksum;
461+
446462
/// True if this path has been resolved.
447463
mutable bool m_is_resolved = false;
448464

lldb/source/Utility/FileSpec.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
6868
FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
6969

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

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

178181
if (pathname.empty())
179182
return;

lldb/unittests/Utility/FileSpecTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,15 @@ TEST(FileSpecTest, TestGetComponents) {
534534
EXPECT_EQ(file_spec.GetComponents(), pair.second);
535535
}
536536
}
537+
538+
TEST(FileSpecTest, TestChecksum) {
539+
Checksum checksum({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
540+
FileSpec file_spec("/foo/bar", FileSpec::Style::posix, checksum);
541+
EXPECT_TRUE(static_cast<bool>(file_spec.GetChecksum()));
542+
EXPECT_EQ(file_spec.GetChecksum(), checksum);
543+
544+
FileSpec copy = file_spec;
545+
546+
EXPECT_TRUE(static_cast<bool>(copy.GetChecksum()));
547+
EXPECT_EQ(file_spec.GetChecksum(), copy.GetChecksum());
548+
}

0 commit comments

Comments
 (0)