TOOL FOR REMOVING TOO MANY #ifdef's 
Author Message
 TOOL FOR REMOVING TOO MANY #ifdef's

Hi,

I was given a huge "PORTABLE" C/C++ source code that's supposed to
compile under DOS, Unix, Windows, etc.  I am currently adding some
features while porting the source set to Windows95.  Of course I
am having quite a bit of problems.

The task of adding new features and building a new Win95 port is
IMMENSELY exacerbated because the source set has a huge number
of #ifdef's for various OS/Version/features builds.
And frankly, I have no idea at all what some of these #ifdef's mean
(Ex. #ifdef _PRACTICAL).

What I would like to do is "SELECTIVELY PREPROCESS THE SOURCE
WITHOUT SUBSTITUTING MACROS". This way I figure I could reduce
the size of the source set by 50%, and make it more intellible.
I tried GNU CPP, but I could not find a way "NOT TO SUBSTITUTE MACROS".

Could anyone point me to a tool that does
   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"

Thanks, Adrian



Tue, 04 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:


> >Could anyone point me to a tool that does
> >   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"
> The name of the tool is the C preprocessor.  Remove the definitions of
> the macros and they won't be substituted.

Dan, where does one find this C preprocessor in general?  Especially one
that's nice enough to produce a text file output?    The Standard certainly
doesn't mandate that an implementation provide that sort of tool.  

Isn't the concept of a preprocessor separate from the C compiler archaic?

The so-called C preprocessor is just a compilation phase if I'm reading the
Standard correctly (ISO subclause 5.1.1.2).



Tue, 04 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:
>Could anyone point me to a tool that does
>   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"

The name of the tool is the C preprocessor.  Remove the definitions of
the macros and they won't be substituted.

The practical way to do this is to create a new header, nomacros.h and
to #undef all the macros (or only those you don't want to be substituted)
in that header.  Include this header after all the other headers have
been included.

'grep #define *.h' or its equivalent for your platform will give you the
list of all the macros defined in the other headers.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Tue, 04 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:

>Hi,

>I was given a huge "PORTABLE" C/C++ source code that's supposed to
>compile under DOS, Unix, Windows, etc.  I am currently adding some
>features while porting the source set to Windows95.  Of course I
>am having quite a bit of problems.

>The task of adding new features and building a new Win95 port is
>IMMENSELY exacerbated because the source set has a huge number
>of #ifdef's for various OS/Version/features builds.
>And frankly, I have no idea at all what some of these #ifdef's mean
>(Ex. #ifdef _PRACTICAL).

>What I would like to do is "SELECTIVELY PREPROCESS THE SOURCE
>WITHOUT SUBSTITUTING MACROS". This way I figure I could reduce
>the size of the source set by 50%, and make it more intellible.
>I tried GNU CPP, but I could not find a way "NOT TO SUBSTITUTE MACROS".

>Could anyone point me to a tool that does
>   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"

>Thanks, Adrian

    I have a perl script that does something like that, (perl is
    available for MSDOS).

    ftp.cs.wisc.edu:/coral/tmp/elim

    Usage: elim [-p <defined name>] [-n <defined name>] files

    -p:    Save the positive (defined) version of the ifdef.
    -n:    Save the negative (not defined) version of the ifdef.

    Example:

    #ifdef    BLAH
            for(i=0; i<5; i++){
    #    else
            for(i=5; i>0; i-){
    #endif

    Running this code through 'elim -p BLAH' will result in:

            for(i=0; i<5; i++){
    Note that the other version would have resulted if the test was #ifndef

    Shaun
--

   "In your heart you know its flat."
                           -Flat Earth Society



Tue, 04 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:


>>Could anyone point me to a tool that does
>>   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"
>The name of the tool is the C preprocessor.  Remove the definitions of
>the macros and they won't be substituted.
>The practical way to do this is to create a new header, nomacros.h and
>to #undef all the macros (or only those you don't want to be substituted)
>in that header.  Include this header after all the other headers have
>been included.
>'grep #define *.h' or its equivalent for your platform will give you the
>list of all the macros defined in the other headers.

There are a number of reasons why you probably don't want to do it
this way.

o The contents of your include files will be in the output instead of
"#include <whatever>".

o You'll get a whole lot of '#line "foo.c" 104'

o Your comments will be gone.

o If you have any #errors you're out of luck.

o You can't undefine __LINE__, __FILE__, __DATE__, etc.

o Any #pragmas will be gone.

etc.

If you have emacs, get the cpp.el library (comes standard with 19.30)
and do the following:  Get familiar with it a little, and then set the
face for the things you don't want to 'invisible'.  Once the file
looks the way you want it, use the following code to delete the stuff
you made invisible.

(defun cpp-delete-invisible-text ()
  "Delete text in invisible cpp overlays."
  (interactive)
  (cpp-delete-regions-with-prop 'invisible))

(defun cpp-delete-regions-with-prop (prop)
  "Delete the text of overlays with property PROP"
  (let ((overlays cpp-overlay-list))
    (while overlays
      (if (overlay-get (car overlays) prop)
          (progn
            (overlay-put (car overlays) 'modification-hooks nil)
            (delete-region (overlay-start (car overlays))
                           (overlay-end (car overlays)))))
      (setq overlays (cdr overlays)))))

Save the file and kill the buffer immediately, as the above code
breaks some assumptions made by cpp.el, and I have no idea what the
consequences are.

If you don't have emacs, get emacs and see above. ;)

C-u 100 M-x praise-emacs

My two cents...

Todd

Quote:
>Dan
>--
>Dan Pop
>CERN, CN Division

>Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 05 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's
Thanks for your good pointer.
Computing is always made more fun by people like you.

Best Regards,
Adrian



Sun, 09 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:
>The task of adding new features and building a new Win95 port is
>IMMENSELY exacerbated because the source set has a huge number
>of #ifdef's for various OS/Version/features builds.
>And frankly, I have no idea at all what some of these #ifdef's mean
>(Ex. #ifdef _PRACTICAL).

>What I would like to do is "SELECTIVELY PREPROCESS THE SOURCE
>WITHOUT SUBSTITUTING MACROS". This way I figure I could reduce
>the size of the source set by 50%, and make it more intellible.
>I tried GNU CPP, but I could not find a way "NOT TO SUBSTITUTE MACROS".

There is a program called "unifdef" in 4.3+ BSD distributions (including
FreeBSD) that will selectively preprocess code to remove ifdef's for
particular symbols you specify, leaving the others alone.  It admits
to being a little stupid in parsing, and it doesn't understand
#if defined(SYMBOL).  The experts could probably rip it apart for
corner cases of commented-out preprocessor statements and similar
issues.  But it might work for you.

If you use this on important source, I'd verify that you get identical
object from the before and after source before trusting it.  And
run a diff on the source and see that it's not doing something
you don't want.

                                        Gordon L. Burditt
                                        sneaky.lerctr.org!gordon



Sun, 09 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:


>> >Could anyone point me to a tool that does
>> >   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"
>> The name of the tool is the C preprocessor.  Remove the definitions of
>> the macros and they won't be substituted.

>Dan, where does one find this C preprocessor in general?  Especially one
>that's nice enough to produce a text file output?    The Standard certainly
>doesn't mandate that an implementation provide that sort of tool.  

The original poster didn't ask for a solution guaranteed by the ANSI C
standard :-)

Quote:
>Isn't the concept of a preprocessor separate from the C compiler archaic?

It doesn't have to be separate from the C compiler.  

About the "archaicity" of the concept, all I can say is that I work
with plenty of modern compilers which use a separate preprocesor.  

The concept of a round wheel is a little bit more archaic, yet I have
to see a car with oval or square wheels :-)

Quote:
>The so-called C preprocessor is just a compilation phase if I'm reading the
>Standard correctly (ISO subclause 5.1.1.2).

Compilers which don't use a separate preprocessor, usually have options
to produce the output of the preprocessing phase either to stdout or to
a file.  It's very difficult to debug complex #define sequences without
being able to see the output of the preprocessing phase, so all real
implementations I know of make this output available to the programmer,
one way or another, even if this is not required by the standard.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 09 Aug 1998 03:00:00 GMT  
 TOOL FOR REMOVING TOO MANY #ifdef's

Quote:
>Could anyone point me to a tool that does
>   "SELECTIVELY PREPROCESS THE SOURCE WITHOUT SUBSTITUTING MACROS"

Many versions of Unix (including old SunOS versions, and current SGI versions)
have a program called "unifdef".  You run it with something like
unifdef -DSunOS -USolaris foo.c
and it will remove any code that is '#ifdef Solaris' or '#ifndef SunOS'
and keep any code that is '#ifndef Solaris' or '#ifdef SunOS'.

--

<a href="http://www.servtech.com/public/ptomblin/">My home page</a>
"The superior pilot uses his superior judgement to avoid situations in which
he has to demonstrate his superior skill" - anon.



Sun, 09 Aug 1998 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. TOOL FOR REMOVING TOO MANY #ifdef's

2. Tool to strip #ifdef's wanted

3. what's the effect of this #ifdef?

4. where to find all those #ifdef's and #defines

5. where to find all those #ifdef FOO's

6. #ifdef'ing preprocessor commands

7. Compiler limit on nested #if or #ifdef's

8. Need to strip #ifdef's

9. Newbie: #ifdef's in resource file?

10. #ifdef's ..

11. Tool for removing surplus #includes

12. tool to remove irrelevant prototypes

 

 
Powered by phpBB® Forum Software