Solved

Cleanup the code : removing unused precompiler defines

Posted on 2007-11-29
20
274 Views
Last Modified: 2013-12-14
Hi All,
I'm thinking on cleaning up my C++ code. Due to the accumulation of maintenance codes , I get a unreadable code. The useful code is flooded in a lot of unused pieces of code. For example :

#ifdef _FIX_BUG_455
#ifdef _FIX_BUG_905
if (a<b)
#else
if (a<=b)
#endif
{
// do someting (after the fix of the bug 455)
#else
// do someting (earlier the fix of the bug 455)
#endif

All the defines are now defined for good in a .h file. Thus, there is no need any more to keep the code in the #else section.

Let me know if there is a tool or method that can help me on performing this.
0
Comment
Question by:morisce
  • 8
  • 8
  • 3
20 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 20375028
I'm not sure what your question is, but if you want to get rid of the unneeded preprocessor statements, you can simply run the source file through the pre-compiler, and catch its output.

Also, I see that you use this for some kind of version management ... consider using a true version management tool, like CVS.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20375403
>> get rid of the unneeded preprocessor statements, you can simply run the source file through the pre-compiler, and catch its output.
You need the /E flag to do this...
http://msdn2.microsoft.com/en-us/library/3xkfswhy(VS.80).aspx
0
 
LVL 5

Assisted Solution

by:morisce
morisce earned 125 total points
ID: 20375499
>> I'm not sure what your question is, but if you want to get rid of the unneeded preprocessor statements, >> you can simply run the source file through the pre-compiler, and catch its output.
I WILL GET A DIRTY CODE. I HAVE TO KEEP THE OTHER DEFINES AND INCLUDES WHTHOUT CHANGES. ONLY THE DEFINES USED FOR VERSION MANAGEMENT SHOULD BE DELETED.

>>Also, I see that you use this for some kind of version management ... consider using a true version >>management tool, like CVS.
THAT'S RIGHT. WE'RE USING NOW A VERSION MANAGEMENT SYSTEM. SO WE DON'T NEED ANY MORE TO KEEP THIS BAD WAY OF CODING.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 125 total points
ID: 20375576
Below is how to generate preprocessed files...

You need to run this from a Visual Studio Command Prompt (look for the icon in you Visual Studio program group!)

Be warned though, the output can be huge as it'll include ALL the includes. To avoid this you may want to comment them out first.
cl /E mycode_orig.cpp > mycode_new.cpp

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20376102
>> I HAVE TO KEEP THE OTHER DEFINES AND INCLUDES WHTHOUT CHANGES. ONLY THE DEFINES USED FOR VERSION MANAGEMENT SHOULD BE DELETED.

Well, how do you tell which defines you want to expand, and which ones you want to keep ? If you can't put that in a few simple rules, it's not going to be easy to automate this.

Can you put it in a few simple rules ?
0
 
LVL 5

Author Comment

by:morisce
ID: 20383302
All the defines of  the version management are defined in a single file versionmanagement.h
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20394477
Sorry for the late reply.

>> All the defines of  the version management are defined in a single file versionmanagement.h

And you want to only expand those macro's ? That's something a normal pre-compiler is not made for. Especially if you want to only partially expand a macro (ie. only the else part for example).

If you've got a lot of source code, you'll be best off to write your own limited version of a pre-compiler that does just that.
If the code amount is limited, you can just do it manually ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21065589
What's the reason for the delete ? Why didn't you reply to my last post ?
0
 
LVL 5

Author Comment

by:morisce
ID: 21066054
I have a constraint of time.
I'm aware that writing an own limited pre-compiler is a solution. But it must consume a lot of time. I hoped to find here another "plug and play" solution.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 53

Expert Comment

by:Infinity08
ID: 21066083
As I said, a normal pre-compiler is not made for that. You'll have to write your own solution that will do specifically what you want to do. Developing that can take little time if you stick to a bare-bones solution.

Just derive some simple rules for the changes you want to do for the source code, and then do those changes. Regular expressions and a language like Perl might help you a lot.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21070573
I'd like to know why this Q is being closed. The advice given here was good. Just because the advice give is, "there is no simple way to do this" doesn't mean isn't correct and should be closed, especially if this is then followed up (as is the case here) with suggested work-arounds!

Also, I8 had asked the OP a question to which there had been no response.

M_O, please clarify your stance on this.

-Rx.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21070684
>> M_O, please clarify your stance on this.
BTW: I still think the simplest way to do this is using the preprocessor... It'd be a lot simpler that trying to write a bunch of regex rules to parse this out so I'm sorry but I don't think the proposed answer is the one that should be selected.

IMO the selected answers should be: -

{http:#20375028} 275pts
{http:#20375403} 125pts
{http:#20375576} 125pts
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21070694
>> M_O, please clarify your stance on this.
BTW: I still think the simplest way to do this is using the preprocessor... It'd be a lot simpler that trying to write a bunch of regex rules to parse this out so I'm sorry but I don't think the proposed answer is the one that should be selected.

IMO the selected answers should be: -

{http:#20375028} 250pts (corrected cos I can add up!)
{http:#20375403} 125pts
{http:#20375576} 125pts
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21070733
>> I still think the simplest way to do this is using the preprocessor

The problem is that it won't work due to this requirement :

        "I HAVE TO KEEP THE OTHER DEFINES AND INCLUDES WHTHOUT CHANGES. ONLY
        THE DEFINES USED FOR VERSION MANAGEMENT SHOULD BE DELETED."

A normal precompiler is not made to selectively expand macro's (can yours do that ?). Which is why morisce will need to develop his own tool to do this job, as I suggested.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21070938
>> The problem is that it won't work due to this requirement
Ok, but there is a reasonable of work there to try and parse that out with Perl and regexes (and I have nothing against either), assuming the OP knows Perl and how to write regexes. Also, what is the point of keeping the related preprocessor stuff as this pruning effectively make is superfluous, no?

It would be much simpler to pre-process the files into the form required and then augment them as necessary after. At least, if done that way, we know the correct rules for pre-processing have been applied; otherwise, a log of sanity testing on a Perl/Regex parsed filer would be necessary to ensure it's correct (and nasty surprises could ensure after the fact).

Basically, the problem is there is no safe/nice/easy solution to this problem, hence no safe/nice/easy answer can of has been provided.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21070988
What could be done is to use Perl to duplicate the related pro-processor lines and then comment out the duplicate. Then run the pre-processor on the file and then run another Perl script to uncomment the pro-processor statements that are left. I doubt that would be too hard.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21071300
>> Basically, the problem is there is no safe/nice/easy solution to this problem

That doesn't mean you can't go for the least "bad" solution.

A good "preprecompiler" can be written without too much problems. The correct way to handle preprocessor statements is defined by the C standard, so there's no issue there (as long as you stick to the C standard). I don't see any problem with that approach ... but then again, I'm not deciding :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21071397
>> That doesn't mean you can't go for the least "bad" solution.
Well, 'least "bad"' is subjective; however, I do agree with the point to which you elude :)

>> I don't see any problem with that approach
I'm just observing that writing, debugging and testing a Perl (or other) script to perform the task of the pre-processor with the correct behavior as defined in the C/C++ standard (and we both know how lovely and clear they are eh?) is by no means a simple/trivial task; especially when there is already a preprocessor that can do this. I think the best solution lays with a combination of a clever bit of perl/sed'ing and smart use of the pre-processor but I don't honestly think there is a simple "clean" solution to this.

Anyway, I'm prepared to agree to disagree since it is Friday late afternoon and I've now got my "beer head" on ;)

-Rx.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21071448
I'll drink to that ! Cheers :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

743 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

12 Experts available now in Live!

Get 1:1 Help Now