Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diff utility that is C++ aware

Is there a free diff utility that can compare two C++ files using their ASTs instead of the text?

What I was thinking of is something like:

  • convert both files to AST
  • Render AST back as C++ code (this canonicalizes indentation)
  • Do normal diff between these two
  • Also try to detect simple refactorings that were done (add/del/rename member for example)
like image 885
tohava Avatar asked Jan 09 '13 18:01

tohava


3 Answers

[Asked by one of the other answerers to post the name of a commercial tool.]

Semantic Designs' SmartDifferencer tool will parse C++, and compute a difference based on ASTs; layout formatting simply doesn't matter. The parser is a full C++11 parser. It can parse most source files without expanding most preprocessor directives as long as they are "structured"; C++ preprocessor usage isn't usually as abusive as it is in C.

There are versions of the SmartDifferencer for other languages.

[Disclosure: I'm the CTO at Semantic Designs]

like image 81
Ira Baxter Avatar answered Sep 23 '22 03:09

Ira Baxter


I can think of two alternative solutions to your problem:

  1. Discuss a coding style that will be used as a group and stick to it. You may have to find compromises between the team members' differing personal coding styles.

  2. If you are using source control, add hooks which remove all formatting on a commit and customize the code formatting on checkout. This takes some works but allows team members to use their own formatting style. Of course, this doesn't solve differing opinions in variable naming and other non-formatting code style elements.

like image 39
Code-Apprentice Avatar answered Sep 26 '22 03:09

Code-Apprentice


There are tools like MOSS (http://theory.stanford.edu/~aiken/moss/) which might be of help.

like image 20
A. K. Avatar answered Sep 26 '22 03:09

A. K.