[Lldb-commits] [lldb] [lldb] Add Checksum to FileSpec (PR #71457)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 8 16:52:44 PST 2023


https://github.com/JDevlieghere updated https://github.com/llvm/llvm-project/pull/71457

>From 0a4b349d6299db6b2a0a6709c40d15b1a4a0f62a Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Wed, 8 Nov 2023 16:51:53 -0800
Subject: [PATCH] [lldb] Add Checksum to FileSpec

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.
---
 lldb/include/lldb/Utility/FileSpec.h    | 20 ++++++++++++++++++--
 lldb/source/Utility/FileSpec.cpp        |  9 ++++++---
 lldb/unittests/Utility/FileSpecTest.cpp | 12 ++++++++++++
 3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/lldb/include/lldb/Utility/FileSpec.h b/lldb/include/lldb/Utility/FileSpec.h
index f06a8543a056e87..ccd25a81c11f368 100644
--- a/lldb/include/lldb/Utility/FileSpec.h
+++ b/lldb/include/lldb/Utility/FileSpec.h
@@ -13,6 +13,7 @@
 #include <optional>
 #include <string>
 
+#include "lldb/Utility/Checksum.h"
 #include "lldb/Utility/ConstString.h"
 
 #include "llvm/ADT/StringRef.h"
@@ -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);
 
@@ -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.
   ///
@@ -420,6 +429,9 @@ 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);
@@ -427,6 +439,7 @@ class FileSpec {
   /// 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;
   }
@@ -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;
 
diff --git a/lldb/source/Utility/FileSpec.cpp b/lldb/source/Utility/FileSpec.cpp
index eb34ef97cea0b2f..4bbfbb7c1fab5e6 100644
--- a/lldb/source/Utility/FileSpec.cpp
+++ b/lldb/source/Utility/FileSpec.cpp
@@ -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)
@@ -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;
diff --git a/lldb/unittests/Utility/FileSpecTest.cpp b/lldb/unittests/Utility/FileSpecTest.cpp
index 2a62f6b1e76120f..565395a495be60e 100644
--- a/lldb/unittests/Utility/FileSpecTest.cpp
+++ b/lldb/unittests/Utility/FileSpecTest.cpp
@@ -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());
+}



More information about the lldb-commits mailing list