1351280Sdim//===- clang/Lex/DependencyDirectivesSourceMinimizer.h -  ----------*- C++ -*-//
2351280Sdim//
3351280Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4351280Sdim// See https://llvm.org/LICENSE.txt for license information.
5351280Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6351280Sdim//
7351280Sdim//===----------------------------------------------------------------------===//
8351280Sdim///
9351280Sdim/// \file
10351280Sdim/// This is the interface for minimizing header and source files to the
11351280Sdim/// minimum necessary preprocessor directives for evaluating includes. It
12351280Sdim/// reduces the source down to #define, #include, #import, @import, and any
13351280Sdim/// conditional preprocessor logic that contains one of those.
14351280Sdim///
15351280Sdim//===----------------------------------------------------------------------===//
16351280Sdim
17351280Sdim#ifndef LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H
18351280Sdim#define LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H
19351280Sdim
20351280Sdim#include "clang/Basic/SourceLocation.h"
21351280Sdim#include "llvm/ADT/ArrayRef.h"
22351280Sdim#include "llvm/ADT/SmallVector.h"
23351280Sdim#include "llvm/ADT/StringRef.h"
24351280Sdim
25351280Sdimnamespace clang {
26351280Sdim
27351280Sdimclass DiagnosticsEngine;
28351280Sdim
29351280Sdimnamespace minimize_source_to_dependency_directives {
30351280Sdim
31351280Sdim/// Represents the kind of preprocessor directive or a module declaration that
32351280Sdim/// is tracked by the source minimizer in its token output.
33351280Sdimenum TokenKind {
34351280Sdim  pp_none,
35351280Sdim  pp_include,
36351280Sdim  pp___include_macros,
37351280Sdim  pp_define,
38351280Sdim  pp_undef,
39351280Sdim  pp_import,
40351280Sdim  pp_pragma_import,
41360784Sdim  pp_pragma_once,
42351280Sdim  pp_include_next,
43351280Sdim  pp_if,
44351280Sdim  pp_ifdef,
45351280Sdim  pp_ifndef,
46351280Sdim  pp_elif,
47351280Sdim  pp_else,
48351280Sdim  pp_endif,
49351280Sdim  decl_at_import,
50360784Sdim  cxx_export_decl,
51360784Sdim  cxx_module_decl,
52360784Sdim  cxx_import_decl,
53351280Sdim  pp_eof,
54351280Sdim};
55351280Sdim
56351280Sdim/// Represents a simplified token that's lexed as part of the source
57351280Sdim/// minimization. It's used to track the location of various preprocessor
58351280Sdim/// directives that could potentially have an effect on the depedencies.
59351280Sdimstruct Token {
60351280Sdim  /// The kind of token.
61351280Sdim  TokenKind K = pp_none;
62351280Sdim
63351280Sdim  /// Offset into the output byte stream of where the directive begins.
64351280Sdim  int Offset = -1;
65351280Sdim
66351280Sdim  Token(TokenKind K, int Offset) : K(K), Offset(Offset) {}
67351280Sdim};
68351280Sdim
69360784Sdim/// Simplified token range to track the range of a potentially skippable PP
70360784Sdim/// directive.
71360784Sdimstruct SkippedRange {
72360784Sdim  /// Offset into the output byte stream of where the skipped directive begins.
73360784Sdim  int Offset;
74360784Sdim
75360784Sdim  /// The number of bytes that can be skipped before the preprocessing must
76360784Sdim  /// resume.
77360784Sdim  int Length;
78360784Sdim};
79360784Sdim
80360784Sdim/// Computes the potential source ranges that can be skipped by the preprocessor
81360784Sdim/// when skipping a directive like #if, #ifdef or #elsif.
82360784Sdim///
83360784Sdim/// \returns false on success, true on error.
84360784Sdimbool computeSkippedRanges(ArrayRef<Token> Input,
85360784Sdim                          llvm::SmallVectorImpl<SkippedRange> &Range);
86360784Sdim
87351280Sdim} // end namespace minimize_source_to_dependency_directives
88351280Sdim
89351280Sdim/// Minimize the input down to the preprocessor directives that might have
90351280Sdim/// an effect on the dependencies for a compilation unit.
91351280Sdim///
92351280Sdim/// This function deletes all non-preprocessor code, and strips anything that
93351280Sdim/// can't affect what gets included. It canonicalizes whitespace where
94351280Sdim/// convenient to stabilize the output against formatting changes in the input.
95351280Sdim///
96351280Sdim/// Clears the output vectors at the beginning of the call.
97351280Sdim///
98351280Sdim/// \returns false on success, true on error. If the diagnostic engine is not
99351280Sdim/// null, an appropriate error is reported using the given input location
100351280Sdim/// with the offset that corresponds to the minimizer's current buffer offset.
101351280Sdimbool minimizeSourceToDependencyDirectives(
102351280Sdim    llvm::StringRef Input, llvm::SmallVectorImpl<char> &Output,
103351280Sdim    llvm::SmallVectorImpl<minimize_source_to_dependency_directives::Token>
104351280Sdim        &Tokens,
105351280Sdim    DiagnosticsEngine *Diags = nullptr,
106351280Sdim    SourceLocation InputSourceLoc = SourceLocation());
107351280Sdim
108351280Sdim} // end namespace clang
109351280Sdim
110351280Sdim#endif // LLVM_CLANG_LEX_DEPENDENCY_DIRECTIVES_SOURCE_MINIMIZER_H
111