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)
commit6e2d36b60b401a0fd5a25f8eb98cddfd3a7b92b4
tree852ca7da741315e7c7fa68bd08b31d81ab7bb015
parent1f8030630be6c5b75c4c2a1edf7658472ff9c0c1
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

llvm-svn: 362459
16 files changed:
clang/include/clang/Basic/DiagnosticLexKinds.td
clang/include/clang/Driver/CC1Options.td
clang/include/clang/Frontend/FrontendActions.h
clang/include/clang/Frontend/FrontendOptions.h
clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h [new file with mode: 0644]
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Frontend/FrontendActions.cpp
clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
clang/lib/Lex/CMakeLists.txt
clang/lib/Lex/DependencyDirectivesSourceMinimizer.cpp [new file with mode: 0644]
clang/test/Frontend/minimize_source_to_dependency_directives.c [new file with mode: 0644]
clang/test/Lexer/minimize_source_to_dependency_directives_at_import_extra_tokens.m [new file with mode: 0644]
clang/test/Lexer/minimize_source_to_dependency_directives_at_import_missing_semi.m [new file with mode: 0644]
clang/test/Lexer/minimize_source_to_dependency_directives_invalid_macro_name.c [new file with mode: 0644]
clang/unittests/Lex/CMakeLists.txt
clang/unittests/Lex/DependencyDirectivesSourceMinimizerTest.cpp [new file with mode: 0644]