Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:58

0001 //===- TokenLexer.h - Lex from a token buffer -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the TokenLexer interface.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_LEX_TOKENLEXER_H
0014 #define LLVM_CLANG_LEX_TOKENLEXER_H
0015 
0016 #include "clang/Basic/SourceLocation.h"
0017 #include "llvm/ADT/ArrayRef.h"
0018 
0019 namespace clang {
0020 
0021 class MacroArgs;
0022 class MacroInfo;
0023 class Preprocessor;
0024 class Token;
0025 class VAOptExpansionContext;
0026 
0027 /// TokenLexer - This implements a lexer that returns tokens from a macro body
0028 /// or token stream instead of lexing from a character buffer.  This is used for
0029 /// macro expansion and _Pragma handling, for example.
0030 class TokenLexer {
0031   friend class Preprocessor;
0032 
0033   /// The macro we are expanding from. This is null if expanding a token stream.
0034   MacroInfo *Macro = nullptr;
0035 
0036   /// The actual arguments specified for a function-like macro, or null. The
0037   /// TokenLexer owns the pointed-to object.
0038   MacroArgs *ActualArgs = nullptr;
0039 
0040   /// The current preprocessor object we are expanding for.
0041   Preprocessor &PP;
0042 
0043   /// This is the pointer to an array of tokens that the macro is
0044   /// defined to, with arguments expanded for function-like macros.  If this is
0045   /// a token stream, these are the tokens we are returning.  This points into
0046   /// the macro definition we are lexing from, a cache buffer that is owned by
0047   /// the preprocessor, or some other buffer that we may or may not own
0048   /// (depending on OwnsTokens).
0049   /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
0050   /// may update the pointer as needed.
0051   const Token *Tokens;
0052 
0053   /// This is the length of the Tokens array.
0054   unsigned NumTokens;
0055 
0056   /// This is the index of the next token that Lex will return.
0057   unsigned CurTokenIdx;
0058 
0059   /// The source location range where this macro was expanded.
0060   SourceLocation ExpandLocStart, ExpandLocEnd;
0061 
0062   /// Source location pointing at the source location entry chunk that
0063   /// was reserved for the current macro expansion.
0064   SourceLocation MacroExpansionStart;
0065 
0066   /// The offset of the macro expansion in the
0067   /// "source location address space".
0068   unsigned MacroStartSLocOffset;
0069 
0070   /// Location of the macro definition.
0071   SourceLocation MacroDefStart;
0072 
0073   /// Length of the macro definition.
0074   unsigned MacroDefLength;
0075 
0076   /// Lexical information about the expansion point of the macro: the identifier
0077   /// that the macro expanded from had these properties.
0078   bool AtStartOfLine : 1;
0079   bool HasLeadingSpace : 1;
0080 
0081   // When this is true, the next token appended to the
0082   // output list during function argument expansion will get a leading space,
0083   // regardless of whether it had one to begin with or not. This is used for
0084   // placemarker support. If still true after function argument expansion, the
0085   // leading space will be applied to the first token following the macro
0086   // expansion.
0087   bool NextTokGetsSpace : 1;
0088 
0089   /// This is true if this TokenLexer allocated the Tokens
0090   /// array, and thus needs to free it when destroyed.  For simple object-like
0091   /// macros (for example) we just point into the token buffer of the macro
0092   /// definition, we don't make a copy of it.
0093   bool OwnsTokens : 1;
0094 
0095   /// This is true when tokens lexed from the TokenLexer
0096   /// should not be subject to further macro expansion.
0097   bool DisableMacroExpansion : 1;
0098 
0099   /// When true, the produced tokens have Token::IsReinjected flag set.
0100   /// See the flag documentation for details.
0101   bool IsReinject : 1;
0102 
0103 public:
0104   /// Create a TokenLexer for the specified macro with the specified actual
0105   /// arguments.  Note that this ctor takes ownership of the ActualArgs pointer.
0106   /// ILEnd specifies the location of the ')' for a function-like macro or the
0107   /// identifier for an object-like macro.
0108   TokenLexer(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
0109              MacroArgs *ActualArgs, Preprocessor &pp)
0110       : PP(pp), OwnsTokens(false) {
0111     Init(Tok, ILEnd, MI, ActualArgs);
0112   }
0113 
0114   /// Create a TokenLexer for the specified token stream.  If 'OwnsTokens' is
0115   /// specified, this takes ownership of the tokens and delete[]'s them when
0116   /// the token lexer is empty.
0117   TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
0118              bool ownsTokens, bool isReinject, Preprocessor &pp)
0119       : PP(pp), OwnsTokens(false) {
0120     Init(TokArray, NumToks, DisableExpansion, ownsTokens, isReinject);
0121   }
0122 
0123   TokenLexer(const TokenLexer &) = delete;
0124   TokenLexer &operator=(const TokenLexer &) = delete;
0125   ~TokenLexer() { destroy(); }
0126 
0127   /// Initialize this TokenLexer to expand from the specified macro
0128   /// with the specified argument information.  Note that this ctor takes
0129   /// ownership of the ActualArgs pointer.  ILEnd specifies the location of the
0130   /// ')' for a function-like macro or the identifier for an object-like macro.
0131   void Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI,
0132             MacroArgs *Actuals);
0133 
0134   /// Initialize this TokenLexer with the specified token stream.
0135   /// This does not take ownership of the specified token vector.
0136   ///
0137   /// DisableExpansion is true when macro expansion of tokens lexed from this
0138   /// stream should be disabled.
0139   void Init(const Token *TokArray, unsigned NumToks, bool DisableMacroExpansion,
0140             bool OwnsTokens, bool IsReinject);
0141 
0142   /// If the next token lexed will pop this macro off the
0143   /// expansion stack, return 2.  If the next unexpanded token is a '(', return
0144   /// 1, otherwise return 0.
0145   unsigned isNextTokenLParen() const;
0146 
0147   /// Lex and return a token from this macro stream.
0148   bool Lex(Token &Tok);
0149 
0150   /// isParsingPreprocessorDirective - Return true if we are in the middle of a
0151   /// preprocessor directive.
0152   bool isParsingPreprocessorDirective() const;
0153 
0154 private:
0155   void destroy();
0156 
0157   /// Return true if the next lex call will pop this macro off the include
0158   /// stack.
0159   bool isAtEnd() const {
0160     return CurTokenIdx == NumTokens;
0161   }
0162 
0163   /// Concatenates the next (sub-)sequence of \p Tokens separated by '##'
0164   /// starting with LHSTok - stopping when we encounter a token that is neither
0165   /// '##' nor preceded by '##'.  Places the result back into \p LHSTok and sets
0166   /// \p CurIdx to point to the token following the last one that was pasted.
0167   ///
0168   /// Also performs the MSVC extension wide-literal token pasting involved with:
0169   ///       \code L #macro-arg. \endcode
0170   ///
0171   /// \param[in,out] LHSTok - Contains the token to the left of '##' in \p
0172   /// Tokens upon entry and will contain the resulting concatenated Token upon
0173   /// exit.
0174   ///
0175   /// \param[in] TokenStream - The stream of Tokens we are lexing from.
0176   ///
0177   /// \param[in,out] CurIdx - Upon entry, \pTokens[\pCurIdx] must equal '##'
0178   /// (with the exception of the MSVC extension mentioned above).  Upon exit, it
0179   /// is set to the index of the token following the last token that was
0180   /// concatenated together.
0181   ///
0182   /// \returns If this returns true, the caller should immediately return the
0183   /// token.
0184   bool pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
0185                    unsigned int &CurIdx);
0186 
0187   /// Calls pasteTokens above, passing in the '*this' object's Tokens and
0188   /// CurTokenIdx data members.
0189   bool pasteTokens(Token &Tok);
0190 
0191 
0192   /// Takes the tail sequence of tokens within ReplacementToks that represent
0193   /// the just expanded __VA_OPT__ tokens (possibly zero tokens) and transforms
0194   /// them into a string.  \p VCtx is used to determine which token represents
0195   /// the first __VA_OPT__ replacement token.
0196   ///
0197   /// \param[in,out] ResultToks - Contains the current Replacement Tokens
0198   /// (prior to rescanning and token pasting), the tail end of which represents
0199   /// the tokens just expanded through __VA_OPT__ processing.  These (sub)
0200   /// sequence of tokens are folded into one stringified token.
0201   ///
0202   /// \param[in] VCtx - contains relevant contextual information about the
0203   /// state of the tokens around and including the __VA_OPT__ token, necessary
0204   /// for stringification.
0205   void stringifyVAOPTContents(SmallVectorImpl<Token> &ResultToks,
0206                               const VAOptExpansionContext &VCtx,
0207                               SourceLocation VAOPTClosingParenLoc);
0208 
0209   /// Expand the arguments of a function-like macro so that we can quickly
0210   /// return preexpanded tokens from Tokens.
0211   void ExpandFunctionArguments();
0212 
0213   /// In microsoft compatibility mode, /##/ pastes
0214   /// together to form a comment that comments out everything in the current
0215   /// macro, other active macros, and anything left on the current physical
0216   /// source line of the expanded buffer.  Handle this by returning the
0217   /// first token on the next line.
0218   void HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc);
0219 
0220   /// If \p loc is a FileID and points inside the current macro
0221   /// definition, returns the appropriate source location pointing at the
0222   /// macro expansion source location entry.
0223   SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
0224 
0225   /// Creates SLocEntries and updates the locations of macro argument
0226   /// tokens to their new expanded locations.
0227   ///
0228   /// \param ArgIdSpellLoc the location of the macro argument id inside the
0229   /// macro definition.
0230   void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
0231                                   Token *begin_tokens, Token *end_tokens);
0232 
0233   /// Remove comma ahead of __VA_ARGS__, if present, according to compiler
0234   /// dialect settings.  Returns true if the comma is removed.
0235   bool MaybeRemoveCommaBeforeVaArgs(SmallVectorImpl<Token> &ResultToks,
0236                                     bool HasPasteOperator,
0237                                     MacroInfo *Macro, unsigned MacroArgNo,
0238                                     Preprocessor &PP);
0239 
0240   void PropagateLineStartLeadingSpaceInfo(Token &Result);
0241 };
0242 
0243 } // namespace clang
0244 
0245 #endif // LLVM_CLANG_LEX_TOKENLEXER_H