Skip to content

Commit ab40ae8

Browse files
authored
[lldb] Store SupportFiles in SourceManager::File (NFC) (#106639)
To support detecting MD5 checksum mismatches, store a SupportFile rather than a plain FileSpec in SourceManager::File.
1 parent f4ea19b commit ab40ae8

File tree

5 files changed

+108
-83
lines changed

5 files changed

+108
-83
lines changed

lldb/include/lldb/Core/SourceManager.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class SourceManager {
3737
const SourceManager::File &rhs);
3838

3939
public:
40-
File(const FileSpec &file_spec, lldb::TargetSP target_sp);
41-
File(const FileSpec &file_spec, lldb::DebuggerSP debugger_sp);
40+
File(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp);
41+
File(lldb::SupportFileSP support_file_sp, lldb::DebuggerSP debugger_sp);
4242

4343
bool ModificationTimeIsStale() const;
4444
bool PathRemappingIsStale() const;
@@ -56,7 +56,10 @@ class SourceManager {
5656

5757
bool LineIsValid(uint32_t line);
5858

59-
const FileSpec &GetFileSpec() { return m_file_spec; }
59+
lldb::SupportFileSP GetSupportFile() const {
60+
assert(m_support_file_sp && "SupportFileSP must always be valid");
61+
return m_support_file_sp;
62+
}
6063

6164
uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
6265

@@ -70,15 +73,13 @@ class SourceManager {
7073

7174
protected:
7275
/// Set file and update modification time.
73-
void SetFileSpec(FileSpec file_spec);
76+
void SetSupportFile(lldb::SupportFileSP support_file_sp);
7477

7578
bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
7679

77-
FileSpec m_file_spec_orig; // The original file spec that was used (can be
78-
// different from m_file_spec)
79-
FileSpec m_file_spec; // The actually file spec being used (if the target
80-
// has source mappings, this might be different from
81-
// m_file_spec_orig)
80+
/// The support file. If the target has source mappings, this might be
81+
/// different from the original support file passed to the constructor.
82+
lldb::SupportFileSP m_support_file_sp;
8283

8384
// Keep the modification time that this file data is valid for
8485
llvm::sys::TimePoint<> m_mod_time;
@@ -93,7 +94,8 @@ class SourceManager {
9394
lldb::TargetWP m_target_wp;
9495

9596
private:
96-
void CommonInitializer(const FileSpec &file_spec, lldb::TargetSP target_sp);
97+
void CommonInitializer(lldb::SupportFileSP support_file_sp,
98+
lldb::TargetSP target_sp);
9799
};
98100

99101
typedef std::shared_ptr<File> FileSP;

lldb/source/Commands/CommandObjectSource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,8 @@ class CommandObjectSourceList : public CommandObjectParsed {
10761076
target.GetSourceManager().GetLastFile());
10771077
if (last_file_sp) {
10781078
const bool show_inlines = true;
1079-
m_breakpoint_locations.Reset(last_file_sp->GetFileSpec(), 0,
1080-
show_inlines);
1079+
m_breakpoint_locations.Reset(
1080+
last_file_sp->GetSupportFile()->GetSpecOnly(), 0, show_inlines);
10811081
SearchFilterForUnconstrainedSearches target_search_filter(
10821082
target.shared_from_this());
10831083
target_search_filter.Search(m_breakpoint_locations);

lldb/source/Core/IOHandlerCursesGUI.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6894,8 +6894,8 @@ class SourceFileWindowDelegate : public WindowDelegate {
68946894
if (context_changed)
68956895
m_selected_line = m_pc_line;
68966896

6897-
if (m_file_sp &&
6898-
m_file_sp->GetFileSpec() == m_sc.line_entry.GetFile()) {
6897+
if (m_file_sp && m_file_sp->GetSupportFile()->GetSpecOnly() ==
6898+
m_sc.line_entry.GetFile()) {
68996899
// Same file, nothing to do, we should either have the lines or
69006900
// not (source file missing)
69016901
if (m_selected_line >= static_cast<size_t>(m_first_visible_line)) {
@@ -7001,7 +7001,8 @@ class SourceFileWindowDelegate : public WindowDelegate {
70017001
LineEntry bp_loc_line_entry;
70027002
if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry(
70037003
bp_loc_line_entry)) {
7004-
if (m_file_sp->GetFileSpec() == bp_loc_line_entry.GetFile()) {
7004+
if (m_file_sp->GetSupportFile()->GetSpecOnly() ==
7005+
bp_loc_line_entry.GetFile()) {
70057006
bp_lines.insert(bp_loc_line_entry.line);
70067007
}
70077008
}
@@ -7332,7 +7333,7 @@ class SourceFileWindowDelegate : public WindowDelegate {
73327333
if (exe_ctx.HasProcessScope() && exe_ctx.GetProcessRef().IsAlive()) {
73337334
BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint(
73347335
nullptr, // Don't limit the breakpoint to certain modules
7335-
m_file_sp->GetFileSpec(), // Source file
7336+
m_file_sp->GetSupportFile()->GetSpecOnly(), // Source file
73367337
m_selected_line +
73377338
1, // Source line number (m_selected_line is zero based)
73387339
0, // Unspecified column.
@@ -7478,7 +7479,8 @@ class SourceFileWindowDelegate : public WindowDelegate {
74787479
LineEntry bp_loc_line_entry;
74797480
if (bp_loc_sp->GetAddress().CalculateSymbolContextLineEntry(
74807481
bp_loc_line_entry)) {
7481-
if (m_file_sp->GetFileSpec() == bp_loc_line_entry.GetFile() &&
7482+
if (m_file_sp->GetSupportFile()->GetSpecOnly() ==
7483+
bp_loc_line_entry.GetFile() &&
74827484
m_selected_line + 1 == bp_loc_line_entry.line) {
74837485
bool removed =
74847486
exe_ctx.GetTargetRef().RemoveBreakpointByID(bp_sp->GetID());
@@ -7492,7 +7494,7 @@ class SourceFileWindowDelegate : public WindowDelegate {
74927494
// No breakpoint found on the location, add it.
74937495
BreakpointSP bp_sp = exe_ctx.GetTargetRef().CreateBreakpoint(
74947496
nullptr, // Don't limit the breakpoint to certain modules
7495-
m_file_sp->GetFileSpec(), // Source file
7497+
m_file_sp->GetSupportFile()->GetSpecOnly(), // Source file
74967498
m_selected_line +
74977499
1, // Source line number (m_selected_line is zero based)
74987500
0, // No column specified.

lldb/source/Core/SourceManager.cpp

Lines changed: 79 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
8787
LLDB_LOG(log, "Source file caching disabled: creating new source file: {0}",
8888
file_spec);
8989
if (target_sp)
90-
return std::make_shared<File>(file_spec, target_sp);
91-
return std::make_shared<File>(file_spec, debugger_sp);
90+
return std::make_shared<File>(std::make_shared<SupportFile>(file_spec),
91+
target_sp);
92+
return std::make_shared<File>(std::make_shared<SupportFile>(file_spec),
93+
debugger_sp);
9294
}
9395

9496
ProcessSP process_sp = target_sp ? target_sp->GetProcessSP() : ProcessSP();
@@ -136,7 +138,8 @@ SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
136138
}
137139

138140
// Check if the file exists on disk.
139-
if (file_sp && !FileSystem::Instance().Exists(file_sp->GetFileSpec())) {
141+
if (file_sp && !FileSystem::Instance().Exists(
142+
file_sp->GetSupportFile()->GetSpecOnly())) {
140143
LLDB_LOG(log, "File doesn't exist on disk: {0}", file_spec);
141144
file_sp.reset();
142145
}
@@ -148,9 +151,11 @@ SourceManager::FileSP SourceManager::GetFile(const FileSpec &file_spec) {
148151

149152
// (Re)create the file.
150153
if (target_sp)
151-
file_sp = std::make_shared<File>(file_spec, target_sp);
154+
file_sp = std::make_shared<File>(std::make_shared<SupportFile>(file_spec),
155+
target_sp);
152156
else
153-
file_sp = std::make_shared<File>(file_spec, debugger_sp);
157+
file_sp = std::make_shared<File>(std::make_shared<SupportFile>(file_spec),
158+
debugger_sp);
154159

155160
// Add the file to the debugger and process cache. If the file was
156161
// invalidated, this will overwrite it.
@@ -444,25 +449,25 @@ void SourceManager::FindLinesMatchingRegex(FileSpec &file_spec,
444449
match_lines);
445450
}
446451

447-
SourceManager::File::File(const FileSpec &file_spec,
452+
SourceManager::File::File(SupportFileSP support_file_sp,
448453
lldb::DebuggerSP debugger_sp)
449-
: m_file_spec_orig(file_spec), m_file_spec(), m_mod_time(),
454+
: m_support_file_sp(std::make_shared<SupportFile>()), m_mod_time(),
450455
m_debugger_wp(debugger_sp), m_target_wp(TargetSP()) {
451-
CommonInitializer(file_spec, {});
456+
CommonInitializer(support_file_sp, {});
452457
}
453458

454-
SourceManager::File::File(const FileSpec &file_spec, TargetSP target_sp)
455-
: m_file_spec_orig(file_spec), m_file_spec(), m_mod_time(),
459+
SourceManager::File::File(SupportFileSP support_file_sp, TargetSP target_sp)
460+
: m_support_file_sp(std::make_shared<SupportFile>()), m_mod_time(),
456461
m_debugger_wp(target_sp ? target_sp->GetDebugger().shared_from_this()
457462
: DebuggerSP()),
458463
m_target_wp(target_sp) {
459-
CommonInitializer(file_spec, target_sp);
464+
CommonInitializer(support_file_sp, target_sp);
460465
}
461466

462-
void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
467+
void SourceManager::File::CommonInitializer(SupportFileSP support_file_sp,
463468
TargetSP target_sp) {
464469
// Set the file and update the modification time.
465-
SetFileSpec(file_spec);
470+
SetSupportFile(support_file_sp);
466471

467472
// Always update the source map modification ID if we have a target.
468473
if (target_sp)
@@ -472,65 +477,76 @@ void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
472477
if (m_mod_time == llvm::sys::TimePoint<>()) {
473478
if (target_sp) {
474479
// If this is just a file name, try finding it in the target.
475-
if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
476-
bool check_inlines = false;
477-
SymbolContextList sc_list;
478-
size_t num_matches =
479-
target_sp->GetImages().ResolveSymbolContextForFilePath(
480-
file_spec.GetFilename().AsCString(), 0, check_inlines,
481-
SymbolContextItem(eSymbolContextModule |
482-
eSymbolContextCompUnit),
483-
sc_list);
484-
bool got_multiple = false;
485-
if (num_matches != 0) {
486-
if (num_matches > 1) {
487-
CompileUnit *test_cu = nullptr;
488-
for (const SymbolContext &sc : sc_list) {
489-
if (sc.comp_unit) {
490-
if (test_cu) {
491-
if (test_cu != sc.comp_unit)
492-
got_multiple = true;
493-
break;
494-
} else
495-
test_cu = sc.comp_unit;
480+
{
481+
FileSpec file_spec = support_file_sp->GetSpecOnly();
482+
if (!file_spec.GetDirectory() && file_spec.GetFilename()) {
483+
bool check_inlines = false;
484+
SymbolContextList sc_list;
485+
size_t num_matches =
486+
target_sp->GetImages().ResolveSymbolContextForFilePath(
487+
file_spec.GetFilename().AsCString(), 0, check_inlines,
488+
SymbolContextItem(eSymbolContextModule |
489+
eSymbolContextCompUnit),
490+
sc_list);
491+
bool got_multiple = false;
492+
if (num_matches != 0) {
493+
if (num_matches > 1) {
494+
CompileUnit *test_cu = nullptr;
495+
for (const SymbolContext &sc : sc_list) {
496+
if (sc.comp_unit) {
497+
if (test_cu) {
498+
if (test_cu != sc.comp_unit)
499+
got_multiple = true;
500+
break;
501+
} else
502+
test_cu = sc.comp_unit;
503+
}
496504
}
497505
}
498-
}
499-
if (!got_multiple) {
500-
SymbolContext sc;
501-
sc_list.GetContextAtIndex(0, sc);
502-
if (sc.comp_unit)
503-
SetFileSpec(sc.comp_unit->GetPrimaryFile());
506+
if (!got_multiple) {
507+
SymbolContext sc;
508+
sc_list.GetContextAtIndex(0, sc);
509+
if (sc.comp_unit)
510+
SetSupportFile(std::make_shared<SupportFile>(
511+
sc.comp_unit->GetPrimaryFile()));
512+
}
504513
}
505514
}
506515
}
507516

508517
// Try remapping the file if it doesn't exist.
509-
if (!FileSystem::Instance().Exists(m_file_spec)) {
510-
// Check target specific source remappings (i.e., the
511-
// target.source-map setting), then fall back to the module
512-
// specific remapping (i.e., the .dSYM remapping dictionary).
513-
auto remapped = target_sp->GetSourcePathMap().FindFile(m_file_spec);
514-
if (!remapped) {
515-
FileSpec new_spec;
516-
if (target_sp->GetImages().FindSourceFile(m_file_spec, new_spec))
517-
remapped = new_spec;
518+
{
519+
FileSpec file_spec = support_file_sp->GetSpecOnly();
520+
if (!FileSystem::Instance().Exists(file_spec)) {
521+
// Check target specific source remappings (i.e., the
522+
// target.source-map setting), then fall back to the module
523+
// specific remapping (i.e., the .dSYM remapping dictionary).
524+
auto remapped = target_sp->GetSourcePathMap().FindFile(file_spec);
525+
if (!remapped) {
526+
FileSpec new_spec;
527+
if (target_sp->GetImages().FindSourceFile(file_spec, new_spec))
528+
remapped = new_spec;
529+
}
530+
if (remapped)
531+
SetSupportFile(std::make_shared<SupportFile>(
532+
*remapped, support_file_sp->GetChecksum()));
518533
}
519-
if (remapped)
520-
SetFileSpec(*remapped);
521534
}
522535
}
523536
}
524537

525538
// If the file exists, read in the data.
526539
if (m_mod_time != llvm::sys::TimePoint<>())
527-
m_data_sp = FileSystem::Instance().CreateDataBuffer(m_file_spec);
540+
m_data_sp = FileSystem::Instance().CreateDataBuffer(
541+
m_support_file_sp->GetSpecOnly());
528542
}
529543

530-
void SourceManager::File::SetFileSpec(FileSpec file_spec) {
544+
void SourceManager::File::SetSupportFile(lldb::SupportFileSP support_file_sp) {
545+
FileSpec file_spec = support_file_sp->GetSpecOnly();
531546
resolve_tilde(file_spec);
532-
m_file_spec = std::move(file_spec);
533-
m_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
547+
m_support_file_sp =
548+
std::make_shared<SupportFile>(file_spec, support_file_sp->GetChecksum());
549+
m_mod_time = FileSystem::Instance().GetModificationTime(file_spec);
534550
}
535551

536552
uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
@@ -603,7 +619,8 @@ bool SourceManager::File::ModificationTimeIsStale() const {
603619
// TODO: use host API to sign up for file modifications to anything in our
604620
// source cache and only update when we determine a file has been updated.
605621
// For now we check each time we want to display info for the file.
606-
auto curr_mod_time = FileSystem::Instance().GetModificationTime(m_file_spec);
622+
auto curr_mod_time = FileSystem::Instance().GetModificationTime(
623+
m_support_file_sp->GetSpecOnly());
607624
return curr_mod_time != llvm::sys::TimePoint<>() &&
608625
m_mod_time != curr_mod_time;
609626
}
@@ -644,7 +661,8 @@ size_t SourceManager::File::DisplaySourceLines(uint32_t line,
644661
debugger_sp->GetStopShowColumnAnsiSuffix());
645662

646663
HighlighterManager mgr;
647-
std::string path = GetFileSpec().GetPath(/*denormalize*/ false);
664+
std::string path =
665+
GetSupportFile()->GetSpecOnly().GetPath(/*denormalize*/ false);
648666
// FIXME: Find a way to get the definitive language this file was written in
649667
// and pass it to the highlighter.
650668
const auto &h = mgr.getHighlighterFor(lldb::eLanguageTypeUnknown, path);
@@ -698,7 +716,8 @@ void SourceManager::File::FindLinesMatchingRegex(
698716

699717
bool lldb_private::operator==(const SourceManager::File &lhs,
700718
const SourceManager::File &rhs) {
701-
if (lhs.m_file_spec != rhs.m_file_spec)
719+
if (!lhs.GetSupportFile()->Equal(*rhs.GetSupportFile(),
720+
SupportFile::eEqualChecksumIfSet))
702721
return false;
703722
return lhs.m_mod_time == rhs.m_mod_time;
704723
}
@@ -778,9 +797,9 @@ void SourceManager::SourceFileCache::AddSourceFile(const FileSpec &file_spec,
778797
assert(file_sp && "invalid FileSP");
779798

780799
AddSourceFileImpl(file_spec, file_sp);
781-
const FileSpec &resolved_file_spec = file_sp->GetFileSpec();
800+
const FileSpec &resolved_file_spec = file_sp->GetSupportFile()->GetSpecOnly();
782801
if (file_spec != resolved_file_spec)
783-
AddSourceFileImpl(file_sp->GetFileSpec(), file_sp);
802+
AddSourceFileImpl(file_sp->GetSupportFile()->GetSpecOnly(), file_sp);
784803
}
785804

786805
void SourceManager::SourceFileCache::RemoveSourceFile(const FileSP &file_sp) {

lldb/unittests/Core/SourceManagerTest.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "lldb/Core/SourceManager.h"
1010
#include "lldb/Host/FileSystem.h"
11+
#include "lldb/Utility/SupportFile.h"
1112
#include "gtest/gtest.h"
1213

1314
#include "TestingSupport/MockTildeExpressionResolver.h"
@@ -29,8 +30,8 @@ TEST_F(SourceFileCache, FindSourceFileFound) {
2930

3031
// Insert: foo
3132
FileSpec foo_file_spec("foo");
32-
auto foo_file_sp =
33-
std::make_shared<SourceManager::File>(foo_file_spec, lldb::DebuggerSP());
33+
auto foo_file_sp = std::make_shared<SourceManager::File>(
34+
std::make_shared<SupportFile>(foo_file_spec), lldb::DebuggerSP());
3435
cache.AddSourceFile(foo_file_spec, foo_file_sp);
3536

3637
// Query: foo, expect found.
@@ -43,8 +44,8 @@ TEST_F(SourceFileCache, FindSourceFileNotFound) {
4344

4445
// Insert: foo
4546
FileSpec foo_file_spec("foo");
46-
auto foo_file_sp =
47-
std::make_shared<SourceManager::File>(foo_file_spec, lldb::DebuggerSP());
47+
auto foo_file_sp = std::make_shared<SourceManager::File>(
48+
std::make_shared<SupportFile>(foo_file_spec), lldb::DebuggerSP());
4849
cache.AddSourceFile(foo_file_spec, foo_file_sp);
4950

5051
// Query: bar, expect not found.
@@ -63,7 +64,8 @@ TEST_F(SourceFileCache, FindSourceFileByUnresolvedPath) {
6364

6465
// Create the file with the resolved file spec.
6566
auto foo_file_sp = std::make_shared<SourceManager::File>(
66-
resolved_foo_file_spec, lldb::DebuggerSP());
67+
std::make_shared<SupportFile>(resolved_foo_file_spec),
68+
lldb::DebuggerSP());
6769

6870
// Cache the result with the unresolved file spec.
6971
cache.AddSourceFile(foo_file_spec, foo_file_sp);

0 commit comments

Comments
 (0)