Add clang source minimizer that reduces source to directives
authorAlex Lorenz <arphaman@gmail.com>
Mon, 3 Jun 2019 22:59:17 +0000 (22:59 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Mon, 3 Jun 2019 22:59:17 +0000 (22:59 +0000)
commite92b41ea8c68738b6362a31ad98cd1d8894266fb
tree1a5fa409ea7a7ac1b3b123a2f521121e3341e031
parent3fb6bc0a3f0a0fbbe02db23219d5abd5a4af6766
Add clang source minimizer that reduces source to directives
that might affect the dependency list for a compilation

This commit 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,

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
from clang directly.

Differential Revision: https://reviews.llvm.org/D55463

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@362459 91177308-0d34-0410-b5e6-96231b3b80d8
16 files changed:
include/clang/Basic/DiagnosticLexKinds.td
include/clang/Driver/CC1Options.td
include/clang/Frontend/FrontendActions.h
include/clang/Frontend/FrontendOptions.h
include/clang/Lex/DependencyDirectivesSourceMinimizer.h [new file with mode: 0644]
lib/Frontend/CompilerInvocation.cpp
lib/Frontend/FrontendActions.cpp
lib/FrontendTool/ExecuteCompilerInvocation.cpp
lib/Lex/CMakeLists.txt
lib/Lex/DependencyDirectivesSourceMinimizer.cpp [new file with mode: 0644]
test/Frontend/minimize_source_to_dependency_directives.c [new file with mode: 0644]
test/Lexer/minimize_source_to_dependency_directives_at_import_extra_tokens.m [new file with mode: 0644]
test/Lexer/minimize_source_to_dependency_directives_at_import_missing_semi.m [new file with mode: 0644]
test/Lexer/minimize_source_to_dependency_directives_invalid_macro_name.c [new file with mode: 0644]
unittests/Lex/CMakeLists.txt
unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp [new file with mode: 0644]