Background
The -E
option runs the preprocessor, and emits a compilable file with all the usual preprocessor “stuff” all done. Include files have been included, macros expanded, conditionals selected, all that good stuff. It’s quite common to ask someone to provide a preprocessed input file in order to figure out where their problem is. The advantage of a preprocessed file is that it (ought to be) compilable by anyone, without any need to reconstruct the original user’s environment. As long as you have the same compiler version, and the right target, it should all Just Work.
Problem
These files can be huge. If the problem is a compiler crash, or even if it isn’t, pretty much the first step anyone takes is to reduce the test case down to something manageable. Even with automated tools, this can take time. I personally have had to do this for files in the multiple hundreds of thousands of lines.
Observation
A whole lot of those lines are from standard headers. #include <vector>
alone produces almost 25k lines of -E
output in my environment. The standard headers are basically never the source of a compiler crash, and yet we have to contend with reducing them over and over again.
Solution
A new option -fkeep-system-includes
which modifies -E
behavior so that it does not copy any system header files to the output, but instead propagates the #include
directive. That is, if you had something like
// my_header.h
void my_code();
// my_code.cpp
#include "my_header.h"
#include <vector>
void my_code() { ... }
then -E -fkeep-system-includes
would present you with
void my_code();
#include <vector>
void my_code() { ... }
instead of 25k lines of irrelevant STL code wrapped by the relevant bits.
I believe in most cases the result will still be compilable “anywhere” although obviously the more platform-dependent it gets the closer you have to be to the user’s environment. But IME most cases aren’t like that; and if it is, you can always fall back to unmodified -E
.
Patch
To be posted, probably tomorrow. But it’s actually pretty simple, once you get past the overhead of adding a new option.
Credit
@gregb had the idea, and other people on our team thought it would be useful too. I thought I’d propose it upstream because it seems like it should be quite widely useful.