Preprocessor.h revision 17d527b051fbc3927b8a1b4ce4607a9b2ed445ee
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===--- Preprocessor.h - C Language Family Preprocessor --------*- C++ -*-===// 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The LLVM Compiler Infrastructure 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details. 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file defines the Preprocessor interface. 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef LLVM_CLANG_LEX_PREPROCESSOR_H 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define LLVM_CLANG_LEX_PREPROCESSOR_H 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/Lexer.h" 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/Lex/PTHLexer.h" 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/PPCallbacks.h" 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/TokenLexer.h" 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Lex/PTHManager.h" 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Diagnostic.h" 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/IdentifierTable.h" 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/SourceLocation.h" 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/DenseMap.h" 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/OwningPtr.h" 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Allocator.h" 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace clang { 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SourceManager; 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileManager; 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileEntry; 342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class HeaderSearch; 352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PragmaNamespace; 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PragmaHandler; 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ScratchBuffer; 382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class TargetInfo; 392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PPCallbacks; 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DirectoryLookup; 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// Preprocessor - This object engages in a tight little dance with the lexer to 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// efficiently preprocess tokens. Lexers know only about tokens within a 442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// single source file, and don't know anything about preprocessor-level issues 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// like the #include stack, token expansion, etc. 462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Preprocessor { 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Diagnostic &Diags; 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const LangOptions &Features; 502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TargetInfo &Target; 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FileManager &FileMgr; 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SourceManager &SourceMgr; 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScratchBuffer *ScratchBuf; 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) HeaderSearch &HeaderInfo; 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// PTH - An optional PTHManager object used for getting tokens from 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// a token cache rather than lexing the original source file. 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) llvm::OwningPtr<PTHManager> PTH; 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// BP - A BumpPtrAllocator object used to quickly allocate and release 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// objects internal to the Preprocessor. 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) llvm::BumpPtrAllocator BP; 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Identifiers for builtin macros and other builtins. 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__ 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__ 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__ 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierInfo *Ident__BASE_FILE__; // __BASE_FILE__ 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierInfo *Ident__TIMESTAMP__; // __TIMESTAMP__ 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierInfo *Ident_Pragma, *Ident__VA_ARGS__; // _Pragma, __VA_ARGS__ 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SourceLocation DATELoc, TIMELoc; 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) enum { 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// MaxIncludeStackDepth - Maximum depth of #includes. 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) MaxAllowedIncludeStackDepth = 200 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // State that is set before the preprocessor begins. 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool KeepComments : 1; 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool KeepMacroComments : 1; 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // State that changes while the preprocessor runs: 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool DisableMacroExpansion : 1; // True if macro expansion is disabled. 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool InMacroArgs : 1; // True if parsing fn macro invocation args. 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Identifiers - This is mapping/lookup information for all identifiers in 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// the program, including program keywords. 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IdentifierTable Identifiers; 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Selectors - This table contains all the selectors in the program. Unlike 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// IdentifierTable above, this table *isn't* populated by the preprocessor. 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// It is declared/instantiated here because it's role/lifetime is 942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// conceptually similar the IdentifierTable. In addition, the current control 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// flow (in clang::ParseAST()), make it convenient to put here. 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// FIXME: Make sure the lifetime of Identifiers/Selectors *isn't* tied to 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// the lifetime fo the preprocessor. 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SelectorTable Selectors; 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// PragmaHandlers - This tracks all of the pragmas that the client registered 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// with this preprocessor. 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PragmaNamespace *PragmaHandlers; 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CurLexer - This is the current top of the stack that we're lexing from if 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// not expanding a macro and we are lexing directly from source code. 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null. 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) llvm::OwningPtr<Lexer> CurLexer; 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CurPTHLexer - This is the current top of stack that we're lexing from if 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// not expanding from a macro and we are lexing from a PTH cache. 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Only one of CurLexer, CurPTHLexer, or CurTokenLexer will be non-null. 1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) llvm::OwningPtr<PTHLexer> CurPTHLexer; 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 114868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// CurPPLexer - This is the current top of the stack what we're lexing from 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// if not expanding a macro. This is an alias for either CurLexer or 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CurPTHLexer. 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PreprocessorLexer* CurPPLexer; 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CurLookup - The DirectoryLookup structure used to find the current 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// FileEntry, if CurLexer is non-null and if applicable. This allows us to 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// implement #include_next and find directory-specific properties. 1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const DirectoryLookup *CurDirLookup; 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CurTokenLexer - This is the current macro we are expanding, if we are 1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// expanding a macro. One of CurLexer and CurTokenLexer must be null. 1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) llvm::OwningPtr<TokenLexer> CurTokenLexer; 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// IncludeMacroStack - This keeps track of the stack of files currently 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// #included, and macros currently being expanded from, not counting 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CurLexer/CurTokenLexer. 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct IncludeStackInfo { 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Lexer *TheLexer; 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PTHLexer *ThePTHLexer; 1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PreprocessorLexer *ThePPLexer; 13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) TokenLexer *TheTokenLexer; 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const DirectoryLookup *TheDirLookup; 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) IncludeStackInfo(Lexer *L, PTHLexer* P, PreprocessorLexer* PPL, 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TokenLexer* TL, const DirectoryLookup *D) 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL), TheTokenLexer(TL), 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) TheDirLookup(D) {} 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<IncludeStackInfo> IncludeMacroStack; 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Callbacks - These are actions invoked when some preprocessor activity is 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// encountered (e.g. a file is #included, etc). 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PPCallbacks *Callbacks; 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Macros - For each IdentifierInfo with 'HasMacro' set, we keep a mapping 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// to the actual definition of the macro. 1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) llvm::DenseMap<IdentifierInfo*, MacroInfo*> Macros; 1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// MICache - A "freelist" of MacroInfo objects that can be reused for quick 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// allocation. 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<MacroInfo*> MICache; 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Various statistics we track for performance analysis. 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned NumDirectives, NumIncluded, NumDefined, NumUndefined, NumPragma; 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned NumIf, NumElse, NumEndif; 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned NumEnteredSourceFiles, MaxIncludeStackDepth; 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned NumMacroExpanded, NumFnMacroExpanded, NumBuiltinMacroExpanded; 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned NumFastMacroExpanded, NumTokenPaste, NumFastTokenPaste; 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned NumSkipped; 1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// Predefines - This string is the predefined macros that preprocessor 1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// should use from the command line etc. 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string Predefines; 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// TokenLexerCache - Cache macro expanders to reduce malloc traffic. 170 enum { TokenLexerCacheSize = 8 }; 171 unsigned NumCachedTokenLexers; 172 TokenLexer *TokenLexerCache[TokenLexerCacheSize]; 173 174private: // Cached tokens state. 175 typedef std::vector<Token> CachedTokensTy; 176 177 /// CachedTokens - Cached tokens are stored here when we do backtracking or 178 /// lookahead. They are "lexed" by the CachingLex() method. 179 CachedTokensTy CachedTokens; 180 181 /// CachedLexPos - The position of the cached token that CachingLex() should 182 /// "lex" next. If it points beyond the CachedTokens vector, it means that 183 /// a normal Lex() should be invoked. 184 CachedTokensTy::size_type CachedLexPos; 185 186 /// BacktrackPositions - Stack of backtrack positions, allowing nested 187 /// backtracks. The EnableBacktrackAtThisPos() method pushes a position to 188 /// indicate where CachedLexPos should be set when the BackTrack() method is 189 /// invoked (at which point the last position is popped). 190 std::vector<CachedTokensTy::size_type> BacktrackPositions; 191 192public: 193 Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target, 194 SourceManager &SM, HeaderSearch &Headers, 195 IdentifierInfoLookup* IILookup = 0); 196 197 ~Preprocessor(); 198 199 Diagnostic &getDiagnostics() const { return Diags; } 200 const LangOptions &getLangOptions() const { return Features; } 201 TargetInfo &getTargetInfo() const { return Target; } 202 FileManager &getFileManager() const { return FileMgr; } 203 SourceManager &getSourceManager() const { return SourceMgr; } 204 HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; } 205 206 IdentifierTable &getIdentifierTable() { return Identifiers; } 207 SelectorTable &getSelectorTable() { return Selectors; } 208 209 void setPTHManager(PTHManager* pm) { PTH.reset(pm); } 210 211 /// SetCommentRetentionState - Control whether or not the preprocessor retains 212 /// comments in output. 213 void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) { 214 this->KeepComments = KeepComments | KeepMacroComments; 215 this->KeepMacroComments = KeepMacroComments; 216 } 217 218 bool getCommentRetentionState() const { return KeepComments; } 219 220 /// isCurrentLexer - Return true if we are lexing directly from the specified 221 /// lexer. 222 bool isCurrentLexer(const PreprocessorLexer *L) const { 223 return CurPPLexer == L; 224 } 225 226 /// getCurrentLexer - Return the current file lexer being lexed from. Note 227 /// that this ignores any potentially active macro expansions and _Pragma 228 /// expansions going on at the time. 229 PreprocessorLexer *getCurrentFileLexer() const; 230 231 /// getPPCallbacks/setPPCallbacks - Accessors for preprocessor callbacks. 232 /// Note that this class takes ownership of any PPCallbacks object given to 233 /// it. 234 PPCallbacks *getPPCallbacks() const { return Callbacks; } 235 void setPPCallbacks(PPCallbacks *C) { 236 delete Callbacks; 237 Callbacks = C; 238 } 239 240 /// getMacroInfo - Given an identifier, return the MacroInfo it is #defined to 241 /// or null if it isn't #define'd. 242 MacroInfo *getMacroInfo(IdentifierInfo *II) const { 243 return II->hasMacroDefinition() ? Macros.find(II)->second : 0; 244 } 245 246 /// setMacroInfo - Specify a macro for this identifier. 247 /// 248 void setMacroInfo(IdentifierInfo *II, MacroInfo *MI); 249 250 const std::string &getPredefines() const { return Predefines; } 251 /// setPredefines - Set the predefines for this Preprocessor. These 252 /// predefines are automatically injected when parsing the main file. 253 void setPredefines(const char *P) { Predefines = P; } 254 void setPredefines(const std::string &P) { Predefines = P; } 255 256 /// getIdentifierInfo - Return information about the specified preprocessor 257 /// identifier token. The version of this method that takes two character 258 /// pointers is preferred unless the identifier is already available as a 259 /// string (this avoids allocation and copying of memory to construct an 260 /// std::string). 261 IdentifierInfo *getIdentifierInfo(const char *NameStart, 262 const char *NameEnd) { 263 return &Identifiers.get(NameStart, NameEnd); 264 } 265 IdentifierInfo *getIdentifierInfo(const char *NameStr) { 266 return getIdentifierInfo(NameStr, NameStr+strlen(NameStr)); 267 } 268 269 /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. 270 /// If 'Namespace' is non-null, then it is a token required to exist on the 271 /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". 272 void AddPragmaHandler(const char *Namespace, PragmaHandler *Handler); 273 274 /// RemovePragmaHandler - Remove the specific pragma handler from 275 /// the preprocessor. If \arg Namespace is non-null, then it should 276 /// be the namespace that \arg Handler was added to. It is an error 277 /// to remove a handler that has not been registered. 278 void RemovePragmaHandler(const char *Namespace, PragmaHandler *Handler); 279 280 /// EnterMainSourceFile - Enter the specified FileID as the main source file, 281 /// which implicitly adds the builtin defines etc. 282 void EnterMainSourceFile(); 283 284 /// EnterSourceFile - Add a source file to the top of the include stack and 285 /// start lexing tokens from it instead of the current buffer. If isMainFile 286 /// is true, this is the main file for the translation unit. 287 void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir); 288 289 /// EnterMacro - Add a Macro to the top of the include stack and start lexing 290 /// tokens from it instead of the current buffer. Args specifies the 291 /// tokens input to a function-like macro. 292 void EnterMacro(Token &Identifier, MacroArgs *Args); 293 294 /// EnterTokenStream - Add a "macro" context to the top of the include stack, 295 /// which will cause the lexer to start returning the specified tokens. 296 /// 297 /// If DisableMacroExpansion is true, tokens lexed from the token stream will 298 /// not be subject to further macro expansion. Otherwise, these tokens will 299 /// be re-macro-expanded when/if expansion is enabled. 300 /// 301 /// If OwnsTokens is false, this method assumes that the specified stream of 302 /// tokens has a permanent owner somewhere, so they do not need to be copied. 303 /// If it is true, it assumes the array of tokens is allocated with new[] and 304 /// must be freed. 305 /// 306 void EnterTokenStream(const Token *Toks, unsigned NumToks, 307 bool DisableMacroExpansion, bool OwnsTokens); 308 309 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the 310 /// lexer stack. This should only be used in situations where the current 311 /// state of the top-of-stack lexer is known. 312 void RemoveTopOfLexerStack(); 313 314 /// EnableBacktrackAtThisPos - From the point that this method is called, and 315 /// until CommitBacktrackedTokens() or Backtrack() is called, the Preprocessor 316 /// keeps track of the lexed tokens so that a subsequent Backtrack() call will 317 /// make the Preprocessor re-lex the same tokens. 318 /// 319 /// Nested backtracks are allowed, meaning that EnableBacktrackAtThisPos can 320 /// be called multiple times and CommitBacktrackedTokens/Backtrack calls will 321 /// be combined with the EnableBacktrackAtThisPos calls in reverse order. 322 /// 323 /// NOTE: *DO NOT* forget to call either CommitBacktrackedTokens or Backtrack 324 /// at some point after EnableBacktrackAtThisPos. If you don't, caching of 325 /// tokens will continue indefinitely. 326 /// 327 void EnableBacktrackAtThisPos(); 328 329 /// CommitBacktrackedTokens - Disable the last EnableBacktrackAtThisPos call. 330 void CommitBacktrackedTokens(); 331 332 /// Backtrack - Make Preprocessor re-lex the tokens that were lexed since 333 /// EnableBacktrackAtThisPos() was previously called. 334 void Backtrack(); 335 336 /// isBacktrackEnabled - True if EnableBacktrackAtThisPos() was called and 337 /// caching of tokens is on. 338 bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); } 339 340 /// Lex - To lex a token from the preprocessor, just pull a token from the 341 /// current lexer or macro object. 342 void Lex(Token &Result) { 343 if (CurLexer) 344 CurLexer->Lex(Result); 345 else if (CurPTHLexer) 346 CurPTHLexer->Lex(Result); 347 else if (CurTokenLexer) 348 CurTokenLexer->Lex(Result); 349 else 350 CachingLex(Result); 351 } 352 353 /// LexNonComment - Lex a token. If it's a comment, keep lexing until we get 354 /// something not a comment. This is useful in -E -C mode where comments 355 /// would foul up preprocessor directive handling. 356 void LexNonComment(Token &Result) { 357 do 358 Lex(Result); 359 while (Result.getKind() == tok::comment); 360 } 361 362 /// LexUnexpandedToken - This is just like Lex, but this disables macro 363 /// expansion of identifier tokens. 364 void LexUnexpandedToken(Token &Result) { 365 // Disable macro expansion. 366 bool OldVal = DisableMacroExpansion; 367 DisableMacroExpansion = true; 368 // Lex the token. 369 Lex(Result); 370 371 // Reenable it. 372 DisableMacroExpansion = OldVal; 373 } 374 375 /// LookAhead - This peeks ahead N tokens and returns that token without 376 /// consuming any tokens. LookAhead(0) returns the next token that would be 377 /// returned by Lex(), LookAhead(1) returns the token after it, etc. This 378 /// returns normal tokens after phase 5. As such, it is equivalent to using 379 /// 'Lex', not 'LexUnexpandedToken'. 380 const Token &LookAhead(unsigned N) { 381 if (CachedLexPos + N < CachedTokens.size()) 382 return CachedTokens[CachedLexPos+N]; 383 else 384 return PeekAhead(N+1); 385 } 386 387 /// RevertCachedTokens - When backtracking is enabled and tokens are cached, 388 /// this allows to revert a specific number of tokens. 389 /// Note that the number of tokens being reverted should be up to the last 390 /// backtrack position, not more. 391 void RevertCachedTokens(unsigned N) { 392 assert(isBacktrackEnabled() && 393 "Should only be called when tokens are cached for backtracking"); 394 assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back()) 395 && "Should revert tokens up to the last backtrack position, not more"); 396 assert(signed(CachedLexPos) - signed(N) >= 0 && 397 "Corrupted backtrack positions ?"); 398 CachedLexPos -= N; 399 } 400 401 /// EnterToken - Enters a token in the token stream to be lexed next. If 402 /// BackTrack() is called afterwards, the token will remain at the insertion 403 /// point. 404 void EnterToken(const Token &Tok) { 405 EnterCachingLexMode(); 406 CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok); 407 } 408 409 /// AnnotateCachedTokens - We notify the Preprocessor that if it is caching 410 /// tokens (because backtrack is enabled) it should replace the most recent 411 /// cached tokens with the given annotation token. This function has no effect 412 /// if backtracking is not enabled. 413 /// 414 /// Note that the use of this function is just for optimization; so that the 415 /// cached tokens doesn't get re-parsed and re-resolved after a backtrack is 416 /// invoked. 417 void AnnotateCachedTokens(const Token &Tok) { 418 assert(Tok.isAnnotationToken() && "Expected annotation token"); 419 if (CachedLexPos != 0 && isBacktrackEnabled()) 420 AnnotatePreviousCachedTokens(Tok); 421 } 422 423 /// Diag - Forwarding function for diagnostics. This emits a diagnostic at 424 /// the specified Token's location, translating the token's start 425 /// position in the current buffer into a SourcePosition object for rendering. 426 DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) { 427 return Diags.Report(FullSourceLoc(Loc, getSourceManager()), DiagID); 428 } 429 430 DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) { 431 return Diags.Report(FullSourceLoc(Tok.getLocation(), getSourceManager()), 432 DiagID); 433 } 434 435 /// getSpelling() - Return the 'spelling' of the Tok token. The spelling of a 436 /// token is the characters used to represent the token in the source file 437 /// after trigraph expansion and escaped-newline folding. In particular, this 438 /// wants to get the true, uncanonicalized, spelling of things like digraphs 439 /// UCNs, etc. 440 std::string getSpelling(const Token &Tok) const; 441 442 /// getSpelling - This method is used to get the spelling of a token into a 443 /// preallocated buffer, instead of as an std::string. The caller is required 444 /// to allocate enough space for the token, which is guaranteed to be at least 445 /// Tok.getLength() bytes long. The length of the actual result is returned. 446 /// 447 /// Note that this method may do two possible things: it may either fill in 448 /// the buffer specified with characters, or it may *change the input pointer* 449 /// to point to a constant buffer with the data already in it (avoiding a 450 /// copy). The caller is not allowed to modify the returned buffer pointer 451 /// if an internal buffer is returned. 452 unsigned getSpelling(const Token &Tok, const char *&Buffer) const; 453 454 /// getSpelledCharacterAt - Return a pointer to the start of the specified 455 /// location in the appropriate MemoryBuffer. 456 char getSpelledCharacterAt(SourceLocation SL) const { 457 if (PTH) { 458 const char *Data; 459 if (PTH->getSpelling(SL, Data)) 460 return *Data; 461 } 462 463 return *SourceMgr.getCharacterData(SL); 464 } 465 466 /// CreateString - Plop the specified string into a scratch buffer and return 467 /// a location for it. If specified, the source location provides a source 468 /// location for the token. 469 SourceLocation CreateString(const char *Buf, unsigned Len, 470 SourceLocation SourceLoc = SourceLocation()); 471 472 /// DumpToken - Print the token to stderr, used for debugging. 473 /// 474 void DumpToken(const Token &Tok, bool DumpFlags = false) const; 475 void DumpLocation(SourceLocation Loc) const; 476 void DumpMacro(const MacroInfo &MI) const; 477 478 /// AdvanceToTokenCharacter - Given a location that specifies the start of a 479 /// token, return a new location that specifies a character within the token. 480 SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart,unsigned Char); 481 482 /// IncrementPasteCounter - Increment the counters for the number of token 483 /// paste operations performed. If fast was specified, this is a 'fast paste' 484 /// case we handled. 485 /// 486 void IncrementPasteCounter(bool isFast) { 487 if (isFast) 488 ++NumFastTokenPaste; 489 else 490 ++NumTokenPaste; 491 } 492 493 void PrintStats(); 494 495 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a 496 /// comment (/##/) in microsoft mode, this method handles updating the current 497 /// state, returning the token on the next source line. 498 void HandleMicrosoftCommentPaste(Token &Tok); 499 500 //===--------------------------------------------------------------------===// 501 // Preprocessor callback methods. These are invoked by a lexer as various 502 // directives and events are found. 503 504 /// LookUpIdentifierInfo - Given a tok::identifier token, look up the 505 /// identifier information for the token and install it into the token. 506 IdentifierInfo *LookUpIdentifierInfo(Token &Identifier, 507 const char *BufPtr = 0); 508 509 /// HandleIdentifier - This callback is invoked when the lexer reads an 510 /// identifier and has filled in the tokens IdentifierInfo member. This 511 /// callback potentially macro expands it or turns it into a named token (like 512 /// 'for'). 513 void HandleIdentifier(Token &Identifier); 514 515 516 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of 517 /// the current file. This either returns the EOF token and returns true, or 518 /// pops a level off the include stack and returns false, at which point the 519 /// client should call lex again. 520 bool HandleEndOfFile(Token &Result, bool isEndOfMacro = false); 521 522 /// HandleEndOfTokenLexer - This callback is invoked when the current 523 /// TokenLexer hits the end of its token stream. 524 bool HandleEndOfTokenLexer(Token &Result); 525 526 /// HandleDirective - This callback is invoked when the lexer sees a # token 527 /// at the start of a line. This consumes the directive, modifies the 528 /// lexer/preprocessor state, and advances the lexer(s) so that the next token 529 /// read is the correct one. 530 void HandleDirective(Token &Result); 531 532 /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If 533 /// not, emit a diagnostic and consume up until the eom. 534 void CheckEndOfDirective(const char *Directive); 535private: 536 537 void PushIncludeMacroStack() { 538 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer.take(), 539 CurPTHLexer.take(), 540 CurPPLexer, 541 CurTokenLexer.take(), 542 CurDirLookup)); 543 CurPPLexer = 0; 544 } 545 546 void PopIncludeMacroStack() { 547 CurLexer.reset(IncludeMacroStack.back().TheLexer); 548 CurPTHLexer.reset(IncludeMacroStack.back().ThePTHLexer); 549 CurPPLexer = IncludeMacroStack.back().ThePPLexer; 550 CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer); 551 CurDirLookup = IncludeMacroStack.back().TheDirLookup; 552 IncludeMacroStack.pop_back(); 553 } 554 555 /// AllocateMacroInfo - Allocate a new MacroInfo object with the provide 556 /// SourceLocation. 557 MacroInfo* AllocateMacroInfo(SourceLocation L); 558 559 /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will 560 /// be reused for allocating new MacroInfo objects. 561 void ReleaseMacroInfo(MacroInfo* MI) { 562 MICache.push_back(MI); 563 } 564 565 /// isInPrimaryFile - Return true if we're in the top-level file, not in a 566 /// #include. 567 bool isInPrimaryFile() const; 568 569 /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the 570 /// current line until the tok::eom token is found. 571 void DiscardUntilEndOfDirective(); 572 573 /// ReadMacroName - Lex and validate a macro name, which occurs after a 574 /// #define or #undef. This emits a diagnostic, sets the token kind to eom, 575 /// and discards the rest of the macro line if the macro name is invalid. 576 void ReadMacroName(Token &MacroNameTok, char isDefineUndef = 0); 577 578 /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro 579 /// definition has just been read. Lex the rest of the arguments and the 580 /// closing ), updating MI with what we learn. Return true if an error occurs 581 /// parsing the arg list. 582 bool ReadMacroDefinitionArgList(MacroInfo *MI); 583 584 /// SkipExcludedConditionalBlock - We just read a #if or related directive and 585 /// decided that the subsequent tokens are in the #if'd out portion of the 586 /// file. Lex the rest of the file, until we see an #endif. If 587 /// FoundNonSkipPortion is true, then we have already emitted code for part of 588 /// this #if directive, so #else/#elif blocks should never be entered. If 589 /// FoundElse is false, then #else directives are ok, if not, then we have 590 /// already seen one so a #else directive is a duplicate. When this returns, 591 /// the caller can lex the first valid token. 592 void SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, 593 bool FoundNonSkipPortion, bool FoundElse); 594 595 /// PTHSkipExcludedConditionalBlock - A fast PTH version of 596 /// SkipExcludedConditionalBlock. 597 void PTHSkipExcludedConditionalBlock(); 598 599 /// EvaluateDirectiveExpression - Evaluate an integer constant expression that 600 /// may occur after a #if or #elif directive and return it as a bool. If the 601 /// expression is equivalent to "!defined(X)" return X in IfNDefMacro. 602 bool EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro); 603 604 /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: 605 /// #pragma GCC poison/system_header/dependency and #pragma once. 606 void RegisterBuiltinPragmas(); 607 608 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the 609 /// identifier table. 610 void RegisterBuiltinMacros(); 611 IdentifierInfo *RegisterBuiltinMacro(const char *Name); 612 613 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to 614 /// be expanded as a macro, handle it and return the next token as 'Tok'. If 615 /// the macro should not be expanded return true, otherwise return false. 616 bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI); 617 618 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be 619 /// lexed is a '('. If so, consume the token and return true, if not, this 620 /// method should have no observable side-effect on the lexed tokens. 621 bool isNextPPTokenLParen(); 622 623 /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is 624 /// invoked to read all of the formal arguments specified for the macro 625 /// invocation. This returns null on error. 626 MacroArgs *ReadFunctionLikeMacroArgs(Token &MacroName, MacroInfo *MI); 627 628 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 629 /// as a builtin macro, handle it and return the next token as 'Tok'. 630 void ExpandBuiltinMacro(Token &Tok); 631 632 /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then 633 /// return the first token after the directive. The _Pragma token has just 634 /// been read into 'Tok'. 635 void Handle_Pragma(Token &Tok); 636 637 /// EnterSourceFileWithLexer - Add a lexer to the top of the include stack and 638 /// start lexing tokens from it instead of the current buffer. 639 void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir); 640 641 /// EnterSourceFileWithPTH - Add a lexer to the top of the include stack and 642 /// start getting tokens from it using the PTH cache. 643 void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir); 644 645 /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully 646 /// checked and spelled filename, e.g. as an operand of #include. This returns 647 /// true if the input filename was in <>'s or false if it were in ""'s. The 648 /// caller is expected to provide a buffer that is large enough to hold the 649 /// spelling of the filename, but is also expected to handle the case when 650 /// this method decides to use a different buffer. 651 bool GetIncludeFilenameSpelling(SourceLocation Loc, 652 const char *&BufStart, const char *&BufEnd); 653 654 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 655 /// return null on failure. isAngled indicates whether the file reference is 656 /// for system #include's or not (i.e. using <> instead of ""). 657 const FileEntry *LookupFile(const char *FilenameStart,const char *FilenameEnd, 658 bool isAngled, const DirectoryLookup *FromDir, 659 const DirectoryLookup *&CurDir); 660 661 662 663 /// IsFileLexer - Returns true if we are lexing from a file and not a 664 /// pragma or a macro. 665 static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) { 666 return L ? !L->isPragmaLexer() : P != 0; 667 } 668 669 static bool IsFileLexer(const IncludeStackInfo& I) { 670 return IsFileLexer(I.TheLexer, I.ThePPLexer); 671 } 672 673 bool IsFileLexer() const { 674 return IsFileLexer(CurLexer.get(), CurPPLexer); 675 } 676 677 //===--------------------------------------------------------------------===// 678 // Caching stuff. 679 void CachingLex(Token &Result); 680 bool InCachingLexMode() const { return CurPPLexer == 0 && CurTokenLexer == 0;} 681 void EnterCachingLexMode(); 682 void ExitCachingLexMode() { 683 if (InCachingLexMode()) 684 RemoveTopOfLexerStack(); 685 } 686 const Token &PeekAhead(unsigned N); 687 void AnnotatePreviousCachedTokens(const Token &Tok); 688 689 //===--------------------------------------------------------------------===// 690 /// Handle*Directive - implement the various preprocessor directives. These 691 /// should side-effect the current preprocessor object so that the next call 692 /// to Lex() will return the appropriate token next. 693 694 void HandleUserDiagnosticDirective(Token &Tok, bool isWarning); 695 void HandleIdentSCCSDirective(Token &Tok); 696 697 // File inclusion. 698 void HandleIncludeDirective(Token &Tok, 699 const DirectoryLookup *LookupFrom = 0, 700 bool isImport = false); 701 void HandleIncludeNextDirective(Token &Tok); 702 void HandleImportDirective(Token &Tok); 703 704 // Macro handling. 705 void HandleDefineDirective(Token &Tok); 706 void HandleUndefDirective(Token &Tok); 707 // HandleAssertDirective(Token &Tok); 708 // HandleUnassertDirective(Token &Tok); 709 710 // Conditional Inclusion. 711 void HandleIfdefDirective(Token &Tok, bool isIfndef, 712 bool ReadAnyTokensBeforeDirective); 713 void HandleIfDirective(Token &Tok, bool ReadAnyTokensBeforeDirective); 714 void HandleEndifDirective(Token &Tok); 715 void HandleElseDirective(Token &Tok); 716 void HandleElifDirective(Token &Tok); 717 718 // Pragmas. 719 void HandlePragmaDirective(); 720public: 721 void HandlePragmaOnce(Token &OnceTok); 722 void HandlePragmaMark(); 723 void HandlePragmaPoison(Token &PoisonTok); 724 void HandlePragmaSystemHeader(Token &SysHeaderTok); 725 void HandlePragmaDependency(Token &DependencyTok); 726 void HandlePragmaComment(Token &CommentTok); 727}; 728 729/// PreprocessorFactory - A generic factory interface for lazily creating 730/// Preprocessor objects on-demand when they are needed. 731class PreprocessorFactory { 732public: 733 virtual ~PreprocessorFactory(); 734 virtual Preprocessor* CreatePreprocessor() = 0; 735}; 736 737} // end namespace clang 738 739#endif 740