title: Is there a way to insert a step between pre-processing and compilation for GCC or cl.exe?
body: <p>I am trying to instrument a big C code base. The code base can be built with GCC and MS cl.exe. The code base contains millions of lines. I am trying to instrument it for code coverage. Because the runtime environment is special, I have to do the instrumentation in a special way.</p>
  
  <p>I have written a transformation tool that can do the instrumentation. But it cannot handle the macro expansion, header file includes, etc. In other words, it must work <strong>after the pre-processing stage</strong>. </p>
  
  <p>I may not have enough time to write a C pre-processor. Since the code base is built with GCC/cl.exe, I am wondering if it is possible to inject my transformation step into the GCC or cl.exe compilation process. Like this:</p>
  
  <pre><code>GCC/cl.exe pre-process  -&gt;  (My transformation) -&gt; GCC/cl.exe compilation
  </code></pre>
  
  <p>Is that possible?</p>
  
  <h2>ADD 1</h2>
  
  <p>So far, all the answers revolve around the GCC. How about the Microsoft cl.exe? I tried the <a href="https://msdn.microsoft.com/en-us/library/8z9z0bx6.aspx" rel="nofollow noreferrer">/P</a> option which will emit the preprocess result to a file. But the result contains many lines like below:</p>
  
  <pre><code>#line 1306 "&lt;some file path&gt;"
  </code></pre>
  
  <p>I am trying to resolve it.</p>
  
  <p>OK, I solved it,. Specify both <code>/P</code> and <code>/EP</code> can suppress the <code>#line</code> directive.</p>
  
  <h2>ADD 2</h2>
  
  <p>The output of specifying both <code>"/P /EP"</code> for <code>cl.exe</code> is a <code>*.i</code> file without <code>#line</code> directives. It's a valid C source file. So it can be <strong>directly</strong> fed into the <code>cl.exe</code>. I just rename the original C file and use the <code>*.i</code> file for instrumentation and then the build process. </p>
  
  <p>(Notice to avoid some header file included through <code>/FI</code>. That can cause some duplicated definition error. Should remove them because their content are already included in the <code>*.i</code> file.)</p>
  
  <h2>ADD 3</h2>
  
  <p>I can just use the <code>/P</code> switch. The <code>#line</code> directive doesn't jeopardize the compilation and can be recognized by a C parser. And without such info, it's difficult to back-track from the instrumented code to the original <code>c</code> source code, as pointed out by Jonathan Leffler.</p>
  
  <h2>ADD 4</h2>
  
  <p>The instrumentation is not easy. For example, the block separation for block-based code coverage is tricky according to <a href="https://blogs.msdn.microsoft.com/phuene/2007/05/03/code-coverage-instrumentation/" rel="nofollow noreferrer">here</a> (note the block 4).</p>