blob: d832df6b6146bdca52b662f5aa81d65a3d8df0b0 [file] [log] [blame]
Andrew Walbran3d2c1972020-04-07 12:24:26 +01001//===- clang/Lex/DependencyDirectivesSourceMinimizer.h - ----------*- C++ -*-//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This is the interface for minimizing header and source files to the
11/// minimum necessary preprocessor directives for evaluating includes. It
12/// reduces the source down to #define, #include, #import, @import, and any
13/// conditional preprocessor logic that contains one of those.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H
18#define LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H
19
20#include "clang/Basic/SourceLocation.h"
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24
25namespace clang {
26
27class DiagnosticsEngine;
28
29namespace minimize_source_to_dependency_directives {
30
31/// Represents the kind of preprocessor directive or a module declaration that
32/// is tracked by the source minimizer in its token output.
33enum TokenKind {
34 pp_none,
35 pp_include,
36 pp___include_macros,
37 pp_define,
38 pp_undef,
39 pp_import,
40 pp_pragma_import,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041 pp_pragma_once,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010042 pp_include_next,
43 pp_if,
44 pp_ifdef,
45 pp_ifndef,
46 pp_elif,
47 pp_else,
48 pp_endif,
49 decl_at_import,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020050 cxx_export_decl,
51 cxx_module_decl,
52 cxx_import_decl,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010053 pp_eof,
54};
55
56/// Represents a simplified token that's lexed as part of the source
57/// minimization. It's used to track the location of various preprocessor
58/// directives that could potentially have an effect on the depedencies.
59struct Token {
60 /// The kind of token.
61 TokenKind K = pp_none;
62
63 /// Offset into the output byte stream of where the directive begins.
64 int Offset = -1;
65
66 Token(TokenKind K, int Offset) : K(K), Offset(Offset) {}
67};
68
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069/// Simplified token range to track the range of a potentially skippable PP
70/// directive.
71struct SkippedRange {
72 /// Offset into the output byte stream of where the skipped directive begins.
73 int Offset;
74
75 /// The number of bytes that can be skipped before the preprocessing must
76 /// resume.
77 int Length;
78};
79
80/// Computes the potential source ranges that can be skipped by the preprocessor
81/// when skipping a directive like #if, #ifdef or #elsif.
82///
83/// \returns false on success, true on error.
84bool computeSkippedRanges(ArrayRef<Token> Input,
85 llvm::SmallVectorImpl<SkippedRange> &Range);
86
Andrew Walbran3d2c1972020-04-07 12:24:26 +010087} // end namespace minimize_source_to_dependency_directives
88
89/// Minimize the input down to the preprocessor directives that might have
90/// an effect on the dependencies for a compilation unit.
91///
92/// This function deletes all non-preprocessor code, and strips anything that
93/// can't affect what gets included. It canonicalizes whitespace where
94/// convenient to stabilize the output against formatting changes in the input.
95///
96/// Clears the output vectors at the beginning of the call.
97///
98/// \returns false on success, true on error. If the diagnostic engine is not
99/// null, an appropriate error is reported using the given input location
100/// with the offset that corresponds to the minimizer's current buffer offset.
101bool minimizeSourceToDependencyDirectives(
102 llvm::StringRef Input, llvm::SmallVectorImpl<char> &Output,
103 llvm::SmallVectorImpl<minimize_source_to_dependency_directives::Token>
104 &Tokens,
105 DiagnosticsEngine *Diags = nullptr,
106 SourceLocation InputSourceLoc = SourceLocation());
107
108} // end namespace clang
109
110#endif // LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H