selective preprocessor expansion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • max(01)*

    #1

    selective preprocessor expansion

    hi.

    i want to examine preprocessed source which only has certain macros
    expanded, for example i would like to have:

    #include <stdio.h>
    #include "other.c"
    int main() {
    ...
    }

    tranformed into:

    #include <stdio.h>
    <insert here the result of other.c inclusion>
    int main() {
    ...
    }

    so that #include <stdio.h> is not expanded... in general i would like to
    have standard library header macros not expanded...

    up to now i only have come up with smt like this:

    #ifdef INCLUDE_STDIO
    #include <stdio.h>
    #endif
    #include "other.c"
    int main() {
    ...
    }

    so that i compile using:

    $ cc -DINCLUDE_STDIO source.c

    *or* i can examine preprocessed source using:

    $ cc -E -P source.c

    [note: this is gcc specific, but i think it is clear anyway: -D option
    defines a macro, -E option does preprocessing only, -P "cleans up"
    output a bit]

    any simpler/better/moregeneral/morestandard/moretested method?

    bye

    max
  • Eric Sosman

    #2
    Re: selective preprocessor expansion

    max(01)* wrote:[color=blue]
    > hi.
    >
    > i want to examine preprocessed source which only has certain macros
    > expanded, for example i would like to have:
    > [...][/color]

    Question 10.18 in the comp.lang.c Frequently Asked
    Questions (FAQ) list



    has some pointers you may find helpful.

    --
    Eric.Sosman@sun .com

    Comment

    • Paul Mensonides

      #3
      Re: selective preprocessor expansion

      "max(01)*" <max@fisso.casa > wrote in message
      news:BzqSc.9810 4$OR2.5210283@n ews3.tin.it...
      [color=blue]
      > any simpler/better/moregeneral/morestandard/moretested method?[/color]

      If you are willing (or can) annotate the #include directives, yes. You can
      create a file called, for example, nothing.h which has no contents. Then define
      a macro to remap the inclusions that you want:

      #ifndef DISABLE_STD_INC LUDE
      #define STD(file) <file>
      #else
      #define STD(file) "nothing.h"
      #endif

      #include STD(stdio.h)

      Regards,
      Paul Mensonides


      Comment

      • Cedric LEMAIRE

        #4
        Re: selective preprocessor expansion

        I propose you to use CodeWorker, a universal parsing tool and a source
        code generator, which can combinate both parsing and code generation
        for transforming programs. CodeWorker is a freeware, available at
        "http://www.codeworker. org".

        I wrote you two little scripts that, I hope, answer your needs. Type:
        codeworker ExpandIncludes. cws <c-file1> ... <c-fileN> -I <path1> ...
        -I <pathn>

        where the leader script "ExpandIncludes .cws" is worth:
        // This function searches the location of an included file path,
        // or returns an empty string if not found
        function getIncludedFile Path(sInclude : value, sCSourcePath : value)
        {
        // do not expand the file twice
        if this.findElemen t(sInclude.getS hortFilename()) return false;
        insert this[sInclude.getSho rtFilename()];
        // normalize the path of the included file: '/' only
        sInclude = sInclude.replac eString('\\', '/');
        // if an absolute path, its location is known with certainty
        if sInclude.startS tring('/') || (sInclude.charA t(1) == ':' &&
        sInclude.charAt (2) == '/') {
        return sInclude;
        }
        // is the included file relative to the C source file?
        local iIndex = sCSourcePath.fi ndLastString('/');
        sCSourcePath = sCSourcePath.le ftString($iInde x + 1$);
        if existFile(sCSou rcePath + sInclude) return sCSourcePath +
        sInclude;
        // if not, looks into -I switches passed on the command line
        if existFile(sIncl ude) return sInclude;
        // failure: included file not found
        traceLine("warn ing: unable to find file \"" + sInclude + "\"");
        return false;
        }

        // iterates all C files passed on the command line and
        // expand them
        foreach i in _ARGS {
        translate("Expa ndIncludes.cwp" , project, i, i + 'c');
        }

        and where the BNF translation script "ExpandIncludes .cwp" is worth:
        #implicitCopy // What We Parse Is What We Write

        // BNF non terminal, whose production rule scans the C source and
        // replaces '#include' directives having the file name between double
        quotes,
        // by the content of the file.
        preprocessing ::=
        // C++-like comments and whitespaces are ignored
        // between BNF terminals and non-terminals, so C are
        #ignore(C++)
        [
        // jumps to the next '#include' or to the next constant string
        ->[
        // do not copy what is parsed during this sequence
        // into the output file
        #explicitCopy
        // BNF terminals to match
        '#'
        "include"
        // read the content of a C-like constant string
        #readCString:sI nputFile
        // do not ignore whitespaces or comments anymore,
        // up to the end of the sequence
        #!ignore
        ->'\n'
        // construct the correct path for the included file
        => local sPath = getIncludedFile Path(sInputFile ,
        getInputFilenam e());
        // continue only if the path isn't empty
        #check(sPath)
        => {
        // the path was found, load its content and
        // inject it in the output file (template-based syntax)
        // after having applied the expansion on it recursively
        traceLine("expa nd the include file \"" + sPath + "\"");
        local sContent = loadFile(sPath) ;
        sContent = translateString ("ExpandInclude s.cwp", this,
        sContent);
        @@sContent@@
        }
        |
        // handle the special case of constant string:
        // we may encounter one with '#include ...' inside!
        #readCString
        // now, the danger is gone!
        ]
        ]*
        // copy implicitly up to the end of the file
        ->#empty
        ;


        Note that, to make the script shorter, I don't resolve the conditional
        preprocessor directives and I suppose that you don't have two files
        with the same name at different directories.


        Regards,

        Cedric
        --
        "max(01)*" <max@fisso.casa > wrote in message news:<BzqSc.981 04$OR2.5210283@ news3.tin.it>.. .
        [color=blue]
        > i want to examine preprocessed source which only has certain macros
        > expanded, for example i would like to have:
        >
        > #include <stdio.h>
        > #include "other.c"
        > int main() {
        > ...
        > }
        >
        > tranformed into:
        >
        > #include <stdio.h>
        > <insert here the result of other.c inclusion>
        > int main() {
        > ...
        > }
        >
        > so that #include <stdio.h> is not expanded... in general i would like to
        > have standard library header macros not expanded...[/color]

        Comment

        • max(01)*

          #5
          Re: selective preprocessor expansion

          Eric Sosman wrote:[color=blue]
          > max(01)* wrote:
          >[color=green]
          >> hi.
          >>
          >> i want to examine preprocessed source which only has certain macros
          >> expanded, for example i would like to have:
          >> [...][/color]
          >
          >
          > Question 10.18 in the comp.lang.c Frequently Asked
          > Questions (FAQ) list
          >
          > http://www.eskimo.com/~scs/C-faq/top.html
          >
          > has some pointers you may find helpful.
          >[/color]

          oops! my apologies... :-(
          thank you very mucho for the pointers...
          <OT> anyways, if anyone knows some technique which applies to the gnu
          compiler, suggestions are welcome ;-( </OT>

          bye, max

          Comment

          • max(01)*

            #6
            Re: selective preprocessor expansion

            many thanks to all kind repliers

            max

            Comment

            • Emmanuel Delahaye

              #7
              Re: selective preprocessor expansion

              max(01)* wrote on 12/08/04 :[color=blue]
              > <OT> anyways, if anyone knows some technique which applies to the gnu
              > compiler, suggestions are welcome ;-( </OT>[/color]

              They know:

              news:gnu.gcc.he lp

              --
              Emmanuel
              The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

              "C is a sharp tool"

              Comment

              Working...