1//===- Preprocessor.h - C Language Family Preprocessor ----------*- 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/// Defines the clang::Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
15#define LLVM_CLANG_LEX_PREPROCESSOR_H
16
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/DiagnosticIDs.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/LLVM.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/Module.h"
23#include "clang/Basic/SourceLocation.h"
24#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/TokenKinds.h"
26#include "clang/Lex/HeaderSearch.h"
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/MacroInfo.h"
29#include "clang/Lex/ModuleLoader.h"
30#include "clang/Lex/ModuleMap.h"
31#include "clang/Lex/PPCallbacks.h"
32#include "clang/Lex/Token.h"
33#include "clang/Lex/TokenLexer.h"
34#include "llvm/ADT/ArrayRef.h"
35#include "llvm/ADT/DenseMap.h"
36#include "llvm/ADT/FoldingSet.h"
37#include "llvm/ADT/FunctionExtras.h"
38#include "llvm/ADT/PointerUnion.h"
39#include "llvm/ADT/STLExtras.h"
40#include "llvm/ADT/SmallPtrSet.h"
41#include "llvm/ADT/SmallVector.h"
42#include "llvm/ADT/StringRef.h"
43#include "llvm/ADT/TinyPtrVector.h"
44#include "llvm/ADT/iterator_range.h"
45#include "llvm/Support/Allocator.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/Registry.h"
48#include <cassert>
49#include <cstddef>
50#include <cstdint>
51#include <map>
52#include <memory>
53#include <optional>
54#include <string>
55#include <utility>
56#include <vector>
57
58namespace llvm {
59
60template<unsigned InternalLen> class SmallString;
61
62} // namespace llvm
63
64namespace clang {
65
66class CodeCompletionHandler;
67class CommentHandler;
68class DirectoryEntry;
69class EmptylineHandler;
70class ExternalPreprocessorSource;
71class FileEntry;
72class FileManager;
73class HeaderSearch;
74class MacroArgs;
75class PragmaHandler;
76class PragmaNamespace;
77class PreprocessingRecord;
78class PreprocessorLexer;
79class PreprocessorOptions;
80class ScratchBuffer;
81class TargetInfo;
82
83namespace Builtin {
84class Context;
85}
86
87/// Stores token information for comparing actual tokens with
88/// predefined values. Only handles simple tokens and identifiers.
89class TokenValue {
90 tok::TokenKind Kind;
91 IdentifierInfo *II;
92
93public:
94 TokenValue(tok::TokenKind Kind) : Kind(Kind), II(nullptr) {
95 assert(Kind != tok::raw_identifier && "Raw identifiers are not supported.");
96 assert(Kind != tok::identifier &&
97 "Identifiers should be created by TokenValue(IdentifierInfo *)");
98 assert(!tok::isLiteral(Kind) && "Literals are not supported.");
99 assert(!tok::isAnnotation(Kind) && "Annotations are not supported.");
100 }
101
102 TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
103
104 bool operator==(const Token &Tok) const {
105 return Tok.getKind() == Kind &&
106 (!II || II == Tok.getIdentifierInfo());
107 }
108};
109
110/// Context in which macro name is used.
111enum MacroUse {
112 // other than #define or #undef
113 MU_Other = 0,
114
115 // macro name specified in #define
116 MU_Define = 1,
117
118 // macro name specified in #undef
119 MU_Undef = 2
120};
121
122/// Engages in a tight little dance with the lexer to efficiently
123/// preprocess tokens.
124///
125/// Lexers know only about tokens within a single source file, and don't
126/// know anything about preprocessor-level issues like the \#include stack,
127/// token expansion, etc.
128class Preprocessor {
129 friend class VAOptDefinitionContext;
130 friend class VariadicMacroScopeGuard;
131
132 llvm::unique_function<void(const clang::Token &)> OnToken;
133 std::shared_ptr<PreprocessorOptions> PPOpts;
134 DiagnosticsEngine *Diags;
135 const LangOptions &LangOpts;
136 const TargetInfo *Target = nullptr;
137 const TargetInfo *AuxTarget = nullptr;
138 FileManager &FileMgr;
139 SourceManager &SourceMgr;
140 std::unique_ptr<ScratchBuffer> ScratchBuf;
141 HeaderSearch &HeaderInfo;
142 ModuleLoader &TheModuleLoader;
143
144 /// External source of macros.
145 ExternalPreprocessorSource *ExternalSource;
146
147 /// A BumpPtrAllocator object used to quickly allocate and release
148 /// objects internal to the Preprocessor.
149 llvm::BumpPtrAllocator BP;
150
151 /// Identifiers for builtin macros and other builtins.
152 IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__
153 IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__
154 IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__
155 IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__
156 IdentifierInfo *Ident__FILE_NAME__; // __FILE_NAME__
157 IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__
158 IdentifierInfo *Ident__COUNTER__; // __COUNTER__
159 IdentifierInfo *Ident_Pragma, *Ident__pragma; // _Pragma, __pragma
160 IdentifierInfo *Ident__identifier; // __identifier
161 IdentifierInfo *Ident__VA_ARGS__; // __VA_ARGS__
162 IdentifierInfo *Ident__VA_OPT__; // __VA_OPT__
163 IdentifierInfo *Ident__has_feature; // __has_feature
164 IdentifierInfo *Ident__has_extension; // __has_extension
165 IdentifierInfo *Ident__has_builtin; // __has_builtin
166 IdentifierInfo *Ident__has_constexpr_builtin; // __has_constexpr_builtin
167 IdentifierInfo *Ident__has_attribute; // __has_attribute
168 IdentifierInfo *Ident__has_include; // __has_include
169 IdentifierInfo *Ident__has_include_next; // __has_include_next
170 IdentifierInfo *Ident__has_warning; // __has_warning
171 IdentifierInfo *Ident__is_identifier; // __is_identifier
172 IdentifierInfo *Ident__building_module; // __building_module
173 IdentifierInfo *Ident__MODULE__; // __MODULE__
174 IdentifierInfo *Ident__has_cpp_attribute; // __has_cpp_attribute
175 IdentifierInfo *Ident__has_c_attribute; // __has_c_attribute
176 IdentifierInfo *Ident__has_declspec; // __has_declspec_attribute
177 IdentifierInfo *Ident__is_target_arch; // __is_target_arch
178 IdentifierInfo *Ident__is_target_vendor; // __is_target_vendor
179 IdentifierInfo *Ident__is_target_os; // __is_target_os
180 IdentifierInfo *Ident__is_target_environment; // __is_target_environment
181 IdentifierInfo *Ident__is_target_variant_os;
182 IdentifierInfo *Ident__is_target_variant_environment;
183 IdentifierInfo *Ident__FLT_EVAL_METHOD__; // __FLT_EVAL_METHOD
184
185 // Weak, only valid (and set) while InMacroArgs is true.
186 Token* ArgMacro;
187
188 SourceLocation DATELoc, TIMELoc;
189
190 // FEM_UnsetOnCommandLine means that an explicit evaluation method was
191 // not specified on the command line. The target is queried to set the
192 // default evaluation method.
193 LangOptions::FPEvalMethodKind CurrentFPEvalMethod =
194 LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
195
196 // The most recent pragma location where the floating point evaluation
197 // method was modified. This is used to determine whether the
198 // 'pragma clang fp eval_method' was used whithin the current scope.
199 SourceLocation LastFPEvalPragmaLocation;
200
201 LangOptions::FPEvalMethodKind TUFPEvalMethod =
202 LangOptions::FPEvalMethodKind::FEM_UnsetOnCommandLine;
203
204 // Next __COUNTER__ value, starts at 0.
205 unsigned CounterValue = 0;
206
207 enum {
208 /// Maximum depth of \#includes.
209 MaxAllowedIncludeStackDepth = 200
210 };
211
212 // State that is set before the preprocessor begins.
213 bool KeepComments : 1;
214 bool KeepMacroComments : 1;
215 bool SuppressIncludeNotFoundError : 1;
216
217 // State that changes while the preprocessor runs:
218 bool InMacroArgs : 1; // True if parsing fn macro invocation args.
219
220 /// Whether the preprocessor owns the header search object.
221 bool OwnsHeaderSearch : 1;
222
223 /// True if macro expansion is disabled.
224 bool DisableMacroExpansion : 1;
225
226 /// Temporarily disables DisableMacroExpansion (i.e. enables expansion)
227 /// when parsing preprocessor directives.
228 bool MacroExpansionInDirectivesOverride : 1;
229
230 class ResetMacroExpansionHelper;
231
232 /// Whether we have already loaded macros from the external source.
233 mutable bool ReadMacrosFromExternalSource : 1;
234
235 /// True if pragmas are enabled.
236 bool PragmasEnabled : 1;
237
238 /// True if the current build action is a preprocessing action.
239 bool PreprocessedOutput : 1;
240
241 /// True if we are currently preprocessing a #if or #elif directive
242 bool ParsingIfOrElifDirective;
243
244 /// True if we are pre-expanding macro arguments.
245 bool InMacroArgPreExpansion;
246
247 /// Mapping/lookup information for all identifiers in
248 /// the program, including program keywords.
249 mutable IdentifierTable Identifiers;
250
251 /// This table contains all the selectors in the program.
252 ///
253 /// Unlike IdentifierTable above, this table *isn't* populated by the
254 /// preprocessor. It is declared/expanded here because its role/lifetime is
255 /// conceptually similar to the IdentifierTable. In addition, the current
256 /// control flow (in clang::ParseAST()), make it convenient to put here.
257 ///
258 /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
259 /// the lifetime of the preprocessor.
260 SelectorTable Selectors;
261
262 /// Information about builtins.
263 std::unique_ptr<Builtin::Context> BuiltinInfo;
264
265 /// Tracks all of the pragmas that the client registered
266 /// with this preprocessor.
267 std::unique_ptr<PragmaNamespace> PragmaHandlers;
268
269 /// Pragma handlers of the original source is stored here during the
270 /// parsing of a model file.
271 std::unique_ptr<PragmaNamespace> PragmaHandlersBackup;
272
273 /// Tracks all of the comment handlers that the client registered
274 /// with this preprocessor.
275 std::vector<CommentHandler *> CommentHandlers;
276
277 /// Empty line handler.
278 EmptylineHandler *Emptyline = nullptr;
279
280 /// True to avoid tearing down the lexer etc on EOF
281 bool IncrementalProcessing = false;
282
283public:
284 /// The kind of translation unit we are processing.
285 const TranslationUnitKind TUKind;
286
287 /// Returns a pointer into the given file's buffer that's guaranteed
288 /// to be between tokens. The returned pointer is always before \p Start.
289 /// The maximum distance betweenthe returned pointer and \p Start is
290 /// limited by a constant value, but also an implementation detail.
291 /// If no such check point exists, \c nullptr is returned.
292 const char *getCheckPoint(FileID FID, const char *Start) const;
293
294private:
295 /// The code-completion handler.
296 CodeCompletionHandler *CodeComplete = nullptr;
297
298 /// The file that we're performing code-completion for, if any.
299 const FileEntry *CodeCompletionFile = nullptr;
300
301 /// The offset in file for the code-completion point.
302 unsigned CodeCompletionOffset = 0;
303
304 /// The location for the code-completion point. This gets instantiated
305 /// when the CodeCompletionFile gets \#include'ed for preprocessing.
306 SourceLocation CodeCompletionLoc;
307
308 /// The start location for the file of the code-completion point.
309 ///
310 /// This gets instantiated when the CodeCompletionFile gets \#include'ed
311 /// for preprocessing.
312 SourceLocation CodeCompletionFileLoc;
313
314 /// The source location of the \c import contextual keyword we just
315 /// lexed, if any.
316 SourceLocation ModuleImportLoc;
317
318 /// The import path for named module that we're currently processing.
319 SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> NamedModuleImportPath;
320
321 llvm::DenseMap<FileID, SmallVector<const char *>> CheckPoints;
322 unsigned CheckPointCounter = 0;
323
324 /// Whether the import is an `@import` or a standard c++ modules import.
325 bool IsAtImport = false;
326
327 /// Whether the last token we lexed was an '@'.
328 bool LastTokenWasAt = false;
329
330 /// A position within a C++20 import-seq.
331 class StdCXXImportSeq {
332 public:
333 enum State : int {
334 // Positive values represent a number of unclosed brackets.
335 AtTopLevel = 0,
336 AfterTopLevelTokenSeq = -1,
337 AfterExport = -2,
338 AfterImportSeq = -3,
339 };
340
341 StdCXXImportSeq(State S) : S(S) {}
342
343 /// Saw any kind of open bracket.
344 void handleOpenBracket() {
345 S = static_cast<State>(std::max<int>(a: S, b: 0) + 1);
346 }
347 /// Saw any kind of close bracket other than '}'.
348 void handleCloseBracket() {
349 S = static_cast<State>(std::max<int>(a: S, b: 1) - 1);
350 }
351 /// Saw a close brace.
352 void handleCloseBrace() {
353 handleCloseBracket();
354 if (S == AtTopLevel && !AfterHeaderName)
355 S = AfterTopLevelTokenSeq;
356 }
357 /// Saw a semicolon.
358 void handleSemi() {
359 if (atTopLevel()) {
360 S = AfterTopLevelTokenSeq;
361 AfterHeaderName = false;
362 }
363 }
364
365 /// Saw an 'export' identifier.
366 void handleExport() {
367 if (S == AfterTopLevelTokenSeq)
368 S = AfterExport;
369 else if (S <= 0)
370 S = AtTopLevel;
371 }
372 /// Saw an 'import' identifier.
373 void handleImport() {
374 if (S == AfterTopLevelTokenSeq || S == AfterExport)
375 S = AfterImportSeq;
376 else if (S <= 0)
377 S = AtTopLevel;
378 }
379
380 /// Saw a 'header-name' token; do not recognize any more 'import' tokens
381 /// until we reach a top-level semicolon.
382 void handleHeaderName() {
383 if (S == AfterImportSeq)
384 AfterHeaderName = true;
385 handleMisc();
386 }
387
388 /// Saw any other token.
389 void handleMisc() {
390 if (S <= 0)
391 S = AtTopLevel;
392 }
393
394 bool atTopLevel() { return S <= 0; }
395 bool afterImportSeq() { return S == AfterImportSeq; }
396 bool afterTopLevelSeq() { return S == AfterTopLevelTokenSeq; }
397
398 private:
399 State S;
400 /// Whether we're in the pp-import-suffix following the header-name in a
401 /// pp-import. If so, a close-brace is not sufficient to end the
402 /// top-level-token-seq of an import-seq.
403 bool AfterHeaderName = false;
404 };
405
406 /// Our current position within a C++20 import-seq.
407 StdCXXImportSeq StdCXXImportSeqState = StdCXXImportSeq::AfterTopLevelTokenSeq;
408
409 /// Track whether we are in a Global Module Fragment
410 class TrackGMF {
411 public:
412 enum GMFState : int {
413 GMFActive = 1,
414 MaybeGMF = 0,
415 BeforeGMFIntroducer = -1,
416 GMFAbsentOrEnded = -2,
417 };
418
419 TrackGMF(GMFState S) : S(S) {}
420
421 /// Saw a semicolon.
422 void handleSemi() {
423 // If it is immediately after the first instance of the module keyword,
424 // then that introduces the GMF.
425 if (S == MaybeGMF)
426 S = GMFActive;
427 }
428
429 /// Saw an 'export' identifier.
430 void handleExport() {
431 // The presence of an 'export' keyword always ends or excludes a GMF.
432 S = GMFAbsentOrEnded;
433 }
434
435 /// Saw an 'import' identifier.
436 void handleImport(bool AfterTopLevelTokenSeq) {
437 // If we see this before any 'module' kw, then we have no GMF.
438 if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer)
439 S = GMFAbsentOrEnded;
440 }
441
442 /// Saw a 'module' identifier.
443 void handleModule(bool AfterTopLevelTokenSeq) {
444 // This was the first module identifier and not preceded by any token
445 // that would exclude a GMF. It could begin a GMF, but only if directly
446 // followed by a semicolon.
447 if (AfterTopLevelTokenSeq && S == BeforeGMFIntroducer)
448 S = MaybeGMF;
449 else
450 S = GMFAbsentOrEnded;
451 }
452
453 /// Saw any other token.
454 void handleMisc() {
455 // We saw something other than ; after the 'module' kw, so not a GMF.
456 if (S == MaybeGMF)
457 S = GMFAbsentOrEnded;
458 }
459
460 bool inGMF() { return S == GMFActive; }
461
462 private:
463 /// Track the transitions into and out of a Global Module Fragment,
464 /// if one is present.
465 GMFState S;
466 };
467
468 TrackGMF TrackGMFState = TrackGMF::BeforeGMFIntroducer;
469
470 /// Track the status of the c++20 module decl.
471 ///
472 /// module-declaration:
473 /// 'export'[opt] 'module' module-name module-partition[opt]
474 /// attribute-specifier-seq[opt] ';'
475 ///
476 /// module-name:
477 /// module-name-qualifier[opt] identifier
478 ///
479 /// module-partition:
480 /// ':' module-name-qualifier[opt] identifier
481 ///
482 /// module-name-qualifier:
483 /// identifier '.'
484 /// module-name-qualifier identifier '.'
485 ///
486 /// Transition state:
487 ///
488 /// NotAModuleDecl --- export ---> FoundExport
489 /// NotAModuleDecl --- module ---> ImplementationCandidate
490 /// FoundExport --- module ---> InterfaceCandidate
491 /// ImplementationCandidate --- Identifier ---> ImplementationCandidate
492 /// ImplementationCandidate --- period ---> ImplementationCandidate
493 /// ImplementationCandidate --- colon ---> ImplementationCandidate
494 /// InterfaceCandidate --- Identifier ---> InterfaceCandidate
495 /// InterfaceCandidate --- period ---> InterfaceCandidate
496 /// InterfaceCandidate --- colon ---> InterfaceCandidate
497 /// ImplementationCandidate --- Semi ---> NamedModuleImplementation
498 /// NamedModuleInterface --- Semi ---> NamedModuleInterface
499 /// NamedModuleImplementation --- Anything ---> NamedModuleImplementation
500 /// NamedModuleInterface --- Anything ---> NamedModuleInterface
501 ///
502 /// FIXME: We haven't handle attribute-specifier-seq here. It may not be bad
503 /// soon since we don't support any module attributes yet.
504 class ModuleDeclSeq {
505 enum ModuleDeclState : int {
506 NotAModuleDecl,
507 FoundExport,
508 InterfaceCandidate,
509 ImplementationCandidate,
510 NamedModuleInterface,
511 NamedModuleImplementation,
512 };
513
514 public:
515 ModuleDeclSeq() = default;
516
517 void handleExport() {
518 if (State == NotAModuleDecl)
519 State = FoundExport;
520 else if (!isNamedModule())
521 reset();
522 }
523
524 void handleModule() {
525 if (State == FoundExport)
526 State = InterfaceCandidate;
527 else if (State == NotAModuleDecl)
528 State = ImplementationCandidate;
529 else if (!isNamedModule())
530 reset();
531 }
532
533 void handleIdentifier(IdentifierInfo *Identifier) {
534 if (isModuleCandidate() && Identifier)
535 Name += Identifier->getName().str();
536 else if (!isNamedModule())
537 reset();
538 }
539
540 void handleColon() {
541 if (isModuleCandidate())
542 Name += ":";
543 else if (!isNamedModule())
544 reset();
545 }
546
547 void handlePeriod() {
548 if (isModuleCandidate())
549 Name += ".";
550 else if (!isNamedModule())
551 reset();
552 }
553
554 void handleSemi() {
555 if (!Name.empty() && isModuleCandidate()) {
556 if (State == InterfaceCandidate)
557 State = NamedModuleInterface;
558 else if (State == ImplementationCandidate)
559 State = NamedModuleImplementation;
560 else
561 llvm_unreachable("Unimaged ModuleDeclState.");
562 } else if (!isNamedModule())
563 reset();
564 }
565
566 void handleMisc() {
567 if (!isNamedModule())
568 reset();
569 }
570
571 bool isModuleCandidate() const {
572 return State == InterfaceCandidate || State == ImplementationCandidate;
573 }
574
575 bool isNamedModule() const {
576 return State == NamedModuleInterface ||
577 State == NamedModuleImplementation;
578 }
579
580 bool isNamedInterface() const { return State == NamedModuleInterface; }
581
582 bool isImplementationUnit() const {
583 return State == NamedModuleImplementation && !getName().contains(C: ':');
584 }
585
586 StringRef getName() const {
587 assert(isNamedModule() && "Can't get name from a non named module");
588 return Name;
589 }
590
591 StringRef getPrimaryName() const {
592 assert(isNamedModule() && "Can't get name from a non named module");
593 return getName().split(Separator: ':').first;
594 }
595
596 void reset() {
597 Name.clear();
598 State = NotAModuleDecl;
599 }
600
601 private:
602 ModuleDeclState State = NotAModuleDecl;
603 std::string Name;
604 };
605
606 ModuleDeclSeq ModuleDeclState;
607
608 /// Whether the module import expects an identifier next. Otherwise,
609 /// it expects a '.' or ';'.
610 bool ModuleImportExpectsIdentifier = false;
611
612 /// The identifier and source location of the currently-active
613 /// \#pragma clang arc_cf_code_audited begin.
614 std::pair<IdentifierInfo *, SourceLocation> PragmaARCCFCodeAuditedInfo;
615
616 /// The source location of the currently-active
617 /// \#pragma clang assume_nonnull begin.
618 SourceLocation PragmaAssumeNonNullLoc;
619
620 /// Set only for preambles which end with an active
621 /// \#pragma clang assume_nonnull begin.
622 ///
623 /// When the preamble is loaded into the main file,
624 /// `PragmaAssumeNonNullLoc` will be set to this to
625 /// replay the unterminated assume_nonnull.
626 SourceLocation PreambleRecordedPragmaAssumeNonNullLoc;
627
628 /// True if we hit the code-completion point.
629 bool CodeCompletionReached = false;
630
631 /// The code completion token containing the information
632 /// on the stem that is to be code completed.
633 IdentifierInfo *CodeCompletionII = nullptr;
634
635 /// Range for the code completion token.
636 SourceRange CodeCompletionTokenRange;
637
638 /// The directory that the main file should be considered to occupy,
639 /// if it does not correspond to a real file (as happens when building a
640 /// module).
641 OptionalDirectoryEntryRef MainFileDir;
642
643 /// The number of bytes that we will initially skip when entering the
644 /// main file, along with a flag that indicates whether skipping this number
645 /// of bytes will place the lexer at the start of a line.
646 ///
647 /// This is used when loading a precompiled preamble.
648 std::pair<int, bool> SkipMainFilePreamble;
649
650 /// Whether we hit an error due to reaching max allowed include depth. Allows
651 /// to avoid hitting the same error over and over again.
652 bool HasReachedMaxIncludeDepth = false;
653
654 /// The number of currently-active calls to Lex.
655 ///
656 /// Lex is reentrant, and asking for an (end-of-phase-4) token can often
657 /// require asking for multiple additional tokens. This counter makes it
658 /// possible for Lex to detect whether it's producing a token for the end
659 /// of phase 4 of translation or for some other situation.
660 unsigned LexLevel = 0;
661
662 /// The number of (LexLevel 0) preprocessor tokens.
663 unsigned TokenCount = 0;
664
665 /// Preprocess every token regardless of LexLevel.
666 bool PreprocessToken = false;
667
668 /// The maximum number of (LexLevel 0) tokens before issuing a -Wmax-tokens
669 /// warning, or zero for unlimited.
670 unsigned MaxTokens = 0;
671 SourceLocation MaxTokensOverrideLoc;
672
673public:
674 struct PreambleSkipInfo {
675 SourceLocation HashTokenLoc;
676 SourceLocation IfTokenLoc;
677 bool FoundNonSkipPortion;
678 bool FoundElse;
679 SourceLocation ElseLoc;
680
681 PreambleSkipInfo(SourceLocation HashTokenLoc, SourceLocation IfTokenLoc,
682 bool FoundNonSkipPortion, bool FoundElse,
683 SourceLocation ElseLoc)
684 : HashTokenLoc(HashTokenLoc), IfTokenLoc(IfTokenLoc),
685 FoundNonSkipPortion(FoundNonSkipPortion), FoundElse(FoundElse),
686 ElseLoc(ElseLoc) {}
687 };
688
689 using IncludedFilesSet = llvm::DenseSet<const FileEntry *>;
690
691private:
692 friend class ASTReader;
693 friend class MacroArgs;
694
695 class PreambleConditionalStackStore {
696 enum State {
697 Off = 0,
698 Recording = 1,
699 Replaying = 2,
700 };
701
702 public:
703 PreambleConditionalStackStore() = default;
704
705 void startRecording() { ConditionalStackState = Recording; }
706 void startReplaying() { ConditionalStackState = Replaying; }
707 bool isRecording() const { return ConditionalStackState == Recording; }
708 bool isReplaying() const { return ConditionalStackState == Replaying; }
709
710 ArrayRef<PPConditionalInfo> getStack() const {
711 return ConditionalStack;
712 }
713
714 void doneReplaying() {
715 ConditionalStack.clear();
716 ConditionalStackState = Off;
717 }
718
719 void setStack(ArrayRef<PPConditionalInfo> s) {
720 if (!isRecording() && !isReplaying())
721 return;
722 ConditionalStack.clear();
723 ConditionalStack.append(in_start: s.begin(), in_end: s.end());
724 }
725
726 bool hasRecordedPreamble() const { return !ConditionalStack.empty(); }
727
728 bool reachedEOFWhileSkipping() const { return SkipInfo.has_value(); }
729
730 void clearSkipInfo() { SkipInfo.reset(); }
731
732 std::optional<PreambleSkipInfo> SkipInfo;
733
734 private:
735 SmallVector<PPConditionalInfo, 4> ConditionalStack;
736 State ConditionalStackState = Off;
737 } PreambleConditionalStack;
738
739 /// The current top of the stack that we're lexing from if
740 /// not expanding a macro and we are lexing directly from source code.
741 ///
742 /// Only one of CurLexer, or CurTokenLexer will be non-null.
743 std::unique_ptr<Lexer> CurLexer;
744
745 /// The current top of the stack that we're lexing from
746 /// if not expanding a macro.
747 ///
748 /// This is an alias for CurLexer.
749 PreprocessorLexer *CurPPLexer = nullptr;
750
751 /// Used to find the current FileEntry, if CurLexer is non-null
752 /// and if applicable.
753 ///
754 /// This allows us to implement \#include_next and find directory-specific
755 /// properties.
756 ConstSearchDirIterator CurDirLookup = nullptr;
757
758 /// The current macro we are expanding, if we are expanding a macro.
759 ///
760 /// One of CurLexer and CurTokenLexer must be null.
761 std::unique_ptr<TokenLexer> CurTokenLexer;
762
763 /// The kind of lexer we're currently working with.
764 typedef bool (*LexerCallback)(Preprocessor &, Token &);
765 LexerCallback CurLexerCallback = &CLK_Lexer;
766
767 /// If the current lexer is for a submodule that is being built, this
768 /// is that submodule.
769 Module *CurLexerSubmodule = nullptr;
770
771 /// Keeps track of the stack of files currently
772 /// \#included, and macros currently being expanded from, not counting
773 /// CurLexer/CurTokenLexer.
774 struct IncludeStackInfo {
775 LexerCallback CurLexerCallback;
776 Module *TheSubmodule;
777 std::unique_ptr<Lexer> TheLexer;
778 PreprocessorLexer *ThePPLexer;
779 std::unique_ptr<TokenLexer> TheTokenLexer;
780 ConstSearchDirIterator TheDirLookup;
781
782 // The following constructors are completely useless copies of the default
783 // versions, only needed to pacify MSVC.
784 IncludeStackInfo(LexerCallback CurLexerCallback, Module *TheSubmodule,
785 std::unique_ptr<Lexer> &&TheLexer,
786 PreprocessorLexer *ThePPLexer,
787 std::unique_ptr<TokenLexer> &&TheTokenLexer,
788 ConstSearchDirIterator TheDirLookup)
789 : CurLexerCallback(std::move(CurLexerCallback)),
790 TheSubmodule(std::move(TheSubmodule)), TheLexer(std::move(TheLexer)),
791 ThePPLexer(std::move(ThePPLexer)),
792 TheTokenLexer(std::move(TheTokenLexer)),
793 TheDirLookup(std::move(TheDirLookup)) {}
794 };
795 std::vector<IncludeStackInfo> IncludeMacroStack;
796
797 /// Actions invoked when some preprocessor activity is
798 /// encountered (e.g. a file is \#included, etc).
799 std::unique_ptr<PPCallbacks> Callbacks;
800
801 struct MacroExpandsInfo {
802 Token Tok;
803 MacroDefinition MD;
804 SourceRange Range;
805
806 MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range)
807 : Tok(Tok), MD(MD), Range(Range) {}
808 };
809 SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
810
811 /// Information about a name that has been used to define a module macro.
812 struct ModuleMacroInfo {
813 /// The most recent macro directive for this identifier.
814 MacroDirective *MD;
815
816 /// The active module macros for this identifier.
817 llvm::TinyPtrVector<ModuleMacro *> ActiveModuleMacros;
818
819 /// The generation number at which we last updated ActiveModuleMacros.
820 /// \see Preprocessor::VisibleModules.
821 unsigned ActiveModuleMacrosGeneration = 0;
822
823 /// Whether this macro name is ambiguous.
824 bool IsAmbiguous = false;
825
826 /// The module macros that are overridden by this macro.
827 llvm::TinyPtrVector<ModuleMacro *> OverriddenMacros;
828
829 ModuleMacroInfo(MacroDirective *MD) : MD(MD) {}
830 };
831
832 /// The state of a macro for an identifier.
833 class MacroState {
834 mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State;
835
836 ModuleMacroInfo *getModuleInfo(Preprocessor &PP,
837 const IdentifierInfo *II) const {
838 if (II->isOutOfDate())
839 PP.updateOutOfDateIdentifier(II&: const_cast<IdentifierInfo&>(*II));
840 // FIXME: Find a spare bit on IdentifierInfo and store a
841 // HasModuleMacros flag.
842 if (!II->hasMacroDefinition() ||
843 (!PP.getLangOpts().Modules &&
844 !PP.getLangOpts().ModulesLocalVisibility) ||
845 !PP.CurSubmoduleState->VisibleModules.getGeneration())
846 return nullptr;
847
848 auto *Info = State.dyn_cast<ModuleMacroInfo*>();
849 if (!Info) {
850 Info = new (PP.getPreprocessorAllocator())
851 ModuleMacroInfo(State.get<MacroDirective *>());
852 State = Info;
853 }
854
855 if (PP.CurSubmoduleState->VisibleModules.getGeneration() !=
856 Info->ActiveModuleMacrosGeneration)
857 PP.updateModuleMacroInfo(II, Info&: *Info);
858 return Info;
859 }
860
861 public:
862 MacroState() : MacroState(nullptr) {}
863 MacroState(MacroDirective *MD) : State(MD) {}
864
865 MacroState(MacroState &&O) noexcept : State(O.State) {
866 O.State = (MacroDirective *)nullptr;
867 }
868
869 MacroState &operator=(MacroState &&O) noexcept {
870 auto S = O.State;
871 O.State = (MacroDirective *)nullptr;
872 State = S;
873 return *this;
874 }
875
876 ~MacroState() {
877 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
878 Info->~ModuleMacroInfo();
879 }
880
881 MacroDirective *getLatest() const {
882 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
883 return Info->MD;
884 return State.get<MacroDirective*>();
885 }
886
887 void setLatest(MacroDirective *MD) {
888 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
889 Info->MD = MD;
890 else
891 State = MD;
892 }
893
894 bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const {
895 auto *Info = getModuleInfo(PP, II);
896 return Info ? Info->IsAmbiguous : false;
897 }
898
899 ArrayRef<ModuleMacro *>
900 getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const {
901 if (auto *Info = getModuleInfo(PP, II))
902 return Info->ActiveModuleMacros;
903 return std::nullopt;
904 }
905
906 MacroDirective::DefInfo findDirectiveAtLoc(SourceLocation Loc,
907 SourceManager &SourceMgr) const {
908 // FIXME: Incorporate module macros into the result of this.
909 if (auto *Latest = getLatest())
910 return Latest->findDirectiveAtLoc(L: Loc, SM: SourceMgr);
911 return {};
912 }
913
914 void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) {
915 if (auto *Info = getModuleInfo(PP, II)) {
916 Info->OverriddenMacros.insert(I: Info->OverriddenMacros.end(),
917 From: Info->ActiveModuleMacros.begin(),
918 To: Info->ActiveModuleMacros.end());
919 Info->ActiveModuleMacros.clear();
920 Info->IsAmbiguous = false;
921 }
922 }
923
924 ArrayRef<ModuleMacro*> getOverriddenMacros() const {
925 if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
926 return Info->OverriddenMacros;
927 return std::nullopt;
928 }
929
930 void setOverriddenMacros(Preprocessor &PP,
931 ArrayRef<ModuleMacro *> Overrides) {
932 auto *Info = State.dyn_cast<ModuleMacroInfo*>();
933 if (!Info) {
934 if (Overrides.empty())
935 return;
936 Info = new (PP.getPreprocessorAllocator())
937 ModuleMacroInfo(State.get<MacroDirective *>());
938 State = Info;
939 }
940 Info->OverriddenMacros.clear();
941 Info->OverriddenMacros.insert(I: Info->OverriddenMacros.end(),
942 From: Overrides.begin(), To: Overrides.end());
943 Info->ActiveModuleMacrosGeneration = 0;
944 }
945 };
946
947 /// For each IdentifierInfo that was associated with a macro, we
948 /// keep a mapping to the history of all macro definitions and #undefs in
949 /// the reverse order (the latest one is in the head of the list).
950 ///
951 /// This mapping lives within the \p CurSubmoduleState.
952 using MacroMap = llvm::DenseMap<const IdentifierInfo *, MacroState>;
953
954 struct SubmoduleState;
955
956 /// Information about a submodule that we're currently building.
957 struct BuildingSubmoduleInfo {
958 /// The module that we are building.
959 Module *M;
960
961 /// The location at which the module was included.
962 SourceLocation ImportLoc;
963
964 /// Whether we entered this submodule via a pragma.
965 bool IsPragma;
966
967 /// The previous SubmoduleState.
968 SubmoduleState *OuterSubmoduleState;
969
970 /// The number of pending module macro names when we started building this.
971 unsigned OuterPendingModuleMacroNames;
972
973 BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc, bool IsPragma,
974 SubmoduleState *OuterSubmoduleState,
975 unsigned OuterPendingModuleMacroNames)
976 : M(M), ImportLoc(ImportLoc), IsPragma(IsPragma),
977 OuterSubmoduleState(OuterSubmoduleState),
978 OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {}
979 };
980 SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack;
981
982 /// Information about a submodule's preprocessor state.
983 struct SubmoduleState {
984 /// The macros for the submodule.
985 MacroMap Macros;
986
987 /// The set of modules that are visible within the submodule.
988 VisibleModuleSet VisibleModules;
989
990 // FIXME: CounterValue?
991 // FIXME: PragmaPushMacroInfo?
992 };
993 std::map<Module *, SubmoduleState> Submodules;
994
995 /// The preprocessor state for preprocessing outside of any submodule.
996 SubmoduleState NullSubmoduleState;
997
998 /// The current submodule state. Will be \p NullSubmoduleState if we're not
999 /// in a submodule.
1000 SubmoduleState *CurSubmoduleState;
1001
1002 /// The files that have been included.
1003 IncludedFilesSet IncludedFiles;
1004
1005 /// The set of top-level modules that affected preprocessing, but were not
1006 /// imported.
1007 llvm::SmallSetVector<Module *, 2> AffectingClangModules;
1008
1009 /// The set of known macros exported from modules.
1010 llvm::FoldingSet<ModuleMacro> ModuleMacros;
1011
1012 /// The names of potential module macros that we've not yet processed.
1013 llvm::SmallVector<const IdentifierInfo *, 32> PendingModuleMacroNames;
1014
1015 /// The list of module macros, for each identifier, that are not overridden by
1016 /// any other module macro.
1017 llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro *>>
1018 LeafModuleMacros;
1019
1020 /// Macros that we want to warn because they are not used at the end
1021 /// of the translation unit.
1022 ///
1023 /// We store just their SourceLocations instead of
1024 /// something like MacroInfo*. The benefit of this is that when we are
1025 /// deserializing from PCH, we don't need to deserialize identifier & macros
1026 /// just so that we can report that they are unused, we just warn using
1027 /// the SourceLocations of this set (that will be filled by the ASTReader).
1028 using WarnUnusedMacroLocsTy = llvm::SmallDenseSet<SourceLocation, 32>;
1029 WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
1030
1031 /// This is a pair of an optional message and source location used for pragmas
1032 /// that annotate macros like pragma clang restrict_expansion and pragma clang
1033 /// deprecated. This pair stores the optional message and the location of the
1034 /// annotation pragma for use producing diagnostics and notes.
1035 using MsgLocationPair = std::pair<std::string, SourceLocation>;
1036
1037 struct MacroAnnotationInfo {
1038 SourceLocation Location;
1039 std::string Message;
1040 };
1041
1042 struct MacroAnnotations {
1043 std::optional<MacroAnnotationInfo> DeprecationInfo;
1044 std::optional<MacroAnnotationInfo> RestrictExpansionInfo;
1045 std::optional<SourceLocation> FinalAnnotationLoc;
1046
1047 static MacroAnnotations makeDeprecation(SourceLocation Loc,
1048 std::string Msg) {
1049 return MacroAnnotations{.DeprecationInfo: MacroAnnotationInfo{.Location: Loc, .Message: std::move(Msg)},
1050 .RestrictExpansionInfo: std::nullopt, .FinalAnnotationLoc: std::nullopt};
1051 }
1052
1053 static MacroAnnotations makeRestrictExpansion(SourceLocation Loc,
1054 std::string Msg) {
1055 return MacroAnnotations{
1056 .DeprecationInfo: std::nullopt, .RestrictExpansionInfo: MacroAnnotationInfo{.Location: Loc, .Message: std::move(Msg)}, .FinalAnnotationLoc: std::nullopt};
1057 }
1058
1059 static MacroAnnotations makeFinal(SourceLocation Loc) {
1060 return MacroAnnotations{.DeprecationInfo: std::nullopt, .RestrictExpansionInfo: std::nullopt, .FinalAnnotationLoc: Loc};
1061 }
1062 };
1063
1064 /// Warning information for macro annotations.
1065 llvm::DenseMap<const IdentifierInfo *, MacroAnnotations> AnnotationInfos;
1066
1067 /// A "freelist" of MacroArg objects that can be
1068 /// reused for quick allocation.
1069 MacroArgs *MacroArgCache = nullptr;
1070
1071 /// For each IdentifierInfo used in a \#pragma push_macro directive,
1072 /// we keep a MacroInfo stack used to restore the previous macro value.
1073 llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>
1074 PragmaPushMacroInfo;
1075
1076 // Various statistics we track for performance analysis.
1077 unsigned NumDirectives = 0;
1078 unsigned NumDefined = 0;
1079 unsigned NumUndefined = 0;
1080 unsigned NumPragma = 0;
1081 unsigned NumIf = 0;
1082 unsigned NumElse = 0;
1083 unsigned NumEndif = 0;
1084 unsigned NumEnteredSourceFiles = 0;
1085 unsigned MaxIncludeStackDepth = 0;
1086 unsigned NumMacroExpanded = 0;
1087 unsigned NumFnMacroExpanded = 0;
1088 unsigned NumBuiltinMacroExpanded = 0;
1089 unsigned NumFastMacroExpanded = 0;
1090 unsigned NumTokenPaste = 0;
1091 unsigned NumFastTokenPaste = 0;
1092 unsigned NumSkipped = 0;
1093
1094 /// The predefined macros that preprocessor should use from the
1095 /// command line etc.
1096 std::string Predefines;
1097
1098 /// The file ID for the preprocessor predefines.
1099 FileID PredefinesFileID;
1100
1101 /// The file ID for the PCH through header.
1102 FileID PCHThroughHeaderFileID;
1103
1104 /// Whether tokens are being skipped until a #pragma hdrstop is seen.
1105 bool SkippingUntilPragmaHdrStop = false;
1106
1107 /// Whether tokens are being skipped until the through header is seen.
1108 bool SkippingUntilPCHThroughHeader = false;
1109
1110 /// \{
1111 /// Cache of macro expanders to reduce malloc traffic.
1112 enum { TokenLexerCacheSize = 8 };
1113 unsigned NumCachedTokenLexers;
1114 std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize];
1115 /// \}
1116
1117 /// Keeps macro expanded tokens for TokenLexers.
1118 //
1119 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1120 /// going to lex in the cache and when it finishes the tokens are removed
1121 /// from the end of the cache.
1122 SmallVector<Token, 16> MacroExpandedTokens;
1123 std::vector<std::pair<TokenLexer *, size_t>> MacroExpandingLexersStack;
1124
1125 /// A record of the macro definitions and expansions that
1126 /// occurred during preprocessing.
1127 ///
1128 /// This is an optional side structure that can be enabled with
1129 /// \c createPreprocessingRecord() prior to preprocessing.
1130 PreprocessingRecord *Record = nullptr;
1131
1132 /// Cached tokens state.
1133 using CachedTokensTy = SmallVector<Token, 1>;
1134
1135 /// Cached tokens are stored here when we do backtracking or
1136 /// lookahead. They are "lexed" by the CachingLex() method.
1137 CachedTokensTy CachedTokens;
1138
1139 /// The position of the cached token that CachingLex() should
1140 /// "lex" next.
1141 ///
1142 /// If it points beyond the CachedTokens vector, it means that a normal
1143 /// Lex() should be invoked.
1144 CachedTokensTy::size_type CachedLexPos = 0;
1145
1146 /// Stack of backtrack positions, allowing nested backtracks.
1147 ///
1148 /// The EnableBacktrackAtThisPos() method pushes a position to
1149 /// indicate where CachedLexPos should be set when the BackTrack() method is
1150 /// invoked (at which point the last position is popped).
1151 std::vector<CachedTokensTy::size_type> BacktrackPositions;
1152
1153 /// True if \p Preprocessor::SkipExcludedConditionalBlock() is running.
1154 /// This is used to guard against calling this function recursively.
1155 ///
1156 /// See comments at the use-site for more context about why it is needed.
1157 bool SkippingExcludedConditionalBlock = false;
1158
1159 /// Keeps track of skipped range mappings that were recorded while skipping
1160 /// excluded conditional directives. It maps the source buffer pointer at
1161 /// the beginning of a skipped block, to the number of bytes that should be
1162 /// skipped.
1163 llvm::DenseMap<const char *, unsigned> RecordedSkippedRanges;
1164
1165 void updateOutOfDateIdentifier(IdentifierInfo &II) const;
1166
1167public:
1168 Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
1169 DiagnosticsEngine &diags, const LangOptions &LangOpts,
1170 SourceManager &SM, HeaderSearch &Headers,
1171 ModuleLoader &TheModuleLoader,
1172 IdentifierInfoLookup *IILookup = nullptr,
1173 bool OwnsHeaderSearch = false,
1174 TranslationUnitKind TUKind = TU_Complete);
1175
1176 ~Preprocessor();
1177
1178 /// Initialize the preprocessor using information about the target.
1179 ///
1180 /// \param Target is owned by the caller and must remain valid for the
1181 /// lifetime of the preprocessor.
1182 /// \param AuxTarget is owned by the caller and must remain valid for
1183 /// the lifetime of the preprocessor.
1184 void Initialize(const TargetInfo &Target,
1185 const TargetInfo *AuxTarget = nullptr);
1186
1187 /// Initialize the preprocessor to parse a model file
1188 ///
1189 /// To parse model files the preprocessor of the original source is reused to
1190 /// preserver the identifier table. However to avoid some duplicate
1191 /// information in the preprocessor some cleanup is needed before it is used
1192 /// to parse model files. This method does that cleanup.
1193 void InitializeForModelFile();
1194
1195 /// Cleanup after model file parsing
1196 void FinalizeForModelFile();
1197
1198 /// Retrieve the preprocessor options used to initialize this
1199 /// preprocessor.
1200 PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
1201
1202 DiagnosticsEngine &getDiagnostics() const { return *Diags; }
1203 void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
1204
1205 const LangOptions &getLangOpts() const { return LangOpts; }
1206 const TargetInfo &getTargetInfo() const { return *Target; }
1207 const TargetInfo *getAuxTargetInfo() const { return AuxTarget; }
1208 FileManager &getFileManager() const { return FileMgr; }
1209 SourceManager &getSourceManager() const { return SourceMgr; }
1210 HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
1211
1212 IdentifierTable &getIdentifierTable() { return Identifiers; }
1213 const IdentifierTable &getIdentifierTable() const { return Identifiers; }
1214 SelectorTable &getSelectorTable() { return Selectors; }
1215 Builtin::Context &getBuiltinInfo() { return *BuiltinInfo; }
1216 llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
1217
1218 void setExternalSource(ExternalPreprocessorSource *Source) {
1219 ExternalSource = Source;
1220 }
1221
1222 ExternalPreprocessorSource *getExternalSource() const {
1223 return ExternalSource;
1224 }
1225
1226 /// Retrieve the module loader associated with this preprocessor.
1227 ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
1228
1229 bool hadModuleLoaderFatalFailure() const {
1230 return TheModuleLoader.HadFatalFailure;
1231 }
1232
1233 /// Retrieve the number of Directives that have been processed by the
1234 /// Preprocessor.
1235 unsigned getNumDirectives() const {
1236 return NumDirectives;
1237 }
1238
1239 /// True if we are currently preprocessing a #if or #elif directive
1240 bool isParsingIfOrElifDirective() const {
1241 return ParsingIfOrElifDirective;
1242 }
1243
1244 /// Control whether the preprocessor retains comments in output.
1245 void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
1246 this->KeepComments = KeepComments | KeepMacroComments;
1247 this->KeepMacroComments = KeepMacroComments;
1248 }
1249
1250 bool getCommentRetentionState() const { return KeepComments; }
1251
1252 void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
1253 bool getPragmasEnabled() const { return PragmasEnabled; }
1254
1255 void SetSuppressIncludeNotFoundError(bool Suppress) {
1256 SuppressIncludeNotFoundError = Suppress;
1257 }
1258
1259 bool GetSuppressIncludeNotFoundError() {
1260 return SuppressIncludeNotFoundError;
1261 }
1262
1263 /// Sets whether the preprocessor is responsible for producing output or if
1264 /// it is producing tokens to be consumed by Parse and Sema.
1265 void setPreprocessedOutput(bool IsPreprocessedOutput) {
1266 PreprocessedOutput = IsPreprocessedOutput;
1267 }
1268
1269 /// Returns true if the preprocessor is responsible for generating output,
1270 /// false if it is producing tokens to be consumed by Parse and Sema.
1271 bool isPreprocessedOutput() const { return PreprocessedOutput; }
1272
1273 /// Return true if we are lexing directly from the specified lexer.
1274 bool isCurrentLexer(const PreprocessorLexer *L) const {
1275 return CurPPLexer == L;
1276 }
1277
1278 /// Return the current lexer being lexed from.
1279 ///
1280 /// Note that this ignores any potentially active macro expansions and _Pragma
1281 /// expansions going on at the time.
1282 PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
1283
1284 /// Return the current file lexer being lexed from.
1285 ///
1286 /// Note that this ignores any potentially active macro expansions and _Pragma
1287 /// expansions going on at the time.
1288 PreprocessorLexer *getCurrentFileLexer() const;
1289
1290 /// Return the submodule owning the file being lexed. This may not be
1291 /// the current module if we have changed modules since entering the file.
1292 Module *getCurrentLexerSubmodule() const { return CurLexerSubmodule; }
1293
1294 /// Returns the FileID for the preprocessor predefines.
1295 FileID getPredefinesFileID() const { return PredefinesFileID; }
1296
1297 /// \{
1298 /// Accessors for preprocessor callbacks.
1299 ///
1300 /// Note that this class takes ownership of any PPCallbacks object given to
1301 /// it.
1302 PPCallbacks *getPPCallbacks() const { return Callbacks.get(); }
1303 void addPPCallbacks(std::unique_ptr<PPCallbacks> C) {
1304 if (Callbacks)
1305 C = std::make_unique<PPChainedCallbacks>(args: std::move(C),
1306 args: std::move(Callbacks));
1307 Callbacks = std::move(C);
1308 }
1309 /// \}
1310
1311 /// Get the number of tokens processed so far.
1312 unsigned getTokenCount() const { return TokenCount; }
1313
1314 /// Get the max number of tokens before issuing a -Wmax-tokens warning.
1315 unsigned getMaxTokens() const { return MaxTokens; }
1316
1317 void overrideMaxTokens(unsigned Value, SourceLocation Loc) {
1318 MaxTokens = Value;
1319 MaxTokensOverrideLoc = Loc;
1320 };
1321
1322 SourceLocation getMaxTokensOverrideLoc() const { return MaxTokensOverrideLoc; }
1323
1324 /// Register a function that would be called on each token in the final
1325 /// expanded token stream.
1326 /// This also reports annotation tokens produced by the parser.
1327 void setTokenWatcher(llvm::unique_function<void(const clang::Token &)> F) {
1328 OnToken = std::move(F);
1329 }
1330
1331 void setPreprocessToken(bool Preprocess) { PreprocessToken = Preprocess; }
1332
1333 bool isMacroDefined(StringRef Id) {
1334 return isMacroDefined(&Identifiers.get(Id));
1335 }
1336 bool isMacroDefined(const IdentifierInfo *II) {
1337 return II->hasMacroDefinition() &&
1338 (!getLangOpts().Modules || (bool)getMacroDefinition(II));
1339 }
1340
1341 /// Determine whether II is defined as a macro within the module M,
1342 /// if that is a module that we've already preprocessed. Does not check for
1343 /// macros imported into M.
1344 bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M) {
1345 if (!II->hasMacroDefinition())
1346 return false;
1347 auto I = Submodules.find(x: M);
1348 if (I == Submodules.end())
1349 return false;
1350 auto J = I->second.Macros.find(Val: II);
1351 if (J == I->second.Macros.end())
1352 return false;
1353 auto *MD = J->second.getLatest();
1354 return MD && MD->isDefined();
1355 }
1356
1357 MacroDefinition getMacroDefinition(const IdentifierInfo *II) {
1358 if (!II->hasMacroDefinition())
1359 return {};
1360
1361 MacroState &S = CurSubmoduleState->Macros[II];
1362 auto *MD = S.getLatest();
1363 while (MD && isa<VisibilityMacroDirective>(Val: MD))
1364 MD = MD->getPrevious();
1365 return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(Val: MD),
1366 S.getActiveModuleMacros(PP&: *this, II),
1367 S.isAmbiguous(PP&: *this, II));
1368 }
1369
1370 MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II,
1371 SourceLocation Loc) {
1372 if (!II->hadMacroDefinition())
1373 return {};
1374
1375 MacroState &S = CurSubmoduleState->Macros[II];
1376 MacroDirective::DefInfo DI;
1377 if (auto *MD = S.getLatest())
1378 DI = MD->findDirectiveAtLoc(L: Loc, SM: getSourceManager());
1379 // FIXME: Compute the set of active module macros at the specified location.
1380 return MacroDefinition(DI.getDirective(),
1381 S.getActiveModuleMacros(PP&: *this, II),
1382 S.isAmbiguous(PP&: *this, II));
1383 }
1384
1385 /// Given an identifier, return its latest non-imported MacroDirective
1386 /// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd.
1387 MacroDirective *getLocalMacroDirective(const IdentifierInfo *II) const {
1388 if (!II->hasMacroDefinition())
1389 return nullptr;
1390
1391 auto *MD = getLocalMacroDirectiveHistory(II);
1392 if (!MD || MD->getDefinition().isUndefined())
1393 return nullptr;
1394
1395 return MD;
1396 }
1397
1398 const MacroInfo *getMacroInfo(const IdentifierInfo *II) const {
1399 return const_cast<Preprocessor*>(this)->getMacroInfo(II);
1400 }
1401
1402 MacroInfo *getMacroInfo(const IdentifierInfo *II) {
1403 if (!II->hasMacroDefinition())
1404 return nullptr;
1405 if (auto MD = getMacroDefinition(II))
1406 return MD.getMacroInfo();
1407 return nullptr;
1408 }
1409
1410 /// Given an identifier, return the latest non-imported macro
1411 /// directive for that identifier.
1412 ///
1413 /// One can iterate over all previous macro directives from the most recent
1414 /// one.
1415 MacroDirective *getLocalMacroDirectiveHistory(const IdentifierInfo *II) const;
1416
1417 /// Add a directive to the macro directive history for this identifier.
1418 void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
1419 DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI,
1420 SourceLocation Loc) {
1421 DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc);
1422 appendMacroDirective(II, MD);
1423 return MD;
1424 }
1425 DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II,
1426 MacroInfo *MI) {
1427 return appendDefMacroDirective(II, MI, Loc: MI->getDefinitionLoc());
1428 }
1429
1430 /// Set a MacroDirective that was loaded from a PCH file.
1431 void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED,
1432 MacroDirective *MD);
1433
1434 /// Register an exported macro for a module and identifier.
1435 ModuleMacro *addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro,
1436 ArrayRef<ModuleMacro *> Overrides, bool &IsNew);
1437 ModuleMacro *getModuleMacro(Module *Mod, const IdentifierInfo *II);
1438
1439 /// Get the list of leaf (non-overridden) module macros for a name.
1440 ArrayRef<ModuleMacro*> getLeafModuleMacros(const IdentifierInfo *II) const {
1441 if (II->isOutOfDate())
1442 updateOutOfDateIdentifier(II&: const_cast<IdentifierInfo&>(*II));
1443 auto I = LeafModuleMacros.find(Val: II);
1444 if (I != LeafModuleMacros.end())
1445 return I->second;
1446 return std::nullopt;
1447 }
1448
1449 /// Get the list of submodules that we're currently building.
1450 ArrayRef<BuildingSubmoduleInfo> getBuildingSubmodules() const {
1451 return BuildingSubmoduleStack;
1452 }
1453
1454 /// \{
1455 /// Iterators for the macro history table. Currently defined macros have
1456 /// IdentifierInfo::hasMacroDefinition() set and an empty
1457 /// MacroInfo::getUndefLoc() at the head of the list.
1458 using macro_iterator = MacroMap::const_iterator;
1459
1460 macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
1461 macro_iterator macro_end(bool IncludeExternalMacros = true) const;
1462
1463 llvm::iterator_range<macro_iterator>
1464 macros(bool IncludeExternalMacros = true) const {
1465 macro_iterator begin = macro_begin(IncludeExternalMacros);
1466 macro_iterator end = macro_end(IncludeExternalMacros);
1467 return llvm::make_range(x: begin, y: end);
1468 }
1469
1470 /// \}
1471
1472 /// Mark the given clang module as affecting the current clang module or translation unit.
1473 void markClangModuleAsAffecting(Module *M) {
1474 assert(M->isModuleMapModule());
1475 if (!BuildingSubmoduleStack.empty()) {
1476 if (M != BuildingSubmoduleStack.back().M)
1477 BuildingSubmoduleStack.back().M->AffectingClangModules.insert(X: M);
1478 } else {
1479 AffectingClangModules.insert(X: M);
1480 }
1481 }
1482
1483 /// Get the set of top-level clang modules that affected preprocessing, but were not
1484 /// imported.
1485 const llvm::SmallSetVector<Module *, 2> &getAffectingClangModules() const {
1486 return AffectingClangModules;
1487 }
1488
1489 /// Mark the file as included.
1490 /// Returns true if this is the first time the file was included.
1491 bool markIncluded(FileEntryRef File) {
1492 HeaderInfo.getFileInfo(FE: File);
1493 return IncludedFiles.insert(V: File).second;
1494 }
1495
1496 /// Return true if this header has already been included.
1497 bool alreadyIncluded(FileEntryRef File) const {
1498 HeaderInfo.getFileInfo(FE: File);
1499 return IncludedFiles.count(V: File);
1500 }
1501
1502 /// Get the set of included files.
1503 IncludedFilesSet &getIncludedFiles() { return IncludedFiles; }
1504 const IncludedFilesSet &getIncludedFiles() const { return IncludedFiles; }
1505
1506 /// Return the name of the macro defined before \p Loc that has
1507 /// spelling \p Tokens. If there are multiple macros with same spelling,
1508 /// return the last one defined.
1509 StringRef getLastMacroWithSpelling(SourceLocation Loc,
1510 ArrayRef<TokenValue> Tokens) const;
1511
1512 /// Get the predefines for this processor.
1513 /// Used by some third-party tools to inspect and add predefines (see
1514 /// https://github.com/llvm/llvm-project/issues/57483).
1515 const std::string &getPredefines() const { return Predefines; }
1516
1517 /// Set the predefines for this Preprocessor.
1518 ///
1519 /// These predefines are automatically injected when parsing the main file.
1520 void setPredefines(std::string P) { Predefines = std::move(P); }
1521
1522 /// Return information about the specified preprocessor
1523 /// identifier token.
1524 IdentifierInfo *getIdentifierInfo(StringRef Name) const {
1525 return &Identifiers.get(Name);
1526 }
1527
1528 /// Add the specified pragma handler to this preprocessor.
1529 ///
1530 /// If \p Namespace is non-null, then it is a token required to exist on the
1531 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1532 void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1533 void AddPragmaHandler(PragmaHandler *Handler) {
1534 AddPragmaHandler(Namespace: StringRef(), Handler);
1535 }
1536
1537 /// Remove the specific pragma handler from this preprocessor.
1538 ///
1539 /// If \p Namespace is non-null, then it should be the namespace that
1540 /// \p Handler was added to. It is an error to remove a handler that
1541 /// has not been registered.
1542 void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
1543 void RemovePragmaHandler(PragmaHandler *Handler) {
1544 RemovePragmaHandler(Namespace: StringRef(), Handler);
1545 }
1546
1547 /// Install empty handlers for all pragmas (making them ignored).
1548 void IgnorePragmas();
1549
1550 /// Set empty line handler.
1551 void setEmptylineHandler(EmptylineHandler *Handler) { Emptyline = Handler; }
1552
1553 EmptylineHandler *getEmptylineHandler() const { return Emptyline; }
1554
1555 /// Add the specified comment handler to the preprocessor.
1556 void addCommentHandler(CommentHandler *Handler);
1557
1558 /// Remove the specified comment handler.
1559 ///
1560 /// It is an error to remove a handler that has not been registered.
1561 void removeCommentHandler(CommentHandler *Handler);
1562
1563 /// Set the code completion handler to the given object.
1564 void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
1565 CodeComplete = &Handler;
1566 }
1567
1568 /// Retrieve the current code-completion handler.
1569 CodeCompletionHandler *getCodeCompletionHandler() const {
1570 return CodeComplete;
1571 }
1572
1573 /// Clear out the code completion handler.
1574 void clearCodeCompletionHandler() {
1575 CodeComplete = nullptr;
1576 }
1577
1578 /// Hook used by the lexer to invoke the "included file" code
1579 /// completion point.
1580 void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled);
1581
1582 /// Hook used by the lexer to invoke the "natural language" code
1583 /// completion point.
1584 void CodeCompleteNaturalLanguage();
1585
1586 /// Set the code completion token for filtering purposes.
1587 void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter) {
1588 CodeCompletionII = Filter;
1589 }
1590
1591 /// Set the code completion token range for detecting replacement range later
1592 /// on.
1593 void setCodeCompletionTokenRange(const SourceLocation Start,
1594 const SourceLocation End) {
1595 CodeCompletionTokenRange = {Start, End};
1596 }
1597 SourceRange getCodeCompletionTokenRange() const {
1598 return CodeCompletionTokenRange;
1599 }
1600
1601 /// Get the code completion token for filtering purposes.
1602 StringRef getCodeCompletionFilter() {
1603 if (CodeCompletionII)
1604 return CodeCompletionII->getName();
1605 return {};
1606 }
1607
1608 /// Retrieve the preprocessing record, or NULL if there is no
1609 /// preprocessing record.
1610 PreprocessingRecord *getPreprocessingRecord() const { return Record; }
1611
1612 /// Create a new preprocessing record, which will keep track of
1613 /// all macro expansions, macro definitions, etc.
1614 void createPreprocessingRecord();
1615
1616 /// Returns true if the FileEntry is the PCH through header.
1617 bool isPCHThroughHeader(const FileEntry *FE);
1618
1619 /// True if creating a PCH with a through header.
1620 bool creatingPCHWithThroughHeader();
1621
1622 /// True if using a PCH with a through header.
1623 bool usingPCHWithThroughHeader();
1624
1625 /// True if creating a PCH with a #pragma hdrstop.
1626 bool creatingPCHWithPragmaHdrStop();
1627
1628 /// True if using a PCH with a #pragma hdrstop.
1629 bool usingPCHWithPragmaHdrStop();
1630
1631 /// Skip tokens until after the #include of the through header or
1632 /// until after a #pragma hdrstop.
1633 void SkipTokensWhileUsingPCH();
1634
1635 /// Process directives while skipping until the through header or
1636 /// #pragma hdrstop is found.
1637 void HandleSkippedDirectiveWhileUsingPCH(Token &Result,
1638 SourceLocation HashLoc);
1639
1640 /// Enter the specified FileID as the main source file,
1641 /// which implicitly adds the builtin defines etc.
1642 void EnterMainSourceFile();
1643
1644 /// Inform the preprocessor callbacks that processing is complete.
1645 void EndSourceFile();
1646
1647 /// Add a source file to the top of the include stack and
1648 /// start lexing tokens from it instead of the current buffer.
1649 ///
1650 /// Emits a diagnostic, doesn't enter the file, and returns true on error.
1651 bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir,
1652 SourceLocation Loc, bool IsFirstIncludeOfFile = true);
1653
1654 /// Add a Macro to the top of the include stack and start lexing
1655 /// tokens from it instead of the current buffer.
1656 ///
1657 /// \param Args specifies the tokens input to a function-like macro.
1658 /// \param ILEnd specifies the location of the ')' for a function-like macro
1659 /// or the identifier for an object-like macro.
1660 void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro,
1661 MacroArgs *Args);
1662
1663private:
1664 /// Add a "macro" context to the top of the include stack,
1665 /// which will cause the lexer to start returning the specified tokens.
1666 ///
1667 /// If \p DisableMacroExpansion is true, tokens lexed from the token stream
1668 /// will not be subject to further macro expansion. Otherwise, these tokens
1669 /// will be re-macro-expanded when/if expansion is enabled.
1670 ///
1671 /// If \p OwnsTokens is false, this method assumes that the specified stream
1672 /// of tokens has a permanent owner somewhere, so they do not need to be
1673 /// copied. If it is true, it assumes the array of tokens is allocated with
1674 /// \c new[] and the Preprocessor will delete[] it.
1675 ///
1676 /// If \p IsReinject the resulting tokens will have Token::IsReinjected flag
1677 /// set, see the flag documentation for details.
1678 void EnterTokenStream(const Token *Toks, unsigned NumToks,
1679 bool DisableMacroExpansion, bool OwnsTokens,
1680 bool IsReinject);
1681
1682public:
1683 void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1684 bool DisableMacroExpansion, bool IsReinject) {
1685 EnterTokenStream(Toks: Toks.release(), NumToks, DisableMacroExpansion, OwnsTokens: true,
1686 IsReinject);
1687 }
1688
1689 void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion,
1690 bool IsReinject) {
1691 EnterTokenStream(Toks: Toks.data(), NumToks: Toks.size(), DisableMacroExpansion, OwnsTokens: false,
1692 IsReinject);
1693 }
1694
1695 /// Pop the current lexer/macro exp off the top of the lexer stack.
1696 ///
1697 /// This should only be used in situations where the current state of the
1698 /// top-of-stack lexer is known.
1699 void RemoveTopOfLexerStack();
1700
1701 /// From the point that this method is called, and until
1702 /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
1703 /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
1704 /// make the Preprocessor re-lex the same tokens.
1705 ///
1706 /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
1707 /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
1708 /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
1709 ///
1710 /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
1711 /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
1712 /// tokens will continue indefinitely.
1713 ///
1714 void EnableBacktrackAtThisPos();
1715
1716 /// Disable the last EnableBacktrackAtThisPos call.
1717 void CommitBacktrackedTokens();
1718
1719 /// Make Preprocessor re-lex the tokens that were lexed since
1720 /// EnableBacktrackAtThisPos() was previously called.
1721 void Backtrack();
1722
1723 /// True if EnableBacktrackAtThisPos() was called and
1724 /// caching of tokens is on.
1725 bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
1726
1727 /// Lex the next token for this preprocessor.
1728 void Lex(Token &Result);
1729
1730 /// Lex all tokens for this preprocessor until (and excluding) end of file.
1731 void LexTokensUntilEOF(std::vector<Token> *Tokens = nullptr);
1732
1733 /// Lex a token, forming a header-name token if possible.
1734 bool LexHeaderName(Token &Result, bool AllowMacroExpansion = true);
1735
1736 bool LexAfterModuleImport(Token &Result);
1737 void CollectPpImportSuffix(SmallVectorImpl<Token> &Toks);
1738
1739 void makeModuleVisible(Module *M, SourceLocation Loc);
1740
1741 SourceLocation getModuleImportLoc(Module *M) const {
1742 return CurSubmoduleState->VisibleModules.getImportLoc(M);
1743 }
1744
1745 /// Lex a string literal, which may be the concatenation of multiple
1746 /// string literals and may even come from macro expansion.
1747 /// \returns true on success, false if a error diagnostic has been generated.
1748 bool LexStringLiteral(Token &Result, std::string &String,
1749 const char *DiagnosticTag, bool AllowMacroExpansion) {
1750 if (AllowMacroExpansion)
1751 Lex(Result);
1752 else
1753 LexUnexpandedToken(Result);
1754 return FinishLexStringLiteral(Result, String, DiagnosticTag,
1755 AllowMacroExpansion);
1756 }
1757
1758 /// Complete the lexing of a string literal where the first token has
1759 /// already been lexed (see LexStringLiteral).
1760 bool FinishLexStringLiteral(Token &Result, std::string &String,
1761 const char *DiagnosticTag,
1762 bool AllowMacroExpansion);
1763
1764 /// Lex a token. If it's a comment, keep lexing until we get
1765 /// something not a comment.
1766 ///
1767 /// This is useful in -E -C mode where comments would foul up preprocessor
1768 /// directive handling.
1769 void LexNonComment(Token &Result) {
1770 do
1771 Lex(Result);
1772 while (Result.getKind() == tok::comment);
1773 }
1774
1775 /// Just like Lex, but disables macro expansion of identifier tokens.
1776 void LexUnexpandedToken(Token &Result) {
1777 // Disable macro expansion.
1778 bool OldVal = DisableMacroExpansion;
1779 DisableMacroExpansion = true;
1780 // Lex the token.
1781 Lex(Result);
1782
1783 // Reenable it.
1784 DisableMacroExpansion = OldVal;
1785 }
1786
1787 /// Like LexNonComment, but this disables macro expansion of
1788 /// identifier tokens.
1789 void LexUnexpandedNonComment(Token &Result) {
1790 do
1791 LexUnexpandedToken(Result);
1792 while (Result.getKind() == tok::comment);
1793 }
1794
1795 /// Parses a simple integer literal to get its numeric value. Floating
1796 /// point literals and user defined literals are rejected. Used primarily to
1797 /// handle pragmas that accept integer arguments.
1798 bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value);
1799
1800 /// Disables macro expansion everywhere except for preprocessor directives.
1801 void SetMacroExpansionOnlyInDirectives() {
1802 DisableMacroExpansion = true;
1803 MacroExpansionInDirectivesOverride = true;
1804 }
1805
1806 /// Peeks ahead N tokens and returns that token without consuming any
1807 /// tokens.
1808 ///
1809 /// LookAhead(0) returns the next token that would be returned by Lex(),
1810 /// LookAhead(1) returns the token after it, etc. This returns normal
1811 /// tokens after phase 5. As such, it is equivalent to using
1812 /// 'Lex', not 'LexUnexpandedToken'.
1813 const Token &LookAhead(unsigned N) {
1814 assert(LexLevel == 0 && "cannot use lookahead while lexing");
1815 if (CachedLexPos + N < CachedTokens.size())
1816 return CachedTokens[CachedLexPos+N];
1817 else
1818 return PeekAhead(N: N+1);
1819 }
1820
1821 /// When backtracking is enabled and tokens are cached,
1822 /// this allows to revert a specific number of tokens.
1823 ///
1824 /// Note that the number of tokens being reverted should be up to the last
1825 /// backtrack position, not more.
1826 void RevertCachedTokens(unsigned N) {
1827 assert(isBacktrackEnabled() &&
1828 "Should only be called when tokens are cached for backtracking");
1829 assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
1830 && "Should revert tokens up to the last backtrack position, not more");
1831 assert(signed(CachedLexPos) - signed(N) >= 0 &&
1832 "Corrupted backtrack positions ?");
1833 CachedLexPos -= N;
1834 }
1835
1836 /// Enters a token in the token stream to be lexed next.
1837 ///
1838 /// If BackTrack() is called afterwards, the token will remain at the
1839 /// insertion point.
1840 /// If \p IsReinject is true, resulting token will have Token::IsReinjected
1841 /// flag set. See the flag documentation for details.
1842 void EnterToken(const Token &Tok, bool IsReinject) {
1843 if (LexLevel) {
1844 // It's not correct in general to enter caching lex mode while in the
1845 // middle of a nested lexing action.
1846 auto TokCopy = std::make_unique<Token[]>(num: 1);
1847 TokCopy[0] = Tok;
1848 EnterTokenStream(Toks: std::move(TokCopy), NumToks: 1, DisableMacroExpansion: true, IsReinject);
1849 } else {
1850 EnterCachingLexMode();
1851 assert(IsReinject && "new tokens in the middle of cached stream");
1852 CachedTokens.insert(I: CachedTokens.begin()+CachedLexPos, Elt: Tok);
1853 }
1854 }
1855
1856 /// We notify the Preprocessor that if it is caching tokens (because
1857 /// backtrack is enabled) it should replace the most recent cached tokens
1858 /// with the given annotation token. This function has no effect if
1859 /// backtracking is not enabled.
1860 ///
1861 /// Note that the use of this function is just for optimization, so that the
1862 /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
1863 /// invoked.
1864 void AnnotateCachedTokens(const Token &Tok) {
1865 assert(Tok.isAnnotation() && "Expected annotation token");
1866 if (CachedLexPos != 0 && isBacktrackEnabled())
1867 AnnotatePreviousCachedTokens(Tok);
1868 }
1869
1870 /// Get the location of the last cached token, suitable for setting the end
1871 /// location of an annotation token.
1872 SourceLocation getLastCachedTokenLocation() const {
1873 assert(CachedLexPos != 0);
1874 return CachedTokens[CachedLexPos-1].getLastLoc();
1875 }
1876
1877 /// Whether \p Tok is the most recent token (`CachedLexPos - 1`) in
1878 /// CachedTokens.
1879 bool IsPreviousCachedToken(const Token &Tok) const;
1880
1881 /// Replace token in `CachedLexPos - 1` in CachedTokens by the tokens
1882 /// in \p NewToks.
1883 ///
1884 /// Useful when a token needs to be split in smaller ones and CachedTokens
1885 /// most recent token must to be updated to reflect that.
1886 void ReplacePreviousCachedToken(ArrayRef<Token> NewToks);
1887
1888 /// Replace the last token with an annotation token.
1889 ///
1890 /// Like AnnotateCachedTokens(), this routine replaces an
1891 /// already-parsed (and resolved) token with an annotation
1892 /// token. However, this routine only replaces the last token with
1893 /// the annotation token; it does not affect any other cached
1894 /// tokens. This function has no effect if backtracking is not
1895 /// enabled.
1896 void ReplaceLastTokenWithAnnotation(const Token &Tok) {
1897 assert(Tok.isAnnotation() && "Expected annotation token");
1898 if (CachedLexPos != 0 && isBacktrackEnabled())
1899 CachedTokens[CachedLexPos-1] = Tok;
1900 }
1901
1902 /// Enter an annotation token into the token stream.
1903 void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind,
1904 void *AnnotationVal);
1905
1906 /// Determine whether it's possible for a future call to Lex to produce an
1907 /// annotation token created by a previous call to EnterAnnotationToken.
1908 bool mightHavePendingAnnotationTokens() {
1909 return CurLexerCallback != CLK_Lexer;
1910 }
1911
1912 /// Update the current token to represent the provided
1913 /// identifier, in order to cache an action performed by typo correction.
1914 void TypoCorrectToken(const Token &Tok) {
1915 assert(Tok.getIdentifierInfo() && "Expected identifier token");
1916 if (CachedLexPos != 0 && isBacktrackEnabled())
1917 CachedTokens[CachedLexPos-1] = Tok;
1918 }
1919
1920 /// Recompute the current lexer kind based on the CurLexer/
1921 /// CurTokenLexer pointers.
1922 void recomputeCurLexerKind();
1923
1924 /// Returns true if incremental processing is enabled
1925 bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
1926
1927 /// Enables the incremental processing
1928 void enableIncrementalProcessing(bool value = true) {
1929 IncrementalProcessing = value;
1930 }
1931
1932 /// Specify the point at which code-completion will be performed.
1933 ///
1934 /// \param File the file in which code completion should occur. If
1935 /// this file is included multiple times, code-completion will
1936 /// perform completion the first time it is included. If NULL, this
1937 /// function clears out the code-completion point.
1938 ///
1939 /// \param Line the line at which code completion should occur
1940 /// (1-based).
1941 ///
1942 /// \param Column the column at which code completion should occur
1943 /// (1-based).
1944 ///
1945 /// \returns true if an error occurred, false otherwise.
1946 bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line,
1947 unsigned Column);
1948
1949 /// Determine if we are performing code completion.
1950 bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }
1951
1952 /// Returns the location of the code-completion point.
1953 ///
1954 /// Returns an invalid location if code-completion is not enabled or the file
1955 /// containing the code-completion point has not been lexed yet.
1956 SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
1957
1958 /// Returns the start location of the file of code-completion point.
1959 ///
1960 /// Returns an invalid location if code-completion is not enabled or the file
1961 /// containing the code-completion point has not been lexed yet.
1962 SourceLocation getCodeCompletionFileLoc() const {
1963 return CodeCompletionFileLoc;
1964 }
1965
1966 /// Returns true if code-completion is enabled and we have hit the
1967 /// code-completion point.
1968 bool isCodeCompletionReached() const { return CodeCompletionReached; }
1969
1970 /// Note that we hit the code-completion point.
1971 void setCodeCompletionReached() {
1972 assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
1973 CodeCompletionReached = true;
1974 // Silence any diagnostics that occur after we hit the code-completion.
1975 getDiagnostics().setSuppressAllDiagnostics(true);
1976 }
1977
1978 /// The location of the currently-active \#pragma clang
1979 /// arc_cf_code_audited begin.
1980 ///
1981 /// Returns an invalid location if there is no such pragma active.
1982 std::pair<IdentifierInfo *, SourceLocation>
1983 getPragmaARCCFCodeAuditedInfo() const {
1984 return PragmaARCCFCodeAuditedInfo;
1985 }
1986
1987 /// Set the location of the currently-active \#pragma clang
1988 /// arc_cf_code_audited begin. An invalid location ends the pragma.
1989 void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident,
1990 SourceLocation Loc) {
1991 PragmaARCCFCodeAuditedInfo = {Ident, Loc};
1992 }
1993
1994 /// The location of the currently-active \#pragma clang
1995 /// assume_nonnull begin.
1996 ///
1997 /// Returns an invalid location if there is no such pragma active.
1998 SourceLocation getPragmaAssumeNonNullLoc() const {
1999 return PragmaAssumeNonNullLoc;
2000 }
2001
2002 /// Set the location of the currently-active \#pragma clang
2003 /// assume_nonnull begin. An invalid location ends the pragma.
2004 void setPragmaAssumeNonNullLoc(SourceLocation Loc) {
2005 PragmaAssumeNonNullLoc = Loc;
2006 }
2007
2008 /// Get the location of the recorded unterminated \#pragma clang
2009 /// assume_nonnull begin in the preamble, if one exists.
2010 ///
2011 /// Returns an invalid location if the premable did not end with
2012 /// such a pragma active or if there is no recorded preamble.
2013 SourceLocation getPreambleRecordedPragmaAssumeNonNullLoc() const {
2014 return PreambleRecordedPragmaAssumeNonNullLoc;
2015 }
2016
2017 /// Record the location of the unterminated \#pragma clang
2018 /// assume_nonnull begin in the preamble.
2019 void setPreambleRecordedPragmaAssumeNonNullLoc(SourceLocation Loc) {
2020 PreambleRecordedPragmaAssumeNonNullLoc = Loc;
2021 }
2022
2023 /// Set the directory in which the main file should be considered
2024 /// to have been found, if it is not a real file.
2025 void setMainFileDir(DirectoryEntryRef Dir) { MainFileDir = Dir; }
2026
2027 /// Instruct the preprocessor to skip part of the main source file.
2028 ///
2029 /// \param Bytes The number of bytes in the preamble to skip.
2030 ///
2031 /// \param StartOfLine Whether skipping these bytes puts the lexer at the
2032 /// start of a line.
2033 void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
2034 SkipMainFilePreamble.first = Bytes;
2035 SkipMainFilePreamble.second = StartOfLine;
2036 }
2037
2038 /// Forwarding function for diagnostics. This emits a diagnostic at
2039 /// the specified Token's location, translating the token's start
2040 /// position in the current buffer into a SourcePosition object for rendering.
2041 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
2042 return Diags->Report(Loc, DiagID);
2043 }
2044
2045 DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
2046 return Diags->Report(Loc: Tok.getLocation(), DiagID);
2047 }
2048
2049 /// Return the 'spelling' of the token at the given
2050 /// location; does not go up to the spelling location or down to the
2051 /// expansion location.
2052 ///
2053 /// \param buffer A buffer which will be used only if the token requires
2054 /// "cleaning", e.g. if it contains trigraphs or escaped newlines
2055 /// \param invalid If non-null, will be set \c true if an error occurs.
2056 StringRef getSpelling(SourceLocation loc,
2057 SmallVectorImpl<char> &buffer,
2058 bool *invalid = nullptr) const {
2059 return Lexer::getSpelling(loc, buffer, SM: SourceMgr, options: LangOpts, invalid);
2060 }
2061
2062 /// Return the 'spelling' of the Tok token.
2063 ///
2064 /// The spelling of a token is the characters used to represent the token in
2065 /// the source file after trigraph expansion and escaped-newline folding. In
2066 /// particular, this wants to get the true, uncanonicalized, spelling of
2067 /// things like digraphs, UCNs, etc.
2068 ///
2069 /// \param Invalid If non-null, will be set \c true if an error occurs.
2070 std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const {
2071 return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
2072 }
2073
2074 /// Get the spelling of a token into a preallocated buffer, instead
2075 /// of as an std::string.
2076 ///
2077 /// The caller is required to allocate enough space for the token, which is
2078 /// guaranteed to be at least Tok.getLength() bytes long. The length of the
2079 /// actual result is returned.
2080 ///
2081 /// Note that this method may do two possible things: it may either fill in
2082 /// the buffer specified with characters, or it may *change the input pointer*
2083 /// to point to a constant buffer with the data already in it (avoiding a
2084 /// copy). The caller is not allowed to modify the returned buffer pointer
2085 /// if an internal buffer is returned.
2086 unsigned getSpelling(const Token &Tok, const char *&Buffer,
2087 bool *Invalid = nullptr) const {
2088 return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
2089 }
2090
2091 /// Get the spelling of a token into a SmallVector.
2092 ///
2093 /// Note that the returned StringRef may not point to the
2094 /// supplied buffer if a copy can be avoided.
2095 StringRef getSpelling(const Token &Tok,
2096 SmallVectorImpl<char> &Buffer,
2097 bool *Invalid = nullptr) const;
2098
2099 /// Relex the token at the specified location.
2100 /// \returns true if there was a failure, false on success.
2101 bool getRawToken(SourceLocation Loc, Token &Result,
2102 bool IgnoreWhiteSpace = false) {
2103 return Lexer::getRawToken(Loc, Result, SM: SourceMgr, LangOpts, IgnoreWhiteSpace);
2104 }
2105
2106 /// Given a Token \p Tok that is a numeric constant with length 1,
2107 /// return the character.
2108 char
2109 getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
2110 bool *Invalid = nullptr) const {
2111 assert(Tok.is(tok::numeric_constant) &&
2112 Tok.getLength() == 1 && "Called on unsupported token");
2113 assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
2114
2115 // If the token is carrying a literal data pointer, just use it.
2116 if (const char *D = Tok.getLiteralData())
2117 return *D;
2118
2119 // Otherwise, fall back on getCharacterData, which is slower, but always
2120 // works.
2121 return *SourceMgr.getCharacterData(SL: Tok.getLocation(), Invalid);
2122 }
2123
2124 /// Retrieve the name of the immediate macro expansion.
2125 ///
2126 /// This routine starts from a source location, and finds the name of the
2127 /// macro responsible for its immediate expansion. It looks through any
2128 /// intervening macro argument expansions to compute this. It returns a
2129 /// StringRef that refers to the SourceManager-owned buffer of the source
2130 /// where that macro name is spelled. Thus, the result shouldn't out-live
2131 /// the SourceManager.
2132 StringRef getImmediateMacroName(SourceLocation Loc) {
2133 return Lexer::getImmediateMacroName(Loc, SM: SourceMgr, LangOpts: getLangOpts());
2134 }
2135
2136 /// Plop the specified string into a scratch buffer and set the
2137 /// specified token's location and length to it.
2138 ///
2139 /// If specified, the source location provides a location of the expansion
2140 /// point of the token.
2141 void CreateString(StringRef Str, Token &Tok,
2142 SourceLocation ExpansionLocStart = SourceLocation(),
2143 SourceLocation ExpansionLocEnd = SourceLocation());
2144
2145 /// Split the first Length characters out of the token starting at TokLoc
2146 /// and return a location pointing to the split token. Re-lexing from the
2147 /// split token will return the split token rather than the original.
2148 SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length);
2149
2150 /// Computes the source location just past the end of the
2151 /// token at this source location.
2152 ///
2153 /// This routine can be used to produce a source location that
2154 /// points just past the end of the token referenced by \p Loc, and
2155 /// is generally used when a diagnostic needs to point just after a
2156 /// token where it expected something different that it received. If
2157 /// the returned source location would not be meaningful (e.g., if
2158 /// it points into a macro), this routine returns an invalid
2159 /// source location.
2160 ///
2161 /// \param Offset an offset from the end of the token, where the source
2162 /// location should refer to. The default offset (0) produces a source
2163 /// location pointing just past the end of the token; an offset of 1 produces
2164 /// a source location pointing to the last character in the token, etc.
2165 SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
2166 return Lexer::getLocForEndOfToken(Loc, Offset, SM: SourceMgr, LangOpts);
2167 }
2168
2169 /// Returns true if the given MacroID location points at the first
2170 /// token of the macro expansion.
2171 ///
2172 /// \param MacroBegin If non-null and function returns true, it is set to
2173 /// begin location of the macro.
2174 bool isAtStartOfMacroExpansion(SourceLocation loc,
2175 SourceLocation *MacroBegin = nullptr) const {
2176 return Lexer::isAtStartOfMacroExpansion(loc, SM: SourceMgr, LangOpts,
2177 MacroBegin);
2178 }
2179
2180 /// Returns true if the given MacroID location points at the last
2181 /// token of the macro expansion.
2182 ///
2183 /// \param MacroEnd If non-null and function returns true, it is set to
2184 /// end location of the macro.
2185 bool isAtEndOfMacroExpansion(SourceLocation loc,
2186 SourceLocation *MacroEnd = nullptr) const {
2187 return Lexer::isAtEndOfMacroExpansion(loc, SM: SourceMgr, LangOpts, MacroEnd);
2188 }
2189
2190 /// Print the token to stderr, used for debugging.
2191 void DumpToken(const Token &Tok, bool DumpFlags = false) const;
2192 void DumpLocation(SourceLocation Loc) const;
2193 void DumpMacro(const MacroInfo &MI) const;
2194 void dumpMacroInfo(const IdentifierInfo *II);
2195
2196 /// Given a location that specifies the start of a
2197 /// token, return a new location that specifies a character within the token.
2198 SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
2199 unsigned Char) const {
2200 return Lexer::AdvanceToTokenCharacter(TokStart, Characters: Char, SM: SourceMgr, LangOpts);
2201 }
2202
2203 /// Increment the counters for the number of token paste operations
2204 /// performed.
2205 ///
2206 /// If fast was specified, this is a 'fast paste' case we handled.
2207 void IncrementPasteCounter(bool isFast) {
2208 if (isFast)
2209 ++NumFastTokenPaste;
2210 else
2211 ++NumTokenPaste;
2212 }
2213
2214 void PrintStats();
2215
2216 size_t getTotalMemory() const;
2217
2218 /// When the macro expander pastes together a comment (/##/) in Microsoft
2219 /// mode, this method handles updating the current state, returning the
2220 /// token on the next source line.
2221 void HandleMicrosoftCommentPaste(Token &Tok);
2222
2223 //===--------------------------------------------------------------------===//
2224 // Preprocessor callback methods. These are invoked by a lexer as various
2225 // directives and events are found.
2226
2227 /// Given a tok::raw_identifier token, look up the
2228 /// identifier information for the token and install it into the token,
2229 /// updating the token kind accordingly.
2230 IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
2231
2232private:
2233 llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
2234
2235public:
2236 /// Specifies the reason for poisoning an identifier.
2237 ///
2238 /// If that identifier is accessed while poisoned, then this reason will be
2239 /// used instead of the default "poisoned" diagnostic.
2240 void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
2241
2242 /// Display reason for poisoned identifier.
2243 void HandlePoisonedIdentifier(Token & Identifier);
2244
2245 void MaybeHandlePoisonedIdentifier(Token & Identifier) {
2246 if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
2247 if(II->isPoisoned()) {
2248 HandlePoisonedIdentifier(Identifier);
2249 }
2250 }
2251 }
2252
2253private:
2254 /// Identifiers used for SEH handling in Borland. These are only
2255 /// allowed in particular circumstances
2256 // __except block
2257 IdentifierInfo *Ident__exception_code,
2258 *Ident___exception_code,
2259 *Ident_GetExceptionCode;
2260 // __except filter expression
2261 IdentifierInfo *Ident__exception_info,
2262 *Ident___exception_info,
2263 *Ident_GetExceptionInfo;
2264 // __finally
2265 IdentifierInfo *Ident__abnormal_termination,
2266 *Ident___abnormal_termination,
2267 *Ident_AbnormalTermination;
2268
2269 const char *getCurLexerEndPos();
2270 void diagnoseMissingHeaderInUmbrellaDir(const Module &Mod);
2271
2272public:
2273 void PoisonSEHIdentifiers(bool Poison = true); // Borland
2274
2275 /// Callback invoked when the lexer reads an identifier and has
2276 /// filled in the tokens IdentifierInfo member.
2277 ///
2278 /// This callback potentially macro expands it or turns it into a named
2279 /// token (like 'for').
2280 ///
2281 /// \returns true if we actually computed a token, false if we need to
2282 /// lex again.
2283 bool HandleIdentifier(Token &Identifier);
2284
2285 /// Callback invoked when the lexer hits the end of the current file.
2286 ///
2287 /// This either returns the EOF token and returns true, or
2288 /// pops a level off the include stack and returns false, at which point the
2289 /// client should call lex again.
2290 bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
2291
2292 /// Callback invoked when the current TokenLexer hits the end of its
2293 /// token stream.
2294 bool HandleEndOfTokenLexer(Token &Result);
2295
2296 /// Callback invoked when the lexer sees a # token at the start of a
2297 /// line.
2298 ///
2299 /// This consumes the directive, modifies the lexer/preprocessor state, and
2300 /// advances the lexer(s) so that the next token read is the correct one.
2301 void HandleDirective(Token &Result);
2302
2303 /// Ensure that the next token is a tok::eod token.
2304 ///
2305 /// If not, emit a diagnostic and consume up until the eod.
2306 /// If \p EnableMacros is true, then we consider macros that expand to zero
2307 /// tokens as being ok.
2308 ///
2309 /// \return The location of the end of the directive (the terminating
2310 /// newline).
2311 SourceLocation CheckEndOfDirective(const char *DirType,
2312 bool EnableMacros = false);
2313
2314 /// Read and discard all tokens remaining on the current line until
2315 /// the tok::eod token is found. Returns the range of the skipped tokens.
2316 SourceRange DiscardUntilEndOfDirective();
2317
2318 /// Returns true if the preprocessor has seen a use of
2319 /// __DATE__ or __TIME__ in the file so far.
2320 bool SawDateOrTime() const {
2321 return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
2322 }
2323 unsigned getCounterValue() const { return CounterValue; }
2324 void setCounterValue(unsigned V) { CounterValue = V; }
2325
2326 LangOptions::FPEvalMethodKind getCurrentFPEvalMethod() const {
2327 assert(CurrentFPEvalMethod != LangOptions::FEM_UnsetOnCommandLine &&
2328 "FPEvalMethod should be set either from command line or from the "
2329 "target info");
2330 return CurrentFPEvalMethod;
2331 }
2332
2333 LangOptions::FPEvalMethodKind getTUFPEvalMethod() const {
2334 return TUFPEvalMethod;
2335 }
2336
2337 SourceLocation getLastFPEvalPragmaLocation() const {
2338 return LastFPEvalPragmaLocation;
2339 }
2340
2341 void setCurrentFPEvalMethod(SourceLocation PragmaLoc,
2342 LangOptions::FPEvalMethodKind Val) {
2343 assert(Val != LangOptions::FEM_UnsetOnCommandLine &&
2344 "FPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2345 // This is the location of the '#pragma float_control" where the
2346 // execution state is modifed.
2347 LastFPEvalPragmaLocation = PragmaLoc;
2348 CurrentFPEvalMethod = Val;
2349 TUFPEvalMethod = Val;
2350 }
2351
2352 void setTUFPEvalMethod(LangOptions::FPEvalMethodKind Val) {
2353 assert(Val != LangOptions::FEM_UnsetOnCommandLine &&
2354 "TUPEvalMethod should never be set to FEM_UnsetOnCommandLine");
2355 TUFPEvalMethod = Val;
2356 }
2357
2358 /// Retrieves the module that we're currently building, if any.
2359 Module *getCurrentModule();
2360
2361 /// Retrieves the module whose implementation we're current compiling, if any.
2362 Module *getCurrentModuleImplementation();
2363
2364 /// If we are preprocessing a named module.
2365 bool isInNamedModule() const { return ModuleDeclState.isNamedModule(); }
2366
2367 /// If we are proprocessing a named interface unit.
2368 /// Note that a module implementation partition is not considered as an
2369 /// named interface unit here although it is importable
2370 /// to ease the parsing.
2371 bool isInNamedInterfaceUnit() const {
2372 return ModuleDeclState.isNamedInterface();
2373 }
2374
2375 /// Get the named module name we're preprocessing.
2376 /// Requires we're preprocessing a named module.
2377 StringRef getNamedModuleName() const { return ModuleDeclState.getName(); }
2378
2379 /// If we are implementing an implementation module unit.
2380 /// Note that the module implementation partition is not considered as an
2381 /// implementation unit.
2382 bool isInImplementationUnit() const {
2383 return ModuleDeclState.isImplementationUnit();
2384 }
2385
2386 /// If we're importing a standard C++20 Named Modules.
2387 bool isInImportingCXXNamedModules() const {
2388 // NamedModuleImportPath will be non-empty only if we're importing
2389 // Standard C++ named modules.
2390 return !NamedModuleImportPath.empty() && getLangOpts().CPlusPlusModules &&
2391 !IsAtImport;
2392 }
2393
2394 /// Allocate a new MacroInfo object with the provided SourceLocation.
2395 MacroInfo *AllocateMacroInfo(SourceLocation L);
2396
2397 /// Turn the specified lexer token into a fully checked and spelled
2398 /// filename, e.g. as an operand of \#include.
2399 ///
2400 /// The caller is expected to provide a buffer that is large enough to hold
2401 /// the spelling of the filename, but is also expected to handle the case
2402 /// when this method decides to use a different buffer.
2403 ///
2404 /// \returns true if the input filename was in <>'s or false if it was
2405 /// in ""'s.
2406 bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Buffer);
2407
2408 /// Given a "foo" or \<foo> reference, look up the indicated file.
2409 ///
2410 /// Returns std::nullopt on failure. \p isAngled indicates whether the file
2411 /// reference is for system \#include's or not (i.e. using <> instead of "").
2412 OptionalFileEntryRef
2413 LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
2414 ConstSearchDirIterator FromDir, const FileEntry *FromFile,
2415 ConstSearchDirIterator *CurDir, SmallVectorImpl<char> *SearchPath,
2416 SmallVectorImpl<char> *RelativePath,
2417 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
2418 bool *IsFrameworkFound, bool SkipCache = false,
2419 bool OpenFile = true, bool CacheFailures = true);
2420
2421 /// Return true if we're in the top-level file, not in a \#include.
2422 bool isInPrimaryFile() const;
2423
2424 /// Lex an on-off-switch (C99 6.10.6p2) and verify that it is
2425 /// followed by EOD. Return true if the token is not a valid on-off-switch.
2426 bool LexOnOffSwitch(tok::OnOffSwitch &Result);
2427
2428 bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
2429 bool *ShadowFlag = nullptr);
2430
2431 void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma);
2432 Module *LeaveSubmodule(bool ForPragma);
2433
2434private:
2435 friend void TokenLexer::ExpandFunctionArguments();
2436
2437 void PushIncludeMacroStack() {
2438 assert(CurLexerCallback != CLK_CachingLexer &&
2439 "cannot push a caching lexer");
2440 IncludeMacroStack.emplace_back(args&: CurLexerCallback, args&: CurLexerSubmodule,
2441 args: std::move(CurLexer), args&: CurPPLexer,
2442 args: std::move(CurTokenLexer), args&: CurDirLookup);
2443 CurPPLexer = nullptr;
2444 }
2445
2446 void PopIncludeMacroStack() {
2447 CurLexer = std::move(IncludeMacroStack.back().TheLexer);
2448 CurPPLexer = IncludeMacroStack.back().ThePPLexer;
2449 CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
2450 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
2451 CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule;
2452 CurLexerCallback = IncludeMacroStack.back().CurLexerCallback;
2453 IncludeMacroStack.pop_back();
2454 }
2455
2456 void PropagateLineStartLeadingSpaceInfo(Token &Result);
2457
2458 /// Determine whether we need to create module macros for #defines in the
2459 /// current context.
2460 bool needModuleMacros() const;
2461
2462 /// Update the set of active module macros and ambiguity flag for a module
2463 /// macro name.
2464 void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
2465
2466 DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
2467 SourceLocation Loc);
2468 UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
2469 VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
2470 bool isPublic);
2471
2472 /// Lex and validate a macro name, which occurs after a
2473 /// \#define or \#undef.
2474 ///
2475 /// \param MacroNameTok Token that represents the name defined or undefined.
2476 /// \param IsDefineUndef Kind if preprocessor directive.
2477 /// \param ShadowFlag Points to flag that is set if macro name shadows
2478 /// a keyword.
2479 ///
2480 /// This emits a diagnostic, sets the token kind to eod,
2481 /// and discards the rest of the macro line if the macro name is invalid.
2482 void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other,
2483 bool *ShadowFlag = nullptr);
2484
2485 /// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2486 /// entire line) of the macro's tokens and adds them to MacroInfo, and while
2487 /// doing so performs certain validity checks including (but not limited to):
2488 /// - # (stringization) is followed by a macro parameter
2489 /// \param MacroNameTok - Token that represents the macro name
2490 /// \param ImmediatelyAfterHeaderGuard - Macro follows an #ifdef header guard
2491 ///
2492 /// Either returns a pointer to a MacroInfo object OR emits a diagnostic and
2493 /// returns a nullptr if an invalid sequence of tokens is encountered.
2494 MacroInfo *ReadOptionalMacroParameterListAndBody(
2495 const Token &MacroNameTok, bool ImmediatelyAfterHeaderGuard);
2496
2497 /// The ( starting an argument list of a macro definition has just been read.
2498 /// Lex the rest of the parameters and the closing ), updating \p MI with
2499 /// what we learn and saving in \p LastTok the last token read.
2500 /// Return true if an error occurs parsing the arg list.
2501 bool ReadMacroParameterList(MacroInfo *MI, Token& LastTok);
2502
2503 /// Provide a suggestion for a typoed directive. If there is no typo, then
2504 /// just skip suggesting.
2505 ///
2506 /// \param Tok - Token that represents the directive
2507 /// \param Directive - String reference for the directive name
2508 void SuggestTypoedDirective(const Token &Tok, StringRef Directive) const;
2509
2510 /// We just read a \#if or related directive and decided that the
2511 /// subsequent tokens are in the \#if'd out portion of the
2512 /// file. Lex the rest of the file, until we see an \#endif. If \p
2513 /// FoundNonSkipPortion is true, then we have already emitted code for part of
2514 /// this \#if directive, so \#else/\#elif blocks should never be entered. If
2515 /// \p FoundElse is false, then \#else directives are ok, if not, then we have
2516 /// already seen one so a \#else directive is a duplicate. When this returns,
2517 /// the caller can lex the first valid token.
2518 void SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
2519 SourceLocation IfTokenLoc,
2520 bool FoundNonSkipPortion, bool FoundElse,
2521 SourceLocation ElseLoc = SourceLocation());
2522
2523 /// Information about the result for evaluating an expression for a
2524 /// preprocessor directive.
2525 struct DirectiveEvalResult {
2526 /// Whether the expression was evaluated as true or not.
2527 bool Conditional;
2528
2529 /// True if the expression contained identifiers that were undefined.
2530 bool IncludedUndefinedIds;
2531
2532 /// The source range for the expression.
2533 SourceRange ExprRange;
2534 };
2535
2536 /// Evaluate an integer constant expression that may occur after a
2537 /// \#if or \#elif directive and return a \p DirectiveEvalResult object.
2538 ///
2539 /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
2540 DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
2541
2542 /// Process a '__has_include("path")' expression.
2543 ///
2544 /// Returns true if successful.
2545 bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II);
2546
2547 /// Process '__has_include_next("path")' expression.
2548 ///
2549 /// Returns true if successful.
2550 bool EvaluateHasIncludeNext(Token &Tok, IdentifierInfo *II);
2551
2552 /// Get the directory and file from which to start \#include_next lookup.
2553 std::pair<ConstSearchDirIterator, const FileEntry *>
2554 getIncludeNextStart(const Token &IncludeNextTok) const;
2555
2556 /// Install the standard preprocessor pragmas:
2557 /// \#pragma GCC poison/system_header/dependency and \#pragma once.
2558 void RegisterBuiltinPragmas();
2559
2560 /// Register builtin macros such as __LINE__ with the identifier table.
2561 void RegisterBuiltinMacros();
2562
2563 /// If an identifier token is read that is to be expanded as a macro, handle
2564 /// it and return the next token as 'Tok'. If we lexed a token, return true;
2565 /// otherwise the caller should lex again.
2566 bool HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &MD);
2567
2568 /// Cache macro expanded tokens for TokenLexers.
2569 //
2570 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
2571 /// going to lex in the cache and when it finishes the tokens are removed
2572 /// from the end of the cache.
2573 Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
2574 ArrayRef<Token> tokens);
2575
2576 void removeCachedMacroExpandedTokensOfLastLexer();
2577
2578 /// Determine whether the next preprocessor token to be
2579 /// lexed is a '('. If so, consume the token and return true, if not, this
2580 /// method should have no observable side-effect on the lexed tokens.
2581 bool isNextPPTokenLParen();
2582
2583 /// After reading "MACRO(", this method is invoked to read all of the formal
2584 /// arguments specified for the macro invocation. Returns null on error.
2585 MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI,
2586 SourceLocation &MacroEnd);
2587
2588 /// If an identifier token is read that is to be expanded
2589 /// as a builtin macro, handle it and return the next token as 'Tok'.
2590 void ExpandBuiltinMacro(Token &Tok);
2591
2592 /// Read a \c _Pragma directive, slice it up, process it, then
2593 /// return the first token after the directive.
2594 /// This assumes that the \c _Pragma token has just been read into \p Tok.
2595 void Handle_Pragma(Token &Tok);
2596
2597 /// Like Handle_Pragma except the pragma text is not enclosed within
2598 /// a string literal.
2599 void HandleMicrosoft__pragma(Token &Tok);
2600
2601 /// Add a lexer to the top of the include stack and
2602 /// start lexing tokens from it instead of the current buffer.
2603 void EnterSourceFileWithLexer(Lexer *TheLexer, ConstSearchDirIterator Dir);
2604
2605 /// Set the FileID for the preprocessor predefines.
2606 void setPredefinesFileID(FileID FID) {
2607 assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
2608 PredefinesFileID = FID;
2609 }
2610
2611 /// Set the FileID for the PCH through header.
2612 void setPCHThroughHeaderFileID(FileID FID);
2613
2614 /// Returns true if we are lexing from a file and not a
2615 /// pragma or a macro.
2616 static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
2617 return L ? !L->isPragmaLexer() : P != nullptr;
2618 }
2619
2620 static bool IsFileLexer(const IncludeStackInfo& I) {
2621 return IsFileLexer(L: I.TheLexer.get(), P: I.ThePPLexer);
2622 }
2623
2624 bool IsFileLexer() const {
2625 return IsFileLexer(L: CurLexer.get(), P: CurPPLexer);
2626 }
2627
2628 //===--------------------------------------------------------------------===//
2629 // Caching stuff.
2630 void CachingLex(Token &Result);
2631
2632 bool InCachingLexMode() const {
2633 // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
2634 // that we are past EOF, not that we are in CachingLex mode.
2635 return !CurPPLexer && !CurTokenLexer && !IncludeMacroStack.empty();
2636 }
2637
2638 void EnterCachingLexMode();
2639 void EnterCachingLexModeUnchecked();
2640
2641 void ExitCachingLexMode() {
2642 if (InCachingLexMode())
2643 RemoveTopOfLexerStack();
2644 }
2645
2646 const Token &PeekAhead(unsigned N);
2647 void AnnotatePreviousCachedTokens(const Token &Tok);
2648
2649 //===--------------------------------------------------------------------===//
2650 /// Handle*Directive - implement the various preprocessor directives. These
2651 /// should side-effect the current preprocessor object so that the next call
2652 /// to Lex() will return the appropriate token next.
2653 void HandleLineDirective();
2654 void HandleDigitDirective(Token &Tok);
2655 void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
2656 void HandleIdentSCCSDirective(Token &Tok);
2657 void HandleMacroPublicDirective(Token &Tok);
2658 void HandleMacroPrivateDirective();
2659
2660 /// An additional notification that can be produced by a header inclusion or
2661 /// import to tell the parser what happened.
2662 struct ImportAction {
2663 enum ActionKind {
2664 None,
2665 ModuleBegin,
2666 ModuleImport,
2667 HeaderUnitImport,
2668 SkippedModuleImport,
2669 Failure,
2670 } Kind;
2671 Module *ModuleForHeader = nullptr;
2672
2673 ImportAction(ActionKind AK, Module *Mod = nullptr)
2674 : Kind(AK), ModuleForHeader(Mod) {
2675 assert((AK == None || Mod || AK == Failure) &&
2676 "no module for module action");
2677 }
2678 };
2679
2680 OptionalFileEntryRef LookupHeaderIncludeOrImport(
2681 ConstSearchDirIterator *CurDir, StringRef &Filename,
2682 SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2683 const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2684 bool &IsMapped, ConstSearchDirIterator LookupFrom,
2685 const FileEntry *LookupFromFile, StringRef &LookupFilename,
2686 SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2687 ModuleMap::KnownHeader &SuggestedModule, bool isAngled);
2688
2689 // File inclusion.
2690 void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok,
2691 ConstSearchDirIterator LookupFrom = nullptr,
2692 const FileEntry *LookupFromFile = nullptr);
2693 ImportAction
2694 HandleHeaderIncludeOrImport(SourceLocation HashLoc, Token &IncludeTok,
2695 Token &FilenameTok, SourceLocation EndLoc,
2696 ConstSearchDirIterator LookupFrom = nullptr,
2697 const FileEntry *LookupFromFile = nullptr);
2698 void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
2699 void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
2700 void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
2701 void HandleMicrosoftImportDirective(Token &Tok);
2702
2703public:
2704 /// Check that the given module is available, producing a diagnostic if not.
2705 /// \return \c true if the check failed (because the module is not available).
2706 /// \c false if the module appears to be usable.
2707 static bool checkModuleIsAvailable(const LangOptions &LangOpts,
2708 const TargetInfo &TargetInfo,
2709 const Module &M, DiagnosticsEngine &Diags);
2710
2711 // Module inclusion testing.
2712 /// Find the module that owns the source or header file that
2713 /// \p Loc points to. If the location is in a file that was included
2714 /// into a module, or is outside any module, returns nullptr.
2715 Module *getModuleForLocation(SourceLocation Loc, bool AllowTextual);
2716
2717 /// We want to produce a diagnostic at location IncLoc concerning an
2718 /// unreachable effect at location MLoc (eg, where a desired entity was
2719 /// declared or defined). Determine whether the right way to make MLoc
2720 /// reachable is by #include, and if so, what header should be included.
2721 ///
2722 /// This is not necessarily fast, and might load unexpected module maps, so
2723 /// should only be called by code that intends to produce an error.
2724 ///
2725 /// \param IncLoc The location at which the missing effect was detected.
2726 /// \param MLoc A location within an unimported module at which the desired
2727 /// effect occurred.
2728 /// \return A file that can be #included to provide the desired effect. Null
2729 /// if no such file could be determined or if a #include is not
2730 /// appropriate (eg, if a module should be imported instead).
2731 OptionalFileEntryRef getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
2732 SourceLocation MLoc);
2733
2734 bool isRecordingPreamble() const {
2735 return PreambleConditionalStack.isRecording();
2736 }
2737
2738 bool hasRecordedPreamble() const {
2739 return PreambleConditionalStack.hasRecordedPreamble();
2740 }
2741
2742 ArrayRef<PPConditionalInfo> getPreambleConditionalStack() const {
2743 return PreambleConditionalStack.getStack();
2744 }
2745
2746 void setRecordedPreambleConditionalStack(ArrayRef<PPConditionalInfo> s) {
2747 PreambleConditionalStack.setStack(s);
2748 }
2749
2750 void setReplayablePreambleConditionalStack(
2751 ArrayRef<PPConditionalInfo> s, std::optional<PreambleSkipInfo> SkipInfo) {
2752 PreambleConditionalStack.startReplaying();
2753 PreambleConditionalStack.setStack(s);
2754 PreambleConditionalStack.SkipInfo = SkipInfo;
2755 }
2756
2757 std::optional<PreambleSkipInfo> getPreambleSkipInfo() const {
2758 return PreambleConditionalStack.SkipInfo;
2759 }
2760
2761private:
2762 /// After processing predefined file, initialize the conditional stack from
2763 /// the preamble.
2764 void replayPreambleConditionalStack();
2765
2766 // Macro handling.
2767 void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
2768 void HandleUndefDirective();
2769
2770 // Conditional Inclusion.
2771 void HandleIfdefDirective(Token &Result, const Token &HashToken,
2772 bool isIfndef, bool ReadAnyTokensBeforeDirective);
2773 void HandleIfDirective(Token &IfToken, const Token &HashToken,
2774 bool ReadAnyTokensBeforeDirective);
2775 void HandleEndifDirective(Token &EndifToken);
2776 void HandleElseDirective(Token &Result, const Token &HashToken);
2777 void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken,
2778 tok::PPKeywordKind Kind);
2779
2780 // Pragmas.
2781 void HandlePragmaDirective(PragmaIntroducer Introducer);
2782
2783public:
2784 void HandlePragmaOnce(Token &OnceTok);
2785 void HandlePragmaMark(Token &MarkTok);
2786 void HandlePragmaPoison();
2787 void HandlePragmaSystemHeader(Token &SysHeaderTok);
2788 void HandlePragmaDependency(Token &DependencyTok);
2789 void HandlePragmaPushMacro(Token &Tok);
2790 void HandlePragmaPopMacro(Token &Tok);
2791 void HandlePragmaIncludeAlias(Token &Tok);
2792 void HandlePragmaModuleBuild(Token &Tok);
2793 void HandlePragmaHdrstop(Token &Tok);
2794 IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
2795
2796 // Return true and store the first token only if any CommentHandler
2797 // has inserted some tokens and getCommentRetentionState() is false.
2798 bool HandleComment(Token &result, SourceRange Comment);
2799
2800 /// A macro is used, update information about macros that need unused
2801 /// warnings.
2802 void markMacroAsUsed(MacroInfo *MI);
2803
2804 void addMacroDeprecationMsg(const IdentifierInfo *II, std::string Msg,
2805 SourceLocation AnnotationLoc) {
2806 auto Annotations = AnnotationInfos.find(Val: II);
2807 if (Annotations == AnnotationInfos.end())
2808 AnnotationInfos.insert(KV: std::make_pair(
2809 x&: II,
2810 y: MacroAnnotations::makeDeprecation(Loc: AnnotationLoc, Msg: std::move(Msg))));
2811 else
2812 Annotations->second.DeprecationInfo =
2813 MacroAnnotationInfo{.Location: AnnotationLoc, .Message: std::move(Msg)};
2814 }
2815
2816 void addRestrictExpansionMsg(const IdentifierInfo *II, std::string Msg,
2817 SourceLocation AnnotationLoc) {
2818 auto Annotations = AnnotationInfos.find(Val: II);
2819 if (Annotations == AnnotationInfos.end())
2820 AnnotationInfos.insert(
2821 KV: std::make_pair(x&: II, y: MacroAnnotations::makeRestrictExpansion(
2822 Loc: AnnotationLoc, Msg: std::move(Msg))));
2823 else
2824 Annotations->second.RestrictExpansionInfo =
2825 MacroAnnotationInfo{.Location: AnnotationLoc, .Message: std::move(Msg)};
2826 }
2827
2828 void addFinalLoc(const IdentifierInfo *II, SourceLocation AnnotationLoc) {
2829 auto Annotations = AnnotationInfos.find(Val: II);
2830 if (Annotations == AnnotationInfos.end())
2831 AnnotationInfos.insert(
2832 KV: std::make_pair(x&: II, y: MacroAnnotations::makeFinal(Loc: AnnotationLoc)));
2833 else
2834 Annotations->second.FinalAnnotationLoc = AnnotationLoc;
2835 }
2836
2837 const MacroAnnotations &getMacroAnnotations(const IdentifierInfo *II) const {
2838 return AnnotationInfos.find(Val: II)->second;
2839 }
2840
2841 void emitMacroExpansionWarnings(const Token &Identifier,
2842 bool IsIfnDef = false) const {
2843 IdentifierInfo *Info = Identifier.getIdentifierInfo();
2844 if (Info->isDeprecatedMacro())
2845 emitMacroDeprecationWarning(Identifier);
2846
2847 if (Info->isRestrictExpansion() &&
2848 !SourceMgr.isInMainFile(Loc: Identifier.getLocation()))
2849 emitRestrictExpansionWarning(Identifier);
2850
2851 if (!IsIfnDef) {
2852 if (Info->getName() == "INFINITY" && getLangOpts().NoHonorInfs)
2853 emitRestrictInfNaNWarning(Identifier, DiagSelection: 0);
2854 if (Info->getName() == "NAN" && getLangOpts().NoHonorNaNs)
2855 emitRestrictInfNaNWarning(Identifier, DiagSelection: 1);
2856 }
2857 }
2858
2859 static void processPathForFileMacro(SmallVectorImpl<char> &Path,
2860 const LangOptions &LangOpts,
2861 const TargetInfo &TI);
2862
2863 static void processPathToFileName(SmallVectorImpl<char> &FileName,
2864 const PresumedLoc &PLoc,
2865 const LangOptions &LangOpts,
2866 const TargetInfo &TI);
2867
2868private:
2869 void emitMacroDeprecationWarning(const Token &Identifier) const;
2870 void emitRestrictExpansionWarning(const Token &Identifier) const;
2871 void emitFinalMacroWarning(const Token &Identifier, bool IsUndef) const;
2872 void emitRestrictInfNaNWarning(const Token &Identifier,
2873 unsigned DiagSelection) const;
2874
2875 /// This boolean state keeps track if the current scanned token (by this PP)
2876 /// is in an "-Wunsafe-buffer-usage" opt-out region. Assuming PP scans a
2877 /// translation unit in a linear order.
2878 bool InSafeBufferOptOutRegion = false;
2879
2880 /// Hold the start location of the current "-Wunsafe-buffer-usage" opt-out
2881 /// region if PP is currently in such a region. Hold undefined value
2882 /// otherwise.
2883 SourceLocation CurrentSafeBufferOptOutStart; // It is used to report the start location of an never-closed region.
2884
2885 // An ordered sequence of "-Wunsafe-buffer-usage" opt-out regions in one
2886 // translation unit. Each region is represented by a pair of start and end
2887 // locations. A region is "open" if its' start and end locations are
2888 // identical.
2889 SmallVector<std::pair<SourceLocation, SourceLocation>, 8> SafeBufferOptOutMap;
2890
2891public:
2892 /// \return true iff the given `Loc` is in a "-Wunsafe-buffer-usage" opt-out
2893 /// region. This `Loc` must be a source location that has been pre-processed.
2894 bool isSafeBufferOptOut(const SourceManager&SourceMgr, const SourceLocation &Loc) const;
2895
2896 /// Alter the state of whether this PP currently is in a
2897 /// "-Wunsafe-buffer-usage" opt-out region.
2898 ///
2899 /// \param isEnter true if this PP is entering a region; otherwise, this PP
2900 /// is exiting a region
2901 /// \param Loc the location of the entry or exit of a
2902 /// region
2903 /// \return true iff it is INVALID to enter or exit a region, i.e.,
2904 /// attempt to enter a region before exiting a previous region, or exiting a
2905 /// region that PP is not currently in.
2906 bool enterOrExitSafeBufferOptOutRegion(bool isEnter,
2907 const SourceLocation &Loc);
2908
2909 /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2910 /// opt-out region
2911 bool isPPInSafeBufferOptOutRegion();
2912
2913 /// \param StartLoc output argument. It will be set to the start location of
2914 /// the current "-Wunsafe-buffer-usage" opt-out region iff this function
2915 /// returns true.
2916 /// \return true iff this PP is currently in a "-Wunsafe-buffer-usage"
2917 /// opt-out region
2918 bool isPPInSafeBufferOptOutRegion(SourceLocation &StartLoc);
2919
2920private:
2921 /// Helper functions to forward lexing to the actual lexer. They all share the
2922 /// same signature.
2923 static bool CLK_Lexer(Preprocessor &P, Token &Result) {
2924 return P.CurLexer->Lex(Result);
2925 }
2926 static bool CLK_TokenLexer(Preprocessor &P, Token &Result) {
2927 return P.CurTokenLexer->Lex(Tok&: Result);
2928 }
2929 static bool CLK_CachingLexer(Preprocessor &P, Token &Result) {
2930 P.CachingLex(Result);
2931 return true;
2932 }
2933 static bool CLK_DependencyDirectivesLexer(Preprocessor &P, Token &Result) {
2934 return P.CurLexer->LexDependencyDirectiveToken(Result);
2935 }
2936 static bool CLK_LexAfterModuleImport(Preprocessor &P, Token &Result) {
2937 return P.LexAfterModuleImport(Result);
2938 }
2939};
2940
2941/// Abstract base class that describes a handler that will receive
2942/// source ranges for each of the comments encountered in the source file.
2943class CommentHandler {
2944public:
2945 virtual ~CommentHandler();
2946
2947 // The handler shall return true if it has pushed any tokens
2948 // to be read using e.g. EnterToken or EnterTokenStream.
2949 virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
2950};
2951
2952/// Abstract base class that describes a handler that will receive
2953/// source ranges for empty lines encountered in the source file.
2954class EmptylineHandler {
2955public:
2956 virtual ~EmptylineHandler();
2957
2958 // The handler handles empty lines.
2959 virtual void HandleEmptyline(SourceRange Range) = 0;
2960};
2961
2962/// Registry of pragma handlers added by plugins
2963using PragmaHandlerRegistry = llvm::Registry<PragmaHandler>;
2964
2965} // namespace clang
2966
2967#endif // LLVM_CLANG_LEX_PREPROCESSOR_H
2968

source code of clang/include/clang/Lex/Preprocessor.h