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