clang API Documentation

TokenLexer.h
Go to the documentation of this file.
00001 //===--- TokenLexer.h - Lex from a token buffer -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the TokenLexer interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_LEX_TOKENLEXER_H
00015 #define LLVM_CLANG_LEX_TOKENLEXER_H
00016 
00017 #include "clang/Basic/SourceLocation.h"
00018 
00019 namespace clang {
00020   class MacroInfo;
00021   class Preprocessor;
00022   class Token;
00023   class MacroArgs;
00024 
00025 /// TokenLexer - This implements a lexer that returns tokens from a macro body
00026 /// or token stream instead of lexing from a character buffer.  This is used for
00027 /// macro expansion and _Pragma handling, for example.
00028 ///
00029 class TokenLexer {
00030   /// Macro - The macro we are expanding from.  This is null if expanding a
00031   /// token stream.
00032   ///
00033   MacroInfo *Macro;
00034 
00035   /// ActualArgs - The actual arguments specified for a function-like macro, or
00036   /// null.  The TokenLexer owns the pointed-to object.
00037   MacroArgs *ActualArgs;
00038 
00039   /// PP - The current preprocessor object we are expanding for.
00040   ///
00041   Preprocessor &PP;
00042 
00043   /// Tokens - This is the pointer to an array of tokens that the macro is
00044   /// defined to, with arguments expanded for function-like macros.  If this is
00045   /// a token stream, these are the tokens we are returning.  This points into
00046   /// the macro definition we are lexing from, a cache buffer that is owned by
00047   /// the preprocessor, or some other buffer that we may or may not own
00048   /// (depending on OwnsTokens).
00049   /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
00050   /// may update the pointer as needed.
00051   const Token *Tokens;
00052   friend class Preprocessor;
00053 
00054   /// NumTokens - This is the length of the Tokens array.
00055   ///
00056   unsigned NumTokens;
00057 
00058   /// CurToken - This is the next token that Lex will return.
00059   ///
00060   unsigned CurToken;
00061 
00062   /// ExpandLocStart/End - The source location range where this macro was
00063   /// expanded.
00064   SourceLocation ExpandLocStart, ExpandLocEnd;
00065 
00066   /// \brief Source location pointing at the source location entry chunk that
00067   /// was reserved for the current macro expansion.
00068   SourceLocation MacroExpansionStart;
00069   
00070   /// \brief The offset of the macro expansion in the
00071   /// "source location address space".
00072   unsigned MacroStartSLocOffset;
00073 
00074   /// \brief Location of the macro definition.
00075   SourceLocation MacroDefStart;
00076   /// \brief Length of the macro definition.
00077   unsigned MacroDefLength;
00078 
00079   /// Lexical information about the expansion point of the macro: the identifier
00080   /// that the macro expanded from had these properties.
00081   bool AtStartOfLine : 1;
00082   bool HasLeadingSpace : 1;
00083 
00084   // NextTokGetsSpace - When this is true, the next token appended to the
00085   // output list during function argument expansion will get a leading space,
00086   // regardless of whether it had one to begin with or not. This is used for
00087   // placemarker support. If still true after function argument expansion, the
00088   // leading space will be applied to the first token following the macro
00089   // expansion.
00090   bool NextTokGetsSpace : 1;
00091 
00092   /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
00093   /// array, and thus needs to free it when destroyed.  For simple object-like
00094   /// macros (for example) we just point into the token buffer of the macro
00095   /// definition, we don't make a copy of it.
00096   bool OwnsTokens : 1;
00097 
00098   /// DisableMacroExpansion - This is true when tokens lexed from the TokenLexer
00099   /// should not be subject to further macro expansion.
00100   bool DisableMacroExpansion : 1;
00101 
00102   TokenLexer(const TokenLexer &) LLVM_DELETED_FUNCTION;
00103   void operator=(const TokenLexer &) LLVM_DELETED_FUNCTION;
00104 public:
00105   /// Create a TokenLexer for the specified macro with the specified actual
00106   /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
00107   /// ILEnd specifies the location of the ')' for a function-like macro or the
00108   /// identifier for an object-like macro.
00109   TokenLexer(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
00110              MacroArgs *ActualArgs, Preprocessor &pp)
00111     : Macro(nullptr), ActualArgs(nullptr), PP(pp), OwnsTokens(false) {
00112     Init(Tok, ILEnd, MI, ActualArgs);
00113   }
00114 
00115   /// Init - Initialize this TokenLexer to expand from the specified macro
00116   /// with the specified argument information.  Note that this ctor takes
00117   /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
00118   /// ')' for a function-like macro or the identifier for an object-like macro.
00119   void Init(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
00120             MacroArgs *ActualArgs);
00121 
00122   /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
00123   /// specified, this takes ownership of the tokens and delete[]'s them when
00124   /// the token lexer is empty.
00125   TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
00126              bool ownsTokens, Preprocessor &pp)
00127     : Macro(nullptr), ActualArgs(nullptr), PP(pp), OwnsTokens(false) {
00128     Init(TokArray, NumToks, DisableExpansion, ownsTokens);
00129   }
00130 
00131   /// Init - Initialize this TokenLexer with the specified token stream.
00132   /// This does not take ownership of the specified token vector.
00133   ///
00134   /// DisableExpansion is true when macro expansion of tokens lexed from this
00135   /// stream should be disabled.
00136   void Init(const Token *TokArray, unsigned NumToks,
00137             bool DisableMacroExpansion, bool OwnsTokens);
00138 
00139   ~TokenLexer() { destroy(); }
00140 
00141   /// isNextTokenLParen - If the next token lexed will pop this macro off the
00142   /// expansion stack, return 2.  If the next unexpanded token is a '(', return
00143   /// 1, otherwise return 0.
00144   unsigned isNextTokenLParen() const;
00145 
00146   /// Lex - Lex and return a token from this macro stream.
00147   bool Lex(Token &Tok);
00148 
00149   /// isParsingPreprocessorDirective - Return true if we are in the middle of a
00150   /// preprocessor directive.
00151   bool isParsingPreprocessorDirective() const;
00152 
00153 private:
00154   void destroy();
00155 
00156   /// isAtEnd - Return true if the next lex call will pop this macro off the
00157   /// include stack.
00158   bool isAtEnd() const {
00159     return CurToken == NumTokens;
00160   }
00161 
00162   /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
00163   /// operator.  Read the ## and RHS, and paste the LHS/RHS together.  If there
00164   /// are is another ## after it, chomp it iteratively.  Return the result as
00165   /// Tok.  If this returns true, the caller should immediately return the
00166   /// token.
00167   bool PasteTokens(Token &Tok);
00168 
00169   /// Expand the arguments of a function-like macro so that we can quickly
00170   /// return preexpanded tokens from Tokens.
00171   void ExpandFunctionArguments();
00172 
00173   /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
00174   /// together to form a comment that comments out everything in the current
00175   /// macro, other active macros, and anything left on the current physical
00176   /// source line of the expanded buffer.  Handle this by returning the
00177   /// first token on the next line.
00178   void HandleMicrosoftCommentPaste(Token &Tok);
00179 
00180   /// \brief If \p loc is a FileID and points inside the current macro
00181   /// definition, returns the appropriate source location pointing at the
00182   /// macro expansion source location entry.
00183   SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
00184 
00185   /// \brief Creates SLocEntries and updates the locations of macro argument
00186   /// tokens to their new expanded locations.
00187   ///
00188   /// \param ArgIdSpellLoc the location of the macro argument id inside the
00189   /// macro definition.
00190   void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
00191                                   Token *begin_tokens, Token *end_tokens);
00192 
00193   /// Remove comma ahead of __VA_ARGS__, if present, according to compiler
00194   /// dialect settings.  Returns true if the comma is removed.
00195   bool MaybeRemoveCommaBeforeVaArgs(SmallVectorImpl<Token> &ResultToks,
00196                                     bool HasPasteOperator,
00197                                     MacroInfo *Macro, unsigned MacroArgNo,
00198                                     Preprocessor &PP);
00199 
00200   void PropagateLineStartLeadingSpaceInfo(Token &Result);
00201 };
00202 
00203 }  // end namespace clang
00204 
00205 #endif