user1150854
user1150854

Reputation: 21

Find functional changes between two revisions of a file (compile diff?)

I'm looking for a tool that checks whether two (C) source code files generate the same binary so that I can find actual functional changes between two files and ignore mere coding style changes. It would be great if this worked even within a file for different changesets, so a file may have changed in coding style on some places, but also had one functional patch added.

Upvotes: 1

Views: 328

Answers (5)

xralf
xralf

Reputation: 3652

There is a branch of computer science that deals with concurrency and parallel processes. One of the applications is deciding whether two systems are behaviorally equivalent (in some bisimulation relation (weak or strong)).

Though it's computationally very difficult to decide whether two large systems are behaviorally equivalent. The usage is mainly for verification of small critical applications where we can't afford failure.

Upvotes: 0

Ian Collins
Ian Collins

Reputation: 141

The only way is to compile both with the same compiler options and do a binary diff.

It's not only style changes you'd have to look out for; someone may have extracted code to a function that gets inlined in an optimised build. This may, or may not, depending on compiler options and version, give the same binary.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

If all you're interested in is whether they both "generate the same binary", then the easiest solution is simply to generate both binaries, and compare.

Note, however, that there are things that would result in binaries that are bitwise different, even though they're functionally identical:

  • Change in external function names
  • Optimisations
  • Reordering non-dependent code snippets
  • etc.

Upvotes: 0

unwind
unwind

Reputation: 399843

It's very very hard to write a program to figure out the "functional" result of another program. Such a program sounds like it would be necessary for this. I would guess that computer programs themselves are right about the most compact and machine-readable way we have to even describe functionality, so it's kind of hard to write a program that analyses a program and generates a "better" description.

Somehow abstracting out and "understanding" that coding style differences don't affect functionality also sounds very, very hard. I find it hard when manually reading other people's code somehow, because the differences in style can be pretty large, even though the end result might be the same in "my style".

I would be surprised if a solution wouldn't also require a solution to the halting problem, which is proven impossible for the general case.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121669

  • Mapping binary back to source to "high level functionality" - unlikely.

  • Comparing two source files with respect to "high level functionality" (ignoring coding style) - possible:

    http://cscope.sourceforge.net/

  • Alternative suggestion:

    Write a tool that "normalizes" your source files - by applying the same formatting to both sets of code.

    This can easily be automated.

    For example:

    1) checkout both from version control,

    2) apply "standard format",

    3) compare

Upvotes: 0

Related Questions