]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Lex/Preprocessor.h
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Lex / Preprocessor.h
1 //===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Defines the clang::Preprocessor interface.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
16 #define LLVM_CLANG_LEX_PREPROCESSOR_H
17
18 #include "clang/Basic/Builtins.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/IdentifierTable.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "clang/Lex/Lexer.h"
23 #include "clang/Lex/MacroInfo.h"
24 #include "clang/Lex/ModuleMap.h"
25 #include "clang/Lex/PPCallbacks.h"
26 #include "clang/Lex/PTHLexer.h"
27 #include "clang/Lex/TokenLexer.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/IntrusiveRefCntPtr.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/TinyPtrVector.h"
34 #include "llvm/Support/Allocator.h"
35 #include "llvm/Support/Registry.h"
36 #include <memory>
37 #include <vector>
38
39 namespace llvm {
40   template<unsigned InternalLen> class SmallString;
41 }
42
43 namespace clang {
44
45 class SourceManager;
46 class ExternalPreprocessorSource;
47 class FileManager;
48 class FileEntry;
49 class HeaderSearch;
50 class PragmaNamespace;
51 class PragmaHandler;
52 class CommentHandler;
53 class ScratchBuffer;
54 class TargetInfo;
55 class PPCallbacks;
56 class CodeCompletionHandler;
57 class DirectoryLookup;
58 class PreprocessingRecord;
59 class ModuleLoader;
60 class PTHManager;
61 class PreprocessorOptions;
62
63 /// \brief Stores token information for comparing actual tokens with
64 /// predefined values.  Only handles simple tokens and identifiers.
65 class TokenValue {
66   tok::TokenKind Kind;
67   IdentifierInfo *II;
68
69 public:
70   TokenValue(tok::TokenKind Kind) : Kind(Kind), II(nullptr) {
71     assert(Kind != tok::raw_identifier && "Raw identifiers are not supported.");
72     assert(Kind != tok::identifier &&
73            "Identifiers should be created by TokenValue(IdentifierInfo *)");
74     assert(!tok::isLiteral(Kind) && "Literals are not supported.");
75     assert(!tok::isAnnotation(Kind) && "Annotations are not supported.");
76   }
77   TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
78   bool operator==(const Token &Tok) const {
79     return Tok.getKind() == Kind &&
80         (!II || II == Tok.getIdentifierInfo());
81   }
82 };
83
84 /// \brief Context in which macro name is used.
85 enum MacroUse {
86   MU_Other  = 0,  // other than #define or #undef
87   MU_Define = 1,  // macro name specified in #define
88   MU_Undef  = 2   // macro name specified in #undef
89 };
90
91 /// \brief Engages in a tight little dance with the lexer to efficiently
92 /// preprocess tokens.
93 ///
94 /// Lexers know only about tokens within a single source file, and don't
95 /// know anything about preprocessor-level issues like the \#include stack,
96 /// token expansion, etc.
97 class Preprocessor {
98   std::shared_ptr<PreprocessorOptions> PPOpts;
99   DiagnosticsEngine        *Diags;
100   LangOptions       &LangOpts;
101   const TargetInfo  *Target;
102   const TargetInfo  *AuxTarget;
103   FileManager       &FileMgr;
104   SourceManager     &SourceMgr;
105   std::unique_ptr<ScratchBuffer> ScratchBuf;
106   HeaderSearch      &HeaderInfo;
107   ModuleLoader      &TheModuleLoader;
108
109   /// \brief External source of macros.
110   ExternalPreprocessorSource *ExternalSource;
111
112
113   /// An optional PTHManager object used for getting tokens from
114   /// a token cache rather than lexing the original source file.
115   std::unique_ptr<PTHManager> PTH;
116
117   /// A BumpPtrAllocator object used to quickly allocate and release
118   /// objects internal to the Preprocessor.
119   llvm::BumpPtrAllocator BP;
120
121   /// Identifiers for builtin macros and other builtins.
122   IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
123   IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
124   IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
125   IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
126   IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
127   IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
128   IdentifierInfo *Ident_Pragma, *Ident__pragma;    // _Pragma, __pragma
129   IdentifierInfo *Ident__identifier;               // __identifier
130   IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
131   IdentifierInfo *Ident__has_feature;              // __has_feature
132   IdentifierInfo *Ident__has_extension;            // __has_extension
133   IdentifierInfo *Ident__has_builtin;              // __has_builtin
134   IdentifierInfo *Ident__has_attribute;            // __has_attribute
135   IdentifierInfo *Ident__has_include;              // __has_include
136   IdentifierInfo *Ident__has_include_next;         // __has_include_next
137   IdentifierInfo *Ident__has_warning;              // __has_warning
138   IdentifierInfo *Ident__is_identifier;            // __is_identifier
139   IdentifierInfo *Ident__building_module;          // __building_module
140   IdentifierInfo *Ident__MODULE__;                 // __MODULE__
141   IdentifierInfo *Ident__has_cpp_attribute;        // __has_cpp_attribute
142   IdentifierInfo *Ident__has_declspec;             // __has_declspec_attribute
143
144   SourceLocation DATELoc, TIMELoc;
145   unsigned CounterValue;  // Next __COUNTER__ value.
146
147   enum {
148     /// \brief Maximum depth of \#includes.
149     MaxAllowedIncludeStackDepth = 200
150   };
151
152   // State that is set before the preprocessor begins.
153   bool KeepComments : 1;
154   bool KeepMacroComments : 1;
155   bool SuppressIncludeNotFoundError : 1;
156
157   // State that changes while the preprocessor runs:
158   bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
159
160   /// Whether the preprocessor owns the header search object.
161   bool OwnsHeaderSearch : 1;
162
163   /// True if macro expansion is disabled.
164   bool DisableMacroExpansion : 1;
165
166   /// Temporarily disables DisableMacroExpansion (i.e. enables expansion)
167   /// when parsing preprocessor directives.
168   bool MacroExpansionInDirectivesOverride : 1;
169
170   class ResetMacroExpansionHelper;
171
172   /// \brief Whether we have already loaded macros from the external source.
173   mutable bool ReadMacrosFromExternalSource : 1;
174
175   /// \brief True if pragmas are enabled.
176   bool PragmasEnabled : 1;
177
178   /// \brief True if the current build action is a preprocessing action.
179   bool PreprocessedOutput : 1;
180
181   /// \brief True if we are currently preprocessing a #if or #elif directive
182   bool ParsingIfOrElifDirective;
183
184   /// \brief True if we are pre-expanding macro arguments.
185   bool InMacroArgPreExpansion;
186
187   /// \brief Mapping/lookup information for all identifiers in
188   /// the program, including program keywords.
189   mutable IdentifierTable Identifiers;
190
191   /// \brief This table contains all the selectors in the program.
192   ///
193   /// Unlike IdentifierTable above, this table *isn't* populated by the
194   /// preprocessor. It is declared/expanded here because its role/lifetime is
195   /// conceptually similar to the IdentifierTable. In addition, the current
196   /// control flow (in clang::ParseAST()), make it convenient to put here.
197   ///
198   /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
199   /// the lifetime of the preprocessor.
200   SelectorTable Selectors;
201
202   /// \brief Information about builtins.
203   Builtin::Context BuiltinInfo;
204
205   /// \brief Tracks all of the pragmas that the client registered
206   /// with this preprocessor.
207   std::unique_ptr<PragmaNamespace> PragmaHandlers;
208
209   /// \brief Pragma handlers of the original source is stored here during the
210   /// parsing of a model file.
211   std::unique_ptr<PragmaNamespace> PragmaHandlersBackup;
212
213   /// \brief Tracks all of the comment handlers that the client registered
214   /// with this preprocessor.
215   std::vector<CommentHandler *> CommentHandlers;
216
217   /// \brief True if we want to ignore EOF token and continue later on (thus 
218   /// avoid tearing the Lexer and etc. down).
219   bool IncrementalProcessing;
220
221   /// The kind of translation unit we are processing.
222   TranslationUnitKind TUKind;
223
224   /// \brief The code-completion handler.
225   CodeCompletionHandler *CodeComplete;
226
227   /// \brief The file that we're performing code-completion for, if any.
228   const FileEntry *CodeCompletionFile;
229
230   /// \brief The offset in file for the code-completion point.
231   unsigned CodeCompletionOffset;
232
233   /// \brief The location for the code-completion point. This gets instantiated
234   /// when the CodeCompletionFile gets \#include'ed for preprocessing.
235   SourceLocation CodeCompletionLoc;
236
237   /// \brief The start location for the file of the code-completion point.
238   ///
239   /// This gets instantiated when the CodeCompletionFile gets \#include'ed
240   /// for preprocessing.
241   SourceLocation CodeCompletionFileLoc;
242
243   /// \brief The source location of the \c import contextual keyword we just 
244   /// lexed, if any.
245   SourceLocation ModuleImportLoc;
246
247   /// \brief The module import path that we're currently processing.
248   SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> ModuleImportPath;
249
250   /// \brief Whether the last token we lexed was an '@'.
251   bool LastTokenWasAt;
252
253   /// \brief Whether the module import expects an identifier next. Otherwise,
254   /// it expects a '.' or ';'.
255   bool ModuleImportExpectsIdentifier;
256   
257   /// \brief The source location of the currently-active
258   /// \#pragma clang arc_cf_code_audited begin.
259   SourceLocation PragmaARCCFCodeAuditedLoc;
260
261   /// \brief The source location of the currently-active
262   /// \#pragma clang assume_nonnull begin.
263   SourceLocation PragmaAssumeNonNullLoc;
264
265   /// \brief True if we hit the code-completion point.
266   bool CodeCompletionReached;
267
268   /// \brief The code completion token containing the information
269   /// on the stem that is to be code completed.
270   IdentifierInfo *CodeCompletionII;
271
272   /// \brief The directory that the main file should be considered to occupy,
273   /// if it does not correspond to a real file (as happens when building a
274   /// module).
275   const DirectoryEntry *MainFileDir;
276
277   /// \brief The number of bytes that we will initially skip when entering the
278   /// main file, along with a flag that indicates whether skipping this number
279   /// of bytes will place the lexer at the start of a line.
280   ///
281   /// This is used when loading a precompiled preamble.
282   std::pair<int, bool> SkipMainFilePreamble;
283
284   /// \brief The current top of the stack that we're lexing from if
285   /// not expanding a macro and we are lexing directly from source code.
286   ///
287   /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
288   std::unique_ptr<Lexer> CurLexer;
289
290   /// \brief The current top of stack that we're lexing from if
291   /// not expanding from a macro and we are lexing from a PTH cache.
292   ///
293   /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
294   std::unique_ptr<PTHLexer> CurPTHLexer;
295
296   /// \brief The current top of the stack what we're lexing from
297   /// if not expanding a macro.
298   ///
299   /// This is an alias for either CurLexer or  CurPTHLexer.
300   PreprocessorLexer *CurPPLexer;
301
302   /// \brief Used to find the current FileEntry, if CurLexer is non-null
303   /// and if applicable.
304   ///
305   /// This allows us to implement \#include_next and find directory-specific
306   /// properties.
307   const DirectoryLookup *CurDirLookup;
308
309   /// \brief The current macro we are expanding, if we are expanding a macro.
310   ///
311   /// One of CurLexer and CurTokenLexer must be null.
312   std::unique_ptr<TokenLexer> CurTokenLexer;
313
314   /// \brief The kind of lexer we're currently working with.
315   enum CurLexerKind {
316     CLK_Lexer,
317     CLK_PTHLexer,
318     CLK_TokenLexer,
319     CLK_CachingLexer,
320     CLK_LexAfterModuleImport
321   } CurLexerKind;
322
323   /// \brief If the current lexer is for a submodule that is being built, this
324   /// is that submodule.
325   Module *CurSubmodule;
326
327   /// \brief Keeps track of the stack of files currently
328   /// \#included, and macros currently being expanded from, not counting
329   /// CurLexer/CurTokenLexer.
330   struct IncludeStackInfo {
331     enum CurLexerKind           CurLexerKind;
332     Module                     *TheSubmodule;
333     std::unique_ptr<Lexer>      TheLexer;
334     std::unique_ptr<PTHLexer>   ThePTHLexer;
335     PreprocessorLexer          *ThePPLexer;
336     std::unique_ptr<TokenLexer> TheTokenLexer;
337     const DirectoryLookup      *TheDirLookup;
338
339     // The following constructors are completely useless copies of the default
340     // versions, only needed to pacify MSVC.
341     IncludeStackInfo(enum CurLexerKind CurLexerKind, Module *TheSubmodule,
342                      std::unique_ptr<Lexer> &&TheLexer,
343                      std::unique_ptr<PTHLexer> &&ThePTHLexer,
344                      PreprocessorLexer *ThePPLexer,
345                      std::unique_ptr<TokenLexer> &&TheTokenLexer,
346                      const DirectoryLookup *TheDirLookup)
347         : CurLexerKind(std::move(CurLexerKind)),
348           TheSubmodule(std::move(TheSubmodule)), TheLexer(std::move(TheLexer)),
349           ThePTHLexer(std::move(ThePTHLexer)),
350           ThePPLexer(std::move(ThePPLexer)),
351           TheTokenLexer(std::move(TheTokenLexer)),
352           TheDirLookup(std::move(TheDirLookup)) {}
353   };
354   std::vector<IncludeStackInfo> IncludeMacroStack;
355
356   /// \brief Actions invoked when some preprocessor activity is
357   /// encountered (e.g. a file is \#included, etc).
358   std::unique_ptr<PPCallbacks> Callbacks;
359
360   struct MacroExpandsInfo {
361     Token Tok;
362     MacroDefinition MD;
363     SourceRange Range;
364     MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range)
365       : Tok(Tok), MD(MD), Range(Range) { }
366   };
367   SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
368
369   /// Information about a name that has been used to define a module macro.
370   struct ModuleMacroInfo {
371     ModuleMacroInfo(MacroDirective *MD)
372         : MD(MD), ActiveModuleMacrosGeneration(0), IsAmbiguous(false) {}
373
374     /// The most recent macro directive for this identifier.
375     MacroDirective *MD;
376     /// The active module macros for this identifier.
377     llvm::TinyPtrVector<ModuleMacro*> ActiveModuleMacros;
378     /// The generation number at which we last updated ActiveModuleMacros.
379     /// \see Preprocessor::VisibleModules.
380     unsigned ActiveModuleMacrosGeneration;
381     /// Whether this macro name is ambiguous.
382     bool IsAmbiguous;
383     /// The module macros that are overridden by this macro.
384     llvm::TinyPtrVector<ModuleMacro*> OverriddenMacros;
385   };
386
387   /// The state of a macro for an identifier.
388   class MacroState {
389     mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State;
390
391     ModuleMacroInfo *getModuleInfo(Preprocessor &PP,
392                                    const IdentifierInfo *II) const {
393       if (II->isOutOfDate())
394         PP.updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
395       // FIXME: Find a spare bit on IdentifierInfo and store a
396       //        HasModuleMacros flag.
397       if (!II->hasMacroDefinition() ||
398           (!PP.getLangOpts().Modules &&
399            !PP.getLangOpts().ModulesLocalVisibility) ||
400           !PP.CurSubmoduleState->VisibleModules.getGeneration())
401         return nullptr;
402
403       auto *Info = State.dyn_cast<ModuleMacroInfo*>();
404       if (!Info) {
405         Info = new (PP.getPreprocessorAllocator())
406             ModuleMacroInfo(State.get<MacroDirective *>());
407         State = Info;
408       }
409
410       if (PP.CurSubmoduleState->VisibleModules.getGeneration() !=
411           Info->ActiveModuleMacrosGeneration)
412         PP.updateModuleMacroInfo(II, *Info);
413       return Info;
414     }
415
416   public:
417     MacroState() : MacroState(nullptr) {}
418     MacroState(MacroDirective *MD) : State(MD) {}
419     MacroState(MacroState &&O) noexcept : State(O.State) {
420       O.State = (MacroDirective *)nullptr;
421     }
422     MacroState &operator=(MacroState &&O) noexcept {
423       auto S = O.State;
424       O.State = (MacroDirective *)nullptr;
425       State = S;
426       return *this;
427     }
428     ~MacroState() {
429       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
430         Info->~ModuleMacroInfo();
431     }
432
433     MacroDirective *getLatest() const {
434       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
435         return Info->MD;
436       return State.get<MacroDirective*>();
437     }
438     void setLatest(MacroDirective *MD) {
439       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
440         Info->MD = MD;
441       else
442         State = MD;
443     }
444
445     bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const {
446       auto *Info = getModuleInfo(PP, II);
447       return Info ? Info->IsAmbiguous : false;
448     }
449     ArrayRef<ModuleMacro *>
450     getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const {
451       if (auto *Info = getModuleInfo(PP, II))
452         return Info->ActiveModuleMacros;
453       return None;
454     }
455
456     MacroDirective::DefInfo findDirectiveAtLoc(SourceLocation Loc,
457                                                SourceManager &SourceMgr) const {
458       // FIXME: Incorporate module macros into the result of this.
459       if (auto *Latest = getLatest())
460         return Latest->findDirectiveAtLoc(Loc, SourceMgr);
461       return MacroDirective::DefInfo();
462     }
463
464     void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) {
465       if (auto *Info = getModuleInfo(PP, II)) {
466         Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
467                                       Info->ActiveModuleMacros.begin(),
468                                       Info->ActiveModuleMacros.end());
469         Info->ActiveModuleMacros.clear();
470         Info->IsAmbiguous = false;
471       }
472     }
473     ArrayRef<ModuleMacro*> getOverriddenMacros() const {
474       if (auto *Info = State.dyn_cast<ModuleMacroInfo*>())
475         return Info->OverriddenMacros;
476       return None;
477     }
478     void setOverriddenMacros(Preprocessor &PP,
479                              ArrayRef<ModuleMacro *> Overrides) {
480       auto *Info = State.dyn_cast<ModuleMacroInfo*>();
481       if (!Info) {
482         if (Overrides.empty())
483           return;
484         Info = new (PP.getPreprocessorAllocator())
485             ModuleMacroInfo(State.get<MacroDirective *>());
486         State = Info;
487       }
488       Info->OverriddenMacros.clear();
489       Info->OverriddenMacros.insert(Info->OverriddenMacros.end(),
490                                     Overrides.begin(), Overrides.end());
491       Info->ActiveModuleMacrosGeneration = 0;
492     }
493   };
494
495   /// For each IdentifierInfo that was associated with a macro, we
496   /// keep a mapping to the history of all macro definitions and #undefs in
497   /// the reverse order (the latest one is in the head of the list).
498   ///
499   /// This mapping lives within the \p CurSubmoduleState.
500   typedef llvm::DenseMap<const IdentifierInfo *, MacroState> MacroMap;
501
502   friend class ASTReader;
503
504   struct SubmoduleState;
505
506   /// \brief Information about a submodule that we're currently building.
507   struct BuildingSubmoduleInfo {
508     BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc,
509                           SubmoduleState *OuterSubmoduleState,
510                           unsigned OuterPendingModuleMacroNames)
511         : M(M), ImportLoc(ImportLoc), OuterSubmoduleState(OuterSubmoduleState),
512           OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {}
513
514     /// The module that we are building.
515     Module *M;
516     /// The location at which the module was included.
517     SourceLocation ImportLoc;
518     /// The previous SubmoduleState.
519     SubmoduleState *OuterSubmoduleState;
520     /// The number of pending module macro names when we started building this.
521     unsigned OuterPendingModuleMacroNames;
522   };
523   SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack;
524
525   /// \brief Information about a submodule's preprocessor state.
526   struct SubmoduleState {
527     /// The macros for the submodule.
528     MacroMap Macros;
529     /// The set of modules that are visible within the submodule.
530     VisibleModuleSet VisibleModules;
531     // FIXME: CounterValue?
532     // FIXME: PragmaPushMacroInfo?
533   };
534   std::map<Module*, SubmoduleState> Submodules;
535
536   /// The preprocessor state for preprocessing outside of any submodule.
537   SubmoduleState NullSubmoduleState;
538
539   /// The current submodule state. Will be \p NullSubmoduleState if we're not
540   /// in a submodule.
541   SubmoduleState *CurSubmoduleState;
542
543   /// The set of known macros exported from modules.
544   llvm::FoldingSet<ModuleMacro> ModuleMacros;
545
546   /// The names of potential module macros that we've not yet processed.
547   llvm::SmallVector<const IdentifierInfo*, 32> PendingModuleMacroNames;
548
549   /// The list of module macros, for each identifier, that are not overridden by
550   /// any other module macro.
551   llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro*>>
552       LeafModuleMacros;
553
554   /// \brief Macros that we want to warn because they are not used at the end
555   /// of the translation unit.
556   ///
557   /// We store just their SourceLocations instead of
558   /// something like MacroInfo*. The benefit of this is that when we are
559   /// deserializing from PCH, we don't need to deserialize identifier & macros
560   /// just so that we can report that they are unused, we just warn using
561   /// the SourceLocations of this set (that will be filled by the ASTReader).
562   /// We are using SmallPtrSet instead of a vector for faster removal.
563   typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
564   WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
565
566   /// \brief A "freelist" of MacroArg objects that can be
567   /// reused for quick allocation.
568   MacroArgs *MacroArgCache;
569   friend class MacroArgs;
570
571   /// For each IdentifierInfo used in a \#pragma push_macro directive,
572   /// we keep a MacroInfo stack used to restore the previous macro value.
573   llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
574
575   // Various statistics we track for performance analysis.
576   unsigned NumDirectives, NumDefined, NumUndefined, NumPragma;
577   unsigned NumIf, NumElse, NumEndif;
578   unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
579   unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
580   unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
581   unsigned NumSkipped;
582
583   /// \brief The predefined macros that preprocessor should use from the
584   /// command line etc.
585   std::string Predefines;
586
587   /// \brief The file ID for the preprocessor predefines.
588   FileID PredefinesFileID;
589
590   /// \{
591   /// \brief Cache of macro expanders to reduce malloc traffic.
592   enum { TokenLexerCacheSize = 8 };
593   unsigned NumCachedTokenLexers;
594   std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize];
595   /// \}
596
597   /// \brief Keeps macro expanded tokens for TokenLexers.
598   //
599   /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
600   /// going to lex in the cache and when it finishes the tokens are removed
601   /// from the end of the cache.
602   SmallVector<Token, 16> MacroExpandedTokens;
603   std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
604
605   /// \brief A record of the macro definitions and expansions that
606   /// occurred during preprocessing.
607   ///
608   /// This is an optional side structure that can be enabled with
609   /// \c createPreprocessingRecord() prior to preprocessing.
610   PreprocessingRecord *Record;
611
612   /// Cached tokens state.
613   typedef SmallVector<Token, 1> CachedTokensTy;
614
615   /// \brief Cached tokens are stored here when we do backtracking or
616   /// lookahead. They are "lexed" by the CachingLex() method.
617   CachedTokensTy CachedTokens;
618
619   /// \brief The position of the cached token that CachingLex() should
620   /// "lex" next.
621   ///
622   /// If it points beyond the CachedTokens vector, it means that a normal
623   /// Lex() should be invoked.
624   CachedTokensTy::size_type CachedLexPos;
625
626   /// \brief Stack of backtrack positions, allowing nested backtracks.
627   ///
628   /// The EnableBacktrackAtThisPos() method pushes a position to
629   /// indicate where CachedLexPos should be set when the BackTrack() method is
630   /// invoked (at which point the last position is popped).
631   std::vector<CachedTokensTy::size_type> BacktrackPositions;
632
633   struct MacroInfoChain {
634     MacroInfo MI;
635     MacroInfoChain *Next;
636   };
637
638   /// MacroInfos are managed as a chain for easy disposal.  This is the head
639   /// of that list.
640   MacroInfoChain *MIChainHead;
641
642   struct DeserializedMacroInfoChain {
643     MacroInfo MI;
644     unsigned OwningModuleID; // MUST be immediately after the MacroInfo object
645                      // so it can be accessed by MacroInfo::getOwningModuleID().
646     DeserializedMacroInfoChain *Next;
647   };
648   DeserializedMacroInfoChain *DeserialMIChainHead;
649
650   void updateOutOfDateIdentifier(IdentifierInfo &II) const;
651
652 public:
653   Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
654                DiagnosticsEngine &diags, LangOptions &opts, SourceManager &SM,
655                HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
656                IdentifierInfoLookup *IILookup = nullptr,
657                bool OwnsHeaderSearch = false,
658                TranslationUnitKind TUKind = TU_Complete);
659
660   ~Preprocessor();
661
662   /// \brief Initialize the preprocessor using information about the target.
663   ///
664   /// \param Target is owned by the caller and must remain valid for the
665   /// lifetime of the preprocessor.
666   /// \param AuxTarget is owned by the caller and must remain valid for
667   /// the lifetime of the preprocessor.
668   void Initialize(const TargetInfo &Target,
669                   const TargetInfo *AuxTarget = nullptr);
670
671   /// \brief Initialize the preprocessor to parse a model file
672   ///
673   /// To parse model files the preprocessor of the original source is reused to
674   /// preserver the identifier table. However to avoid some duplicate
675   /// information in the preprocessor some cleanup is needed before it is used
676   /// to parse model files. This method does that cleanup.
677   void InitializeForModelFile();
678
679   /// \brief Cleanup after model file parsing
680   void FinalizeForModelFile();
681
682   /// \brief Retrieve the preprocessor options used to initialize this
683   /// preprocessor.
684   PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
685   
686   DiagnosticsEngine &getDiagnostics() const { return *Diags; }
687   void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
688
689   const LangOptions &getLangOpts() const { return LangOpts; }
690   const TargetInfo &getTargetInfo() const { return *Target; }
691   const TargetInfo *getAuxTargetInfo() const { return AuxTarget; }
692   FileManager &getFileManager() const { return FileMgr; }
693   SourceManager &getSourceManager() const { return SourceMgr; }
694   HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
695
696   IdentifierTable &getIdentifierTable() { return Identifiers; }
697   const IdentifierTable &getIdentifierTable() const { return Identifiers; }
698   SelectorTable &getSelectorTable() { return Selectors; }
699   Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
700   llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
701
702   void setPTHManager(PTHManager* pm);
703
704   PTHManager *getPTHManager() { return PTH.get(); }
705
706   void setExternalSource(ExternalPreprocessorSource *Source) {
707     ExternalSource = Source;
708   }
709
710   ExternalPreprocessorSource *getExternalSource() const {
711     return ExternalSource;
712   }
713
714   /// \brief Retrieve the module loader associated with this preprocessor.
715   ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
716
717   bool hadModuleLoaderFatalFailure() const {
718     return TheModuleLoader.HadFatalFailure;
719   }
720
721   /// \brief True if we are currently preprocessing a #if or #elif directive
722   bool isParsingIfOrElifDirective() const { 
723     return ParsingIfOrElifDirective;
724   }
725
726   /// \brief Control whether the preprocessor retains comments in output.
727   void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
728     this->KeepComments = KeepComments | KeepMacroComments;
729     this->KeepMacroComments = KeepMacroComments;
730   }
731
732   bool getCommentRetentionState() const { return KeepComments; }
733
734   void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
735   bool getPragmasEnabled() const { return PragmasEnabled; }
736
737   void SetSuppressIncludeNotFoundError(bool Suppress) {
738     SuppressIncludeNotFoundError = Suppress;
739   }
740
741   bool GetSuppressIncludeNotFoundError() {
742     return SuppressIncludeNotFoundError;
743   }
744
745   /// Sets whether the preprocessor is responsible for producing output or if
746   /// it is producing tokens to be consumed by Parse and Sema.
747   void setPreprocessedOutput(bool IsPreprocessedOutput) {
748     PreprocessedOutput = IsPreprocessedOutput;
749   }
750
751   /// Returns true if the preprocessor is responsible for generating output,
752   /// false if it is producing tokens to be consumed by Parse and Sema.
753   bool isPreprocessedOutput() const { return PreprocessedOutput; }
754
755   /// \brief Return true if we are lexing directly from the specified lexer.
756   bool isCurrentLexer(const PreprocessorLexer *L) const {
757     return CurPPLexer == L;
758   }
759
760   /// \brief Return the current lexer being lexed from.
761   ///
762   /// Note that this ignores any potentially active macro expansions and _Pragma
763   /// expansions going on at the time.
764   PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
765
766   /// \brief Return the current file lexer being lexed from.
767   ///
768   /// Note that this ignores any potentially active macro expansions and _Pragma
769   /// expansions going on at the time.
770   PreprocessorLexer *getCurrentFileLexer() const;
771
772   /// \brief Return the submodule owning the file being lexed.
773   Module *getCurrentSubmodule() const { return CurSubmodule; }
774
775   /// \brief Returns the FileID for the preprocessor predefines.
776   FileID getPredefinesFileID() const { return PredefinesFileID; }
777
778   /// \{
779   /// \brief Accessors for preprocessor callbacks.
780   ///
781   /// Note that this class takes ownership of any PPCallbacks object given to
782   /// it.
783   PPCallbacks *getPPCallbacks() const { return Callbacks.get(); }
784   void addPPCallbacks(std::unique_ptr<PPCallbacks> C) {
785     if (Callbacks)
786       C = llvm::make_unique<PPChainedCallbacks>(std::move(C),
787                                                 std::move(Callbacks));
788     Callbacks = std::move(C);
789   }
790   /// \}
791
792   bool isMacroDefined(StringRef Id) {
793     return isMacroDefined(&Identifiers.get(Id));
794   }
795   bool isMacroDefined(const IdentifierInfo *II) {
796     return II->hasMacroDefinition() &&
797            (!getLangOpts().Modules || (bool)getMacroDefinition(II));
798   }
799
800   /// \brief Determine whether II is defined as a macro within the module M,
801   /// if that is a module that we've already preprocessed. Does not check for
802   /// macros imported into M.
803   bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M) {
804     if (!II->hasMacroDefinition())
805       return false;
806     auto I = Submodules.find(M);
807     if (I == Submodules.end())
808       return false;
809     auto J = I->second.Macros.find(II);
810     if (J == I->second.Macros.end())
811       return false;
812     auto *MD = J->second.getLatest();
813     return MD && MD->isDefined();
814   }
815
816   MacroDefinition getMacroDefinition(const IdentifierInfo *II) {
817     if (!II->hasMacroDefinition())
818       return MacroDefinition();
819
820     MacroState &S = CurSubmoduleState->Macros[II];
821     auto *MD = S.getLatest();
822     while (MD && isa<VisibilityMacroDirective>(MD))
823       MD = MD->getPrevious();
824     return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD),
825                            S.getActiveModuleMacros(*this, II),
826                            S.isAmbiguous(*this, II));
827   }
828
829   MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II,
830                                           SourceLocation Loc) {
831     if (!II->hadMacroDefinition())
832       return MacroDefinition();
833
834     MacroState &S = CurSubmoduleState->Macros[II];
835     MacroDirective::DefInfo DI;
836     if (auto *MD = S.getLatest())
837       DI = MD->findDirectiveAtLoc(Loc, getSourceManager());
838     // FIXME: Compute the set of active module macros at the specified location.
839     return MacroDefinition(DI.getDirective(),
840                            S.getActiveModuleMacros(*this, II),
841                            S.isAmbiguous(*this, II));
842   }
843
844   /// \brief Given an identifier, return its latest non-imported MacroDirective
845   /// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd.
846   MacroDirective *getLocalMacroDirective(const IdentifierInfo *II) const {
847     if (!II->hasMacroDefinition())
848       return nullptr;
849
850     auto *MD = getLocalMacroDirectiveHistory(II);
851     if (!MD || MD->getDefinition().isUndefined())
852       return nullptr;
853
854     return MD;
855   }
856
857   const MacroInfo *getMacroInfo(const IdentifierInfo *II) const {
858     return const_cast<Preprocessor*>(this)->getMacroInfo(II);
859   }
860
861   MacroInfo *getMacroInfo(const IdentifierInfo *II) {
862     if (!II->hasMacroDefinition())
863       return nullptr;
864     if (auto MD = getMacroDefinition(II))
865       return MD.getMacroInfo();
866     return nullptr;
867   }
868
869   /// \brief Given an identifier, return the latest non-imported macro
870   /// directive for that identifier.
871   ///
872   /// One can iterate over all previous macro directives from the most recent
873   /// one.
874   MacroDirective *getLocalMacroDirectiveHistory(const IdentifierInfo *II) const;
875
876   /// \brief Add a directive to the macro directive history for this identifier.
877   void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
878   DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI,
879                                              SourceLocation Loc) {
880     DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc);
881     appendMacroDirective(II, MD);
882     return MD;
883   }
884   DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II,
885                                              MacroInfo *MI) {
886     return appendDefMacroDirective(II, MI, MI->getDefinitionLoc());
887   }
888   /// \brief Set a MacroDirective that was loaded from a PCH file.
889   void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED,
890                                MacroDirective *MD);
891
892   /// \brief Register an exported macro for a module and identifier.
893   ModuleMacro *addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro,
894                               ArrayRef<ModuleMacro *> Overrides, bool &IsNew);
895   ModuleMacro *getModuleMacro(Module *Mod, IdentifierInfo *II);
896
897   /// \brief Get the list of leaf (non-overridden) module macros for a name.
898   ArrayRef<ModuleMacro*> getLeafModuleMacros(const IdentifierInfo *II) const {
899     if (II->isOutOfDate())
900       updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
901     auto I = LeafModuleMacros.find(II);
902     if (I != LeafModuleMacros.end())
903       return I->second;
904     return None;
905   }
906
907   /// \{
908   /// Iterators for the macro history table. Currently defined macros have
909   /// IdentifierInfo::hasMacroDefinition() set and an empty
910   /// MacroInfo::getUndefLoc() at the head of the list.
911   typedef MacroMap::const_iterator macro_iterator;
912   macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
913   macro_iterator macro_end(bool IncludeExternalMacros = true) const;
914   llvm::iterator_range<macro_iterator>
915   macros(bool IncludeExternalMacros = true) const {
916     return llvm::make_range(macro_begin(IncludeExternalMacros),
917                             macro_end(IncludeExternalMacros));
918   }
919   /// \}
920
921   /// \brief Return the name of the macro defined before \p Loc that has
922   /// spelling \p Tokens.  If there are multiple macros with same spelling,
923   /// return the last one defined.
924   StringRef getLastMacroWithSpelling(SourceLocation Loc,
925                                      ArrayRef<TokenValue> Tokens) const;
926
927   const std::string &getPredefines() const { return Predefines; }
928   /// \brief Set the predefines for this Preprocessor.
929   ///
930   /// These predefines are automatically injected when parsing the main file.
931   void setPredefines(const char *P) { Predefines = P; }
932   void setPredefines(StringRef P) { Predefines = P; }
933
934   /// Return information about the specified preprocessor
935   /// identifier token.
936   IdentifierInfo *getIdentifierInfo(StringRef Name) const {
937     return &Identifiers.get(Name);
938   }
939
940   /// \brief Add the specified pragma handler to this preprocessor.
941   ///
942   /// If \p Namespace is non-null, then it is a token required to exist on the
943   /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
944   void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
945   void AddPragmaHandler(PragmaHandler *Handler) {
946     AddPragmaHandler(StringRef(), Handler);
947   }
948
949   /// \brief Remove the specific pragma handler from this preprocessor.
950   ///
951   /// If \p Namespace is non-null, then it should be the namespace that
952   /// \p Handler was added to. It is an error to remove a handler that
953   /// has not been registered.
954   void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
955   void RemovePragmaHandler(PragmaHandler *Handler) {
956     RemovePragmaHandler(StringRef(), Handler);
957   }
958
959   /// Install empty handlers for all pragmas (making them ignored).
960   void IgnorePragmas();
961
962   /// \brief Add the specified comment handler to the preprocessor.
963   void addCommentHandler(CommentHandler *Handler);
964
965   /// \brief Remove the specified comment handler.
966   ///
967   /// It is an error to remove a handler that has not been registered.
968   void removeCommentHandler(CommentHandler *Handler);
969
970   /// \brief Set the code completion handler to the given object.
971   void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
972     CodeComplete = &Handler;
973   }
974
975   /// \brief Retrieve the current code-completion handler.
976   CodeCompletionHandler *getCodeCompletionHandler() const {
977     return CodeComplete;
978   }
979
980   /// \brief Clear out the code completion handler.
981   void clearCodeCompletionHandler() {
982     CodeComplete = nullptr;
983   }
984
985   /// \brief Hook used by the lexer to invoke the "natural language" code
986   /// completion point.
987   void CodeCompleteNaturalLanguage();
988
989   /// \brief Set the code completion token for filtering purposes.
990   void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter) {
991     CodeCompletionII = Filter;
992   }
993
994   /// \brief Get the code completion token for filtering purposes.
995   StringRef getCodeCompletionFilter() {
996     if (CodeCompletionII)
997       return CodeCompletionII->getName();
998     return {};
999   }
1000
1001   /// \brief Retrieve the preprocessing record, or NULL if there is no
1002   /// preprocessing record.
1003   PreprocessingRecord *getPreprocessingRecord() const { return Record; }
1004
1005   /// \brief Create a new preprocessing record, which will keep track of
1006   /// all macro expansions, macro definitions, etc.
1007   void createPreprocessingRecord();
1008
1009   /// \brief Enter the specified FileID as the main source file,
1010   /// which implicitly adds the builtin defines etc.
1011   void EnterMainSourceFile();
1012
1013   /// \brief Inform the preprocessor callbacks that processing is complete.
1014   void EndSourceFile();
1015
1016   /// \brief Add a source file to the top of the include stack and
1017   /// start lexing tokens from it instead of the current buffer.
1018   ///
1019   /// Emits a diagnostic, doesn't enter the file, and returns true on error.
1020   bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
1021                        SourceLocation Loc);
1022
1023   /// \brief Add a Macro to the top of the include stack and start lexing
1024   /// tokens from it instead of the current buffer.
1025   ///
1026   /// \param Args specifies the tokens input to a function-like macro.
1027   /// \param ILEnd specifies the location of the ')' for a function-like macro
1028   /// or the identifier for an object-like macro.
1029   void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroInfo *Macro,
1030                   MacroArgs *Args);
1031
1032   /// \brief Add a "macro" context to the top of the include stack,
1033   /// which will cause the lexer to start returning the specified tokens.
1034   ///
1035   /// If \p DisableMacroExpansion is true, tokens lexed from the token stream
1036   /// will not be subject to further macro expansion. Otherwise, these tokens
1037   /// will be re-macro-expanded when/if expansion is enabled.
1038   ///
1039   /// If \p OwnsTokens is false, this method assumes that the specified stream
1040   /// of tokens has a permanent owner somewhere, so they do not need to be
1041   /// copied. If it is true, it assumes the array of tokens is allocated with
1042   /// \c new[] and the Preprocessor will delete[] it.
1043 private:
1044   void EnterTokenStream(const Token *Toks, unsigned NumToks,
1045                         bool DisableMacroExpansion, bool OwnsTokens);
1046
1047 public:
1048   void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1049                         bool DisableMacroExpansion) {
1050     EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true);
1051   }
1052   void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion) {
1053     EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false);
1054   }
1055
1056   /// \brief Pop the current lexer/macro exp off the top of the lexer stack.
1057   ///
1058   /// This should only be used in situations where the current state of the
1059   /// top-of-stack lexer is known.
1060   void RemoveTopOfLexerStack();
1061
1062   /// From the point that this method is called, and until
1063   /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
1064   /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
1065   /// make the Preprocessor re-lex the same tokens.
1066   ///
1067   /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
1068   /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
1069   /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
1070   ///
1071   /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
1072   /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
1073   /// tokens will continue indefinitely.
1074   ///
1075   void EnableBacktrackAtThisPos();
1076
1077   /// \brief Disable the last EnableBacktrackAtThisPos call.
1078   void CommitBacktrackedTokens();
1079
1080   /// \brief Make Preprocessor re-lex the tokens that were lexed since
1081   /// EnableBacktrackAtThisPos() was previously called.
1082   void Backtrack();
1083
1084   /// \brief True if EnableBacktrackAtThisPos() was called and
1085   /// caching of tokens is on.
1086   bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
1087
1088   /// \brief Lex the next token for this preprocessor.
1089   void Lex(Token &Result);
1090
1091   void LexAfterModuleImport(Token &Result);
1092
1093   void makeModuleVisible(Module *M, SourceLocation Loc);
1094
1095   SourceLocation getModuleImportLoc(Module *M) const {
1096     return CurSubmoduleState->VisibleModules.getImportLoc(M);
1097   }
1098
1099   /// \brief Lex a string literal, which may be the concatenation of multiple
1100   /// string literals and may even come from macro expansion.
1101   /// \returns true on success, false if a error diagnostic has been generated.
1102   bool LexStringLiteral(Token &Result, std::string &String,
1103                         const char *DiagnosticTag, bool AllowMacroExpansion) {
1104     if (AllowMacroExpansion)
1105       Lex(Result);
1106     else
1107       LexUnexpandedToken(Result);
1108     return FinishLexStringLiteral(Result, String, DiagnosticTag,
1109                                   AllowMacroExpansion);
1110   }
1111
1112   /// \brief Complete the lexing of a string literal where the first token has
1113   /// already been lexed (see LexStringLiteral).
1114   bool FinishLexStringLiteral(Token &Result, std::string &String,
1115                               const char *DiagnosticTag,
1116                               bool AllowMacroExpansion);
1117
1118   /// \brief Lex a token.  If it's a comment, keep lexing until we get
1119   /// something not a comment.
1120   ///
1121   /// This is useful in -E -C mode where comments would foul up preprocessor
1122   /// directive handling.
1123   void LexNonComment(Token &Result) {
1124     do
1125       Lex(Result);
1126     while (Result.getKind() == tok::comment);
1127   }
1128
1129   /// \brief Just like Lex, but disables macro expansion of identifier tokens.
1130   void LexUnexpandedToken(Token &Result) {
1131     // Disable macro expansion.
1132     bool OldVal = DisableMacroExpansion;
1133     DisableMacroExpansion = true;
1134     // Lex the token.
1135     Lex(Result);
1136
1137     // Reenable it.
1138     DisableMacroExpansion = OldVal;
1139   }
1140
1141   /// \brief Like LexNonComment, but this disables macro expansion of
1142   /// identifier tokens.
1143   void LexUnexpandedNonComment(Token &Result) {
1144     do
1145       LexUnexpandedToken(Result);
1146     while (Result.getKind() == tok::comment);
1147   }
1148
1149   /// \brief Parses a simple integer literal to get its numeric value.  Floating
1150   /// point literals and user defined literals are rejected.  Used primarily to
1151   /// handle pragmas that accept integer arguments.
1152   bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value);
1153
1154   /// Disables macro expansion everywhere except for preprocessor directives.
1155   void SetMacroExpansionOnlyInDirectives() {
1156     DisableMacroExpansion = true;
1157     MacroExpansionInDirectivesOverride = true;
1158   }
1159
1160   /// \brief Peeks ahead N tokens and returns that token without consuming any
1161   /// tokens.
1162   ///
1163   /// LookAhead(0) returns the next token that would be returned by Lex(),
1164   /// LookAhead(1) returns the token after it, etc.  This returns normal
1165   /// tokens after phase 5.  As such, it is equivalent to using
1166   /// 'Lex', not 'LexUnexpandedToken'.
1167   const Token &LookAhead(unsigned N) {
1168     if (CachedLexPos + N < CachedTokens.size())
1169       return CachedTokens[CachedLexPos+N];
1170     else
1171       return PeekAhead(N+1);
1172   }
1173
1174   /// \brief When backtracking is enabled and tokens are cached,
1175   /// this allows to revert a specific number of tokens.
1176   ///
1177   /// Note that the number of tokens being reverted should be up to the last
1178   /// backtrack position, not more.
1179   void RevertCachedTokens(unsigned N) {
1180     assert(isBacktrackEnabled() &&
1181            "Should only be called when tokens are cached for backtracking");
1182     assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
1183          && "Should revert tokens up to the last backtrack position, not more");
1184     assert(signed(CachedLexPos) - signed(N) >= 0 &&
1185            "Corrupted backtrack positions ?");
1186     CachedLexPos -= N;
1187   }
1188
1189   /// \brief Enters a token in the token stream to be lexed next.
1190   ///
1191   /// If BackTrack() is called afterwards, the token will remain at the
1192   /// insertion point.
1193   void EnterToken(const Token &Tok) {
1194     EnterCachingLexMode();
1195     CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
1196   }
1197
1198   /// We notify the Preprocessor that if it is caching tokens (because
1199   /// backtrack is enabled) it should replace the most recent cached tokens
1200   /// with the given annotation token. This function has no effect if
1201   /// backtracking is not enabled.
1202   ///
1203   /// Note that the use of this function is just for optimization, so that the
1204   /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
1205   /// invoked.
1206   void AnnotateCachedTokens(const Token &Tok) {
1207     assert(Tok.isAnnotation() && "Expected annotation token");
1208     if (CachedLexPos != 0 && isBacktrackEnabled())
1209       AnnotatePreviousCachedTokens(Tok);
1210   }
1211
1212   /// Get the location of the last cached token, suitable for setting the end
1213   /// location of an annotation token.
1214   SourceLocation getLastCachedTokenLocation() const {
1215     assert(CachedLexPos != 0);
1216     return CachedTokens[CachedLexPos-1].getLastLoc();
1217   }
1218
1219   /// \brief Whether \p Tok is the most recent token (`CachedLexPos - 1`) in
1220   /// CachedTokens.
1221   bool IsPreviousCachedToken(const Token &Tok) const;
1222
1223   /// \brief Replace token in `CachedLexPos - 1` in CachedTokens by the tokens
1224   /// in \p NewToks.
1225   ///
1226   /// Useful when a token needs to be split in smaller ones and CachedTokens
1227   /// most recent token must to be updated to reflect that.
1228   void ReplacePreviousCachedToken(ArrayRef<Token> NewToks);
1229
1230   /// \brief Replace the last token with an annotation token.
1231   ///
1232   /// Like AnnotateCachedTokens(), this routine replaces an
1233   /// already-parsed (and resolved) token with an annotation
1234   /// token. However, this routine only replaces the last token with
1235   /// the annotation token; it does not affect any other cached
1236   /// tokens. This function has no effect if backtracking is not
1237   /// enabled.
1238   void ReplaceLastTokenWithAnnotation(const Token &Tok) {
1239     assert(Tok.isAnnotation() && "Expected annotation token");
1240     if (CachedLexPos != 0 && isBacktrackEnabled())
1241       CachedTokens[CachedLexPos-1] = Tok;
1242   }
1243
1244   /// Update the current token to represent the provided
1245   /// identifier, in order to cache an action performed by typo correction.
1246   void TypoCorrectToken(const Token &Tok) {
1247     assert(Tok.getIdentifierInfo() && "Expected identifier token");
1248     if (CachedLexPos != 0 && isBacktrackEnabled())
1249       CachedTokens[CachedLexPos-1] = Tok;
1250   }
1251
1252   /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
1253   /// CurTokenLexer pointers.
1254   void recomputeCurLexerKind();
1255
1256   /// \brief Returns true if incremental processing is enabled
1257   bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
1258
1259   /// \brief Enables the incremental processing
1260   void enableIncrementalProcessing(bool value = true) {
1261     IncrementalProcessing = value;
1262   }
1263   
1264   /// \brief Specify the point at which code-completion will be performed.
1265   ///
1266   /// \param File the file in which code completion should occur. If
1267   /// this file is included multiple times, code-completion will
1268   /// perform completion the first time it is included. If NULL, this
1269   /// function clears out the code-completion point.
1270   ///
1271   /// \param Line the line at which code completion should occur
1272   /// (1-based).
1273   ///
1274   /// \param Column the column at which code completion should occur
1275   /// (1-based).
1276   ///
1277   /// \returns true if an error occurred, false otherwise.
1278   bool SetCodeCompletionPoint(const FileEntry *File,
1279                               unsigned Line, unsigned Column);
1280
1281   /// \brief Determine if we are performing code completion.
1282   bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }
1283
1284   /// \brief Returns the location of the code-completion point.
1285   ///
1286   /// Returns an invalid location if code-completion is not enabled or the file
1287   /// containing the code-completion point has not been lexed yet.
1288   SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
1289
1290   /// \brief Returns the start location of the file of code-completion point.
1291   ///
1292   /// Returns an invalid location if code-completion is not enabled or the file
1293   /// containing the code-completion point has not been lexed yet.
1294   SourceLocation getCodeCompletionFileLoc() const {
1295     return CodeCompletionFileLoc;
1296   }
1297
1298   /// \brief Returns true if code-completion is enabled and we have hit the
1299   /// code-completion point.
1300   bool isCodeCompletionReached() const { return CodeCompletionReached; }
1301
1302   /// \brief Note that we hit the code-completion point.
1303   void setCodeCompletionReached() {
1304     assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
1305     CodeCompletionReached = true;
1306     // Silence any diagnostics that occur after we hit the code-completion.
1307     getDiagnostics().setSuppressAllDiagnostics(true);
1308   }
1309
1310   /// \brief The location of the currently-active \#pragma clang
1311   /// arc_cf_code_audited begin.
1312   ///
1313   /// Returns an invalid location if there is no such pragma active.
1314   SourceLocation getPragmaARCCFCodeAuditedLoc() const {
1315     return PragmaARCCFCodeAuditedLoc;
1316   }
1317
1318   /// \brief Set the location of the currently-active \#pragma clang
1319   /// arc_cf_code_audited begin.  An invalid location ends the pragma.
1320   void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
1321     PragmaARCCFCodeAuditedLoc = Loc;
1322   }
1323
1324   /// \brief The location of the currently-active \#pragma clang
1325   /// assume_nonnull begin.
1326   ///
1327   /// Returns an invalid location if there is no such pragma active.
1328   SourceLocation getPragmaAssumeNonNullLoc() const {
1329     return PragmaAssumeNonNullLoc;
1330   }
1331
1332   /// \brief Set the location of the currently-active \#pragma clang
1333   /// assume_nonnull begin.  An invalid location ends the pragma.
1334   void setPragmaAssumeNonNullLoc(SourceLocation Loc) {
1335     PragmaAssumeNonNullLoc = Loc;
1336   }
1337
1338   /// \brief Set the directory in which the main file should be considered
1339   /// to have been found, if it is not a real file.
1340   void setMainFileDir(const DirectoryEntry *Dir) {
1341     MainFileDir = Dir;
1342   }
1343
1344   /// \brief Instruct the preprocessor to skip part of the main source file.
1345   ///
1346   /// \param Bytes The number of bytes in the preamble to skip.
1347   ///
1348   /// \param StartOfLine Whether skipping these bytes puts the lexer at the
1349   /// start of a line.
1350   void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
1351     SkipMainFilePreamble.first = Bytes;
1352     SkipMainFilePreamble.second = StartOfLine;
1353   }
1354
1355   /// Forwarding function for diagnostics.  This emits a diagnostic at
1356   /// the specified Token's location, translating the token's start
1357   /// position in the current buffer into a SourcePosition object for rendering.
1358   DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
1359     return Diags->Report(Loc, DiagID);
1360   }
1361
1362   DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
1363     return Diags->Report(Tok.getLocation(), DiagID);
1364   }
1365
1366   /// Return the 'spelling' of the token at the given
1367   /// location; does not go up to the spelling location or down to the
1368   /// expansion location.
1369   ///
1370   /// \param buffer A buffer which will be used only if the token requires
1371   ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
1372   /// \param invalid If non-null, will be set \c true if an error occurs.
1373   StringRef getSpelling(SourceLocation loc,
1374                         SmallVectorImpl<char> &buffer,
1375                         bool *invalid = nullptr) const {
1376     return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
1377   }
1378
1379   /// \brief Return the 'spelling' of the Tok token.
1380   ///
1381   /// The spelling of a token is the characters used to represent the token in
1382   /// the source file after trigraph expansion and escaped-newline folding.  In
1383   /// particular, this wants to get the true, uncanonicalized, spelling of
1384   /// things like digraphs, UCNs, etc.
1385   ///
1386   /// \param Invalid If non-null, will be set \c true if an error occurs.
1387   std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const {
1388     return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
1389   }
1390
1391   /// \brief Get the spelling of a token into a preallocated buffer, instead
1392   /// of as an std::string.
1393   ///
1394   /// The caller is required to allocate enough space for the token, which is
1395   /// guaranteed to be at least Tok.getLength() bytes long. The length of the
1396   /// actual result is returned.
1397   ///
1398   /// Note that this method may do two possible things: it may either fill in
1399   /// the buffer specified with characters, or it may *change the input pointer*
1400   /// to point to a constant buffer with the data already in it (avoiding a
1401   /// copy).  The caller is not allowed to modify the returned buffer pointer
1402   /// if an internal buffer is returned.
1403   unsigned getSpelling(const Token &Tok, const char *&Buffer,
1404                        bool *Invalid = nullptr) const {
1405     return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
1406   }
1407
1408   /// \brief Get the spelling of a token into a SmallVector.
1409   ///
1410   /// Note that the returned StringRef may not point to the
1411   /// supplied buffer if a copy can be avoided.
1412   StringRef getSpelling(const Token &Tok,
1413                         SmallVectorImpl<char> &Buffer,
1414                         bool *Invalid = nullptr) const;
1415
1416   /// \brief Relex the token at the specified location.
1417   /// \returns true if there was a failure, false on success.
1418   bool getRawToken(SourceLocation Loc, Token &Result,
1419                    bool IgnoreWhiteSpace = false) {
1420     return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace);
1421   }
1422
1423   /// \brief Given a Token \p Tok that is a numeric constant with length 1,
1424   /// return the character.
1425   char
1426   getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
1427                                               bool *Invalid = nullptr) const {
1428     assert(Tok.is(tok::numeric_constant) &&
1429            Tok.getLength() == 1 && "Called on unsupported token");
1430     assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
1431
1432     // If the token is carrying a literal data pointer, just use it.
1433     if (const char *D = Tok.getLiteralData())
1434       return *D;
1435
1436     // Otherwise, fall back on getCharacterData, which is slower, but always
1437     // works.
1438     return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
1439   }
1440
1441   /// \brief Retrieve the name of the immediate macro expansion.
1442   ///
1443   /// This routine starts from a source location, and finds the name of the
1444   /// macro responsible for its immediate expansion. It looks through any
1445   /// intervening macro argument expansions to compute this. It returns a
1446   /// StringRef that refers to the SourceManager-owned buffer of the source
1447   /// where that macro name is spelled. Thus, the result shouldn't out-live
1448   /// the SourceManager.
1449   StringRef getImmediateMacroName(SourceLocation Loc) {
1450     return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
1451   }
1452
1453   /// \brief Plop the specified string into a scratch buffer and set the
1454   /// specified token's location and length to it. 
1455   ///
1456   /// If specified, the source location provides a location of the expansion
1457   /// point of the token.
1458   void CreateString(StringRef Str, Token &Tok,
1459                     SourceLocation ExpansionLocStart = SourceLocation(),
1460                     SourceLocation ExpansionLocEnd = SourceLocation());
1461
1462   /// \brief Computes the source location just past the end of the
1463   /// token at this source location.
1464   ///
1465   /// This routine can be used to produce a source location that
1466   /// points just past the end of the token referenced by \p Loc, and
1467   /// is generally used when a diagnostic needs to point just after a
1468   /// token where it expected something different that it received. If
1469   /// the returned source location would not be meaningful (e.g., if
1470   /// it points into a macro), this routine returns an invalid
1471   /// source location.
1472   ///
1473   /// \param Offset an offset from the end of the token, where the source
1474   /// location should refer to. The default offset (0) produces a source
1475   /// location pointing just past the end of the token; an offset of 1 produces
1476   /// a source location pointing to the last character in the token, etc.
1477   SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
1478     return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
1479   }
1480
1481   /// \brief Returns true if the given MacroID location points at the first
1482   /// token of the macro expansion.
1483   ///
1484   /// \param MacroBegin If non-null and function returns true, it is set to
1485   /// begin location of the macro.
1486   bool isAtStartOfMacroExpansion(SourceLocation loc,
1487                                  SourceLocation *MacroBegin = nullptr) const {
1488     return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
1489                                             MacroBegin);
1490   }
1491
1492   /// \brief Returns true if the given MacroID location points at the last
1493   /// token of the macro expansion.
1494   ///
1495   /// \param MacroEnd If non-null and function returns true, it is set to
1496   /// end location of the macro.
1497   bool isAtEndOfMacroExpansion(SourceLocation loc,
1498                                SourceLocation *MacroEnd = nullptr) const {
1499     return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
1500   }
1501
1502   /// \brief Print the token to stderr, used for debugging.
1503   void DumpToken(const Token &Tok, bool DumpFlags = false) const;
1504   void DumpLocation(SourceLocation Loc) const;
1505   void DumpMacro(const MacroInfo &MI) const;
1506   void dumpMacroInfo(const IdentifierInfo *II);
1507
1508   /// \brief Given a location that specifies the start of a
1509   /// token, return a new location that specifies a character within the token.
1510   SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
1511                                          unsigned Char) const {
1512     return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
1513   }
1514
1515   /// \brief Increment the counters for the number of token paste operations
1516   /// performed.
1517   ///
1518   /// If fast was specified, this is a 'fast paste' case we handled.
1519   void IncrementPasteCounter(bool isFast) {
1520     if (isFast)
1521       ++NumFastTokenPaste;
1522     else
1523       ++NumTokenPaste;
1524   }
1525
1526   void PrintStats();
1527
1528   size_t getTotalMemory() const;
1529
1530   /// When the macro expander pastes together a comment (/##/) in Microsoft
1531   /// mode, this method handles updating the current state, returning the
1532   /// token on the next source line.
1533   void HandleMicrosoftCommentPaste(Token &Tok);
1534
1535   //===--------------------------------------------------------------------===//
1536   // Preprocessor callback methods.  These are invoked by a lexer as various
1537   // directives and events are found.
1538
1539   /// Given a tok::raw_identifier token, look up the
1540   /// identifier information for the token and install it into the token,
1541   /// updating the token kind accordingly.
1542   IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
1543
1544 private:
1545   llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
1546
1547 public:
1548
1549   /// \brief Specifies the reason for poisoning an identifier.
1550   ///
1551   /// If that identifier is accessed while poisoned, then this reason will be
1552   /// used instead of the default "poisoned" diagnostic.
1553   void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
1554
1555   /// \brief Display reason for poisoned identifier.
1556   void HandlePoisonedIdentifier(Token & Tok);
1557
1558   void MaybeHandlePoisonedIdentifier(Token & Identifier) {
1559     if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
1560       if(II->isPoisoned()) {
1561         HandlePoisonedIdentifier(Identifier);
1562       }
1563     }
1564   }
1565
1566 private:
1567   /// Identifiers used for SEH handling in Borland. These are only
1568   /// allowed in particular circumstances
1569   // __except block
1570   IdentifierInfo *Ident__exception_code,
1571                  *Ident___exception_code,
1572                  *Ident_GetExceptionCode;
1573   // __except filter expression
1574   IdentifierInfo *Ident__exception_info,
1575                  *Ident___exception_info,
1576                  *Ident_GetExceptionInfo;
1577   // __finally
1578   IdentifierInfo *Ident__abnormal_termination,
1579                  *Ident___abnormal_termination,
1580                  *Ident_AbnormalTermination;
1581
1582   const char *getCurLexerEndPos();
1583
1584 public:
1585   void PoisonSEHIdentifiers(bool Poison = true); // Borland
1586
1587   /// \brief Callback invoked when the lexer reads an identifier and has
1588   /// filled in the tokens IdentifierInfo member. 
1589   ///
1590   /// This callback potentially macro expands it or turns it into a named
1591   /// token (like 'for').
1592   ///
1593   /// \returns true if we actually computed a token, false if we need to
1594   /// lex again.
1595   bool HandleIdentifier(Token &Identifier);
1596
1597
1598   /// \brief Callback invoked when the lexer hits the end of the current file.
1599   ///
1600   /// This either returns the EOF token and returns true, or
1601   /// pops a level off the include stack and returns false, at which point the
1602   /// client should call lex again.
1603   bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
1604
1605   /// \brief Callback invoked when the current TokenLexer hits the end of its
1606   /// token stream.
1607   bool HandleEndOfTokenLexer(Token &Result);
1608
1609   /// \brief Callback invoked when the lexer sees a # token at the start of a
1610   /// line.
1611   ///
1612   /// This consumes the directive, modifies the lexer/preprocessor state, and
1613   /// advances the lexer(s) so that the next token read is the correct one.
1614   void HandleDirective(Token &Result);
1615
1616   /// \brief Ensure that the next token is a tok::eod token.
1617   ///
1618   /// If not, emit a diagnostic and consume up until the eod.
1619   /// If \p EnableMacros is true, then we consider macros that expand to zero
1620   /// tokens as being ok.
1621   void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
1622
1623   /// \brief Read and discard all tokens remaining on the current line until
1624   /// the tok::eod token is found.
1625   void DiscardUntilEndOfDirective();
1626
1627   /// \brief Returns true if the preprocessor has seen a use of
1628   /// __DATE__ or __TIME__ in the file so far.
1629   bool SawDateOrTime() const {
1630     return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
1631   }
1632   unsigned getCounterValue() const { return CounterValue; }
1633   void setCounterValue(unsigned V) { CounterValue = V; }
1634
1635   /// \brief Retrieves the module that we're currently building, if any.
1636   Module *getCurrentModule();
1637   
1638   /// \brief Allocate a new MacroInfo object with the provided SourceLocation.
1639   MacroInfo *AllocateMacroInfo(SourceLocation L);
1640
1641   /// \brief Allocate a new MacroInfo object loaded from an AST file.
1642   MacroInfo *AllocateDeserializedMacroInfo(SourceLocation L,
1643                                            unsigned SubModuleID);
1644
1645   /// \brief Turn the specified lexer token into a fully checked and spelled
1646   /// filename, e.g. as an operand of \#include. 
1647   ///
1648   /// The caller is expected to provide a buffer that is large enough to hold
1649   /// the spelling of the filename, but is also expected to handle the case
1650   /// when this method decides to use a different buffer.
1651   ///
1652   /// \returns true if the input filename was in <>'s or false if it was
1653   /// in ""'s.
1654   bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1655
1656   /// \brief Given a "foo" or \<foo> reference, look up the indicated file.
1657   ///
1658   /// Returns null on failure.  \p isAngled indicates whether the file
1659   /// reference is for system \#include's or not (i.e. using <> instead of "").
1660   const FileEntry *LookupFile(SourceLocation FilenameLoc, StringRef Filename,
1661                               bool isAngled, const DirectoryLookup *FromDir,
1662                               const FileEntry *FromFile,
1663                               const DirectoryLookup *&CurDir,
1664                               SmallVectorImpl<char> *SearchPath,
1665                               SmallVectorImpl<char> *RelativePath,
1666                               ModuleMap::KnownHeader *SuggestedModule,
1667                               bool SkipCache = false);
1668
1669   /// \brief Get the DirectoryLookup structure used to find the current
1670   /// FileEntry, if CurLexer is non-null and if applicable. 
1671   ///
1672   /// This allows us to implement \#include_next and find directory-specific
1673   /// properties.
1674   const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1675
1676   /// \brief Return true if we're in the top-level file, not in a \#include.
1677   bool isInPrimaryFile() const;
1678
1679   /// \brief Handle cases where the \#include name is expanded
1680   /// from a macro as multiple tokens, which need to be glued together. 
1681   ///
1682   /// This occurs for code like:
1683   /// \code
1684   ///    \#define FOO <x/y.h>
1685   ///    \#include FOO
1686   /// \endcode
1687   /// because in this case, "<x/y.h>" is returned as 7 tokens, not one.
1688   ///
1689   /// This code concatenates and consumes tokens up to the '>' token.  It
1690   /// returns false if the > was found, otherwise it returns true if it finds
1691   /// and consumes the EOD marker.
1692   bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1693                               SourceLocation &End);
1694
1695   /// \brief Lex an on-off-switch (C99 6.10.6p2) and verify that it is
1696   /// followed by EOD.  Return true if the token is not a valid on-off-switch.
1697   bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
1698
1699   bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
1700                       bool *ShadowFlag = nullptr);
1701
1702 private:
1703
1704   void PushIncludeMacroStack() {
1705     assert(CurLexerKind != CLK_CachingLexer && "cannot push a caching lexer");
1706     IncludeMacroStack.emplace_back(
1707         CurLexerKind, CurSubmodule, std::move(CurLexer), std::move(CurPTHLexer),
1708         CurPPLexer, std::move(CurTokenLexer), CurDirLookup);
1709     CurPPLexer = nullptr;
1710   }
1711
1712   void PopIncludeMacroStack() {
1713     CurLexer = std::move(IncludeMacroStack.back().TheLexer);
1714     CurPTHLexer = std::move(IncludeMacroStack.back().ThePTHLexer);
1715     CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1716     CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
1717     CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1718     CurSubmodule = IncludeMacroStack.back().TheSubmodule;
1719     CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1720     IncludeMacroStack.pop_back();
1721   }
1722
1723   void PropagateLineStartLeadingSpaceInfo(Token &Result);
1724
1725   void EnterSubmodule(Module *M, SourceLocation ImportLoc);
1726   void LeaveSubmodule();
1727
1728   /// Determine whether we need to create module macros for #defines in the
1729   /// current context.
1730   bool needModuleMacros() const;
1731
1732   /// Update the set of active module macros and ambiguity flag for a module
1733   /// macro name.
1734   void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
1735
1736   /// \brief Allocate a new MacroInfo object.
1737   MacroInfo *AllocateMacroInfo();
1738
1739   DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
1740                                                SourceLocation Loc);
1741   UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
1742   VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
1743                                                              bool isPublic);
1744
1745   /// \brief Lex and validate a macro name, which occurs after a
1746   /// \#define or \#undef.
1747   ///
1748   /// \param MacroNameTok Token that represents the name defined or undefined.
1749   /// \param IsDefineUndef Kind if preprocessor directive.
1750   /// \param ShadowFlag Points to flag that is set if macro name shadows
1751   ///                   a keyword.
1752   ///
1753   /// This emits a diagnostic, sets the token kind to eod,
1754   /// and discards the rest of the macro line if the macro name is invalid.
1755   void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other,
1756                      bool *ShadowFlag = nullptr);
1757
1758   /// The ( starting an argument list of a macro definition has just been read.
1759   /// Lex the rest of the arguments and the closing ), updating \p MI with
1760   /// what we learn and saving in \p LastTok the last token read.
1761   /// Return true if an error occurs parsing the arg list.
1762   bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok);
1763
1764   /// We just read a \#if or related directive and decided that the
1765   /// subsequent tokens are in the \#if'd out portion of the
1766   /// file.  Lex the rest of the file, until we see an \#endif.  If \p
1767   /// FoundNonSkipPortion is true, then we have already emitted code for part of
1768   /// this \#if directive, so \#else/\#elif blocks should never be entered. If
1769   /// \p FoundElse is false, then \#else directives are ok, if not, then we have
1770   /// already seen one so a \#else directive is a duplicate.  When this returns,
1771   /// the caller can lex the first valid token.
1772   void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1773                                     bool FoundNonSkipPortion, bool FoundElse,
1774                                     SourceLocation ElseLoc = SourceLocation());
1775
1776   /// \brief A fast PTH version of SkipExcludedConditionalBlock.
1777   void PTHSkipExcludedConditionalBlock();
1778
1779   /// \brief Evaluate an integer constant expression that may occur after a
1780   /// \#if or \#elif directive and return it as a bool.
1781   ///
1782   /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
1783   bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
1784
1785   /// \brief Install the standard preprocessor pragmas:
1786   /// \#pragma GCC poison/system_header/dependency and \#pragma once.
1787   void RegisterBuiltinPragmas();
1788
1789   /// \brief Register builtin macros such as __LINE__ with the identifier table.
1790   void RegisterBuiltinMacros();
1791
1792   /// If an identifier token is read that is to be expanded as a macro, handle
1793   /// it and return the next token as 'Tok'.  If we lexed a token, return true;
1794   /// otherwise the caller should lex again.
1795   bool HandleMacroExpandedIdentifier(Token &Tok, const MacroDefinition &MD);
1796
1797   /// \brief Cache macro expanded tokens for TokenLexers.
1798   //
1799   /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1800   /// going to lex in the cache and when it finishes the tokens are removed
1801   /// from the end of the cache.
1802   Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
1803                                   ArrayRef<Token> tokens);
1804   void removeCachedMacroExpandedTokensOfLastLexer();
1805   friend void TokenLexer::ExpandFunctionArguments();
1806
1807   /// Determine whether the next preprocessor token to be
1808   /// lexed is a '('.  If so, consume the token and return true, if not, this
1809   /// method should have no observable side-effect on the lexed tokens.
1810   bool isNextPPTokenLParen();
1811
1812   /// After reading "MACRO(", this method is invoked to read all of the formal
1813   /// arguments specified for the macro invocation.  Returns null on error.
1814   MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
1815                                        SourceLocation &ExpansionEnd);
1816
1817   /// \brief If an identifier token is read that is to be expanded
1818   /// as a builtin macro, handle it and return the next token as 'Tok'.
1819   void ExpandBuiltinMacro(Token &Tok);
1820
1821   /// \brief Read a \c _Pragma directive, slice it up, process it, then
1822   /// return the first token after the directive.
1823   /// This assumes that the \c _Pragma token has just been read into \p Tok.
1824   void Handle_Pragma(Token &Tok);
1825
1826   /// \brief Like Handle_Pragma except the pragma text is not enclosed within
1827   /// a string literal.
1828   void HandleMicrosoft__pragma(Token &Tok);
1829
1830   /// \brief Add a lexer to the top of the include stack and
1831   /// start lexing tokens from it instead of the current buffer.
1832   void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
1833
1834   /// \brief Add a lexer to the top of the include stack and
1835   /// start getting tokens from it using the PTH cache.
1836   void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
1837
1838   /// \brief Set the FileID for the preprocessor predefines.
1839   void setPredefinesFileID(FileID FID) {
1840     assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
1841     PredefinesFileID = FID;
1842   }
1843
1844   /// \brief Returns true if we are lexing from a file and not a
1845   /// pragma or a macro.
1846   static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
1847     return L ? !L->isPragmaLexer() : P != nullptr;
1848   }
1849
1850   static bool IsFileLexer(const IncludeStackInfo& I) {
1851     return IsFileLexer(I.TheLexer.get(), I.ThePPLexer);
1852   }
1853
1854   bool IsFileLexer() const {
1855     return IsFileLexer(CurLexer.get(), CurPPLexer);
1856   }
1857
1858   //===--------------------------------------------------------------------===//
1859   // Caching stuff.
1860   void CachingLex(Token &Result);
1861   bool InCachingLexMode() const {
1862     // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
1863     // that we are past EOF, not that we are in CachingLex mode.
1864     return !CurPPLexer && !CurTokenLexer && !CurPTHLexer &&
1865            !IncludeMacroStack.empty();
1866   }
1867   void EnterCachingLexMode();
1868   void ExitCachingLexMode() {
1869     if (InCachingLexMode())
1870       RemoveTopOfLexerStack();
1871   }
1872   const Token &PeekAhead(unsigned N);
1873   void AnnotatePreviousCachedTokens(const Token &Tok);
1874
1875   //===--------------------------------------------------------------------===//
1876   /// Handle*Directive - implement the various preprocessor directives.  These
1877   /// should side-effect the current preprocessor object so that the next call
1878   /// to Lex() will return the appropriate token next.
1879   void HandleLineDirective();
1880   void HandleDigitDirective(Token &Tok);
1881   void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1882   void HandleIdentSCCSDirective(Token &Tok);
1883   void HandleMacroPublicDirective(Token &Tok);
1884   void HandleMacroPrivateDirective();
1885
1886   // File inclusion.
1887   void HandleIncludeDirective(SourceLocation HashLoc,
1888                               Token &Tok,
1889                               const DirectoryLookup *LookupFrom = nullptr,
1890                               const FileEntry *LookupFromFile = nullptr,
1891                               bool isImport = false);
1892   void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1893   void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1894   void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
1895   void HandleMicrosoftImportDirective(Token &Tok);
1896
1897 public:
1898   // Module inclusion testing.
1899   /// \brief Find the module that owns the source or header file that
1900   /// \p Loc points to. If the location is in a file that was included
1901   /// into a module, or is outside any module, returns nullptr.
1902   Module *getModuleForLocation(SourceLocation Loc);
1903
1904   /// \brief Find the module that contains the specified location, either
1905   /// directly or indirectly.
1906   Module *getModuleContainingLocation(SourceLocation Loc);
1907
1908   /// \brief We want to produce a diagnostic at location IncLoc concerning a
1909   /// missing module import.
1910   ///
1911   /// \param IncLoc The location at which the missing import was detected.
1912   /// \param MLoc A location within the desired module at which some desired
1913   ///        effect occurred (eg, where a desired entity was declared).
1914   ///
1915   /// \return A file that can be #included to import a module containing MLoc.
1916   ///         Null if no such file could be determined or if a #include is not
1917   ///         appropriate.
1918   const FileEntry *getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
1919                                                           SourceLocation MLoc);
1920
1921 private:
1922   // Macro handling.
1923   void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterTopLevelIfndef);
1924   void HandleUndefDirective();
1925
1926   // Conditional Inclusion.
1927   void HandleIfdefDirective(Token &Tok, bool isIfndef,
1928                             bool ReadAnyTokensBeforeDirective);
1929   void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1930   void HandleEndifDirective(Token &Tok);
1931   void HandleElseDirective(Token &Tok);
1932   void HandleElifDirective(Token &Tok);
1933
1934   // Pragmas.
1935   void HandlePragmaDirective(SourceLocation IntroducerLoc,
1936                              PragmaIntroducerKind Introducer);
1937 public:
1938   void HandlePragmaOnce(Token &OnceTok);
1939   void HandlePragmaMark();
1940   void HandlePragmaPoison();
1941   void HandlePragmaSystemHeader(Token &SysHeaderTok);
1942   void HandlePragmaDependency(Token &DependencyTok);
1943   void HandlePragmaPushMacro(Token &Tok);
1944   void HandlePragmaPopMacro(Token &Tok);
1945   void HandlePragmaIncludeAlias(Token &Tok);
1946   IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1947
1948   // Return true and store the first token only if any CommentHandler
1949   // has inserted some tokens and getCommentRetentionState() is false.
1950   bool HandleComment(Token &Token, SourceRange Comment);
1951
1952   /// \brief A macro is used, update information about macros that need unused
1953   /// warnings.
1954   void markMacroAsUsed(MacroInfo *MI);
1955 };
1956
1957 /// \brief Abstract base class that describes a handler that will receive
1958 /// source ranges for each of the comments encountered in the source file.
1959 class CommentHandler {
1960 public:
1961   virtual ~CommentHandler();
1962
1963   // The handler shall return true if it has pushed any tokens
1964   // to be read using e.g. EnterToken or EnterTokenStream.
1965   virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
1966 };
1967
1968 /// \brief Registry of pragma handlers added by plugins
1969 typedef llvm::Registry<PragmaHandler> PragmaHandlerRegistry;
1970
1971 }  // end namespace clang
1972
1973 #endif