Preprocessor.h revision 69bda4c027671df7163619f215209529eb236620
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the Preprocessor interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_LEX_PREPROCESSOR_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek#include "clang/Lex/MacroInfo.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Lexer.h"
199c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek#include "clang/Lex/PTHLexer.h"
20eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner#include "clang/Lex/PPCallbacks.h"
215d75de0f821023f4ed4815825bf3aea8a0b5e40dChris Lattner#include "clang/Lex/TokenLexer.h"
226137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek#include "clang/Lex/PTHManager.h"
231b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
24adc4eeb08042a35ae914fc557ffec0cef3df2374Chris Lattner#include "clang/Basic/Diagnostic.h"
25c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner#include "clang/Basic/IdentifierTable.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
27cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner#include "llvm/ADT/DenseMap.h"
284f32786ac45210143654390177105eb749b614e9Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h"
290827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis#include "llvm/ADT/SmallPtrSet.h"
30caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek#include "llvm/ADT/OwningPtr.h"
31f4d5eb4866a27d497f0bb75b12c2ffd48ad4d9c0Benjamin Kramer#include "llvm/ADT/SmallVector.h"
325b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis#include "llvm/ADT/ArrayRef.h"
330ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek#include "llvm/Support/Allocator.h"
342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor#include <vector>
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
3988a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregorclass ExternalPreprocessorSource;
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileManager;
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass HeaderSearch;
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaNamespace;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaHandler;
452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorclass CommentHandler;
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass ScratchBuffer;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass TargetInfo;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PPCallbacks;
49f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorclass CodeCompletionHandler;
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass DirectoryLookup;
5194dc8f640ebea52241412512ed48601626edbc58Douglas Gregorclass PreprocessingRecord;
526aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ModuleLoader;
53ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
54e5956bd2730c051835f9acd9e957c5d79f99e7c3Chris Lattner/// Preprocessor - This object engages in a tight little dance with the lexer to
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// efficiently preprocess tokens.  Lexers know only about tokens within a
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// single source file, and don't know anything about preprocessor-level issues
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// like the #include stack, token expansion, etc.
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
594f32786ac45210143654390177105eb749b614e9Ted Kremenekclass Preprocessor : public llvm::RefCountedBase<Preprocessor> {
60d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine        *Diags;
613e3cd93b2fd9644e970c389e715c13883faf68b6Douglas Gregor  LangOptions       &Features;
62998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  const TargetInfo  *Target;
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager       &FileMgr;
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager     &SourceMgr;
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ScratchBuffer     *ScratchBuf;
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch      &HeaderInfo;
676aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ModuleLoader      &TheModuleLoader;
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6988a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  /// \brief External source of macros.
7088a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  ExternalPreprocessorSource *ExternalSource;
71a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
72ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
736137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// PTH - An optional PTHManager object used for getting tokens from
746137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  ///  a token cache rather than lexing the original source file.
756137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  llvm::OwningPtr<PTHManager> PTH;
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  /// BP - A BumpPtrAllocator object used to quickly allocate and release
780ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  ///  objects internal to the Preprocessor.
790ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  llvm::BumpPtrAllocator BP;
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers for builtin macros and other builtins.
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
87c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
881ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  IdentifierInfo *Ident_Pragma, *Ident__pragma;    // _Pragma, __pragma
891ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
90148772a841cae6f32db16d890e788b92a763bb3fChris Lattner  IdentifierInfo *Ident__has_feature;              // __has_feature
91c1b5fa41f09512c74030b9a2a0d1564535e22a76Peter Collingbourne  IdentifierInfo *Ident__has_extension;            // __has_extension
92148772a841cae6f32db16d890e788b92a763bb3fChris Lattner  IdentifierInfo *Ident__has_builtin;              // __has_builtin
93cae5095c116c29a4e52c91fa0ad88df09dd2b45fAnders Carlsson  IdentifierInfo *Ident__has_attribute;            // __has_attribute
9492bd8c70a6837b647a6c55964f8d0a50bf561dbcJohn Thompson  IdentifierInfo *Ident__has_include;              // __has_include
9592bd8c70a6837b647a6c55964f8d0a50bf561dbcJohn Thompson  IdentifierInfo *Ident__has_include_next;         // __has_include_next
96d768150ef57f617c8d9fef48f3c92e8f21698024Ted Kremenek  IdentifierInfo *Ident__has_warning;              // __has_warning
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation DATELoc, TIMELoc;
99c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  unsigned CounterValue;  // Next __COUNTER__ value.
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum {
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// MaxIncludeStackDepth - Maximum depth of #includes.
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MaxAllowedIncludeStackDepth = 200
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that is set before the preprocessor begins.
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepComments : 1;
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepMacroComments : 1;
109f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  bool SuppressIncludeNotFoundError : 1;
110ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that changes while the preprocessor runs:
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1145814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar  /// Whether the preprocessor owns the header search object.
1155814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar  bool OwnsHeaderSearch : 1;
1165814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar
1171d9c54df56391ac4740db27d551782e81189cb51Chris Lattner  /// DisableMacroExpansion - True if macro expansion is disabled.
1181d9c54df56391ac4740db27d551782e81189cb51Chris Lattner  bool DisableMacroExpansion : 1;
1191d9c54df56391ac4740db27d551782e81189cb51Chris Lattner
12088a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  /// \brief Whether we have already loaded macros from the external source.
12188a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  mutable bool ReadMacrosFromExternalSource : 1;
122a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers - This is mapping/lookup information for all identifiers in
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the program, including program keywords.
125c3222091e1ffa35d0264ca6b680a88c9dc84ede2Daniel Dunbar  mutable IdentifierTable Identifiers;
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// Selectors - This table contains all the selectors in the program. Unlike
12868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
1299e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// It is declared/expanded here because it's role/lifetime is
13068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// conceptually similar the IdentifierTable. In addition, the current control
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// flow (in clang::ParseAST()), make it convenient to put here.
13268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
133083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  /// the lifetime of the preprocessor.
13429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable Selectors;
1352e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
1361b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  /// BuiltinInfo - Information about builtins.
1371b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  Builtin::Context BuiltinInfo;
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PragmaHandlers - This tracks all of the pragmas that the client registered
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// with this preprocessor.
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PragmaNamespace *PragmaHandlers;
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Tracks all of the comment handlers that the client registered
1442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// with this preprocessor.
1452e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<CommentHandler *> CommentHandlers;
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
147f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief The code-completion handler.
148f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  CodeCompletionHandler *CodeComplete;
149ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1502968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief The file that we're performing code-completion for, if any.
1512968442603b029949246467253eeac8139a5b6d8Douglas Gregor  const FileEntry *CodeCompletionFile;
1522968442603b029949246467253eeac8139a5b6d8Douglas Gregor
1537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The offset in file for the code-completion point.
1547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  unsigned CodeCompletionOffset;
1557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The location for the code-completion point. This gets instantiated
1577d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// when the CodeCompletionFile gets #include'ed for preprocessing.
1587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation CodeCompletionLoc;
1597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The start location for the file of the code-completion point.
1617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// This gets instantiated when the CodeCompletionFile gets #include'ed
1627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// for preprocessing.
1637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation CodeCompletionFileLoc;
1647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
165c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor  /// \brief The source location of the 'import' contextual keyword we just
166c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor  /// lexed, if any.
167b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  SourceLocation ModuleImportLoc;
1688dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
169b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// \brief The module import path that we're currently processing.
170b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2>
171b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor    ModuleImportPath;
172b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor
173b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// \brief Whether the module import expectes an identifier next. Otherwise,
174b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// it expects a '.' or ';'.
175b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  bool ModuleImportExpectsIdentifier;
176b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor
1778dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// \brief The source location of the currently-active
1788dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// #pragma clang arc_cf_code_audited begin.
1798dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation PragmaARCCFCodeAuditedLoc;
180ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief True if we hit the code-completion point.
1827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool CodeCompletionReached;
1837d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
184f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief The number of bytes that we will initially skip when entering the
185f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// main file, which is used when loading a precompiled preamble, along
186f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// with a flag that indicates whether skipping this number of bytes will
187f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// place the lexer at the start of a line.
188f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<unsigned, bool> SkipMainFilePreamble;
189ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLexer - This is the current top of the stack that we're lexing from if
1919c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// not expanding a macro and we are lexing directly from source code.
1929c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
193caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek  llvm::OwningPtr<Lexer> CurLexer;
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1959c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// CurPTHLexer - This is the current top of stack that we're lexing from if
1969c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  not expanding from a macro and we are lexing from a PTH cache.
1979c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
1989c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  llvm::OwningPtr<PTHLexer> CurPTHLexer;
1991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2009c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// CurPPLexer - This is the current top of the stack what we're lexing from
2019c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  if not expanding a macro.  This is an alias for either CurLexer or
2029c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  CurPTHLexer.
20323f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  PreprocessorLexer *CurPPLexer;
204ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLookup - The DirectoryLookup structure used to find the current
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// implement #include_next and find directory-specific properties.
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const DirectoryLookup *CurDirLookup;
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2106cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurTokenLexer - This is the current macro we are expanding, if we are
2116cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
212caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek  llvm::OwningPtr<TokenLexer> CurTokenLexer;
2131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
214b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  /// \brief The kind of lexer we're currently working with.
215ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  enum CurLexerKind {
216ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_Lexer,
217ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_PTHLexer,
218ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_TokenLexer,
219b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CLK_CachingLexer,
220b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CLK_LexAfterModuleImport
221b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  } CurLexerKind;
222b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor
2235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncludeMacroStack - This keeps track of the stack of files currently
2245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// #included, and macros currently being expanded from, not counting
2256cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurLexer/CurTokenLexer.
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  struct IncludeStackInfo {
227b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    enum CurLexerKind     CurLexerKind;
2289c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    Lexer                 *TheLexer;
2299c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    PTHLexer              *ThePTHLexer;
2309c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    PreprocessorLexer     *ThePPLexer;
2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TokenLexer            *TheTokenLexer;
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const DirectoryLookup *TheDirLookup;
2339c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek
234ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P,
235b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor                     PreprocessorLexer* PPL,
2369c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                     TokenLexer* TL, const DirectoryLookup *D)
237ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL),
238b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor        TheTokenLexer(TL), TheDirLookup(D) {}
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::vector<IncludeStackInfo> IncludeMacroStack;
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Callbacks - These are actions invoked when some preprocessor activity is
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// encountered (e.g. a file is #included, etc).
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *Callbacks;
2451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
246cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
247cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// to the actual definition of the macro.
248cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2500827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Macros that we want to warn because they are not used at the end
2510827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// of the translation unit; we store just their SourceLocations instead
2520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// something like MacroInfo*. The benefit of this is that when we are
2530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// deserializing from PCH, we don't need to deserialize identifier & macros
2540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// just so that we can report that they are unused, we just warn using
2550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the SourceLocations of this set (that will be filled by the ASTReader).
2560827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// We are using SmallPtrSet instead of a vector for faster removal.
2570827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
2580827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
2590827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
26023f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  /// MacroArgCache - This is a "freelist" of MacroArg objects that can be
26123f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  /// reused for quick allocation.
26223f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  MacroArgs *MacroArgCache;
26323f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  friend class MacroArgs;
264ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
265ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma
266ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// push_macro directive, we keep a MacroInfo stack used to restore
267f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  /// previous macro value.
268f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Various statistics we track for performance analysis.
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumIf, NumElse, NumEndif;
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumSkipped;
2771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
278aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// Predefines - This string is the predefined macros that preprocessor
279aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// should use from the command line etc.
280aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  std::string Predefines;
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2826cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
2836cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  enum { TokenLexerCacheSize = 8 };
2846cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  unsigned NumCachedTokenLexers;
2856cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
2869e0ed0bd5a3a7bac73973980ff32132a7724e674Argyrios Kyrtzidis
2875b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// \brief Keeps macro expanded tokens for TokenLexers.
2885b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  //
2895b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
2905b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// going to lex in the cache and when it finishes the tokens are removed
2915b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// from the end of the cache.
292686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<Token, 16> MacroExpandedTokens;
2935b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
2945b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis
2959e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// \brief A record of the macro definitions and expansions that
296ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// occurred during preprocessing.
29794dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  ///
29894dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// This is an optional side structure that can be enabled with
29994dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// \c createPreprocessingRecord() prior to preprocessing.
300b9e1b75772db2c7db566c6034ba90a07f22e35ebDouglas Gregor  PreprocessingRecord *Record;
301ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
302e671e1bc73615eda155059a772266ed2882d758cChris Lattnerprivate:  // Cached tokens state.
303686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  typedef SmallVector<Token, 1> CachedTokensTy;
30403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
30503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// CachedTokens - Cached tokens are stored here when we do backtracking or
30603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// lookahead. They are "lexed" by the CachingLex() method.
30703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  CachedTokensTy CachedTokens;
30803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
30903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// CachedLexPos - The position of the cached token that CachingLex() should
31003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// "lex" next. If it points beyond the CachedTokens vector, it means that
31103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// a normal Lex() should be invoked.
31203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  CachedTokensTy::size_type CachedLexPos;
31303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
314a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// BacktrackPositions - Stack of backtrack positions, allowing nested
315a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
316a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// indicate where CachedLexPos should be set when the BackTrack() method is
317a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// invoked (at which point the last position is popped).
318a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  std::vector<CachedTokensTy::size_type> BacktrackPositions;
31903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
320af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  struct MacroInfoChain {
321af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek    MacroInfo MI;
322af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek    MacroInfoChain *Next;
3239714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek    MacroInfoChain *Prev;
324af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  };
325af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek
326af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  /// MacroInfos are managed as a chain for easy disposal.  This is the head
327af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  /// of that list.
328af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  MacroInfoChain *MIChainHead;
329af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek
3309714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
3319714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  /// allocation.
3329714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  MacroInfoChain *MICache;
3339714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek
334295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor  MacroInfo *getInfoForMacro(IdentifierInfo *II) const;
335ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
337d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
338998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               const TargetInfo *target,
33972b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek               SourceManager &SM, HeaderSearch &Headers,
3406aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor               ModuleLoader &TheModuleLoader,
3415814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar               IdentifierInfoLookup *IILookup = 0,
342998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               bool OwnsHeaderSearch = false,
343998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               bool DelayInitialization = false);
34472b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ~Preprocessor();
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
347998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// \brief Initialize the preprocessor, if the constructor did not already
348998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// perform the initialization.
349998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  ///
350998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// \param Target Information about the target.
351998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  void Initialize(const TargetInfo &Target);
352ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
353d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return *Diags; }
354d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
3557c175fb196a2bc3dbc86ea3865c713e1875f3f6dChris Lattner
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const LangOptions &getLangOptions() const { return Features; }
357998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  const TargetInfo &getTargetInfo() const { return *Target; }
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager &getFileManager() const { return FileMgr; }
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager &getSourceManager() const { return SourceMgr; }
3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierTable &getIdentifierTable() { return Identifiers; }
36329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable &getSelectorTable() { return Selectors; }
3641b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
36532fca722dd974b8202d0fb9c71b6c185c0767da6Chris Lattner  llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
3661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  void setPTHManager(PTHManager* pm);
3681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
369880dcf21dfdb3ff763c60195b6794bab0d913095Ted Kremenek  PTHManager *getPTHManager() { return PTH.get(); }
3702e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
37188a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  void setExternalSource(ExternalPreprocessorSource *Source) {
37288a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor    ExternalSource = Source;
37388a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  }
374a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
37588a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  ExternalPreprocessorSource *getExternalSource() const {
37688a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor    return ExternalSource;
37788a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  }
378a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
3796aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  /// \brief Retrieve the module loader associated with this preprocessor.
3806aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
381ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// SetCommentRetentionState - Control whether or not the preprocessor retains
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// comments in output.
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepComments = KeepComments | KeepMacroComments;
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepMacroComments = KeepMacroComments;
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getCommentRetentionState() const { return KeepComments; }
3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
391f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  void SetSuppressIncludeNotFoundError(bool Suppress) {
392f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman    SuppressIncludeNotFoundError = Suppress;
393f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  }
394f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman
395f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  bool GetSuppressIncludeNotFoundError() {
396f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman    return SuppressIncludeNotFoundError;
397f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  }
398f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isCurrentLexer - Return true if we are lexing directly from the specified
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer.
4017062d9e9fc3ed89c4e4ea7055efea6585b7bac8dTed Kremenek  bool isCurrentLexer(const PreprocessorLexer *L) const {
4027062d9e9fc3ed89c4e4ea7055efea6585b7bac8dTed Kremenek    return CurPPLexer == L;
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
405a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// getCurrentLexer - Return the current lexer being lexed from.  Note
406a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// that this ignores any potentially active macro expansions and _Pragma
407a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// expansions going on at the time.
408a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
409a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
410321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// getCurrentFileLexer - Return the current file lexer being lexed from.
411321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// Note that this ignores any potentially active macro expansions and _Pragma
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansions going on at the time.
41368e48e4a81767997ef0231e47eca4f665102c95eTed Kremenek  PreprocessorLexer *getCurrentFileLexer() const;
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
415a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  /// getPPCallbacks/addPPCallbacks - Accessors for preprocessor callbacks.
416eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// Note that this class takes ownership of any PPCallbacks object given to
417eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// it.
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *getPPCallbacks() const { return Callbacks; }
419a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  void addPPCallbacks(PPCallbacks *C) {
420e5393fb93eb879d9ebbef102ae9311fa77e023ccDaniel Dunbar    if (Callbacks)
421e5393fb93eb879d9ebbef102ae9311fa77e023ccDaniel Dunbar      C = new PPChainedCallbacks(C, Callbacks);
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Callbacks = C;
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
425cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to
426cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// or null if it isn't #define'd.
427cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
428295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor    if (!II->hasMacroDefinition())
429295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor      return 0;
430ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
431295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor    return getInfoForMacro(II);
432cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  }
4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
434cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// setMacroInfo - Specify a macro for this identifier.
435cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  ///
436cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43809b5c1d08a33ecf5d9c61b922fbe679867336684Chris Lattner  /// macro_iterator/macro_begin/macro_end - This allows you to walk the current
43909b5c1d08a33ecf5d9c61b922fbe679867336684Chris Lattner  /// state of the macro table.  This visits every currently-defined macro.
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  typedef llvm::DenseMap<IdentifierInfo*,
44109b5c1d08a33ecf5d9c61b922fbe679867336684Chris Lattner                         MacroInfo*>::const_iterator macro_iterator;
44288a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
44388a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  macro_iterator macro_end(bool IncludeExternalMacros = true) const;
444a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
445e3d5e3ae5bd8028774f07d7c3751d4db82118942Chris Lattner  const std::string &getPredefines() const { return Predefines; }
446aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// setPredefines - Set the predefines for this Preprocessor.  These
447aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// predefines are automatically injected when parsing the main file.
448aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const char *P) { Predefines = P; }
449aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const std::string &P) { Predefines = P; }
4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getIdentifierInfo - Return information about the specified preprocessor
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier token.  The version of this method that takes two character
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pointers is preferred unless the identifier is already available as a
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// string (this avoids allocation and copying of memory to construct an
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// std::string).
456686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  IdentifierInfo *getIdentifierInfo(StringRef Name) const {
4573da736c1143126be19b253804b3b135ebcd3d6ffDaniel Dunbar    return &Identifiers.get(Name);
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If 'Namespace' is non-null, then it is a token required to exist on the
4625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
463686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
4649b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  void AddPragmaHandler(PragmaHandler *Handler) {
465686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    AddPragmaHandler(StringRef(), Handler);
4669b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  }
4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4684095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// RemovePragmaHandler - Remove the specific pragma handler from
4694095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// the preprocessor. If \arg Namespace is non-null, then it should
4704095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// be the namespace that \arg Handler was added to. It is an error
4714095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// to remove a handler that has not been registered.
472686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
4739b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  void RemovePragmaHandler(PragmaHandler *Handler) {
474686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    RemovePragmaHandler(StringRef(), Handler);
4759b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  }
4764095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar
4772e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// \brief Add the specified comment handler to the preprocessor.
4782e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  void AddCommentHandler(CommentHandler *Handler);
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// \brief Remove the specified comment handler.
4812e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  ///
4822e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// It is an error to remove a handler that has not been registered.
4832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  void RemoveCommentHandler(CommentHandler *Handler);
4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
485f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief Set the code completion handler to the given object.
486f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
487f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    CodeComplete = &Handler;
488f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
489ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4901fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  /// \brief Retrieve the current code-completion handler.
4911fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  CodeCompletionHandler *getCodeCompletionHandler() const {
4921fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    return CodeComplete;
4931fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  }
494ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
495f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief Clear out the code completion handler.
496f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  void clearCodeCompletionHandler() {
497f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    CodeComplete = 0;
498f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
499ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
50055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  /// \brief Hook used by the lexer to invoke the "natural language" code
50155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  /// completion point.
50255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  void CodeCompleteNaturalLanguage();
503ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
50494dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// \brief Retrieve the preprocessing record, or NULL if there is no
50594dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// preprocessing record.
506b9e1b75772db2c7db566c6034ba90a07f22e35ebDouglas Gregor  PreprocessingRecord *getPreprocessingRecord() const { return Record; }
507ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
508ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Create a new preprocessing record, which will keep track of
50994dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// all macro expansions, macro definitions, etc.
5109e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  void createPreprocessingRecord(bool IncludeNestedMacroExpansions);
511ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
51253b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
5137dcc968f17a6ff9088c9651dddccc8d4025a1271Ted Kremenek  /// which implicitly adds the builtin defines etc.
514e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  void EnterMainSourceFile();
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
516dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  /// EndSourceFile - Inform the preprocessor callbacks that processing is
517dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  /// complete.
518dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  void EndSourceFile();
519dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFile - Add a source file to the top of the include stack and
521e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  /// start lexing tokens from it instead of the current buffer.  Emit an error
522e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  /// and don't enter the file on error.
523e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
524e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                       SourceLocation Loc);
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens from it instead of the current buffer.  Args specifies the
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens input to a function-like macro.
529e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  ///
530e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// ILEnd specifies the location of the ')' for a function-like macro or the
531e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// identifier for an object-like macro.
532e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroArgs *Args);
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
5356b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// which will cause the lexer to start returning the specified tokens.
5366b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
5376b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
5386b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// not be subject to further macro expansion.  Otherwise, these tokens will
5396b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// be re-macro-expanded when/if expansion is enabled.
5406b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
5416b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If OwnsTokens is false, this method assumes that the specified stream of
5426b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// tokens has a permanent owner somewhere, so they do not need to be copied.
5436b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If it is true, it assumes the array of tokens is allocated with new[] and
5446b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// must be freed.
5456b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
5466b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  void EnterTokenStream(const Token *Toks, unsigned NumToks,
5476b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner                        bool DisableMacroExpansion, bool OwnsTokens);
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer stack.  This should only be used in situations where the current
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// state of the top-of-stack lexer is known.
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RemoveTopOfLexerStack();
55303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
55403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// EnableBacktrackAtThisPos - From the point that this method is called, and
555ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
556ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
557ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// make the Preprocessor re-lex the same tokens.
55803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
559a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
560ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
561ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
56203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
563ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
564ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
565ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// tokens will continue indefinitely.
56603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
567a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  void EnableBacktrackAtThisPos();
56803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
569ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
570ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  void CommitBacktrackedTokens();
57103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
57203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// EnableBacktrackAtThisPos() was previously called.
574a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  void Backtrack();
57503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
57603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
57703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// caching of tokens is on.
578fad03b2b38a3baea4b67e79e676fee15078e3258Argyrios Kyrtzidis  bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
57903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Lex - To lex a token from the preprocessor, just pull a token from the
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// current lexer or macro object.
582d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Lex(Token &Result) {
583b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    switch (CurLexerKind) {
584b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_Lexer: CurLexer->Lex(Result); break;
585b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_PTHLexer: CurPTHLexer->Lex(Result); break;
586b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_TokenLexer: CurTokenLexer->Lex(Result); break;
587b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_CachingLexer: CachingLex(Result); break;
588b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_LexAfterModuleImport: LexAfterModuleImport(Result); break;
589b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    }
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
592b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  void LexAfterModuleImport(Token &Result);
593ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// something not a comment.  This is useful in -E -C mode where comments
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// would foul up preprocessor directive handling.
597d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexNonComment(Token &Result) {
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    do
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Result);
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (Result.getKind() == tok::comment);
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
602d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek
6035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexUnexpandedToken - This is just like Lex, but this disables macro
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansion of identifier tokens.
605d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexUnexpandedToken(Token &Result) {
6065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Disable macro expansion.
6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    bool OldVal = DisableMacroExpansion;
6085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = true;
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Lex the token.
6105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Lex(Result);
6111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Reenable it.
6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = OldVal;
6145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61688710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
61788710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  /// expansion of identifier tokens.
61888710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  void LexUnexpandedNonComment(Token &Result) {
61988710f22be1acb04b7d812aebe87bea56394c07cEli Friedman    do
62088710f22be1acb04b7d812aebe87bea56394c07cEli Friedman      LexUnexpandedToken(Result);
62188710f22be1acb04b7d812aebe87bea56394c07cEli Friedman    while (Result.getKind() == tok::comment);
62288710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  }
62388710f22be1acb04b7d812aebe87bea56394c07cEli Friedman
6246b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// LookAhead - This peeks ahead N tokens and returns that token without
6256b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// consuming any tokens.  LookAhead(0) returns the next token that would be
6266b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
6276b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returns normal tokens after phase 5.  As such, it is equivalent to using
6286b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// 'Lex', not 'LexUnexpandedToken'.
62903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  const Token &LookAhead(unsigned N) {
63003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    if (CachedLexPos + N < CachedTokens.size())
63103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      return CachedTokens[CachedLexPos+N];
63203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    else
63303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      return PeekAhead(N+1);
6349e0ed0bd5a3a7bac73973980ff32132a7724e674Argyrios Kyrtzidis  }
6353604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis
63608b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// RevertCachedTokens - When backtracking is enabled and tokens are cached,
63708b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// this allows to revert a specific number of tokens.
63808b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// Note that the number of tokens being reverted should be up to the last
63908b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// backtrack position, not more.
64008b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  void RevertCachedTokens(unsigned N) {
64108b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(isBacktrackEnabled() &&
64208b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis           "Should only be called when tokens are cached for backtracking");
64308b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
64408b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis         && "Should revert tokens up to the last backtrack position, not more");
64508b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(signed(CachedLexPos) - signed(N) >= 0 &&
64608b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis           "Corrupted backtrack positions ?");
64708b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    CachedLexPos -= N;
64808b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  }
64908b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis
6503604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// EnterToken - Enters a token in the token stream to be lexed next. If
6513604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// BackTrack() is called afterwards, the token will remain at the insertion
6523604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// point.
6533604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void EnterToken(const Token &Tok) {
6543604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis    EnterCachingLexMode();
6553604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis    CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
6563604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  }
6573604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis
6583604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching
6593604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// tokens (because backtrack is enabled) it should replace the most recent
6603604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// cached tokens with the given annotation token. This function has no effect
6613604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// if backtracking is not enabled.
6623604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  ///
6633604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// Note that the use of this function is just for optimization; so that the
6643604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
6653604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// invoked.
6663604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void AnnotateCachedTokens(const Token &Tok) {
66747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner    assert(Tok.isAnnotation() && "Expected annotation token");
66883cf05a3b0e655dc8ea1cb4c4e1eef541b770992Chris Lattner    if (CachedLexPos != 0 && isBacktrackEnabled())
6693604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis      AnnotatePreviousCachedTokens(Tok);
6703604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  }
67139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Replace the last token with an annotation token.
67339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  ///
67439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// Like AnnotateCachedTokens(), this routine replaces an
67539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// already-parsed (and resolved) token with an annotation
67639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// token. However, this routine only replaces the last token with
67739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// the annotation token; it does not affect any other cached
67839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// tokens. This function has no effect if backtracking is not
67939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// enabled.
68039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  void ReplaceLastTokenWithAnnotation(const Token &Tok) {
68139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    assert(Tok.isAnnotation() && "Expected annotation token");
68239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (CachedLexPos != 0 && isBacktrackEnabled())
68339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      CachedTokens[CachedLexPos-1] = Tok;
68439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
68539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
686d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
687d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  /// CurTokenLexer pointers.
688d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  void recomputeCurLexerKind();
689d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor
6902968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Specify the point at which code-completion will be performed.
6912968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
6922968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param File the file in which code completion should occur. If
6932968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// this file is included multiple times, code-completion will
6942968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// perform completion the first time it is included. If NULL, this
6952968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// function clears out the code-completion point.
6962968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
6972968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Line the line at which code completion should occur
6982968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// (1-based).
6992968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7002968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Column the column at which code completion should occur
7012968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// (1-based).
7022968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7032968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \returns true if an error occurred, false otherwise.
704a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  bool SetCodeCompletionPoint(const FileEntry *File,
7052968442603b029949246467253eeac8139a5b6d8Douglas Gregor                              unsigned Line, unsigned Column);
7062968442603b029949246467253eeac8139a5b6d8Douglas Gregor
7073437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis  /// \brief Determine if we are performing code completion.
7083437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis  bool isCodeCompletionEnabled() const { return CodeCompletionFile != 0; }
7093437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis
7107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns the location of the code-completion point.
7117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// Returns an invalid location if code-completion is not enabled or the file
7127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// containing the code-completion point has not been lexed yet.
7137d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
7147d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7157d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns the start location of the file of code-completion point.
7167d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// Returns an invalid location if code-completion is not enabled or the file
7177d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// containing the code-completion point has not been lexed yet.
7187d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation getCodeCompletionFileLoc() const {
7197d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return CodeCompletionFileLoc;
7207d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
7217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7227d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns true if code-completion is enabled and we have hit the
7237d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// code-completion point.
7247d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool isCodeCompletionReached() const { return CodeCompletionReached; }
7257d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Note that we hit the code-completion point.
7277d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  void setCodeCompletionReached() {
7287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
7297d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    CodeCompletionReached = true;
7307d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    // Silence any diagnostics that occur after we hit the code-completion.
7317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    getDiagnostics().setSuppressAllDiagnostics(true);
7327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
7337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7348dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// \brief The location of the currently-active #pragma clang
7358dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// arc_cf_code_audited begin.  Returns an invalid location if there
7368dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// is no such pragma active.
7378dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation getPragmaARCCFCodeAuditedLoc() const {
7388dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    return PragmaARCCFCodeAuditedLoc;
7398dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  }
7408dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
7418dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// \brief Set the location of the currently-active #pragma clang
7428dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// arc_cf_code_audited begin.  An invalid location ends the pragma.
7438dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
7448dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    PragmaARCCFCodeAuditedLoc = Loc;
7458dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  }
7468dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
747f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Instruct the preprocessor to skip part of the main
748f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// the main source file.
749f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
750f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief Bytes The number of bytes in the preamble to skip.
751f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
752f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief StartOfLine Whether skipping these bytes puts the lexer at the
753f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// start of a line.
754ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
755f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    SkipMainFilePreamble.first = Bytes;
756f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    SkipMainFilePreamble.second = StartOfLine;
757f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  }
758ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
760d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  /// the specified Token's location, translating the token's start
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// position in the current buffer into a SourcePosition object for rendering.
76298d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
76333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->Report(Loc, DiagID);
764adc4eeb08042a35ae914fc557ffec0cef3df2374Chris Lattner  }
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76698d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
76733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    return Diags->Report(Tok.getLocation(), DiagID);
768adc4eeb08042a35ae914fc557ffec0cef3df2374Chris Lattner  }
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// getSpelling() - Return the 'spelling' of the token at the given
771834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// location; does not go up to the spelling location or down to the
7729e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// expansion location.
77330900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  ///
774834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// \param buffer A buffer which will be used only if the token requires
775834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
77630900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  /// \param invalid If non-null, will be set \c true if an error occurs.
777686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getSpelling(SourceLocation loc,
778686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                              SmallVectorImpl<char> &buffer,
779834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall                              bool *invalid = 0) const {
780834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall    return Lexer::getSpelling(loc, buffer, SourceMgr, Features, invalid);
781834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
78230900dafbe972f4802c2663262b4393edab0d1cdJohn McCall
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// token is the characters used to represent the token in the source file
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// after trigraph expansion and escaped-newline folding.  In particular, this
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// wants to get the true, uncanonicalized, spelling of things like digraphs
7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// UCNs, etc.
78850f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
78930900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  /// \param Invalid If non-null, will be set \c true if an error occurs.
790b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
791b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return Lexer::getSpelling(Tok, SourceMgr, Features, Invalid);
792b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
7930ff1042ddaad1419264be0de6da17f3b378482a4Daniel Dunbar
7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling - This method is used to get the spelling of a token into a
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// preallocated buffer, instead of as an std::string.  The caller is required
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to allocate enough space for the token, which is guaranteed to be at least
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Tok.getLength() bytes long.  The length of the actual result is returned.
7985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
7995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Note that this method may do two possible things: it may either fill in
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the buffer specified with characters, or it may *change the input pointer*
8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to point to a constant buffer with the data already in it (avoiding a
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// copy).  The caller is not allowed to modify the returned buffer pointer
8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// if an internal buffer is returned.
804ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  unsigned getSpelling(const Token &Tok, const char *&Buffer,
80548cf9824fbad42995f4d91d59d08d2620effd683Chris Lattner                       bool *Invalid = 0) const {
806b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner    return Lexer::getSpelling(Tok, Buffer, SourceMgr, Features, Invalid);
80748cf9824fbad42995f4d91d59d08d2620effd683Chris Lattner  }
808b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor
809ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// getSpelling - This method is used to get the spelling of a token into a
810ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// SmallVector. Note that the returned StringRef may not point to the
811ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// supplied buffer if a copy can be avoided.
812686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getSpelling(const Token &Tok,
813ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                              SmallVectorImpl<char> &Buffer,
81450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                              bool *Invalid = 0) const;
815ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer
8167216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner  /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
8177216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner  /// with length 1, return the character.
818ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  char getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
81950f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0) const {
8207216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    assert(Tok.is(tok::numeric_constant) &&
8217216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner           Tok.getLength() == 1 && "Called on unsupported token");
8227216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
8237216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner
8247216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // If the token is carrying a literal data pointer, just use it.
8257216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    if (const char *D = Tok.getLiteralData())
8267216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner      return *D;
82728396608ec20d44e9d1470e1ea51689bb504d0deTed Kremenek
8287216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // Otherwise, fall back on getCharacterData, which is slower, but always
8297216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // works.
830a543016fe07030f695d6d56fd22c8c8da617e0d7Douglas Gregor    return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
83128396608ec20d44e9d1470e1ea51689bb504d0deTed Kremenek  }
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
833c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// \brief Retrieve the name of the immediate macro expansion.
834c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  ///
835c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// This routine starts from a source location, and finds the name of the macro
836c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// responsible for its immediate expansion. It looks through any intervening
837c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// macro argument expansions to compute this. It returns a StringRef which
838c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// refers to the SourceManager-owned buffer of the source where that macro
839c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// name is spelled. Thus, the result shouldn't out-live the SourceManager.
840c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  StringRef getImmediateMacroName(SourceLocation Loc) {
841c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks    return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOptions());
842c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  }
843c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
84447246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  /// CreateString - Plop the specified string into a scratch buffer and set the
84547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  /// specified token's location and length to it.  If specified, the source
8469e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// location provides a location of the expansion point of the token.
847a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara  void CreateString(const char *Buf, unsigned Len, Token &Tok,
848a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                    SourceLocation ExpansionLocStart = SourceLocation(),
849a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                    SourceLocation ExpansionLocEnd = SourceLocation());
850b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor
851b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// \brief Computes the source location just past the end of the
852b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// token at this source location.
853b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  ///
854b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// This routine can be used to produce a source location that
855b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// points just past the end of the token referenced by \p Loc, and
856b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// is generally used when a diagnostic needs to point just after a
857b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// token where it expected something different that it received. If
858b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// the returned source location would not be meaningful (e.g., if
859b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// it points into a macro), this routine returns an invalid
860b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// source location.
86133e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  ///
86233e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// \param Offset an offset from the end of the token, where the source
86333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// location should refer to. The default offset (0) produces a source
86433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// location pointing just past the end of the token; an offset of 1 produces
86533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// a source location pointing to the last character in the token, etc.
8667ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
8677ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, Features);
8687ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8707a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the first
8719e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// token of the macro expansion.
87269bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
87369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// \param MacroBegin If non-null and function returns true, it is set to
87469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// begin location of the macro.
87569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  bool isAtStartOfMacroExpansion(SourceLocation loc,
87669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                 SourceLocation *MacroBegin = 0) const {
87769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, Features,
87869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                            MacroBegin);
8797a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  }
8807a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
8817a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the last
8829e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// token of the macro expansion.
88369bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
88469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// \param MacroBegin If non-null and function returns true, it is set to
88569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// end location of the macro.
88669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  bool isAtEndOfMacroExpansion(SourceLocation loc,
88769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                               SourceLocation *MacroEnd = 0) const {
88869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis    return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, Features, MacroEnd);
8897a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  }
8907a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// DumpToken - Print the token to stderr, used for debugging.
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
893d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
894c3d8d57b010e2ed15a2a7685d5761db14f5d2252Chris Lattner  void DumpLocation(SourceLocation Loc) const;
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void DumpMacro(const MacroInfo &MI) const;
8961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89797ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
89897ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// token, return a new location that specifies a character within the token.
899872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
900872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner                                         unsigned Char) const {
9017ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner    return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, Features);
902872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner  }
903872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner
9045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncrementPasteCounter - Increment the counters for the number of token
9055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// paste operations performed.  If fast was specified, this is a 'fast paste'
9065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// case we handled.
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
9085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void IncrementPasteCounter(bool isFast) {
9095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (isFast)
9105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumFastTokenPaste;
9115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else
9125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumTokenPaste;
9135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats();
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
917c5c5e92ec53f7e6ac7ebbbf77c6d8e4b7d88daecArgyrios Kyrtzidis  size_t getTotalMemory() const;
918c5c5e92ec53f7e6ac7ebbbf77c6d8e4b7d88daecArgyrios Kyrtzidis
9193f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
9203f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// comment (/##/) in microsoft mode, this method handles updating the current
9213f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// state, returning the token on the next source line.
9223f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  void HandleMicrosoftCommentPaste(Token &Tok);
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
9255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Preprocessor callback methods.  These are invoked by a lexer as various
9265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // directives and events are found.
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
928c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
929c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// identifier information for the token and install it into the token,
930c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// updating the token kind accordingly.
931c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
9321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
93328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
93428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
93528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
93628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
93728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
93828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // SetPoisonReason - Call this function to indicate the reason for
93928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // poisoning an identifier. If that identifier is accessed while
94028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // poisoned, then this reason will be used instead of the default
94128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // "poisoned" diagnostic.
94228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
94328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
94428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // HandlePoisonedIdentifier - Display reason for poisoned
94528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // identifier.
94628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void HandlePoisonedIdentifier(Token & Tok);
94728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
94828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void MaybeHandlePoisonedIdentifier(Token & Identifier) {
94928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
95028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      if(II->isPoisoned()) {
95128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        HandlePoisonedIdentifier(Identifier);
95228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
95328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
95428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
95528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
95628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
95728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  /// Identifiers used for SEH handling in Borland. These are only
95828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  /// allowed in particular circumstances
959ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __except block
960ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__exception_code,
961ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___exception_code,
962ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_GetExceptionCode;
963ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __except filter expression
964ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__exception_info,
965ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___exception_info,
966ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_GetExceptionInfo;
967ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __finally
968ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__abnormal_termination,
969ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___abnormal_termination,
970ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_AbnormalTermination;
97128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
97228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void PoisonSEHIdentifiers(bool Poison = true); // Borland
97328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleIdentifier - This callback is invoked when the lexer reads an
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier and has filled in the tokens IdentifierInfo member.  This
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// callback potentially macro expands it or turns it into a named token (like
9775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// 'for').
978d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentifier(Token &Identifier);
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the current file.  This either returns the EOF token and returns true, or
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pops a level off the include stack and returns false, at which point the
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// client should call lex again.
985d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
987fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// HandleEndOfTokenLexer - This callback is invoked when the current
988fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// TokenLexer hits the end of its token stream.
989fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  bool HandleEndOfTokenLexer(Token &Result);
9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleDirective - This callback is invoked when the lexer sees a # token
9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// at the start of a line.  This consumes the directive, modifies the
9935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
9945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// read is the correct one.
995d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleDirective(Token &Result);
9965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
99784021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// CheckEndOfDirective - Ensure that the next token is a tok::eod token.  If
99884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// not, emit a diagnostic and consume up until the eod.  If EnableMacros is
999ab82f41b217ce588a9456c0b4411f219d3ed0df8Chris Lattner  /// true, then we consider macros that expand to zero tokens as being ok.
1000ab82f41b217ce588a9456c0b4411f219d3ed0df8Chris Lattner  void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1002478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
100384021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// current line until the tok::eod token is found.
1004478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  void DiscardUntilEndOfDirective();
10051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10067c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  /// SawDateOrTime - This returns true if the preprocessor has seen a use of
10077c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  /// __DATE__ or __TIME__ in the file so far.
10087c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  bool SawDateOrTime() const {
10097c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
10107c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  }
1011c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  unsigned getCounterValue() const { return CounterValue; }
1012c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  void setCounterValue(unsigned V) { CounterValue = V; }
10131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
101490db26000aefe9335370013eec64c85232d80227Douglas Gregor  /// \brief Retrieves the module that we're currently building, if any.
101590db26000aefe9335370013eec64c85232d80227Douglas Gregor  Module *getCurrentModule();
101690db26000aefe9335370013eec64c85232d80227Douglas Gregor
1017f908c519c1625e81d9e33d8a2a306a92834fe317Chris Lattner  /// AllocateMacroInfo - Allocate a new MacroInfo object with the provide
1018f908c519c1625e81d9e33d8a2a306a92834fe317Chris Lattner  ///  SourceLocation.
1019f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *AllocateMacroInfo(SourceLocation L);
1020f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
1021f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  /// CloneMacroInfo - Allocate a new MacroInfo object which is clone of MI.
1022f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *CloneMacroInfo(const MacroInfo &MI);
10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1024a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1025a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// checked and spelled filename, e.g. as an operand of #include. This returns
1026a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// true if the input filename was in <>'s or false if it were in ""'s.  The
1027a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// caller is expected to provide a buffer that is large enough to hold the
1028a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// spelling of the filename, but is also expected to handle the case when
1029a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// this method decides to use a different buffer.
1030686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1031a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
1032a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
1033a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// return null on failure.  isAngled indicates whether the file reference is
1034a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// for system #include's or not (i.e. using <> instead of "").
1035686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  const FileEntry *LookupFile(StringRef Filename,
1036f45b646244705410866d62f1d8bf017a047ed662Chris Lattner                              bool isAngled, const DirectoryLookup *FromDir,
1037b5142bb7af5c70fffd09f05172a1379a35a9c29aChandler Carruth                              const DirectoryLookup *&CurDir,
1038686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                              SmallVectorImpl<char> *SearchPath,
1039fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor                              SmallVectorImpl<char> *RelativePath,
10401a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor                              Module **SuggestedModule,
10411c2e9332fa69727425a3a2b912e36e2ab62083f8Douglas Gregor                              bool SkipCache = false);
1042a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
1043a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// GetCurLookup - The DirectoryLookup structure used to find the current
1044a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
1045a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// implement #include_next and find directory-specific properties.
1046a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1047a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
1048a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// isInPrimaryFile - Return true if we're in the top-level file, not in a
1049a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// #include.
1050a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  bool isInPrimaryFile() const;
1051a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
1052a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// ConcatenateIncludeName - Handle cases where the #include name is expanded
1053a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// from a macro as multiple tokens, which need to be glued together.  This
1054a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// occurs for code like:
1055a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  ///    #define FOO <a/b.h>
1056a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  ///    #include FOO
1057a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1058a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  ///
1059321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// This code concatenates and consumes tokens up to the '>' token.  It
1060321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// returns false if the > was found, otherwise it returns true if it finds
106184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// and consumes the EOD marker.
1062ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  bool ConcatenateIncludeName(llvm::SmallString<128> &FilenameBuffer,
1063ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor                              SourceLocation &End);
1064a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
10659d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne  /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is
106684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// followed by EOD.  Return true if the token is not a valid on-off-switch.
10679d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne  bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
10689d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne
10695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1071d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  void PushIncludeMacroStack() {
1072b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind,
1073b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor                                                 CurLexer.take(),
10749c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurPTHLexer.take(),
10759c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurPPLexer,
10769c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurTokenLexer.take(),
10779c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurDirLookup));
1078f89963b1e819d5e73796a4f9d7cd70129846e2ecTed Kremenek    CurPPLexer = 0;
1079d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  }
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1081d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  void PopIncludeMacroStack() {
1082caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek    CurLexer.reset(IncludeMacroStack.back().TheLexer);
10839c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer);
10849c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1085caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek    CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
10869c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1087b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1088d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek    IncludeMacroStack.pop_back();
1089d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  }
10901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1091f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  /// AllocateMacroInfo - Allocate a new MacroInfo object.
1092f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *AllocateMacroInfo();
1093f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
10940ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  /// ReleaseMacroInfo - Release the specified MacroInfo.  This memory will
10950ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  ///  be reused for allocating new MacroInfo objects.
10960301b3ff132a4d986c092d161cb77d74b04cd2a6Chris Lattner  void ReleaseMacroInfo(MacroInfo* MI);
10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroName - Lex and validate a macro name, which occurs after a
109984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// #define or #undef.  This emits a diagnostic, sets the token kind to eod,
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// and discards the rest of the macro line if the macro name is invalid.
1101d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// definition has just been read.  Lex the rest of the arguments and the
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// closing ), updating MI with what we learn.  Return true if an error occurs
11065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// parsing the arg list.
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool ReadMacroDefinitionArgList(MacroInfo *MI);
11081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// SkipExcludedConditionalBlock - We just read a #if or related directive and
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// decided that the subsequent tokens are in the #if'd out portion of the
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// file.  Lex the rest of the file, until we see an #endif.  If
11125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FoundNonSkipPortion is true, then we have already emitted code for part of
11135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this #if directive, so #else/#elif blocks should never be entered. If
11145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FoundElse is false, then #else directives are ok, if not, then we have
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// already seen one so a #else directive is a duplicate.  When this returns,
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the caller can lex the first valid token.
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
11186b4ff04dd22e7159887ea6c1bb8b1c01fc9c70a9Argyrios Kyrtzidis                                    bool FoundNonSkipPortion, bool FoundElse,
11196b4ff04dd22e7159887ea6c1bb8b1c01fc9c70a9Argyrios Kyrtzidis                                    SourceLocation ElseLoc = SourceLocation());
11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1121268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  /// PTHSkipExcludedConditionalBlock - A fast PTH version of
1122268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  ///  SkipExcludedConditionalBlock.
1123268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  void PTHSkipExcludedConditionalBlock();
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
11265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// may occur after a #if or #elif directive and return it as a bool.  If the
11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// #pragma GCC poison/system_header/dependency and #pragma once.
11325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinPragmas();
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
11355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier table.
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinMacros();
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
11395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
11405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the macro should not be expanded return true, otherwise return false.
1141d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11435b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// \brief Cache macro expanded tokens for TokenLexers.
11445b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  //
11455b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
11465b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// going to lex in the cache and when it finishes the tokens are removed
11475b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// from the end of the cache.
11485b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
11492d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                                  ArrayRef<Token> tokens);
11505b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  void removeCachedMacroExpandedTokensOfLastLexer();
11515b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  friend void TokenLexer::ExpandFunctionArguments();
11525b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis
11535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
11545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexed is a '('.  If so, consume the token and return true, if not, this
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// method should have no observable side-effect on the lexed tokens.
11565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isNextPPTokenLParen();
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
11595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invoked to read all of the formal arguments specified for the macro
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invocation.  This returns null on error.
1161e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
11629e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth                                       SourceLocation &ExpansionEnd);
11635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
11655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// as a builtin macro, handle it and return the next token as 'Tok'.
1166d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ExpandBuiltinMacro(Token &Tok);
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// return the first token after the directive.  The _Pragma token has just
11705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// been read into 'Tok'.
1171d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Handle_Pragma(Token &Tok);
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11731ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
11741ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  /// is not enclosed within a string literal.
11751ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  void HandleMicrosoft__pragma(Token &Tok);
11761ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall
11775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
11785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// start lexing tokens from it instead of the current buffer.
11795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
11806137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek
11816137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and
11826137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// start getting tokens from it using the PTH cache.
11836137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118534eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  /// IsFileLexer - Returns true if we are lexing from a file and not a
118634eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  ///  pragma or a macro.
118734eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
118834eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek    return L ? !L->isPragmaLexer() : P != 0;
118941938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
119041938c8493b4380df738263166b746eacb33c309Ted Kremenek
119181d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek  static bool IsFileLexer(const IncludeStackInfo& I) {
119281d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek    return IsFileLexer(I.TheLexer, I.ThePPLexer);
119341938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
119441938c8493b4380df738263166b746eacb33c309Ted Kremenek
119581d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek  bool IsFileLexer() const {
119681d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek    return IsFileLexer(CurLexer.get(), CurPPLexer);
119741938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
120003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  // Caching stuff.
120103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void CachingLex(Token &Result);
120203569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis  bool InCachingLexMode() const {
120303569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis    // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
120403569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis    // that we are past EOF, not that we are in CachingLex mode.
1205ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 &&
120656eb1ec6e54080e47bbc62412737c25afb5211edTed Kremenek           !IncludeMacroStack.empty();
120703569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis  }
120803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void EnterCachingLexMode();
120903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void ExitCachingLexMode() {
121003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    if (InCachingLexMode())
121103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      RemoveTopOfLexerStack();
121203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  }
121303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  const Token &PeekAhead(unsigned N);
12143604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void AnnotatePreviousCachedTokens(const Token &Tok);
121503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
121603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle*Directive - implement the various preprocessor directives.  These
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// should side-effect the current preprocessor object so that the next call
12195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to Lex() will return the appropriate token next.
1220359cc4475487ea0a660b3e8918334a2866e71c66Chris Lattner  void HandleLineDirective(Token &Tok);
1221478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  void HandleDigitDirective(Token &Tok);
1222d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1223d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentSCCSDirective(Token &Tok);
122494ad28b31433058445a27db722f60402ee820beaDouglas Gregor  void HandleMacroPublicDirective(Token &Tok);
1225aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor  void HandleMacroPrivateDirective(Token &Tok);
1226ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // File inclusion.
1228ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeDirective(SourceLocation HashLoc,
1229ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor                              Token &Tok,
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const DirectoryLookup *LookupFrom = 0,
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              bool isImport = false);
1232ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1233ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1234ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Macro handling.
123742e6737f2efb113563140ad794c21c7709250402Chris Lattner  void HandleDefineDirective(Token &Tok);
1238d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUndefDirective(Token &Tok);
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Conditional Inclusion.
1241d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfdefDirective(Token &Tok, bool isIfndef,
12425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                            bool ReadAnyTokensBeforeDirective);
1243d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1244d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleEndifDirective(Token &Tok);
1245d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElseDirective(Token &Tok);
1246d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElifDirective(Token &Tok);
12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Pragmas.
124980c60f72848896f867f6b7e664e7060d9e78f019Douglas Gregor  void HandlePragmaDirective(unsigned Introducer);
12505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1251d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaOnce(Token &OnceTok);
12522243449253475574fc6f14986ff8f7fce5d46799Chris Lattner  void HandlePragmaMark();
1253d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaPoison(Token &PoisonTok);
1254d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaSystemHeader(Token &SysHeaderTok);
1255d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaDependency(Token &DependencyTok);
1256636c5ef6572e899d36cec1b0023fb28ba65189e1Chris Lattner  void HandlePragmaComment(Token &CommentTok);
1257abfe094ce71c42656dcb84a3bdc3e79cb3c16fc3Chris Lattner  void HandlePragmaMessage(Token &MessageTok);
1258f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  void HandlePragmaPushMacro(Token &Tok);
1259f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  void HandlePragmaPopMacro(Token &Tok);
1260f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1261f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
1262046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // Return true and store the first token only if any CommentHandler
1263046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // has inserted some tokens and getCommentRetentionState() is false.
1264046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  bool HandleComment(Token &Token, SourceRange Comment);
12650827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
12660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief A macro is used, update information about macros that need unused
12670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// warnings.
12680827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void markMacroAsUsed(MacroInfo *MI);
12695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
12705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Abstract base class that describes a handler that will receive
12722e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// source ranges for each of the comments encountered in the source file.
12732e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorclass CommentHandler {
12742e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorpublic:
12752e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  virtual ~CommentHandler();
12761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1277046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // The handler shall return true if it has pushed any tokens
1278046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // to be read using e.g. EnterToken or EnterTokenStream.
1279046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
12802e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor};
12811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1285