Preprocessor.h revision 374b3837d676133fcc1eb70a25c8baf8ec4a5c4a
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"
24d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer#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
368fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramernamespace llvm {
378fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer  template<unsigned InternalLen> class SmallString;
388fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer}
398fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
4388a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregorclass ExternalPreprocessorSource;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileManager;
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass HeaderSearch;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaNamespace;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaHandler;
492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorclass CommentHandler;
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass ScratchBuffer;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass TargetInfo;
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PPCallbacks;
53f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorclass CodeCompletionHandler;
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass DirectoryLookup;
5594dc8f640ebea52241412512ed48601626edbc58Douglas Gregorclass PreprocessingRecord;
566aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ModuleLoader;
57ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
58e5956bd2730c051835f9acd9e957c5d79f99e7c3Chris Lattner/// Preprocessor - This object engages in a tight little dance with the lexer to
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// efficiently preprocess tokens.  Lexers know only about tokens within a
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// single source file, and don't know anything about preprocessor-level issues
6158bf98725b6d4588338e191d2ab981b104471dabJames Dennett/// like the \#include stack, token expansion, etc.
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
63c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass Preprocessor : public RefCountedBase<Preprocessor> {
64d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  DiagnosticsEngine        *Diags;
654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  LangOptions       &LangOpts;
66998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  const TargetInfo  *Target;
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager       &FileMgr;
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager     &SourceMgr;
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ScratchBuffer     *ScratchBuf;
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch      &HeaderInfo;
716aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ModuleLoader      &TheModuleLoader;
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7388a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  /// \brief External source of macros.
7488a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  ExternalPreprocessorSource *ExternalSource;
75a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
76ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
776137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// PTH - An optional PTHManager object used for getting tokens from
786137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  ///  a token cache rather than lexing the original source file.
796f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<PTHManager> PTH;
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
810ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  /// BP - A BumpPtrAllocator object used to quickly allocate and release
820ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  ///  objects internal to the Preprocessor.
830ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  llvm::BumpPtrAllocator BP;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers for builtin macros and other builtins.
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
91c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
921ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  IdentifierInfo *Ident_Pragma, *Ident__pragma;    // _Pragma, __pragma
931ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
94148772a841cae6f32db16d890e788b92a763bb3fChris Lattner  IdentifierInfo *Ident__has_feature;              // __has_feature
95c1b5fa41f09512c74030b9a2a0d1564535e22a76Peter Collingbourne  IdentifierInfo *Ident__has_extension;            // __has_extension
96148772a841cae6f32db16d890e788b92a763bb3fChris Lattner  IdentifierInfo *Ident__has_builtin;              // __has_builtin
97cae5095c116c29a4e52c91fa0ad88df09dd2b45fAnders Carlsson  IdentifierInfo *Ident__has_attribute;            // __has_attribute
9892bd8c70a6837b647a6c55964f8d0a50bf561dbcJohn Thompson  IdentifierInfo *Ident__has_include;              // __has_include
9992bd8c70a6837b647a6c55964f8d0a50bf561dbcJohn Thompson  IdentifierInfo *Ident__has_include_next;         // __has_include_next
100d768150ef57f617c8d9fef48f3c92e8f21698024Ted Kremenek  IdentifierInfo *Ident__has_warning;              // __has_warning
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation DATELoc, TIMELoc;
103c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  unsigned CounterValue;  // Next __COUNTER__ value.
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum {
10658bf98725b6d4588338e191d2ab981b104471dabJames Dennett    /// MaxIncludeStackDepth - Maximum depth of \#includes.
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MaxAllowedIncludeStackDepth = 200
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that is set before the preprocessor begins.
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepComments : 1;
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepMacroComments : 1;
113f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  bool SuppressIncludeNotFoundError : 1;
114ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that changes while the preprocessor runs:
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1185814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar  /// Whether the preprocessor owns the header search object.
1195814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar  bool OwnsHeaderSearch : 1;
1205814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar
1211d9c54df56391ac4740db27d551782e81189cb51Chris Lattner  /// DisableMacroExpansion - True if macro expansion is disabled.
1221d9c54df56391ac4740db27d551782e81189cb51Chris Lattner  bool DisableMacroExpansion : 1;
1231d9c54df56391ac4740db27d551782e81189cb51Chris Lattner
1248c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// MacroExpansionInDirectivesOverride - Temporarily disables
1258c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// DisableMacroExpansion (i.e. enables expansion) when parsing preprocessor
1268c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// directives.
1278c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  bool MacroExpansionInDirectivesOverride : 1;
1288c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie
1298c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  class ResetMacroExpansionHelper;
1308c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie
13188a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  /// \brief Whether we have already loaded macros from the external source.
13288a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  mutable bool ReadMacrosFromExternalSource : 1;
133a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
1346fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  /// \brief True if pragmas are enabled.
1356fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  bool PragmasEnabled : 1;
1366fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose
13714e645557ae91c6770d62beb00a1c522e0bfd5d6Argyrios Kyrtzidis  /// \brief True if we are pre-expanding macro arguments.
13814e645557ae91c6770d62beb00a1c522e0bfd5d6Argyrios Kyrtzidis  bool InMacroArgPreExpansion;
13914e645557ae91c6770d62beb00a1c522e0bfd5d6Argyrios Kyrtzidis
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers - This is mapping/lookup information for all identifiers in
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the program, including program keywords.
142c3222091e1ffa35d0264ca6b680a88c9dc84ede2Daniel Dunbar  mutable IdentifierTable Identifiers;
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// Selectors - This table contains all the selectors in the program. Unlike
14568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
1469e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// It is declared/expanded here because it's role/lifetime is
14768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// conceptually similar the IdentifierTable. In addition, the current control
1481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// flow (in clang::ParseAST()), make it convenient to put here.
14968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
150083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  /// the lifetime of the preprocessor.
15129238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable Selectors;
1522e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
1531b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  /// BuiltinInfo - Information about builtins.
1541b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  Builtin::Context BuiltinInfo;
1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PragmaHandlers - This tracks all of the pragmas that the client registered
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// with this preprocessor.
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PragmaNamespace *PragmaHandlers;
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Tracks all of the comment handlers that the client registered
1612e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// with this preprocessor.
1622e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<CommentHandler *> CommentHandlers;
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
164e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// \brief True if we want to ignore EOF token and continue later on (thus
165e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// avoid tearing the Lexer and etc. down).
166e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  bool IncrementalProcessing;
167e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
168f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief The code-completion handler.
169f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  CodeCompletionHandler *CodeComplete;
170ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1712968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief The file that we're performing code-completion for, if any.
1722968442603b029949246467253eeac8139a5b6d8Douglas Gregor  const FileEntry *CodeCompletionFile;
1732968442603b029949246467253eeac8139a5b6d8Douglas Gregor
1747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The offset in file for the code-completion point.
1757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  unsigned CodeCompletionOffset;
1767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The location for the code-completion point. This gets instantiated
17858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// when the CodeCompletionFile gets \#include'ed for preprocessing.
1797d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation CodeCompletionLoc;
1807d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
1817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The start location for the file of the code-completion point.
18258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
18358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// This gets instantiated when the CodeCompletionFile gets \#include'ed
1847d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// for preprocessing.
1857d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation CodeCompletionFileLoc;
1867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
187c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor  /// \brief The source location of the 'import' contextual keyword we just
188c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor  /// lexed, if any.
189b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  SourceLocation ModuleImportLoc;
1908dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
191b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// \brief The module import path that we're currently processing.
192b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2>
193b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor    ModuleImportPath;
194b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor
195b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// \brief Whether the module import expectes an identifier next. Otherwise,
196b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// it expects a '.' or ';'.
197b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  bool ModuleImportExpectsIdentifier;
198b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor
1998dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// \brief The source location of the currently-active
2008dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// #pragma clang arc_cf_code_audited begin.
2018dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation PragmaARCCFCodeAuditedLoc;
202ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief True if we hit the code-completion point.
2047d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool CodeCompletionReached;
2057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
206f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief The number of bytes that we will initially skip when entering the
207f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// main file, which is used when loading a precompiled preamble, along
208f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// with a flag that indicates whether skipping this number of bytes will
209f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// place the lexer at the start of a line.
210f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<unsigned, bool> SkipMainFilePreamble;
211ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLexer - This is the current top of the stack that we're lexing from if
2139c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// not expanding a macro and we are lexing directly from source code.
2149c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
2156f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<Lexer> CurLexer;
2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2179c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// CurPTHLexer - This is the current top of stack that we're lexing from if
2189c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  not expanding from a macro and we are lexing from a PTH cache.
2199c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
2206f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<PTHLexer> CurPTHLexer;
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2229c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// CurPPLexer - This is the current top of the stack what we're lexing from
2239c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  if not expanding a macro.  This is an alias for either CurLexer or
2249c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  CurPTHLexer.
22523f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  PreprocessorLexer *CurPPLexer;
226ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLookup - The DirectoryLookup structure used to find the current
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
22958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// implement \#include_next and find directory-specific properties.
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const DirectoryLookup *CurDirLookup;
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2326cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurTokenLexer - This is the current macro we are expanding, if we are
2336cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
2346f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<TokenLexer> CurTokenLexer;
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
236b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  /// \brief The kind of lexer we're currently working with.
237ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  enum CurLexerKind {
238ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_Lexer,
239ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_PTHLexer,
240ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_TokenLexer,
241b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CLK_CachingLexer,
242b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CLK_LexAfterModuleImport
243b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  } CurLexerKind;
244b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncludeMacroStack - This keeps track of the stack of files currently
24658bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \#included, and macros currently being expanded from, not counting
2476cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurLexer/CurTokenLexer.
2485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  struct IncludeStackInfo {
249b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    enum CurLexerKind     CurLexerKind;
2509c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    Lexer                 *TheLexer;
2519c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    PTHLexer              *ThePTHLexer;
2529c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    PreprocessorLexer     *ThePPLexer;
2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TokenLexer            *TheTokenLexer;
2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const DirectoryLookup *TheDirLookup;
2559c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek
256ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P,
257b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor                     PreprocessorLexer* PPL,
2589c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                     TokenLexer* TL, const DirectoryLookup *D)
259ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL),
260b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor        TheTokenLexer(TL), TheDirLookup(D) {}
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::vector<IncludeStackInfo> IncludeMacroStack;
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Callbacks - These are actions invoked when some preprocessor activity is
26558bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// encountered (e.g. a file is \#included, etc).
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *Callbacks;
2671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26866c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis  struct MacroExpandsInfo {
26966c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis    Token Tok;
27066c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis    MacroInfo *MI;
27166c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis    SourceRange Range;
27266c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis    MacroExpandsInfo(Token Tok, MacroInfo *MI, SourceRange Range)
27366c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis      : Tok(Tok), MI(MI), Range(Range) { }
27466c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis  };
27566c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis  SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
27666c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis
2778a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// Macros - For each IdentifierInfo that was associated with a macro, we
2788a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// keep a mapping to the history of all macro definitions and #undefs in
2798a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// the reverse order (the latest one is in the head of the list).
280cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
281c12906ee78e006df6a71acf52ca9dd3171ddbf07Douglas Gregor  friend class ASTReader;
282c12906ee78e006df6a71acf52ca9dd3171ddbf07Douglas Gregor
2830827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Macros that we want to warn because they are not used at the end
2840827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// of the translation unit; we store just their SourceLocations instead
2850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// something like MacroInfo*. The benefit of this is that when we are
2860827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// deserializing from PCH, we don't need to deserialize identifier & macros
2870827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// just so that we can report that they are unused, we just warn using
2880827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the SourceLocations of this set (that will be filled by the ASTReader).
2890827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// We are using SmallPtrSet instead of a vector for faster removal.
2900827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
2910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
2920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
29323f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  /// MacroArgCache - This is a "freelist" of MacroArg objects that can be
29423f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  /// reused for quick allocation.
29523f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  MacroArgs *MacroArgCache;
29623f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  friend class MacroArgs;
297ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
298ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma
299ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// push_macro directive, we keep a MacroInfo stack used to restore
300f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  /// previous macro value.
301f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
3021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Various statistics we track for performance analysis.
3045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumIf, NumElse, NumEndif;
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
3075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumSkipped;
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// Predefines - This string is the predefined macros that preprocessor
312aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// should use from the command line etc.
313aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  std::string Predefines;
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3156cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
3166cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  enum { TokenLexerCacheSize = 8 };
3176cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  unsigned NumCachedTokenLexers;
3186cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
3199e0ed0bd5a3a7bac73973980ff32132a7724e674Argyrios Kyrtzidis
3205b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// \brief Keeps macro expanded tokens for TokenLexers.
3215b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  //
3225b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
3235b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// going to lex in the cache and when it finishes the tokens are removed
3245b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// from the end of the cache.
325686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<Token, 16> MacroExpandedTokens;
3265b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
3275b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis
3289e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// \brief A record of the macro definitions and expansions that
329ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// occurred during preprocessing.
33094dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  ///
33194dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// This is an optional side structure that can be enabled with
33294dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// \c createPreprocessingRecord() prior to preprocessing.
333b9e1b75772db2c7db566c6034ba90a07f22e35ebDouglas Gregor  PreprocessingRecord *Record;
334ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
335e671e1bc73615eda155059a772266ed2882d758cChris Lattnerprivate:  // Cached tokens state.
336686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  typedef SmallVector<Token, 1> CachedTokensTy;
33703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
33803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// CachedTokens - Cached tokens are stored here when we do backtracking or
33903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// lookahead. They are "lexed" by the CachingLex() method.
34003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  CachedTokensTy CachedTokens;
34103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
34203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// CachedLexPos - The position of the cached token that CachingLex() should
34303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// "lex" next. If it points beyond the CachedTokens vector, it means that
34403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// a normal Lex() should be invoked.
34503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  CachedTokensTy::size_type CachedLexPos;
34603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
347a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// BacktrackPositions - Stack of backtrack positions, allowing nested
348a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
349a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// indicate where CachedLexPos should be set when the BackTrack() method is
350a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// invoked (at which point the last position is popped).
351a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  std::vector<CachedTokensTy::size_type> BacktrackPositions;
35203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
353af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  struct MacroInfoChain {
354af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek    MacroInfo MI;
355af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek    MacroInfoChain *Next;
3569714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek    MacroInfoChain *Prev;
357af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  };
358af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek
359af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  /// MacroInfos are managed as a chain for easy disposal.  This is the head
360af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  /// of that list.
361af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  MacroInfoChain *MIChainHead;
362af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek
3639714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
3649714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  /// allocation.
3659714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  MacroInfoChain *MICache;
3669714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek
367295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor  MacroInfo *getInfoForMacro(IdentifierInfo *II) const;
368ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
370d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
371998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               const TargetInfo *target,
37272b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek               SourceManager &SM, HeaderSearch &Headers,
3736aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor               ModuleLoader &TheModuleLoader,
3745814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar               IdentifierInfoLookup *IILookup = 0,
375998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               bool OwnsHeaderSearch = false,
376e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann               bool DelayInitialization = false,
377e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann               bool IncrProcessing = false);
37872b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ~Preprocessor();
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
381998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// \brief Initialize the preprocessor, if the constructor did not already
382998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// perform the initialization.
383998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  ///
384998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// \param Target Information about the target.
385998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  void Initialize(const TargetInfo &Target);
386ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
387d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return *Diags; }
388d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
3897c175fb196a2bc3dbc86ea3865c713e1875f3f6dChris Lattner
3904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &getLangOpts() const { return LangOpts; }
391998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  const TargetInfo &getTargetInfo() const { return *Target; }
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager &getFileManager() const { return FileMgr; }
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager &getSourceManager() const { return SourceMgr; }
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierTable &getIdentifierTable() { return Identifiers; }
39729238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable &getSelectorTable() { return Selectors; }
3981b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
39932fca722dd974b8202d0fb9c71b6c185c0767da6Chris Lattner  llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
401337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  void setPTHManager(PTHManager* pm);
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
403880dcf21dfdb3ff763c60195b6794bab0d913095Ted Kremenek  PTHManager *getPTHManager() { return PTH.get(); }
4042e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
40588a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  void setExternalSource(ExternalPreprocessorSource *Source) {
40688a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor    ExternalSource = Source;
40788a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  }
408a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
40988a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  ExternalPreprocessorSource *getExternalSource() const {
41088a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor    return ExternalSource;
41188a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  }
412a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
4136aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  /// \brief Retrieve the module loader associated with this preprocessor.
4146aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
415ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// SetCommentRetentionState - Control whether or not the preprocessor retains
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// comments in output.
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepComments = KeepComments | KeepMacroComments;
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepMacroComments = KeepMacroComments;
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getCommentRetentionState() const { return KeepComments; }
4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4256fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
4266fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  bool getPragmasEnabled() const { return PragmasEnabled; }
4276fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose
428f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  void SetSuppressIncludeNotFoundError(bool Suppress) {
429f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman    SuppressIncludeNotFoundError = Suppress;
430f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  }
431f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman
432f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  bool GetSuppressIncludeNotFoundError() {
433f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman    return SuppressIncludeNotFoundError;
434f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  }
435f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isCurrentLexer - Return true if we are lexing directly from the specified
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer.
4387062d9e9fc3ed89c4e4ea7055efea6585b7bac8dTed Kremenek  bool isCurrentLexer(const PreprocessorLexer *L) const {
4397062d9e9fc3ed89c4e4ea7055efea6585b7bac8dTed Kremenek    return CurPPLexer == L;
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
442a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// getCurrentLexer - Return the current lexer being lexed from.  Note
443a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// that this ignores any potentially active macro expansions and _Pragma
444a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// expansions going on at the time.
445a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
446a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
447321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// getCurrentFileLexer - Return the current file lexer being lexed from.
448321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// Note that this ignores any potentially active macro expansions and _Pragma
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansions going on at the time.
45068e48e4a81767997ef0231e47eca4f665102c95eTed Kremenek  PreprocessorLexer *getCurrentFileLexer() const;
4511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
452a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  /// getPPCallbacks/addPPCallbacks - Accessors for preprocessor callbacks.
453eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// Note that this class takes ownership of any PPCallbacks object given to
454eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// it.
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *getPPCallbacks() const { return Callbacks; }
456a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  void addPPCallbacks(PPCallbacks *C) {
457e5393fb93eb879d9ebbef102ae9311fa77e023ccDaniel Dunbar    if (Callbacks)
458e5393fb93eb879d9ebbef102ae9311fa77e023ccDaniel Dunbar      C = new PPChainedCallbacks(C, Callbacks);
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Callbacks = C;
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Given an identifier, return the MacroInfo it is \#defined to
46358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// or null if it isn't \#define'd.
464cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
465295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor    if (!II->hasMacroDefinition())
466295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor      return 0;
467ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
468295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor    return getInfoForMacro(II);
469cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  }
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Specify a macro for this identifier.
4725d5051f3e79bf754134ccdf7a1dc7778cd80c73eDouglas Gregor  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI,
4735d5051f3e79bf754134ccdf7a1dc7778cd80c73eDouglas Gregor                    bool LoadedFromAST = false);
474e40c4238a572bf8241a04e0005f70550cbfc1cfbAlexander Kornienko  /// \brief Undefine a macro for this identifier.
475e40c4238a572bf8241a04e0005f70550cbfc1cfbAlexander Kornienko  void clearMacroInfo(IdentifierInfo *II);
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4778a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// macro_iterator/macro_begin/macro_end - This allows you to walk the macro
4788a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// history table. Currently defined macros have
4798a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// IdentifierInfo::hasMacroDefinition() set and an empty
4808a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// MacroInfo::getUndefLoc() at the head of the list.
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  typedef llvm::DenseMap<IdentifierInfo*,
48209b5c1d08a33ecf5d9c61b922fbe679867336684Chris Lattner                         MacroInfo*>::const_iterator macro_iterator;
48388a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
48488a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  macro_iterator macro_end(bool IncludeExternalMacros = true) const;
485a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
486e3d5e3ae5bd8028774f07d7c3751d4db82118942Chris Lattner  const std::string &getPredefines() const { return Predefines; }
487aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// setPredefines - Set the predefines for this Preprocessor.  These
488aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// predefines are automatically injected when parsing the main file.
489aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const char *P) { Predefines = P; }
490aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const std::string &P) { Predefines = P; }
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getIdentifierInfo - Return information about the specified preprocessor
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier token.  The version of this method that takes two character
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pointers is preferred unless the identifier is already available as a
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// string (this avoids allocation and copying of memory to construct an
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// std::string).
497686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  IdentifierInfo *getIdentifierInfo(StringRef Name) const {
4983da736c1143126be19b253804b3b135ebcd3d6ffDaniel Dunbar    return &Identifiers.get(Name);
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If 'Namespace' is non-null, then it is a token required to exist on the
5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
504686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
5059b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  void AddPragmaHandler(PragmaHandler *Handler) {
506686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    AddPragmaHandler(StringRef(), Handler);
5079b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  }
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5094095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// RemovePragmaHandler - Remove the specific pragma handler from
5101824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// the preprocessor. If \p Namespace is non-null, then it should
5111824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// be the namespace that \p Handler was added to. It is an error
5124095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// to remove a handler that has not been registered.
513686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
5149b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  void RemovePragmaHandler(PragmaHandler *Handler) {
515686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    RemovePragmaHandler(StringRef(), Handler);
5169b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  }
5174095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar
5182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// \brief Add the specified comment handler to the preprocessor.
519aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  void addCommentHandler(CommentHandler *Handler);
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5212e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// \brief Remove the specified comment handler.
5222e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  ///
5232e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// It is an error to remove a handler that has not been registered.
524aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  void removeCommentHandler(CommentHandler *Handler);
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
526f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief Set the code completion handler to the given object.
527f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
528f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    CodeComplete = &Handler;
529f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
530ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
5311fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  /// \brief Retrieve the current code-completion handler.
5321fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  CodeCompletionHandler *getCodeCompletionHandler() const {
5331fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    return CodeComplete;
5341fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  }
535ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
536f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief Clear out the code completion handler.
537f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  void clearCodeCompletionHandler() {
538f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    CodeComplete = 0;
539f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
540ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
54155817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  /// \brief Hook used by the lexer to invoke the "natural language" code
54255817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  /// completion point.
54355817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  void CodeCompleteNaturalLanguage();
544ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
54594dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// \brief Retrieve the preprocessing record, or NULL if there is no
54694dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// preprocessing record.
547b9e1b75772db2c7db566c6034ba90a07f22e35ebDouglas Gregor  PreprocessingRecord *getPreprocessingRecord() const { return Record; }
548ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
549ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Create a new preprocessing record, which will keep track of
55094dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// all macro expansions, macro definitions, etc.
551c6c54521f95760a5eaf29b668d4bf41fe2af49d7Argyrios Kyrtzidis  void createPreprocessingRecord(bool RecordConditionalDirectives);
552ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
55353b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
5547dcc968f17a6ff9088c9651dddccc8d4025a1271Ted Kremenek  /// which implicitly adds the builtin defines etc.
555e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  void EnterMainSourceFile();
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
557dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  /// EndSourceFile - Inform the preprocessor callbacks that processing is
558dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  /// complete.
559dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  void EndSourceFile();
560dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFile - Add a source file to the top of the include stack and
562e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  /// start lexing tokens from it instead of the current buffer.  Emit an error
563e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  /// and don't enter the file on error.
564e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
565e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                       SourceLocation Loc);
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens from it instead of the current buffer.  Args specifies the
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens input to a function-like macro.
570e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  ///
571e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// ILEnd specifies the location of the ')' for a function-like macro or the
572e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// identifier for an object-like macro.
573c30981a563a8947cb26b1e308d122fa2ef90fcebRichard Smith  void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroInfo *Macro,
574c30981a563a8947cb26b1e308d122fa2ef90fcebRichard Smith                  MacroArgs *Args);
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
5776b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// which will cause the lexer to start returning the specified tokens.
5786b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
5796b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
5806b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// not be subject to further macro expansion.  Otherwise, these tokens will
5816b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// be re-macro-expanded when/if expansion is enabled.
5826b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
5836b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If OwnsTokens is false, this method assumes that the specified stream of
5846b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// tokens has a permanent owner somewhere, so they do not need to be copied.
5856b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If it is true, it assumes the array of tokens is allocated with new[] and
5866b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// must be freed.
5876b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
5886b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  void EnterTokenStream(const Token *Toks, unsigned NumToks,
5896b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner                        bool DisableMacroExpansion, bool OwnsTokens);
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer stack.  This should only be used in situations where the current
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// state of the top-of-stack lexer is known.
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RemoveTopOfLexerStack();
59503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
59603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// EnableBacktrackAtThisPos - From the point that this method is called, and
597ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
598ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
599ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// make the Preprocessor re-lex the same tokens.
60003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
601a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
602ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
603ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
60403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
605ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
606ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
607ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// tokens will continue indefinitely.
60803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
609a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  void EnableBacktrackAtThisPos();
61003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
611ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
612ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  void CommitBacktrackedTokens();
61303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
61403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// EnableBacktrackAtThisPos() was previously called.
616a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  void Backtrack();
61703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
61803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
61903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// caching of tokens is on.
620fad03b2b38a3baea4b67e79e676fee15078e3258Argyrios Kyrtzidis  bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
62103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
6225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Lex - To lex a token from the preprocessor, just pull a token from the
6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// current lexer or macro object.
624d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Lex(Token &Result) {
625b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    switch (CurLexerKind) {
626b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_Lexer: CurLexer->Lex(Result); break;
627b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_PTHLexer: CurPTHLexer->Lex(Result); break;
628b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_TokenLexer: CurTokenLexer->Lex(Result); break;
629b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_CachingLexer: CachingLex(Result); break;
630b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_LexAfterModuleImport: LexAfterModuleImport(Result); break;
631b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    }
6325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
634b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  void LexAfterModuleImport(Token &Result);
635ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// something not a comment.  This is useful in -E -C mode where comments
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// would foul up preprocessor directive handling.
639d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexNonComment(Token &Result) {
6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    do
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Result);
6425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (Result.getKind() == tok::comment);
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
644d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek
6455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexUnexpandedToken - This is just like Lex, but this disables macro
6465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansion of identifier tokens.
647d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexUnexpandedToken(Token &Result) {
6485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Disable macro expansion.
6495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    bool OldVal = DisableMacroExpansion;
6505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = true;
6515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Lex the token.
6525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Lex(Result);
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Reenable it.
6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = OldVal;
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65888710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
65988710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  /// expansion of identifier tokens.
66088710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  void LexUnexpandedNonComment(Token &Result) {
66188710f22be1acb04b7d812aebe87bea56394c07cEli Friedman    do
66288710f22be1acb04b7d812aebe87bea56394c07cEli Friedman      LexUnexpandedToken(Result);
66388710f22be1acb04b7d812aebe87bea56394c07cEli Friedman    while (Result.getKind() == tok::comment);
66488710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  }
66588710f22be1acb04b7d812aebe87bea56394c07cEli Friedman
6668c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// Disables macro expansion everywhere except for preprocessor directives.
6678c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  void SetMacroExpansionOnlyInDirectives() {
6688c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie    DisableMacroExpansion = true;
6698c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie    MacroExpansionInDirectivesOverride = true;
6708c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  }
6718c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie
6726b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// LookAhead - This peeks ahead N tokens and returns that token without
6736b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// consuming any tokens.  LookAhead(0) returns the next token that would be
6746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
6756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returns normal tokens after phase 5.  As such, it is equivalent to using
6766b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// 'Lex', not 'LexUnexpandedToken'.
67703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  const Token &LookAhead(unsigned N) {
67803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    if (CachedLexPos + N < CachedTokens.size())
67903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      return CachedTokens[CachedLexPos+N];
68003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    else
68103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      return PeekAhead(N+1);
6829e0ed0bd5a3a7bac73973980ff32132a7724e674Argyrios Kyrtzidis  }
6833604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis
68408b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// RevertCachedTokens - When backtracking is enabled and tokens are cached,
68508b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// this allows to revert a specific number of tokens.
68608b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// Note that the number of tokens being reverted should be up to the last
68708b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// backtrack position, not more.
68808b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  void RevertCachedTokens(unsigned N) {
68908b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(isBacktrackEnabled() &&
69008b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis           "Should only be called when tokens are cached for backtracking");
69108b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
69208b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis         && "Should revert tokens up to the last backtrack position, not more");
69308b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(signed(CachedLexPos) - signed(N) >= 0 &&
69408b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis           "Corrupted backtrack positions ?");
69508b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    CachedLexPos -= N;
69608b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  }
69708b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis
6983604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// EnterToken - Enters a token in the token stream to be lexed next. If
6993604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// BackTrack() is called afterwards, the token will remain at the insertion
7003604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// point.
7013604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void EnterToken(const Token &Tok) {
7023604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis    EnterCachingLexMode();
7033604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis    CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
7043604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  }
7053604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis
7063604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching
7073604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// tokens (because backtrack is enabled) it should replace the most recent
7083604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// cached tokens with the given annotation token. This function has no effect
7093604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// if backtracking is not enabled.
7103604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  ///
7113604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// Note that the use of this function is just for optimization; so that the
7123604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
7133604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// invoked.
7143604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void AnnotateCachedTokens(const Token &Tok) {
71547246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner    assert(Tok.isAnnotation() && "Expected annotation token");
71683cf05a3b0e655dc8ea1cb4c4e1eef541b770992Chris Lattner    if (CachedLexPos != 0 && isBacktrackEnabled())
7173604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis      AnnotatePreviousCachedTokens(Tok);
7183604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  }
71939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Replace the last token with an annotation token.
72139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  ///
72239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// Like AnnotateCachedTokens(), this routine replaces an
72339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// already-parsed (and resolved) token with an annotation
72439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// token. However, this routine only replaces the last token with
72539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// the annotation token; it does not affect any other cached
72639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// tokens. This function has no effect if backtracking is not
72739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// enabled.
72839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  void ReplaceLastTokenWithAnnotation(const Token &Tok) {
72939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    assert(Tok.isAnnotation() && "Expected annotation token");
73039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (CachedLexPos != 0 && isBacktrackEnabled())
73139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      CachedTokens[CachedLexPos-1] = Tok;
73239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
73339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
7340576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  /// TypoCorrectToken - Update the current token to represent the provided
7350576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  /// identifier, in order to cache an action performed by typo correction.
7360576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  void TypoCorrectToken(const Token &Tok) {
7370576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    assert(Tok.getIdentifierInfo() && "Expected identifier token");
7380576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (CachedLexPos != 0 && isBacktrackEnabled())
7390576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      CachedTokens[CachedLexPos-1] = Tok;
7400576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
7410576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
742d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
743d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  /// CurTokenLexer pointers.
744d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  void recomputeCurLexerKind();
745e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
746e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// \brief Returns true if incremental processing is enabled
747e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
748e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
749e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// \brief Enables the incremental processing
750e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  void enableIncrementalProcessing(bool value = true) {
751e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    IncrementalProcessing = value;
752e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  }
753d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor
7542968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Specify the point at which code-completion will be performed.
7552968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7562968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param File the file in which code completion should occur. If
7572968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// this file is included multiple times, code-completion will
7582968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// perform completion the first time it is included. If NULL, this
7592968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// function clears out the code-completion point.
7602968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7612968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Line the line at which code completion should occur
7622968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// (1-based).
7632968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7642968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Column the column at which code completion should occur
7652968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// (1-based).
7662968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
7672968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \returns true if an error occurred, false otherwise.
768a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  bool SetCodeCompletionPoint(const FileEntry *File,
7692968442603b029949246467253eeac8139a5b6d8Douglas Gregor                              unsigned Line, unsigned Column);
7702968442603b029949246467253eeac8139a5b6d8Douglas Gregor
7713437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis  /// \brief Determine if we are performing code completion.
7723437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis  bool isCodeCompletionEnabled() const { return CodeCompletionFile != 0; }
7733437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis
7747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns the location of the code-completion point.
7757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// Returns an invalid location if code-completion is not enabled or the file
7767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// containing the code-completion point has not been lexed yet.
7777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
7787d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7797d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns the start location of the file of code-completion point.
7807d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// Returns an invalid location if code-completion is not enabled or the file
7817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// containing the code-completion point has not been lexed yet.
7827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation getCodeCompletionFileLoc() const {
7837d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return CodeCompletionFileLoc;
7847d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
7857d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns true if code-completion is enabled and we have hit the
7877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// code-completion point.
7887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool isCodeCompletionReached() const { return CodeCompletionReached; }
7897d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
7907d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Note that we hit the code-completion point.
791d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  void setCodeCompletionReached() {
792d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
793d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    CodeCompletionReached = true;
794d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    // Silence any diagnostics that occur after we hit the code-completion.
795d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    getDiagnostics().setSuppressAllDiagnostics(true);
796d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  }
7977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
798809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief The location of the currently-active \#pragma clang
7998dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// arc_cf_code_audited begin.  Returns an invalid location if there
8008dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// is no such pragma active.
8018dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation getPragmaARCCFCodeAuditedLoc() const {
8028dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    return PragmaARCCFCodeAuditedLoc;
8038dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  }
8048dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
805809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Set the location of the currently-active \#pragma clang
8068dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// arc_cf_code_audited begin.  An invalid location ends the pragma.
8078dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
8088dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    PragmaARCCFCodeAuditedLoc = Loc;
8098dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  }
8108dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
811809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Instruct the preprocessor to skip part of the main source file.
812f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
813809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \param Bytes The number of bytes in the preamble to skip.
814f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
815809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \param StartOfLine Whether skipping these bytes puts the lexer at the
816f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// start of a line.
817ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
818f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    SkipMainFilePreamble.first = Bytes;
819f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    SkipMainFilePreamble.second = StartOfLine;
820f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  }
821ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
823d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  /// the specified Token's location, translating the token's start
8245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// position in the current buffer into a SourcePosition object for rendering.
825d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
826d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    return Diags->Report(Loc, DiagID);
827d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  }
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
829d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
830d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    return Diags->Report(Tok.getLocation(), DiagID);
831d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  }
8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
833834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// getSpelling() - Return the 'spelling' of the token at the given
834834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// location; does not go up to the spelling location or down to the
8359e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// expansion location.
83630900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  ///
837834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// \param buffer A buffer which will be used only if the token requires
838834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
83930900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  /// \param invalid If non-null, will be set \c true if an error occurs.
840686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getSpelling(SourceLocation loc,
841686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                              SmallVectorImpl<char> &buffer,
842834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall                              bool *invalid = 0) const {
8434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
844834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
84530900dafbe972f4802c2663262b4393edab0d1cdJohn McCall
8465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
8475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// token is the characters used to represent the token in the source file
8485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// after trigraph expansion and escaped-newline folding.  In particular, this
8495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// wants to get the true, uncanonicalized, spelling of things like digraphs
8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// UCNs, etc.
85150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
85230900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  /// \param Invalid If non-null, will be set \c true if an error occurs.
853b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
8544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
855b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
8560ff1042ddaad1419264be0de6da17f3b378482a4Daniel Dunbar
8575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling - This method is used to get the spelling of a token into a
8585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// preallocated buffer, instead of as an std::string.  The caller is required
8595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to allocate enough space for the token, which is guaranteed to be at least
8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Tok.getLength() bytes long.  The length of the actual result is returned.
8615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Note that this method may do two possible things: it may either fill in
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the buffer specified with characters, or it may *change the input pointer*
8645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to point to a constant buffer with the data already in it (avoiding a
8655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// copy).  The caller is not allowed to modify the returned buffer pointer
8665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// if an internal buffer is returned.
867ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  unsigned getSpelling(const Token &Tok, const char *&Buffer,
86848cf9824fbad42995f4d91d59d08d2620effd683Chris Lattner                       bool *Invalid = 0) const {
8694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
87048cf9824fbad42995f4d91d59d08d2620effd683Chris Lattner  }
871b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor
872ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// getSpelling - This method is used to get the spelling of a token into a
873ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// SmallVector. Note that the returned StringRef may not point to the
874ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// supplied buffer if a copy can be avoided.
875686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getSpelling(const Token &Tok,
876f4fb07ed4dfd144a39d904fc77b3b0bab580eb2eNico Weber                        SmallVectorImpl<char> &Buffer,
877f4fb07ed4dfd144a39d904fc77b3b0bab580eb2eNico Weber                        bool *Invalid = 0) const;
878ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer
8797216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner  /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
8807216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner  /// with length 1, return the character.
881ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  char getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
88250f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0) const {
8837216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    assert(Tok.is(tok::numeric_constant) &&
8847216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner           Tok.getLength() == 1 && "Called on unsupported token");
8857216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
8867216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner
8877216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // If the token is carrying a literal data pointer, just use it.
8887216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    if (const char *D = Tok.getLiteralData())
8897216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner      return *D;
89028396608ec20d44e9d1470e1ea51689bb504d0deTed Kremenek
8917216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // Otherwise, fall back on getCharacterData, which is slower, but always
8927216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // works.
893a543016fe07030f695d6d56fd22c8c8da617e0d7Douglas Gregor    return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
89428396608ec20d44e9d1470e1ea51689bb504d0deTed Kremenek  }
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// \brief Retrieve the name of the immediate macro expansion.
897c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  ///
898c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// This routine starts from a source location, and finds the name of the macro
899c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// responsible for its immediate expansion. It looks through any intervening
900c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// macro argument expansions to compute this. It returns a StringRef which
901c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// refers to the SourceManager-owned buffer of the source where that macro
902c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// name is spelled. Thus, the result shouldn't out-live the SourceManager.
903c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  StringRef getImmediateMacroName(SourceLocation Loc) {
9044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
905c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  }
906c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
90747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  /// CreateString - Plop the specified string into a scratch buffer and set the
90847246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  /// specified token's location and length to it.  If specified, the source
9099e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// location provides a location of the expansion point of the token.
910374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko  void CreateString(StringRef Str, Token &Tok,
911a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                    SourceLocation ExpansionLocStart = SourceLocation(),
912a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                    SourceLocation ExpansionLocEnd = SourceLocation());
913b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor
914b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// \brief Computes the source location just past the end of the
915b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// token at this source location.
916b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  ///
917b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// This routine can be used to produce a source location that
918b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// points just past the end of the token referenced by \p Loc, and
919b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// is generally used when a diagnostic needs to point just after a
920b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// token where it expected something different that it received. If
921b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// the returned source location would not be meaningful (e.g., if
922b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// it points into a macro), this routine returns an invalid
923b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// source location.
92433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  ///
92533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// \param Offset an offset from the end of the token, where the source
92633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// location should refer to. The default offset (0) produces a source
92733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// location pointing just past the end of the token; an offset of 1 produces
92833e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// a source location pointing to the last character in the token, etc.
9297ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
9304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
9317ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
9321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9337a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the first
9349e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// token of the macro expansion.
93569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
93669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// \param MacroBegin If non-null and function returns true, it is set to
93769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// begin location of the macro.
93869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  bool isAtStartOfMacroExpansion(SourceLocation loc,
93969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                 SourceLocation *MacroBegin = 0) const {
9404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
94169bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                            MacroBegin);
9427a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  }
9437a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
9447a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the last
9459e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// token of the macro expansion.
94669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
947ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// \param MacroEnd If non-null and function returns true, it is set to
94869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// end location of the macro.
94969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  bool isAtEndOfMacroExpansion(SourceLocation loc,
95069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                               SourceLocation *MacroEnd = 0) const {
9514e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
9527a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  }
9537a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
9545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// DumpToken - Print the token to stderr, used for debugging.
9555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
956d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
957c3d8d57b010e2ed15a2a7685d5761db14f5d2252Chris Lattner  void DumpLocation(SourceLocation Loc) const;
9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void DumpMacro(const MacroInfo &MI) const;
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96097ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
96197ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// token, return a new location that specifies a character within the token.
962872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
963872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner                                         unsigned Char) const {
9644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
965872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner  }
966872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner
9675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncrementPasteCounter - Increment the counters for the number of token
9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// paste operations performed.  If fast was specified, this is a 'fast paste'
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// case we handled.
9701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void IncrementPasteCounter(bool isFast) {
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (isFast)
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumFastTokenPaste;
9745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else
9755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumTokenPaste;
9765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats();
9795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
980c5c5e92ec53f7e6ac7ebbbf77c6d8e4b7d88daecArgyrios Kyrtzidis  size_t getTotalMemory() const;
981c5c5e92ec53f7e6ac7ebbbf77c6d8e4b7d88daecArgyrios Kyrtzidis
9823f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
9833f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// comment (/##/) in microsoft mode, this method handles updating the current
9843f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// state, returning the token on the next source line.
9853f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  void HandleMicrosoftCommentPaste(Token &Tok);
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Preprocessor callback methods.  These are invoked by a lexer as various
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // directives and events are found.
9905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
991c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
992c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// identifier information for the token and install it into the token,
993c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// updating the token kind accordingly.
994c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
99628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
99728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
99828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
99928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
100028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
100128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // SetPoisonReason - Call this function to indicate the reason for
100228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // poisoning an identifier. If that identifier is accessed while
100328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // poisoned, then this reason will be used instead of the default
100428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // "poisoned" diagnostic.
100528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
100628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
100728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // HandlePoisonedIdentifier - Display reason for poisoned
100828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // identifier.
100928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void HandlePoisonedIdentifier(Token & Tok);
101028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
101128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void MaybeHandlePoisonedIdentifier(Token & Identifier) {
101228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
101328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      if(II->isPoisoned()) {
101428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        HandlePoisonedIdentifier(Identifier);
101528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
101628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
101728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
101828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
101928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
102028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  /// Identifiers used for SEH handling in Borland. These are only
102128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  /// allowed in particular circumstances
1022ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __except block
1023ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__exception_code,
1024ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___exception_code,
1025ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_GetExceptionCode;
1026ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __except filter expression
1027ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__exception_info,
1028ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___exception_info,
1029ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_GetExceptionInfo;
1030ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __finally
1031ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__abnormal_termination,
1032ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___abnormal_termination,
1033ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_AbnormalTermination;
103428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
103528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void PoisonSEHIdentifiers(bool Poison = true); // Borland
103628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
10375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleIdentifier - This callback is invoked when the lexer reads an
10385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier and has filled in the tokens IdentifierInfo member.  This
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// callback potentially macro expands it or turns it into a named token (like
10405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// 'for').
1041d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentifier(Token &Identifier);
10425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the current file.  This either returns the EOF token and returns true, or
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pops a level off the include stack and returns false, at which point the
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// client should call lex again.
1048d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1050fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// HandleEndOfTokenLexer - This callback is invoked when the current
1051fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// TokenLexer hits the end of its token stream.
1052fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  bool HandleEndOfTokenLexer(Token &Result);
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleDirective - This callback is invoked when the lexer sees a # token
10551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// at the start of a line.  This consumes the directive, modifies the
10565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
10575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// read is the correct one.
1058d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleDirective(Token &Result);
10595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
106084021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// CheckEndOfDirective - Ensure that the next token is a tok::eod token.  If
106184021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// not, emit a diagnostic and consume up until the eod.  If EnableMacros is
1062ab82f41b217ce588a9456c0b4411f219d3ed0df8Chris Lattner  /// true, then we consider macros that expand to zero tokens as being ok.
1063ab82f41b217ce588a9456c0b4411f219d3ed0df8Chris Lattner  void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
10641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1065478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
106684021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// current line until the tok::eod token is found.
1067478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  void DiscardUntilEndOfDirective();
10681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10697c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  /// SawDateOrTime - This returns true if the preprocessor has seen a use of
10707c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  /// __DATE__ or __TIME__ in the file so far.
10717c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  bool SawDateOrTime() const {
10727c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
10737c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  }
1074c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  unsigned getCounterValue() const { return CounterValue; }
1075c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  void setCounterValue(unsigned V) { CounterValue = V; }
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107790db26000aefe9335370013eec64c85232d80227Douglas Gregor  /// \brief Retrieves the module that we're currently building, if any.
107890db26000aefe9335370013eec64c85232d80227Douglas Gregor  Module *getCurrentModule();
107990db26000aefe9335370013eec64c85232d80227Douglas Gregor
108058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Allocate a new MacroInfo object with the provided SourceLocation.
1081f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *AllocateMacroInfo(SourceLocation L);
1082f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
108358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Allocate a new MacroInfo object which is clone of \p MI.
1084f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *CloneMacroInfo(const MacroInfo &MI);
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108658bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Turn the specified lexer token into a fully checked and spelled
108758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// filename, e.g. as an operand of \#include.
108858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
108958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// The caller is expected to provide a buffer that is large enough to hold
109058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// the spelling of the filename, but is also expected to handle the case
109158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// when this method decides to use a different buffer.
109258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
109358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \returns true if the input filename was in <>'s or false if it was
109458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// in ""'s.
1095686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1096a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
109758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Given a "foo" or \<foo> reference, look up the indicated file.
109858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
109958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// Returns null on failure.  \p isAngled indicates whether the file
110058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// reference is for system \#include's or not (i.e. using <> instead of "").
1101686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  const FileEntry *LookupFile(StringRef Filename,
1102f45b646244705410866d62f1d8bf017a047ed662Chris Lattner                              bool isAngled, const DirectoryLookup *FromDir,
1103b5142bb7af5c70fffd09f05172a1379a35a9c29aChandler Carruth                              const DirectoryLookup *&CurDir,
1104686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                              SmallVectorImpl<char> *SearchPath,
1105fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor                              SmallVectorImpl<char> *RelativePath,
11061a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor                              Module **SuggestedModule,
11071c2e9332fa69727425a3a2b912e36e2ab62083f8Douglas Gregor                              bool SkipCache = false);
1108a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
1109a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// GetCurLookup - The DirectoryLookup structure used to find the current
1110a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
111158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// implement \#include_next and find directory-specific properties.
1112a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1113a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
111458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Return true if we're in the top-level file, not in a \#include.
1115a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  bool isInPrimaryFile() const;
1116a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
111758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// ConcatenateIncludeName - Handle cases where the \#include name is expanded
1118a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// from a macro as multiple tokens, which need to be glued together.  This
1119a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// occurs for code like:
112058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \code
112170517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  ///    \#define FOO <x/y.h>
112258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///    \#include FOO
112358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \endcode
112470517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// because in this case, "<x/y.h>" is returned as 7 tokens, not one.
1125a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  ///
1126321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// This code concatenates and consumes tokens up to the '>' token.  It
1127321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// returns false if the > was found, otherwise it returns true if it finds
112884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// and consumes the EOD marker.
1129f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1130ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor                              SourceLocation &End);
1131a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
11329d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne  /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is
113384021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// followed by EOD.  Return true if the token is not a valid on-off-switch.
11349d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne  bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
11359d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne
11365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1138d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  void PushIncludeMacroStack() {
1139b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind,
1140b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor                                                 CurLexer.take(),
11419c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurPTHLexer.take(),
11429c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurPPLexer,
11439c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurTokenLexer.take(),
11449c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurDirLookup));
1145f89963b1e819d5e73796a4f9d7cd70129846e2ecTed Kremenek    CurPPLexer = 0;
1146d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  }
11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1148d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  void PopIncludeMacroStack() {
1149caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek    CurLexer.reset(IncludeMacroStack.back().TheLexer);
11509c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer);
11519c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1152caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek    CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
11539c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1154b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1155d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek    IncludeMacroStack.pop_back();
1156d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  }
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Allocate a new MacroInfo object.
1159f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *AllocateMacroInfo();
1160f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
116158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Release the specified MacroInfo for re-use.
116258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
116358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// This memory will  be reused for allocating new MacroInfo objects.
11640301b3ff132a4d986c092d161cb77d74b04cd2a6Chris Lattner  void ReleaseMacroInfo(MacroInfo* MI);
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroName - Lex and validate a macro name, which occurs after a
116758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \#define or \#undef.  This emits a diagnostic, sets the token kind to eod,
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// and discards the rest of the macro line if the macro name is invalid.
1169d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
11725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// definition has just been read.  Lex the rest of the arguments and the
1173e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  /// closing ), updating MI with what we learn and saving in LastTok the
1174e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  /// last token read.
1175e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  /// Return true if an error occurs parsing the arg list.
1176e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok);
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// We just read a \#if or related directive and decided that the
117958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// subsequent tokens are in the \#if'd out portion of the
118058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// file.  Lex the rest of the file, until we see an \#endif.  If \p
11815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FoundNonSkipPortion is true, then we have already emitted code for part of
118258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// this \#if directive, so \#else/\#elif blocks should never be entered. If
118358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \p FoundElse is false, then \#else directives are ok, if not, then we have
118458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// already seen one so a \#else directive is a duplicate.  When this returns,
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the caller can lex the first valid token.
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
11876b4ff04dd22e7159887ea6c1bb8b1c01fc9c70a9Argyrios Kyrtzidis                                    bool FoundNonSkipPortion, bool FoundElse,
11886b4ff04dd22e7159887ea6c1bb8b1c01fc9c70a9Argyrios Kyrtzidis                                    SourceLocation ElseLoc = SourceLocation());
11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief A fast PTH version of SkipExcludedConditionalBlock.
1191268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  void PTHSkipExcludedConditionalBlock();
11921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
11945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// may occur after a #if or #elif directive and return it as a bool.  If the
11955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
11965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
119958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \#pragma GCC poison/system_header/dependency and \#pragma once.
12005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinPragmas();
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Register builtin macros such as __LINE__ with the identifier table.
12035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinMacros();
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
12065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
12075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the macro should not be expanded return true, otherwise return false.
1208d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
12091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12105b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// \brief Cache macro expanded tokens for TokenLexers.
12115b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  //
12125b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
12135b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// going to lex in the cache and when it finishes the tokens are removed
12145b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// from the end of the cache.
12155b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
12162d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                                  ArrayRef<Token> tokens);
12175b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  void removeCachedMacroExpandedTokensOfLastLexer();
12185b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  friend void TokenLexer::ExpandFunctionArguments();
12195b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis
12205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
12215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexed is a '('.  If so, consume the token and return true, if not, this
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// method should have no observable side-effect on the lexed tokens.
12235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isNextPPTokenLParen();
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
12265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invoked to read all of the formal arguments specified for the macro
12275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invocation.  This returns null on error.
1228e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
12299e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth                                       SourceLocation &ExpansionEnd);
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
12315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
12325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// as a builtin macro, handle it and return the next token as 'Tok'.
1233d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ExpandBuiltinMacro(Token &Tok);
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
12365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// return the first token after the directive.  The _Pragma token has just
12375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// been read into 'Tok'.
1238d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Handle_Pragma(Token &Tok);
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12401ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
12411ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  /// is not enclosed within a string literal.
12421ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  void HandleMicrosoft__pragma(Token &Tok);
12431ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall
12445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
12455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// start lexing tokens from it instead of the current buffer.
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
12476137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek
12486137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and
12496137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// start getting tokens from it using the PTH cache.
12506137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
125234eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  /// IsFileLexer - Returns true if we are lexing from a file and not a
125334eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  ///  pragma or a macro.
125434eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
125534eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek    return L ? !L->isPragmaLexer() : P != 0;
125641938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
125741938c8493b4380df738263166b746eacb33c309Ted Kremenek
125881d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek  static bool IsFileLexer(const IncludeStackInfo& I) {
125981d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek    return IsFileLexer(I.TheLexer, I.ThePPLexer);
126041938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
126141938c8493b4380df738263166b746eacb33c309Ted Kremenek
126281d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek  bool IsFileLexer() const {
126381d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek    return IsFileLexer(CurLexer.get(), CurPPLexer);
126441938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
126703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  // Caching stuff.
126803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void CachingLex(Token &Result);
126903569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis  bool InCachingLexMode() const {
127003569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis    // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
127103569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis    // that we are past EOF, not that we are in CachingLex mode.
1272ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 &&
127356eb1ec6e54080e47bbc62412737c25afb5211edTed Kremenek           !IncludeMacroStack.empty();
127403569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis  }
127503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void EnterCachingLexMode();
127603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void ExitCachingLexMode() {
127703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    if (InCachingLexMode())
127803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      RemoveTopOfLexerStack();
127903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  }
128003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  const Token &PeekAhead(unsigned N);
12813604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void AnnotatePreviousCachedTokens(const Token &Tok);
128203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
128303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
12845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle*Directive - implement the various preprocessor directives.  These
12855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// should side-effect the current preprocessor object so that the next call
12865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to Lex() will return the appropriate token next.
1287359cc4475487ea0a660b3e8918334a2866e71c66Chris Lattner  void HandleLineDirective(Token &Tok);
1288478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  void HandleDigitDirective(Token &Tok);
1289d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1290d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentSCCSDirective(Token &Tok);
129194ad28b31433058445a27db722f60402ee820beaDouglas Gregor  void HandleMacroPublicDirective(Token &Tok);
1292aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor  void HandleMacroPrivateDirective(Token &Tok);
1293ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
12945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // File inclusion.
1295ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeDirective(SourceLocation HashLoc,
1296ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor                              Token &Tok,
12975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const DirectoryLookup *LookupFrom = 0,
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              bool isImport = false);
1299ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1300ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1301ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
13024207edaf03da28fa917a94b31c5c1eef0e6417dcAaron Ballman  void HandleMicrosoftImportDirective(Token &Tok);
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Macro handling.
130542e6737f2efb113563140ad794c21c7709250402Chris Lattner  void HandleDefineDirective(Token &Tok);
1306d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUndefDirective(Token &Tok);
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Conditional Inclusion.
1309d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfdefDirective(Token &Tok, bool isIfndef,
13105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                            bool ReadAnyTokensBeforeDirective);
1311d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1312d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleEndifDirective(Token &Tok);
1313d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElseDirective(Token &Tok);
1314d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElifDirective(Token &Tok);
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Pragmas.
131780c60f72848896f867f6b7e664e7060d9e78f019Douglas Gregor  void HandlePragmaDirective(unsigned Introducer);
13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1319d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaOnce(Token &OnceTok);
13202243449253475574fc6f14986ff8f7fce5d46799Chris Lattner  void HandlePragmaMark();
1321d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaPoison(Token &PoisonTok);
1322d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaSystemHeader(Token &SysHeaderTok);
1323d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaDependency(Token &DependencyTok);
1324636c5ef6572e899d36cec1b0023fb28ba65189e1Chris Lattner  void HandlePragmaComment(Token &CommentTok);
1325abfe094ce71c42656dcb84a3bdc3e79cb3c16fc3Chris Lattner  void HandlePragmaMessage(Token &MessageTok);
1326f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  void HandlePragmaPushMacro(Token &Tok);
1327f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  void HandlePragmaPopMacro(Token &Tok);
13284c55c54db8e676aa3e042188773d9d82d59fff91Aaron Ballman  void HandlePragmaIncludeAlias(Token &Tok);
1329f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1330f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
1331046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // Return true and store the first token only if any CommentHandler
1332046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // has inserted some tokens and getCommentRetentionState() is false.
1333046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  bool HandleComment(Token &Token, SourceRange Comment);
13340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
13350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief A macro is used, update information about macros that need unused
13360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// warnings.
13370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void markMacroAsUsed(MacroInfo *MI);
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
13395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Abstract base class that describes a handler that will receive
13412e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// source ranges for each of the comments encountered in the source file.
13422e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorclass CommentHandler {
13432e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorpublic:
13442e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  virtual ~CommentHandler();
13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1346046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // The handler shall return true if it has pushed any tokens
1347046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // to be read using e.g. EnterToken or EnterTokenStream.
1348046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
13492e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor};
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
13525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1354