This patch introduces a dependency directives source minimizer to clang that minimizes header and source files to the minimum necessary preprocessor directives for evaluating includes. It reduces the source down to #define, #include, #import, @import, and any conditional preprocessor logic that contains one of those.
The source minimizer works by lexing the input with a custom fast lexer that recognizes the preprocessor directives it cares about, and emitting those directives in the minimized source. It ignores source code, comments, and normalizes whitespace. It gives up and fails if seems any directives that it doesn't recognize as valid (e.g. #define 0).
In addition to the source minimizer this patch adds a print-dependency-directives-minimized-source CC1 option that allows you to invoke the minimizer using clang directly.
There a couple of known issues with the source minimizer:
- It fails to detect @import that was formed in a macro expansion. We are planning to add a warning to discourage this use.
- It fails to detect _Pragma ("clang import"). We are planning to probably add a warning to discourage this use.
- It assumes raw string literals are valid when minimizing source without respecting language mode.
This is based on code that was included in the original WIP patch I posted before the dev meeting: https://reviews.llvm.org/D53354 . It's based on the original filter-to-includes code written by @dexonsmith . We are planning to use to implement fast dependency scanning for explicit module builds.