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