TokenLexer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- TokenLexer.h - Lex from a token buffer -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the TokenLexer interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_LEX_TOKENLEXER_H
  18. #define LLVM_CLANG_LEX_TOKENLEXER_H
  19. #include "clang/Basic/SourceLocation.h"
  20. #include "llvm/ADT/ArrayRef.h"
  21. namespace clang {
  22. class MacroArgs;
  23. class MacroInfo;
  24. class Preprocessor;
  25. class Token;
  26. class VAOptExpansionContext;
  27. /// TokenLexer - This implements a lexer that returns tokens from a macro body
  28. /// or token stream instead of lexing from a character buffer. This is used for
  29. /// macro expansion and _Pragma handling, for example.
  30. class TokenLexer {
  31. friend class Preprocessor;
  32. /// The macro we are expanding from. This is null if expanding a token stream.
  33. MacroInfo *Macro = nullptr;
  34. /// The actual arguments specified for a function-like macro, or null. The
  35. /// TokenLexer owns the pointed-to object.
  36. MacroArgs *ActualArgs = nullptr;
  37. /// The current preprocessor object we are expanding for.
  38. Preprocessor &PP;
  39. /// This is the pointer to an array of tokens that the macro is
  40. /// defined to, with arguments expanded for function-like macros. If this is
  41. /// a token stream, these are the tokens we are returning. This points into
  42. /// the macro definition we are lexing from, a cache buffer that is owned by
  43. /// the preprocessor, or some other buffer that we may or may not own
  44. /// (depending on OwnsTokens).
  45. /// Note that if it points into Preprocessor's cache buffer, the Preprocessor
  46. /// may update the pointer as needed.
  47. const Token *Tokens;
  48. /// This is the length of the Tokens array.
  49. unsigned NumTokens;
  50. /// This is the index of the next token that Lex will return.
  51. unsigned CurTokenIdx;
  52. /// The source location range where this macro was expanded.
  53. SourceLocation ExpandLocStart, ExpandLocEnd;
  54. /// Source location pointing at the source location entry chunk that
  55. /// was reserved for the current macro expansion.
  56. SourceLocation MacroExpansionStart;
  57. /// The offset of the macro expansion in the
  58. /// "source location address space".
  59. unsigned MacroStartSLocOffset;
  60. /// Location of the macro definition.
  61. SourceLocation MacroDefStart;
  62. /// Length of the macro definition.
  63. unsigned MacroDefLength;
  64. /// Lexical information about the expansion point of the macro: the identifier
  65. /// that the macro expanded from had these properties.
  66. bool AtStartOfLine : 1;
  67. bool HasLeadingSpace : 1;
  68. // When this is true, the next token appended to the
  69. // output list during function argument expansion will get a leading space,
  70. // regardless of whether it had one to begin with or not. This is used for
  71. // placemarker support. If still true after function argument expansion, the
  72. // leading space will be applied to the first token following the macro
  73. // expansion.
  74. bool NextTokGetsSpace : 1;
  75. /// This is true if this TokenLexer allocated the Tokens
  76. /// array, and thus needs to free it when destroyed. For simple object-like
  77. /// macros (for example) we just point into the token buffer of the macro
  78. /// definition, we don't make a copy of it.
  79. bool OwnsTokens : 1;
  80. /// This is true when tokens lexed from the TokenLexer
  81. /// should not be subject to further macro expansion.
  82. bool DisableMacroExpansion : 1;
  83. /// When true, the produced tokens have Token::IsReinjected flag set.
  84. /// See the flag documentation for details.
  85. bool IsReinject : 1;
  86. public:
  87. /// Create a TokenLexer for the specified macro with the specified actual
  88. /// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
  89. /// ILEnd specifies the location of the ')' for a function-like macro or the
  90. /// identifier for an object-like macro.
  91. TokenLexer(Token &Tok, SourceLocation ILEnd, MacroInfo *MI,
  92. MacroArgs *ActualArgs, Preprocessor &pp)
  93. : PP(pp), OwnsTokens(false) {
  94. Init(Tok, ILEnd, MI, ActualArgs);
  95. }
  96. /// Create a TokenLexer for the specified token stream. If 'OwnsTokens' is
  97. /// specified, this takes ownership of the tokens and delete[]'s them when
  98. /// the token lexer is empty.
  99. TokenLexer(const Token *TokArray, unsigned NumToks, bool DisableExpansion,
  100. bool ownsTokens, bool isReinject, Preprocessor &pp)
  101. : PP(pp), OwnsTokens(false) {
  102. Init(TokArray, NumToks, DisableExpansion, ownsTokens, isReinject);
  103. }
  104. TokenLexer(const TokenLexer &) = delete;
  105. TokenLexer &operator=(const TokenLexer &) = delete;
  106. ~TokenLexer() { destroy(); }
  107. /// Initialize this TokenLexer to expand from the specified macro
  108. /// with the specified argument information. Note that this ctor takes
  109. /// ownership of the ActualArgs pointer. ILEnd specifies the location of the
  110. /// ')' for a function-like macro or the identifier for an object-like macro.
  111. void Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI,
  112. MacroArgs *Actuals);
  113. /// Initialize this TokenLexer with the specified token stream.
  114. /// This does not take ownership of the specified token vector.
  115. ///
  116. /// DisableExpansion is true when macro expansion of tokens lexed from this
  117. /// stream should be disabled.
  118. void Init(const Token *TokArray, unsigned NumToks, bool DisableMacroExpansion,
  119. bool OwnsTokens, bool IsReinject);
  120. /// If the next token lexed will pop this macro off the
  121. /// expansion stack, return 2. If the next unexpanded token is a '(', return
  122. /// 1, otherwise return 0.
  123. unsigned isNextTokenLParen() const;
  124. /// Lex and return a token from this macro stream.
  125. bool Lex(Token &Tok);
  126. /// isParsingPreprocessorDirective - Return true if we are in the middle of a
  127. /// preprocessor directive.
  128. bool isParsingPreprocessorDirective() const;
  129. private:
  130. void destroy();
  131. /// Return true if the next lex call will pop this macro off the include
  132. /// stack.
  133. bool isAtEnd() const {
  134. return CurTokenIdx == NumTokens;
  135. }
  136. /// Concatenates the next (sub-)sequence of \p Tokens separated by '##'
  137. /// starting with LHSTok - stopping when we encounter a token that is neither
  138. /// '##' nor preceded by '##'. Places the result back into \p LHSTok and sets
  139. /// \p CurIdx to point to the token following the last one that was pasted.
  140. ///
  141. /// Also performs the MSVC extension wide-literal token pasting involved with:
  142. /// \code L #macro-arg. \endcode
  143. ///
  144. /// \param[in,out] LHSTok - Contains the token to the left of '##' in \p
  145. /// Tokens upon entry and will contain the resulting concatenated Token upon
  146. /// exit.
  147. ///
  148. /// \param[in] TokenStream - The stream of Tokens we are lexing from.
  149. ///
  150. /// \param[in,out] CurIdx - Upon entry, \pTokens[\pCurIdx] must equal '##'
  151. /// (with the exception of the MSVC extension mentioned above). Upon exit, it
  152. /// is set to the index of the token following the last token that was
  153. /// concatenated together.
  154. ///
  155. /// \returns If this returns true, the caller should immediately return the
  156. /// token.
  157. bool pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
  158. unsigned int &CurIdx);
  159. /// Calls pasteTokens above, passing in the '*this' object's Tokens and
  160. /// CurTokenIdx data members.
  161. bool pasteTokens(Token &Tok);
  162. /// Takes the tail sequence of tokens within ReplacementToks that represent
  163. /// the just expanded __VA_OPT__ tokens (possibly zero tokens) and transforms
  164. /// them into a string. \p VCtx is used to determine which token represents
  165. /// the first __VA_OPT__ replacement token.
  166. ///
  167. /// \param[in,out] ResultToks - Contains the current Replacement Tokens
  168. /// (prior to rescanning and token pasting), the tail end of which represents
  169. /// the tokens just expanded through __VA_OPT__ processing. These (sub)
  170. /// sequence of tokens are folded into one stringified token.
  171. ///
  172. /// \param[in] VCtx - contains relevant contextual information about the
  173. /// state of the tokens around and including the __VA_OPT__ token, necessary
  174. /// for stringification.
  175. void stringifyVAOPTContents(SmallVectorImpl<Token> &ResultToks,
  176. const VAOptExpansionContext &VCtx,
  177. SourceLocation VAOPTClosingParenLoc);
  178. /// Expand the arguments of a function-like macro so that we can quickly
  179. /// return preexpanded tokens from Tokens.
  180. void ExpandFunctionArguments();
  181. /// In microsoft compatibility mode, /##/ pastes
  182. /// together to form a comment that comments out everything in the current
  183. /// macro, other active macros, and anything left on the current physical
  184. /// source line of the expanded buffer. Handle this by returning the
  185. /// first token on the next line.
  186. void HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc);
  187. /// If \p loc is a FileID and points inside the current macro
  188. /// definition, returns the appropriate source location pointing at the
  189. /// macro expansion source location entry.
  190. SourceLocation getExpansionLocForMacroDefLoc(SourceLocation loc) const;
  191. /// Creates SLocEntries and updates the locations of macro argument
  192. /// tokens to their new expanded locations.
  193. ///
  194. /// \param ArgIdSpellLoc the location of the macro argument id inside the
  195. /// macro definition.
  196. void updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
  197. Token *begin_tokens, Token *end_tokens);
  198. /// Remove comma ahead of __VA_ARGS__, if present, according to compiler
  199. /// dialect settings. Returns true if the comma is removed.
  200. bool MaybeRemoveCommaBeforeVaArgs(SmallVectorImpl<Token> &ResultToks,
  201. bool HasPasteOperator,
  202. MacroInfo *Macro, unsigned MacroArgNo,
  203. Preprocessor &PP);
  204. void PropagateLineStartLeadingSpaceInfo(Token &Result);
  205. };
  206. } // namespace clang
  207. #endif // LLVM_CLANG_LEX_TOKENLEXER_H
  208. #ifdef __GNUC__
  209. #pragma GCC diagnostic pop
  210. #endif