Preprocessor.h revision 58bf98725b6d4588338e191d2ab981b104471dab
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//  This file defines the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
15#define LLVM_CLANG_LEX_PREPROCESSOR_H
16
17#include "clang/Lex/MacroInfo.h"
18#include "clang/Lex/Lexer.h"
19#include "clang/Lex/PTHLexer.h"
20#include "clang/Lex/PPCallbacks.h"
21#include "clang/Lex/TokenLexer.h"
22#include "clang/Lex/PTHManager.h"
23#include "clang/Basic/Builtins.h"
24#include "clang/Basic/Diagnostic.h"
25#include "clang/Basic/IdentifierTable.h"
26#include "clang/Basic/SourceLocation.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/IntrusiveRefCntPtr.h"
29#include "llvm/ADT/SmallPtrSet.h"
30#include "llvm/ADT/OwningPtr.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/Support/Allocator.h"
34#include <vector>
35
36namespace llvm {
37  template<unsigned InternalLen> class SmallString;
38}
39
40namespace clang {
41
42class SourceManager;
43class ExternalPreprocessorSource;
44class FileManager;
45class FileEntry;
46class HeaderSearch;
47class PragmaNamespace;
48class PragmaHandler;
49class CommentHandler;
50class ScratchBuffer;
51class TargetInfo;
52class PPCallbacks;
53class CodeCompletionHandler;
54class DirectoryLookup;
55class PreprocessingRecord;
56class ModuleLoader;
57
58/// Preprocessor - This object engages in a tight little dance with the lexer to
59/// efficiently preprocess tokens.  Lexers know only about tokens within a
60/// single source file, and don't know anything about preprocessor-level issues
61/// like the \#include stack, token expansion, etc.
62///
63class Preprocessor : public RefCountedBase<Preprocessor> {
64  DiagnosticsEngine        *Diags;
65  LangOptions       &LangOpts;
66  const TargetInfo  *Target;
67  FileManager       &FileMgr;
68  SourceManager     &SourceMgr;
69  ScratchBuffer     *ScratchBuf;
70  HeaderSearch      &HeaderInfo;
71  ModuleLoader      &TheModuleLoader;
72
73  /// \brief External source of macros.
74  ExternalPreprocessorSource *ExternalSource;
75
76
77  /// PTH - An optional PTHManager object used for getting tokens from
78  ///  a token cache rather than lexing the original source file.
79  OwningPtr<PTHManager> PTH;
80
81  /// BP - A BumpPtrAllocator object used to quickly allocate and release
82  ///  objects internal to the Preprocessor.
83  llvm::BumpPtrAllocator BP;
84
85  /// Identifiers for builtin macros and other builtins.
86  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
87  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
88  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
89  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
90  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
91  IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
92  IdentifierInfo *Ident_Pragma, *Ident__pragma;    // _Pragma, __pragma
93  IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
94  IdentifierInfo *Ident__has_feature;              // __has_feature
95  IdentifierInfo *Ident__has_extension;            // __has_extension
96  IdentifierInfo *Ident__has_builtin;              // __has_builtin
97  IdentifierInfo *Ident__has_attribute;            // __has_attribute
98  IdentifierInfo *Ident__has_include;              // __has_include
99  IdentifierInfo *Ident__has_include_next;         // __has_include_next
100  IdentifierInfo *Ident__has_warning;              // __has_warning
101
102  SourceLocation DATELoc, TIMELoc;
103  unsigned CounterValue;  // Next __COUNTER__ value.
104
105  enum {
106    /// MaxIncludeStackDepth - Maximum depth of \#includes.
107    MaxAllowedIncludeStackDepth = 200
108  };
109
110  // State that is set before the preprocessor begins.
111  bool KeepComments : 1;
112  bool KeepMacroComments : 1;
113  bool SuppressIncludeNotFoundError : 1;
114
115  // State that changes while the preprocessor runs:
116  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
117
118  /// Whether the preprocessor owns the header search object.
119  bool OwnsHeaderSearch : 1;
120
121  /// DisableMacroExpansion - True if macro expansion is disabled.
122  bool DisableMacroExpansion : 1;
123
124  /// MacroExpansionInDirectivesOverride - Temporarily disables
125  /// DisableMacroExpansion (i.e. enables expansion) when parsing preprocessor
126  /// directives.
127  bool MacroExpansionInDirectivesOverride : 1;
128
129  class ResetMacroExpansionHelper;
130
131  /// \brief Whether we have already loaded macros from the external source.
132  mutable bool ReadMacrosFromExternalSource : 1;
133
134  /// \brief True if pragmas are enabled.
135  bool PragmasEnabled : 1;
136
137  /// \brief True if we are pre-expanding macro arguments.
138  bool InMacroArgPreExpansion;
139
140  /// Identifiers - This is mapping/lookup information for all identifiers in
141  /// the program, including program keywords.
142  mutable IdentifierTable Identifiers;
143
144  /// Selectors - This table contains all the selectors in the program. Unlike
145  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
146  /// It is declared/expanded here because it's role/lifetime is
147  /// conceptually similar the IdentifierTable. In addition, the current control
148  /// flow (in clang::ParseAST()), make it convenient to put here.
149  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
150  /// the lifetime of the preprocessor.
151  SelectorTable Selectors;
152
153  /// BuiltinInfo - Information about builtins.
154  Builtin::Context BuiltinInfo;
155
156  /// PragmaHandlers - This tracks all of the pragmas that the client registered
157  /// with this preprocessor.
158  PragmaNamespace *PragmaHandlers;
159
160  /// \brief Tracks all of the comment handlers that the client registered
161  /// with this preprocessor.
162  std::vector<CommentHandler *> CommentHandlers;
163
164  /// \brief True if we want to ignore EOF token and continue later on (thus
165  /// avoid tearing the Lexer and etc. down).
166  bool IncrementalProcessing;
167
168  /// \brief The code-completion handler.
169  CodeCompletionHandler *CodeComplete;
170
171  /// \brief The file that we're performing code-completion for, if any.
172  const FileEntry *CodeCompletionFile;
173
174  /// \brief The offset in file for the code-completion point.
175  unsigned CodeCompletionOffset;
176
177  /// \brief The location for the code-completion point. This gets instantiated
178  /// when the CodeCompletionFile gets \#include'ed for preprocessing.
179  SourceLocation CodeCompletionLoc;
180
181  /// \brief The start location for the file of the code-completion point.
182  ///
183  /// This gets instantiated when the CodeCompletionFile gets \#include'ed
184  /// for preprocessing.
185  SourceLocation CodeCompletionFileLoc;
186
187  /// \brief The source location of the 'import' contextual keyword we just
188  /// lexed, if any.
189  SourceLocation ModuleImportLoc;
190
191  /// \brief The module import path that we're currently processing.
192  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2>
193    ModuleImportPath;
194
195  /// \brief Whether the module import expectes an identifier next. Otherwise,
196  /// it expects a '.' or ';'.
197  bool ModuleImportExpectsIdentifier;
198
199  /// \brief The source location of the currently-active
200  /// #pragma clang arc_cf_code_audited begin.
201  SourceLocation PragmaARCCFCodeAuditedLoc;
202
203  /// \brief True if we hit the code-completion point.
204  bool CodeCompletionReached;
205
206  /// \brief The number of bytes that we will initially skip when entering the
207  /// main file, which is used when loading a precompiled preamble, along
208  /// with a flag that indicates whether skipping this number of bytes will
209  /// place the lexer at the start of a line.
210  std::pair<unsigned, bool> SkipMainFilePreamble;
211
212  /// CurLexer - This is the current top of the stack that we're lexing from if
213  /// not expanding a macro and we are lexing directly from source code.
214  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
215  OwningPtr<Lexer> CurLexer;
216
217  /// CurPTHLexer - This is the current top of stack that we're lexing from if
218  ///  not expanding from a macro and we are lexing from a PTH cache.
219  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
220  OwningPtr<PTHLexer> CurPTHLexer;
221
222  /// CurPPLexer - This is the current top of the stack what we're lexing from
223  ///  if not expanding a macro.  This is an alias for either CurLexer or
224  ///  CurPTHLexer.
225  PreprocessorLexer *CurPPLexer;
226
227  /// CurLookup - The DirectoryLookup structure used to find the current
228  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
229  /// implement \#include_next and find directory-specific properties.
230  const DirectoryLookup *CurDirLookup;
231
232  /// CurTokenLexer - This is the current macro we are expanding, if we are
233  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
234  OwningPtr<TokenLexer> CurTokenLexer;
235
236  /// \brief The kind of lexer we're currently working with.
237  enum CurLexerKind {
238    CLK_Lexer,
239    CLK_PTHLexer,
240    CLK_TokenLexer,
241    CLK_CachingLexer,
242    CLK_LexAfterModuleImport
243  } CurLexerKind;
244
245  /// IncludeMacroStack - This keeps track of the stack of files currently
246  /// \#included, and macros currently being expanded from, not counting
247  /// CurLexer/CurTokenLexer.
248  struct IncludeStackInfo {
249    enum CurLexerKind     CurLexerKind;
250    Lexer                 *TheLexer;
251    PTHLexer              *ThePTHLexer;
252    PreprocessorLexer     *ThePPLexer;
253    TokenLexer            *TheTokenLexer;
254    const DirectoryLookup *TheDirLookup;
255
256    IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P,
257                     PreprocessorLexer* PPL,
258                     TokenLexer* TL, const DirectoryLookup *D)
259      : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL),
260        TheTokenLexer(TL), TheDirLookup(D) {}
261  };
262  std::vector<IncludeStackInfo> IncludeMacroStack;
263
264  /// Callbacks - These are actions invoked when some preprocessor activity is
265  /// encountered (e.g. a file is \#included, etc).
266  PPCallbacks *Callbacks;
267
268  struct MacroExpandsInfo {
269    Token Tok;
270    MacroInfo *MI;
271    SourceRange Range;
272    MacroExpandsInfo(Token Tok, MacroInfo *MI, SourceRange Range)
273      : Tok(Tok), MI(MI), Range(Range) { }
274  };
275  SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
276
277  /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
278  /// to the actual definition of the macro.
279  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
280
281  /// \brief Macros that we want to warn because they are not used at the end
282  /// of the translation unit; we store just their SourceLocations instead
283  /// something like MacroInfo*. The benefit of this is that when we are
284  /// deserializing from PCH, we don't need to deserialize identifier & macros
285  /// just so that we can report that they are unused, we just warn using
286  /// the SourceLocations of this set (that will be filled by the ASTReader).
287  /// We are using SmallPtrSet instead of a vector for faster removal.
288  typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
289  WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
290
291  /// MacroArgCache - This is a "freelist" of MacroArg objects that can be
292  /// reused for quick allocation.
293  MacroArgs *MacroArgCache;
294  friend class MacroArgs;
295
296  /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma
297  /// push_macro directive, we keep a MacroInfo stack used to restore
298  /// previous macro value.
299  llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
300
301  // Various statistics we track for performance analysis.
302  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
303  unsigned NumIf, NumElse, NumEndif;
304  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
305  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
306  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
307  unsigned NumSkipped;
308
309  /// Predefines - This string is the predefined macros that preprocessor
310  /// should use from the command line etc.
311  std::string Predefines;
312
313  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
314  enum { TokenLexerCacheSize = 8 };
315  unsigned NumCachedTokenLexers;
316  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
317
318  /// \brief Keeps macro expanded tokens for TokenLexers.
319  //
320  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
321  /// going to lex in the cache and when it finishes the tokens are removed
322  /// from the end of the cache.
323  SmallVector<Token, 16> MacroExpandedTokens;
324  std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
325
326  /// \brief A record of the macro definitions and expansions that
327  /// occurred during preprocessing.
328  ///
329  /// This is an optional side structure that can be enabled with
330  /// \c createPreprocessingRecord() prior to preprocessing.
331  PreprocessingRecord *Record;
332
333private:  // Cached tokens state.
334  typedef SmallVector<Token, 1> CachedTokensTy;
335
336  /// CachedTokens - Cached tokens are stored here when we do backtracking or
337  /// lookahead. They are "lexed" by the CachingLex() method.
338  CachedTokensTy CachedTokens;
339
340  /// CachedLexPos - The position of the cached token that CachingLex() should
341  /// "lex" next. If it points beyond the CachedTokens vector, it means that
342  /// a normal Lex() should be invoked.
343  CachedTokensTy::size_type CachedLexPos;
344
345  /// BacktrackPositions - Stack of backtrack positions, allowing nested
346  /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
347  /// indicate where CachedLexPos should be set when the BackTrack() method is
348  /// invoked (at which point the last position is popped).
349  std::vector<CachedTokensTy::size_type> BacktrackPositions;
350
351  struct MacroInfoChain {
352    MacroInfo MI;
353    MacroInfoChain *Next;
354    MacroInfoChain *Prev;
355  };
356
357  /// MacroInfos are managed as a chain for easy disposal.  This is the head
358  /// of that list.
359  MacroInfoChain *MIChainHead;
360
361  /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
362  /// allocation.
363  MacroInfoChain *MICache;
364
365  MacroInfo *getInfoForMacro(IdentifierInfo *II) const;
366
367public:
368  Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
369               const TargetInfo *target,
370               SourceManager &SM, HeaderSearch &Headers,
371               ModuleLoader &TheModuleLoader,
372               IdentifierInfoLookup *IILookup = 0,
373               bool OwnsHeaderSearch = false,
374               bool DelayInitialization = false,
375               bool IncrProcessing = false);
376
377  ~Preprocessor();
378
379  /// \brief Initialize the preprocessor, if the constructor did not already
380  /// perform the initialization.
381  ///
382  /// \param Target Information about the target.
383  void Initialize(const TargetInfo &Target);
384
385  DiagnosticsEngine &getDiagnostics() const { return *Diags; }
386  void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
387
388  const LangOptions &getLangOpts() const { return LangOpts; }
389  const TargetInfo &getTargetInfo() const { return *Target; }
390  FileManager &getFileManager() const { return FileMgr; }
391  SourceManager &getSourceManager() const { return SourceMgr; }
392  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
393
394  IdentifierTable &getIdentifierTable() { return Identifiers; }
395  SelectorTable &getSelectorTable() { return Selectors; }
396  Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
397  llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
398
399  void setPTHManager(PTHManager* pm);
400
401  PTHManager *getPTHManager() { return PTH.get(); }
402
403  void setExternalSource(ExternalPreprocessorSource *Source) {
404    ExternalSource = Source;
405  }
406
407  ExternalPreprocessorSource *getExternalSource() const {
408    return ExternalSource;
409  }
410
411  /// \brief Retrieve the module loader associated with this preprocessor.
412  ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
413
414  /// SetCommentRetentionState - Control whether or not the preprocessor retains
415  /// comments in output.
416  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
417    this->KeepComments = KeepComments | KeepMacroComments;
418    this->KeepMacroComments = KeepMacroComments;
419  }
420
421  bool getCommentRetentionState() const { return KeepComments; }
422
423  void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
424  bool getPragmasEnabled() const { return PragmasEnabled; }
425
426  void SetSuppressIncludeNotFoundError(bool Suppress) {
427    SuppressIncludeNotFoundError = Suppress;
428  }
429
430  bool GetSuppressIncludeNotFoundError() {
431    return SuppressIncludeNotFoundError;
432  }
433
434  /// isCurrentLexer - Return true if we are lexing directly from the specified
435  /// lexer.
436  bool isCurrentLexer(const PreprocessorLexer *L) const {
437    return CurPPLexer == L;
438  }
439
440  /// getCurrentLexer - Return the current lexer being lexed from.  Note
441  /// that this ignores any potentially active macro expansions and _Pragma
442  /// expansions going on at the time.
443  PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
444
445  /// getCurrentFileLexer - Return the current file lexer being lexed from.
446  /// Note that this ignores any potentially active macro expansions and _Pragma
447  /// expansions going on at the time.
448  PreprocessorLexer *getCurrentFileLexer() const;
449
450  /// getPPCallbacks/addPPCallbacks - Accessors for preprocessor callbacks.
451  /// Note that this class takes ownership of any PPCallbacks object given to
452  /// it.
453  PPCallbacks *getPPCallbacks() const { return Callbacks; }
454  void addPPCallbacks(PPCallbacks *C) {
455    if (Callbacks)
456      C = new PPChainedCallbacks(C, Callbacks);
457    Callbacks = C;
458  }
459
460  /// \brief Given an identifier, return the MacroInfo it is \#defined to
461  /// or null if it isn't \#define'd.
462  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
463    if (!II->hasMacroDefinition())
464      return 0;
465
466    return getInfoForMacro(II);
467  }
468
469  /// \brief Specify a macro for this identifier.
470  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI,
471                    bool LoadedFromAST = false);
472
473  /// macro_iterator/macro_begin/macro_end - This allows you to walk the current
474  /// state of the macro table.  This visits every currently-defined macro.
475  typedef llvm::DenseMap<IdentifierInfo*,
476                         MacroInfo*>::const_iterator macro_iterator;
477  macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
478  macro_iterator macro_end(bool IncludeExternalMacros = true) const;
479
480  const std::string &getPredefines() const { return Predefines; }
481  /// setPredefines - Set the predefines for this Preprocessor.  These
482  /// predefines are automatically injected when parsing the main file.
483  void setPredefines(const char *P) { Predefines = P; }
484  void setPredefines(const std::string &P) { Predefines = P; }
485
486  /// getIdentifierInfo - Return information about the specified preprocessor
487  /// identifier token.  The version of this method that takes two character
488  /// pointers is preferred unless the identifier is already available as a
489  /// string (this avoids allocation and copying of memory to construct an
490  /// std::string).
491  IdentifierInfo *getIdentifierInfo(StringRef Name) const {
492    return &Identifiers.get(Name);
493  }
494
495  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
496  /// If 'Namespace' is non-null, then it is a token required to exist on the
497  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
498  void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
499  void AddPragmaHandler(PragmaHandler *Handler) {
500    AddPragmaHandler(StringRef(), Handler);
501  }
502
503  /// RemovePragmaHandler - Remove the specific pragma handler from
504  /// the preprocessor. If \arg Namespace is non-null, then it should
505  /// be the namespace that \arg Handler was added to. It is an error
506  /// to remove a handler that has not been registered.
507  void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
508  void RemovePragmaHandler(PragmaHandler *Handler) {
509    RemovePragmaHandler(StringRef(), Handler);
510  }
511
512  /// \brief Add the specified comment handler to the preprocessor.
513  void addCommentHandler(CommentHandler *Handler);
514
515  /// \brief Remove the specified comment handler.
516  ///
517  /// It is an error to remove a handler that has not been registered.
518  void removeCommentHandler(CommentHandler *Handler);
519
520  /// \brief Set the code completion handler to the given object.
521  void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
522    CodeComplete = &Handler;
523  }
524
525  /// \brief Retrieve the current code-completion handler.
526  CodeCompletionHandler *getCodeCompletionHandler() const {
527    return CodeComplete;
528  }
529
530  /// \brief Clear out the code completion handler.
531  void clearCodeCompletionHandler() {
532    CodeComplete = 0;
533  }
534
535  /// \brief Hook used by the lexer to invoke the "natural language" code
536  /// completion point.
537  void CodeCompleteNaturalLanguage();
538
539  /// \brief Retrieve the preprocessing record, or NULL if there is no
540  /// preprocessing record.
541  PreprocessingRecord *getPreprocessingRecord() const { return Record; }
542
543  /// \brief Create a new preprocessing record, which will keep track of
544  /// all macro expansions, macro definitions, etc.
545  void createPreprocessingRecord(bool RecordConditionalDirectives);
546
547  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
548  /// which implicitly adds the builtin defines etc.
549  void EnterMainSourceFile();
550
551  /// EndSourceFile - Inform the preprocessor callbacks that processing is
552  /// complete.
553  void EndSourceFile();
554
555  /// EnterSourceFile - Add a source file to the top of the include stack and
556  /// start lexing tokens from it instead of the current buffer.  Emit an error
557  /// and don't enter the file on error.
558  void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
559                       SourceLocation Loc);
560
561  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
562  /// tokens from it instead of the current buffer.  Args specifies the
563  /// tokens input to a function-like macro.
564  ///
565  /// ILEnd specifies the location of the ')' for a function-like macro or the
566  /// identifier for an object-like macro.
567  void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroArgs *Args);
568
569  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
570  /// which will cause the lexer to start returning the specified tokens.
571  ///
572  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
573  /// not be subject to further macro expansion.  Otherwise, these tokens will
574  /// be re-macro-expanded when/if expansion is enabled.
575  ///
576  /// If OwnsTokens is false, this method assumes that the specified stream of
577  /// tokens has a permanent owner somewhere, so they do not need to be copied.
578  /// If it is true, it assumes the array of tokens is allocated with new[] and
579  /// must be freed.
580  ///
581  void EnterTokenStream(const Token *Toks, unsigned NumToks,
582                        bool DisableMacroExpansion, bool OwnsTokens);
583
584  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
585  /// lexer stack.  This should only be used in situations where the current
586  /// state of the top-of-stack lexer is known.
587  void RemoveTopOfLexerStack();
588
589  /// EnableBacktrackAtThisPos - From the point that this method is called, and
590  /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
591  /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
592  /// make the Preprocessor re-lex the same tokens.
593  ///
594  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
595  /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
596  /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
597  ///
598  /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
599  /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
600  /// tokens will continue indefinitely.
601  ///
602  void EnableBacktrackAtThisPos();
603
604  /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
605  void CommitBacktrackedTokens();
606
607  /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
608  /// EnableBacktrackAtThisPos() was previously called.
609  void Backtrack();
610
611  /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
612  /// caching of tokens is on.
613  bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
614
615  /// Lex - To lex a token from the preprocessor, just pull a token from the
616  /// current lexer or macro object.
617  void Lex(Token &Result) {
618    switch (CurLexerKind) {
619    case CLK_Lexer: CurLexer->Lex(Result); break;
620    case CLK_PTHLexer: CurPTHLexer->Lex(Result); break;
621    case CLK_TokenLexer: CurTokenLexer->Lex(Result); break;
622    case CLK_CachingLexer: CachingLex(Result); break;
623    case CLK_LexAfterModuleImport: LexAfterModuleImport(Result); break;
624    }
625  }
626
627  void LexAfterModuleImport(Token &Result);
628
629  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
630  /// something not a comment.  This is useful in -E -C mode where comments
631  /// would foul up preprocessor directive handling.
632  void LexNonComment(Token &Result) {
633    do
634      Lex(Result);
635    while (Result.getKind() == tok::comment);
636  }
637
638  /// LexUnexpandedToken - This is just like Lex, but this disables macro
639  /// expansion of identifier tokens.
640  void LexUnexpandedToken(Token &Result) {
641    // Disable macro expansion.
642    bool OldVal = DisableMacroExpansion;
643    DisableMacroExpansion = true;
644    // Lex the token.
645    Lex(Result);
646
647    // Reenable it.
648    DisableMacroExpansion = OldVal;
649  }
650
651  /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
652  /// expansion of identifier tokens.
653  void LexUnexpandedNonComment(Token &Result) {
654    do
655      LexUnexpandedToken(Result);
656    while (Result.getKind() == tok::comment);
657  }
658
659  /// Disables macro expansion everywhere except for preprocessor directives.
660  void SetMacroExpansionOnlyInDirectives() {
661    DisableMacroExpansion = true;
662    MacroExpansionInDirectivesOverride = true;
663  }
664
665  /// LookAhead - This peeks ahead N tokens and returns that token without
666  /// consuming any tokens.  LookAhead(0) returns the next token that would be
667  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
668  /// returns normal tokens after phase 5.  As such, it is equivalent to using
669  /// 'Lex', not 'LexUnexpandedToken'.
670  const Token &LookAhead(unsigned N) {
671    if (CachedLexPos + N < CachedTokens.size())
672      return CachedTokens[CachedLexPos+N];
673    else
674      return PeekAhead(N+1);
675  }
676
677  /// RevertCachedTokens - When backtracking is enabled and tokens are cached,
678  /// this allows to revert a specific number of tokens.
679  /// Note that the number of tokens being reverted should be up to the last
680  /// backtrack position, not more.
681  void RevertCachedTokens(unsigned N) {
682    assert(isBacktrackEnabled() &&
683           "Should only be called when tokens are cached for backtracking");
684    assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
685         && "Should revert tokens up to the last backtrack position, not more");
686    assert(signed(CachedLexPos) - signed(N) >= 0 &&
687           "Corrupted backtrack positions ?");
688    CachedLexPos -= N;
689  }
690
691  /// EnterToken - Enters a token in the token stream to be lexed next. If
692  /// BackTrack() is called afterwards, the token will remain at the insertion
693  /// point.
694  void EnterToken(const Token &Tok) {
695    EnterCachingLexMode();
696    CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
697  }
698
699  /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching
700  /// tokens (because backtrack is enabled) it should replace the most recent
701  /// cached tokens with the given annotation token. This function has no effect
702  /// if backtracking is not enabled.
703  ///
704  /// Note that the use of this function is just for optimization; so that the
705  /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
706  /// invoked.
707  void AnnotateCachedTokens(const Token &Tok) {
708    assert(Tok.isAnnotation() && "Expected annotation token");
709    if (CachedLexPos != 0 && isBacktrackEnabled())
710      AnnotatePreviousCachedTokens(Tok);
711  }
712
713  /// \brief Replace the last token with an annotation token.
714  ///
715  /// Like AnnotateCachedTokens(), this routine replaces an
716  /// already-parsed (and resolved) token with an annotation
717  /// token. However, this routine only replaces the last token with
718  /// the annotation token; it does not affect any other cached
719  /// tokens. This function has no effect if backtracking is not
720  /// enabled.
721  void ReplaceLastTokenWithAnnotation(const Token &Tok) {
722    assert(Tok.isAnnotation() && "Expected annotation token");
723    if (CachedLexPos != 0 && isBacktrackEnabled())
724      CachedTokens[CachedLexPos-1] = Tok;
725  }
726
727  /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
728  /// CurTokenLexer pointers.
729  void recomputeCurLexerKind();
730
731  /// \brief Returns true if incremental processing is enabled
732  bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
733
734  /// \brief Enables the incremental processing
735  void enableIncrementalProcessing(bool value = true) {
736    IncrementalProcessing = value;
737  }
738
739  /// \brief Specify the point at which code-completion will be performed.
740  ///
741  /// \param File the file in which code completion should occur. If
742  /// this file is included multiple times, code-completion will
743  /// perform completion the first time it is included. If NULL, this
744  /// function clears out the code-completion point.
745  ///
746  /// \param Line the line at which code completion should occur
747  /// (1-based).
748  ///
749  /// \param Column the column at which code completion should occur
750  /// (1-based).
751  ///
752  /// \returns true if an error occurred, false otherwise.
753  bool SetCodeCompletionPoint(const FileEntry *File,
754                              unsigned Line, unsigned Column);
755
756  /// \brief Determine if we are performing code completion.
757  bool isCodeCompletionEnabled() const { return CodeCompletionFile != 0; }
758
759  /// \brief Returns the location of the code-completion point.
760  /// Returns an invalid location if code-completion is not enabled or the file
761  /// containing the code-completion point has not been lexed yet.
762  SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
763
764  /// \brief Returns the start location of the file of code-completion point.
765  /// Returns an invalid location if code-completion is not enabled or the file
766  /// containing the code-completion point has not been lexed yet.
767  SourceLocation getCodeCompletionFileLoc() const {
768    return CodeCompletionFileLoc;
769  }
770
771  /// \brief Returns true if code-completion is enabled and we have hit the
772  /// code-completion point.
773  bool isCodeCompletionReached() const { return CodeCompletionReached; }
774
775  /// \brief Note that we hit the code-completion point.
776  void setCodeCompletionReached() {
777    assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
778    CodeCompletionReached = true;
779    // Silence any diagnostics that occur after we hit the code-completion.
780    getDiagnostics().setSuppressAllDiagnostics(true);
781  }
782
783  /// \brief The location of the currently-active \#pragma clang
784  /// arc_cf_code_audited begin.  Returns an invalid location if there
785  /// is no such pragma active.
786  SourceLocation getPragmaARCCFCodeAuditedLoc() const {
787    return PragmaARCCFCodeAuditedLoc;
788  }
789
790  /// \brief Set the location of the currently-active \#pragma clang
791  /// arc_cf_code_audited begin.  An invalid location ends the pragma.
792  void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
793    PragmaARCCFCodeAuditedLoc = Loc;
794  }
795
796  /// \brief Instruct the preprocessor to skip part of the main source file.
797  ///
798  /// \param Bytes The number of bytes in the preamble to skip.
799  ///
800  /// \param StartOfLine Whether skipping these bytes puts the lexer at the
801  /// start of a line.
802  void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
803    SkipMainFilePreamble.first = Bytes;
804    SkipMainFilePreamble.second = StartOfLine;
805  }
806
807  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
808  /// the specified Token's location, translating the token's start
809  /// position in the current buffer into a SourcePosition object for rendering.
810  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
811    return Diags->Report(Loc, DiagID);
812  }
813
814  DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
815    return Diags->Report(Tok.getLocation(), DiagID);
816  }
817
818  /// getSpelling() - Return the 'spelling' of the token at the given
819  /// location; does not go up to the spelling location or down to the
820  /// expansion location.
821  ///
822  /// \param buffer A buffer which will be used only if the token requires
823  ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
824  /// \param invalid If non-null, will be set \c true if an error occurs.
825  StringRef getSpelling(SourceLocation loc,
826                              SmallVectorImpl<char> &buffer,
827                              bool *invalid = 0) const {
828    return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
829  }
830
831  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
832  /// token is the characters used to represent the token in the source file
833  /// after trigraph expansion and escaped-newline folding.  In particular, this
834  /// wants to get the true, uncanonicalized, spelling of things like digraphs
835  /// UCNs, etc.
836  ///
837  /// \param Invalid If non-null, will be set \c true if an error occurs.
838  std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
839    return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
840  }
841
842  /// getSpelling - This method is used to get the spelling of a token into a
843  /// preallocated buffer, instead of as an std::string.  The caller is required
844  /// to allocate enough space for the token, which is guaranteed to be at least
845  /// Tok.getLength() bytes long.  The length of the actual result is returned.
846  ///
847  /// Note that this method may do two possible things: it may either fill in
848  /// the buffer specified with characters, or it may *change the input pointer*
849  /// to point to a constant buffer with the data already in it (avoiding a
850  /// copy).  The caller is not allowed to modify the returned buffer pointer
851  /// if an internal buffer is returned.
852  unsigned getSpelling(const Token &Tok, const char *&Buffer,
853                       bool *Invalid = 0) const {
854    return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
855  }
856
857  /// getSpelling - This method is used to get the spelling of a token into a
858  /// SmallVector. Note that the returned StringRef may not point to the
859  /// supplied buffer if a copy can be avoided.
860  StringRef getSpelling(const Token &Tok,
861                        SmallVectorImpl<char> &Buffer,
862                        bool *Invalid = 0) const;
863
864  /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
865  /// with length 1, return the character.
866  char getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
867                                                   bool *Invalid = 0) const {
868    assert(Tok.is(tok::numeric_constant) &&
869           Tok.getLength() == 1 && "Called on unsupported token");
870    assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
871
872    // If the token is carrying a literal data pointer, just use it.
873    if (const char *D = Tok.getLiteralData())
874      return *D;
875
876    // Otherwise, fall back on getCharacterData, which is slower, but always
877    // works.
878    return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
879  }
880
881  /// \brief Retrieve the name of the immediate macro expansion.
882  ///
883  /// This routine starts from a source location, and finds the name of the macro
884  /// responsible for its immediate expansion. It looks through any intervening
885  /// macro argument expansions to compute this. It returns a StringRef which
886  /// refers to the SourceManager-owned buffer of the source where that macro
887  /// name is spelled. Thus, the result shouldn't out-live the SourceManager.
888  StringRef getImmediateMacroName(SourceLocation Loc) {
889    return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
890  }
891
892  /// CreateString - Plop the specified string into a scratch buffer and set the
893  /// specified token's location and length to it.  If specified, the source
894  /// location provides a location of the expansion point of the token.
895  void CreateString(const char *Buf, unsigned Len, Token &Tok,
896                    SourceLocation ExpansionLocStart = SourceLocation(),
897                    SourceLocation ExpansionLocEnd = SourceLocation());
898
899  /// \brief Computes the source location just past the end of the
900  /// token at this source location.
901  ///
902  /// This routine can be used to produce a source location that
903  /// points just past the end of the token referenced by \p Loc, and
904  /// is generally used when a diagnostic needs to point just after a
905  /// token where it expected something different that it received. If
906  /// the returned source location would not be meaningful (e.g., if
907  /// it points into a macro), this routine returns an invalid
908  /// source location.
909  ///
910  /// \param Offset an offset from the end of the token, where the source
911  /// location should refer to. The default offset (0) produces a source
912  /// location pointing just past the end of the token; an offset of 1 produces
913  /// a source location pointing to the last character in the token, etc.
914  SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
915    return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
916  }
917
918  /// \brief Returns true if the given MacroID location points at the first
919  /// token of the macro expansion.
920  ///
921  /// \param MacroBegin If non-null and function returns true, it is set to
922  /// begin location of the macro.
923  bool isAtStartOfMacroExpansion(SourceLocation loc,
924                                 SourceLocation *MacroBegin = 0) const {
925    return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
926                                            MacroBegin);
927  }
928
929  /// \brief Returns true if the given MacroID location points at the last
930  /// token of the macro expansion.
931  ///
932  /// \param MacroBegin If non-null and function returns true, it is set to
933  /// end location of the macro.
934  bool isAtEndOfMacroExpansion(SourceLocation loc,
935                               SourceLocation *MacroEnd = 0) const {
936    return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
937  }
938
939  /// DumpToken - Print the token to stderr, used for debugging.
940  ///
941  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
942  void DumpLocation(SourceLocation Loc) const;
943  void DumpMacro(const MacroInfo &MI) const;
944
945  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
946  /// token, return a new location that specifies a character within the token.
947  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
948                                         unsigned Char) const {
949    return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
950  }
951
952  /// IncrementPasteCounter - Increment the counters for the number of token
953  /// paste operations performed.  If fast was specified, this is a 'fast paste'
954  /// case we handled.
955  ///
956  void IncrementPasteCounter(bool isFast) {
957    if (isFast)
958      ++NumFastTokenPaste;
959    else
960      ++NumTokenPaste;
961  }
962
963  void PrintStats();
964
965  size_t getTotalMemory() const;
966
967  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
968  /// comment (/##/) in microsoft mode, this method handles updating the current
969  /// state, returning the token on the next source line.
970  void HandleMicrosoftCommentPaste(Token &Tok);
971
972  //===--------------------------------------------------------------------===//
973  // Preprocessor callback methods.  These are invoked by a lexer as various
974  // directives and events are found.
975
976  /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
977  /// identifier information for the token and install it into the token,
978  /// updating the token kind accordingly.
979  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
980
981private:
982  llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
983
984public:
985
986  // SetPoisonReason - Call this function to indicate the reason for
987  // poisoning an identifier. If that identifier is accessed while
988  // poisoned, then this reason will be used instead of the default
989  // "poisoned" diagnostic.
990  void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
991
992  // HandlePoisonedIdentifier - Display reason for poisoned
993  // identifier.
994  void HandlePoisonedIdentifier(Token & Tok);
995
996  void MaybeHandlePoisonedIdentifier(Token & Identifier) {
997    if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
998      if(II->isPoisoned()) {
999        HandlePoisonedIdentifier(Identifier);
1000      }
1001    }
1002  }
1003
1004private:
1005  /// Identifiers used for SEH handling in Borland. These are only
1006  /// allowed in particular circumstances
1007  // __except block
1008  IdentifierInfo *Ident__exception_code,
1009                 *Ident___exception_code,
1010                 *Ident_GetExceptionCode;
1011  // __except filter expression
1012  IdentifierInfo *Ident__exception_info,
1013                 *Ident___exception_info,
1014                 *Ident_GetExceptionInfo;
1015  // __finally
1016  IdentifierInfo *Ident__abnormal_termination,
1017                 *Ident___abnormal_termination,
1018                 *Ident_AbnormalTermination;
1019public:
1020  void PoisonSEHIdentifiers(bool Poison = true); // Borland
1021
1022  /// HandleIdentifier - This callback is invoked when the lexer reads an
1023  /// identifier and has filled in the tokens IdentifierInfo member.  This
1024  /// callback potentially macro expands it or turns it into a named token (like
1025  /// 'for').
1026  void HandleIdentifier(Token &Identifier);
1027
1028
1029  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1030  /// the current file.  This either returns the EOF token and returns true, or
1031  /// pops a level off the include stack and returns false, at which point the
1032  /// client should call lex again.
1033  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
1034
1035  /// HandleEndOfTokenLexer - This callback is invoked when the current
1036  /// TokenLexer hits the end of its token stream.
1037  bool HandleEndOfTokenLexer(Token &Result);
1038
1039  /// HandleDirective - This callback is invoked when the lexer sees a # token
1040  /// at the start of a line.  This consumes the directive, modifies the
1041  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
1042  /// read is the correct one.
1043  void HandleDirective(Token &Result);
1044
1045  /// CheckEndOfDirective - Ensure that the next token is a tok::eod token.  If
1046  /// not, emit a diagnostic and consume up until the eod.  If EnableMacros is
1047  /// true, then we consider macros that expand to zero tokens as being ok.
1048  void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
1049
1050  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1051  /// current line until the tok::eod token is found.
1052  void DiscardUntilEndOfDirective();
1053
1054  /// SawDateOrTime - This returns true if the preprocessor has seen a use of
1055  /// __DATE__ or __TIME__ in the file so far.
1056  bool SawDateOrTime() const {
1057    return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
1058  }
1059  unsigned getCounterValue() const { return CounterValue; }
1060  void setCounterValue(unsigned V) { CounterValue = V; }
1061
1062  /// \brief Retrieves the module that we're currently building, if any.
1063  Module *getCurrentModule();
1064
1065  /// \brief Allocate a new MacroInfo object with the provided SourceLocation.
1066  MacroInfo *AllocateMacroInfo(SourceLocation L);
1067
1068  /// \brief Allocate a new MacroInfo object which is clone of \p MI.
1069  MacroInfo *CloneMacroInfo(const MacroInfo &MI);
1070
1071  /// \brief Turn the specified lexer token into a fully checked and spelled
1072  /// filename, e.g. as an operand of \#include.
1073  ///
1074  /// The caller is expected to provide a buffer that is large enough to hold
1075  /// the spelling of the filename, but is also expected to handle the case
1076  /// when this method decides to use a different buffer.
1077  ///
1078  /// \returns true if the input filename was in <>'s or false if it was
1079  /// in ""'s.
1080  bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1081
1082  /// \brief Given a "foo" or \<foo> reference, look up the indicated file.
1083  ///
1084  /// Returns null on failure.  \p isAngled indicates whether the file
1085  /// reference is for system \#include's or not (i.e. using <> instead of "").
1086  const FileEntry *LookupFile(StringRef Filename,
1087                              bool isAngled, const DirectoryLookup *FromDir,
1088                              const DirectoryLookup *&CurDir,
1089                              SmallVectorImpl<char> *SearchPath,
1090                              SmallVectorImpl<char> *RelativePath,
1091                              Module **SuggestedModule,
1092                              bool SkipCache = false);
1093
1094  /// GetCurLookup - The DirectoryLookup structure used to find the current
1095  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
1096  /// implement \#include_next and find directory-specific properties.
1097  const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1098
1099  /// \brief Return true if we're in the top-level file, not in a \#include.
1100  bool isInPrimaryFile() const;
1101
1102  /// ConcatenateIncludeName - Handle cases where the \#include name is expanded
1103  /// from a macro as multiple tokens, which need to be glued together.  This
1104  /// occurs for code like:
1105  /// \code
1106  ///    \#define FOO <a/b.h>
1107  ///    \#include FOO
1108  /// \endcode
1109  /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1110  ///
1111  /// This code concatenates and consumes tokens up to the '>' token.  It
1112  /// returns false if the > was found, otherwise it returns true if it finds
1113  /// and consumes the EOD marker.
1114  bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1115                              SourceLocation &End);
1116
1117  /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is
1118  /// followed by EOD.  Return true if the token is not a valid on-off-switch.
1119  bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
1120
1121private:
1122
1123  void PushIncludeMacroStack() {
1124    IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind,
1125                                                 CurLexer.take(),
1126                                                 CurPTHLexer.take(),
1127                                                 CurPPLexer,
1128                                                 CurTokenLexer.take(),
1129                                                 CurDirLookup));
1130    CurPPLexer = 0;
1131  }
1132
1133  void PopIncludeMacroStack() {
1134    CurLexer.reset(IncludeMacroStack.back().TheLexer);
1135    CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer);
1136    CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1137    CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
1138    CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1139    CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1140    IncludeMacroStack.pop_back();
1141  }
1142
1143  /// \brief Allocate a new MacroInfo object.
1144  MacroInfo *AllocateMacroInfo();
1145
1146  /// \brief Release the specified MacroInfo for re-use.
1147  ///
1148  /// This memory will  be reused for allocating new MacroInfo objects.
1149  void ReleaseMacroInfo(MacroInfo* MI);
1150
1151  /// ReadMacroName - Lex and validate a macro name, which occurs after a
1152  /// \#define or \#undef.  This emits a diagnostic, sets the token kind to eod,
1153  /// and discards the rest of the macro line if the macro name is invalid.
1154  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
1155
1156  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1157  /// definition has just been read.  Lex the rest of the arguments and the
1158  /// closing ), updating MI with what we learn and saving in LastTok the
1159  /// last token read.
1160  /// Return true if an error occurs parsing the arg list.
1161  bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok);
1162
1163  /// We just read a \#if or related directive and decided that the
1164  /// subsequent tokens are in the \#if'd out portion of the
1165  /// file.  Lex the rest of the file, until we see an \#endif.  If \p
1166  /// FoundNonSkipPortion is true, then we have already emitted code for part of
1167  /// this \#if directive, so \#else/\#elif blocks should never be entered. If
1168  /// \p FoundElse is false, then \#else directives are ok, if not, then we have
1169  /// already seen one so a \#else directive is a duplicate.  When this returns,
1170  /// the caller can lex the first valid token.
1171  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1172                                    bool FoundNonSkipPortion, bool FoundElse,
1173                                    SourceLocation ElseLoc = SourceLocation());
1174
1175  /// \brief A fast PTH version of SkipExcludedConditionalBlock.
1176  void PTHSkipExcludedConditionalBlock();
1177
1178  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
1179  /// may occur after a #if or #elif directive and return it as a bool.  If the
1180  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
1181  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
1182
1183  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1184  /// \#pragma GCC poison/system_header/dependency and \#pragma once.
1185  void RegisterBuiltinPragmas();
1186
1187  /// \brief Register builtin macros such as __LINE__ with the identifier table.
1188  void RegisterBuiltinMacros();
1189
1190  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
1191  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
1192  /// the macro should not be expanded return true, otherwise return false.
1193  bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
1194
1195  /// \brief Cache macro expanded tokens for TokenLexers.
1196  //
1197  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1198  /// going to lex in the cache and when it finishes the tokens are removed
1199  /// from the end of the cache.
1200  Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
1201                                  ArrayRef<Token> tokens);
1202  void removeCachedMacroExpandedTokensOfLastLexer();
1203  friend void TokenLexer::ExpandFunctionArguments();
1204
1205  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
1206  /// lexed is a '('.  If so, consume the token and return true, if not, this
1207  /// method should have no observable side-effect on the lexed tokens.
1208  bool isNextPPTokenLParen();
1209
1210  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
1211  /// invoked to read all of the formal arguments specified for the macro
1212  /// invocation.  This returns null on error.
1213  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
1214                                       SourceLocation &ExpansionEnd);
1215
1216  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1217  /// as a builtin macro, handle it and return the next token as 'Tok'.
1218  void ExpandBuiltinMacro(Token &Tok);
1219
1220  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
1221  /// return the first token after the directive.  The _Pragma token has just
1222  /// been read into 'Tok'.
1223  void Handle_Pragma(Token &Tok);
1224
1225  /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
1226  /// is not enclosed within a string literal.
1227  void HandleMicrosoft__pragma(Token &Tok);
1228
1229  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
1230  /// start lexing tokens from it instead of the current buffer.
1231  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
1232
1233  /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and
1234  /// start getting tokens from it using the PTH cache.
1235  void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
1236
1237  /// IsFileLexer - Returns true if we are lexing from a file and not a
1238  ///  pragma or a macro.
1239  static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
1240    return L ? !L->isPragmaLexer() : P != 0;
1241  }
1242
1243  static bool IsFileLexer(const IncludeStackInfo& I) {
1244    return IsFileLexer(I.TheLexer, I.ThePPLexer);
1245  }
1246
1247  bool IsFileLexer() const {
1248    return IsFileLexer(CurLexer.get(), CurPPLexer);
1249  }
1250
1251  //===--------------------------------------------------------------------===//
1252  // Caching stuff.
1253  void CachingLex(Token &Result);
1254  bool InCachingLexMode() const {
1255    // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
1256    // that we are past EOF, not that we are in CachingLex mode.
1257    return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 &&
1258           !IncludeMacroStack.empty();
1259  }
1260  void EnterCachingLexMode();
1261  void ExitCachingLexMode() {
1262    if (InCachingLexMode())
1263      RemoveTopOfLexerStack();
1264  }
1265  const Token &PeekAhead(unsigned N);
1266  void AnnotatePreviousCachedTokens(const Token &Tok);
1267
1268  //===--------------------------------------------------------------------===//
1269  /// Handle*Directive - implement the various preprocessor directives.  These
1270  /// should side-effect the current preprocessor object so that the next call
1271  /// to Lex() will return the appropriate token next.
1272  void HandleLineDirective(Token &Tok);
1273  void HandleDigitDirective(Token &Tok);
1274  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1275  void HandleIdentSCCSDirective(Token &Tok);
1276  void HandleMacroPublicDirective(Token &Tok);
1277  void HandleMacroPrivateDirective(Token &Tok);
1278
1279  // File inclusion.
1280  void HandleIncludeDirective(SourceLocation HashLoc,
1281                              Token &Tok,
1282                              const DirectoryLookup *LookupFrom = 0,
1283                              bool isImport = false);
1284  void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1285  void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1286  void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
1287  void HandleMicrosoftImportDirective(Token &Tok);
1288
1289  // Macro handling.
1290  void HandleDefineDirective(Token &Tok);
1291  void HandleUndefDirective(Token &Tok);
1292
1293  // Conditional Inclusion.
1294  void HandleIfdefDirective(Token &Tok, bool isIfndef,
1295                            bool ReadAnyTokensBeforeDirective);
1296  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1297  void HandleEndifDirective(Token &Tok);
1298  void HandleElseDirective(Token &Tok);
1299  void HandleElifDirective(Token &Tok);
1300
1301  // Pragmas.
1302  void HandlePragmaDirective(unsigned Introducer);
1303public:
1304  void HandlePragmaOnce(Token &OnceTok);
1305  void HandlePragmaMark();
1306  void HandlePragmaPoison(Token &PoisonTok);
1307  void HandlePragmaSystemHeader(Token &SysHeaderTok);
1308  void HandlePragmaDependency(Token &DependencyTok);
1309  void HandlePragmaComment(Token &CommentTok);
1310  void HandlePragmaMessage(Token &MessageTok);
1311  void HandlePragmaPushMacro(Token &Tok);
1312  void HandlePragmaPopMacro(Token &Tok);
1313  void HandlePragmaIncludeAlias(Token &Tok);
1314  IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1315
1316  // Return true and store the first token only if any CommentHandler
1317  // has inserted some tokens and getCommentRetentionState() is false.
1318  bool HandleComment(Token &Token, SourceRange Comment);
1319
1320  /// \brief A macro is used, update information about macros that need unused
1321  /// warnings.
1322  void markMacroAsUsed(MacroInfo *MI);
1323};
1324
1325/// \brief Abstract base class that describes a handler that will receive
1326/// source ranges for each of the comments encountered in the source file.
1327class CommentHandler {
1328public:
1329  virtual ~CommentHandler();
1330
1331  // The handler shall return true if it has pushed any tokens
1332  // to be read using e.g. EnterToken or EnterTokenStream.
1333  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
1334};
1335
1336}  // end namespace clang
1337
1338#endif
1339