Preprocessor.h revision 7bfaaaecb3113f955db31e8d8a51acffd1bc0c27
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/Lexer.h"
18#include "clang/Lex/PPCallbacks.h"
19#include "clang/Lex/TokenLexer.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/SourceLocation.h"
22#include "llvm/ADT/DenseMap.h"
23
24namespace clang {
25
26class SourceManager;
27class FileManager;
28class FileEntry;
29class HeaderSearch;
30class PragmaNamespace;
31class PragmaHandler;
32class ScratchBuffer;
33class TargetInfo;
34class PPCallbacks;
35class DirectoryLookup;
36
37/// Preprocessor - This object engages in a tight little dance with the lexer to
38/// efficiently preprocess tokens.  Lexers know only about tokens within a
39/// single source file, and don't know anything about preprocessor-level issues
40/// like the #include stack, token expansion, etc.
41///
42class Preprocessor {
43  Diagnostic        &Diags;
44  const LangOptions &Features;
45  TargetInfo        &Target;
46  FileManager       &FileMgr;
47  SourceManager     &SourceMgr;
48  ScratchBuffer     *ScratchBuf;
49  HeaderSearch      &HeaderInfo;
50
51  /// Identifiers for builtin macros and other builtins.
52  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
53  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
54  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
55  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
56  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
57  IdentifierInfo *Ident_Pragma, *Ident__VA_ARGS__; // _Pragma, __VA_ARGS__
58
59  SourceLocation DATELoc, TIMELoc;
60
61  enum {
62    /// MaxIncludeStackDepth - Maximum depth of #includes.
63    MaxAllowedIncludeStackDepth = 200
64  };
65
66  // State that is set before the preprocessor begins.
67  bool KeepComments : 1;
68  bool KeepMacroComments : 1;
69
70  // State that changes while the preprocessor runs:
71  bool DisableMacroExpansion : 1;  // True if macro expansion is disabled.
72  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
73
74  /// CacheTokens - True when the lexed tokens are cached for backtracking.
75  bool CacheTokens : 1;
76
77  /// Identifiers - This is mapping/lookup information for all identifiers in
78  /// the program, including program keywords.
79  IdentifierTable Identifiers;
80
81  /// Selectors - This table contains all the selectors in the program. Unlike
82  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
83  /// It is declared/instantiated here because it's role/lifetime is
84  /// conceptually similar the IdentifierTable. In addition, the current control
85  /// flow (in clang::ParseAST()), make it convenient to put here.
86  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
87  /// the lifetime fo the preprocessor.
88  SelectorTable Selectors;
89
90  /// PragmaHandlers - This tracks all of the pragmas that the client registered
91  /// with this preprocessor.
92  PragmaNamespace *PragmaHandlers;
93
94  /// CurLexer - This is the current top of the stack that we're lexing from if
95  /// not expanding a macro.  One of CurLexer and CurTokenLexer must be null.
96  Lexer *CurLexer;
97
98  /// CurLookup - The DirectoryLookup structure used to find the current
99  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
100  /// implement #include_next and find directory-specific properties.
101  const DirectoryLookup *CurDirLookup;
102
103  /// CurTokenLexer - This is the current macro we are expanding, if we are
104  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
105  TokenLexer *CurTokenLexer;
106
107  /// IncludeMacroStack - This keeps track of the stack of files currently
108  /// #included, and macros currently being expanded from, not counting
109  /// CurLexer/CurTokenLexer.
110  struct IncludeStackInfo {
111    Lexer *TheLexer;
112    const DirectoryLookup *TheDirLookup;
113    TokenLexer *TheTokenLexer;
114    IncludeStackInfo(Lexer *L, const DirectoryLookup *D, TokenLexer *TL)
115      : TheLexer(L), TheDirLookup(D), TheTokenLexer(TL) {
116    }
117  };
118  std::vector<IncludeStackInfo> IncludeMacroStack;
119
120  /// Callbacks - These are actions invoked when some preprocessor activity is
121  /// encountered (e.g. a file is #included, etc).
122  PPCallbacks *Callbacks;
123
124  /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
125  /// to the actual definition of the macro.
126  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
127
128  // Various statistics we track for performance analysis.
129  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
130  unsigned NumIf, NumElse, NumEndif;
131  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
132  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
133  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
134  unsigned NumSkipped;
135
136  /// Predefines - This string is the predefined macros that preprocessor
137  /// should use from the command line etc.
138  std::string Predefines;
139
140  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
141  enum { TokenLexerCacheSize = 8 };
142  unsigned NumCachedTokenLexers;
143  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
144
145  // Cached tokens state.
146
147  typedef std::vector<Token> CachedTokensTy;
148
149  /// CachedTokens - Cached tokens are stored here when we do backtracking or
150  /// lookahead. They are "lexed" by the CachingLex() method.
151  CachedTokensTy CachedTokens;
152
153  /// CachedLexPos - The position of the cached token that CachingLex() should
154  /// "lex" next. If it points beyond the CachedTokens vector, it means that
155  /// a normal Lex() should be invoked.
156  CachedTokensTy::size_type CachedLexPos;
157
158  /// CachedBacktrackPos - Gets set by the EnableBacktrackAtThisPos() method,
159  /// to indicate the position where CachedLexPos should be set when the
160  /// BackTrack() method is invoked.
161  CachedTokensTy::size_type CachedBacktrackPos;
162
163public:
164  Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target,
165               SourceManager &SM, HeaderSearch &Headers);
166  ~Preprocessor();
167
168  Diagnostic &getDiagnostics() const { return Diags; }
169  const LangOptions &getLangOptions() const { return Features; }
170  TargetInfo &getTargetInfo() const { return Target; }
171  FileManager &getFileManager() const { return FileMgr; }
172  SourceManager &getSourceManager() const { return SourceMgr; }
173  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
174
175  IdentifierTable &getIdentifierTable() { return Identifiers; }
176  SelectorTable &getSelectorTable() { return Selectors; }
177
178  inline FullSourceLoc getFullLoc(SourceLocation Loc) const {
179    return FullSourceLoc(Loc, getSourceManager());
180  }
181
182  /// SetCommentRetentionState - Control whether or not the preprocessor retains
183  /// comments in output.
184  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
185    this->KeepComments = KeepComments | KeepMacroComments;
186    this->KeepMacroComments = KeepMacroComments;
187  }
188
189  bool getCommentRetentionState() const { return KeepComments; }
190
191  /// isCurrentLexer - Return true if we are lexing directly from the specified
192  /// lexer.
193  bool isCurrentLexer(const Lexer *L) const {
194    return CurLexer == L;
195  }
196
197  /// getCurrentLexer - Return the current file lexer being lexed from.  Note
198  /// that this ignores any potentially active macro expansions and _Pragma
199  /// expansions going on at the time.
200  Lexer *getCurrentFileLexer() const;
201
202  /// getPPCallbacks/setPPCallbacks - Accessors for preprocessor callbacks.
203  /// Note that this class takes ownership of any PPCallbacks object given to
204  /// it.
205  PPCallbacks *getPPCallbacks() const { return Callbacks; }
206  void setPPCallbacks(PPCallbacks *C) {
207    delete Callbacks;
208    Callbacks = C;
209  }
210
211  /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to
212  /// or null if it isn't #define'd.
213  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
214    return II->hasMacroDefinition() ? Macros.find(II)->second : 0;
215  }
216
217  /// setMacroInfo - Specify a macro for this identifier.
218  ///
219  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
220
221  const std::string &getPredefines() const { return Predefines; }
222  /// setPredefines - Set the predefines for this Preprocessor.  These
223  /// predefines are automatically injected when parsing the main file.
224  void setPredefines(const char *P) { Predefines = P; }
225  void setPredefines(const std::string &P) { Predefines = P; }
226
227  /// getIdentifierInfo - Return information about the specified preprocessor
228  /// identifier token.  The version of this method that takes two character
229  /// pointers is preferred unless the identifier is already available as a
230  /// string (this avoids allocation and copying of memory to construct an
231  /// std::string).
232  IdentifierInfo *getIdentifierInfo(const char *NameStart,
233                                    const char *NameEnd) {
234    return &Identifiers.get(NameStart, NameEnd);
235  }
236  IdentifierInfo *getIdentifierInfo(const char *NameStr) {
237    return getIdentifierInfo(NameStr, NameStr+strlen(NameStr));
238  }
239
240  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
241  /// If 'Namespace' is non-null, then it is a token required to exist on the
242  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
243  void AddPragmaHandler(const char *Namespace, PragmaHandler *Handler);
244
245  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
246  /// which implicitly adds the builtin defines etc.
247  void EnterMainSourceFile();
248
249  /// EnterSourceFile - Add a source file to the top of the include stack and
250  /// start lexing tokens from it instead of the current buffer.  If isMainFile
251  /// is true, this is the main file for the translation unit.
252  void EnterSourceFile(unsigned CurFileID, const DirectoryLookup *Dir);
253
254  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
255  /// tokens from it instead of the current buffer.  Args specifies the
256  /// tokens input to a function-like macro.
257  void EnterMacro(Token &Identifier, MacroArgs *Args);
258
259  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
260  /// which will cause the lexer to start returning the specified tokens.
261  ///
262  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
263  /// not be subject to further macro expansion.  Otherwise, these tokens will
264  /// be re-macro-expanded when/if expansion is enabled.
265  ///
266  /// If OwnsTokens is false, this method assumes that the specified stream of
267  /// tokens has a permanent owner somewhere, so they do not need to be copied.
268  /// If it is true, it assumes the array of tokens is allocated with new[] and
269  /// must be freed.
270  ///
271  void EnterTokenStream(const Token *Toks, unsigned NumToks,
272                        bool DisableMacroExpansion, bool OwnsTokens);
273
274  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
275  /// lexer stack.  This should only be used in situations where the current
276  /// state of the top-of-stack lexer is known.
277  void RemoveTopOfLexerStack();
278
279  /// EnableBacktrackAtThisPos - From the point that this method is called, and
280  /// until DisableBacktrack() or Backtrack() is called, the Preprocessor keeps
281  /// track of the lexed tokens so that a subsequent Backtrack() call will make
282  /// the Preprocessor re-lex the same tokens.
283  ///
284  /// EnableBacktrackAtThisPos should not be called again until DisableBacktrack
285  /// or Backtrack is called.
286  ///
287  /// NOTE: *DO NOT* forget to call either DisableBacktrack() or Backtrack() at
288  /// some point after EnableBacktrackAtThisPos. If you don't, caching of tokens
289  /// will continue indefinitely.
290  ///
291  void EnableBacktrackAtThisPos() {
292    assert(!CacheTokens && "Backtrack is already enabled!");
293    CacheTokens = true;
294    CachedBacktrackPos = CachedLexPos;
295    EnterCachingLexMode();
296  }
297
298  /// DisableBacktrack - Stop the caching of tokens that was enabled by
299  /// EnableBacktrackAtThisPos().
300  void DisableBacktrack() {
301    assert(CacheTokens && "Backtrack is not enabled!");
302    CacheTokens = false;
303  }
304
305  /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
306  /// EnableBacktrackAtThisPos() was previously called.
307  void Backtrack() {
308    assert(CacheTokens && "Backtrack is not enabled!");
309    CacheTokens = false;
310    CachedLexPos = CachedBacktrackPos;
311  }
312
313  /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
314  /// caching of tokens is on.
315  bool isBacktrackEnabled() const { return CacheTokens; }
316
317  /// Lex - To lex a token from the preprocessor, just pull a token from the
318  /// current lexer or macro object.
319  void Lex(Token &Result) {
320    if (CurLexer)
321      CurLexer->Lex(Result);
322    else if (CurTokenLexer)
323      CurTokenLexer->Lex(Result);
324    else
325      CachingLex(Result);
326  }
327
328  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
329  /// something not a comment.  This is useful in -E -C mode where comments
330  /// would foul up preprocessor directive handling.
331  void LexNonComment(Token &Result) {
332    do
333      Lex(Result);
334    while (Result.getKind() == tok::comment);
335  }
336
337  /// LexUnexpandedToken - This is just like Lex, but this disables macro
338  /// expansion of identifier tokens.
339  void LexUnexpandedToken(Token &Result) {
340    // Disable macro expansion.
341    bool OldVal = DisableMacroExpansion;
342    DisableMacroExpansion = true;
343    // Lex the token.
344    Lex(Result);
345
346    // Reenable it.
347    DisableMacroExpansion = OldVal;
348  }
349
350  /// LookAhead - This peeks ahead N tokens and returns that token without
351  /// consuming any tokens.  LookAhead(0) returns the next token that would be
352  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
353  /// returns normal tokens after phase 5.  As such, it is equivalent to using
354  /// 'Lex', not 'LexUnexpandedToken'.
355  const Token &LookAhead(unsigned N) {
356    if (CachedLexPos + N < CachedTokens.size())
357      return CachedTokens[CachedLexPos+N];
358    else
359      return PeekAhead(N+1);
360  }
361
362  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
363  /// the specified Token's location, translating the token's start
364  /// position in the current buffer into a SourcePosition object for rendering.
365  void Diag(SourceLocation Loc, unsigned DiagID);
366  void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
367  void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
368            const SourceRange &R1, const SourceRange &R2);
369  void Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R);
370  void Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R1,
371            const SourceRange &R2);
372  void Diag(const Token &Tok, unsigned DiagID) {
373    Diag(Tok.getLocation(), DiagID);
374  }
375  void Diag(const Token &Tok, unsigned DiagID, const std::string &Msg) {
376    Diag(Tok.getLocation(), DiagID, Msg);
377  }
378
379  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
380  /// token is the characters used to represent the token in the source file
381  /// after trigraph expansion and escaped-newline folding.  In particular, this
382  /// wants to get the true, uncanonicalized, spelling of things like digraphs
383  /// UCNs, etc.
384  std::string getSpelling(const Token &Tok) const;
385
386  /// getSpelling - This method is used to get the spelling of a token into a
387  /// preallocated buffer, instead of as an std::string.  The caller is required
388  /// to allocate enough space for the token, which is guaranteed to be at least
389  /// Tok.getLength() bytes long.  The length of the actual result is returned.
390  ///
391  /// Note that this method may do two possible things: it may either fill in
392  /// the buffer specified with characters, or it may *change the input pointer*
393  /// to point to a constant buffer with the data already in it (avoiding a
394  /// copy).  The caller is not allowed to modify the returned buffer pointer
395  /// if an internal buffer is returned.
396  unsigned getSpelling(const Token &Tok, const char *&Buffer) const;
397
398
399  /// CreateString - Plop the specified string into a scratch buffer and return
400  /// a location for it.  If specified, the source location provides a source
401  /// location for the token.
402  SourceLocation CreateString(const char *Buf, unsigned Len,
403                              SourceLocation SourceLoc = SourceLocation());
404
405  /// DumpToken - Print the token to stderr, used for debugging.
406  ///
407  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
408  void DumpLocation(SourceLocation Loc) const;
409  void DumpMacro(const MacroInfo &MI) const;
410
411  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
412  /// token, return a new location that specifies a character within the token.
413  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,unsigned Char);
414
415  /// IncrementPasteCounter - Increment the counters for the number of token
416  /// paste operations performed.  If fast was specified, this is a 'fast paste'
417  /// case we handled.
418  ///
419  void IncrementPasteCounter(bool isFast) {
420    if (isFast)
421      ++NumFastTokenPaste;
422    else
423      ++NumTokenPaste;
424  }
425
426  void PrintStats();
427
428  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
429  /// comment (/##/) in microsoft mode, this method handles updating the current
430  /// state, returning the token on the next source line.
431  void HandleMicrosoftCommentPaste(Token &Tok);
432
433  //===--------------------------------------------------------------------===//
434  // Preprocessor callback methods.  These are invoked by a lexer as various
435  // directives and events are found.
436
437  /// LookUpIdentifierInfo - Given a tok::identifier token, look up the
438  /// identifier information for the token and install it into the token.
439  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier,
440                                       const char *BufPtr = 0);
441
442  /// HandleIdentifier - This callback is invoked when the lexer reads an
443  /// identifier and has filled in the tokens IdentifierInfo member.  This
444  /// callback potentially macro expands it or turns it into a named token (like
445  /// 'for').
446  void HandleIdentifier(Token &Identifier);
447
448
449  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
450  /// the current file.  This either returns the EOF token and returns true, or
451  /// pops a level off the include stack and returns false, at which point the
452  /// client should call lex again.
453  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
454
455  /// HandleEndOfTokenLexer - This callback is invoked when the current
456  /// TokenLexer hits the end of its token stream.
457  bool HandleEndOfTokenLexer(Token &Result);
458
459  /// HandleDirective - This callback is invoked when the lexer sees a # token
460  /// at the start of a line.  This consumes the directive, modifies the
461  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
462  /// read is the correct one.
463  void HandleDirective(Token &Result);
464
465  /// CheckEndOfDirective - Ensure that the next token is a tok::eom token.  If
466  /// not, emit a diagnostic and consume up until the eom.
467  void CheckEndOfDirective(const char *Directive);
468private:
469  /// isInPrimaryFile - Return true if we're in the top-level file, not in a
470  /// #include.
471  bool isInPrimaryFile() const;
472
473  /// isSystemHeader - Return true if F is a system header.
474  bool isSystemHeader(const FileEntry* F) const;
475
476  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
477  /// current line until the tok::eom token is found.
478  void DiscardUntilEndOfDirective();
479
480  /// ReadMacroName - Lex and validate a macro name, which occurs after a
481  /// #define or #undef.  This emits a diagnostic, sets the token kind to eom,
482  /// and discards the rest of the macro line if the macro name is invalid.
483  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
484
485  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
486  /// definition has just been read.  Lex the rest of the arguments and the
487  /// closing ), updating MI with what we learn.  Return true if an error occurs
488  /// parsing the arg list.
489  bool ReadMacroDefinitionArgList(MacroInfo *MI);
490
491  /// SkipExcludedConditionalBlock - We just read a #if or related directive and
492  /// decided that the subsequent tokens are in the #if'd out portion of the
493  /// file.  Lex the rest of the file, until we see an #endif.  If
494  /// FoundNonSkipPortion is true, then we have already emitted code for part of
495  /// this #if directive, so #else/#elif blocks should never be entered. If
496  /// FoundElse is false, then #else directives are ok, if not, then we have
497  /// already seen one so a #else directive is a duplicate.  When this returns,
498  /// the caller can lex the first valid token.
499  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
500                                    bool FoundNonSkipPortion, bool FoundElse);
501
502  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
503  /// may occur after a #if or #elif directive and return it as a bool.  If the
504  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
505  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
506
507  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
508  /// #pragma GCC poison/system_header/dependency and #pragma once.
509  void RegisterBuiltinPragmas();
510
511  /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
512  /// identifier table.
513  void RegisterBuiltinMacros();
514  IdentifierInfo *RegisterBuiltinMacro(const char *Name);
515
516  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
517  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
518  /// the macro should not be expanded return true, otherwise return false.
519  bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
520
521  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
522  /// lexed is a '('.  If so, consume the token and return true, if not, this
523  /// method should have no observable side-effect on the lexed tokens.
524  bool isNextPPTokenLParen();
525
526  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
527  /// invoked to read all of the formal arguments specified for the macro
528  /// invocation.  This returns null on error.
529  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI);
530
531  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
532  /// as a builtin macro, handle it and return the next token as 'Tok'.
533  void ExpandBuiltinMacro(Token &Tok);
534
535  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
536  /// return the first token after the directive.  The _Pragma token has just
537  /// been read into 'Tok'.
538  void Handle_Pragma(Token &Tok);
539
540
541  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
542  /// start lexing tokens from it instead of the current buffer.
543  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
544
545  /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
546  /// checked and spelled filename, e.g. as an operand of #include. This returns
547  /// true if the input filename was in <>'s or false if it were in ""'s.  The
548  /// caller is expected to provide a buffer that is large enough to hold the
549  /// spelling of the filename, but is also expected to handle the case when
550  /// this method decides to use a different buffer.
551  bool GetIncludeFilenameSpelling(SourceLocation Loc,
552                                  const char *&BufStart, const char *&BufEnd);
553
554  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
555  /// return null on failure.  isAngled indicates whether the file reference is
556  /// for system #include's or not (i.e. using <> instead of "").
557  const FileEntry *LookupFile(const char *FilenameStart,const char *FilenameEnd,
558                              bool isAngled, const DirectoryLookup *FromDir,
559                              const DirectoryLookup *&CurDir);
560
561  //===--------------------------------------------------------------------===//
562  // Caching stuff.
563  void CachingLex(Token &Result);
564  bool InCachingLexMode() const { return CurLexer == 0 && CurTokenLexer == 0; }
565  void EnterCachingLexMode();
566  void ExitCachingLexMode() {
567    if (InCachingLexMode())
568      RemoveTopOfLexerStack();
569  }
570  const Token &PeekAhead(unsigned N);
571
572  //===--------------------------------------------------------------------===//
573  /// Handle*Directive - implement the various preprocessor directives.  These
574  /// should side-effect the current preprocessor object so that the next call
575  /// to Lex() will return the appropriate token next.
576
577  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
578  void HandleIdentSCCSDirective(Token &Tok);
579
580  // File inclusion.
581  void HandleIncludeDirective(Token &Tok,
582                              const DirectoryLookup *LookupFrom = 0,
583                              bool isImport = false);
584  void HandleIncludeNextDirective(Token &Tok);
585  void HandleImportDirective(Token &Tok);
586
587  // Macro handling.
588  void HandleDefineDirective(Token &Tok);
589  void HandleUndefDirective(Token &Tok);
590  // HandleAssertDirective(Token &Tok);
591  // HandleUnassertDirective(Token &Tok);
592
593  // Conditional Inclusion.
594  void HandleIfdefDirective(Token &Tok, bool isIfndef,
595                            bool ReadAnyTokensBeforeDirective);
596  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
597  void HandleEndifDirective(Token &Tok);
598  void HandleElseDirective(Token &Tok);
599  void HandleElifDirective(Token &Tok);
600
601  // Pragmas.
602  void HandlePragmaDirective();
603public:
604  void HandlePragmaOnce(Token &OnceTok);
605  void HandlePragmaMark();
606  void HandlePragmaPoison(Token &PoisonTok);
607  void HandlePragmaSystemHeader(Token &SysHeaderTok);
608  void HandlePragmaDependency(Token &DependencyTok);
609};
610
611/// PreprocessorFactory - A generic factory interface for lazily creating
612///  Preprocessor objects on-demand when they are needed.
613class PreprocessorFactory {
614public:
615  virtual ~PreprocessorFactory();
616  virtual Preprocessor* CreatePreprocessor() = 0;
617};
618
619}  // end namespace clang
620
621#endif
622