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