Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Lex/PreprocessorOptions.h
Line
Count
Source
1
//===- PreprocessorOptions.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
#ifndef LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_
10
#define LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_
11
12
#include "clang/Basic/BitmaskEnum.h"
13
#include "clang/Basic/LLVM.h"
14
#include "clang/Lex/PreprocessorExcludedConditionalDirectiveSkipMapping.h"
15
#include "llvm/ADT/StringRef.h"
16
#include "llvm/ADT/StringSet.h"
17
#include <functional>
18
#include <map>
19
#include <memory>
20
#include <set>
21
#include <string>
22
#include <utility>
23
#include <vector>
24
25
namespace llvm {
26
27
class MemoryBuffer;
28
29
} // namespace llvm
30
31
namespace clang {
32
33
/// Enumerate the kinds of standard library that
34
enum ObjCXXARCStandardLibraryKind {
35
  ARCXX_nolib,
36
37
  /// libc++
38
  ARCXX_libcxx,
39
40
  /// libstdc++
41
  ARCXX_libstdcxx
42
};
43
44
/// Whether to disable the normal validation performed on precompiled
45
/// headers and module files when they are loaded.
46
enum class DisableValidationForModuleKind {
47
  /// Perform validation, don't disable it.
48
  None = 0,
49
50
  /// Disable validation for a precompiled header and the modules it depends on.
51
  PCH = 0x1,
52
53
  /// Disable validation for module files.
54
  Module = 0x2,
55
56
  /// Disable validation for all kinds.
57
  All = PCH | Module,
58
59
  LLVM_MARK_AS_BITMASK_ENUM(Module)
60
};
61
62
/// PreprocessorOptions - This class is used for passing the various options
63
/// used in preprocessor initialization to InitializePreprocessor().
64
class PreprocessorOptions {
65
public:
66
  std::vector<std::pair<std::string, bool/*isUndef*/>> Macros;
67
  std::vector<std::string> Includes;
68
  std::vector<std::string> MacroIncludes;
69
70
  /// Initialize the preprocessor with the compiler and target specific
71
  /// predefines.
72
  bool UsePredefines = true;
73
74
  /// Whether we should maintain a detailed record of all macro
75
  /// definitions and expansions.
76
  bool DetailedRecord = false;
77
78
  /// When true, we are creating or using a PCH where a #pragma hdrstop is
79
  /// expected to indicate the beginning or end of the PCH.
80
  bool PCHWithHdrStop = false;
81
82
  /// When true, we are creating a PCH or creating the PCH object while
83
  /// expecting a #pragma hdrstop to separate the two.  Allow for a
84
  /// missing #pragma hdrstop, which generates a PCH for the whole file,
85
  /// and creates an empty PCH object.
86
  bool PCHWithHdrStopCreate = false;
87
88
  /// If non-empty, the filename used in an #include directive in the primary
89
  /// source file (or command-line preinclude) that is used to implement
90
  /// MSVC-style precompiled headers. When creating a PCH, after the #include
91
  /// of this header, the PCH generation stops. When using a PCH, tokens are
92
  /// skipped until after an #include of this header is seen.
93
  std::string PCHThroughHeader;
94
95
  /// The implicit PCH included at the start of the translation unit, or empty.
96
  std::string ImplicitPCHInclude;
97
98
  /// Headers that will be converted to chained PCHs in memory.
99
  std::vector<std::string> ChainedIncludes;
100
101
  /// Whether to disable most of the normal validation performed on
102
  /// precompiled headers and module files.
103
  DisableValidationForModuleKind DisablePCHOrModuleValidation =
104
      DisableValidationForModuleKind::None;
105
106
  /// When true, a PCH with compiler errors will not be rejected.
107
  bool AllowPCHWithCompilerErrors = false;
108
109
  /// When true, a PCH with modules cache path different to the current
110
  /// compilation will not be rejected.
111
  bool AllowPCHWithDifferentModulesCachePath = false;
112
113
  /// Dump declarations that are deserialized from PCH, for testing.
114
  bool DumpDeserializedPCHDecls = false;
115
116
  /// This is a set of names for decls that we do not want to be
117
  /// deserialized, and we emit an error if they are; for testing purposes.
118
  std::set<std::string> DeserializedPCHDeclsToErrorOn;
119
120
  /// If non-zero, the implicit PCH include is actually a precompiled
121
  /// preamble that covers this number of bytes in the main source file.
122
  ///
123
  /// The boolean indicates whether the preamble ends at the start of a new
124
  /// line.
125
  std::pair<unsigned, bool> PrecompiledPreambleBytes;
126
127
  /// True indicates that a preamble is being generated.
128
  ///
129
  /// When the lexer is done, one of the things that need to be preserved is the
130
  /// conditional #if stack, so the ASTWriter/ASTReader can save/restore it when
131
  /// processing the rest of the file. Similarly, we track an unterminated
132
  /// #pragma assume_nonnull.
133
  bool GeneratePreamble = false;
134
135
  /// Whether to write comment locations into the PCH when building it.
136
  /// Reading the comments from the PCH can be a performance hit even if the
137
  /// clients don't use them.
138
  bool WriteCommentListToPCH = true;
139
140
  /// When enabled, preprocessor is in a mode for parsing a single file only.
141
  ///
142
  /// Disables #includes of other files and if there are unresolved identifiers
143
  /// in preprocessor directive conditions it causes all blocks to be parsed so
144
  /// that the client can get the maximum amount of information from the parser.
145
  bool SingleFileParseMode = false;
146
147
  /// When enabled, the preprocessor will construct editor placeholder tokens.
148
  bool LexEditorPlaceholders = true;
149
150
  /// True if the SourceManager should report the original file name for
151
  /// contents of files that were remapped to other files. Defaults to true.
152
  bool RemappedFilesKeepOriginalName = true;
153
154
  /// The set of file remappings, which take existing files on
155
  /// the system (the first part of each pair) and gives them the
156
  /// contents of other files on the system (the second part of each
157
  /// pair).
158
  std::vector<std::pair<std::string, std::string>> RemappedFiles;
159
160
  /// The set of file-to-buffer remappings, which take existing files
161
  /// on the system (the first part of each pair) and gives them the contents
162
  /// of the specified memory buffer (the second part of each pair).
163
  std::vector<std::pair<std::string, llvm::MemoryBuffer *>> RemappedFileBuffers;
164
165
  /// Whether the compiler instance should retain (i.e., not free)
166
  /// the buffers associated with remapped files.
167
  ///
168
  /// This flag defaults to false; it can be set true only through direct
169
  /// manipulation of the compiler invocation object, in cases where the
170
  /// compiler invocation and its buffers will be reused.
171
  bool RetainRemappedFileBuffers = false;
172
173
  /// When enabled, excluded conditional blocks retain in the main file.
174
  bool RetainExcludedConditionalBlocks = false;
175
176
  /// The Objective-C++ ARC standard library that we should support,
177
  /// by providing appropriate definitions to retrofit the standard library
178
  /// with support for lifetime-qualified pointers.
179
  ObjCXXARCStandardLibraryKind ObjCXXARCStandardLibrary = ARCXX_nolib;
180
181
  /// Records the set of modules
182
  class FailedModulesSet {
183
    llvm::StringSet<> Failed;
184
185
  public:
186
880
    bool hasAlreadyFailed(StringRef module) {
187
880
      return Failed.count(module) > 0;
188
880
    }
189
190
43
    void addFailed(StringRef module) {
191
43
      Failed.insert(module);
192
43
    }
193
  };
194
195
  /// The set of modules that failed to build.
196
  ///
197
  /// This pointer will be shared among all of the compiler instances created
198
  /// to (re)build modules, so that once a module fails to build anywhere,
199
  /// other instances will see that the module has failed and won't try to
200
  /// build it again.
201
  std::shared_ptr<FailedModulesSet> FailedModules;
202
203
  /// Contains the currently active skipped range mappings for skipping excluded
204
  /// conditional directives.
205
  ///
206
  /// The pointer is passed to the Preprocessor when it's constructed. The
207
  /// pointer is unowned, the client is responsible for its lifetime.
208
  ExcludedPreprocessorDirectiveSkipMapping
209
      *ExcludedConditionalDirectiveSkipMappings = nullptr;
210
211
  /// Set up preprocessor for RunAnalysis action.
212
  bool SetUpStaticAnalyzer = false;
213
214
  /// Prevents intended crashes when using #pragma clang __debug. For testing.
215
  bool DisablePragmaDebugCrash = false;
216
217
public:
218
198k
  PreprocessorOptions() : PrecompiledPreambleBytes(0, false) {}
219
220
678k
  void addMacroDef(StringRef Name) {
221
678k
    Macros.emplace_back(std::string(Name), false);
222
678k
  }
223
90
  void addMacroUndef(StringRef Name) {
224
90
    Macros.emplace_back(std::string(Name), true);
225
90
  }
226
227
19
  void addRemappedFile(StringRef From, StringRef To) {
228
19
    RemappedFiles.emplace_back(std::string(From), std::string(To));
229
19
  }
230
231
4.10k
  void addRemappedFile(StringRef From, llvm::MemoryBuffer *To) {
232
4.10k
    RemappedFileBuffers.emplace_back(std::string(From), To);
233
4.10k
  }
234
235
1.54k
  void clearRemappedFiles() {
236
1.54k
    RemappedFiles.clear();
237
1.54k
    RemappedFileBuffers.clear();
238
1.54k
  }
239
240
  /// Reset any options that are not considered when building a
241
  /// module.
242
1.87k
  void resetNonModularOptions() {
243
1.87k
    Includes.clear();
244
1.87k
    MacroIncludes.clear();
245
1.87k
    ChainedIncludes.clear();
246
1.87k
    DumpDeserializedPCHDecls = false;
247
1.87k
    ImplicitPCHInclude.clear();
248
1.87k
    SingleFileParseMode = false;
249
1.87k
    LexEditorPlaceholders = true;
250
1.87k
    RetainRemappedFileBuffers = true;
251
1.87k
    PrecompiledPreambleBytes.first = 0;
252
1.87k
    PrecompiledPreambleBytes.second = false;
253
1.87k
    RetainExcludedConditionalBlocks = false;
254
1.87k
  }
255
};
256
257
} // namespace clang
258
259
#endif // LLVM_CLANG_LEX_PREPROCESSOROPTIONS_H_