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