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
171b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
18d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer#include "clang/Basic/Diagnostic.h"
19c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner#include "clang/Basic/IdentifierTable.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
2130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/Lexer.h"
2230a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/MacroInfo.h"
23bc3f628815b3841dc99109e7f67f9afa7793bc94Lawrence Crowl#include "clang/Lex/ModuleMap.h"
2430a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/PPCallbacks.h"
2530a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/PTHLexer.h"
2630a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/PTHManager.h"
2730a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Lex/TokenLexer.h"
2830a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/ArrayRef.h"
29cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner#include "llvm/ADT/DenseMap.h"
304f32786ac45210143654390177105eb749b614e9Ted Kremenek#include "llvm/ADT/IntrusiveRefCntPtr.h"
31caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek#include "llvm/ADT/OwningPtr.h"
3230a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/SmallPtrSet.h"
33f4d5eb4866a27d497f0bb75b12c2ffd48ad4d9c0Benjamin Kramer#include "llvm/ADT/SmallVector.h"
340ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek#include "llvm/Support/Allocator.h"
352e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor#include <vector>
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
378fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramernamespace llvm {
388fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer  template<unsigned InternalLen> class SmallString;
398fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer}
408fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
4488a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregorclass ExternalPreprocessorSource;
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileManager;
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass HeaderSearch;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaNamespace;
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaHandler;
502e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorclass CommentHandler;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass ScratchBuffer;
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass TargetInfo;
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PPCallbacks;
54f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregorclass CodeCompletionHandler;
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass DirectoryLookup;
5694dc8f640ebea52241412512ed48601626edbc58Douglas Gregorclass PreprocessingRecord;
576aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregorclass ModuleLoader;
5836a16498ff911a218f26c7955376bbe99ddb16dfDouglas Gregorclass PreprocessorOptions;
59ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
601952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko/// \brief Stores token information for comparing actual tokens with
611952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko/// predefined values.  Only handles simple tokens and identifiers.
621952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenkoclass TokenValue {
631952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  tok::TokenKind Kind;
641952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  IdentifierInfo *II;
651952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko
661952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenkopublic:
671952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  TokenValue(tok::TokenKind Kind) : Kind(Kind), II(0) {
681952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    assert(Kind != tok::raw_identifier && "Raw identifiers are not supported.");
691952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    assert(Kind != tok::identifier &&
701952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko           "Identifiers should be created by TokenValue(IdentifierInfo *)");
711952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    assert(!tok::isLiteral(Kind) && "Literals are not supported.");
721952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    assert(!tok::isAnnotation(Kind) && "Annotations are not supported.");
731952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  }
741952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
751952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  bool operator==(const Token &Tok) const {
761952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    return Tok.getKind() == Kind &&
771952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko        (!II || II == Tok.getIdentifierInfo());
781952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  }
791952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko};
801952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko
81e5956bd2730c051835f9acd9e957c5d79f99e7c3Chris Lattner/// Preprocessor - This object engages in a tight little dance with the lexer to
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// efficiently preprocess tokens.  Lexers know only about tokens within a
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// single source file, and don't know anything about preprocessor-level issues
8458bf98725b6d4588338e191d2ab981b104471dabJames Dennett/// like the \#include stack, token expansion, etc.
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
86c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmithclass Preprocessor : public RefCountedBase<Preprocessor> {
87cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  IntrusiveRefCntPtr<PreprocessorOptions> PPOpts;
88d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  DiagnosticsEngine        *Diags;
894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  LangOptions       &LangOpts;
90998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  const TargetInfo  *Target;
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager       &FileMgr;
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager     &SourceMgr;
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ScratchBuffer     *ScratchBuf;
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch      &HeaderInfo;
956aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ModuleLoader      &TheModuleLoader;
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9788a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  /// \brief External source of macros.
9888a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  ExternalPreprocessorSource *ExternalSource;
99a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
100ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1016137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// PTH - An optional PTHManager object used for getting tokens from
1026137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  ///  a token cache rather than lexing the original source file.
1036f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<PTHManager> PTH;
1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1050ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  /// BP - A BumpPtrAllocator object used to quickly allocate and release
1060ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  ///  objects internal to the Preprocessor.
1070ea76727ae91bca918a8414ed85b530eddcfedebTed Kremenek  llvm::BumpPtrAllocator BP;
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers for builtin macros and other builtins.
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
115c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
1161ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  IdentifierInfo *Ident_Pragma, *Ident__pragma;    // _Pragma, __pragma
1171ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
118148772a841cae6f32db16d890e788b92a763bb3fChris Lattner  IdentifierInfo *Ident__has_feature;              // __has_feature
119c1b5fa41f09512c74030b9a2a0d1564535e22a76Peter Collingbourne  IdentifierInfo *Ident__has_extension;            // __has_extension
120148772a841cae6f32db16d890e788b92a763bb3fChris Lattner  IdentifierInfo *Ident__has_builtin;              // __has_builtin
121cae5095c116c29a4e52c91fa0ad88df09dd2b45fAnders Carlsson  IdentifierInfo *Ident__has_attribute;            // __has_attribute
12292bd8c70a6837b647a6c55964f8d0a50bf561dbcJohn Thompson  IdentifierInfo *Ident__has_include;              // __has_include
12392bd8c70a6837b647a6c55964f8d0a50bf561dbcJohn Thompson  IdentifierInfo *Ident__has_include_next;         // __has_include_next
124d768150ef57f617c8d9fef48f3c92e8f21698024Ted Kremenek  IdentifierInfo *Ident__has_warning;              // __has_warning
125b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  IdentifierInfo *Ident__building_module;          // __building_module
126b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  IdentifierInfo *Ident__MODULE__;                 // __MODULE__
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation DATELoc, TIMELoc;
129c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  unsigned CounterValue;  // Next __COUNTER__ value.
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum {
13258bf98725b6d4588338e191d2ab981b104471dabJames Dennett    /// MaxIncludeStackDepth - Maximum depth of \#includes.
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MaxAllowedIncludeStackDepth = 200
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that is set before the preprocessor begins.
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepComments : 1;
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepMacroComments : 1;
139f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  bool SuppressIncludeNotFoundError : 1;
140ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that changes while the preprocessor runs:
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1445814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar  /// Whether the preprocessor owns the header search object.
1455814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar  bool OwnsHeaderSearch : 1;
1465814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar
1471d9c54df56391ac4740db27d551782e81189cb51Chris Lattner  /// DisableMacroExpansion - True if macro expansion is disabled.
1481d9c54df56391ac4740db27d551782e81189cb51Chris Lattner  bool DisableMacroExpansion : 1;
1491d9c54df56391ac4740db27d551782e81189cb51Chris Lattner
1508c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// MacroExpansionInDirectivesOverride - Temporarily disables
1518c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// DisableMacroExpansion (i.e. enables expansion) when parsing preprocessor
1528c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// directives.
1538c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  bool MacroExpansionInDirectivesOverride : 1;
1548c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie
1558c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  class ResetMacroExpansionHelper;
1568c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie
15788a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  /// \brief Whether we have already loaded macros from the external source.
15888a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  mutable bool ReadMacrosFromExternalSource : 1;
159a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
1606fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  /// \brief True if pragmas are enabled.
1616fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  bool PragmasEnabled : 1;
1626fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose
16374c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  /// \brief True if the current build action is a preprocessing action.
16474c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  bool PreprocessedOutput : 1;
16574c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose
16631672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  /// \brief True if we are currently preprocessing a #if or #elif directive
16731672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  bool ParsingIfOrElifDirective;
16831672b1745195d709b641f1c0f44e203742fa73bAaron Ballman
16914e645557ae91c6770d62beb00a1c522e0bfd5d6Argyrios Kyrtzidis  /// \brief True if we are pre-expanding macro arguments.
17014e645557ae91c6770d62beb00a1c522e0bfd5d6Argyrios Kyrtzidis  bool InMacroArgPreExpansion;
17114e645557ae91c6770d62beb00a1c522e0bfd5d6Argyrios Kyrtzidis
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers - This is mapping/lookup information for all identifiers in
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the program, including program keywords.
174c3222091e1ffa35d0264ca6b680a88c9dc84ede2Daniel Dunbar  mutable IdentifierTable Identifiers;
1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// Selectors - This table contains all the selectors in the program. Unlike
17768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
1789e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// It is declared/expanded here because it's role/lifetime is
17968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// conceptually similar the IdentifierTable. In addition, the current control
1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// flow (in clang::ParseAST()), make it convenient to put here.
18168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
182083abdf67f157e9d2ab5a8c9d5e71240479d3c99Sebastian Redl  /// the lifetime of the preprocessor.
18329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable Selectors;
1842e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
1851b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  /// BuiltinInfo - Information about builtins.
1861b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  Builtin::Context BuiltinInfo;
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PragmaHandlers - This tracks all of the pragmas that the client registered
1895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// with this preprocessor.
1905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PragmaNamespace *PragmaHandlers;
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Tracks all of the comment handlers that the client registered
1932e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// with this preprocessor.
1942e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  std::vector<CommentHandler *> CommentHandlers;
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
196e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// \brief True if we want to ignore EOF token and continue later on (thus
197e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// avoid tearing the Lexer and etc. down).
198e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  bool IncrementalProcessing;
199e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
200f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief The code-completion handler.
201f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  CodeCompletionHandler *CodeComplete;
202ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2032968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief The file that we're performing code-completion for, if any.
2042968442603b029949246467253eeac8139a5b6d8Douglas Gregor  const FileEntry *CodeCompletionFile;
2052968442603b029949246467253eeac8139a5b6d8Douglas Gregor
2067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The offset in file for the code-completion point.
2077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  unsigned CodeCompletionOffset;
2087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
2097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The location for the code-completion point. This gets instantiated
21058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// when the CodeCompletionFile gets \#include'ed for preprocessing.
2117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation CodeCompletionLoc;
2127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
2137d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief The start location for the file of the code-completion point.
21458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
21558bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// This gets instantiated when the CodeCompletionFile gets \#include'ed
2167d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// for preprocessing.
2177d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation CodeCompletionFileLoc;
2187d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
219c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor  /// \brief The source location of the 'import' contextual keyword we just
220c13a34b690d2dc2a03c2fea75a0a1438636c19ceDouglas Gregor  /// lexed, if any.
221b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  SourceLocation ModuleImportLoc;
2228dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
223b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// \brief The module import path that we're currently processing.
224cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> ModuleImportPath;
225b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor
226b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// \brief Whether the module import expectes an identifier next. Otherwise,
227b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  /// it expects a '.' or ';'.
228b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor  bool ModuleImportExpectsIdentifier;
229b514c792821a8f053027d88444e13bfaa8efef76Douglas Gregor
2308dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// \brief The source location of the currently-active
2318dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// #pragma clang arc_cf_code_audited begin.
2328dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation PragmaARCCFCodeAuditedLoc;
233ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief True if we hit the code-completion point.
2357d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool CodeCompletionReached;
2367d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
237f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// \brief The number of bytes that we will initially skip when entering the
238f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// main file, which is used when loading a precompiled preamble, along
239f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// with a flag that indicates whether skipping this number of bytes will
240f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// place the lexer at the start of a line.
241f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  std::pair<unsigned, bool> SkipMainFilePreamble;
242ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLexer - This is the current top of the stack that we're lexing from if
2449c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// not expanding a macro and we are lexing directly from source code.
2459c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
2466f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<Lexer> CurLexer;
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2489c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// CurPTHLexer - This is the current top of stack that we're lexing from if
2499c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  not expanding from a macro and we are lexing from a PTH cache.
2509c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
2516f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<PTHLexer> CurPTHLexer;
2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2539c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  /// CurPPLexer - This is the current top of the stack what we're lexing from
2549c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  if not expanding a macro.  This is an alias for either CurLexer or
2559c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek  ///  CurPTHLexer.
25623f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  PreprocessorLexer *CurPPLexer;
257ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
2585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLookup - The DirectoryLookup structure used to find the current
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
26058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// implement \#include_next and find directory-specific properties.
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const DirectoryLookup *CurDirLookup;
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2636cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurTokenLexer - This is the current macro we are expanding, if we are
2646cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
2656f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<TokenLexer> CurTokenLexer;
2661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
267b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  /// \brief The kind of lexer we're currently working with.
268ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  enum CurLexerKind {
269ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_Lexer,
270ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_PTHLexer,
271ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    CLK_TokenLexer,
272b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CLK_CachingLexer,
273b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CLK_LexAfterModuleImport
274b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  } CurLexerKind;
275b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor
2765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncludeMacroStack - This keeps track of the stack of files currently
27758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \#included, and macros currently being expanded from, not counting
2786cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurLexer/CurTokenLexer.
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  struct IncludeStackInfo {
280b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    enum CurLexerKind     CurLexerKind;
2819c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    Lexer                 *TheLexer;
2829c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    PTHLexer              *ThePTHLexer;
2839c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    PreprocessorLexer     *ThePPLexer;
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    TokenLexer            *TheTokenLexer;
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const DirectoryLookup *TheDirLookup;
2869c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek
287ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie    IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P,
288b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor                     PreprocessorLexer* PPL,
2899c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                     TokenLexer* TL, const DirectoryLookup *D)
290ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie      : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL),
291b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor        TheTokenLexer(TL), TheDirLookup(D) {}
2925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::vector<IncludeStackInfo> IncludeMacroStack;
2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Callbacks - These are actions invoked when some preprocessor activity is
29658bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// encountered (e.g. a file is \#included, etc).
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *Callbacks;
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29966c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis  struct MacroExpandsInfo {
30066c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis    Token Tok;
301c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis    MacroDirective *MD;
30266c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis    SourceRange Range;
303c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis    MacroExpandsInfo(Token Tok, MacroDirective *MD, SourceRange Range)
304c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis      : Tok(Tok), MD(MD), Range(Range) { }
30566c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis  };
30666c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis  SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
30766c44e700fb3f244b2c443bfbd6a05b5f1843ec0Argyrios Kyrtzidis
3088a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// Macros - For each IdentifierInfo that was associated with a macro, we
3098a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// keep a mapping to the history of all macro definitions and #undefs in
3108a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// the reverse order (the latest one is in the head of the list).
3119818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  llvm::DenseMap<const IdentifierInfo*, MacroDirective*> Macros;
312c12906ee78e006df6a71acf52ca9dd3171ddbf07Douglas Gregor  friend class ASTReader;
313c12906ee78e006df6a71acf52ca9dd3171ddbf07Douglas Gregor
3140827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief Macros that we want to warn because they are not used at the end
3150827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// of the translation unit; we store just their SourceLocations instead
3160827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// something like MacroInfo*. The benefit of this is that when we are
3170827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// deserializing from PCH, we don't need to deserialize identifier & macros
3180827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// just so that we can report that they are unused, we just warn using
3190827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// the SourceLocations of this set (that will be filled by the ASTReader).
3200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// We are using SmallPtrSet instead of a vector for faster removal.
3210827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
3220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
3230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
32423f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  /// MacroArgCache - This is a "freelist" of MacroArg objects that can be
32523f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  /// reused for quick allocation.
32623f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  MacroArgs *MacroArgCache;
32723f77e59718385512984d4e2a021bef52b9f6ddfChris Lattner  friend class MacroArgs;
328ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
329ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma
330ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// push_macro directive, we keep a MacroInfo stack used to restore
331f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  /// previous macro value.
332f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Various statistics we track for performance analysis.
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumIf, NumElse, NumEndif;
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumSkipped;
3411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
342aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// Predefines - This string is the predefined macros that preprocessor
343aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// should use from the command line etc.
344aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  std::string Predefines;
3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34695d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis  /// \brief The file ID for the preprocessor predefines.
34795d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis  FileID PredefinesFileID;
34895d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis
3496cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
3506cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  enum { TokenLexerCacheSize = 8 };
3516cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  unsigned NumCachedTokenLexers;
3526cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
3539e0ed0bd5a3a7bac73973980ff32132a7724e674Argyrios Kyrtzidis
3545b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// \brief Keeps macro expanded tokens for TokenLexers.
3555b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  //
3565b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
3575b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// going to lex in the cache and when it finishes the tokens are removed
3585b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// from the end of the cache.
359686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<Token, 16> MacroExpandedTokens;
3605b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
3615b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis
3629e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// \brief A record of the macro definitions and expansions that
363ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// occurred during preprocessing.
36494dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  ///
36594dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// This is an optional side structure that can be enabled with
36694dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// \c createPreprocessingRecord() prior to preprocessing.
367b9e1b75772db2c7db566c6034ba90a07f22e35ebDouglas Gregor  PreprocessingRecord *Record;
368ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
369e671e1bc73615eda155059a772266ed2882d758cChris Lattnerprivate:  // Cached tokens state.
370686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  typedef SmallVector<Token, 1> CachedTokensTy;
37103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
37203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// CachedTokens - Cached tokens are stored here when we do backtracking or
37303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// lookahead. They are "lexed" by the CachingLex() method.
37403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  CachedTokensTy CachedTokens;
37503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
37603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// CachedLexPos - The position of the cached token that CachingLex() should
37703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// "lex" next. If it points beyond the CachedTokens vector, it means that
37803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// a normal Lex() should be invoked.
37903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  CachedTokensTy::size_type CachedLexPos;
38003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
381a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// BacktrackPositions - Stack of backtrack positions, allowing nested
382a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
383a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// indicate where CachedLexPos should be set when the BackTrack() method is
384a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// invoked (at which point the last position is popped).
385a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  std::vector<CachedTokensTy::size_type> BacktrackPositions;
38603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
387af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  struct MacroInfoChain {
388af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek    MacroInfo MI;
389af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek    MacroInfoChain *Next;
3909714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek    MacroInfoChain *Prev;
391af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  };
392af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek
393af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  /// MacroInfos are managed as a chain for easy disposal.  This is the head
394af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  /// of that list.
395af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek  MacroInfoChain *MIChainHead;
396af8fa25c0d4e0540952a50bbd06dc1558954ccd9Ted Kremenek
3979714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
3989714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  /// allocation.
3999714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek  MacroInfoChain *MICache;
4009714a2385cb66b6efa373fc668641de602dd9adbTed Kremenek
4013e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis  struct DeserializedMacroInfoChain {
4023e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis    MacroInfo MI;
4033e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis    unsigned OwningModuleID; // MUST be immediately after the MacroInfo object
4043e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis                     // so it can be accessed by MacroInfo::getOwningModuleID().
4053e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis    DeserializedMacroInfoChain *Next;
4063e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis  };
4073e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis  DeserializedMacroInfoChain *DeserialMIChainHead;
4083e25b990f1e2ba2a9a63dde5fd111492a7f6194aArgyrios Kyrtzidis
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
410cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
41136a16498ff911a218f26c7955376bbe99ddb16dfDouglas Gregor               DiagnosticsEngine &diags, LangOptions &opts,
412998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               const TargetInfo *target,
41372b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek               SourceManager &SM, HeaderSearch &Headers,
4146aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor               ModuleLoader &TheModuleLoader,
4155814e657c9ad9ef6049a2a4af0d2aad248a8a15cDaniel Dunbar               IdentifierInfoLookup *IILookup = 0,
416998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor               bool OwnsHeaderSearch = false,
417e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann               bool DelayInitialization = false,
418e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann               bool IncrProcessing = false);
41972b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ~Preprocessor();
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
422998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// \brief Initialize the preprocessor, if the constructor did not already
423998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// perform the initialization.
424998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  ///
425998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  /// \param Target Information about the target.
426998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  void Initialize(const TargetInfo &Target);
427ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
428a71a7d8a1ce4474e7bdb680658fb58b6caf391d3Douglas Gregor  /// \brief Retrieve the preprocessor options used to initialize this
429a71a7d8a1ce4474e7bdb680658fb58b6caf391d3Douglas Gregor  /// preprocessor.
430a71a7d8a1ce4474e7bdb680658fb58b6caf391d3Douglas Gregor  PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
431a71a7d8a1ce4474e7bdb680658fb58b6caf391d3Douglas Gregor
432d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  DiagnosticsEngine &getDiagnostics() const { return *Diags; }
433d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
4347c175fb196a2bc3dbc86ea3865c713e1875f3f6dChris Lattner
4354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &getLangOpts() const { return LangOpts; }
436998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  const TargetInfo &getTargetInfo() const { return *Target; }
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager &getFileManager() const { return FileMgr; }
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager &getSourceManager() const { return SourceMgr; }
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierTable &getIdentifierTable() { return Identifiers; }
44229238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable &getSelectorTable() { return Selectors; }
4431b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner  Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
44432fca722dd974b8202d0fb9c71b6c185c0767da6Chris Lattner  llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446337edcdbec05316b407d0d64865c88ff8597d910Ted Kremenek  void setPTHManager(PTHManager* pm);
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
448880dcf21dfdb3ff763c60195b6794bab0d913095Ted Kremenek  PTHManager *getPTHManager() { return PTH.get(); }
4492e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
45088a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  void setExternalSource(ExternalPreprocessorSource *Source) {
45188a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor    ExternalSource = Source;
45288a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  }
453a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
45488a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  ExternalPreprocessorSource *getExternalSource() const {
45588a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor    return ExternalSource;
45688a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  }
457a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
4586aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  /// \brief Retrieve the module loader associated with this preprocessor.
4596aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
460ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
4613b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis  bool hadModuleLoaderFatalFailure() const {
4623b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis    return TheModuleLoader.HadFatalFailure;
4633b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis  }
4643b7deda7137e62810a810ce25b062927a9fc7c71Argyrios Kyrtzidis
46531672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  /// \brief True if we are currently preprocessing a #if or #elif directive
46631672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  bool isParsingIfOrElifDirective() const {
46731672b1745195d709b641f1c0f44e203742fa73bAaron Ballman    return ParsingIfOrElifDirective;
46831672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  }
46931672b1745195d709b641f1c0f44e203742fa73bAaron Ballman
4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// SetCommentRetentionState - Control whether or not the preprocessor retains
4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// comments in output.
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepComments = KeepComments | KeepMacroComments;
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepMacroComments = KeepMacroComments;
4755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getCommentRetentionState() const { return KeepComments; }
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4796fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
4806fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose  bool getPragmasEnabled() const { return PragmasEnabled; }
4816fe6a49c4058211ff4489023c78615ec0266c5ffJordan Rose
482f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  void SetSuppressIncludeNotFoundError(bool Suppress) {
483f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman    SuppressIncludeNotFoundError = Suppress;
484f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  }
485f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman
486f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  bool GetSuppressIncludeNotFoundError() {
487f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman    return SuppressIncludeNotFoundError;
488f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman  }
489f84139a1331c63c998e8b7d54148c75ac0b48ccdEli Friedman
49074c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  /// Sets whether the preprocessor is responsible for producing output or if
49174c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  /// it is producing tokens to be consumed by Parse and Sema.
49274c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  void setPreprocessedOutput(bool IsPreprocessedOutput) {
49374c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose    PreprocessedOutput = IsPreprocessedOutput;
49474c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  }
49574c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose
49674c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  /// Returns true if the preprocessor is responsible for generating output,
49774c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  /// false if it is producing tokens to be consumed by Parse and Sema.
49874c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose  bool isPreprocessedOutput() const { return PreprocessedOutput; }
49974c2498bb9e339345ee32bdd095e76157cec3b86Jordan Rose
5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isCurrentLexer - Return true if we are lexing directly from the specified
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer.
5027062d9e9fc3ed89c4e4ea7055efea6585b7bac8dTed Kremenek  bool isCurrentLexer(const PreprocessorLexer *L) const {
5037062d9e9fc3ed89c4e4ea7055efea6585b7bac8dTed Kremenek    return CurPPLexer == L;
5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
506a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// getCurrentLexer - Return the current lexer being lexed from.  Note
507a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// that this ignores any potentially active macro expansions and _Pragma
508a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// expansions going on at the time.
509a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
510a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
511321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// getCurrentFileLexer - Return the current file lexer being lexed from.
512321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// Note that this ignores any potentially active macro expansions and _Pragma
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansions going on at the time.
51468e48e4a81767997ef0231e47eca4f665102c95eTed Kremenek  PreprocessorLexer *getCurrentFileLexer() const;
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51695d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis  /// \brief Returns the file ID for the preprocessor predefines.
5179484c1dcf084452228aae439edcd00e1b4ce7470Argyrios Kyrtzidis  FileID getPredefinesFileID() const { return PredefinesFileID; }
51895d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis
519a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  /// getPPCallbacks/addPPCallbacks - Accessors for preprocessor callbacks.
520eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// Note that this class takes ownership of any PPCallbacks object given to
521eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// it.
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *getPPCallbacks() const { return Callbacks; }
523a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  void addPPCallbacks(PPCallbacks *C) {
524e5393fb93eb879d9ebbef102ae9311fa77e023ccDaniel Dunbar    if (Callbacks)
525e5393fb93eb879d9ebbef102ae9311fa77e023ccDaniel Dunbar      C = new PPChainedCallbacks(C, Callbacks);
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Callbacks = C;
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  /// \brief Given an identifier, return its latest MacroDirective if it is
530c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  // \#defined or null if it isn't \#define'd.
5319818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  MacroDirective *getMacroDirective(IdentifierInfo *II) const {
532295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor    if (!II->hasMacroDefinition())
533295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor      return 0;
534ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
5359818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    MacroDirective *MD = getMacroDirectiveHistory(II);
536c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis    assert(MD->isDefined() && "Macro is undefined!");
5379818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    return MD;
5389818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  }
5399818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis
5409818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  const MacroInfo *getMacroInfo(IdentifierInfo *II) const {
5419818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    return const_cast<Preprocessor*>(this)->getMacroInfo(II);
5429818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  }
5439818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis
5449818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  MacroInfo *getMacroInfo(IdentifierInfo *II) {
5459818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    if (MacroDirective *MD = getMacroDirective(II))
546c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis      return MD->getMacroInfo();
5479818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis    return 0;
548cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  }
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5504d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko  /// \brief Given an identifier, return the (probably #undef'd) MacroInfo
5514d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko  /// representing the most recent macro definition. One can iterate over all
5524d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko  /// previous macro definitions from it. This method should only be called for
5534d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko  /// identifiers that hadMacroDefinition().
5549818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  MacroDirective *getMacroDirectiveHistory(const IdentifierInfo *II) const;
5554d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko
556c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  /// \brief Add a directive to the macro directive history for this identifier.
557c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD);
558c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI,
559c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis                                             SourceLocation Loc,
560c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis                                             bool isImported) {
561c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis    DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc, isImported);
562c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis    appendMacroDirective(II, MD);
5639317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis    return MD;
5649317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  }
565c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI){
566c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis    return appendDefMacroDirective(II, MI, MI->getDefinitionLoc(), false);
5679818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  }
5689317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  /// \brief Set a MacroDirective that was loaded from a PCH file.
5699317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *MD);
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5718a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// macro_iterator/macro_begin/macro_end - This allows you to walk the macro
5728a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// history table. Currently defined macros have
5738a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// IdentifierInfo::hasMacroDefinition() set and an empty
5748a64bb58c3b24d7d97895e435bbc0965c99bd3beAlexander Kornienko  /// MacroInfo::getUndefLoc() at the head of the list.
575b3958476b21702a15f64f09d2862506d03dffe7bDmitri Gribenko  typedef llvm::DenseMap<const IdentifierInfo *,
5769818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis                         MacroDirective*>::const_iterator macro_iterator;
57788a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
57888a35862fbe473f2a4f0c19f24dbe536937e1dc6Douglas Gregor  macro_iterator macro_end(bool IncludeExternalMacros = true) const;
579a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam
5801952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  /// \brief Return the name of the macro defined before \p Loc that has
5811952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  /// spelling \p Tokens.  If there are multiple macros with same spelling,
5821952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  /// return the last one defined.
5831952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  StringRef getLastMacroWithSpelling(SourceLocation Loc,
5841952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                     ArrayRef<TokenValue> Tokens) const;
5851952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko
586e3d5e3ae5bd8028774f07d7c3751d4db82118942Chris Lattner  const std::string &getPredefines() const { return Predefines; }
587aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// setPredefines - Set the predefines for this Preprocessor.  These
588aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// predefines are automatically injected when parsing the main file.
589aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const char *P) { Predefines = P; }
590aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const std::string &P) { Predefines = P; }
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59203e67fbf7a2646447972742b6bb97c82ce4698fbNico Weber  /// Return information about the specified preprocessor
59303e67fbf7a2646447972742b6bb97c82ce4698fbNico Weber  /// identifier token.
594686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  IdentifierInfo *getIdentifierInfo(StringRef Name) const {
5953da736c1143126be19b253804b3b135ebcd3d6ffDaniel Dunbar    return &Identifiers.get(Name);
5965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If 'Namespace' is non-null, then it is a token required to exist on the
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
601686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
6029b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  void AddPragmaHandler(PragmaHandler *Handler) {
603686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    AddPragmaHandler(StringRef(), Handler);
6049b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  }
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6064095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// RemovePragmaHandler - Remove the specific pragma handler from
6071824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// the preprocessor. If \p Namespace is non-null, then it should
6081824d54df85a462ada812dadda18130f951d40f3Dmitri Gribenko  /// be the namespace that \p Handler was added to. It is an error
6094095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar  /// to remove a handler that has not been registered.
610686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
6119b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  void RemovePragmaHandler(PragmaHandler *Handler) {
612686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    RemovePragmaHandler(StringRef(), Handler);
6139b36c3f0de0105e903130bbda3c4aea7d792c0afArgyrios Kyrtzidis  }
6144095080aff204008eefb26b100906c6ca2bc4bb6Daniel Dunbar
6152e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// \brief Add the specified comment handler to the preprocessor.
616aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  void addCommentHandler(CommentHandler *Handler);
6171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6182e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// \brief Remove the specified comment handler.
6192e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  ///
6202e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  /// It is an error to remove a handler that has not been registered.
621aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  void removeCommentHandler(CommentHandler *Handler);
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief Set the code completion handler to the given object.
624f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
625f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    CodeComplete = &Handler;
626f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
627ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
6281fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  /// \brief Retrieve the current code-completion handler.
6291fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  CodeCompletionHandler *getCodeCompletionHandler() const {
6301fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor    return CodeComplete;
6311fbb447e9d43c2c676e94081fbfee7eb6cbe933bDouglas Gregor  }
632ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
633f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  /// \brief Clear out the code completion handler.
634f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  void clearCodeCompletionHandler() {
635f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor    CodeComplete = 0;
636f44e854ed1e3aa86d2ed6d615ccd109d50ddcff9Douglas Gregor  }
637ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
63855817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  /// \brief Hook used by the lexer to invoke the "natural language" code
63955817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  /// completion point.
64055817afdf9d453a443262a733f6caf6692dca118Douglas Gregor  void CodeCompleteNaturalLanguage();
641ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
64294dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// \brief Retrieve the preprocessing record, or NULL if there is no
64394dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// preprocessing record.
644b9e1b75772db2c7db566c6034ba90a07f22e35ebDouglas Gregor  PreprocessingRecord *getPreprocessingRecord() const { return Record; }
645ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
646ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  /// \brief Create a new preprocessing record, which will keep track of
64794dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  /// all macro expansions, macro definitions, etc.
64837ed12720a35b7bfa1c4de73ad6f1c6c1c88ee17Argyrios Kyrtzidis  void createPreprocessingRecord();
649ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
65053b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
6517dcc968f17a6ff9088c9651dddccc8d4025a1271Ted Kremenek  /// which implicitly adds the builtin defines etc.
652e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  void EnterMainSourceFile();
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  /// EndSourceFile - Inform the preprocessor callbacks that processing is
655dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  /// complete.
656dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar  void EndSourceFile();
657dbd8209b33e6c9f151e4913a9c095d64a95439c4Daniel Dunbar
6585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFile - Add a source file to the top of the include stack and
659e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  /// start lexing tokens from it instead of the current buffer.  Emit an error
660e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  /// and don't enter the file on error.
661e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner  void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
662e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner                       SourceLocation Loc);
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens from it instead of the current buffer.  Args specifies the
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens input to a function-like macro.
667e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  ///
668e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// ILEnd specifies the location of the ')' for a function-like macro or the
669e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  /// identifier for an object-like macro.
670c30981a563a8947cb26b1e308d122fa2ef90fcebRichard Smith  void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroInfo *Macro,
671c30981a563a8947cb26b1e308d122fa2ef90fcebRichard Smith                  MacroArgs *Args);
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
6746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// which will cause the lexer to start returning the specified tokens.
6756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
6766b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
6776b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// not be subject to further macro expansion.  Otherwise, these tokens will
6786b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// be re-macro-expanded when/if expansion is enabled.
6796b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
6806b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If OwnsTokens is false, this method assumes that the specified stream of
6816b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// tokens has a permanent owner somewhere, so they do not need to be copied.
6826b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If it is true, it assumes the array of tokens is allocated with new[] and
6836b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// must be freed.
6846b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
6856b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  void EnterTokenStream(const Token *Toks, unsigned NumToks,
6866b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner                        bool DisableMacroExpansion, bool OwnsTokens);
6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
6895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer stack.  This should only be used in situations where the current
6905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// state of the top-of-stack lexer is known.
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RemoveTopOfLexerStack();
69203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
69303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// EnableBacktrackAtThisPos - From the point that this method is called, and
694ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
695ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
696ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// make the Preprocessor re-lex the same tokens.
69703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
698a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
699ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
700ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
70103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
702ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
703ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
704ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// tokens will continue indefinitely.
70503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  ///
706a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  void EnableBacktrackAtThisPos();
70703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
708ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
709ed5c38682c056c147c8a4abb748b4f285de206ddArgyrios Kyrtzidis  void CommitBacktrackedTokens();
71003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
71103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// EnableBacktrackAtThisPos() was previously called.
713a9e274c01ebae45629d93aaa07be450fb77dd3cbArgyrios Kyrtzidis  void Backtrack();
71403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
71503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
71603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  /// caching of tokens is on.
717fad03b2b38a3baea4b67e79e676fee15078e3258Argyrios Kyrtzidis  bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
71803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Lex - To lex a token from the preprocessor, just pull a token from the
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// current lexer or macro object.
721d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Lex(Token &Result) {
722b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    switch (CurLexerKind) {
723b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_Lexer: CurLexer->Lex(Result); break;
724b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_PTHLexer: CurPTHLexer->Lex(Result); break;
725b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_TokenLexer: CurTokenLexer->Lex(Result); break;
726b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_CachingLexer: CachingLex(Result); break;
727b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    case CLK_LexAfterModuleImport: LexAfterModuleImport(Result); break;
728b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    }
7295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
731b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor  void LexAfterModuleImport(Token &Result);
732ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
73302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  /// \brief Lex a string literal, which may be the concatenation of multiple
73402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  /// string literals and may even come from macro expansion.
73502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  /// \returns true on success, false if a error diagnostic has been generated.
73602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  bool LexStringLiteral(Token &Result, std::string &String,
73797f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs                        const char *DiagnosticTag, bool AllowMacroExpansion) {
73802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs    if (AllowMacroExpansion)
73902a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      Lex(Result);
74002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs    else
74102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      LexUnexpandedToken(Result);
74297f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs    return FinishLexStringLiteral(Result, String, DiagnosticTag,
74397f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs                                  AllowMacroExpansion);
74402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  }
74502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs
74602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  /// \brief Complete the lexing of a string literal where the first token has
74702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  /// already been lexed (see LexStringLiteral).
74802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs  bool FinishLexStringLiteral(Token &Result, std::string &String,
74997f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs                              const char *DiagnosticTag,
75002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs                              bool AllowMacroExpansion);
75102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// something not a comment.  This is useful in -E -C mode where comments
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// would foul up preprocessor directive handling.
755d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexNonComment(Token &Result) {
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    do
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Result);
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (Result.getKind() == tok::comment);
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
760d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexUnexpandedToken - This is just like Lex, but this disables macro
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansion of identifier tokens.
763d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexUnexpandedToken(Token &Result) {
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Disable macro expansion.
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    bool OldVal = DisableMacroExpansion;
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = true;
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Lex the token.
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Lex(Result);
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Reenable it.
7715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = OldVal;
7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77488710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
77588710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  /// expansion of identifier tokens.
77688710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  void LexUnexpandedNonComment(Token &Result) {
77788710f22be1acb04b7d812aebe87bea56394c07cEli Friedman    do
77888710f22be1acb04b7d812aebe87bea56394c07cEli Friedman      LexUnexpandedToken(Result);
77988710f22be1acb04b7d812aebe87bea56394c07cEli Friedman    while (Result.getKind() == tok::comment);
78088710f22be1acb04b7d812aebe87bea56394c07cEli Friedman  }
78188710f22be1acb04b7d812aebe87bea56394c07cEli Friedman
7828c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  /// Disables macro expansion everywhere except for preprocessor directives.
7838c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  void SetMacroExpansionOnlyInDirectives() {
7848c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie    DisableMacroExpansion = true;
7858c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie    MacroExpansionInDirectivesOverride = true;
7868c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie  }
7878c0b3787e7ccc7978b42dfbb84da2b802c743a5dDavid Blaikie
7886b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// LookAhead - This peeks ahead N tokens and returns that token without
7896b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// consuming any tokens.  LookAhead(0) returns the next token that would be
7906b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
7916b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returns normal tokens after phase 5.  As such, it is equivalent to using
7926b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// 'Lex', not 'LexUnexpandedToken'.
79303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  const Token &LookAhead(unsigned N) {
79403db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    if (CachedLexPos + N < CachedTokens.size())
79503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      return CachedTokens[CachedLexPos+N];
79603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    else
79703db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      return PeekAhead(N+1);
7989e0ed0bd5a3a7bac73973980ff32132a7724e674Argyrios Kyrtzidis  }
7993604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis
80008b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// RevertCachedTokens - When backtracking is enabled and tokens are cached,
80108b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// this allows to revert a specific number of tokens.
80208b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// Note that the number of tokens being reverted should be up to the last
80308b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  /// backtrack position, not more.
80408b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  void RevertCachedTokens(unsigned N) {
80508b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(isBacktrackEnabled() &&
80608b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis           "Should only be called when tokens are cached for backtracking");
80708b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
80808b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis         && "Should revert tokens up to the last backtrack position, not more");
80908b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    assert(signed(CachedLexPos) - signed(N) >= 0 &&
81008b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis           "Corrupted backtrack positions ?");
81108b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis    CachedLexPos -= N;
81208b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis  }
81308b2c3743a29a2dddcf72e95f747760e213cdde7Argyrios Kyrtzidis
8143604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// EnterToken - Enters a token in the token stream to be lexed next. If
8153604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// BackTrack() is called afterwards, the token will remain at the insertion
8163604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// point.
8173604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void EnterToken(const Token &Tok) {
8183604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis    EnterCachingLexMode();
8193604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis    CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
8203604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  }
8213604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis
8223604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching
8233604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// tokens (because backtrack is enabled) it should replace the most recent
8243604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// cached tokens with the given annotation token. This function has no effect
8253604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// if backtracking is not enabled.
8263604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  ///
8273604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// Note that the use of this function is just for optimization; so that the
8283604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
8293604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  /// invoked.
8303604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void AnnotateCachedTokens(const Token &Tok) {
83147246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner    assert(Tok.isAnnotation() && "Expected annotation token");
83283cf05a3b0e655dc8ea1cb4c4e1eef541b770992Chris Lattner    if (CachedLexPos != 0 && isBacktrackEnabled())
8333604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis      AnnotatePreviousCachedTokens(Tok);
8343604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  }
83539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
836440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  /// Get the location of the last cached token, suitable for setting the end
837440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  /// location of an annotation token.
838440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  SourceLocation getLastCachedTokenLocation() const {
839440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    assert(CachedLexPos != 0);
840440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith    return CachedTokens[CachedLexPos-1].getLocation();
841440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith  }
842440d456c5cf9613a3ee6a3297f892ddd8da5b8f8Richard Smith
8431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// \brief Replace the last token with an annotation token.
84439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  ///
84539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// Like AnnotateCachedTokens(), this routine replaces an
84639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// already-parsed (and resolved) token with an annotation
84739a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// token. However, this routine only replaces the last token with
84839a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// the annotation token; it does not affect any other cached
84939a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// tokens. This function has no effect if backtracking is not
85039a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  /// enabled.
85139a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  void ReplaceLastTokenWithAnnotation(const Token &Tok) {
85239a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    assert(Tok.isAnnotation() && "Expected annotation token");
85339a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor    if (CachedLexPos != 0 && isBacktrackEnabled())
85439a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor      CachedTokens[CachedLexPos-1] = Tok;
85539a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor  }
85639a8de10c18365bde7062d8959b7ed525449c561Douglas Gregor
8570576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  /// TypoCorrectToken - Update the current token to represent the provided
8580576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  /// identifier, in order to cache an action performed by typo correction.
8590576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  void TypoCorrectToken(const Token &Tok) {
8600576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    assert(Tok.getIdentifierInfo() && "Expected identifier token");
8610576681bac125be07f77f66b02a3dba2c3a24557Richard Smith    if (CachedLexPos != 0 && isBacktrackEnabled())
8620576681bac125be07f77f66b02a3dba2c3a24557Richard Smith      CachedTokens[CachedLexPos-1] = Tok;
8630576681bac125be07f77f66b02a3dba2c3a24557Richard Smith  }
8640576681bac125be07f77f66b02a3dba2c3a24557Richard Smith
865d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
866d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  /// CurTokenLexer pointers.
867d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor  void recomputeCurLexerKind();
868e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
869e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// \brief Returns true if incremental processing is enabled
870e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
871e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann
872e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  /// \brief Enables the incremental processing
873e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  void enableIncrementalProcessing(bool value = true) {
874e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann    IncrementalProcessing = value;
875e55329d6834647ba0e06f8a319e5d84c77310035Axel Naumann  }
876d6aba06861c41ccbc4926e5fe3cecd97b20410c0Douglas Gregor
8772968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \brief Specify the point at which code-completion will be performed.
8782968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
8792968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param File the file in which code completion should occur. If
8802968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// this file is included multiple times, code-completion will
8812968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// perform completion the first time it is included. If NULL, this
8822968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// function clears out the code-completion point.
8832968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
8842968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Line the line at which code completion should occur
8852968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// (1-based).
8862968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
8872968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \param Column the column at which code completion should occur
8882968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// (1-based).
8892968442603b029949246467253eeac8139a5b6d8Douglas Gregor  ///
8902968442603b029949246467253eeac8139a5b6d8Douglas Gregor  /// \returns true if an error occurred, false otherwise.
891a5d10c4df435964600e104ebef6a96b106e416b7Kovarththanan Rajaratnam  bool SetCodeCompletionPoint(const FileEntry *File,
8922968442603b029949246467253eeac8139a5b6d8Douglas Gregor                              unsigned Line, unsigned Column);
8932968442603b029949246467253eeac8139a5b6d8Douglas Gregor
8943437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis  /// \brief Determine if we are performing code completion.
8953437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis  bool isCodeCompletionEnabled() const { return CodeCompletionFile != 0; }
8963437f1f1294499d4ef306c1089fcb3e29ec2aa68Argyrios Kyrtzidis
8977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns the location of the code-completion point.
8987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// Returns an invalid location if code-completion is not enabled or the file
8997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// containing the code-completion point has not been lexed yet.
9007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
9017d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
9027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns the start location of the file of code-completion point.
9037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// Returns an invalid location if code-completion is not enabled or the file
9047d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// containing the code-completion point has not been lexed yet.
9057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  SourceLocation getCodeCompletionFileLoc() const {
9067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return CodeCompletionFileLoc;
9077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  }
9087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
9097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Returns true if code-completion is enabled and we have hit the
9107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// code-completion point.
9117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  bool isCodeCompletionReached() const { return CodeCompletionReached; }
9127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
9137d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis  /// \brief Note that we hit the code-completion point.
914d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  void setCodeCompletionReached() {
915d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
916d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    CodeCompletionReached = true;
917d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    // Silence any diagnostics that occur after we hit the code-completion.
918d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    getDiagnostics().setSuppressAllDiagnostics(true);
919d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  }
9207d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis
921809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief The location of the currently-active \#pragma clang
9228dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// arc_cf_code_audited begin.  Returns an invalid location if there
9238dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// is no such pragma active.
9248dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  SourceLocation getPragmaARCCFCodeAuditedLoc() const {
9258dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    return PragmaARCCFCodeAuditedLoc;
9268dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  }
9278dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
928809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Set the location of the currently-active \#pragma clang
9298dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  /// arc_cf_code_audited begin.  An invalid location ends the pragma.
9308dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
9318dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall    PragmaARCCFCodeAuditedLoc = Loc;
9328dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall  }
9338dfac0baaf0f81d3945bcb306480e358ba8d1f08John McCall
934809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Instruct the preprocessor to skip part of the main source file.
935f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
936809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \param Bytes The number of bytes in the preamble to skip.
937f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  ///
938809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \param StartOfLine Whether skipping these bytes puts the lexer at the
939f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  /// start of a line.
940ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
941f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    SkipMainFilePreamble.first = Bytes;
942f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor    SkipMainFilePreamble.second = StartOfLine;
943f4f6c9db68465b886ec2e596feaa6ecc782395a4Douglas Gregor  }
944ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
946d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  /// the specified Token's location, translating the token's start
9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// position in the current buffer into a SourcePosition object for rendering.
948d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
949d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    return Diags->Report(Loc, DiagID);
950d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  }
9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
952d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
953d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer    return Diags->Report(Tok.getLocation(), DiagID);
954d7a3e2c5f61cd4893f95b69a424fe4def3aa0f69Benjamin Kramer  }
9551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
956834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// getSpelling() - Return the 'spelling' of the token at the given
957834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// location; does not go up to the spelling location or down to the
9589e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// expansion location.
95930900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  ///
960834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  /// \param buffer A buffer which will be used only if the token requires
961834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
96230900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  /// \param invalid If non-null, will be set \c true if an error occurs.
963686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getSpelling(SourceLocation loc,
964fd61d6fe0aa9853f0577ca88a63901c3773e8101Nick Lewycky                        SmallVectorImpl<char> &buffer,
965fd61d6fe0aa9853f0577ca88a63901c3773e8101Nick Lewycky                        bool *invalid = 0) const {
9664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
967834e3f6c77d9ac03997a3f0c56934edcf406a355John McCall  }
96830900dafbe972f4802c2663262b4393edab0d1cdJohn McCall
9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
9705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// token is the characters used to represent the token in the source file
9715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// after trigraph expansion and escaped-newline folding.  In particular, this
9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// wants to get the true, uncanonicalized, spelling of things like digraphs
9735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// UCNs, etc.
97450f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor  ///
97530900dafbe972f4802c2663262b4393edab0d1cdJohn McCall  /// \param Invalid If non-null, will be set \c true if an error occurs.
976b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
9774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
978b0607279cb98bbf2bbfe0db170aed39ef91e86a2Chris Lattner  }
9790ff1042ddaad1419264be0de6da17f3b378482a4Daniel Dunbar
9805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling - This method is used to get the spelling of a token into a
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// preallocated buffer, instead of as an std::string.  The caller is required
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to allocate enough space for the token, which is guaranteed to be at least
9835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Tok.getLength() bytes long.  The length of the actual result is returned.
9845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
9855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Note that this method may do two possible things: it may either fill in
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the buffer specified with characters, or it may *change the input pointer*
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to point to a constant buffer with the data already in it (avoiding a
9885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// copy).  The caller is not allowed to modify the returned buffer pointer
9895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// if an internal buffer is returned.
990ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  unsigned getSpelling(const Token &Tok, const char *&Buffer,
99148cf9824fbad42995f4d91d59d08d2620effd683Chris Lattner                       bool *Invalid = 0) const {
9924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
99348cf9824fbad42995f4d91d59d08d2620effd683Chris Lattner  }
994b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor
995ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// getSpelling - This method is used to get the spelling of a token into a
996ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// SmallVector. Note that the returned StringRef may not point to the
997ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer  /// supplied buffer if a copy can be avoided.
998686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getSpelling(const Token &Tok,
999f4fb07ed4dfd144a39d904fc77b3b0bab580eb2eNico Weber                        SmallVectorImpl<char> &Buffer,
1000f4fb07ed4dfd144a39d904fc77b3b0bab580eb2eNico Weber                        bool *Invalid = 0) const;
1001ddeea5644367c9c153c9fee9e51bdea85ce43cbdBenjamin Kramer
1002d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  /// \brief Relex the token at the specified location.
1003d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  /// \returns true if there was a failure, false on success.
1004d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  bool getRawToken(SourceLocation Loc, Token &Result) {
1005d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis    return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts);
1006d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis  }
1007d93335c43fd462145fee3ea8f4d84d430577c821Argyrios Kyrtzidis
10087216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner  /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
10097216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner  /// with length 1, return the character.
1010ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  char getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
101150f6af7a6d6951a63f3da7d4c5a7d3965bf73b63Douglas Gregor                                                   bool *Invalid = 0) const {
10127216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    assert(Tok.is(tok::numeric_constant) &&
10137216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner           Tok.getLength() == 1 && "Called on unsupported token");
10147216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
10157216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner
10167216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // If the token is carrying a literal data pointer, just use it.
10177216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    if (const char *D = Tok.getLiteralData())
10187216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner      return *D;
101928396608ec20d44e9d1470e1ea51689bb504d0deTed Kremenek
10207216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // Otherwise, fall back on getCharacterData, which is slower, but always
10217216dc9cb49f47254595120cf15a737cee53f0bdChris Lattner    // works.
1022a543016fe07030f695d6d56fd22c8c8da617e0d7Douglas Gregor    return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
102328396608ec20d44e9d1470e1ea51689bb504d0deTed Kremenek  }
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1025c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// \brief Retrieve the name of the immediate macro expansion.
1026c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  ///
1027c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// This routine starts from a source location, and finds the name of the macro
1028c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// responsible for its immediate expansion. It looks through any intervening
1029c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// macro argument expansions to compute this. It returns a StringRef which
1030c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// refers to the SourceManager-owned buffer of the source where that macro
1031c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  /// name is spelled. Thus, the result shouldn't out-live the SourceManager.
1032c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  StringRef getImmediateMacroName(SourceLocation Loc) {
10334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
1034c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks  }
1035c2a8d6cee01fc4845f5409bf5c021a64616ac8c3Anna Zaks
103647246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  /// CreateString - Plop the specified string into a scratch buffer and set the
103747246be8ac5b0ddde6c402b8fc6946b6135487b5Chris Lattner  /// specified token's location and length to it.  If specified, the source
10389e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// location provides a location of the expansion point of the token.
1039374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko  void CreateString(StringRef Str, Token &Tok,
1040a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                    SourceLocation ExpansionLocStart = SourceLocation(),
1041a08529cc3f00e0b47a3c028823634129ac46847bAbramo Bagnara                    SourceLocation ExpansionLocEnd = SourceLocation());
1042b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor
1043b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// \brief Computes the source location just past the end of the
1044b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// token at this source location.
1045b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  ///
1046b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// This routine can be used to produce a source location that
1047b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// points just past the end of the token referenced by \p Loc, and
1048b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// is generally used when a diagnostic needs to point just after a
1049b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// token where it expected something different that it received. If
1050b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// the returned source location would not be meaningful (e.g., if
1051b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// it points into a macro), this routine returns an invalid
1052b2fb6de9070fea9abc56c8e8d5469066e964cefeDouglas Gregor  /// source location.
105333e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  ///
105433e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// \param Offset an offset from the end of the token, where the source
105533e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// location should refer to. The default offset (0) produces a source
105633e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// location pointing just past the end of the token; an offset of 1 produces
105733e9abd21083a0191a7676a04b497006d2da184dDouglas Gregor  /// a source location pointing to the last character in the token, etc.
10587ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
10594e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
10607ef5c27eb6e8ebe58b52013246c06753c3613263Chris Lattner  }
10611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10627a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the first
10639e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// token of the macro expansion.
106469bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
106569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// \param MacroBegin If non-null and function returns true, it is set to
106669bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// begin location of the macro.
106769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  bool isAtStartOfMacroExpansion(SourceLocation loc,
106869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                 SourceLocation *MacroBegin = 0) const {
10694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
107069bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                                            MacroBegin);
10717a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  }
10727a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
10737a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  /// \brief Returns true if the given MacroID location points at the last
10749e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth  /// token of the macro expansion.
107569bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  ///
1076ad0fe03b897f9486191e75c8d90c3ffa9b4fd6a5Ted Kremenek  /// \param MacroEnd If non-null and function returns true, it is set to
107769bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  /// end location of the macro.
107869bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis  bool isAtEndOfMacroExpansion(SourceLocation loc,
107969bda4c027671df7163619f215209529eb236620Argyrios Kyrtzidis                               SourceLocation *MacroEnd = 0) const {
10804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
10817a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis  }
10827a759606d93975866051f67104ae58446e55f404Argyrios Kyrtzidis
10835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// DumpToken - Print the token to stderr, used for debugging.
10845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
1085d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
1086c3d8d57b010e2ed15a2a7685d5761db14f5d2252Chris Lattner  void DumpLocation(SourceLocation Loc) const;
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void DumpMacro(const MacroInfo &MI) const;
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108997ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
109097ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// token, return a new location that specifies a character within the token.
1091872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
1092872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner                                         unsigned Char) const {
10934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
1094872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner  }
1095872a45e91778eb0b706ff57272fe547d4512eb19Chris Lattner
10965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncrementPasteCounter - Increment the counters for the number of token
10975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// paste operations performed.  If fast was specified, this is a 'fast paste'
10985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// case we handled.
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ///
11005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void IncrementPasteCounter(bool isFast) {
11015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (isFast)
11025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumFastTokenPaste;
11035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else
11045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumTokenPaste;
11055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
11061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats();
11085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1109c5c5e92ec53f7e6ac7ebbbf77c6d8e4b7d88daecArgyrios Kyrtzidis  size_t getTotalMemory() const;
1110c5c5e92ec53f7e6ac7ebbbf77c6d8e4b7d88daecArgyrios Kyrtzidis
11113f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
11123f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// comment (/##/) in microsoft mode, this method handles updating the current
11133f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// state, returning the token on the next source line.
11143f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  void HandleMicrosoftCommentPaste(Token &Tok);
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
11175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Preprocessor callback methods.  These are invoked by a lexer as various
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // directives and events are found.
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1120c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
1121c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// identifier information for the token and install it into the token,
1122c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  /// updating the token kind accordingly.
1123c4bf2b9afb7d47445a9dc6bc848657098a4e3851Abramo Bagnara  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
112528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
112628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
112728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
112828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
112928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
113028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // SetPoisonReason - Call this function to indicate the reason for
113128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // poisoning an identifier. If that identifier is accessed while
113228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // poisoned, then this reason will be used instead of the default
113328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // "poisoned" diagnostic.
113428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
113528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
113628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // HandlePoisonedIdentifier - Display reason for poisoned
113728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  // identifier.
113828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void HandlePoisonedIdentifier(Token & Tok);
113928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
114028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void MaybeHandlePoisonedIdentifier(Token & Identifier) {
114128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
114228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      if(II->isPoisoned()) {
114328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley        HandlePoisonedIdentifier(Identifier);
114428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      }
114528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    }
114628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
114728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
114828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyprivate:
114928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  /// Identifiers used for SEH handling in Borland. These are only
115028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  /// allowed in particular circumstances
1151ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __except block
1152ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__exception_code,
1153ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___exception_code,
1154ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_GetExceptionCode;
1155ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __except filter expression
1156ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__exception_info,
1157ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___exception_info,
1158ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_GetExceptionInfo;
1159ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  // __finally
1160ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie  IdentifierInfo *Ident__abnormal_termination,
1161ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident___abnormal_termination,
1162ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie                 *Ident_AbnormalTermination;
116328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
116428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  void PoisonSEHIdentifiers(bool Poison = true); // Borland
116528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
11665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleIdentifier - This callback is invoked when the lexer reads an
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier and has filled in the tokens IdentifierInfo member.  This
11685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// callback potentially macro expands it or turns it into a named token (like
11695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// 'for').
1170d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentifier(Token &Identifier);
11715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
11745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the current file.  This either returns the EOF token and returns true, or
11755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pops a level off the include stack and returns false, at which point the
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// client should call lex again.
1177d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1179fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// HandleEndOfTokenLexer - This callback is invoked when the current
1180fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// TokenLexer hits the end of its token stream.
1181fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  bool HandleEndOfTokenLexer(Token &Result);
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleDirective - This callback is invoked when the lexer sees a # token
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// at the start of a line.  This consumes the directive, modifies the
11855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
11865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// read is the correct one.
1187d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleDirective(Token &Result);
11885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
118984021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// CheckEndOfDirective - Ensure that the next token is a tok::eod token.  If
119084021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// not, emit a diagnostic and consume up until the eod.  If EnableMacros is
1191ab82f41b217ce588a9456c0b4411f219d3ed0df8Chris Lattner  /// true, then we consider macros that expand to zero tokens as being ok.
1192ab82f41b217ce588a9456c0b4411f219d3ed0df8Chris Lattner  void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1194478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
119584021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// current line until the tok::eod token is found.
1196478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  void DiscardUntilEndOfDirective();
11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11987c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  /// SawDateOrTime - This returns true if the preprocessor has seen a use of
11997c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  /// __DATE__ or __TIME__ in the file so far.
12007c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  bool SawDateOrTime() const {
12017c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner    return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
12027c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  }
1203c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  unsigned getCounterValue() const { return CounterValue; }
1204c1f9d828c733ec1eba06d01070735d1f36fda733Chris Lattner  void setCounterValue(unsigned V) { CounterValue = V; }
12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120690db26000aefe9335370013eec64c85232d80227Douglas Gregor  /// \brief Retrieves the module that we're currently building, if any.
120790db26000aefe9335370013eec64c85232d80227Douglas Gregor  Module *getCurrentModule();
120890db26000aefe9335370013eec64c85232d80227Douglas Gregor
120958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Allocate a new MacroInfo object with the provided SourceLocation.
1210f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *AllocateMacroInfo(SourceLocation L);
1211f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
1212baa74bd3968028d8e5b10ee9b50d0dceb41e85a9Argyrios Kyrtzidis  /// \brief Allocate a new MacroInfo object loaded from an AST file.
1213baa74bd3968028d8e5b10ee9b50d0dceb41e85a9Argyrios Kyrtzidis  MacroInfo *AllocateDeserializedMacroInfo(SourceLocation L,
1214baa74bd3968028d8e5b10ee9b50d0dceb41e85a9Argyrios Kyrtzidis                                           unsigned SubModuleID);
1215baa74bd3968028d8e5b10ee9b50d0dceb41e85a9Argyrios Kyrtzidis
121658bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Turn the specified lexer token into a fully checked and spelled
121758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// filename, e.g. as an operand of \#include.
121858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
121958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// The caller is expected to provide a buffer that is large enough to hold
122058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// the spelling of the filename, but is also expected to handle the case
122158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// when this method decides to use a different buffer.
122258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
122358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \returns true if the input filename was in <>'s or false if it was
122458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// in ""'s.
1225686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1226a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
122758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Given a "foo" or \<foo> reference, look up the indicated file.
122858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
122958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// Returns null on failure.  \p isAngled indicates whether the file
123058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// reference is for system \#include's or not (i.e. using <> instead of "").
1231bc3f628815b3841dc99109e7f67f9afa7793bc94Lawrence Crowl  const FileEntry *LookupFile(SourceLocation FilenameLoc, StringRef Filename,
1232f45b646244705410866d62f1d8bf017a047ed662Chris Lattner                              bool isAngled, const DirectoryLookup *FromDir,
1233b5142bb7af5c70fffd09f05172a1379a35a9c29aChandler Carruth                              const DirectoryLookup *&CurDir,
1234686775deca8b8685eb90801495880e3abdd844c2Chris Lattner                              SmallVectorImpl<char> *SearchPath,
1235fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor                              SmallVectorImpl<char> *RelativePath,
1236bc3f628815b3841dc99109e7f67f9afa7793bc94Lawrence Crowl                              ModuleMap::KnownHeader *SuggestedModule,
12371c2e9332fa69727425a3a2b912e36e2ab62083f8Douglas Gregor                              bool SkipCache = false);
1238a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
1239a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// GetCurLookup - The DirectoryLookup structure used to find the current
1240a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
124158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// implement \#include_next and find directory-specific properties.
1242a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1243a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
124458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Return true if we're in the top-level file, not in a \#include.
1245a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  bool isInPrimaryFile() const;
1246a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
124758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// ConcatenateIncludeName - Handle cases where the \#include name is expanded
1248a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// from a macro as multiple tokens, which need to be glued together.  This
1249a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  /// occurs for code like:
125058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \code
125170517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  ///    \#define FOO <x/y.h>
125258bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///    \#include FOO
125358bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \endcode
125470517ca5c07c4b41ff8662b94ee22047b0299f8cDmitri Gribenko  /// because in this case, "<x/y.h>" is returned as 7 tokens, not one.
1255a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson  ///
1256321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// This code concatenates and consumes tokens up to the '>' token.  It
1257321c2688198f6f1a4086964e505f239683423cecChris Lattner  /// returns false if the > was found, otherwise it returns true if it finds
125884021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// and consumes the EOD marker.
1259f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1260ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor                              SourceLocation &End);
1261a28cc090f265d5bdaef9ebb24fb9d579928d3a18John Thompson
12629d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne  /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is
126384021556baceb76eedf7d44be8ba71d9b8cfaccePeter Collingbourne  /// followed by EOD.  Return true if the token is not a valid on-off-switch.
12649d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne  bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
12659d3f5f7550a2fab4178ed01425758c349b73a609Peter Collingbourne
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
12671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1268d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  void PushIncludeMacroStack() {
1269b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind,
1270b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor                                                 CurLexer.take(),
12719c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurPTHLexer.take(),
12729c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurPPLexer,
12739c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurTokenLexer.take(),
12749c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek                                                 CurDirLookup));
1275f89963b1e819d5e73796a4f9d7cd70129846e2ecTed Kremenek    CurPPLexer = 0;
1276d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  }
12771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1278d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  void PopIncludeMacroStack() {
1279caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek    CurLexer.reset(IncludeMacroStack.back().TheLexer);
12809c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer);
12819c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1282caaa7df2c78bbd40197823034c0275f3dcbd63e7Ted Kremenek    CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
12839c1b750c59d510e6c9eccb1f37bccc46ccfe6844Ted Kremenek    CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1284b8db7cd9ac05c522855631670ec2e97255384f5aDouglas Gregor    CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1285d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek    IncludeMacroStack.pop_back();
1286d038def6e3f33bebf8c12bb3a95b2492c154a5ddTed Kremenek  }
12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Allocate a new MacroInfo object.
1289f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  MacroInfo *AllocateMacroInfo();
1290f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
1291c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI,
1292c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis                                               SourceLocation Loc,
1293c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis                                               bool isImported);
1294c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc);
1295c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc,
1296c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis                                                             bool isPublic);
12979818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis
129858bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Release the specified MacroInfo for re-use.
129958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  ///
130058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// This memory will  be reused for allocating new MacroInfo objects.
13010301b3ff132a4d986c092d161cb77d74b04cd2a6Chris Lattner  void ReleaseMacroInfo(MacroInfo* MI);
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroName - Lex and validate a macro name, which occurs after a
130458bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \#define or \#undef.  This emits a diagnostic, sets the token kind to eod,
13055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// and discards the rest of the macro line if the macro name is invalid.
1306d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
13095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// definition has just been read.  Lex the rest of the arguments and the
1310e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  /// closing ), updating MI with what we learn and saving in LastTok the
1311e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  /// last token read.
1312e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  /// Return true if an error occurs parsing the arg list.
1313e2e8768c290ceceb3ac2e369b5243f40d6c0e3e2Abramo Bagnara  bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok);
13141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
131558bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// We just read a \#if or related directive and decided that the
131658bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// subsequent tokens are in the \#if'd out portion of the
131758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// file.  Lex the rest of the file, until we see an \#endif.  If \p
13185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FoundNonSkipPortion is true, then we have already emitted code for part of
131958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// this \#if directive, so \#else/\#elif blocks should never be entered. If
132058bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \p FoundElse is false, then \#else directives are ok, if not, then we have
132158bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// already seen one so a \#else directive is a duplicate.  When this returns,
13225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the caller can lex the first valid token.
13235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
13246b4ff04dd22e7159887ea6c1bb8b1c01fc9c70a9Argyrios Kyrtzidis                                    bool FoundNonSkipPortion, bool FoundElse,
13256b4ff04dd22e7159887ea6c1bb8b1c01fc9c70a9Argyrios Kyrtzidis                                    SourceLocation ElseLoc = SourceLocation());
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
132758bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief A fast PTH version of SkipExcludedConditionalBlock.
1328268ee7016a2811803989487c0ad3799486092c63Ted Kremenek  void PTHSkipExcludedConditionalBlock();
13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
13315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// may occur after a #if or #elif directive and return it as a bool.  If the
13325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
13335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
13341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
133658bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \#pragma GCC poison/system_header/dependency and \#pragma once.
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinPragmas();
13381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
133958bf98725b6d4588338e191d2ab981b104471dabJames Dennett  /// \brief Register builtin macros such as __LINE__ with the identifier table.
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinMacros();
13411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
13445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the macro should not be expanded return true, otherwise return false.
1345c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis  bool HandleMacroExpandedIdentifier(Token &Tok, MacroDirective *MD);
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13475b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// \brief Cache macro expanded tokens for TokenLexers.
13485b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  //
13495b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
13505b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// going to lex in the cache and when it finishes the tokens are removed
13515b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  /// from the end of the cache.
13525b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
13532d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                                  ArrayRef<Token> tokens);
13545b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  void removeCachedMacroExpandedTokensOfLastLexer();
13555b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis  friend void TokenLexer::ExpandFunctionArguments();
13565b3284a9275a27f5c4410e25eb8933be540601d6Argyrios Kyrtzidis
13575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexed is a '('.  If so, consume the token and return true, if not, this
13595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// method should have no observable side-effect on the lexed tokens.
13605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isNextPPTokenLParen();
13611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
13635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invoked to read all of the formal arguments specified for the macro
13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invocation.  This returns null on error.
1365e7fb48466afcbf2c4ccdfa658824282fdc3c512cChris Lattner  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
13669e5bb85ac899eeab7c21b5ff9030c3da6ff4837bChandler Carruth                                       SourceLocation &ExpansionEnd);
13675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
13695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// as a builtin macro, handle it and return the next token as 'Tok'.
1370d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ExpandBuiltinMacro(Token &Tok);
13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
13735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// return the first token after the directive.  The _Pragma token has just
13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// been read into 'Tok'.
1375d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Handle_Pragma(Token &Tok);
13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13771ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
13781ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  /// is not enclosed within a string literal.
13791ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall  void HandleMicrosoft__pragma(Token &Tok);
13801ef8a2e7675f3d8b6e8d9963b00378086e1dcdc7John McCall
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// start lexing tokens from it instead of the current buffer.
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
13846137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek
13856137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and
13866137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  /// start getting tokens from it using the PTH cache.
13876137dc99ef0c2b14050631367057758b0d596cb3Ted Kremenek  void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
13881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138995d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis  /// \brief Set the file ID for the preprocessor predefines.
139095d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis  void setPredefinesFileID(FileID FID) {
139195d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis    assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
139295d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis    PredefinesFileID = FID;
139395d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis  }
139495d912c994832333a998bc7d4ae0e8e830a2d38bArgyrios Kyrtzidis
139534eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  /// IsFileLexer - Returns true if we are lexing from a file and not a
139634eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  ///  pragma or a macro.
139734eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek  static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
139834eaa24e6c2dc72bbbd5ad09694368cfd09c5f14Ted Kremenek    return L ? !L->isPragmaLexer() : P != 0;
139941938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
140041938c8493b4380df738263166b746eacb33c309Ted Kremenek
140181d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek  static bool IsFileLexer(const IncludeStackInfo& I) {
140281d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek    return IsFileLexer(I.TheLexer, I.ThePPLexer);
140341938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
140441938c8493b4380df738263166b746eacb33c309Ted Kremenek
140581d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek  bool IsFileLexer() const {
140681d24e135c60dcbe421864e6a4334ba99b8fe7ecTed Kremenek    return IsFileLexer(CurLexer.get(), CurPPLexer);
140741938c8493b4380df738263166b746eacb33c309Ted Kremenek  }
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
141003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  // Caching stuff.
141103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void CachingLex(Token &Result);
141203569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis  bool InCachingLexMode() const {
141303569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis    // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
141403569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis    // that we are past EOF, not that we are in CachingLex mode.
14157247c88d1e41514a41085f83ebf03dd5220e054aDavid Blaikie    return !CurPPLexer && !CurTokenLexer && !CurPTHLexer &&
141656eb1ec6e54080e47bbc62412737c25afb5211edTed Kremenek           !IncludeMacroStack.empty();
141703569eaabceb14a20e23d043135fdccbc7309a96Argyrios Kyrtzidis  }
141803db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void EnterCachingLexMode();
141903db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  void ExitCachingLexMode() {
142003db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis    if (InCachingLexMode())
142103db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis      RemoveTopOfLexerStack();
142203db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  }
142303db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  const Token &PeekAhead(unsigned N);
14243604e3895ecd850291b518e5a82246c888ce9d0fArgyrios Kyrtzidis  void AnnotatePreviousCachedTokens(const Token &Tok);
142503db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis
142603db1b31dd926409b7defc1c90b66549464652c0Argyrios Kyrtzidis  //===--------------------------------------------------------------------===//
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle*Directive - implement the various preprocessor directives.  These
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// should side-effect the current preprocessor object so that the next call
14295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to Lex() will return the appropriate token next.
1430359cc4475487ea0a660b3e8918334a2866e71c66Chris Lattner  void HandleLineDirective(Token &Tok);
1431478a18ec47fdb7e0e580a2635648456e9db9ad4fChris Lattner  void HandleDigitDirective(Token &Tok);
1432d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1433d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentSCCSDirective(Token &Tok);
143494ad28b31433058445a27db722f60402ee820beaDouglas Gregor  void HandleMacroPublicDirective(Token &Tok);
1435aa93a875605536d72a10359a0098396192b7d4ecDouglas Gregor  void HandleMacroPrivateDirective(Token &Tok);
1436ba243b59a1074e0962f6abfa3bb9aa984eac1245David Blaikie
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // File inclusion.
1438ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeDirective(SourceLocation HashLoc,
1439ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor                              Token &Tok,
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const DirectoryLookup *LookupFrom = 0,
14415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              bool isImport = false);
1442ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1443ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1444ecdcb883cbc6bb4a2445dc6f02d58d9bdb54a0edDouglas Gregor  void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
14454207edaf03da28fa917a94b31c5c1eef0e6417dcAaron Ballman  void HandleMicrosoftImportDirective(Token &Tok);
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Macro handling.
1448671538e8a51eab5bd65a1f9f327ba7f44f84e486Richard Trieu  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterTopLevelIfndef);
1449d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUndefDirective(Token &Tok);
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Conditional Inclusion.
1452d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfdefDirective(Token &Tok, bool isIfndef,
14535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                            bool ReadAnyTokensBeforeDirective);
1454d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1455d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleEndifDirective(Token &Tok);
1456d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElseDirective(Token &Tok);
1457d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElifDirective(Token &Tok);
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Pragmas.
14600189fd61ec72d86e008aa5615be80581f84cf703Enea Zaffanella  void HandlePragmaDirective(SourceLocation IntroducerLoc,
14610189fd61ec72d86e008aa5615be80581f84cf703Enea Zaffanella                             PragmaIntroducerKind Introducer);
14625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1463d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaOnce(Token &OnceTok);
14642243449253475574fc6f14986ff8f7fce5d46799Chris Lattner  void HandlePragmaMark();
1465d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaPoison(Token &PoisonTok);
1466d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaSystemHeader(Token &SysHeaderTok);
1467d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaDependency(Token &DependencyTok);
1468f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  void HandlePragmaPushMacro(Token &Tok);
1469f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  void HandlePragmaPopMacro(Token &Tok);
14704c55c54db8e676aa3e042188773d9d82d59fff91Aaron Ballman  void HandlePragmaIncludeAlias(Token &Tok);
1471f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner  IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1472f47724bf78299c7a50f008e0443c5f9f9f279ddcChris Lattner
1473046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // Return true and store the first token only if any CommentHandler
1474046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // has inserted some tokens and getCommentRetentionState() is false.
1475046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  bool HandleComment(Token &Token, SourceRange Comment);
14760827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
14770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// \brief A macro is used, update information about macros that need unused
14780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  /// warnings.
14790827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  void markMacroAsUsed(MacroInfo *MI);
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Abstract base class that describes a handler that will receive
14832e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor/// source ranges for each of the comments encountered in the source file.
14842e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorclass CommentHandler {
14852e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregorpublic:
14862e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  virtual ~CommentHandler();
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1488046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // The handler shall return true if it has pushed any tokens
1489046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  // to be read using e.g. EnterToken or EnterTokenStream.
1490046c2277dcbcc8eb89dbb5b1b8c5226b7cb81635Chris Lattner  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
14912e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor};
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
14945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
1496