Skip to content

Commit 1882662

Browse files
committed
[lldb] Add Checksum to FileSpec
Store a Checksum in FileSpec. Its purpose is to store the MD5 hash added to the DWARF 5 line table.
1 parent 5ba3f9d commit 1882662

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

lldb/include/lldb/Utility/FileSpec.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
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"
1920
#include "llvm/Support/FileSystem.h"
2021
#include "llvm/Support/FormatVariadic.h"
22+
#include "llvm/Support/MD5.h"
2123
#include "llvm/Support/Path.h"
2224

2325
#include <cstddef>
2426
#include <cstdint>
27+
#include <optional>
2528

2629
namespace lldb_private {
2730
class Stream;
@@ -72,7 +75,8 @@ class FileSpec {
7275
/// The style of the path
7376
///
7477
/// \see FileSpec::SetFile (const char *path)
75-
explicit FileSpec(llvm::StringRef path, Style style = Style::native);
78+
explicit FileSpec(llvm::StringRef path, Style style = Style::native,
79+
const Checksum &checksum = {});
7680

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

@@ -362,7 +366,11 @@ class FileSpec {
362366
///
363367
/// \param[in] style
364368
/// The style for the given path.
365-
void SetFile(llvm::StringRef path, Style style);
369+
///
370+
/// \param[in] checksum
371+
/// The checksum for the given path.
372+
void SetFile(llvm::StringRef path, Style style,
373+
const Checksum &checksum = {});
366374

367375
/// Change the file specified with a new path.
368376
///
@@ -420,13 +428,17 @@ class FileSpec {
420428
/// The lifetime of the StringRefs is tied to the lifetime of the FileSpec.
421429
std::vector<llvm::StringRef> GetComponents() const;
422430

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

427438
/// Called anytime m_directory or m_filename is changed to clear any cached
428439
/// state in this object.
429440
void PathWasModified() {
441+
m_checksum = Checksum();
430442
m_is_resolved = false;
431443
m_absolute = Absolute::Calculate;
432444
}
@@ -443,6 +455,9 @@ class FileSpec {
443455
/// The unique'd filename path.
444456
ConstString m_filename;
445457

458+
/// The optional MD5 checksum of the file.
459+
Checksum m_checksum;
460+
446461
/// True if this path has been resolved.
447462
mutable bool m_is_resolved = false;
448463

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: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ TEST(FileSpecTest, FileAndDirectoryComponents) {
2828

2929
FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
3030
EXPECT_STREQ("F:\\bar", fs_windows.GetPath().c_str());
31-
// EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetPath().c_str()); // It returns
32-
// "F:/"
31+
// EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetPath().c_str()); // It
32+
// returns "F:/"
3333
EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());
3434

3535
FileSpec fs_posix_root("/", FileSpec::Style::posix);
@@ -297,45 +297,18 @@ TEST(FileSpecTest, FormatFileSpec) {
297297

298298
TEST(FileSpecTest, IsRelative) {
299299
llvm::StringRef not_relative[] = {
300-
"/",
301-
"/a",
302-
"/a/",
303-
"/a/b",
304-
"/a/b/",
305-
"//",
306-
"//a/",
307-
"//a/b",
308-
"//a/b/",
309-
"~",
310-
"~/",
311-
"~/a",
312-
"~/a/",
313-
"~/a/b",
314-
"~/a/b/",
315-
"/foo/.",
316-
"/foo/..",
317-
"/foo/../",
318-
"/foo/../.",
300+
"/", "/a", "/a/", "/a/b", "/a/b/", "//", "//a/",
301+
"//a/b", "//a/b/", "~", "~/", "~/a", "~/a/", "~/a/b",
302+
"~/a/b/", "/foo/.", "/foo/..", "/foo/../", "/foo/../.",
319303
};
320-
for (const auto &path: not_relative) {
304+
for (const auto &path : not_relative) {
321305
SCOPED_TRACE(path);
322306
EXPECT_FALSE(PosixSpec(path).IsRelative());
323307
}
324308
llvm::StringRef is_relative[] = {
325-
".",
326-
"./",
327-
".///",
328-
"a",
329-
"./a",
330-
"./a/",
331-
"./a/",
332-
"./a/b",
333-
"./a/b/",
334-
"../foo",
335-
"foo/bar.c",
336-
"./foo/bar.c"
337-
};
338-
for (const auto &path: is_relative) {
309+
".", "./", ".///", "a", "./a", "./a/",
310+
"./a/", "./a/b", "./a/b/", "../foo", "foo/bar.c", "./foo/bar.c"};
311+
for (const auto &path : is_relative) {
339312
SCOPED_TRACE(path);
340313
EXPECT_TRUE(PosixSpec(path).IsRelative());
341314
}
@@ -421,7 +394,6 @@ TEST(FileSpecTest, Match) {
421394

422395
EXPECT_TRUE(Match("", "/foo/bar"));
423396
EXPECT_TRUE(Match("", ""));
424-
425397
}
426398

427399
TEST(FileSpecTest, TestAbsoluteCaching) {
@@ -534,3 +506,15 @@ TEST(FileSpecTest, TestGetComponents) {
534506
EXPECT_EQ(file_spec.GetComponents(), pair.second);
535507
}
536508
}
509+
510+
TEST(FileSpecTest, TestChecksum) {
511+
Checksum checksum({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
512+
FileSpec file_spec("/foo/bar", FileSpec::Style::posix, checksum);
513+
EXPECT_TRUE(static_cast<bool>(file_spec.GetChecksum()));
514+
EXPECT_EQ(file_spec.GetChecksum(), checksum);
515+
516+
FileSpec copy = file_spec;
517+
518+
EXPECT_TRUE(static_cast<bool>(copy.GetChecksum()));
519+
EXPECT_EQ(file_spec.GetChecksum(), copy.GetChecksum());
520+
}

0 commit comments

Comments
 (0)