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