Preprocessor.h revision a9e274c01ebae45629d93aaa07be450fb77dd3cb
1d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org//===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===// 2d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// 3d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// The LLVM Compiler Infrastructure 4d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// 5d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// This file is distributed under the University of Illinois Open Source 6d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// License. See LICENSE.TXT for details. 7d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// 87f9bebe3906fb30f0632f9d010754d12dd6c02d7yusukes@chromium.org//===----------------------------------------------------------------------===// 9d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// 10cd35d0324e9cb7d46625ea1c0990dc75c69fc835bashi@chromium.org// This file defines the Preprocessor interface. 11d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org// 12d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org//===----------------------------------------------------------------------===// 13d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 1474343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H 15d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org#define LLVM_CLANG_LEX_PREPROCESSOR_H 16d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 17d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org#include "clang/Lex/Lexer.h" 1874343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org#include "clang/Lex/PPCallbacks.h" 1974343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org#include "clang/Lex/TokenLexer.h" 2074343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org#include "clang/Basic/IdentifierTable.h" 2174343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org#include "clang/Basic/SourceLocation.h" 2274343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org#include "llvm/ADT/DenseMap.h" 23d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 24d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgnamespace clang { 25d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 26d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass SourceManager; 27d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass FileManager; 28d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass FileEntry; 29d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass HeaderSearch; 30d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass PragmaNamespace; 31d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass PragmaHandler; 32d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass ScratchBuffer; 33d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass TargetInfo; 34d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass PPCallbacks; 35d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass DirectoryLookup; 36d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 37d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org/// Preprocessor - This object engages in a tight little dance with the lexer to 38d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org/// efficiently preprocess tokens. Lexers know only about tokens within a 39d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org/// single source file, and don't know anything about preprocessor-level issues 40d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org/// like the #include stack, token expansion, etc. 41d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org/// 42d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgclass Preprocessor { 43d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org Diagnostic &Diags; 44d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org const LangOptions &Features; 45d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org TargetInfo &Target; 46d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org FileManager &FileMgr; 47d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org SourceManager &SourceMgr; 48d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org ScratchBuffer *ScratchBuf; 49d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org HeaderSearch &HeaderInfo; 50d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 51d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// Identifiers for builtin macros and other builtins. 52d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__ 53d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__ 54d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__ 55d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__ 56d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__ 57d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *Ident_Pragma, *Ident__VA_ARGS__; // _Pragma, __VA_ARGS__ 58d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 59d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org SourceLocation DATELoc, TIMELoc; 60d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 61d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org enum { 62d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// MaxIncludeStackDepth - Maximum depth of #includes. 63d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org MaxAllowedIncludeStackDepth = 200 64d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org }; 65d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 66d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org // State that is set before the preprocessor begins. 67d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org bool KeepComments : 1; 68d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org bool KeepMacroComments : 1; 69d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 70d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org // State that changes while the preprocessor runs: 71d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org bool DisableMacroExpansion : 1; // True if macro expansion is disabled. 72d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org bool InMacroArgs : 1; // True if parsing fn macro invocation args. 73d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 74d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CacheTokens - True when the lexed tokens are cached for backtracking. 75d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org bool CacheTokens : 1; 76d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 77d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// Identifiers - This is mapping/lookup information for all identifiers in 78d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// the program, including program keywords. 79d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierTable Identifiers; 80d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 81d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// Selectors - This table contains all the selectors in the program. Unlike 82d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// IdentifierTable above, this table *isn't* populated by the preprocessor. 83d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// It is declared/instantiated here because it's role/lifetime is 84d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// conceptually similar the IdentifierTable. In addition, the current control 85d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// flow (in clang::ParseAST()), make it convenient to put here. 86d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to 87d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// the lifetime fo the preprocessor. 88d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org SelectorTable Selectors; 89d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 90d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// PragmaHandlers - This tracks all of the pragmas that the client registered 91d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// with this preprocessor. 92d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org PragmaNamespace *PragmaHandlers; 93d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 94d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CurLexer - This is the current top of the stack that we're lexing from if 95d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// not expanding a macro. One of CurLexer and CurTokenLexer must be null. 96d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org Lexer *CurLexer; 97d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 98d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CurLookup - The DirectoryLookup structure used to find the current 99d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// FileEntry, if CurLexer is non-null and if applicable. This allows us to 100d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// implement #include_next and find directory-specific properties. 101d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org const DirectoryLookup *CurDirLookup; 102d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 103d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CurTokenLexer - This is the current macro we are expanding, if we are 104d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// expanding a macro. One of CurLexer and CurTokenLexer must be null. 105d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org TokenLexer *CurTokenLexer; 106d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 107d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// IncludeMacroStack - This keeps track of the stack of files currently 108d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// #included, and macros currently being expanded from, not counting 109d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CurLexer/CurTokenLexer. 110d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org struct IncludeStackInfo { 111d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org Lexer *TheLexer; 11293aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com const DirectoryLookup *TheDirLookup; 11393aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com TokenLexer *TheTokenLexer; 11493aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com IncludeStackInfo(Lexer *L, const DirectoryLookup *D, TokenLexer *TL) 11593aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com : TheLexer(L), TheDirLookup(D), TheTokenLexer(TL) { 11693aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com } 11793aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com }; 11893aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com std::vector<IncludeStackInfo> IncludeMacroStack; 11993aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com 12093aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com /// Callbacks - These are actions invoked when some preprocessor activity is 12193aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com /// encountered (e.g. a file is #included, etc). 12293aedf7d27cb93f93077f9f7f758e830a392bdfcbashi@google.com PPCallbacks *Callbacks; 123d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 124d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping 125d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// to the actual definition of the macro. 126d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros; 127d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 128d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org // Various statistics we track for performance analysis. 129d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma; 130d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumIf, NumElse, NumEndif; 131d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumEnteredSourceFiles, MaxIncludeStackDepth; 132d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded; 133d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste; 134d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumSkipped; 135d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 136d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// Predefines - This string is the predefined macros that preprocessor 137d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// should use from the command line etc. 138d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org std::string Predefines; 139d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 140d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// TokenLexerCache - Cache macro expanders to reduce malloc traffic. 141d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org enum { TokenLexerCacheSize = 8 }; 142d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org unsigned NumCachedTokenLexers; 143d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org TokenLexer *TokenLexerCache[TokenLexerCacheSize]; 144d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 145d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org // Cached tokens state. 146d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 147d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org typedef std::vector<Token> CachedTokensTy; 148d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 149d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CachedTokens - Cached tokens are stored here when we do backtracking or 150d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// lookahead. They are "lexed" by the CachingLex() method. 151d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org CachedTokensTy CachedTokens; 152d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 153d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// CachedLexPos - The position of the cached token that CachingLex() should 154d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// "lex" next. If it points beyond the CachedTokens vector, it means that 1556263d06109011f6cff761aed14ded117c8fa1836yusukes@chromium.org /// a normal Lex() should be invoked. 156d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org CachedTokensTy::size_type CachedLexPos; 157d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 158d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// BacktrackPositions - Stack of backtrack positions, allowing nested 159d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to 160d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// indicate where CachedLexPos should be set when the BackTrack() method is 161d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// invoked (at which point the last position is popped). 162d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org std::vector<CachedTokensTy::size_type> BacktrackPositions; 163d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 164d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.orgpublic: 165d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target, 166d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org SourceManager &SM, HeaderSearch &Headers); 16774343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org ~Preprocessor(); 16874343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org 16974343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org Diagnostic &getDiagnostics() const { return Diags; } 17074343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org const LangOptions &getLangOptions() const { return Features; } 17174343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org TargetInfo &getTargetInfo() const { return Target; } 17274343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org FileManager &getFileManager() const { return FileMgr; } 17374343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org SourceManager &getSourceManager() const { return SourceMgr; } 17474343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; } 17574343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org 176a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org IdentifierTable &getIdentifierTable() { return Identifiers; } 177a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org SelectorTable &getSelectorTable() { return Selectors; } 178a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org 179a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org inline FullSourceLoc getFullLoc(SourceLocation Loc) const { 180a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org return FullSourceLoc(Loc, getSourceManager()); 181a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org } 182a416dc6e594169d1f809a08efbcf6de831dff291ksakamoto@chromium.org 18374343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org /// SetCommentRetentionState - Control whether or not the preprocessor retains 18474343ba7bd420519a9a4f7b22977d7771286f6f8ksakamoto@chromium.org /// comments in output. 185d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) { 186d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org this->KeepComments = KeepComments | KeepMacroComments; 187d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org this->KeepMacroComments = KeepMacroComments; 188d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org } 189d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 190d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org bool getCommentRetentionState() const { return KeepComments; } 19100b790a7ad0d65b066a61760f58e7dbfc055cd2dbashi@google.com 192d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// isCurrentLexer - Return true if we are lexing directly from the specified 193ced7112cb41d09cd344bbe4b20459049d6039491bashi@chromium.org /// lexer. 194a574866c0e4b1539098e64422f59d01c80cea6cfbashi@chromium.org bool isCurrentLexer(const Lexer *L) const { 195d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org return CurLexer == L; 196d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org } 197d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 198d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// getCurrentLexer - Return the current file lexer being lexed from. Note 199f12575f6e5a4b053188d4e205ae6ceb95a60fb2cyusukes@chromium.org /// that this ignores any potentially active macro expansions and _Pragma 200d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// expansions going on at the time. 201d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org Lexer *getCurrentFileLexer() const; 202eea48361b1ffcc1bded0ba25c8f747e634cd8e51ksakamoto@chromium.org 203d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// getPPCallbacks/setPPCallbacks - Accessors for preprocessor callbacks. 204d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// Note that this class takes ownership of any PPCallbacks object given to 205d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// it. 206d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org PPCallbacks *getPPCallbacks() const { return Callbacks; } 207d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org void setPPCallbacks(PPCallbacks *C) { 208d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org delete Callbacks; 2094dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org Callbacks = C; 2104dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org } 2114dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org 212d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to 213d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// or null if it isn't #define'd. 214d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org MacroInfo *getMacroInfo(IdentifierInfo *II) const { 215d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org return II->hasMacroDefinition() ? Macros.find(II)->second : 0; 216d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org } 217d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 218d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// setMacroInfo - Specify a macro for this identifier. 219d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// 220d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org void setMacroInfo(IdentifierInfo *II, MacroInfo *MI); 221d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 222d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org const std::string &getPredefines() const { return Predefines; } 223d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// setPredefines - Set the predefines for this Preprocessor. These 224d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// predefines are automatically injected when parsing the main file. 225d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org void setPredefines(const char *P) { Predefines = P; } 226d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org void setPredefines(const std::string &P) { Predefines = P; } 227d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 228d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// getIdentifierInfo - Return information about the specified preprocessor 229d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// identifier token. The version of this method that takes two character 230d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// pointers is preferred unless the identifier is already available as a 231d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// string (this avoids allocation and copying of memory to construct an 232d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// std::string). 233d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org IdentifierInfo *getIdentifierInfo(const char *NameStart, 234d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org const char *NameEnd) { 2354dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org return &Identifiers.get(NameStart, NameEnd); 2364dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org } 2374dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org IdentifierInfo *getIdentifierInfo(const char *NameStr) { 2384dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org return getIdentifierInfo(NameStr, NameStr+strlen(NameStr)); 2394dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org } 2404dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org 2414dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. 2424dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org /// If 'Namespace' is non-null, then it is a token required to exist on the 2434dcad600f99d85201d9db3cb8bee166ec2aaca85bashi@chromium.org /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". 244d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org void AddPragmaHandler(const char *Namespace, PragmaHandler *Handler); 245d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org 246d257d186ae2a08042a412824678f98241a1a4f3cyusukes@chromium.org /// EnterMainSourceFile - Enter the specified FileID as the main source file, 247 /// which implicitly adds the builtin defines etc. 248 void EnterMainSourceFile(); 249 250 /// EnterSourceFile - Add a source file to the top of the include stack and 251 /// start lexing tokens from it instead of the current buffer. If isMainFile 252 /// is true, this is the main file for the translation unit. 253 void EnterSourceFile(unsigned CurFileID, const DirectoryLookup *Dir); 254 255 /// EnterMacro - Add a Macro to the top of the include stack and start lexing 256 /// tokens from it instead of the current buffer. Args specifies the 257 /// tokens input to a function-like macro. 258 void EnterMacro(Token &Identifier, MacroArgs *Args); 259 260 /// EnterTokenStream - Add a "macro" context to the top of the include stack, 261 /// which will cause the lexer to start returning the specified tokens. 262 /// 263 /// If DisableMacroExpansion is true, tokens lexed from the token stream will 264 /// not be subject to further macro expansion. Otherwise, these tokens will 265 /// be re-macro-expanded when/if expansion is enabled. 266 /// 267 /// If OwnsTokens is false, this method assumes that the specified stream of 268 /// tokens has a permanent owner somewhere, so they do not need to be copied. 269 /// If it is true, it assumes the array of tokens is allocated with new[] and 270 /// must be freed. 271 /// 272 void EnterTokenStream(const Token *Toks, unsigned NumToks, 273 bool DisableMacroExpansion, bool OwnsTokens); 274 275 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the 276 /// lexer stack. This should only be used in situations where the current 277 /// state of the top-of-stack lexer is known. 278 void RemoveTopOfLexerStack(); 279 280 /// EnableBacktrackAtThisPos - From the point that this method is called, and 281 /// until DisableBacktrack() or Backtrack() is called, the Preprocessor keeps 282 /// track of the lexed tokens so that a subsequent Backtrack() call will make 283 /// the Preprocessor re-lex the same tokens. 284 /// 285 /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can 286 /// be called multiple times and DisableBacktrack/Backtrack calls will be 287 /// combined with the EnableBacktrackAtThisPos calls in reverse order. 288 /// 289 /// NOTE: *DO NOT* forget to call either DisableBacktrack() or Backtrack() at 290 /// some point after EnableBacktrackAtThisPos. If you don't, caching of tokens 291 /// will continue indefinitely. 292 /// 293 void EnableBacktrackAtThisPos(); 294 295 /// DisableBacktrack - Disable the last EnableBacktrackAtThisPos() call. 296 void DisableBacktrack(); 297 298 /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since 299 /// EnableBacktrackAtThisPos() was previously called. 300 void Backtrack(); 301 302 /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and 303 /// caching of tokens is on. 304 bool isBacktrackEnabled() const { return CacheTokens; } 305 306 /// Lex - To lex a token from the preprocessor, just pull a token from the 307 /// current lexer or macro object. 308 void Lex(Token &Result) { 309 if (CurLexer) 310 CurLexer->Lex(Result); 311 else if (CurTokenLexer) 312 CurTokenLexer->Lex(Result); 313 else 314 CachingLex(Result); 315 } 316 317 /// LexNonComment - Lex a token. If it's a comment, keep lexing until we get 318 /// something not a comment. This is useful in -E -C mode where comments 319 /// would foul up preprocessor directive handling. 320 void LexNonComment(Token &Result) { 321 do 322 Lex(Result); 323 while (Result.getKind() == tok::comment); 324 } 325 326 /// LexUnexpandedToken - This is just like Lex, but this disables macro 327 /// expansion of identifier tokens. 328 void LexUnexpandedToken(Token &Result) { 329 // Disable macro expansion. 330 bool OldVal = DisableMacroExpansion; 331 DisableMacroExpansion = true; 332 // Lex the token. 333 Lex(Result); 334 335 // Reenable it. 336 DisableMacroExpansion = OldVal; 337 } 338 339 /// LookAhead - This peeks ahead N tokens and returns that token without 340 /// consuming any tokens. LookAhead(0) returns the next token that would be 341 /// returned by Lex(), LookAhead(1) returns the token after it, etc. This 342 /// returns normal tokens after phase 5. As such, it is equivalent to using 343 /// 'Lex', not 'LexUnexpandedToken'. 344 const Token &LookAhead(unsigned N) { 345 if (CachedLexPos + N < CachedTokens.size()) 346 return CachedTokens[CachedLexPos+N]; 347 else 348 return PeekAhead(N+1); 349 } 350 351 /// Diag - Forwarding function for diagnostics. This emits a diagnostic at 352 /// the specified Token's location, translating the token's start 353 /// position in the current buffer into a SourcePosition object for rendering. 354 void Diag(SourceLocation Loc, unsigned DiagID); 355 void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg); 356 void Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, 357 const SourceRange &R1, const SourceRange &R2); 358 void Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R); 359 void Diag(SourceLocation Loc, unsigned DiagID, const SourceRange &R1, 360 const SourceRange &R2); 361 void Diag(const Token &Tok, unsigned DiagID) { 362 Diag(Tok.getLocation(), DiagID); 363 } 364 void Diag(const Token &Tok, unsigned DiagID, const std::string &Msg) { 365 Diag(Tok.getLocation(), DiagID, Msg); 366 } 367 368 /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a 369 /// token is the characters used to represent the token in the source file 370 /// after trigraph expansion and escaped-newline folding. In particular, this 371 /// wants to get the true, uncanonicalized, spelling of things like digraphs 372 /// UCNs, etc. 373 std::string getSpelling(const Token &Tok) const; 374 375 /// getSpelling - This method is used to get the spelling of a token into a 376 /// preallocated buffer, instead of as an std::string. The caller is required 377 /// to allocate enough space for the token, which is guaranteed to be at least 378 /// Tok.getLength() bytes long. The length of the actual result is returned. 379 /// 380 /// Note that this method may do two possible things: it may either fill in 381 /// the buffer specified with characters, or it may *change the input pointer* 382 /// to point to a constant buffer with the data already in it (avoiding a 383 /// copy). The caller is not allowed to modify the returned buffer pointer 384 /// if an internal buffer is returned. 385 unsigned getSpelling(const Token &Tok, const char *&Buffer) const; 386 387 388 /// CreateString - Plop the specified string into a scratch buffer and return 389 /// a location for it. If specified, the source location provides a source 390 /// location for the token. 391 SourceLocation CreateString(const char *Buf, unsigned Len, 392 SourceLocation SourceLoc = SourceLocation()); 393 394 /// DumpToken - Print the token to stderr, used for debugging. 395 /// 396 void DumpToken(const Token &Tok, bool DumpFlags = false) const; 397 void DumpLocation(SourceLocation Loc) const; 398 void DumpMacro(const MacroInfo &MI) const; 399 400 /// AdvanceToTokenCharacter - Given a location that specifies the start of a 401 /// token, return a new location that specifies a character within the token. 402 SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,unsigned Char); 403 404 /// IncrementPasteCounter - Increment the counters for the number of token 405 /// paste operations performed. If fast was specified, this is a 'fast paste' 406 /// case we handled. 407 /// 408 void IncrementPasteCounter(bool isFast) { 409 if (isFast) 410 ++NumFastTokenPaste; 411 else 412 ++NumTokenPaste; 413 } 414 415 void PrintStats(); 416 417 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a 418 /// comment (/##/) in microsoft mode, this method handles updating the current 419 /// state, returning the token on the next source line. 420 void HandleMicrosoftCommentPaste(Token &Tok); 421 422 //===--------------------------------------------------------------------===// 423 // Preprocessor callback methods. These are invoked by a lexer as various 424 // directives and events are found. 425 426 /// LookUpIdentifierInfo - Given a tok::identifier token, look up the 427 /// identifier information for the token and install it into the token. 428 IdentifierInfo *LookUpIdentifierInfo(Token &Identifier, 429 const char *BufPtr = 0); 430 431 /// HandleIdentifier - This callback is invoked when the lexer reads an 432 /// identifier and has filled in the tokens IdentifierInfo member. This 433 /// callback potentially macro expands it or turns it into a named token (like 434 /// 'for'). 435 void HandleIdentifier(Token &Identifier); 436 437 438 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of 439 /// the current file. This either returns the EOF token and returns true, or 440 /// pops a level off the include stack and returns false, at which point the 441 /// client should call lex again. 442 bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false); 443 444 /// HandleEndOfTokenLexer - This callback is invoked when the current 445 /// TokenLexer hits the end of its token stream. 446 bool HandleEndOfTokenLexer(Token &Result); 447 448 /// HandleDirective - This callback is invoked when the lexer sees a # token 449 /// at the start of a line. This consumes the directive, modifies the 450 /// lexer/preprocessor state, and advances the lexer(s) so that the next token 451 /// read is the correct one. 452 void HandleDirective(Token &Result); 453 454 /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If 455 /// not, emit a diagnostic and consume up until the eom. 456 void CheckEndOfDirective(const char *Directive); 457private: 458 /// isInPrimaryFile - Return true if we're in the top-level file, not in a 459 /// #include. 460 bool isInPrimaryFile() const; 461 462 /// isSystemHeader - Return true if F is a system header. 463 bool isSystemHeader(const FileEntry* F) const; 464 465 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the 466 /// current line until the tok::eom token is found. 467 void DiscardUntilEndOfDirective(); 468 469 /// ReadMacroName - Lex and validate a macro name, which occurs after a 470 /// #define or #undef. This emits a diagnostic, sets the token kind to eom, 471 /// and discards the rest of the macro line if the macro name is invalid. 472 void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0); 473 474 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro 475 /// definition has just been read. Lex the rest of the arguments and the 476 /// closing ), updating MI with what we learn. Return true if an error occurs 477 /// parsing the arg list. 478 bool ReadMacroDefinitionArgList(MacroInfo *MI); 479 480 /// SkipExcludedConditionalBlock - We just read a #if or related directive and 481 /// decided that the subsequent tokens are in the #if'd out portion of the 482 /// file. Lex the rest of the file, until we see an #endif. If 483 /// FoundNonSkipPortion is true, then we have already emitted code for part of 484 /// this #if directive, so #else/#elif blocks should never be entered. If 485 /// FoundElse is false, then #else directives are ok, if not, then we have 486 /// already seen one so a #else directive is a duplicate. When this returns, 487 /// the caller can lex the first valid token. 488 void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, 489 bool FoundNonSkipPortion, bool FoundElse); 490 491 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that 492 /// may occur after a #if or #elif directive and return it as a bool. If the 493 /// expression is equivalent to "!defined(X)" return X in IfNDefMacro. 494 bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro); 495 496 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: 497 /// #pragma GCC poison/system_header/dependency and #pragma once. 498 void RegisterBuiltinPragmas(); 499 500 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the 501 /// identifier table. 502 void RegisterBuiltinMacros(); 503 IdentifierInfo *RegisterBuiltinMacro(const char *Name); 504 505 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to 506 /// be expanded as a macro, handle it and return the next token as 'Tok'. If 507 /// the macro should not be expanded return true, otherwise return false. 508 bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI); 509 510 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be 511 /// lexed is a '('. If so, consume the token and return true, if not, this 512 /// method should have no observable side-effect on the lexed tokens. 513 bool isNextPPTokenLParen(); 514 515 /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is 516 /// invoked to read all of the formal arguments specified for the macro 517 /// invocation. This returns null on error. 518 MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI); 519 520 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 521 /// as a builtin macro, handle it and return the next token as 'Tok'. 522 void ExpandBuiltinMacro(Token &Tok); 523 524 /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then 525 /// return the first token after the directive. The _Pragma token has just 526 /// been read into 'Tok'. 527 void Handle_Pragma(Token &Tok); 528 529 530 /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and 531 /// start lexing tokens from it instead of the current buffer. 532 void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir); 533 534 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully 535 /// checked and spelled filename, e.g. as an operand of #include. This returns 536 /// true if the input filename was in <>'s or false if it were in ""'s. The 537 /// caller is expected to provide a buffer that is large enough to hold the 538 /// spelling of the filename, but is also expected to handle the case when 539 /// this method decides to use a different buffer. 540 bool GetIncludeFilenameSpelling(SourceLocation Loc, 541 const char *&BufStart, const char *&BufEnd); 542 543 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 544 /// return null on failure. isAngled indicates whether the file reference is 545 /// for system #include's or not (i.e. using <> instead of ""). 546 const FileEntry *LookupFile(const char *FilenameStart,const char *FilenameEnd, 547 bool isAngled, const DirectoryLookup *FromDir, 548 const DirectoryLookup *&CurDir); 549 550 //===--------------------------------------------------------------------===// 551 // Caching stuff. 552 void CachingLex(Token &Result); 553 bool InCachingLexMode() const { return CurLexer == 0 && CurTokenLexer == 0; } 554 void EnterCachingLexMode(); 555 void ExitCachingLexMode() { 556 if (InCachingLexMode()) 557 RemoveTopOfLexerStack(); 558 } 559 const Token &PeekAhead(unsigned N); 560 561 //===--------------------------------------------------------------------===// 562 /// Handle*Directive - implement the various preprocessor directives. These 563 /// should side-effect the current preprocessor object so that the next call 564 /// to Lex() will return the appropriate token next. 565 566 void HandleUserDiagnosticDirective(Token &Tok, bool isWarning); 567 void HandleIdentSCCSDirective(Token &Tok); 568 569 // File inclusion. 570 void HandleIncludeDirective(Token &Tok, 571 const DirectoryLookup *LookupFrom = 0, 572 bool isImport = false); 573 void HandleIncludeNextDirective(Token &Tok); 574 void HandleImportDirective(Token &Tok); 575 576 // Macro handling. 577 void HandleDefineDirective(Token &Tok); 578 void HandleUndefDirective(Token &Tok); 579 // HandleAssertDirective(Token &Tok); 580 // HandleUnassertDirective(Token &Tok); 581 582 // Conditional Inclusion. 583 void HandleIfdefDirective(Token &Tok, bool isIfndef, 584 bool ReadAnyTokensBeforeDirective); 585 void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective); 586 void HandleEndifDirective(Token &Tok); 587 void HandleElseDirective(Token &Tok); 588 void HandleElifDirective(Token &Tok); 589 590 // Pragmas. 591 void HandlePragmaDirective(); 592public: 593 void HandlePragmaOnce(Token &OnceTok); 594 void HandlePragmaMark(); 595 void HandlePragmaPoison(Token &PoisonTok); 596 void HandlePragmaSystemHeader(Token &SysHeaderTok); 597 void HandlePragmaDependency(Token &DependencyTok); 598}; 599 600/// PreprocessorFactory - A generic factory interface for lazily creating 601/// Preprocessor objects on-demand when they are needed. 602class PreprocessorFactory { 603public: 604 virtual ~PreprocessorFactory(); 605 virtual Preprocessor* CreatePreprocessor() = 0; 606}; 607 608} // end namespace clang 609 610#endif 611