Preprocessor.h revision 97f8461a2c553f68a258612d2322e4281c3f0915
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===//
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details.
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===----------------------------------------------------------------------===//
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//  This file defines the Preprocessor interface.
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===//
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define LLVM_CLANG_LEX_PREPROCESSOR_H
164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/MacroInfo.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/Lexer.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/PTHLexer.h"
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "clang/Lex/PPCallbacks.h"
21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "clang/Lex/PPMutationListener.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/TokenLexer.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/PTHManager.h"
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Basic/Builtins.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Diagnostic.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/IdentifierTable.h"
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/SourceLocation.h"
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "llvm/ADT/DenseMap.h"
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/IntrusiveRefCntPtr.h"
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/SmallPtrSet.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/OwningPtr.h"
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/SmallVector.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/ArrayRef.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Allocator.h"
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace llvm {
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  template<unsigned InternalLen> class SmallString;
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace clang {
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SourceManager;
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ExternalPreprocessorSource;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileManager;
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileEntry;
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class HeaderSearch;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PragmaNamespace;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PragmaHandler;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CommentHandler;
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ScratchBuffer;
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TargetInfo;
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PPCallbacks;
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class CodeCompletionHandler;
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DirectoryLookup;
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PreprocessingRecord;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ModuleLoader;
58d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass PreprocessorOptions;
59d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
60d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// \brief Stores token information for comparing actual tokens with
61d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// predefined values.  Only handles simple tokens and identifiers.
62d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass TokenValue {
63d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  tok::TokenKind Kind;
64d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  IdentifierInfo *II;
65d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
66d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochpublic:
67d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  TokenValue(tok::TokenKind Kind) : Kind(Kind), II(0) {
68d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    assert(Kind != tok::raw_identifier && "Raw identifiers are not supported.");
69d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    assert(Kind != tok::identifier &&
70d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch           "Identifiers should be created by TokenValue(IdentifierInfo *)");
71d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    assert(!tok::isLiteral(Kind) && "Literals are not supported.");
72d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    assert(!tok::isAnnotation(Kind) && "Annotations are not supported.");
73d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  }
74d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {}
75d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  bool operator==(const Token &Tok) const {
76d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    return Tok.getKind() == Kind &&
77d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch        (!II || II == Tok.getIdentifierInfo());
78d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  }
79d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch};
80d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
81d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// Preprocessor - This object engages in a tight little dance with the lexer to
82d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// efficiently preprocess tokens.  Lexers know only about tokens within a
83d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// single source file, and don't know anything about preprocessor-level issues
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// like the \#include stack, token expansion, etc.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Preprocessor : public RefCountedBase<Preprocessor> {
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  llvm::IntrusiveRefCntPtr<PreprocessorOptions> PPOpts;
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DiagnosticsEngine        *Diags;
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LangOptions       &LangOpts;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const TargetInfo  *Target;
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileManager       &FileMgr;
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SourceManager     &SourceMgr;
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ScratchBuffer     *ScratchBuf;
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HeaderSearch      &HeaderInfo;
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ModuleLoader      &TheModuleLoader;
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief External source of macros.
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExternalPreprocessorSource *ExternalSource;
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// PTH - An optional PTHManager object used for getting tokens from
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  a token cache rather than lexing the original source file.
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  OwningPtr<PTHManager> PTH;
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// BP - A BumpPtrAllocator object used to quickly allocate and release
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  objects internal to the Preprocessor.
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  llvm::BumpPtrAllocator BP;
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Identifiers for builtin macros and other builtins.
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__COUNTER__;                // __COUNTER__
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident_Pragma, *Ident__pragma;    // _Pragma, __pragma
1170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IdentifierInfo *Ident__has_feature;              // __has_feature
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IdentifierInfo *Ident__has_extension;            // __has_extension
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IdentifierInfo *Ident__has_builtin;              // __has_builtin
1210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  IdentifierInfo *Ident__has_attribute;            // __has_attribute
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IdentifierInfo *Ident__has_include;              // __has_include
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IdentifierInfo *Ident__has_include_next;         // __has_include_next
1240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  IdentifierInfo *Ident__has_warning;              // __has_warning
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IdentifierInfo *Ident__building_module;          // __building_module
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IdentifierInfo *Ident__MODULE__;                 // __MODULE__
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation DATELoc, TIMELoc;
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  unsigned CounterValue;  // Next __COUNTER__ value.
130d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  enum {
13258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    /// MaxIncludeStackDepth - Maximum depth of \#includes.
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MaxAllowedIncludeStackDepth = 200
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // State that is set before the preprocessor begins.
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool KeepComments : 1;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool KeepMacroComments : 1;
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool SuppressIncludeNotFoundError : 1;
14068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
14168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // State that changes while the preprocessor runs:
14268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
14368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Whether the preprocessor owns the header search object.
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool OwnsHeaderSearch : 1;
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// DisableMacroExpansion - True if macro expansion is disabled.
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool DisableMacroExpansion : 1;
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// MacroExpansionInDirectivesOverride - Temporarily disables
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// DisableMacroExpansion (i.e. enables expansion) when parsing preprocessor
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// directives.
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool MacroExpansionInDirectivesOverride : 1;
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  class ResetMacroExpansionHelper;
15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Whether we have already loaded macros from the external source.
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable bool ReadMacrosFromExternalSource : 1;
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief True if pragmas are enabled.
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool PragmasEnabled : 1;
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief True if we are pre-expanding macro arguments.
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool InMacroArgPreExpansion;
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Identifiers - This is mapping/lookup information for all identifiers in
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// the program, including program keywords.
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable IdentifierTable Identifiers;
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Selectors - This table contains all the selectors in the program. Unlike
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// It is declared/expanded here because it's role/lifetime is
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// conceptually similar the IdentifierTable. In addition, the current control
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// flow (in clang::ParseAST()), make it convenient to put here.
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// the lifetime of the preprocessor.
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SelectorTable Selectors;
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// BuiltinInfo - Information about builtins.
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Builtin::Context BuiltinInfo;
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// PragmaHandlers - This tracks all of the pragmas that the client registered
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// with this preprocessor.
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PragmaNamespace *PragmaHandlers;
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
186ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// \brief Tracks all of the comment handlers that the client registered
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// with this preprocessor.
188ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  std::vector<CommentHandler *> CommentHandlers;
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief True if we want to ignore EOF token and continue later on (thus
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// avoid tearing the Lexer and etc. down).
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool IncrementalProcessing;
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief The code-completion handler.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CodeCompletionHandler *CodeComplete;
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief The file that we're performing code-completion for, if any.
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const FileEntry *CodeCompletionFile;
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief The offset in file for the code-completion point.
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  unsigned CodeCompletionOffset;
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief The location for the code-completion point. This gets instantiated
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// when the CodeCompletionFile gets \#include'ed for preprocessing.
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation CodeCompletionLoc;
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief The start location for the file of the code-completion point.
208c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// This gets instantiated when the CodeCompletionFile gets \#include'ed
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// for preprocessing.
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation CodeCompletionFileLoc;
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief The source location of the 'import' contextual keyword we just
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// lexed, if any.
215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation ModuleImportLoc;
216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief The module import path that we're currently processing.
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2>
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ModuleImportPath;
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Whether the module import expectes an identifier next. Otherwise,
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// it expects a '.' or ';'.
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool ModuleImportExpectsIdentifier;
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief The source location of the currently-active
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// #pragma clang arc_cf_code_audited begin.
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SourceLocation PragmaARCCFCodeAuditedLoc;
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief True if we hit the code-completion point.
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool CodeCompletionReached;
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief The number of bytes that we will initially skip when entering the
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// main file, which is used when loading a precompiled preamble, along
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// with a flag that indicates whether skipping this number of bytes will
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// place the lexer at the start of a line.
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::pair<unsigned, bool> SkipMainFilePreamble;
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
238ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// CurLexer - This is the current top of the stack that we're lexing from if
239ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// not expanding a macro and we are lexing directly from source code.
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  OwningPtr<Lexer> CurLexer;
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// CurPTHLexer - This is the current top of stack that we're lexing from if
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  not expanding from a macro and we are lexing from a PTH cache.
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null.
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  OwningPtr<PTHLexer> CurPTHLexer;
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// CurPPLexer - This is the current top of the stack what we're lexing from
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  if not expanding a macro.  This is an alias for either CurLexer or
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///  CurPTHLexer.
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PreprocessorLexer *CurPPLexer;
2520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
253ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// CurLookup - The DirectoryLookup structure used to find the current
254ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
255ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// implement \#include_next and find directory-specific properties.
256ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  const DirectoryLookup *CurDirLookup;
257ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
258ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// CurTokenLexer - This is the current macro we are expanding, if we are
259ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
260ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  OwningPtr<TokenLexer> CurTokenLexer;
261ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
262ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  /// \brief The kind of lexer we're currently working with.
263ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  enum CurLexerKind {
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CLK_Lexer,
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CLK_PTHLexer,
266d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    CLK_TokenLexer,
267d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    CLK_CachingLexer,
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CLK_LexAfterModuleImport
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } CurLexerKind;
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// IncludeMacroStack - This keeps track of the stack of files currently
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// \#included, and macros currently being expanded from, not counting
2735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// CurLexer/CurTokenLexer.
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct IncludeStackInfo {
2755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    enum CurLexerKind     CurLexerKind;
2765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Lexer                 *TheLexer;
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PTHLexer              *ThePTHLexer;
2785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    PreprocessorLexer     *ThePPLexer;
2795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    TokenLexer            *TheTokenLexer;
2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const DirectoryLookup *TheDirLookup;
281d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
282d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch    IncludeStackInfo(enum CurLexerKind K, Lexer *L, PTHLexer* P,
283d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch                     PreprocessorLexer* PPL,
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     TokenLexer* TL, const DirectoryLookup *D)
285d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch      : CurLexerKind(K), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL),
286d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch        TheTokenLexer(TL), TheDirLookup(D) {}
287d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  };
288d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  std::vector<IncludeStackInfo> IncludeMacroStack;
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Callbacks - These are actions invoked when some preprocessor activity is
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// encountered (e.g. a file is \#included, etc).
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PPCallbacks *Callbacks;
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Listener whose actions are invoked when an entity in the
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// preprocessor (e.g., a macro) that was loaded from an AST file is
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// later mutated.
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PPMutationListener *Listener;
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct MacroExpandsInfo {
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Token Tok;
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MacroInfo *MI;
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SourceRange Range;
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MacroExpandsInfo(Token Tok, MacroInfo *MI, SourceRange Range)
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : Tok(Tok), MI(MI), Range(Range) { }
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Macros - For each IdentifierInfo that was associated with a macro, we
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// keep a mapping to the history of all macro definitions and #undefs in
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// the reverse order (the latest one is in the head of the list).
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class ASTReader;
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Macros that we want to warn because they are not used at the end
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// of the translation unit; we store just their SourceLocations instead
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// something like MacroInfo*. The benefit of this is that when we are
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// deserializing from PCH, we don't need to deserialize identifier & macros
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// just so that we can report that they are unused, we just warn using
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// the SourceLocations of this set (that will be filled by the ASTReader).
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// We are using SmallPtrSet instead of a vector for faster removal.
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef llvm::SmallPtrSet<SourceLocation, 32> WarnUnusedMacroLocsTy;
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WarnUnusedMacroLocsTy WarnUnusedMacroLocs;
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// MacroArgCache - This is a "freelist" of MacroArg objects that can be
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// reused for quick allocation.
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MacroArgs *MacroArgCache;
3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class MacroArgs;
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// PragmaPushMacroInfo - For each IdentifierInfo used in a #pragma
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// push_macro directive, we keep a MacroInfo stack used to restore
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// previous macro value.
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> > PragmaPushMacroInfo;
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Various statistics we track for performance analysis.
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned NumIf, NumElse, NumEndif;
337424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
338424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
33958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
34090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  unsigned NumSkipped;
341424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
342424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  /// Predefines - This string is the predefined macros that preprocessor
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  /// should use from the command line etc.
344424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::string Predefines;
345424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
34658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum { TokenLexerCacheSize = 8 };
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned NumCachedTokenLexers;
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  /// \brief Keeps macro expanded tokens for TokenLexers.
352868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
353868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
354868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  /// going to lex in the cache and when it finishes the tokens are removed
355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  /// from the end of the cache.
35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  SmallVector<Token, 16> MacroExpandedTokens;
35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  std::vector<std::pair<TokenLexer *, size_t> > MacroExpandingLexersStack;
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief A record of the macro definitions and expansions that
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// occurred during preprocessing.
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// This is an optional side structure that can be enabled with
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \c createPreprocessingRecord() prior to preprocessing.
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PreprocessingRecord *Record;
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)private:  // Cached tokens state.
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef SmallVector<Token, 1> CachedTokensTy;
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// CachedTokens - Cached tokens are stored here when we do backtracking or
370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// lookahead. They are "lexed" by the CachingLex() method.
371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  CachedTokensTy CachedTokens;
372c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// CachedLexPos - The position of the cached token that CachingLex() should
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// "lex" next. If it points beyond the CachedTokens vector, it means that
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// a normal Lex() should be invoked.
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CachedTokensTy::size_type CachedLexPos;
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// BacktrackPositions - Stack of backtrack positions, allowing nested
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// indicate where CachedLexPos should be set when the BackTrack() method is
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// invoked (at which point the last position is popped).
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<CachedTokensTy::size_type> BacktrackPositions;
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct MacroInfoChain {
385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    MacroInfo MI;
386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    MacroInfoChain *Next;
387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    MacroInfoChain *Prev;
388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// MacroInfos are managed as a chain for easy disposal.  This is the head
391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// of that list.
392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MacroInfoChain *MIChainHead;
393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// MICache - A "freelist" of MacroInfo objects that can be reused for quick
395c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// allocation.
396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MacroInfoChain *MICache;
397c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
398c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)public:
399c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Preprocessor(llvm::IntrusiveRefCntPtr<PreprocessorOptions> PPOpts,
400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               DiagnosticsEngine &diags, LangOptions &opts,
401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               const TargetInfo *target,
402c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               SourceManager &SM, HeaderSearch &Headers,
403c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               ModuleLoader &TheModuleLoader,
404c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               IdentifierInfoLookup *IILookup = 0,
405c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               bool OwnsHeaderSearch = false,
406c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               bool DelayInitialization = false,
407c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)               bool IncrProcessing = false);
408c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
409c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ~Preprocessor();
410c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Initialize the preprocessor, if the constructor did not already
412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// perform the initialization.
413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \param Target Information about the target.
415c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void Initialize(const TargetInfo &Target);
416c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
417c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Retrieve the preprocessor options used to initialize this
418c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// preprocessor.
419c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
421c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DiagnosticsEngine &getDiagnostics() const { return *Diags; }
422c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; }
423c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
424d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const LangOptions &getLangOpts() const { return LangOpts; }
425d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  const TargetInfo &getTargetInfo() const { return *Target; }
426d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FileManager &getFileManager() const { return FileMgr; }
427d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  SourceManager &getSourceManager() const { return SourceMgr; }
428cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
429cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
430cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  IdentifierTable &getIdentifierTable() { return Identifiers; }
431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SelectorTable &getSelectorTable() { return Selectors; }
432cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Builtin::Context &getBuiltinInfo() { return BuiltinInfo; }
433cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
434cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
435cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void setPTHManager(PTHManager* pm);
436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PTHManager *getPTHManager() { return PTH.get(); }
438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
439cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void setExternalSource(ExternalPreprocessorSource *Source) {
440cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ExternalSource = Source;
441cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
442cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
443cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ExternalPreprocessorSource *getExternalSource() const {
444cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return ExternalSource;
445cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
446cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// \brief Retrieve the module loader associated with this preprocessor.
448cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ModuleLoader &getModuleLoader() const { return TheModuleLoader; }
449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// SetCommentRetentionState - Control whether or not the preprocessor retains
451cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// comments in output.
452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    this->KeepComments = KeepComments | KeepMacroComments;
454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    this->KeepMacroComments = KeepMacroComments;
45546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
456cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
45746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool getCommentRetentionState() const { return KeepComments; }
458cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
45946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; }
460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool getPragmasEnabled() const { return PragmasEnabled; }
461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
462cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void SetSuppressIncludeNotFoundError(bool Suppress) {
463cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SuppressIncludeNotFoundError = Suppress;
464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
465cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
466cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool GetSuppressIncludeNotFoundError() {
467cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return SuppressIncludeNotFoundError;
468cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
469cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
470cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// isCurrentLexer - Return true if we are lexing directly from the specified
471cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// lexer.
472cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool isCurrentLexer(const PreprocessorLexer *L) const {
473cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return CurPPLexer == L;
474cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
475cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
476cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// getCurrentLexer - Return the current lexer being lexed from.  Note
477cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// that this ignores any potentially active macro expansions and _Pragma
478cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// expansions going on at the time.
479cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; }
480cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
481cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// getCurrentFileLexer - Return the current file lexer being lexed from.
482cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// Note that this ignores any potentially active macro expansions and _Pragma
48346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// expansions going on at the time.
48446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  PreprocessorLexer *getCurrentFileLexer() const;
48546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
48646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// getPPCallbacks/addPPCallbacks - Accessors for preprocessor callbacks.
48746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// Note that this class takes ownership of any PPCallbacks object given to
488cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// it.
489cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPCallbacks *getPPCallbacks() const { return Callbacks; }
490cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void addPPCallbacks(PPCallbacks *C) {
491cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (Callbacks)
492cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      C = new PPChainedCallbacks(C, Callbacks);
493cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    Callbacks = C;
494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Attach an preprocessor mutation listener to the preprocessor.
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ///
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// The preprocessor mutation listener provides the ability to track
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// modifications to the preprocessor entities committed after they were
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// initially created.
501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void setPPMutationListener(PPMutationListener *Listener) {
502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    this->Listener = Listener;
503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Retrieve a pointer to the preprocessor mutation listener
506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// associated with this preprocessor, if any.
507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PPMutationListener *getPPMutationListener() const { return Listener; }
508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Given an identifier, return the MacroInfo it is \#defined to
510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// or null if it isn't \#define'd.
511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!II->hasMacroDefinition())
513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return 0;
514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    MacroInfo *MI = getMacroInfoHistory(II);
516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    assert(MI->getUndefLoc().isInvalid() && "Macro is undefined!");
517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return MI;
518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Given an identifier, return the (probably #undef'd) MacroInfo
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// representing the most recent macro definition. One can iterate over all
522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// previous macro definitions from it. This method should only be called for
523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// identifiers that hadMacroDefinition().
524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MacroInfo *getMacroInfoHistory(IdentifierInfo *II) const;
525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Specify a macro for this identifier.
527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Add a MacroInfo that was loaded from an AST file.
529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI,
530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          MacroInfo *Hint = 0);
531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Make the given MacroInfo, that was loaded from an AST file and
532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// previously hidden, visible.
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void makeLoadedMacroInfoVisible(IdentifierInfo *II, MacroInfo *MI);
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Undefine a macro for this identifier.
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void clearMacroInfo(IdentifierInfo *II);
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// macro_iterator/macro_begin/macro_end - This allows you to walk the macro
538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// history table. Currently defined macros have
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// IdentifierInfo::hasMacroDefinition() set and an empty
540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// MacroInfo::getUndefLoc() at the head of the list.
541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  typedef llvm::DenseMap<IdentifierInfo*,
542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         MacroInfo*>::const_iterator macro_iterator;
543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  macro_iterator macro_begin(bool IncludeExternalMacros = true) const;
544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  macro_iterator macro_end(bool IncludeExternalMacros = true) const;
545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
546c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Return the name of the macro defined before \p Loc that has
547c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// spelling \p Tokens.  If there are multiple macros with same spelling,
548c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// return the last one defined.
549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  StringRef getLastMacroWithSpelling(SourceLocation Loc,
550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                     ArrayRef<TokenValue> Tokens) const;
551c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
552c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const std::string &getPredefines() const { return Predefines; }
553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// setPredefines - Set the predefines for this Preprocessor.  These
554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// predefines are automatically injected when parsing the main file.
555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void setPredefines(const char *P) { Predefines = P; }
556c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void setPredefines(const std::string &P) { Predefines = P; }
557c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
55890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// Return information about the specified preprocessor
55990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// identifier token.
56090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  IdentifierInfo *getIdentifierInfo(StringRef Name) const {
56190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return &Identifiers.get(Name);
56290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
56390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
56490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
56590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// If 'Namespace' is non-null, then it is a token required to exist on the
56690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
56790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler);
56890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void AddPragmaHandler(PragmaHandler *Handler) {
56990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    AddPragmaHandler(StringRef(), Handler);
57090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
57190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
57290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  /// RemovePragmaHandler - Remove the specific pragma handler from
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// the preprocessor. If \p Namespace is non-null, then it should
5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// be the namespace that \p Handler was added to. It is an error
5752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// to remove a handler that has not been registered.
5762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler);
5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemovePragmaHandler(PragmaHandler *Handler) {
5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RemovePragmaHandler(StringRef(), Handler);
5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Add the specified comment handler to the preprocessor.
5822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void addCommentHandler(CommentHandler *Handler);
5832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Remove the specified comment handler.
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// It is an error to remove a handler that has not been registered.
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void removeCommentHandler(CommentHandler *Handler);
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Set the code completion handler to the given object.
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void setCodeCompletionHandler(CodeCompletionHandler &Handler) {
5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CodeComplete = &Handler;
5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Retrieve the current code-completion handler.
5952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CodeCompletionHandler *getCodeCompletionHandler() const {
5962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return CodeComplete;
5972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Clear out the code completion handler.
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void clearCodeCompletionHandler() {
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    CodeComplete = 0;
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Hook used by the lexer to invoke the "natural language" code
6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// completion point.
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CodeCompleteNaturalLanguage();
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Retrieve the preprocessing record, or NULL if there is no
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// preprocessing record.
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PreprocessingRecord *getPreprocessingRecord() const { return Record; }
611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Create a new preprocessing record, which will keep track of
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// all macro expansions, macro definitions, etc.
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void createPreprocessingRecord(bool RecordConditionalDirectives);
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// which implicitly adds the builtin defines etc.
618c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void EnterMainSourceFile();
619c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
620c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// EndSourceFile - Inform the preprocessor callbacks that processing is
621c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// complete.
6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EndSourceFile();
623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
624c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// EnterSourceFile - Add a source file to the top of the include stack and
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// start lexing tokens from it instead of the current buffer.  Emit an error
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// and don't enter the file on error.
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir,
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       SourceLocation Loc);
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// tokens from it instead of the current buffer.  Args specifies the
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// tokens input to a function-like macro.
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// ILEnd specifies the location of the ')' for a function-like macro or the
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// identifier for an object-like macro.
6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnterMacro(Token &Identifier, SourceLocation ILEnd, MacroInfo *Macro,
6372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  MacroArgs *Args);
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// which will cause the lexer to start returning the specified tokens.
6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// not be subject to further macro expansion.  Otherwise, these tokens will
6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// be re-macro-expanded when/if expansion is enabled.
6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// If OwnsTokens is false, this method assumes that the specified stream of
6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// tokens has a permanent owner somewhere, so they do not need to be copied.
6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// If it is true, it assumes the array of tokens is allocated with new[] and
6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// must be freed.
6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnterTokenStream(const Token *Toks, unsigned NumToks,
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        bool DisableMacroExpansion, bool OwnsTokens);
6532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// lexer stack.  This should only be used in situations where the current
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// state of the top-of-stack lexer is known.
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveTopOfLexerStack();
6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// EnableBacktrackAtThisPos - From the point that this method is called, and
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// keeps track of the lexed tokens so that a subsequent Backtrack() call will
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// make the Preprocessor re-lex the same tokens.
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can
6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will
6662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// be combined with the EnableBacktrackAtThisPos calls in reverse order.
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack
6692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// at some point after EnableBacktrackAtThisPos. If you don't, caching of
6702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// tokens will continue indefinitely.
6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void EnableBacktrackAtThisPos();
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call.
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CommitBacktrackedTokens();
6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since
6782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// EnableBacktrackAtThisPos() was previously called.
6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void Backtrack();
6802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and
6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// caching of tokens is on.
6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); }
6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
685ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  /// Lex - To lex a token from the preprocessor, just pull a token from the
6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// current lexer or macro object.
687ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  void Lex(Token &Result) {
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    switch (CurLexerKind) {
6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CLK_Lexer: CurLexer->Lex(Result); break;
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CLK_PTHLexer: CurPTHLexer->Lex(Result); break;
6912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CLK_TokenLexer: CurTokenLexer->Lex(Result); break;
692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    case CLK_CachingLexer: CachingLex(Result); break;
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    case CLK_LexAfterModuleImport: LexAfterModuleImport(Result); break;
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LexAfterModuleImport(Token &Result);
698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \brief Lex a string literal, which may be the concatenation of multiple
700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// string literals and may even come from macro expansion.
701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// \returns true on success, false if a error diagnostic has been generated.
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool LexStringLiteral(Token &Result, std::string &String,
7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        const char *DiagnosticTag, bool AllowMacroExpansion) {
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (AllowMacroExpansion)
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Lex(Result);
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    else
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      LexUnexpandedToken(Result);
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return FinishLexStringLiteral(Result, String, DiagnosticTag,
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  AllowMacroExpansion);
7102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Complete the lexing of a string literal where the first token has
7132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// already been lexed (see LexStringLiteral).
7142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool FinishLexStringLiteral(Token &Result, std::string &String,
7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              const char *DiagnosticTag,
7162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              bool AllowMacroExpansion);
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// something not a comment.  This is useful in -E -C mode where comments
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// would foul up preprocessor directive handling.
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LexNonComment(Token &Result) {
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    do
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Lex(Result);
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    while (Result.getKind() == tok::comment);
7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
726c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// LexUnexpandedToken - This is just like Lex, but this disables macro
7282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// expansion of identifier tokens.
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LexUnexpandedToken(Token &Result) {
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Disable macro expansion.
731    bool OldVal = DisableMacroExpansion;
732    DisableMacroExpansion = true;
733    // Lex the token.
734    Lex(Result);
735
736    // Reenable it.
737    DisableMacroExpansion = OldVal;
738  }
739
740  /// LexUnexpandedNonComment - Like LexNonComment, but this disables macro
741  /// expansion of identifier tokens.
742  void LexUnexpandedNonComment(Token &Result) {
743    do
744      LexUnexpandedToken(Result);
745    while (Result.getKind() == tok::comment);
746  }
747
748  /// Disables macro expansion everywhere except for preprocessor directives.
749  void SetMacroExpansionOnlyInDirectives() {
750    DisableMacroExpansion = true;
751    MacroExpansionInDirectivesOverride = true;
752  }
753
754  /// LookAhead - This peeks ahead N tokens and returns that token without
755  /// consuming any tokens.  LookAhead(0) returns the next token that would be
756  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
757  /// returns normal tokens after phase 5.  As such, it is equivalent to using
758  /// 'Lex', not 'LexUnexpandedToken'.
759  const Token &LookAhead(unsigned N) {
760    if (CachedLexPos + N < CachedTokens.size())
761      return CachedTokens[CachedLexPos+N];
762    else
763      return PeekAhead(N+1);
764  }
765
766  /// RevertCachedTokens - When backtracking is enabled and tokens are cached,
767  /// this allows to revert a specific number of tokens.
768  /// Note that the number of tokens being reverted should be up to the last
769  /// backtrack position, not more.
770  void RevertCachedTokens(unsigned N) {
771    assert(isBacktrackEnabled() &&
772           "Should only be called when tokens are cached for backtracking");
773    assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back())
774         && "Should revert tokens up to the last backtrack position, not more");
775    assert(signed(CachedLexPos) - signed(N) >= 0 &&
776           "Corrupted backtrack positions ?");
777    CachedLexPos -= N;
778  }
779
780  /// EnterToken - Enters a token in the token stream to be lexed next. If
781  /// BackTrack() is called afterwards, the token will remain at the insertion
782  /// point.
783  void EnterToken(const Token &Tok) {
784    EnterCachingLexMode();
785    CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok);
786  }
787
788  /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching
789  /// tokens (because backtrack is enabled) it should replace the most recent
790  /// cached tokens with the given annotation token. This function has no effect
791  /// if backtracking is not enabled.
792  ///
793  /// Note that the use of this function is just for optimization; so that the
794  /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is
795  /// invoked.
796  void AnnotateCachedTokens(const Token &Tok) {
797    assert(Tok.isAnnotation() && "Expected annotation token");
798    if (CachedLexPos != 0 && isBacktrackEnabled())
799      AnnotatePreviousCachedTokens(Tok);
800  }
801
802  /// \brief Replace the last token with an annotation token.
803  ///
804  /// Like AnnotateCachedTokens(), this routine replaces an
805  /// already-parsed (and resolved) token with an annotation
806  /// token. However, this routine only replaces the last token with
807  /// the annotation token; it does not affect any other cached
808  /// tokens. This function has no effect if backtracking is not
809  /// enabled.
810  void ReplaceLastTokenWithAnnotation(const Token &Tok) {
811    assert(Tok.isAnnotation() && "Expected annotation token");
812    if (CachedLexPos != 0 && isBacktrackEnabled())
813      CachedTokens[CachedLexPos-1] = Tok;
814  }
815
816  /// TypoCorrectToken - Update the current token to represent the provided
817  /// identifier, in order to cache an action performed by typo correction.
818  void TypoCorrectToken(const Token &Tok) {
819    assert(Tok.getIdentifierInfo() && "Expected identifier token");
820    if (CachedLexPos != 0 && isBacktrackEnabled())
821      CachedTokens[CachedLexPos-1] = Tok;
822  }
823
824  /// \brief Recompute the current lexer kind based on the CurLexer/CurPTHLexer/
825  /// CurTokenLexer pointers.
826  void recomputeCurLexerKind();
827
828  /// \brief Returns true if incremental processing is enabled
829  bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; }
830
831  /// \brief Enables the incremental processing
832  void enableIncrementalProcessing(bool value = true) {
833    IncrementalProcessing = value;
834  }
835
836  /// \brief Specify the point at which code-completion will be performed.
837  ///
838  /// \param File the file in which code completion should occur. If
839  /// this file is included multiple times, code-completion will
840  /// perform completion the first time it is included. If NULL, this
841  /// function clears out the code-completion point.
842  ///
843  /// \param Line the line at which code completion should occur
844  /// (1-based).
845  ///
846  /// \param Column the column at which code completion should occur
847  /// (1-based).
848  ///
849  /// \returns true if an error occurred, false otherwise.
850  bool SetCodeCompletionPoint(const FileEntry *File,
851                              unsigned Line, unsigned Column);
852
853  /// \brief Determine if we are performing code completion.
854  bool isCodeCompletionEnabled() const { return CodeCompletionFile != 0; }
855
856  /// \brief Returns the location of the code-completion point.
857  /// Returns an invalid location if code-completion is not enabled or the file
858  /// containing the code-completion point has not been lexed yet.
859  SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; }
860
861  /// \brief Returns the start location of the file of code-completion point.
862  /// Returns an invalid location if code-completion is not enabled or the file
863  /// containing the code-completion point has not been lexed yet.
864  SourceLocation getCodeCompletionFileLoc() const {
865    return CodeCompletionFileLoc;
866  }
867
868  /// \brief Returns true if code-completion is enabled and we have hit the
869  /// code-completion point.
870  bool isCodeCompletionReached() const { return CodeCompletionReached; }
871
872  /// \brief Note that we hit the code-completion point.
873  void setCodeCompletionReached() {
874    assert(isCodeCompletionEnabled() && "Code-completion not enabled!");
875    CodeCompletionReached = true;
876    // Silence any diagnostics that occur after we hit the code-completion.
877    getDiagnostics().setSuppressAllDiagnostics(true);
878  }
879
880  /// \brief The location of the currently-active \#pragma clang
881  /// arc_cf_code_audited begin.  Returns an invalid location if there
882  /// is no such pragma active.
883  SourceLocation getPragmaARCCFCodeAuditedLoc() const {
884    return PragmaARCCFCodeAuditedLoc;
885  }
886
887  /// \brief Set the location of the currently-active \#pragma clang
888  /// arc_cf_code_audited begin.  An invalid location ends the pragma.
889  void setPragmaARCCFCodeAuditedLoc(SourceLocation Loc) {
890    PragmaARCCFCodeAuditedLoc = Loc;
891  }
892
893  /// \brief Instruct the preprocessor to skip part of the main source file.
894  ///
895  /// \param Bytes The number of bytes in the preamble to skip.
896  ///
897  /// \param StartOfLine Whether skipping these bytes puts the lexer at the
898  /// start of a line.
899  void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) {
900    SkipMainFilePreamble.first = Bytes;
901    SkipMainFilePreamble.second = StartOfLine;
902  }
903
904  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
905  /// the specified Token's location, translating the token's start
906  /// position in the current buffer into a SourcePosition object for rendering.
907  DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const {
908    return Diags->Report(Loc, DiagID);
909  }
910
911  DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const {
912    return Diags->Report(Tok.getLocation(), DiagID);
913  }
914
915  /// getSpelling() - Return the 'spelling' of the token at the given
916  /// location; does not go up to the spelling location or down to the
917  /// expansion location.
918  ///
919  /// \param buffer A buffer which will be used only if the token requires
920  ///   "cleaning", e.g. if it contains trigraphs or escaped newlines
921  /// \param invalid If non-null, will be set \c true if an error occurs.
922  StringRef getSpelling(SourceLocation loc,
923                              SmallVectorImpl<char> &buffer,
924                              bool *invalid = 0) const {
925    return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid);
926  }
927
928  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
929  /// token is the characters used to represent the token in the source file
930  /// after trigraph expansion and escaped-newline folding.  In particular, this
931  /// wants to get the true, uncanonicalized, spelling of things like digraphs
932  /// UCNs, etc.
933  ///
934  /// \param Invalid If non-null, will be set \c true if an error occurs.
935  std::string getSpelling(const Token &Tok, bool *Invalid = 0) const {
936    return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid);
937  }
938
939  /// getSpelling - This method is used to get the spelling of a token into a
940  /// preallocated buffer, instead of as an std::string.  The caller is required
941  /// to allocate enough space for the token, which is guaranteed to be at least
942  /// Tok.getLength() bytes long.  The length of the actual result is returned.
943  ///
944  /// Note that this method may do two possible things: it may either fill in
945  /// the buffer specified with characters, or it may *change the input pointer*
946  /// to point to a constant buffer with the data already in it (avoiding a
947  /// copy).  The caller is not allowed to modify the returned buffer pointer
948  /// if an internal buffer is returned.
949  unsigned getSpelling(const Token &Tok, const char *&Buffer,
950                       bool *Invalid = 0) const {
951    return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid);
952  }
953
954  /// getSpelling - This method is used to get the spelling of a token into a
955  /// SmallVector. Note that the returned StringRef may not point to the
956  /// supplied buffer if a copy can be avoided.
957  StringRef getSpelling(const Token &Tok,
958                        SmallVectorImpl<char> &Buffer,
959                        bool *Invalid = 0) const;
960
961  /// getSpellingOfSingleCharacterNumericConstant - Tok is a numeric constant
962  /// with length 1, return the character.
963  char getSpellingOfSingleCharacterNumericConstant(const Token &Tok,
964                                                   bool *Invalid = 0) const {
965    assert(Tok.is(tok::numeric_constant) &&
966           Tok.getLength() == 1 && "Called on unsupported token");
967    assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1");
968
969    // If the token is carrying a literal data pointer, just use it.
970    if (const char *D = Tok.getLiteralData())
971      return *D;
972
973    // Otherwise, fall back on getCharacterData, which is slower, but always
974    // works.
975    return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid);
976  }
977
978  /// \brief Retrieve the name of the immediate macro expansion.
979  ///
980  /// This routine starts from a source location, and finds the name of the macro
981  /// responsible for its immediate expansion. It looks through any intervening
982  /// macro argument expansions to compute this. It returns a StringRef which
983  /// refers to the SourceManager-owned buffer of the source where that macro
984  /// name is spelled. Thus, the result shouldn't out-live the SourceManager.
985  StringRef getImmediateMacroName(SourceLocation Loc) {
986    return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts());
987  }
988
989  /// CreateString - Plop the specified string into a scratch buffer and set the
990  /// specified token's location and length to it.  If specified, the source
991  /// location provides a location of the expansion point of the token.
992  void CreateString(StringRef Str, Token &Tok,
993                    SourceLocation ExpansionLocStart = SourceLocation(),
994                    SourceLocation ExpansionLocEnd = SourceLocation());
995
996  /// \brief Computes the source location just past the end of the
997  /// token at this source location.
998  ///
999  /// This routine can be used to produce a source location that
1000  /// points just past the end of the token referenced by \p Loc, and
1001  /// is generally used when a diagnostic needs to point just after a
1002  /// token where it expected something different that it received. If
1003  /// the returned source location would not be meaningful (e.g., if
1004  /// it points into a macro), this routine returns an invalid
1005  /// source location.
1006  ///
1007  /// \param Offset an offset from the end of the token, where the source
1008  /// location should refer to. The default offset (0) produces a source
1009  /// location pointing just past the end of the token; an offset of 1 produces
1010  /// a source location pointing to the last character in the token, etc.
1011  SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) {
1012    return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
1013  }
1014
1015  /// \brief Returns true if the given MacroID location points at the first
1016  /// token of the macro expansion.
1017  ///
1018  /// \param MacroBegin If non-null and function returns true, it is set to
1019  /// begin location of the macro.
1020  bool isAtStartOfMacroExpansion(SourceLocation loc,
1021                                 SourceLocation *MacroBegin = 0) const {
1022    return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts,
1023                                            MacroBegin);
1024  }
1025
1026  /// \brief Returns true if the given MacroID location points at the last
1027  /// token of the macro expansion.
1028  ///
1029  /// \param MacroEnd If non-null and function returns true, it is set to
1030  /// end location of the macro.
1031  bool isAtEndOfMacroExpansion(SourceLocation loc,
1032                               SourceLocation *MacroEnd = 0) const {
1033    return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd);
1034  }
1035
1036  /// DumpToken - Print the token to stderr, used for debugging.
1037  ///
1038  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
1039  void DumpLocation(SourceLocation Loc) const;
1040  void DumpMacro(const MacroInfo &MI) const;
1041
1042  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
1043  /// token, return a new location that specifies a character within the token.
1044  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,
1045                                         unsigned Char) const {
1046    return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts);
1047  }
1048
1049  /// IncrementPasteCounter - Increment the counters for the number of token
1050  /// paste operations performed.  If fast was specified, this is a 'fast paste'
1051  /// case we handled.
1052  ///
1053  void IncrementPasteCounter(bool isFast) {
1054    if (isFast)
1055      ++NumFastTokenPaste;
1056    else
1057      ++NumTokenPaste;
1058  }
1059
1060  void PrintStats();
1061
1062  size_t getTotalMemory() const;
1063
1064  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
1065  /// comment (/##/) in microsoft mode, this method handles updating the current
1066  /// state, returning the token on the next source line.
1067  void HandleMicrosoftCommentPaste(Token &Tok);
1068
1069  //===--------------------------------------------------------------------===//
1070  // Preprocessor callback methods.  These are invoked by a lexer as various
1071  // directives and events are found.
1072
1073  /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
1074  /// identifier information for the token and install it into the token,
1075  /// updating the token kind accordingly.
1076  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const;
1077
1078private:
1079  llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons;
1080
1081public:
1082
1083  // SetPoisonReason - Call this function to indicate the reason for
1084  // poisoning an identifier. If that identifier is accessed while
1085  // poisoned, then this reason will be used instead of the default
1086  // "poisoned" diagnostic.
1087  void SetPoisonReason(IdentifierInfo *II, unsigned DiagID);
1088
1089  // HandlePoisonedIdentifier - Display reason for poisoned
1090  // identifier.
1091  void HandlePoisonedIdentifier(Token & Tok);
1092
1093  void MaybeHandlePoisonedIdentifier(Token & Identifier) {
1094    if(IdentifierInfo * II = Identifier.getIdentifierInfo()) {
1095      if(II->isPoisoned()) {
1096        HandlePoisonedIdentifier(Identifier);
1097      }
1098    }
1099  }
1100
1101private:
1102  /// Identifiers used for SEH handling in Borland. These are only
1103  /// allowed in particular circumstances
1104  // __except block
1105  IdentifierInfo *Ident__exception_code,
1106                 *Ident___exception_code,
1107                 *Ident_GetExceptionCode;
1108  // __except filter expression
1109  IdentifierInfo *Ident__exception_info,
1110                 *Ident___exception_info,
1111                 *Ident_GetExceptionInfo;
1112  // __finally
1113  IdentifierInfo *Ident__abnormal_termination,
1114                 *Ident___abnormal_termination,
1115                 *Ident_AbnormalTermination;
1116public:
1117  void PoisonSEHIdentifiers(bool Poison = true); // Borland
1118
1119  /// HandleIdentifier - This callback is invoked when the lexer reads an
1120  /// identifier and has filled in the tokens IdentifierInfo member.  This
1121  /// callback potentially macro expands it or turns it into a named token (like
1122  /// 'for').
1123  void HandleIdentifier(Token &Identifier);
1124
1125
1126  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1127  /// the current file.  This either returns the EOF token and returns true, or
1128  /// pops a level off the include stack and returns false, at which point the
1129  /// client should call lex again.
1130  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
1131
1132  /// HandleEndOfTokenLexer - This callback is invoked when the current
1133  /// TokenLexer hits the end of its token stream.
1134  bool HandleEndOfTokenLexer(Token &Result);
1135
1136  /// HandleDirective - This callback is invoked when the lexer sees a # token
1137  /// at the start of a line.  This consumes the directive, modifies the
1138  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
1139  /// read is the correct one.
1140  void HandleDirective(Token &Result);
1141
1142  /// CheckEndOfDirective - Ensure that the next token is a tok::eod token.  If
1143  /// not, emit a diagnostic and consume up until the eod.  If EnableMacros is
1144  /// true, then we consider macros that expand to zero tokens as being ok.
1145  void CheckEndOfDirective(const char *Directive, bool EnableMacros = false);
1146
1147  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1148  /// current line until the tok::eod token is found.
1149  void DiscardUntilEndOfDirective();
1150
1151  /// SawDateOrTime - This returns true if the preprocessor has seen a use of
1152  /// __DATE__ or __TIME__ in the file so far.
1153  bool SawDateOrTime() const {
1154    return DATELoc != SourceLocation() || TIMELoc != SourceLocation();
1155  }
1156  unsigned getCounterValue() const { return CounterValue; }
1157  void setCounterValue(unsigned V) { CounterValue = V; }
1158
1159  /// \brief Retrieves the module that we're currently building, if any.
1160  Module *getCurrentModule();
1161
1162  /// \brief Allocate a new MacroInfo object with the provided SourceLocation.
1163  MacroInfo *AllocateMacroInfo(SourceLocation L);
1164
1165  /// \brief Allocate a new MacroInfo object which is clone of \p MI.
1166  MacroInfo *CloneMacroInfo(const MacroInfo &MI);
1167
1168  /// \brief Turn the specified lexer token into a fully checked and spelled
1169  /// filename, e.g. as an operand of \#include.
1170  ///
1171  /// The caller is expected to provide a buffer that is large enough to hold
1172  /// the spelling of the filename, but is also expected to handle the case
1173  /// when this method decides to use a different buffer.
1174  ///
1175  /// \returns true if the input filename was in <>'s or false if it was
1176  /// in ""'s.
1177  bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Filename);
1178
1179  /// \brief Given a "foo" or \<foo> reference, look up the indicated file.
1180  ///
1181  /// Returns null on failure.  \p isAngled indicates whether the file
1182  /// reference is for system \#include's or not (i.e. using <> instead of "").
1183  const FileEntry *LookupFile(StringRef Filename,
1184                              bool isAngled, const DirectoryLookup *FromDir,
1185                              const DirectoryLookup *&CurDir,
1186                              SmallVectorImpl<char> *SearchPath,
1187                              SmallVectorImpl<char> *RelativePath,
1188                              Module **SuggestedModule,
1189                              bool SkipCache = false);
1190
1191  /// GetCurLookup - The DirectoryLookup structure used to find the current
1192  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
1193  /// implement \#include_next and find directory-specific properties.
1194  const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; }
1195
1196  /// \brief Return true if we're in the top-level file, not in a \#include.
1197  bool isInPrimaryFile() const;
1198
1199  /// ConcatenateIncludeName - Handle cases where the \#include name is expanded
1200  /// from a macro as multiple tokens, which need to be glued together.  This
1201  /// occurs for code like:
1202  /// \code
1203  ///    \#define FOO <x/y.h>
1204  ///    \#include FOO
1205  /// \endcode
1206  /// because in this case, "<x/y.h>" is returned as 7 tokens, not one.
1207  ///
1208  /// This code concatenates and consumes tokens up to the '>' token.  It
1209  /// returns false if the > was found, otherwise it returns true if it finds
1210  /// and consumes the EOD marker.
1211  bool ConcatenateIncludeName(SmallString<128> &FilenameBuffer,
1212                              SourceLocation &End);
1213
1214  /// LexOnOffSwitch - Lex an on-off-switch (C99 6.10.6p2) and verify that it is
1215  /// followed by EOD.  Return true if the token is not a valid on-off-switch.
1216  bool LexOnOffSwitch(tok::OnOffSwitch &OOS);
1217
1218private:
1219
1220  void PushIncludeMacroStack() {
1221    IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind,
1222                                                 CurLexer.take(),
1223                                                 CurPTHLexer.take(),
1224                                                 CurPPLexer,
1225                                                 CurTokenLexer.take(),
1226                                                 CurDirLookup));
1227    CurPPLexer = 0;
1228  }
1229
1230  void PopIncludeMacroStack() {
1231    CurLexer.reset(IncludeMacroStack.back().TheLexer);
1232    CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer);
1233    CurPPLexer = IncludeMacroStack.back().ThePPLexer;
1234    CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer);
1235    CurDirLookup  = IncludeMacroStack.back().TheDirLookup;
1236    CurLexerKind = IncludeMacroStack.back().CurLexerKind;
1237    IncludeMacroStack.pop_back();
1238  }
1239
1240  /// \brief Allocate a new MacroInfo object.
1241  MacroInfo *AllocateMacroInfo();
1242
1243  /// \brief Release the specified MacroInfo for re-use.
1244  ///
1245  /// This memory will  be reused for allocating new MacroInfo objects.
1246  void ReleaseMacroInfo(MacroInfo* MI);
1247
1248  /// ReadMacroName - Lex and validate a macro name, which occurs after a
1249  /// \#define or \#undef.  This emits a diagnostic, sets the token kind to eod,
1250  /// and discards the rest of the macro line if the macro name is invalid.
1251  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
1252
1253  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1254  /// definition has just been read.  Lex the rest of the arguments and the
1255  /// closing ), updating MI with what we learn and saving in LastTok the
1256  /// last token read.
1257  /// Return true if an error occurs parsing the arg list.
1258  bool ReadMacroDefinitionArgList(MacroInfo *MI, Token& LastTok);
1259
1260  /// We just read a \#if or related directive and decided that the
1261  /// subsequent tokens are in the \#if'd out portion of the
1262  /// file.  Lex the rest of the file, until we see an \#endif.  If \p
1263  /// FoundNonSkipPortion is true, then we have already emitted code for part of
1264  /// this \#if directive, so \#else/\#elif blocks should never be entered. If
1265  /// \p FoundElse is false, then \#else directives are ok, if not, then we have
1266  /// already seen one so a \#else directive is a duplicate.  When this returns,
1267  /// the caller can lex the first valid token.
1268  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1269                                    bool FoundNonSkipPortion, bool FoundElse,
1270                                    SourceLocation ElseLoc = SourceLocation());
1271
1272  /// \brief A fast PTH version of SkipExcludedConditionalBlock.
1273  void PTHSkipExcludedConditionalBlock();
1274
1275  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
1276  /// may occur after a #if or #elif directive and return it as a bool.  If the
1277  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
1278  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
1279
1280  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1281  /// \#pragma GCC poison/system_header/dependency and \#pragma once.
1282  void RegisterBuiltinPragmas();
1283
1284  /// \brief Register builtin macros such as __LINE__ with the identifier table.
1285  void RegisterBuiltinMacros();
1286
1287  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
1288  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
1289  /// the macro should not be expanded return true, otherwise return false.
1290  bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
1291
1292  /// \brief Cache macro expanded tokens for TokenLexers.
1293  //
1294  /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
1295  /// going to lex in the cache and when it finishes the tokens are removed
1296  /// from the end of the cache.
1297  Token *cacheMacroExpandedTokens(TokenLexer *tokLexer,
1298                                  ArrayRef<Token> tokens);
1299  void removeCachedMacroExpandedTokensOfLastLexer();
1300  friend void TokenLexer::ExpandFunctionArguments();
1301
1302  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
1303  /// lexed is a '('.  If so, consume the token and return true, if not, this
1304  /// method should have no observable side-effect on the lexed tokens.
1305  bool isNextPPTokenLParen();
1306
1307  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
1308  /// invoked to read all of the formal arguments specified for the macro
1309  /// invocation.  This returns null on error.
1310  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI,
1311                                       SourceLocation &ExpansionEnd);
1312
1313  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1314  /// as a builtin macro, handle it and return the next token as 'Tok'.
1315  void ExpandBuiltinMacro(Token &Tok);
1316
1317  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
1318  /// return the first token after the directive.  The _Pragma token has just
1319  /// been read into 'Tok'.
1320  void Handle_Pragma(Token &Tok);
1321
1322  /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
1323  /// is not enclosed within a string literal.
1324  void HandleMicrosoft__pragma(Token &Tok);
1325
1326  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
1327  /// start lexing tokens from it instead of the current buffer.
1328  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
1329
1330  /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and
1331  /// start getting tokens from it using the PTH cache.
1332  void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir);
1333
1334  /// IsFileLexer - Returns true if we are lexing from a file and not a
1335  ///  pragma or a macro.
1336  static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) {
1337    return L ? !L->isPragmaLexer() : P != 0;
1338  }
1339
1340  static bool IsFileLexer(const IncludeStackInfo& I) {
1341    return IsFileLexer(I.TheLexer, I.ThePPLexer);
1342  }
1343
1344  bool IsFileLexer() const {
1345    return IsFileLexer(CurLexer.get(), CurPPLexer);
1346  }
1347
1348  //===--------------------------------------------------------------------===//
1349  // Caching stuff.
1350  void CachingLex(Token &Result);
1351  bool InCachingLexMode() const {
1352    // If the Lexer pointers are 0 and IncludeMacroStack is empty, it means
1353    // that we are past EOF, not that we are in CachingLex mode.
1354    return CurPPLexer == 0 && CurTokenLexer == 0 && CurPTHLexer == 0 &&
1355           !IncludeMacroStack.empty();
1356  }
1357  void EnterCachingLexMode();
1358  void ExitCachingLexMode() {
1359    if (InCachingLexMode())
1360      RemoveTopOfLexerStack();
1361  }
1362  const Token &PeekAhead(unsigned N);
1363  void AnnotatePreviousCachedTokens(const Token &Tok);
1364
1365  //===--------------------------------------------------------------------===//
1366  /// Handle*Directive - implement the various preprocessor directives.  These
1367  /// should side-effect the current preprocessor object so that the next call
1368  /// to Lex() will return the appropriate token next.
1369  void HandleLineDirective(Token &Tok);
1370  void HandleDigitDirective(Token &Tok);
1371  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
1372  void HandleIdentSCCSDirective(Token &Tok);
1373  void HandleMacroPublicDirective(Token &Tok);
1374  void HandleMacroPrivateDirective(Token &Tok);
1375
1376  // File inclusion.
1377  void HandleIncludeDirective(SourceLocation HashLoc,
1378                              Token &Tok,
1379                              const DirectoryLookup *LookupFrom = 0,
1380                              bool isImport = false);
1381  void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok);
1382  void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok);
1383  void HandleImportDirective(SourceLocation HashLoc, Token &Tok);
1384  void HandleMicrosoftImportDirective(Token &Tok);
1385
1386  // Macro handling.
1387  void HandleDefineDirective(Token &Tok);
1388  void HandleUndefDirective(Token &Tok);
1389  void UndefineMacro(IdentifierInfo *II, MacroInfo *MI,
1390                     SourceLocation UndefLoc);
1391
1392  // Conditional Inclusion.
1393  void HandleIfdefDirective(Token &Tok, bool isIfndef,
1394                            bool ReadAnyTokensBeforeDirective);
1395  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
1396  void HandleEndifDirective(Token &Tok);
1397  void HandleElseDirective(Token &Tok);
1398  void HandleElifDirective(Token &Tok);
1399
1400  // Pragmas.
1401  void HandlePragmaDirective(unsigned Introducer);
1402public:
1403  void HandlePragmaOnce(Token &OnceTok);
1404  void HandlePragmaMark();
1405  void HandlePragmaPoison(Token &PoisonTok);
1406  void HandlePragmaSystemHeader(Token &SysHeaderTok);
1407  void HandlePragmaDependency(Token &DependencyTok);
1408  void HandlePragmaComment(Token &CommentTok);
1409  void HandlePragmaMessage(Token &MessageTok);
1410  void HandlePragmaPushMacro(Token &Tok);
1411  void HandlePragmaPopMacro(Token &Tok);
1412  void HandlePragmaIncludeAlias(Token &Tok);
1413  IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok);
1414
1415  // Return true and store the first token only if any CommentHandler
1416  // has inserted some tokens and getCommentRetentionState() is false.
1417  bool HandleComment(Token &Token, SourceRange Comment);
1418
1419  /// \brief A macro is used, update information about macros that need unused
1420  /// warnings.
1421  void markMacroAsUsed(MacroInfo *MI);
1422};
1423
1424/// \brief Abstract base class that describes a handler that will receive
1425/// source ranges for each of the comments encountered in the source file.
1426class CommentHandler {
1427public:
1428  virtual ~CommentHandler();
1429
1430  // The handler shall return true if it has pushed any tokens
1431  // to be read using e.g. EnterToken or EnterTokenStream.
1432  virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
1433};
1434
1435}  // end namespace clang
1436
1437#endif
1438