From c5da7f7dd5df82e17ef957143c5007285ab11596 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Thu, 31 Oct 2024 17:26:28 -0600 Subject: [PATCH] Show diff color when symbols differ --- objdiff-cli/src/cmd/diff.rs | 8 ++++++-- objdiff-core/src/diff/code.rs | 14 ++++++++++---- objdiff-core/src/diff/display.rs | 11 ++++++----- objdiff-gui/src/views/function_diff.rs | 8 ++++++-- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/objdiff-cli/src/cmd/diff.rs b/objdiff-cli/src/cmd/diff.rs index c24dff4..92ad717 100644 --- a/objdiff-cli/src/cmd/diff.rs +++ b/objdiff-cli/src/cmd/diff.rs @@ -844,10 +844,14 @@ impl FunctionDiffUi { base_color = COLOR_ROTATION[diff.idx % COLOR_ROTATION.len()] } } - DiffText::Symbol(sym) => { + DiffText::Symbol(sym, diff) => { let name = sym.demangled_name.as_ref().unwrap_or(&sym.name); label_text = name.clone(); - base_color = Color::White; + if let Some(diff) = diff { + base_color = COLOR_ROTATION[diff.idx % COLOR_ROTATION.len()] + } else { + base_color = Color::White; + } } DiffText::Spacing(n) => { line.spans.push(Span::raw(" ".repeat(n))); diff --git a/objdiff-core/src/diff/code.rs b/objdiff-core/src/diff/code.rs index 86584e6..6dc48d7 100644 --- a/objdiff-core/src/diff/code.rs +++ b/objdiff-core/src/diff/code.rs @@ -330,8 +330,11 @@ fn compare_ins( let a_str = match a { ObjInsArg::PlainText(arg) => arg.to_string(), ObjInsArg::Arg(arg) => arg.to_string(), - ObjInsArg::Reloc => String::new(), - ObjInsArg::BranchDest(arg) => format!("{arg}"), + ObjInsArg::Reloc => left_ins + .reloc + .as_ref() + .map_or_else(|| "".to_string(), |r| r.target.name.clone()), + ObjInsArg::BranchDest(arg) => arg.to_string(), }; let a_diff = if let Some(idx) = state.left_args_idx.get(&a_str) { ObjInsArgDiff { idx: *idx } @@ -344,8 +347,11 @@ fn compare_ins( let b_str = match b { ObjInsArg::PlainText(arg) => arg.to_string(), ObjInsArg::Arg(arg) => arg.to_string(), - ObjInsArg::Reloc => String::new(), - ObjInsArg::BranchDest(arg) => format!("{arg}"), + ObjInsArg::Reloc => right_ins + .reloc + .as_ref() + .map_or_else(|| "".to_string(), |r| r.target.name.clone()), + ObjInsArg::BranchDest(arg) => arg.to_string(), }; let b_diff = if let Some(idx) = state.right_args_idx.get(&b_str) { ObjInsArgDiff { idx: *idx } diff --git a/objdiff-core/src/diff/display.rs b/objdiff-core/src/diff/display.rs index 262fb82..bc0d080 100644 --- a/objdiff-core/src/diff/display.rs +++ b/objdiff-core/src/diff/display.rs @@ -22,7 +22,7 @@ pub enum DiffText<'a> { /// Branch destination BranchDest(u64, Option<&'a ObjInsArgDiff>), /// Symbol name - Symbol(&'a ObjSymbol), + Symbol(&'a ObjSymbol, Option<&'a ObjInsArgDiff>), /// Number of spaces Spacing(usize), /// End of line @@ -71,7 +71,7 @@ pub fn display_diff( cb(DiffText::Argument(v, diff))?; } ObjInsArg::Reloc => { - display_reloc_name(ins.reloc.as_ref().unwrap(), &mut cb)?; + display_reloc_name(ins.reloc.as_ref().unwrap(), &mut cb, diff)?; } ObjInsArg::BranchDest(dest) => { if let Some(dest) = dest.checked_sub(base_addr) { @@ -92,8 +92,9 @@ pub fn display_diff( fn display_reloc_name( reloc: &ObjReloc, mut cb: impl FnMut(DiffText) -> Result<(), E>, + diff: Option<&ObjInsArgDiff>, ) -> Result<(), E> { - cb(DiffText::Symbol(&reloc.target))?; + cb(DiffText::Symbol(&reloc.target, diff))?; match reloc.addend.cmp(&0i64) { Ordering::Greater => cb(DiffText::Basic(&format!("+{:#x}", reloc.addend))), Ordering::Less => cb(DiffText::Basic(&format!("-{:#x}", -reloc.addend))), @@ -106,7 +107,7 @@ impl PartialEq> for HighlightKind { match (self, other) { (HighlightKind::Opcode(a), DiffText::Opcode(_, b)) => a == b, (HighlightKind::Arg(a), DiffText::Argument(b, _)) => a.loose_eq(b), - (HighlightKind::Symbol(a), DiffText::Symbol(b)) => a == &b.name, + (HighlightKind::Symbol(a), DiffText::Symbol(b, _)) => a == &b.name, (HighlightKind::Address(a), DiffText::Address(b) | DiffText::BranchDest(b, _)) => { a == b } @@ -124,7 +125,7 @@ impl From> for HighlightKind { match value { DiffText::Opcode(_, op) => HighlightKind::Opcode(op), DiffText::Argument(arg, _) => HighlightKind::Arg(arg.clone()), - DiffText::Symbol(sym) => HighlightKind::Symbol(sym.name.to_string()), + DiffText::Symbol(sym, _) => HighlightKind::Symbol(sym.name.to_string()), DiffText::Address(addr) | DiffText::BranchDest(addr, _) => HighlightKind::Address(addr), _ => HighlightKind::None, } diff --git a/objdiff-gui/src/views/function_diff.rs b/objdiff-gui/src/views/function_diff.rs index dea683c..168ed7e 100644 --- a/objdiff-gui/src/views/function_diff.rs +++ b/objdiff-gui/src/views/function_diff.rs @@ -292,10 +292,14 @@ fn diff_text_ui( base_color = appearance.diff_colors[diff.idx % appearance.diff_colors.len()] } } - DiffText::Symbol(sym) => { + DiffText::Symbol(sym, diff) => { let name = sym.demangled_name.as_ref().unwrap_or(&sym.name); label_text = name.clone(); - base_color = appearance.emphasized_text_color; + if let Some(diff) = diff { + base_color = appearance.diff_colors[diff.idx % appearance.diff_colors.len()] + } else { + base_color = appearance.emphasized_text_color; + } } DiffText::Spacing(n) => { ui.add_space(n as f32 * space_width);