title: Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?
body: <h3>Original Question</h3>
  
  <p>What I'd like is not a standard C pre-processor, but a variation on it which would accept from somewhere - probably the command line via -DNAME1 and -UNAME2 options - a specification of which macros are defined, and would then eliminate dead code.</p>
  
  <p>It may be easier to understand what I'm after with some examples:</p>
  
  <pre><code>#ifdef NAME1
  #define ALBUQUERQUE "ambidextrous"
  #else
  #define PHANTASMAGORIA "ghostly"
  #endif
  </code></pre>
  
  <p>If the command were run with '-DNAME1', the output would be:</p>
  
  <pre><code>#define ALBUQUERQUE "ambidextrous"
  </code></pre>
  
  <p>If the command were run with '-UNAME1', the output would be:</p>
  
  <pre><code>#define PHANTASMAGORIA "ghostly"
  </code></pre>
  
  <p>If the command were run with neither option, the output would be the same as the input.</p>
  
  <p>This is a simple case - I'd be hoping that the code could handle more complex cases too.</p>
  
  <p>To illustrate with a real-world but still simple example:</p>
  
  <pre><code>#ifdef USE_VOID
  #ifdef PLATFORM1
  #define VOID void
  #else
  #undef VOID
  typedef void    VOID;
  #endif /* PLATFORM1 */
  typedef void *  VOIDPTR;
  #else
  typedef mint     VOID;
  typedef char *  VOIDPTR;
  #endif /* USE_VOID */
  </code></pre>
  
  <p>I'd like to run the command with <code>-DUSE_VOID -UPLATFORM1</code> and get the output:</p>
  
  <pre><code>#undef VOID
  typedef void    VOID;
  typedef void *  VOIDPTR;
  </code></pre>
  
  <p>Another example:</p>
  
  <pre><code>#ifndef DOUBLEPAD
  #if (defined NT) || (defined OLDUNIX)
  #define DOUBLEPAD 8
  #else
  #define DOUBLEPAD 0
  #endif /* NT */
  #endif /* !DOUBLEPAD */
  </code></pre>
  
  <p>Ideally, I'd like to run with <code>-UOLDUNIX</code> and get the output:</p>
  
  <pre><code>#ifndef DOUBLEPAD
  #if (defined NT)
  #define DOUBLEPAD 8
  #else
  #define DOUBLEPAD 0
  #endif /* NT */
  #endif /* !DOUBLEPAD */
  </code></pre>
  
  <p>This may be pushing my luck!</p>
  
  <p>Motivation: large, ancient code base with lots of conditional code.  Many of the conditions no longer apply - the OLDUNIX platform, for example, is no longer made and no longer supported, so there is no need to have references to it in the code.  Other conditions are always true.  For example, features are added with conditional compilation so that a single version of the code can be used for both older versions of the software where the feature is not available and newer versions where it is available (more or less).  Eventually, the old versions without the feature are no longer supported - everything uses the feature - so the condition on whether the feature is present or not should be removed, and the 'when feature is absent' code should be removed too.  I'd like to have a tool to do the job automatically because it will be faster and more reliable than doing it manually (which is rather critical when the code base includes 21,500 source files).</p>
  
  <p>(A really clever version of the tool might read <code>#include</code>'d files to determine whether the control macros - those specified by -D or -U on the command line - are defined in those files.  I'm not sure whether that's truly helpful except as a backup diagnostic.  Whatever else it does, though, the pseudo-pre-processor must not expand macros or include files verbatim.  The output must be source similar to, but usually simpler than, the input code.)</p>
  
  <h3>Status Report (one year later)</h3>
  
  <p>After a year of use, I am very happy with '<a href="https://sourceforge.net/projects/sunifdef/" rel="noreferrer">sunifdef</a>' recommended by the selected answer.  It hasn't made a mistake yet, and I don't expect it to.  The only quibble I have with it is stylistic.  Given an input such as:</p>
  
  <pre><code>#if (defined(A) &amp;&amp; defined(B)) || defined(C) || (defined(D) &amp;&amp; defined(E))
  </code></pre>
  
  <p>and run with '-UC' (C is never defined), the output is:</p>
  
  <pre><code>#if defined(A) &amp;&amp; defined(B) || defined(D) &amp;&amp; defined(E)
  </code></pre>
  
  <p>This is technically correct because '&amp;&amp;' binds tighter than '||', but it is an open invitation to confusion.  I would much prefer it to include parentheses around the sets of '&amp;&amp;' conditions, as in the original:</p>
  
  <pre><code>#if (defined(A) &amp;&amp; defined(B)) || (defined(D) &amp;&amp; defined(E))
  </code></pre>
  
  <p>However, given the obscurity of some of the code I have to work with, for that to be the biggest nit-pick is a strong compliment; it is valuable tool to me.</p>
  
  <hr>
  
  <h3>The New Kid on the Block</h3>
  
  <p>Having checked the URL for inclusion in the information above, I see that (as predicted) there is an new program called <a href="http://coan2.sourceforge.net/" rel="noreferrer">Coan</a> that is the successor to 'sunifdef'.  It is available on SourceForge and has been since January 2010.  I'll be checking it out...further reports later this year, or maybe next year, or sometime, or never.</p>