-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
16 changed files
with
1,443 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//===- clang/Lex/DependencyDirectivesSourceMinimizer.h - ----------*- C++ -*-// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This is the interface for minimizing 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. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H | ||
#define LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H | ||
|
||
#include "clang/Basic/SourceLocation.h" | ||
#include "llvm/ADT/ArrayRef.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/StringRef.h" | ||
|
||
namespace clang { | ||
|
||
class DiagnosticsEngine; | ||
|
||
namespace minimize_source_to_dependency_directives { | ||
|
||
/// Represents the kind of preprocessor directive or a module declaration that | ||
/// is tracked by the source minimizer in its token output. | ||
enum TokenKind { | ||
pp_none, | ||
pp_include, | ||
pp___include_macros, | ||
pp_define, | ||
pp_undef, | ||
pp_import, | ||
pp_pragma_import, | ||
pp_include_next, | ||
pp_if, | ||
pp_ifdef, | ||
pp_ifndef, | ||
pp_elif, | ||
pp_else, | ||
pp_endif, | ||
decl_at_import, | ||
pp_eof, | ||
}; | ||
|
||
/// Represents a simplified token that's lexed as part of the source | ||
/// minimization. It's used to track the location of various preprocessor | ||
/// directives that could potentially have an effect on the depedencies. | ||
struct Token { | ||
/// The kind of token. | ||
TokenKind K = pp_none; | ||
|
||
/// Offset into the output byte stream of where the directive begins. | ||
int Offset = -1; | ||
|
||
Token(TokenKind K, int Offset) : K(K), Offset(Offset) {} | ||
}; | ||
|
||
} // end namespace minimize_source_to_dependency_directives | ||
|
||
/// Minimize the input down to the preprocessor directives that might have | ||
/// an effect on the dependencies for a compilation unit. | ||
/// | ||
/// This function deletes all non-preprocessor code, and strips anything that | ||
/// can't affect what gets included. It canonicalizes whitespace where | ||
/// convenient to stabilize the output against formatting changes in the input. | ||
/// | ||
/// Clears the output vectors at the beginning of the call. | ||
/// | ||
/// \returns false on success, true on error. If the diagnostic engine is not | ||
/// null, an appropriate error is reported using the given input location | ||
/// with the offset that corresponds to the minimizer's current buffer offset. | ||
bool minimizeSourceToDependencyDirectives( | ||
llvm::StringRef Input, llvm::SmallVectorImpl<char> &Output, | ||
llvm::SmallVectorImpl<minimize_source_to_dependency_directives::Token> | ||
&Tokens, | ||
DiagnosticsEngine *Diags = nullptr, | ||
SourceLocation InputSourceLoc = SourceLocation()); | ||
|
||
} // end namespace clang | ||
|
||
#endif // LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.