Skip to content

Commit 7f14b68

Browse files
committed
Ignore PlainText segments when diffing
1 parent c5da7f7 commit 7f14b68

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

objdiff-core/src/diff/code.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,10 @@ fn compare_ins(
299299
) -> Result<InsDiffResult> {
300300
let mut result = InsDiffResult::default();
301301
if let (Some(left_ins), Some(right_ins)) = (&left.ins, &right.ins) {
302-
if left_ins.args.len() != right_ins.args.len()
303-
|| left_ins.op != right_ins.op
304-
// Check if any PlainText segments differ (punctuation and spacing)
305-
// This indicates a more significant difference than a simple arg mismatch
306-
|| !left_ins.args.iter().zip(&right_ins.args).all(|(a, b)| match (a, b) {
307-
(ObjInsArg::PlainText(l), ObjInsArg::PlainText(r)) => l == r,
308-
_ => true,
309-
})
310-
{
302+
// Count only non-PlainText args
303+
let left_args_count = left_ins.iter_args().count();
304+
let right_args_count = right_ins.iter_args().count();
305+
if left_args_count != right_args_count || left_ins.op != right_ins.op {
311306
// Totally different op
312307
result.kind = ObjInsDiffKind::Replace;
313308
state.diff_count += 1;
@@ -318,7 +313,7 @@ fn compare_ins(
318313
result.kind = ObjInsDiffKind::OpMismatch;
319314
state.diff_count += 1;
320315
}
321-
for (a, b) in left_ins.args.iter().zip(&right_ins.args) {
316+
for (a, b) in left_ins.iter_args().zip(right_ins.iter_args()) {
322317
if arg_eq(config, left_obj, right_obj, a, b, left, right) {
323318
result.left_args_diff.push(None);
324319
result.right_args_diff.push(None);

objdiff-core/src/diff/display.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,31 @@ pub fn display_diff<E>(
5858
cb(DiffText::Spacing(4))?;
5959
}
6060
cb(DiffText::Opcode(&ins.mnemonic, ins.op))?;
61+
let mut arg_diff_idx = 0; // non-PlainText index
6162
for (i, arg) in ins.args.iter().enumerate() {
6263
if i == 0 {
6364
cb(DiffText::Spacing(1))?;
6465
}
65-
let diff = ins_diff.arg_diff.get(i).and_then(|o| o.as_ref());
66+
let diff = ins_diff.arg_diff.get(arg_diff_idx).and_then(|o| o.as_ref());
6667
match arg {
6768
ObjInsArg::PlainText(s) => {
6869
cb(DiffText::Basic(s))?;
6970
}
7071
ObjInsArg::Arg(v) => {
7172
cb(DiffText::Argument(v, diff))?;
73+
arg_diff_idx += 1;
7274
}
7375
ObjInsArg::Reloc => {
7476
display_reloc_name(ins.reloc.as_ref().unwrap(), &mut cb, diff)?;
77+
arg_diff_idx += 1;
7578
}
7679
ObjInsArg::BranchDest(dest) => {
7780
if let Some(dest) = dest.checked_sub(base_addr) {
7881
cb(DiffText::BranchDest(dest, diff))?;
7982
} else {
8083
cb(DiffText::Basic("<unknown>"))?;
8184
}
85+
arg_diff_idx += 1;
8286
}
8387
}
8488
}

objdiff-core/src/diff/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub struct ObjInsDiff {
245245
pub branch_from: Option<ObjInsBranchFrom>,
246246
/// Branches to instruction
247247
pub branch_to: Option<ObjInsBranchTo>,
248-
/// Arg diffs
248+
/// Arg diffs (only contains non-PlainText args)
249249
pub arg_diff: Vec<Option<ObjInsArgDiff>>,
250250
}
251251

objdiff-core/src/obj/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub enum ObjInsArg {
8585
}
8686

8787
impl ObjInsArg {
88+
#[inline]
89+
pub fn is_plain_text(&self) -> bool { matches!(self, ObjInsArg::PlainText(_)) }
90+
8891
pub fn loose_eq(&self, other: &ObjInsArg) -> bool {
8992
match (self, other) {
9093
(ObjInsArg::Arg(a), ObjInsArg::Arg(b)) => a.loose_eq(b),
@@ -112,6 +115,14 @@ pub struct ObjIns {
112115
pub orig: Option<String>,
113116
}
114117

118+
impl ObjIns {
119+
/// Iterate over non-PlainText arguments.
120+
#[inline]
121+
pub fn iter_args(&self) -> impl DoubleEndedIterator<Item = &ObjInsArg> {
122+
self.args.iter().filter(|a| !a.is_plain_text())
123+
}
124+
}
125+
115126
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
116127
pub enum ObjSymbolKind {
117128
#[default]

0 commit comments

Comments
 (0)