Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1778
  • Last Modified:

Diff tool for C++ code that can ignore comments and white spaces

I'm looking for e good Diff tool for C++ source code that can ignore comments and white spaces.

Does anyone have a good link for such a tool?

I prefer a free tool, but I'll consider a comersial tool if it's well priced.
0
Axter
Asked:
Axter
  • 8
  • 6
  • 5
  • +1
2 Solutions
 
PaulCaswellCommented:
David,

What a brilliant idea! I've felt the need for one of these many times but never got around to searching for one. If you do find one I'd be interested.

I'd also want it to treat stuff like:

int a = 1;
int b = 2;

to be exactly the same as

int b = 2;
int a = 1;

to name a particularly trivial case.

Or even perhaps:

for ( i = 0; i < n; i++ )

to be treated as exactly the same as

for ( i = 0; i < n; ++i )

Paul

Incidentally, you may like to contribute here:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21891654.html
0
 
Harisha M GCommented:
Not a tool.. but an idea:

Remove extra white spaces and comments from both the files, and then compare it ?

For ex, if a file is like:

main()
{
    int a;
}

Change it to (internally)

main() { int a; }

Similarly do the same for the other file, and then compare
0
 
Harisha M GCommented:
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
AxterAuthor Commented:
mgh_mgharish,
I'm not interested in a regular diff tool.
I already have an excellent diff tool that can ignore white-spaces (WinMerge), however it does not ignore comments.

I'm looking for a diff tool that can ignore comments.
0
 
gran88Commented:
You're comment on,
"
Or even perhaps:

for ( i = 0; i < n; i++ )

to be treated as exactly the same as

for ( i = 0; i < n; ++i )
"

The compiler does not see those two expressions as equivelent, though they may result in the same output.  Operator precedance is important, and I believe one is evaluated before the other.
0
 
AxterAuthor Commented:
>>The compiler does not see those two expressions as equivelent, though they may result in the same output.  Operator precedance
>>is important, and I believe one is evaluated before the other.

In general, you're correct.
However, on most moden compilers, if (i) is a built-in type, then it will treat them the same.
I always prefer to write it in the optimal format for consistency. (++i).
0
 
AxterAuthor Commented:
>>What a brilliant idea! I've felt the need for one of these many times but never got around to searching for one. If you do find one I'd
>>be interested.

If I can't find one, I'll probably think about creating my own.
I like adding CVS automated comments to my source code, so I can easily track changes without having to use the poorly designed CVS interface to find the comments.
My co-workers constantly complain about the CVS comments, and one of their major complaints is that the comments add additional noise when mergeing code.
0
 
PaulCaswellCommented:
Axter,

Also, when working with large amounts of code it is good practice to add comments to parts of the code that were difficult to understand so next time its a bit easier. Later, when reviewing all changes before release, all these comments interfere with the review process, which is essentially a file compare with the vcs version. This quickly becomes a force against good coding practice.

If the comparison can be made ignoring comment changes this good practice might even come back. :-)

I suspect that the primary issue is that a line-by-line comparison is simple to implement reliably and quickly while a full syntax-checking comparison could end up expensive time-wise. However, modern computers should be able to handle the extra cycles without even noticing so I think it is a worthy project just for the challenge. If you decide to do it I'd be happy to join in if you think I could help.

Paul
0
 
PaulCaswellCommented:
How would you code it?

My first thought is to take an existing file compare where the source is available, perhaps under public licence. Add a simplistic syntax checker that merely keeps track of 'inString' and 'inComment' status. Then, for each line-difference, arrange for the string changes and comment changes to be hilit in a different colour, which could also be set to normal black-on-white to ignore this kind of difference.

Paul

0
 
Harisha M GCommented:
Paul, why not join the WinMerge coding team (if possible) or look at its code and improve it ?
0
 
PaulCaswellCommented:
Good idea Harish!

What do you think David? Sounds like a place to start if we can find a ready-made one.

Paul
0
 
Harisha M GCommented:
:) would this work ? http:Q_21897914.html#16980074
0
 
PaulCaswellCommented:
gran88,

>>The compiler does not see those two expressions as equivelent, though they may result in the same output.  Operator precedance is important, and I believe one is evaluated before the other.

You are correct, and the code generated may be different too, but the process will be identical and therefore the result must be identical. So the two loop are 'functionally' identical.

Paul
0
 
AxterAuthor Commented:
>>Paul, why not join the WinMerge coding team (if possible) or look at its code and improve it ?

I hate to say it, but I didn't even realize WinMerge is an Open Source project.

This is great!
0
 
AxterAuthor Commented:
>>What do you think David? Sounds like a place to start if we can find a ready-made one.

I'm going to be looking at the code this weekend.
I'll see what it would take to join the team, but at least I know I can make my own changes if necessary.

Here's the WinMerge site, if anyone is interested.
http://winmerge.org
0
 
AxterAuthor Commented:
I was looking to see if there was a previous proposal to do this on WinMerge, and there was:
http://sourceforge.net/tracker/index.php?func=detail&aid=1267732&group_id=13216&atid=363216

In above link, user kimmov seems to indicate that this would be hard to do for multi-line comments.

I'm still going to look into it.
0
 
AxterAuthor Commented:
Apperently WinMerge has a plugin that will work with a single line commet (IgnoreCommentsC.dll).

I just tested it out, and it works pretty good for single lines.
But you have to select the plugin each time you do a diff, which I find annoying.

0
 
AxterAuthor Commented:
To anyone interested, I modified WinMerge to ignore comments.
To download the modified version, use following link:
http://axter.com/winmerge/winmerge.zip

If you want to download the new source code, use following link:
http://axter.com/winmerge/WinMerge-2.4.8-axter-src.zip
0
 
PaulCaswellCommented:
Nice! I'll have a play. Thanks!
0
 
Harisha M GCommented:
Thanks Axter !!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now