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?

Looks like there has been some research done on tree edit distance. Applying that to the AST's seems like it would be the first thing to try. (If someone wanted to try writing this kind of thing.)
–
Jay KominekFeb 7 '09 at 8:40

2

I'm not sure if it would really be useful. a difference like the one you mentioned is more easily seen than read, especially if you have a tool highlighting differences within a line. the ability to recognize if some code has just been moved around unchanged would be easier and more useful, imho!
–
UncleZeivFeb 7 '09 at 8:48

1

@UncleZeiv I would hope that feature would naturally follow from the nature of the tool. In addition, it would be able to detect that there are no changes if someone went through and changed the curly brace or indent styles, for example, or rearranged the file so static methods are grouped, etc..
–
jasonmrayFeb 7 '09 at 9:38

I need this in Visual Studio now. Forcing developers within a team to use the same formatting structure to facilitate diffs is backwards thinking. The code should be formatted to some standard on check-in, and any time a developer opens a file, it should be formatted to their liking. I'm shocked this sort of thinking isn't more wide spread at this point.
–
LangdonFeb 9 '12 at 3:01

9 Answers
9

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:

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.

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

Does Structure Compare allow you to merge changes like other source control merge editors? I.e. Copy this method from this version to the other version.
–
Jonathan ParkerMar 8 '09 at 3:13

1

Yes, when you select a change or a difference (either in the upper or lower panes), the toolbar buttons (shown in the screenshots) give you the option to copy the change from left to right or vice versa.
–
Hosam AlyMar 8 '09 at 8:03

1

Unfortunately, the screenshots are no longer visible in your (highest-upvoted and accepted!) answer. Could you submit them again?
–
blubbApr 23 '13 at 7:27

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.

To be more specific, looking for diff3 not plain diff2
–
The ANTLR GuyNov 20 '10 at 18:26

2

@Terence: No publication exists of our diff algorithm. It is a Levenstein min distance computation using suffix trees to identify equal subtrees, with some huerstics to handle renaming. IIRC, Yang had a paper on this in Software Practice and Experience. Ours and Yang's are diff2, not diff3.
–
Ira BaxterNov 20 '10 at 20:47

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.

Thanks for the link. I can think of a few different approaches for implementing sematic diff tools, and you are correct -- most can be abstracted into a "tree diff". More complex situations may even need to be abstracted into a "graph diff".
–
jasonmrayMar 7 '09 at 20:56

Yea. IBM's Rational Modeler (built on eclipse) tries to do this with UML models (showing the differences between two models graphically). I can't comment on the usefulness of the results as I don't use it much.
–
bendinMar 7 '09 at 20:59

I agree that XML is a good place to start, as you can simply come up with schemas to represent other structures (such as java code, for example), and use an XML based tree-diff to implement a code diff.
–
jasonmrayMar 7 '09 at 20:59

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.

Then you have a limited imagination! What about swapping the positions of two methods in a file while leaving them unchanged? What about refactorings?
–
Robin GreenJul 2 '11 at 18:13

(You can't swap around data declarations in Java this way, and still have equivalence, due to initializers; I assume C# has similar troubles). If you go for pure semantic diff, then you are trying to solve Turing machine equivalence. There a lot of range for doing better then pure text matching, and worse than Turing impossible.
–
Ira BaxterDec 19 '12 at 18:51

Eclipse is able to do that. Check my answer.
–
Hosam AlyMar 8 '09 at 14:58

1

Is there a "pretty printer" wrapped into any of the text diff tools available? This seems like a much more trivial thing to create considering how many false positives it would save.
–
Andrew HubbsSep 28 '10 at 4:44

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 here [zip] or go to the project on the codeplex site here.

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.