Tool to remove obsolete #ifdef statements from a C program?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Tool to remove obsolete #ifdef statements from a C program?
Dear all,
I have an old C program that has some #ifdef foo statements which are defined in every build of the program. Since it would be exactly the same if they were there or not I'd like to get rid of them to make things easier to look at. The problem is that there are rather a lot of them, does anyone know of a pre-existing tool to remove all the #ifdef foo statements and their associated #elif, #else and #endif lines?
I could script this myself but don't want to waste the effort if a tool already exists!
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
you could write a program to do it:
Code:
-psudeo code-
file1 = open inputfile read
file2 = open outputfile write
while not end-of-file file1
getline from file1 into buffer
compare buffer with #ifdef,#else,#endif
if buffer not = #ifdef,#else,#endif then
write buffer to file2
while end
close file1,file2
return
$ more file
1 text i want
#ifdef <token>
code
#else
code
#endif
2 text i want
3 text i want
#ifdef <token>
code
#else
code
#endif
$ awk -vf=0 '/#endif/{f=0;next}/#ifdef/{f=1}f==0' file
1 text i want
2 text i want
3 text i want
If I understood correctly, the OP wants to keep the code contained in the ifdefs -he just wants to eliminate the ifdef statements.
If he wants to eliminate both the ifdef statements and the code they contain, then he could use 'unfidef' which is part of the linux kernel sources, or available separately.
$ more file
1 text i want
#ifdef <token>
code
#else
code
#endif
2 text i want
3 text i want
#ifdef <token>
code
#else
code
#endif
$ awk -vf=0 '/#endif/{f=0;next}/#ifdef/{f=1}f==0' file
1 text i want
2 text i want
3 text i want
You need to keep one condition or the other; eliminating both changes the code. There should be a line that says "code" still, whether it be the first or the second.
Kevin Barry
You might check to see if Eclipse has some refactoring tool to do this for you (though I recommended command-line before) so that context, etc. can be taken into account.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.