Semantic Diff Utilities

Language AgnosticDiffSemantics

Language Agnostic Problem Overview


I'm trying to find some good examples of semantic diff/merge utilities. The traditional paradigm of comparing source code files works by comparing lines and characters.. but are there any utilities out there (for any language) that actually consider the structure of code when comparing files?

For example, existing diff programs will report "difference found at character 2 of line 125. File x contains v-o-i-d, where file y contains b-o-o-l". A specialized tool should be able to report "Return type of method doSomething() changed from void to bool".

I would argue that this type of semantic information is actually what the user is looking for when comparing code, and should be the goal of next-generation progamming tools. Are there any examples of this in available tools?

Language Agnostic Solutions


Solution 1 - Language Agnostic

We've developed a tool that is able to precisely deal with this scenario. Check http://www.semanticmerge.com

It merges (and diffs) based on code structure and not using text-based algorithms, which basically allows you to deal with cases like the following, involving strong refactor. It is also able to render both the differences and the merge conflicts as you can see below:

enter image description here

And instead of getting confused with the text blocks being moved, since it parses first, it is able to display the conflicts on a per method basis (per element in fact). A case like the previous won't even have manual conflicts to solve.

enter image description here

It is a language-aware merge tool and it has been great to be finally able to answer this SO question :-)

Solution 2 - Language Agnostic

Eclipse has had this feature for a long time. It's called "Structure Compare", and it's very nice. Here is a sample screenshot for Java, followed by another for an XML file:

(Note the minus and plus icons on methods in the upper pane.)

Eclipse's Java Structure Comparer Eclipse's XML Structure Comparer

Solution 3 - Language Agnostic

To do "semantic comparisons" well, you need to compare the syntax trees of the languages, and take into account the meaning of symbols. A really good semantic diff would understand the language semantics, and realize when one block of code was equivalent in function to another. Going this far requires a theorem prover, and while it would be extremely cute, isn't presently practical for a real tool.

A workable approximation of this is simply comparing syntax trees, and reporting changes in terms of structures inserted, deleted, moved, or changed. Getting somewhat closer to a "semantic comparison", one could report when an identifier is changed consistently across a block of code.

See our http://www.semanticdesigns.com/Products/SmartDifferencer/index.html for a syntax tree-based comparison engine that works with many languages, that does the above approximation.

EDIT Jan 2010: Versions available for C++, C#, Java, PHP, and COBOL. The website shows specific examples for most of these.

EDIT May 2010: Python and JavaScript added.

EDIT Oct 2010: EGL added.

EDIT Nov 2010: VB6, VBScript, VB.net added

Solution 4 - Language Agnostic

What you're groping for is a "tree diff". It turns out that this is much harder to do well than a simple line-oriented textual diff, which is really just the comparison of two flat sequences.

"A Fine-Grained XML Structural Comparison Approach" concludes, in part with:

> Our theoretical study as well as our experimental evaluation showed that the proposed method yields improved structural similarity results with respect to existing alternatives, while having the same time complexity (O(N^2))

(emphasis mine)

Indeed, if you're looking for more examples of tree differencing I suggest focusing on XML since that's been driving practical developments in that area.

Solution 5 - Language Agnostic

Shameless plug for my own project:

HTML Tree Diff does structure-aware comparison of xml and html documents, written in python.

http://pypi.python.org/pypi/html-tree-diff/0.1.0

Solution 6 - Language Agnostic

The solution to this would be on a per language basis. I.e. unless it's designed with a plugin architecture that defers a lot of the parsing of the code into a tree and the semantic comparison to a language specific plugin then it will be very difficult to support multiple languages. What language(s) are you interested in having such a tool for. Personally I'd love one for C#.

For C# there is an assembly diff add-in to Reflector but it only does a diff on the IL not the C#.

You can download the diff add-in http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=reflectoraddins&DownloadId=7135&FileTime=128165526958930000&Build=14730">here</a> [zip] or go to the project on the codeplex site http://reflectoraddins.codeplex.com/">here</a>;.

Solution 7 - Language Agnostic

A company called Zynamics offers a binary-level semantic diff tool. It uses a meta-assembly language called REIL to perform graph-theoretic analysis of 2 versions of a binary, and produces a color-coded graph to illustrate differences between them. I am not sure of the price, but I doubt it is free.

Solution 8 - Language Agnostic

http://prettydiff.com/

Pretty Diff minifies each input to remove comments and unnecessary white space and then beautifies the code prior to the diff algorithm. I cannot think of anyway to become more code semantic than this. And, its written JavaScript so it runs directly in the browser.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjasonmrayView Question on Stackoverflow
Solution 1 - Language AgnosticpabloView Answer on Stackoverflow
Solution 2 - Language AgnosticHosam AlyView Answer on Stackoverflow
Solution 3 - Language AgnosticIra BaxterView Answer on Stackoverflow
Solution 4 - Language AgnosticbendinView Answer on Stackoverflow
Solution 5 - Language AgnosticChristian OudardView Answer on Stackoverflow
Solution 6 - Language AgnosticJonathan ParkerView Answer on Stackoverflow
Solution 7 - Language AgnosticDavid V McKayView Answer on Stackoverflow
Solution 8 - Language AgnosticaustincheneyView Answer on Stackoverflow