Preprocessor.h revision 8ed3044a33679cbfa0617d465a50ec557d671ed7
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file defines the Preprocessor interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#define LLVM_CLANG_LEX_PREPROCESSOR_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Lexer.h"
18eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner#include "clang/Lex/PPCallbacks.h"
195d75de0f821023f4ed4815825bf3aea8a0b5e40dChris Lattner#include "clang/Lex/TokenLexer.h"
20c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner#include "clang/Basic/IdentifierTable.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceLocation.h"
22cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner#include "llvm/ADT/DenseMap.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass SourceManager;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileManager;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass FileEntry;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass HeaderSearch;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaNamespace;
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PragmaHandler;
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass ScratchBuffer;
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass TargetInfo;
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass PPCallbacks;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass DirectoryLookup;
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37e5956bd2730c051835f9acd9e957c5d79f99e7c3Chris Lattner/// Preprocessor - This object engages in a tight little dance with the lexer to
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// efficiently preprocess tokens.  Lexers know only about tokens within a
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// single source file, and don't know anything about preprocessor-level issues
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// like the #include stack, token expansion, etc.
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass Preprocessor {
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Diagnostic        &Diags;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const LangOptions &Features;
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  TargetInfo        &Target;
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager       &FileMgr;
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager     &SourceMgr;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ScratchBuffer     *ScratchBuf;
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch      &HeaderInfo;
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers for builtin macros and other builtins.
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__LINE__, *Ident__FILE__;   // __LINE__, __FILE__
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__DATE__, *Ident__TIME__;   // __DATE__, __TIME__
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__INCLUDE_LEVEL__;          // __INCLUDE_LEVEL__
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__BASE_FILE__;              // __BASE_FILE__
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident__TIMESTAMP__;              // __TIMESTAMP__
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *Ident_Pragma, *Ident__VA_ARGS__; // _Pragma, __VA_ARGS__
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation DATELoc, TIMELoc;
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  enum {
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    /// MaxIncludeStackDepth - Maximum depth of #includes.
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    MaxAllowedIncludeStackDepth = 200
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that is set before the preprocessor begins.
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepComments : 1;
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool KeepMacroComments : 1;
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // State that changes while the preprocessor runs:
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool DisableMacroExpansion : 1;  // True if macro expansion is disabled.
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool InMacroArgs : 1;            // True if parsing fn macro invocation args.
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Identifiers - This is mapping/lookup information for all identifiers in
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the program, including program keywords.
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierTable Identifiers;
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// Selectors - This table contains all the selectors in the program. Unlike
7968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// IdentifierTable above, this table *isn't* populated by the preprocessor.
8068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// It is declared/instantiated here because it's role/lifetime is
8168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// conceptually similar the IdentifierTable. In addition, the current control
8268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// flow (in clang::ParseAST()), make it convenient to put here.
8368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to
8468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  /// the lifetime fo the preprocessor.
8529238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable Selectors;
8668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PragmaHandlers - This tracks all of the pragmas that the client registered
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// with this preprocessor.
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PragmaNamespace *PragmaHandlers;
905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLexer - This is the current top of the stack that we're lexing from if
926cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// not expanding a macro.  One of CurLexer and CurTokenLexer must be null.
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Lexer *CurLexer;
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CurLookup - The DirectoryLookup structure used to find the current
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FileEntry, if CurLexer is non-null and if applicable.  This allows us to
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// implement #include_next and find directory-specific properties.
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const DirectoryLookup *CurDirLookup;
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1006cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurTokenLexer - This is the current macro we are expanding, if we are
1016cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// expanding a macro.  One of CurLexer and CurTokenLexer must be null.
1026cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  TokenLexer *CurTokenLexer;
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncludeMacroStack - This keeps track of the stack of files currently
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// #included, and macros currently being expanded from, not counting
1066cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// CurLexer/CurTokenLexer.
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  struct IncludeStackInfo {
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Lexer *TheLexer;
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    const DirectoryLookup *TheDirLookup;
1106cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner    TokenLexer *TheTokenLexer;
1111543e9c69202b8e128c7fe34784ae7aa90964889Chris Lattner    IncludeStackInfo(Lexer *L, const DirectoryLookup *D, TokenLexer *TL)
1126cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner      : TheLexer(L), TheDirLookup(D), TheTokenLexer(TL) {
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  };
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  std::vector<IncludeStackInfo> IncludeMacroStack;
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Callbacks - These are actions invoked when some preprocessor activity is
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// encountered (e.g. a file is #included, etc).
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *Callbacks;
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
121cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping
122cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// to the actual definition of the macro.
123cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros;
124cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Various statistics we track for performance analysis.
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma;
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumIf, NumElse, NumEndif;
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumEnteredSourceFiles, MaxIncludeStackDepth;
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded;
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste;
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumSkipped;
1329594acf32de2939b15eafa8fe818607bfc56bf66Chris Lattner
133aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// Predefines - This string is the predefined macros that preprocessor
134aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// should use from the command line etc.
135aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  std::string Predefines;
13653b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner
1376cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  /// TokenLexerCache - Cache macro expanders to reduce malloc traffic.
1386cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  enum { TokenLexerCacheSize = 8 };
1396cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  unsigned NumCachedTokenLexers;
1406cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner  TokenLexer *TokenLexerCache[TokenLexerCacheSize];
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target,
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer               SourceManager &SM, HeaderSearch &Headers);
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ~Preprocessor();
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Diagnostic &getDiagnostics() const { return Diags; }
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const LangOptions &getLangOptions() const { return Features; }
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  TargetInfo &getTargetInfo() const { return Target; }
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FileManager &getFileManager() const { return FileMgr; }
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceManager &getSourceManager() const { return SourceMgr; }
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; }
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierTable &getIdentifierTable() { return Identifiers; }
15429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable &getSelectorTable() { return Selectors; }
15568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
156e0579f08d4a4ae57971b74ca1863106df4c6399fTed Kremenek  inline FullSourceLoc getFullLoc(SourceLocation Loc) const {
15794b3cdb57fd5d245963e597626e0dfd88d479795Chris Lattner    return FullSourceLoc(Loc, getSourceManager());
158ea8646993741739d8a04d67396fe466dcc3a104fTed Kremenek  }
159ea8646993741739d8a04d67396fe466dcc3a104fTed Kremenek
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// SetCommentRetentionState - Control whether or not the preprocessor retains
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// comments in output.
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) {
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepComments = KeepComments | KeepMacroComments;
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    this->KeepMacroComments = KeepMacroComments;
1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool getCommentRetentionState() const { return KeepComments; }
1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isCurrentLexer - Return true if we are lexing directly from the specified
1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer.
1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCurrentLexer(const Lexer *L) const {
1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return CurLexer == L;
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getCurrentLexer - Return the current file lexer being lexed from.  Note
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// that this ignores any potentially active macro expansions and _Pragma
1775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansions going on at the time.
1785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Lexer *getCurrentFileLexer() const;
1795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
180eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// getPPCallbacks/setPPCallbacks - Accessors for preprocessor callbacks.
181eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// Note that this class takes ownership of any PPCallbacks object given to
182eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner  /// it.
1835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PPCallbacks *getPPCallbacks() const { return Callbacks; }
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setPPCallbacks(PPCallbacks *C) {
185eb50ed88c2aa040fac08bf2a50bde4dd3da6eb19Chris Lattner    delete Callbacks;
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Callbacks = C;
1875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
189cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to
190cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// or null if it isn't #define'd.
191cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  MacroInfo *getMacroInfo(IdentifierInfo *II) const {
192cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner    return II->hasMacroDefinition() ? Macros.find(II)->second : 0;
193cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  }
194cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner
195cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  /// setMacroInfo - Specify a macro for this identifier.
196cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  ///
197cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner  void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
198cc1a875f94630e58d24a55577ffbf0e89b7da8c7Chris Lattner
199aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// setPredefines - Set the predefines for this Preprocessor.  These
200aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  /// predefines are automatically injected when parsing the main file.
201aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const char *P) { Predefines = P; }
202aa39197431a0a0b1326ecf6b3be6a11f6e2f8503Chris Lattner  void setPredefines(const std::string &P) { Predefines = P; }
20353b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner
2045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getIdentifierInfo - Return information about the specified preprocessor
2055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier token.  The version of this method that takes two character
2065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pointers is preferred unless the identifier is already available as a
2075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// string (this avoids allocation and copying of memory to construct an
2085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// std::string).
2095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *getIdentifierInfo(const char *NameStart,
2105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                    const char *NameEnd) {
2115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return &Identifiers.get(NameStart, NameEnd);
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *getIdentifierInfo(const char *NameStr) {
2145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getIdentifierInfo(NameStr, NameStr+strlen(NameStr));
2155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
2185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If 'Namespace' is non-null, then it is a token required to exist on the
2195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
2205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void AddPragmaHandler(const char *Namespace, PragmaHandler *Handler);
2215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22253b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  /// EnterMainSourceFile - Enter the specified FileID as the main source file,
2237dcc968f17a6ff9088c9651dddccc8d4025a1271Ted Kremenek  /// which implicitly adds the builtin defines etc.
22495041a2029a069386ee67439f6d0fb524a9d184fTed Kremenek  void EnterMainSourceFile();
22553b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner
2265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFile - Add a source file to the top of the include stack and
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// start lexing tokens from it instead of the current buffer.  If isMainFile
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// is true, this is the main file for the translation unit.
22953b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  void EnterSourceFile(unsigned CurFileID, const DirectoryLookup *Dir);
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterMacro - Add a Macro to the top of the include stack and start lexing
2325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens from it instead of the current buffer.  Args specifies the
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens input to a function-like macro.
234d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void EnterMacro(Token &Identifier, MacroArgs *Args);
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterTokenStream - Add a "macro" context to the top of the include stack,
2376b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// which will cause the lexer to start returning the specified tokens.
2386b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
2396b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If DisableMacroExpansion is true, tokens lexed from the token stream will
2406b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// not be subject to further macro expansion.  Otherwise, these tokens will
2416b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// be re-macro-expanded when/if expansion is enabled.
2426b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
2436b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If OwnsTokens is false, this method assumes that the specified stream of
2446b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// tokens has a permanent owner somewhere, so they do not need to be copied.
2456b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// If it is true, it assumes the array of tokens is allocated with new[] and
2466b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// must be freed.
2476b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
2486b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  void EnterTokenStream(const Token *Toks, unsigned NumToks,
2496b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner                        bool DisableMacroExpansion, bool OwnsTokens);
2505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer stack.  This should only be used in situations where the current
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// state of the top-of-stack lexer is known.
2545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RemoveTopOfLexerStack();
2555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Lex - To lex a token from the preprocessor, just pull a token from the
2575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// current lexer or macro object.
258d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Lex(Token &Result) {
2595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (CurLexer)
2605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      CurLexer->Lex(Result);
2615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else
2626cfe7594a46b5d270142cfcb688a9c1a3a487a48Chris Lattner      CurTokenLexer->Lex(Result);
2635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
2665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// something not a comment.  This is useful in -E -C mode where comments
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// would foul up preprocessor directive handling.
268d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexNonComment(Token &Result) {
2695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    do
2705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Lex(Result);
2715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    while (Result.getKind() == tok::comment);
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LexUnexpandedToken - This is just like Lex, but this disables macro
2755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expansion of identifier tokens.
276d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void LexUnexpandedToken(Token &Result) {
2775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Disable macro expansion.
2785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    bool OldVal = DisableMacroExpansion;
2795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = true;
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Lex the token.
2815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Lex(Result);
2825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Reenable it.
2845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    DisableMacroExpansion = OldVal;
2855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2876b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// LookAhead - This peeks ahead N tokens and returns that token without
2886b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// consuming any tokens.  LookAhead(0) returns the next token that would be
2896b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returned by Lex(), LookAhead(1) returns the token after it, etc.  This
2906b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// returns normal tokens after phase 5.  As such, it is equivalent to using
2916b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// 'Lex', not 'LexUnexpandedToken'.
2926b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
2936b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// NOTE: is a relatively expensive method, so it should not be used in common
2946b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  /// code paths if possible!
2956b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  ///
2966b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner  Token LookAhead(unsigned N);
2976b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
299d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  /// the specified Token's location, translating the token's start
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// position in the current buffer into a SourcePosition object for rendering.
3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void Diag(SourceLocation Loc, unsigned DiagID);
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg);
3038ed3044a33679cbfa0617d465a50ec557d671ed7Chris Lattner  void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
3048ed3044a33679cbfa0617d465a50ec557d671ed7Chris Lattner            const SourceRange &R1, const SourceRange &R2);
3058ed3044a33679cbfa0617d465a50ec557d671ed7Chris Lattner  void Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R);
3068ed3044a33679cbfa0617d465a50ec557d671ed7Chris Lattner  void Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R1,
3078ed3044a33679cbfa0617d465a50ec557d671ed7Chris Lattner            const SourceRange &R2);
308d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Diag(const Token &Tok, unsigned DiagID) {
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok.getLocation(), DiagID);
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
311d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Diag(const Token &Tok, unsigned DiagID, const std::string &Msg) {
3125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Diag(Tok.getLocation(), DiagID, Msg);
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling() - Return the 'spelling' of the Tok token.  The spelling of a
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// token is the characters used to represent the token in the source file
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// after trigraph expansion and escaped-newline folding.  In particular, this
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// wants to get the true, uncanonicalized, spelling of things like digraphs
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// UCNs, etc.
320d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  std::string getSpelling(const Token &Tok) const;
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getSpelling - This method is used to get the spelling of a token into a
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// preallocated buffer, instead of as an std::string.  The caller is required
3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to allocate enough space for the token, which is guaranteed to be at least
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Tok.getLength() bytes long.  The length of the actual result is returned.
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Note that this method may do two possible things: it may either fill in
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the buffer specified with characters, or it may *change the input pointer*
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to point to a constant buffer with the data already in it (avoiding a
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// copy).  The caller is not allowed to modify the returned buffer pointer
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// if an internal buffer is returned.
332d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  unsigned getSpelling(const Token &Tok, const char *&Buffer) const;
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CreateString - Plop the specified string into a scratch buffer and return
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// a location for it.  If specified, the source location provides a source
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// location for the token.
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation CreateString(const char *Buf, unsigned Len,
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              SourceLocation SourceLoc = SourceLocation());
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// DumpToken - Print the token to stderr, used for debugging.
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
343d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void DumpToken(const Token &Tok, bool DumpFlags = false) const;
344c3d8d57b010e2ed15a2a7685d5761db14f5d2252Chris Lattner  void DumpLocation(SourceLocation Loc) const;
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void DumpMacro(const MacroInfo &MI) const;
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
34797ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// AdvanceToTokenCharacter - Given a location that specifies the start of a
34897ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  /// token, return a new location that specifies a character within the token.
34997ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner  SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,unsigned Char);
35097ba77cf09bf7b83b679165ce67ad7d49ffd568cChris Lattner
3515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IncrementPasteCounter - Increment the counters for the number of token
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// paste operations performed.  If fast was specified, this is a 'fast paste'
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// case we handled.
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void IncrementPasteCounter(bool isFast) {
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (isFast)
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumFastTokenPaste;
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumTokenPaste;
3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats();
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3643f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
3653f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// comment (/##/) in microsoft mode, this method handles updating the current
3663f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  /// state, returning the token on the next source line.
3673f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner  void HandleMicrosoftCommentPaste(Token &Tok);
3683f1cc838f9caf469990f23fccd0940263c0c61ffChris Lattner
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Preprocessor callback methods.  These are invoked by a lexer as various
3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // directives and events are found.
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LookUpIdentifierInfo - Given a tok::identifier token, look up the
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier information for the token and install it into the token.
375d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  IdentifierInfo *LookUpIdentifierInfo(Token &Identifier,
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                       const char *BufPtr = 0);
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleIdentifier - This callback is invoked when the lexer reads an
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier and has filled in the tokens IdentifierInfo member.  This
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// callback potentially macro expands it or turns it into a named token (like
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// 'for').
382d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentifier(Token &Identifier);
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
3865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the current file.  This either returns the EOF token and returns true, or
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// pops a level off the include stack and returns false, at which point the
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// client should call lex again.
389d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false);
3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
391fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// HandleEndOfTokenLexer - This callback is invoked when the current
392fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  /// TokenLexer hits the end of its token stream.
393fde2bf9befede63e3f01f84519784c17b4c81480Chris Lattner  bool HandleEndOfTokenLexer(Token &Result);
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleDirective - This callback is invoked when the lexer sees a # token
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// at the start of a line.  This consumes the directive, modifies the
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexer/preprocessor state, and advances the lexer(s) so that the next token
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// read is the correct one.
399d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleDirective(Token &Result);
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CheckEndOfDirective - Ensure that the next token is a tok::eom token.  If
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// not, emit a diagnostic and consume up until the eom.
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void CheckEndOfDirective(const char *Directive);
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerprivate:
40553b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  /// isInPrimaryFile - Return true if we're in the top-level file, not in a
40653b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  /// #include.
40753b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner  bool isInPrimaryFile() const;
40853b0dabbe52219a8057659b90539837394ef0fa1Chris Lattner
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// current line until the tok::eom token is found.
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void DiscardUntilEndOfDirective();
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroName - Lex and validate a macro name, which occurs after a
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// #define or #undef.  This emits a diagnostic, sets the token kind to eom,
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// and discards the rest of the macro line if the macro name is invalid.
416d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0);
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// definition has just been read.  Lex the rest of the arguments and the
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// closing ), updating MI with what we learn.  Return true if an error occurs
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// parsing the arg list.
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool ReadMacroDefinitionArgList(MacroInfo *MI);
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// SkipExcludedConditionalBlock - We just read a #if or related directive and
4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// decided that the subsequent tokens are in the #if'd out portion of the
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// file.  Lex the rest of the file, until we see an #endif.  If
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FoundNonSkipPortion is true, then we have already emitted code for part of
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this #if directive, so #else/#elif blocks should never be entered. If
4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// FoundElse is false, then #else directives are ok, if not, then we have
4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// already seen one so a #else directive is a duplicate.  When this returns,
4315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the caller can lex the first valid token.
4325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                    bool FoundNonSkipPortion, bool FoundElse);
4345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateDirectiveExpression - Evaluate an integer constant expression that
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// may occur after a #if or #elif directive and return it as a bool.  If the
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression is equivalent to "!defined(X)" return X in IfNDefMacro.
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro);
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
4415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// #pragma GCC poison/system_header/dependency and #pragma once.
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinPragmas();
4435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
4455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier table.
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void RegisterBuiltinMacros();
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo *RegisterBuiltinMacro(const char *Name);
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// HandleMacroExpandedIdentifier - If an identifier token is read that is to
4505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// be expanded as a macro, handle it and return the next token as 'Tok'.  If
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the macro should not be expanded return true, otherwise return false.
452d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
4535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lexed is a '('.  If so, consume the token and return true, if not, this
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// method should have no observable side-effect on the lexed tokens.
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isNextPPTokenLParen();
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invoked to read all of the formal arguments specified for the macro
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// invocation.  This returns null on error.
462d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI);
4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// as a builtin macro, handle it and return the next token as 'Tok'.
466d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void ExpandBuiltinMacro(Token &Tok);
4675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// return the first token after the directive.  The _Pragma token has just
4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// been read into 'Tok'.
471d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void Handle_Pragma(Token &Tok);
4725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and
4755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// start lexing tokens from it instead of the current buffer.
4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir);
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
4795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// checked and spelled filename, e.g. as an operand of #include. This returns
4805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// true if the input filename was in <>'s or false if it were in ""'s.  The
4815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// caller is expected to provide a buffer that is large enough to hold the
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// spelling of the filename, but is also expected to handle the case when
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this method decides to use a different buffer.
484f1c99acc544a4e70f308db4e7200ca04cd5a06d2Chris Lattner  bool GetIncludeFilenameSpelling(SourceLocation Loc,
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                  const char *&BufStart, const char *&BufEnd);
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// return null on failure.  isAngled indicates whether the file reference is
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// for system #include's or not (i.e. using <> instead of "").
4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const FileEntry *LookupFile(const char *FilenameStart,const char *FilenameEnd,
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              bool isAngled, const DirectoryLookup *FromDir,
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const DirectoryLookup *&CurDir);
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Handle*Directive - implement the various preprocessor directives.  These
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// should side-effect the current preprocessor object so that the next call
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// to Lex() will return the appropriate token next.
4985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
499d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUserDiagnosticDirective(Token &Tok, bool isWarning);
500d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIdentSCCSDirective(Token &Tok);
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // File inclusion.
503d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIncludeDirective(Token &Tok,
5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              const DirectoryLookup *LookupFrom = 0,
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              bool isImport = false);
506d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIncludeNextDirective(Token &Tok);
507d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleImportDirective(Token &Tok);
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Macro handling.
51042e6737f2efb113563140ad794c21c7709250402Chris Lattner  void HandleDefineDirective(Token &Tok);
511d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleUndefDirective(Token &Tok);
512d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  // HandleAssertDirective(Token &Tok);
513d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  // HandleUnassertDirective(Token &Tok);
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Conditional Inclusion.
516d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfdefDirective(Token &Tok, bool isIfndef,
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                            bool ReadAnyTokensBeforeDirective);
518d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective);
519d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleEndifDirective(Token &Tok);
520d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElseDirective(Token &Tok);
521d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandleElifDirective(Token &Tok);
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Pragmas.
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void HandlePragmaDirective();
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
526d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaOnce(Token &OnceTok);
5272243449253475574fc6f14986ff8f7fce5d46799Chris Lattner  void HandlePragmaMark();
528d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaPoison(Token &PoisonTok);
529d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaSystemHeader(Token &SysHeaderTok);
530d217773f106856a11879ec79dc468efefaf2ee75Chris Lattner  void HandlePragmaDependency(Token &DependencyTok);
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
533ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenek/// PreprocessorFactory - A generic factory interface for lazily creating
534ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenek///  Preprocessor objects on-demand when they are needed.
535ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenekclass PreprocessorFactory {
536ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenekpublic:
537ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenek  virtual ~PreprocessorFactory();
538ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenek  virtual Preprocessor* CreatePreprocessor() = 0;
539ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenek};
540ec6c574478a22008847d7ebc2498ef3336752096Ted Kremenek
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
544