<?xml version="1.0" encoding="windows-1252"?>
<node id="951711" title="Re: C code pre-processor" created="2012-02-03 15:46:00" updated="2012-02-03 15:46:00">
<type id="11">
note</type>
<author id="533863">
roboticus</author>
<data>
<field name="doctext">
&lt;p&gt;[dgsmith_50]:&lt;/p&gt;
&lt;p&gt;The following definitely doesn't do everything you want, as it's a quick knockup.  It'll need some tuning, but I think you can tweak it into something useful for you:&lt;/p&gt;
&lt;c&gt;
$ cat C_preprocessor_light.pl
#!/usr/bin/perl
#
#   see perlmonks 951682
#
use strict;
use warnings;

my %DEF;
my @fl_print = (1);
while (&lt;DATA&gt;) {
    if (/^\s*#\s*define\s+(\w+)/)   { $DEF{$1}=1; }
    elsif (/^\s*#\s*undef\s+(\w+)/) { delete $DEF{$1}; }
    elsif (/^\s*#\s*ifdef\s+(\w+)/) {
        push @fl_print, (exists($DEF{$1}) and $fl_print[-1]);
    }
    elsif (/^\s*#\s*ifndef\s+(\w+)/){
        push @fl_print, (!exists($DEF{$1}) and $fl_print[-1]);
    }
    elsif (/^\s*#\s*endif/){
        pop @fl_print if @fl_print &gt; 1;
    }
    print if ($fl_print[-1]);
}
__DATA__
/* Foo */
#ifndef BLARG2
BAR
#define BLARG
#define GRAK
zippy
#ifdef BLARG
happy
#ifndef GRAK
lucky
#endif
puppy
#endif
#foo
#endif
bloink
&lt;/c&gt;
&lt;p&gt;It doesn't hide exactly the lines I would want to hide, but it's close...&lt;/p&gt;
&lt;p&gt;...[roboticus]&lt;/p&gt;
&lt;p&gt;&lt;i&gt;When your only tool is a hammer, all problems look like your thumb.&lt;/i&gt;&lt;/p&gt;</field>
<field name="root_node">
951682</field>
<field name="parent_node">
951682</field>
</data>
</node>
