Seeking preprocessor-type software for stripping unused code - Linux

This is a discussion on Seeking preprocessor-type software for stripping unused code - Linux ; I'm looking for a program which does preprocessor-type processing of source files to strip out all of the non-operational #if's and to eliminate all of the operational preprocessor commands too leaving just the code that will be compiled. I'd like ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: Seeking preprocessor-type software for stripping unused code

  1. Seeking preprocessor-type software for stripping unused code

    I'm looking for a program which does preprocessor-type processing of source
    files to strip out all of the non-operational #if's and to eliminate all of
    the operational preprocessor commands too leaving just the code that will be
    compiled. I'd like an option to strip comments as well.

    TIA
    Norm


  2. Re: Seeking preprocessor-type software for stripping unused code

    Norm Dresner wrote:
    > I'm looking for a program which does preprocessor-type processing of source
    > files to strip out all of the non-operational #if's and to eliminate all of
    > the operational preprocessor commands too leaving just the code that will be
    > compiled. I'd like an option to strip comments as well.
    >
    > TIA
    > Norm
    >


    gcc -E ?

    --

    Tauno Voipio
    tauno voipio (at) iki fi

  3. Re: Seeking preprocessor-type software for stripping unused code

    On 2007-01-12, Tauno Voipio wrote:
    > Norm Dresner wrote:
    >> I'm looking for a program which does preprocessor-type processing of source
    >> files to strip out all of the non-operational #if's and to eliminate all of
    >> the operational preprocessor commands too leaving just the code that will be
    >> compiled. I'd like an option to strip comments as well.

    >
    > gcc -E ?


    Nope.

    That does all sorts of other stuff (expanding #include
    statements, macro expansion and substitution, etc.). What you
    get is completely unreadable. I've often wanted to do what the
    OP describes, but I've never found a tool that does it.

    That I usually end up doing is loading the source file into a
    folding editor (like Scite) that allows you to hide all of the
    code that won't be compiled. It takes some time to go through
    and manually fold up all of the unmet #if conditions, but it's
    the only thing I know of.

    --
    Grant Edwards grante Yow! HERE!! Put THIS
    at on!! I'm in CHARGE!!
    visi.com

  4. Re: Seeking preprocessor-type software for stripping unused code

    In article <12qgh33r1dgjuf5@corp.supernews.com>,
    Grant Edwards wrote:
    >On 2007-01-12, Tauno Voipio wrote:
    >> gcc -E ?

    >
    >Nope.
    >
    >That does all sorts of other stuff (expanding #include
    >statements, macro expansion and substitution, etc.). What you
    >get is completely unreadable. I've often wanted to do what the
    >OP describes, but I've never found a tool that does it.


    How do you decide which #if branches are active without knowing what's been
    defined in the previously #included files?

    --
    Alan Curry
    pacman@world.std.com

  5. Re: Seeking preprocessor-type software for stripping unused code

    On 2007-01-13, Alan Curry wrote:
    > In article <12qgh33r1dgjuf5@corp.supernews.com>,
    > Grant Edwards wrote:
    >>On 2007-01-12, Tauno Voipio wrote:
    >>> gcc -E ?

    >>
    >>Nope.
    >>
    >>That does all sorts of other stuff (expanding #include
    >>statements, macro expansion and substitution, etc.). What you
    >>get is completely unreadable. I've often wanted to do what the
    >>OP describes, but I've never found a tool that does it.

    >
    > How do you decide which #if branches are active without
    > knowing what's been defined in the previously #included files?


    You don't. I didn't say I wanted to ignore the files, I just
    don't want to see the results other than the inclusion or
    omission of original lines in the source file.

    --
    Grant Edwards grante Yow! Are you still
    at SEXUALLY ACTIVE? Did you
    visi.com BRING th' REINFORCEMENTS?

  6. Re: Seeking preprocessor-type software for stripping unused code

    In article <12qgla3fbv1oj15@corp.supernews.com>,
    Grant Edwards wrote:
    >On 2007-01-13, Alan Curry wrote:
    >> In article <12qgh33r1dgjuf5@corp.supernews.com>,
    >> How do you decide which #if branches are active without
    >> knowing what's been defined in the previously #included files?

    >
    >You don't. I didn't say I wanted to ignore the files, I just
    >don't want to see the results other than the inclusion or
    >omission of original lines in the source file.


    Seems like it should be possible to parse the cpp output to extract the
    numbers of the lines that made it through, and then print just those lines
    from the main source file. My crude attempt to do that in a perl script
    follows.

    There's also a BSD thing called "unifdef" which I discovered after writing
    the perl script, which does roughly the same job.

    #!/usr/bin/perl -W
    use strict;

    @ARGV or die "Usage: $0 [-D, -I, and other compiler flags] file.c\n";

    my $file=pop @ARGV;

    my $kid=open CPP, '-|';
    if(!defined($kid)) {
    die "pipe/fork failed: $!\n";
    }
    if(!$kid) {
    { exec(($ENV{CC}||'gcc'), qw/-E -dD -C/, @ARGV, $file); }
    die "gcc exec failed: $!\n";
    }

    my $nextline=0;
    my @keeplines=();
    while() {
    if(/^# (\d+) "\Q$file\E"/) {
    $nextline=$1
    } elsif($nextline && !/^# \d+ ["<]/) {
    push @keeplines, $nextline++;
    } elsif($nextline) {
    $nextline=0
    }
    }
    close(CPP) or die "error reading from gcc pipe: $!\n";
    $? and die "gcc exited $?\n";

    "@keeplines" eq "@{[sort { $a <=> $b } @keeplines]}" or die "That's weird";

    open F, '<', $file or die "$file: $!\n";
    $nextline=1;
    while() {
    if($nextline++==$keeplines[0]) {
    print;
    shift @keeplines;
    } else {
    # Kludge to display omitted lines in bold/red on some terminals
    #chomp;
    #print "\c[[31;1m$_\c[[m\n";
    }
    last if !@keeplines;
    }
    close(F);
    @keeplines and die "$file ended unexpectedly\n";

    __END__

    --
    Alan Curry
    pacman@world.std.com

  7. Re: Seeking preprocessor-type software for stripping unused code

    On 2007-01-13, Grant Edwards wrote:
    > On 2007-01-13, Alan Curry wrote:
    >> In article <12qgh33r1dgjuf5@corp.supernews.com>,
    >> Grant Edwards wrote:
    >>>On 2007-01-12, Tauno Voipio wrote:
    >>>> gcc -E ?
    >>>
    >>>Nope.
    >>>
    >>>That does all sorts of other stuff (expanding #include
    >>>statements, macro expansion and substitution, etc.). What you
    >>>get is completely unreadable. I've often wanted to do what the
    >>>OP describes, but I've never found a tool that does it.

    >>
    >> How do you decide which #if branches are active without
    >> knowing what's been defined in the previously #included files?

    >
    > You don't. I didn't say I wanted to ignore the files, I just
    > don't want to see the results other than the inclusion or
    > omission of original lines in the source file.


    I see three options.

    Compile to annotated assembler extract the orignal C from the annotation.
    this'll probably involve some non-trivial scripting.

    Or complle to object with debug symbols and use the line numbers to
    determine which '#if's succeeded.

    get the source to GCC and add this feature (shouldn't be too hard to find
    where to edit)

    using anything other than a c compiler to parse the file will probably bite
    you

    Bye.
    Jasen

  8. Re: Seeking preprocessor-type software for stripping unused code

    "Grant Edwards" wrote in message
    news:12qgh33r1dgjuf5@corp.supernews.com...
    | On 2007-01-12, Tauno Voipio wrote:
    | > Norm Dresner wrote:
    | >> I'm looking for a program which does preprocessor-type processing of
    source
    | >> files to strip out all of the non-operational #if's and to eliminate
    all of
    | >> the operational preprocessor commands too leaving just the code that
    will be
    | >> compiled. I'd like an option to strip comments as well.
    | >
    | > gcc -E ?
    |
    | Nope.
    |
    | That does all sorts of other stuff (expanding #include
    | statements, macro expansion and substitution, etc.). What you
    | get is completely unreadable. I've often wanted to do what the
    | OP describes, but I've never found a tool that does it.
    |

    I'm the OP. About 15-20 years ago while still using MS-DOS, I hacked up the
    source code for a public domain C-preprocessor to do something like this,
    but it was only C, not C++, and I haven't found the source code on any of my
    computers yet.

    If I can find it, I'd be willing to make the code available but I'm not very
    optimistic, which is why I was looking for an existing program.

    Norm


  9. Re: Seeking preprocessor-type software for stripping unused code

    Norm Dresner wrote:
    > If I can find it, I'd be willing to make the code available but I'm not very
    > optimistic, which is why I was looking for an existing program.


    The existing program, which does exactly what you want, has been
    mentioned already. It's called "unifdef".

  10. Re: Seeking preprocessor-type software for stripping unused code

    "Henry Townsend" wrote in message
    news:-PudnX5oSYDfJTTYnZ2dnUVZ_vbinZ2d@comcast.com...
    | Norm Dresner wrote:
    | > If I can find it, I'd be willing to make the code available but I'm not
    very
    | > optimistic, which is why I was looking for an existing program.
    |
    | The existing program, which does exactly what you want, has been
    | mentioned already. It's called "unifdef".

    It's not obvious from the descriptions I've found that it actually processes
    all #include directives as well. Does it?

    Norm


  11. Re: Seeking preprocessor-type software for stripping unused code

    Norm writes:
    > It's not obvious from the descriptions I've found that [unifidef]
    > actually processes all #include directives as well. Does it?


    Here is the man page:

    UNIFDEF(1) BSD General Commands Manual UNIFDEF(1)

    NAME
    unifdef, unifdefall - remove preprocessor conditionals from code

    SYNOPSIS
    unifdef [-ceklst] [-Ipath -Dsym[=val] -Usym -iDsym[=val] -iUsym]
    ... [file] unifdefall [-Ipath] ... file

    DESCRIPTION
    The unifdef utility selectively processes conditional cpp(1) directives.
    It removes from a file both the directives and any additional text
    that they specify should be removed, while otherwise leaving the file
    alone.

    The unifdef utility acts on #if, #ifdef, #ifndef, #elif, #else, and
    #endif lines, and it understands only the commonly-used subset of the
    expression syntax for #if and #elif lines. It handles integer values
    of symbols defined on the command line, the defined() operator applied
    to symbols defined or undefined on the command line, the operators !,
    <, >, <=, >=, ==, !=, &&, ||, and parenthesized expressions. Anything
    that it does not understand is passed through unharmed. It only
    processes #ifdef and #ifndef directives if the symbol is spec- ified
    on the command line, otherwise they are also passed through unchanged.
    By default, it ignores #if and #elif lines with constant expressions,
    or they may be processed by specifying the -k flag on the command
    line.

    The unifdef utility also understands just enough about C to know when
    one of the directives is inactive because it is inside a comment, or
    affected by a backslash-continued line. It spots unusually-formatted
    pre- processor directives and knows when the layout is too odd to
    handle.

    A script called unifdefall can be used to remove all conditional
    cpp(1) directives from a file. It uses unifdef -s and cpp -dM to get
    lists of all the controlling symbols and their definitions (or lack
    thereof), then invokes unifdef with appropriate arguments to process
    the file.

    Available options:

    -Dsym[=val]
    Specify that a symbol is defined, and optionally specify what
    value to give it for the purpose of han- dling #if and #elif
    directives.

    -Usym Specify that a symbol is undefined. If the same symbol appears
    in more than one argument, the last occurrence dominates.

    -c If the -c flag is specified, then the operation of unifdef is
    complemented, i.e., the lines that would have been removed or
    blanked are retained and vice versa.

    -e Because unifdef processes its input one line at a time, it
    cannot remove preprocessor directives that span more than one
    line. The most common example of this is a directive with a
    multi-line comment hanging off its right hand end. By
    default, if unifdef has to process such a directive, it will
    com- plain that the line is too obfuscated. The -e option
    changes the behaviour so that, where possible, such lines are
    left unprocessed instead of reporting an error.

    -k Process #if and #elif lines with constant expressions. By
    default, sections controlled by such lines are passed through
    unchanged because they typically start ``#if 0'' and are used
    as a kind of comment to sketch out future or past development.
    It would be rude to strip them out, just as it would be for
    normal comments.

    -l Replace removed lines with blank lines instead of deleting them.

    -s Instead of processing the input file as usual, this option causes
    unifdef to produce a list of symbols that appear in
    expressions that unifdef understands. It is useful in
    conjunction with the -dM option of cpp(1) for creating unifdef
    command lines.

    -t Disables parsing for C comments and line continuations, which is
    useful for plain text.

    -iDsym[=val]
    -iUsym Ignore #ifdefs. If your C code uses #ifdefs to delimit non-C
    lines, such as comments or code which is under construction,
    then you must tell unifdef which symbols are used for that
    purpose so that it will not try to parse comments and line
    continuations inside those #ifdefs. One specifies ignored
    symbols with -iDsym[=val] and -iUsym similar to -Dsym[=val]
    and -Usym above.

    -Ipath Specifies to unifdefall an additional place to look for
    #include files. This option is ignored by unifdef for
    compatibility with cpp(1) and to simplify the implementation
    of unifdefall.

    The unifdef utility copies its output to stdout and will take its
    input from stdin if no file argument is given.

    The unifdef utility works nicely with the -Dsym option of diff(1).

    SEE ALSO
    cpp(1), diff(1)

    DIAGNOSTICS
    Too many levels of nesting.

    Inappropriate #elif, #else or #endif.

    Obfuscated preprocessor control line.

    Premature EOF (with the line number of the most recent unterminated
    #if).

    EOF in comment.

    The unifdef utility exits 0 if the output is an exact copy of the
    input, 1 if not, and 2 if in trouble.

    BUGS
    Expression evaluation is very limited.

    Preprocessor control lines split across more than one physical line
    (because of comments or backslash-newline) cannot be handled in every
    situation.

    Trigraphs are not recognized.

    There is no support for symbols with different definitions at
    different points in the source file.

    The text-mode and ignore functionality does not correspond to modern
    cpp(1) behaviour.

    HISTORY
    The unifdef command appeared in 4.3BSD. ANSI C support was added
    in FreeBSD 4.7.

    BSD September 24, 2002 BSD

+ Reply to Thread