Solved

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

Posted on 2006-06-24
20
1,702 Views
Last Modified: 2012-08-14
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
Comment
Question by:Axter
  • 8
  • 6
  • 5
  • +1
20 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16980049
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16980074
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16980079
0
 
LVL 30

Author Comment

by:Axter
ID: 16981161
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
 
LVL 2

Expert Comment

by:gran88
ID: 16983939
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
 
LVL 30

Author Comment

by:Axter
ID: 16984340
>>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
 
LVL 30

Author Comment

by:Axter
ID: 16984592
>>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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16987545
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16987679
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
 
LVL 37

Accepted Solution

by:
Harisha M G earned 300 total points
ID: 16987765
Paul, why not join the WinMerge coding team (if possible) or look at its code and improve it ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 200 total points
ID: 16987846
Good idea Harish!

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

Paul
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16987896
:) would this work ? http:Q_21897914.html#16980074
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16987909
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
 
LVL 30

Author Comment

by:Axter
ID: 16987933
>>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
 
LVL 30

Author Comment

by:Axter
ID: 16987951
>>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
 
LVL 30

Author Comment

by:Axter
ID: 16992398
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
 
LVL 30

Author Comment

by:Axter
ID: 16992705
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
 
LVL 30

Author Comment

by:Axter
ID: 17039607
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17039687
Nice! I'll have a play. Thanks!
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 17046130
Thanks Axter !!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now