]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Lex/Preprocessor.h
Merge ^/head r311132 through r311305.
[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 : public RefCountedBase<Preprocessor> {
98   IntrusiveRefCntPtr<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(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
654                DiagnosticsEngine &diags, LangOptions &opts,
655                SourceManager &SM, HeaderSearch &Headers,
656                ModuleLoader &TheModuleLoader,
657                IdentifierInfoLookup *IILookup = nullptr,
658                bool OwnsHeaderSearch = false,
659                TranslationUnitKind TUKind = TU_Complete);
660
661   ~Preprocessor();
662
663   /// \brief Initialize the preprocessor using information about the target.
664   ///
665   /// \param Target is owned by the caller and must remain valid for the
666   /// lifetime of the preprocessor.
667   /// \param AuxTarget is owned by the caller and must remain valid for
668   /// the lifetime of the preprocessor.
669   void Initialize(const TargetInfo &Target,
670                   const TargetInfo *AuxTarget = nullptr);
671
672   /// \brief Initialize the preprocessor to parse a model file
673   ///
674   /// To parse model files the preprocessor of the original source is reused to
675   /// preserver the identifier table. However to avoid some duplicate
676   /// information in the preprocessor some cleanup is needed before it is used
677   /// to parse model files. This method does that cleanup.
678   void InitializeForModelFile();
679
680   /// \brief Cleanup after model file parsing
681   void FinalizeForModelFile();
682
683   /// \brief Retrieve the preprocessor options used to initialize this
684   /// preprocessor.
685   PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
686   
687   DiagnosticsEngine &getDiagnostics() const { return *Diags; }
688   void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
689
690   const LangOptions &getLangOpts() const { return LangOpts; }
691   const TargetInfo &getTargetInfo() const { return *Target; }
692   const TargetInfo *getAuxTargetInfo() const { return AuxTarget; }
693   FileManager &getFileManager() const { return FileMgr; }
694   SourceManager &getSourceManager() const { return SourceMgr; }
695   HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
696
697   IdentifierTable &getIdentifierTable() { return Identifiers; }
698   const IdentifierTable &getIdentifierTable() const { return Identifiers; }
699   SelectorTable &getSelectorTable() { return Selectors; }
700   Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
701   llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
702
703   void setPTHManager(PTHManager* pm);
704
705   PTHManager *getPTHManager() { return PTH.get(); }
706
707   void setExternalSource(ExternalPreprocessorSource *Source) {
708     ExternalSource = Source;
709   }
710
711   ExternalPreprocessorSource *getExternalSource() const {
712     return ExternalSource;
713   }
714
715   /// \brief Retrieve the module loader associated with this preprocessor.
716   ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
717
718   bool hadModuleLoaderFatalFailure() const {
719     return TheModuleLoader.HadFatalFailure;
720   }
721
722   /// \brief True if we are currently preprocessing a #if or #elif directive
723   bool isParsingIfOrElifDirective() const { 
724     return ParsingIfOrElifDirective;
725   }
726
727   /// \brief Control whether the preprocessor retains comments in output.
728   void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
729     this->KeepComments = KeepComments | KeepMacroComments;
730     this->KeepMacroComments = KeepMacroComments;
731   }
732
733   bool getCommentRetentionState() const { return KeepComments; }
734
735   void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
736   bool getPragmasEnabled() const { return PragmasEnabled; }
737
738   void SetSuppressIncludeNotFoundError(bool Suppress) {
739     SuppressIncludeNotFoundError = Suppress;
740   }
741
742   bool GetSuppressIncludeNotFoundError() {
743     return SuppressIncludeNotFoundError;
744   }
745
746   /// Sets whether the preprocessor is responsible for producing output or if
747   /// it is producing tokens to be consumed by Parse and Sema.
748   void setPreprocessedOutput(bool IsPreprocessedOutput) {
749     PreprocessedOutput = IsPreprocessedOutput;
750   }
751
752   /// Returns true if the preprocessor is responsible for generating output,
753   /// false if it is producing tokens to be consumed by Parse and Sema.
754   bool isPreprocessedOutput() const { return PreprocessedOutput; }
755
756   /// \brief Return true if we are lexing directly from the specified lexer.
757   bool isCurrentLexer(const PreprocessorLexer *L) const {
758     return CurPPLexer == L;
759   }
760
761   /// \brief Return the current lexer being lexed from.
762   ///
763   /// Note that this ignores any potentially active macro expansions and _Pragma
764   /// expansions going on at the time.
765   PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
766
767   /// \brief Return the current file lexer being lexed from.
768   ///
769   /// Note that this ignores any potentially active macro expansions and _Pragma
770   /// expansions going on at the time.
771   PreprocessorLexer *getCurrentFileLexer() const;
772
773   /// \brief Return the submodule owning the file being lexed.
774   Module *getCurrentSubmodule() const { return CurSubmodule; }
775
776   /// \brief Returns the FileID for the preprocessor predefines.
777   FileID getPredefinesFileID() const { return PredefinesFileID; }
778
779   /// \{
780   /// \brief Accessors for preprocessor callbacks.
781   ///
782   /// Note that this class takes ownership of any PPCallbacks object given to
783   /// it.
784   PPCallbacks *getPPCallbacks() const { return Callbacks.get(); }
785   void addPPCallbacks(std::unique_ptr<PPCallbacks> C) {
786     if (Callbacks)
787       C = llvm::make_unique<PPChainedCallbacks>(std::move(C),
788                                                 std::move(Callbacks));
789     Callbacks = std::move(C);
790   }
791   /// \}
792
793   bool isMacroDefined(StringRef Id) {
794     return isMacroDefined(&Identifiers.get(Id));
795   }
796   bool isMacroDefined(const IdentifierInfo *II) {
797     return II->hasMacroDefinition() &&
798            (!getLangOpts().Modules || (bool)getMacroDefinition(II));
799   }
800
801   /// \brief Determine whether II is defined as a macro within the module M,
802   /// if that is a module that we've already preprocessed. Does not check for
803   /// macros imported into M.
804   bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M) {
805     if (!II->hasMacroDefinition())
806       return false;
807     auto I = Submodules.find(M);
808     if (I == Submodules.end())
809       return false;
810     auto J = I->second.Macros.find(II);
811     if (J == I->second.Macros.end())
812       return false;
813     auto *MD = J->second.getLatest();
814     return MD && MD->isDefined();
815   }
816
817   MacroDefinition getMacroDefinition(const IdentifierInfo *II) {
818     if (!II->hasMacroDefinition())
819       return MacroDefinition();
820
821     MacroState &S = CurSubmoduleState->Macros[II];
822     auto *MD = S.getLatest();
823     while (MD && isa<VisibilityMacroDirective>(MD))
824       MD = MD->getPrevious();
825     return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD),
826                            S.getActiveModuleMacros(*this, II),
827                            S.isAmbiguous(*this, II));
828   }
829
830   MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II,
831                                           SourceLocation Loc) {
832     if (!II->hadMacroDefinition())
833       return MacroDefinition();
834
835     MacroState &S = CurSubmoduleState->Macros[II];
836     MacroDirective::DefInfo DI;
837     if (auto *MD = S.getLatest())
838       DI = MD->findDirectiveAtLoc(Loc, getSourceManager());
839     // FIXME: Compute the set of active module macros at the specified location.
840     return MacroDefinition(DI.getDirective(),
841                            S.getActiveModuleMacros(*this, II),
842                            S.isAmbiguous(*this, II));
843   }
844
845   /// \brief Given an identifier, return its latest non-imported MacroDirective
846   /// if it is \#define'd and not \#undef'd, or null if it isn't \#define'd.
847   MacroDirective *getLocalMacroDirective(const IdentifierInfo *II) const {
848     if (!II->hasMacroDefinition())
849       return nullptr;
850
851     auto *MD = getLocalMacroDirectiveHistory(II);
852     if (!MD || MD->getDefinition().isUndefined())
853       return nullptr;
854
855     return MD;
856   }
857
858   const MacroInfo *getMacroInfo(const IdentifierInfo *II) const {
859     return const_cast<Preprocessor*>(this)->getMacroInfo(II);
860   }
861
862   MacroInfo *getMacroInfo(const IdentifierInfo *II) {
863     if (!II->hasMacroDefinition())
864       return nullptr;
865     if (auto MD = getMacroDefinition(II))
866       return MD.getMacroInfo();
867     return nullptr;
868   }
869
870   /// \brief Given an identifier, return the latest non-imported macro
871   /// directive for that identifier.
872   ///
873   /// One can iterate over all previous macro directives from the most recent
874   /// one.
875   MacroDirective *getLocalMacroDirectiveHistory(const IdentifierInfo *II) const;
876
877   /// \brief Add a directive to the macro directive history for this identifier.
878   void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
879   DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI,
880                                              SourceLocation Loc) {
881     DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc);
882     appendMacroDirective(II, MD);
883     return MD;
884   }
885   DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II,
886                                              MacroInfo *MI) {
887     return appendDefMacroDirective(II, MI, MI->getDefinitionLoc());
888   }
889   /// \brief Set a MacroDirective that was loaded from a PCH file.
890   void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED,
891                                MacroDirective *MD);
892
893   /// \brief Register an exported macro for a module and identifier.
894   ModuleMacro *addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro,
895                               ArrayRef<ModuleMacro *> Overrides, bool &IsNew);
896   ModuleMacro *getModuleMacro(Module *Mod, IdentifierInfo *II);
897
898   /// \brief Get the list of leaf (non-overridden) module macros for a name.
899   ArrayRef<ModuleMacro*> getLeafModuleMacros(const IdentifierInfo *II) const {
900     if (II->isOutOfDate())
901       updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II));
902     auto I = LeafModuleMacros.find(II);
903     if (I != LeafModuleMacros.end())
904       return I->second;
905     return None;
906   }
907
908   /// \{
909   /// Iterators for the macro history table. Currently defined macros have
910   /// IdentifierInfo::hasMacroDefinition() set and an empty
911   /// MacroInfo::getUndefLoc() at the head of the list.
912   typedef MacroMap::const_iterator macro_iterator;
913   macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
914   macro_iterator macro_end(bool IncludeExternalMacros = true) const;
915   llvm::iterator_range<macro_iterator>
916   macros(bool IncludeExternalMacros = true) const {
917     return llvm::make_range(macro_begin(IncludeExternalMacros),
918                             macro_end(IncludeExternalMacros));
919   }
920   /// \}
921
922   /// \brief Return the name of the macro defined before \p Loc that has
923   /// spelling \p Tokens.  If there are multiple macros with same spelling,
924   /// return the last one defined.
925   StringRef getLastMacroWithSpelling(SourceLocation Loc,
926                                      ArrayRef<TokenValue> Tokens) const;
927
928   const std::string &getPredefines() const { return Predefines; }
929   /// \brief Set the predefines for this Preprocessor.
930   ///
931   /// These predefines are automatically injected when parsing the main file.
932   void setPredefines(const char *P) { Predefines = P; }
933   void setPredefines(StringRef P) { Predefines = P; }
934
935   /// Return information about the specified preprocessor
936   /// identifier token.
937   IdentifierInfo *getIdentifierInfo(StringRef Name) const {
938     return &Identifiers.get(Name);
939   }
940
941   /// \brief Add the specified pragma handler to this preprocessor.
942   ///
943   /// If \p Namespace is non-null, then it is a token required to exist on the
944   /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
945   void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
946   void AddPragmaHandler(PragmaHandler *Handler) {
947     AddPragmaHandler(StringRef(), Handler);
948   }
949
950   /// \brief Remove the specific pragma handler from this preprocessor.
951   ///
952   /// If \p Namespace is non-null, then it should be the namespace that
953   /// \p Handler was added to. It is an error to remove a handler that
954   /// has not been registered.
955   void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
956   void RemovePragmaHandler(PragmaHandler *Handler) {
957     RemovePragmaHandler(StringRef(), Handler);
958   }
959
960   /// Install empty handlers for all pragmas (making them ignored).
961   void IgnorePragmas();
962
963   /// \brief Add the specified comment handler to the preprocessor.
964   void addCommentHandler(CommentHandler *Handler);
965
966   /// \brief Remove the specified comment handler.
967   ///
968   /// It is an error to remove a handler that has not been registered.
969   void removeCommentHandler(CommentHandler *Handler);
970
971   /// \brief Set the code completion handler to the given object.
972   void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
973     CodeComplete = &Handler;
974   }
975
976   /// \brief Retrieve the current code-completion handler.
977   CodeCompletionHandler *getCodeCompletionHandler() const {
978     return CodeComplete;
979   }
980
981   /// \brief Clear out the code completion handler.
982   void clearCodeCompletionHandler() {
983     CodeComplete = nullptr;
984   }
985
986   /// \brief Hook used by the lexer to invoke the "natural language" code
987   /// completion point.
988   void CodeCompleteNaturalLanguage();
989
990   /// \brief Set the code completion token for filtering purposes.
991   void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter) {
992     CodeCompletionII = Filter;
993   }
994
995   /// \brief Get the code completion token for filtering purposes.
996   StringRef getCodeCompletionFilter() {
997     if (CodeCompletionII)
998       return CodeCompletionII->getName();
999     return {};
1000   }
1001
1002   /// \brief Retrieve the preprocessing record, or NULL if there is no
1003   /// preprocessing record.
1004   PreprocessingRecord *getPreprocessingRecord() const { return Record; }
1005
1006   /// \brief Create a new preprocessing record, which will keep track of
1007   /// all macro expansions, macro definitions, etc.
1008   void createPreprocessingRecord();
1009
1010   /// \brief Enter the specified FileID as the main source file,
1011   /// which implicitly adds the builtin defines etc.
1012   void EnterMainSourceFile();
1013
1014   /// \brief Inform the preprocessor callbacks that processing is complete.
1015   void EndSourceFile();
1016
1017   /// \brief Add a source file to the top of the include stack and
1018   /// start lexing tokens from it instead of the current buffer.
1019   ///
1020   /// Emits a diagnostic, doesn't enter the file, and returns true on error.
1021   bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
1022                        SourceLocation Loc);
1023
1024   /// \brief Add a Macro to the top of the include stack and start lexing
1025   /// tokens from it instead of the current buffer.
1026   ///
1027   /// \param Args specifies the tokens input to a function-like macro.
1028   /// \param ILEnd specifies the location of the ')' for a function-like macro
1029   /// or the identifier for an object-like macro.
1030   void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroInfo *Macro,
1031                   MacroArgs *Args);
1032
1033   /// \brief Add a "macro" context to the top of the include stack,
1034   /// which will cause the lexer to start returning the specified tokens.
1035   ///
1036   /// If \p DisableMacroExpansion is true, tokens lexed from the token stream
1037   /// will not be subject to further macro expansion. Otherwise, these tokens
1038   /// will be re-macro-expanded when/if expansion is enabled.
1039   ///
1040   /// If \p OwnsTokens is false, this method assumes that the specified stream
1041   /// of tokens has a permanent owner somewhere, so they do not need to be
1042   /// copied. If it is true, it assumes the array of tokens is allocated with
1043   /// \c new[] and the Preprocessor will delete[] it.
1044 private:
1045   void EnterTokenStream(const Token *Toks, unsigned NumToks,
1046                         bool DisableMacroExpansion, bool OwnsTokens);
1047
1048 public:
1049   void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks,
1050                         bool DisableMacroExpansion) {
1051     EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true);
1052   }
1053   void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion) {
1054     EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false);
1055   }
1056
1057   /// \brief Pop the current lexer/macro exp off the top of the lexer stack.
1058   ///
1059   /// This should only be used in situations where the current state of the
1060   /// top-of-stack lexer is known.
1061   void RemoveTopOfLexerStack();
1062
1063   /// From the point that this method is called, and until
1064   /// CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
1065   /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
1066   /// make the Preprocessor re-lex the same tokens.
1067   ///
1068   /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
1069   /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
1070   /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
1071   ///
1072   /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
1073   /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
1074   /// tokens will continue indefinitely.
1075   ///
1076   void EnableBacktrackAtThisPos();
1077
1078   /// \brief Disable the last EnableBacktrackAtThisPos call.
1079   void CommitBacktrackedTokens();
1080
1081   /// \brief Make Preprocessor re-lex the tokens that were lexed since
1082   /// EnableBacktrackAtThisPos() was previously called.
1083   void Backtrack();
1084
1085   /// \brief True if EnableBacktrackAtThisPos() was called and
1086   /// caching of tokens is on.
1087   bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
1088
1089   /// \brief Lex the next token for this preprocessor.
1090   void Lex(Token &Result);
1091
1092   void LexAfterModuleImport(Token &Result);
1093
1094   void makeModuleVisible(Module *M, SourceLocation Loc);
1095
1096   SourceLocation getModuleImportLoc(Module *M) const {
1097     return CurSubmoduleState->VisibleModules.getImportLoc(M);
1098   }
1099
1100   /// \brief Lex a string literal, which may be the concatenation of multiple
1101   /// string literals and may even come from macro expansion.
1102   /// \returns true on success, false if a error diagnostic has been generated.
1103   bool LexStringLiteral(Token &Result, std::string &String,
1104                         const char *DiagnosticTag, bool AllowMacroExpansion) {
1105     if (AllowMacroExpansion)
1106       Lex(Result);
1107     else
1108       LexUnexpandedToken(Result);
1109     return FinishLexStringLiteral(Result, String, DiagnosticTag,
1110                                   AllowMacroExpansion);
1111   }
1112
1113   /// \brief Complete the lexing of a string literal where the first token has
1114   /// already been lexed (see LexStringLiteral).
1115   bool FinishLexStringLiteral(Token &Result, std::string &String,
1116                               const char *DiagnosticTag,
1117                               bool AllowMacroExpansion);
1118
1119   /// \brief Lex a token.  If it's a comment, keep lexing until we get
1120   /// something not a comment.
1121   ///
1122   /// This is useful in -E -C mode where comments would foul up preprocessor
1123   /// directive handling.
1124   void LexNonComment(Token &Result) {
1125     do
1126       Lex(Result);
1127     while (Result.getKind() == tok::comment);
1128   }
1129
1130   /// \brief Just like Lex, but disables macro expansion of identifier tokens.
1131   void LexUnexpandedToken(Token &Result) {
1132     // Disable macro expansion.
1133     bool OldVal = DisableMacroExpansion;
1134     DisableMacroExpansion = true;
1135     // Lex the token.
1136     Lex(Result);
1137
1138     // Reenable it.
1139     DisableMacroExpansion = OldVal;
1140   }
1141
1142   /// \brief Like LexNonComment, but this disables macro expansion of
1143   /// identifier tokens.
1144   void LexUnexpandedNonComment(Token &Result) {
1145     do
1146       LexUnexpandedToken(Result);
1147     while (Result.getKind() == tok::comment);
1148   }
1149
1150   /// \brief Parses a simple integer literal to get its numeric value.  Floating
1151   /// point literals and user defined literals are rejected.  Used primarily to
1152   /// handle pragmas that accept integer arguments.
1153   bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value);
1154
1155   /// Disables macro expansion everywhere except for preprocessor directives.
1156   void SetMacroExpansionOnlyInDirectives() {
1157     DisableMacroExpansion = true;
1158     MacroExpansionInDirectivesOverride = true;
1159   }
1160
1161   /// \brief Peeks ahead N tokens and returns that token without consuming any
1162   /// tokens.
1163   ///
1164   /// LookAhead(0) returns the next token that would be returned by Lex(),
1165   /// LookAhead(1) returns the token after it, etc.  This returns normal
1166   /// tokens after phase 5.  As such, it is equivalent to using
1167   /// 'Lex', not 'LexUnexpandedToken'.
1168   const Token &LookAhead(unsigned N) {
1169     if (CachedLexPos + N < CachedTokens.size())
1170       return CachedTokens[CachedLexPos+N];
1171     else
1172       return PeekAhead(N+1);
1173   }
1174
1175   /// \brief When backtracking is enabled and tokens are cached,
1176   /// this allows to revert a specific number of tokens.
1177   ///
1178   /// Note that the number of tokens being reverted should be up to the last
1179   /// backtrack position, not more.
1180   void RevertCachedTokens(unsigned N) {
1181     assert(isBacktrackEnabled() &&
1182            "Should only be called when tokens are cached for backtracking");
1183     assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
1184          && "Should revert tokens up to the last backtrack position, not more");
1185     assert(signed(CachedLexPos) - signed(N) >= 0 &&
1186            "Corrupted backtrack positions ?");
1187     CachedLexPos -= N;
1188   }
1189
1190   /// \brief Enters a token in the token stream to be lexed next.
1191   ///
1192   /// If BackTrack() is called afterwards, the token will remain at the
1193   /// insertion point.
1194   void EnterToken(const Token &Tok) {
1195     EnterCachingLexMode();
1196     CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
1197   }
1198
1199   /// We notify the Preprocessor that if it is caching tokens (because
1200   /// backtrack is enabled) it should replace the most recent cached tokens
1201   /// with the given annotation token. This function has no effect if
1202   /// backtracking is not enabled.
1203   ///
1204   /// Note that the use of this function is just for optimization, so that the
1205   /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
1206   /// invoked.
1207   void AnnotateCachedTokens(const Token &Tok) {
1208     assert(Tok.isAnnotation() && "Expected annotation token");
1209     if (CachedLexPos != 0 && isBacktrackEnabled())
1210       AnnotatePreviousCachedTokens(Tok);
1211   }
1212
1213   /// Get the location of the last cached token, suitable for setting the end
1214   /// location of an annotation token.
1215   SourceLocation getLastCachedTokenLocation() const {
1216     assert(CachedLexPos != 0);
1217     return CachedTokens[CachedLexPos-1].getLastLoc();
1218   }
1219
1220   /// \brief Whether \p Tok is the most recent token (`CachedLexPos - 1`) in
1221   /// CachedTokens.
1222   bool IsPreviousCachedToken(const Token &Tok) const;
1223
1224   /// \brief Replace token in `CachedLexPos - 1` in CachedTokens by the tokens
1225   /// in \p NewToks.
1226   ///
1227   /// Useful when a token needs to be split in smaller ones and CachedTokens
1228   /// most recent token must to be updated to reflect that.
1229   void ReplacePreviousCachedToken(ArrayRef<Token> NewToks);
1230
1231   /// \brief Replace the last token with an annotation token.
1232   ///
1233   /// Like AnnotateCachedTokens(), this routine replaces an
1234   /// already-parsed (and resolved) token with an annotation
1235   /// token. However, this routine only replaces the last token with
1236   /// the annotation token; it does not affect any other cached
1237   /// tokens. This function has no effect if backtracking is not
1238   /// enabled.
1239   void ReplaceLastTokenWithAnnotation(const Token &Tok) {
1240     assert(Tok.isAnnotation() && "Expected annotation token");
1241     if (CachedLexPos != 0 && isBacktrackEnabled())
1242       CachedTokens[CachedLexPos-1] = Tok;
1243   }
1244
1245   /// Update the current token to represent the provided
1246   /// identifier, in order to cache an action performed by typo correction.
1247   void TypoCorrectToken(const Token &Tok) {
1248     assert(Tok.getIdentifierInfo() && "Expected identifier token");
1249     if (CachedLexPos != 0 && isBacktrackEnabled())
1250       CachedTokens[CachedLexPos-1] = Tok;
1251   }
1252
1253   /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
1254   /// CurTokenLexer pointers.
1255   void recomputeCurLexerKind();
1256
1257   /// \brief Returns true if incremental processing is enabled
1258   bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
1259
1260   /// \brief Enables the incremental processing
1261   void enableIncrementalProcessing(bool value = true) {
1262     IncrementalProcessing = value;
1263   }
1264   
1265   /// \brief Specify the point at which code-completion will be performed.
1266   ///
1267   /// \param File the file in which code completion should occur. If
1268   /// this file is included multiple times, code-completion will
1269   /// perform completion the first time it is included. If NULL, this
1270   /// function clears out the code-completion point.
1271   ///
1272   /// \param Line the line at which code completion should occur
1273   /// (1-based).
1274   ///
1275   /// \param Column the column at which code completion should occur
1276   /// (1-based).
1277   ///
1278   /// \returns true if an error occurred, false otherwise.
1279   bool SetCodeCompletionPoint(const FileEntry *File,
1280                               unsigned Line, unsigned Column);
1281
1282   /// \brief Determine if we are performing code completion.
1283   bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; }
1284
1285   /// \brief Returns the location of the code-completion point.
1286   ///
1287   /// Returns an invalid location if code-completion is not enabled or the file
1288   /// containing the code-completion point has not been lexed yet.
1289   SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
1290
1291   /// \brief Returns the start location of the file of code-completion point.
1292   ///
1293   /// Returns an invalid location if code-completion is not enabled or the file
1294   /// containing the code-completion point has not been lexed yet.
1295   SourceLocation getCodeCompletionFileLoc() const {
1296     return CodeCompletionFileLoc;
1297   }
1298
1299   /// \brief Returns true if code-completion is enabled and we have hit the
1300   /// code-completion point.
1301   bool isCodeCompletionReached() const { return CodeCompletionReached; }
1302
1303   /// \brief Note that we hit the code-completion point.
1304   void setCodeCompletionReached() {
1305     assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
1306     CodeCompletionReached = true;
1307     // Silence any diagnostics that occur after we hit the code-completion.
1308     getDiagnostics().setSuppressAllDiagnostics(true);
1309   }
1310
1311   /// \brief The location of the currently-active \#pragma clang
1312   /// arc_cf_code_audited begin.
1313   ///
1314   /// Returns an invalid location if there is no such pragma active.
1315   SourceLocation getPragmaARCCFCodeAuditedLoc() const {
1316     return PragmaARCCFCodeAuditedLoc;
1317   }
1318
1319   /// \brief Set the location of the currently-active \#pragma clang
1320   /// arc_cf_code_audited begin.  An invalid location ends the pragma.
1321   void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
1322     PragmaARCCFCodeAuditedLoc = Loc;
1323   }
1324
1325   /// \brief The location of the currently-active \#pragma clang
1326   /// assume_nonnull begin.
1327   ///
1328   /// Returns an invalid location if there is no such pragma active.
1329   SourceLocation getPragmaAssumeNonNullLoc() const {
1330     return PragmaAssumeNonNullLoc;
1331   }
1332
1333   /// \brief Set the location of the currently-active \#pragma clang
1334   /// assume_nonnull begin.  An invalid location ends the pragma.
1335   void setPragmaAssumeNonNullLoc(SourceLocation Loc) {
1336     PragmaAssumeNonNullLoc = Loc;
1337   }
1338
1339   /// \brief Set the directory in which the main file should be considered
1340   /// to have been found, if it is not a real file.
1341   void setMainFileDir(const DirectoryEntry *Dir) {
1342     MainFileDir = Dir;
1343   }
1344
1345   /// \brief Instruct the preprocessor to skip part of the main source file.
1346   ///
1347   /// \param Bytes The number of bytes in the preamble to skip.
1348   ///
1349   /// \param StartOfLine Whether skipping these bytes puts the lexer at the
1350   /// start of a line.
1351   void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
1352     SkipMainFilePreamble.first = Bytes;
1353     SkipMainFilePreamble.second = StartOfLine;
1354   }
1355
1356   /// Forwarding function for diagnostics.  This emits a diagnostic at
1357   /// the specified Token's location, translating the token's start
1358   /// position in the current buffer into a SourcePosition object for rendering.
1359   DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
1360     return Diags->Report(Loc, DiagID);
1361   }
1362
1363   DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
1364     return Diags->Report(Tok.getLocation(), DiagID);
1365   }
1366
1367   /// Return the 'spelling' of the token at the given
1368   /// location; does not go up to the spelling location or down to the
1369   /// expansion location.
1370   ///
1371   /// \param buffer A buffer which will be used only if the token requires
1372   ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
1373   /// \param invalid If non-null, will be set \c true if an error occurs.
1374   StringRef getSpelling(SourceLocation loc,
1375                         SmallVectorImpl<char> &buffer,
1376                         bool *invalid = nullptr) const {
1377     return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
1378   }
1379
1380   /// \brief Return the 'spelling' of the Tok token.
1381   ///
1382   /// The spelling of a token is the characters used to represent the token in
1383   /// the source file after trigraph expansion and escaped-newline folding.  In
1384   /// particular, this wants to get the true, uncanonicalized, spelling of
1385   /// things like digraphs, UCNs, etc.
1386   ///
1387   /// \param Invalid If non-null, will be set \c true if an error occurs.
1388   std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const {
1389     return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
1390   }
1391
1392   /// \brief Get the spelling of a token into a preallocated buffer, instead
1393   /// of as an std::string.
1394   ///
1395   /// The caller is required to allocate enough space for the token, which is
1396   /// guaranteed to be at least Tok.getLength() bytes long. The length of the
1397   /// actual result is returned.
1398   ///
1399   /// Note that this method may do two possible things: it may either fill in
1400   /// the buffer specified with characters, or it may *change the input pointer*
1401   /// to point to a constant buffer with the data already in it (avoiding a
1402   /// copy).  The caller is not allowed to modify the returned buffer pointer
1403   /// if an internal buffer is returned.
1404   unsigned getSpelling(const Token &Tok, const char *&Buffer,
1405                        bool *Invalid = nullptr) const {
1406     return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
1407   }
1408
1409   /// \brief Get the spelling of a token into a SmallVector.
1410   ///
1411   /// Note that the returned StringRef may not point to the
1412   /// supplied buffer if a copy can be avoided.
1413   StringRef getSpelling(const Token &Tok,
1414                         SmallVectorImpl<char> &Buffer,
1415                         bool *Invalid = nullptr) const;
1416
1417   /// \brief Relex the token at the specified location.
1418   /// \returns true if there was a failure, false on success.
1419   bool getRawToken(SourceLocation Loc, Token &Result,
1420                    bool IgnoreWhiteSpace = false) {
1421     return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace);
1422   }
1423
1424   /// \brief Given a Token \p Tok that is a numeric constant with length 1,
1425   /// return the character.
1426   char
1427   getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
1428                                               bool *Invalid = nullptr) const {
1429     assert(Tok.is(tok::numeric_constant) &&
1430            Tok.getLength() == 1 && "Called on unsupported token");
1431     assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
1432
1433     // If the token is carrying a literal data pointer, just use it.
1434     if (const char *D = Tok.getLiteralData())
1435       return *D;
1436
1437     // Otherwise, fall back on getCharacterData, which is slower, but always
1438     // works.
1439     return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
1440   }
1441
1442   /// \brief Retrieve the name of the immediate macro expansion.
1443   ///
1444   /// This routine starts from a source location, and finds the name of the
1445   /// macro responsible for its immediate expansion. It looks through any
1446   /// intervening macro argument expansions to compute this. It returns a
1447   /// StringRef that refers to the SourceManager-owned buffer of the source
1448   /// where that macro name is spelled. Thus, the result shouldn't out-live
1449   /// the SourceManager.
1450   StringRef getImmediateMacroName(SourceLocation Loc) {
1451     return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
1452   }
1453
1454   /// \brief Plop the specified string into a scratch buffer and set the
1455   /// specified token's location and length to it. 
1456   ///
1457   /// If specified, the source location provides a location of the expansion
1458   /// point of the token.
1459   void CreateString(StringRef Str, Token &Tok,
1460                     SourceLocation ExpansionLocStart = SourceLocation(),
1461                     SourceLocation ExpansionLocEnd = SourceLocation());
1462
1463   /// \brief Computes the source location just past the end of the
1464   /// token at this source location.
1465   ///
1466   /// This routine can be used to produce a source location that
1467   /// points just past the end of the token referenced by \p Loc, and
1468   /// is generally used when a diagnostic needs to point just after a
1469   /// token where it expected something different that it received. If
1470   /// the returned source location would not be meaningful (e.g., if
1471   /// it points into a macro), this routine returns an invalid
1472   /// source location.
1473   ///
1474   /// \param Offset an offset from the end of the token, where the source
1475   /// location should refer to. The default offset (0) produces a source
1476   /// location pointing just past the end of the token; an offset of 1 produces
1477   /// a source location pointing to the last character in the token, etc.
1478   SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
1479     return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
1480   }
1481
1482   /// \brief Returns true if the given MacroID location points at the first
1483   /// token of the macro expansion.
1484   ///
1485   /// \param MacroBegin If non-null and function returns true, it is set to
1486   /// begin location of the macro.
1487   bool isAtStartOfMacroExpansion(SourceLocation loc,
1488                                  SourceLocation *MacroBegin = nullptr) const {
1489     return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
1490                                             MacroBegin);
1491   }
1492
1493   /// \brief Returns true if the given MacroID location points at the last
1494   /// token of the macro expansion.
1495   ///
1496   /// \param MacroEnd If non-null and function returns true, it is set to
1497   /// end location of the macro.
1498   bool isAtEndOfMacroExpansion(SourceLocation loc,
1499                                SourceLocation *MacroEnd = nullptr) const {
1500     return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
1501   }
1502
1503   /// \brief Print the token to stderr, used for debugging.
1504   void DumpToken(const Token &Tok, bool DumpFlags = false) const;
1505   void DumpLocation(SourceLocation Loc) const;
1506   void DumpMacro(const MacroInfo &MI) const;
1507   void dumpMacroInfo(const IdentifierInfo *II);
1508
1509   /// \brief Given a location that specifies the start of a
1510   /// token, return a new location that specifies a character within the token.
1511   SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
1512                                          unsigned Char) const {
1513     return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
1514   }
1515
1516   /// \brief Increment the counters for the number of token paste operations
1517   /// performed.
1518   ///
1519   /// If fast was specified, this is a 'fast paste' case we handled.
1520   void IncrementPasteCounter(bool isFast) {
1521     if (isFast)
1522       ++NumFastTokenPaste;
1523     else
1524       ++NumTokenPaste;
1525   }
1526
1527   void PrintStats();
1528
1529   size_t getTotalMemory() const;
1530
1531   /// When the macro expander pastes together a comment (/##/) in Microsoft
1532   /// mode, this method handles updating the current state, returning the
1533   /// token on the next source line.
1534   void HandleMicrosoftCommentPaste(Token &Tok);
1535
1536   //===--------------------------------------------------------------------===//
1537   // Preprocessor callback methods.  These are invoked by a lexer as various
1538   // directives and events are found.
1539
1540   /// Given a tok::raw_identifier token, look up the
1541   /// identifier information for the token and install it into the token,
1542   /// updating the token kind accordingly.
1543   IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
1544
1545 private:
1546   llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
1547
1548 public:
1549
1550   /// \brief Specifies the reason for poisoning an identifier.
1551   ///
1552   /// If that identifier is accessed while poisoned, then this reason will be
1553   /// used instead of the default "poisoned" diagnostic.
1554   void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
1555
1556   /// \brief Display reason for poisoned identifier.
1557   void HandlePoisonedIdentifier(Token & Tok);
1558
1559   void MaybeHandlePoisonedIdentifier(Token & Identifier) {
1560     if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
1561       if(II->isPoisoned()) {
1562         HandlePoisonedIdentifier(Identifier);
1563       }
1564     }
1565   }
1566
1567 private:
1568   /// Identifiers used for SEH handling in Borland. These are only
1569   /// allowed in particular circumstances
1570   // __except block
1571   IdentifierInfo *Ident__exception_code,
1572                  *Ident___exception_code,
1573                  *Ident_GetExceptionCode;
1574   // __except filter expression
1575   IdentifierInfo *Ident__exception_info,
1576                  *Ident___exception_info,
1577                  *Ident_GetExceptionInfo;
1578   // __finally
1579   IdentifierInfo *Ident__abnormal_termination,
1580                  *Ident___abnormal_termination,
1581                  *Ident_AbnormalTermination;
1582
1583   const char *getCurLexerEndPos();
1584
1585 public:
1586   void PoisonSEHIdentifiers(bool Poison = true); // Borland
1587
1588   /// \brief Callback invoked when the lexer reads an identifier and has
1589   /// filled in the tokens IdentifierInfo member. 
1590   ///
1591   /// This callback potentially macro expands it or turns it into a named
1592   /// token (like 'for').
1593   ///
1594   /// \returns true if we actually computed a token, false if we need to
1595   /// lex again.
1596   bool HandleIdentifier(Token &Identifier);
1597
1598
1599   /// \brief Callback invoked when the lexer hits the end of the current file.
1600   ///
1601   /// This either returns the EOF token and returns true, or
1602   /// pops a level off the include stack and returns false, at which point the
1603   /// client should call lex again.
1604   bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
1605
1606   /// \brief Callback invoked when the current TokenLexer hits the end of its
1607   /// token stream.
1608   bool HandleEndOfTokenLexer(Token &Result);
1609
1610   /// \brief Callback invoked when the lexer sees a # token at the start of a
1611   /// line.
1612   ///
1613   /// This consumes the directive, modifies the lexer/preprocessor state, and
1614   /// advances the lexer(s) so that the next token read is the correct one.
1615   void HandleDirective(Token &Result);
1616
1617   /// \brief Ensure that the next token is a tok::eod token.
1618   ///
1619   /// If not, emit a diagnostic and consume up until the eod.
1620   /// If \p EnableMacros is true, then we consider macros that expand to zero
1621   /// tokens as being ok.
1622   void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
1623
1624   /// \brief Read and discard all tokens remaining on the current line until
1625   /// the tok::eod token is found.
1626   void DiscardUntilEndOfDirective();
1627
1628   /// \brief Returns true if the preprocessor has seen a use of
1629   /// __DATE__ or __TIME__ in the file so far.
1630   bool SawDateOrTime() const {
1631     return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
1632   }
1633   unsigned getCounterValue() const { return CounterValue; }
1634   void setCounterValue(unsigned V) { CounterValue = V; }
1635
1636   /// \brief Retrieves the module that we're currently building, if any.
1637   Module *getCurrentModule();
1638   
1639   /// \brief Allocate a new MacroInfo object with the provided SourceLocation.
1640   MacroInfo *AllocateMacroInfo(SourceLocation L);
1641
1642   /// \brief Allocate a new MacroInfo object loaded from an AST file.
1643   MacroInfo *AllocateDeserializedMacroInfo(SourceLocation L,
1644                                            unsigned SubModuleID);
1645
1646   /// \brief Turn the specified lexer token into a fully checked and spelled
1647   /// filename, e.g. as an operand of \#include. 
1648   ///
1649   /// The caller is expected to provide a buffer that is large enough to hold
1650   /// the spelling of the filename, but is also expected to handle the case
1651   /// when this method decides to use a different buffer.
1652   ///
1653   /// \returns true if the input filename was in <>'s or false if it was
1654   /// in ""'s.
1655   bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1656
1657   /// \brief Given a "foo" or \<foo> reference, look up the indicated file.
1658   ///
1659   /// Returns null on failure.  \p isAngled indicates whether the file
1660   /// reference is for system \#include's or not (i.e. using <> instead of "").
1661   const FileEntry *LookupFile(SourceLocation FilenameLoc, StringRef Filename,
1662                               bool isAngled, const DirectoryLookup *FromDir,
1663                               const FileEntry *FromFile,
1664                               const DirectoryLookup *&CurDir,
1665                               SmallVectorImpl<char> *SearchPath,
1666                               SmallVectorImpl<char> *RelativePath,
1667                               ModuleMap::KnownHeader *SuggestedModule,
1668                               bool SkipCache = false);
1669
1670   /// \brief Get the DirectoryLookup structure used to find the current
1671   /// FileEntry, if CurLexer is non-null and if applicable. 
1672   ///
1673   /// This allows us to implement \#include_next and find directory-specific
1674   /// properties.
1675   const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1676
1677   /// \brief Return true if we're in the top-level file, not in a \#include.
1678   bool isInPrimaryFile() const;
1679
1680   /// \brief Handle cases where the \#include name is expanded
1681   /// from a macro as multiple tokens, which need to be glued together. 
1682   ///
1683   /// This occurs for code like:
1684   /// \code
1685   ///    \#define FOO <x/y.h>
1686   ///    \#include FOO
1687   /// \endcode
1688   /// because in this case, "<x/y.h>" is returned as 7 tokens, not one.
1689   ///
1690   /// This code concatenates and consumes tokens up to the '>' token.  It
1691   /// returns false if the > was found, otherwise it returns true if it finds
1692   /// and consumes the EOD marker.
1693   bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1694                               SourceLocation &End);
1695
1696   /// \brief Lex an on-off-switch (C99 6.10.6p2) and verify that it is
1697   /// followed by EOD.  Return true if the token is not a valid on-off-switch.
1698   bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
1699
1700   bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
1701                       bool *ShadowFlag = nullptr);
1702
1703 private:
1704
1705   void PushIncludeMacroStack() {
1706     assert(CurLexerKind != CLK_CachingLexer && "cannot push a caching lexer");
1707     IncludeMacroStack.emplace_back(
1708         CurLexerKind, CurSubmodule, std::move(CurLexer), std::move(CurPTHLexer),
1709         CurPPLexer, std::move(CurTokenLexer), CurDirLookup);
1710     CurPPLexer = nullptr;
1711   }
1712
1713   void PopIncludeMacroStack() {
1714     CurLexer = std::move(IncludeMacroStack.back().TheLexer);
1715     CurPTHLexer = std::move(IncludeMacroStack.back().ThePTHLexer);
1716     CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1717     CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer);
1718     CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1719     CurSubmodule = IncludeMacroStack.back().TheSubmodule;
1720     CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1721     IncludeMacroStack.pop_back();
1722   }
1723
1724   void PropagateLineStartLeadingSpaceInfo(Token &Result);
1725
1726   void EnterSubmodule(Module *M, SourceLocation ImportLoc);
1727   void LeaveSubmodule();
1728
1729   /// Determine whether we need to create module macros for #defines in the
1730   /// current context.
1731   bool needModuleMacros() const;
1732
1733   /// Update the set of active module macros and ambiguity flag for a module
1734   /// macro name.
1735   void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info);
1736
1737   /// \brief Allocate a new MacroInfo object.
1738   MacroInfo *AllocateMacroInfo();
1739
1740   DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
1741                                                SourceLocation Loc);
1742   UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
1743   VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
1744                                                              bool isPublic);
1745
1746   /// \brief Lex and validate a macro name, which occurs after a
1747   /// \#define or \#undef.
1748   ///
1749   /// \param MacroNameTok Token that represents the name defined or undefined.
1750   /// \param IsDefineUndef Kind if preprocessor directive.
1751   /// \param ShadowFlag Points to flag that is set if macro name shadows
1752   ///                   a keyword.
1753   ///
1754   /// This emits a diagnostic, sets the token kind to eod,
1755   /// and discards the rest of the macro line if the macro name is invalid.
1756   void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other,
1757                      bool *ShadowFlag = nullptr);
1758
1759   /// The ( starting an argument list of a macro definition has just been read.
1760   /// Lex the rest of the arguments and the closing ), updating \p MI with
1761   /// what we learn and saving in \p LastTok the last token read.
1762   /// Return true if an error occurs parsing the arg list.
1763   bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok);
1764
1765   /// We just read a \#if or related directive and decided that the
1766   /// subsequent tokens are in the \#if'd out portion of the
1767   /// file.  Lex the rest of the file, until we see an \#endif.  If \p
1768   /// FoundNonSkipPortion is true, then we have already emitted code for part of
1769   /// this \#if directive, so \#else/\#elif blocks should never be entered. If
1770   /// \p FoundElse is false, then \#else directives are ok, if not, then we have
1771   /// already seen one so a \#else directive is a duplicate.  When this returns,
1772   /// the caller can lex the first valid token.
1773   void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1774                                     bool FoundNonSkipPortion, bool FoundElse,
1775                                     SourceLocation ElseLoc = SourceLocation());
1776
1777   /// \brief A fast PTH version of SkipExcludedConditionalBlock.
1778   void PTHSkipExcludedConditionalBlock();
1779
1780   /// \brief Evaluate an integer constant expression that may occur after a
1781   /// \#if or \#elif directive and return it as a bool.
1782   ///
1783   /// If the expression is equivalent to "!defined(X)" return X in IfNDefMacro.
1784   bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
1785
1786   /// \brief Install the standard preprocessor pragmas:
1787   /// \#pragma GCC poison/system_header/dependency and \#pragma once.
1788   void RegisterBuiltinPragmas();
1789
1790   /// \brief Register builtin macros such as __LINE__ with the identifier table.
1791   void RegisterBuiltinMacros();
1792
1793   /// If an identifier token is read that is to be expanded as a macro, handle
1794   /// it and return the next token as 'Tok'.  If we lexed a token, return true;
1795   /// otherwise the caller should lex again.
1796   bool HandleMacroExpandedIdentifier(Token &Tok, const MacroDefinition &MD);
1797
1798   /// \brief Cache macro expanded tokens for TokenLexers.
1799   //
1800   /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1801   /// going to lex in the cache and when it finishes the tokens are removed
1802   /// from the end of the cache.
1803   Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
1804                                   ArrayRef<Token> tokens);
1805   void removeCachedMacroExpandedTokensOfLastLexer();
1806   friend void TokenLexer::ExpandFunctionArguments();
1807
1808   /// Determine whether the next preprocessor token to be
1809   /// lexed is a '('.  If so, consume the token and return true, if not, this
1810   /// method should have no observable side-effect on the lexed tokens.
1811   bool isNextPPTokenLParen();
1812
1813   /// After reading "MACRO(", this method is invoked to read all of the formal
1814   /// arguments specified for the macro invocation.  Returns null on error.
1815   MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
1816                                        SourceLocation &ExpansionEnd);
1817
1818   /// \brief If an identifier token is read that is to be expanded
1819   /// as a builtin macro, handle it and return the next token as 'Tok'.
1820   void ExpandBuiltinMacro(Token &Tok);
1821
1822   /// \brief Read a \c _Pragma directive, slice it up, process it, then
1823   /// return the first token after the directive.
1824   /// This assumes that the \c _Pragma token has just been read into \p Tok.
1825   void Handle_Pragma(Token &Tok);
1826
1827   /// \brief Like Handle_Pragma except the pragma text is not enclosed within
1828   /// a string literal.
1829   void HandleMicrosoft__pragma(Token &Tok);
1830
1831   /// \brief Add a lexer to the top of the include stack and
1832   /// start lexing tokens from it instead of the current buffer.
1833   void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
1834
1835   /// \brief Add a lexer to the top of the include stack and
1836   /// start getting tokens from it using the PTH cache.
1837   void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
1838
1839   /// \brief Set the FileID for the preprocessor predefines.
1840   void setPredefinesFileID(FileID FID) {
1841     assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
1842     PredefinesFileID = FID;
1843   }
1844
1845   /// \brief Returns true if we are lexing from a file and not a
1846   /// pragma or a macro.
1847   static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
1848     return L ? !L->isPragmaLexer() : P != nullptr;
1849   }
1850
1851   static bool IsFileLexer(const IncludeStackInfo& I) {
1852     return IsFileLexer(I.TheLexer.get(), I.ThePPLexer);
1853   }
1854
1855   bool IsFileLexer() const {
1856     return IsFileLexer(CurLexer.get(), CurPPLexer);
1857   }
1858
1859   //===--------------------------------------------------------------------===//
1860   // Caching stuff.
1861   void CachingLex(Token &Result);
1862   bool InCachingLexMode() const {
1863     // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
1864     // that we are past EOF, not that we are in CachingLex mode.
1865     return !CurPPLexer && !CurTokenLexer && !CurPTHLexer &&
1866            !IncludeMacroStack.empty();
1867   }
1868   void EnterCachingLexMode();
1869   void ExitCachingLexMode() {
1870     if (InCachingLexMode())
1871       RemoveTopOfLexerStack();
1872   }
1873   const Token &PeekAhead(unsigned N);
1874   void AnnotatePreviousCachedTokens(const Token &Tok);
1875
1876   //===--------------------------------------------------------------------===//
1877   /// Handle*Directive - implement the various preprocessor directives.  These
1878   /// should side-effect the current preprocessor object so that the next call
1879   /// to Lex() will return the appropriate token next.
1880   void HandleLineDirective();
1881   void HandleDigitDirective(Token &Tok);
1882   void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1883   void HandleIdentSCCSDirective(Token &Tok);
1884   void HandleMacroPublicDirective(Token &Tok);
1885   void HandleMacroPrivateDirective();
1886
1887   // File inclusion.
1888   void HandleIncludeDirective(SourceLocation HashLoc,
1889                               Token &Tok,
1890                               const DirectoryLookup *LookupFrom = nullptr,
1891                               const FileEntry *LookupFromFile = nullptr,
1892                               bool isImport = false);
1893   void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1894   void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1895   void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
1896   void HandleMicrosoftImportDirective(Token &Tok);
1897
1898 public:
1899   // Module inclusion testing.
1900   /// \brief Find the module that owns the source or header file that
1901   /// \p Loc points to. If the location is in a file that was included
1902   /// into a module, or is outside any module, returns nullptr.
1903   Module *getModuleForLocation(SourceLocation Loc);
1904
1905   /// \brief Find the module that contains the specified location, either
1906   /// directly or indirectly.
1907   Module *getModuleContainingLocation(SourceLocation Loc);
1908
1909   /// \brief We want to produce a diagnostic at location IncLoc concerning a
1910   /// missing module import.
1911   ///
1912   /// \param IncLoc The location at which the missing import was detected.
1913   /// \param MLoc A location within the desired module at which some desired
1914   ///        effect occurred (eg, where a desired entity was declared).
1915   ///
1916   /// \return A file that can be #included to import a module containing MLoc.
1917   ///         Null if no such file could be determined or if a #include is not
1918   ///         appropriate.
1919   const FileEntry *getModuleHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
1920                                                           SourceLocation MLoc);
1921
1922 private:
1923   // Macro handling.
1924   void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterTopLevelIfndef);
1925   void HandleUndefDirective();
1926
1927   // Conditional Inclusion.
1928   void HandleIfdefDirective(Token &Tok, bool isIfndef,
1929                             bool ReadAnyTokensBeforeDirective);
1930   void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1931   void HandleEndifDirective(Token &Tok);
1932   void HandleElseDirective(Token &Tok);
1933   void HandleElifDirective(Token &Tok);
1934
1935   // Pragmas.
1936   void HandlePragmaDirective(SourceLocation IntroducerLoc,
1937                              PragmaIntroducerKind Introducer);
1938 public:
1939   void HandlePragmaOnce(Token &OnceTok);
1940   void HandlePragmaMark();
1941   void HandlePragmaPoison();
1942   void HandlePragmaSystemHeader(Token &SysHeaderTok);
1943   void HandlePragmaDependency(Token &DependencyTok);
1944   void HandlePragmaPushMacro(Token &Tok);
1945   void HandlePragmaPopMacro(Token &Tok);
1946   void HandlePragmaIncludeAlias(Token &Tok);
1947   IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1948
1949   // Return true and store the first token only if any CommentHandler
1950   // has inserted some tokens and getCommentRetentionState() is false.
1951   bool HandleComment(Token &Token, SourceRange Comment);
1952
1953   /// \brief A macro is used, update information about macros that need unused
1954   /// warnings.
1955   void markMacroAsUsed(MacroInfo *MI);
1956 };
1957
1958 /// \brief Abstract base class that describes a handler that will receive
1959 /// source ranges for each of the comments encountered in the source file.
1960 class CommentHandler {
1961 public:
1962   virtual ~CommentHandler();
1963
1964   // The handler shall return true if it has pushed any tokens
1965   // to be read using e.g. EnterToken or EnterTokenStream.
1966   virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
1967 };
1968
1969 /// \brief Registry of pragma handlers added by plugins
1970 typedef llvm::Registry<PragmaHandler> PragmaHandlerRegistry;
1971
1972 }  // end namespace clang
1973
1974 #endif