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