简体   繁体   中英

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)

[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]

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.

像MOSS( http://theory.stanford.edu/~aiken/moss/ )这样的工具可能会有所帮助。

You could pass the code through AStyle to normalize the indentation/spacing/formatting before doing the diff. This will not do anything for refactorings, but honestly you would need a full preprocess/compile pass to do this properly AFAICS.

http://astyle.sourceforge.net/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM