Skip to content

Commit c955af3

Browse files
committed
[lldb] [disassembler] chore: enhance VariableAnnotator to return structured data: remove VarState and keep only VariableAnnotation struct
Signed-off-by: Nikita B <[email protected]>
1 parent 4cee0d7 commit c955af3

File tree

2 files changed

+33
-53
lines changed

2 files changed

+33
-53
lines changed

lldb/include/lldb/Core/Disassembler.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -590,24 +590,8 @@ struct VariableAnnotation {
590590
/// Tracks live variable annotations across instructions and produces
591591
/// per-instruction "events" like `name = RDI` or `name = <undef>`.
592592
class VariableAnnotator {
593-
struct VarState {
594-
/// Display name.
595-
std::string name;
596-
/// Last printed location (empty means <undef>).
597-
std::string last_loc;
598-
/// Address range where this variable state is valid.
599-
lldb::addr_t start_address;
600-
lldb::addr_t end_address;
601-
/// Register numbering scheme for location interpretation.
602-
lldb::RegisterKind register_kind;
603-
604-
std::optional<std::string> decl_file;
605-
std::optional<uint32_t> decl_line;
606-
std::optional<std::string> type_name;
607-
};
608-
609593
// Live state from the previous instruction, keyed by Variable::GetID().
610-
llvm::DenseMap<lldb::user_id_t, VarState> Live_;
594+
llvm::DenseMap<lldb::user_id_t, VariableAnnotation> Live_;
611595

612596
static constexpr const char *kUndefLocation = "undef";
613597

@@ -623,11 +607,6 @@ class VariableAnnotator {
623607
std::vector<VariableAnnotation>
624608
AnnotateStructured(Instruction &inst, Target &target,
625609
const lldb::ModuleSP &module_sp);
626-
627-
private:
628-
VariableAnnotation createAnnotation(
629-
const VarState &var_state, bool is_live,
630-
const std::optional<std::string> &location_desc = std::nullopt);
631610
};
632611

633612
} // namespace lldb_private

lldb/source/Core/Disassembler.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,12 @@ VariableAnnotator::AnnotateStructured(Instruction &inst, Target &target,
330330

331331
// If we lost module context, mark all live variables as undefined.
332332
if (!module_sp) {
333-
for (const auto &KV : Live_)
334-
annotations.push_back(createAnnotation(KV.second, false, kUndefLocation));
333+
for (const auto &KV : Live_) {
334+
auto annotation_entity = KV.second;
335+
annotation_entity.is_live = false;
336+
annotation_entity.location_description = kUndefLocation;
337+
annotations.push_back(annotation_entity);
338+
}
335339
Live_.clear();
336340
return annotations;
337341
}
@@ -343,8 +347,12 @@ VariableAnnotator::AnnotateStructured(Instruction &inst, Target &target,
343347
if (!module_sp->ResolveSymbolContextForAddress(iaddr, mask, sc) ||
344348
!sc.function) {
345349
// No function context: everything dies here.
346-
for (const auto &KV : Live_)
347-
annotations.push_back(createAnnotation(KV.second, false, kUndefLocation));
350+
for (const auto &KV : Live_) {
351+
auto annotation_entity = KV.second;
352+
annotation_entity.is_live = false;
353+
annotation_entity.location_description = kUndefLocation;
354+
annotations.push_back(annotation_entity);
355+
}
348356
Live_.clear();
349357
return annotations;
350358
}
@@ -373,7 +381,7 @@ VariableAnnotator::AnnotateStructured(Instruction &inst, Target &target,
373381
// Prefer "register-only" output when we have an ABI.
374382
opts.PrintRegisterOnly = static_cast<bool>(abi_sp);
375383

376-
llvm::DenseMap<lldb::user_id_t, VarState> Current;
384+
llvm::DenseMap<lldb::user_id_t, VariableAnnotation> Current;
377385

378386
for (size_t i = 0, e = var_list.GetSize(); i != e; ++i) {
379387
lldb::VariableSP v = var_list.GetVariableAtIndex(i);
@@ -422,10 +430,11 @@ VariableAnnotator::AnnotateStructured(Instruction &inst, Target &target,
422430
if (const char *type_str = type->GetName().AsCString())
423431
type_name = type_str;
424432

425-
Current.try_emplace(
426-
v->GetID(), VarState{std::string(name), std::string(loc), start_addr,
427-
end_addr, entry.expr->GetRegisterKind(), decl_file,
428-
decl_line, type_name});
433+
Current.try_emplace(v->GetID(),
434+
VariableAnnotation{std::string(name), std::string(loc),
435+
start_addr, end_addr, true,
436+
entry.expr->GetRegisterKind(),
437+
decl_file, decl_line, type_name});
429438
}
430439

431440
// Diff Live_ → Current.
@@ -435,41 +444,33 @@ VariableAnnotator::AnnotateStructured(Instruction &inst, Target &target,
435444
auto it = Live_.find(KV.first);
436445
if (it == Live_.end()) {
437446
// Newly live.
438-
annotations.push_back(createAnnotation(KV.second, true));
439-
} else if (it->second.last_loc != KV.second.last_loc) {
447+
auto annotation_entity = KV.second;
448+
annotation_entity.is_live = true;
449+
annotations.push_back(annotation_entity);
450+
} else if (it->second.location_description !=
451+
KV.second.location_description) {
440452
// Location changed.
441-
annotations.push_back(createAnnotation(KV.second, true));
453+
auto annotation_entity = KV.second;
454+
annotation_entity.is_live = true;
455+
annotations.push_back(annotation_entity);
442456
}
443457
}
444458

445459
// 2) Ends: anything that was live but is not in Current becomes
446460
// <kUndefLocation>.
447461
for (const auto &KV : Live_)
448-
if (!Current.count(KV.first))
449-
annotations.push_back(createAnnotation(KV.second, false, kUndefLocation));
462+
if (!Current.count(KV.first)) {
463+
auto annotation_entity = KV.second;
464+
annotation_entity.is_live = false;
465+
annotation_entity.location_description = kUndefLocation;
466+
annotations.push_back(annotation_entity);
467+
}
450468

451469
// Commit new state.
452470
Live_ = std::move(Current);
453471
return annotations;
454472
}
455473

456-
VariableAnnotation VariableAnnotator::createAnnotation(
457-
const VarState &var_state, bool is_live,
458-
const std::optional<std::string> &location_desc) {
459-
VariableAnnotation annotation;
460-
annotation.variable_name = var_state.name;
461-
annotation.location_description =
462-
location_desc.has_value() ? *location_desc : var_state.last_loc;
463-
annotation.start_address = var_state.start_address;
464-
annotation.end_address = var_state.end_address;
465-
annotation.is_live = is_live;
466-
annotation.register_kind = var_state.register_kind;
467-
annotation.decl_file = var_state.decl_file;
468-
annotation.decl_line = var_state.decl_line;
469-
annotation.type_name = var_state.type_name;
470-
return annotation;
471-
}
472-
473474
void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
474475
const ExecutionContext &exe_ctx,
475476
bool mixed_source_and_assembly,

0 commit comments

Comments
 (0)