prassi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monk,

Here is the problem description, I have a .h file which has all the "macro" flags definition and the .c file uses these macro.

The problem is I need to remove the part of the code which are undefined in the .c file. Here is the example,

Below is the example of "types.h" file,

#include <stdio.h> #include <stdlib.h> #define ALPHA 1 #define BETA #define GAMMA 0

Here is the corresponding C file

#include "types.h" void main() { int a, b, c; float d,e,f; #if (ALPHA) c = a+b; #endif #ifdef BETA f = e*d; #if (GAMMA) c = c + 10; #endif #endif }
The above example is a dummy example from the above code the result has to be like,
void main() { int a, b, c; float d,e,f; #if (ALPHA) c = a+b; #endif #ifdef BETA f = e*d; #endif }

I was reading through the XS of perl will this help me, as of now I dont have any code or solution still looking for ideas.

Thanks a lot, -Prassi

Replies are listed 'Best First'.
Re: C macro pre processing
by CountZero (Bishop) on Jun 24, 2012 at 08:19 UTC
    The "E" switch of gcc tells it to do only pre-processing and then stop. Output is to standard output, so you will want to capture that in a file.

    However, the pre-processor goes all the way and resolves all macros, leaving no trace of them anymore. Its output is:

    void main() { int a, b, c; float d,e,f; c = a+b; f = e*d; }
    Actually it is much longer, related to the stdio.h and stdlib.h files.

    And that is exactly where your problems will be: you will have to write a dedicated pre-processor which goes to all the included files to see which macros are and remain undefined (macros can be anywhere, not just in your header files) and then parse all files to see which code is to be deleted, leaving all other macro references in place.

    Of course your example is trivial, but once you have many #if and #ifdef tests deeply nested, parsing the .c files will become more and more complicated. It will no longer be a straight search-and-delete thing, but you will actually have to parse what you are handling.

    Anyhow, have a look at Text::EP3. It is an extensible pre-processor written in Perl, so you may be able to adapt it to your needs.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: C macro pre processing
by BrowserUk (Patriarch) on Jun 24, 2012 at 06:53 UTC

    Doesn't gcc -E file.c (or cl /E file.c) do exactly this?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      As far as I understand gcc -E , it expands all macros to their definition, while the OP seems to want to only remove the parts that vanish due to their guarding #define being false or not defined.

      That doesn't fit the problem description, but it might well be the solution the OP wants.

        Ah! Yes, I can see the utility of such a tool. It would be very useful when exploring complex code.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: C macro pre processing
by roboticus (Chancellor) on Jun 24, 2012 at 15:03 UTC

    prassi:

    I recently answered a very similar question with a quick hack which you might be able to massage into something useful.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: C macro pre processing
by bulk88 (Priest) on Jun 24, 2012 at 14:25 UTC
Re: C macro pre processing
by Anonymous Monk on Jun 24, 2012 at 06:06 UTC

    I was reading through the XS of perl will this help me, as of now I dont have any code or solution still looking for ideas.

    I was reading through your previous questions and can't help but think , surely there must be some gnu gcc refactoring tools, at least something to parse source into a tree, maybe splint can do it, maybe it can't, I really don't know, but this is where I would look