clang 24.0.0git
TokenLexer.cpp
Go to the documentation of this file.
1//===- TokenLexer.cpp - Lex from a token stream ---------------------------===//
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 implements the TokenLexer interface.
10//
11//===----------------------------------------------------------------------===//
12
21#include "clang/Lex/Lexer.h"
22#include "clang/Lex/MacroArgs.h"
23#include "clang/Lex/MacroInfo.h"
25#include "clang/Lex/Token.h"
27#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/iterator_range.h"
31#include <cassert>
32#include <cstring>
33#include <optional>
34
35using namespace clang;
36
37/// Create a TokenLexer for the specified macro with the specified actual
38/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
40 MacroArgs *Actuals) {
41 // If the client is reusing a TokenLexer, make sure to free any memory
42 // associated with it.
43 destroy();
44
45 Macro = MI;
46 ActualArgs = Actuals;
47 CurTokenIdx = 0;
48
49 ExpandLocStart = Tok.getLocation();
50 ExpandLocEnd = ELEnd;
51 AtStartOfLine = Tok.isAtStartOfLine();
52 HasLeadingSpace = Tok.hasLeadingSpace();
53 NextTokGetsSpace = false;
54 Tokens = &*Macro->tokens_begin();
55 OwnsTokens = false;
56 DisableMacroExpansion = false;
57 IsReinject = false;
58 NumTokens = Macro->tokens_end()-Macro->tokens_begin();
59 MacroExpansionStart = SourceLocation();
60 LexingCXXModuleDirective = false;
61
62 SourceManager &SM = PP.getSourceManager();
63 MacroStartSLocOffset = SM.getNextLocalOffset();
64
65 if (NumTokens > 0) {
66 assert(Tokens[0].getLocation().isValid());
67 assert((Tokens[0].getLocation().isFileID() || Tokens[0].is(tok::comment)) &&
68 "Macro defined in macro?");
69 assert(ExpandLocStart.isValid());
70
71 // Reserve a source location entry chunk for the length of the macro
72 // definition. Tokens that get lexed directly from the definition will
73 // have their locations pointing inside this chunk. This is to avoid
74 // creating separate source location entries for each token.
75 MacroDefStart = SM.getExpansionLoc(Tokens[0].getLocation());
76 MacroDefLength = Macro->getDefinitionLength(SM);
77 MacroExpansionStart = SM.createExpansionLoc(MacroDefStart,
78 ExpandLocStart,
79 ExpandLocEnd,
80 MacroDefLength);
81 }
82
83 // If this is a function-like macro, expand the arguments and change
84 // Tokens to point to the expanded tokens.
85 if (Macro->isFunctionLike() && Macro->getNumParams())
86 ExpandFunctionArguments();
87
88 // Mark the macro as currently disabled, so that it is not recursively
89 // expanded. The macro must be disabled only after argument pre-expansion of
90 // function-like macro arguments occurs.
91 Macro->DisableMacro();
92}
93
94/// Create a TokenLexer for the specified token stream. This does not
95/// take ownership of the specified token vector.
96void TokenLexer::Init(const Token *TokArray, unsigned NumToks,
97 bool disableMacroExpansion, bool ownsTokens,
98 bool isReinject) {
99 assert(!isReinject || disableMacroExpansion);
100 // If the client is reusing a TokenLexer, make sure to free any memory
101 // associated with it.
102 destroy();
103
104 Macro = nullptr;
105 ActualArgs = nullptr;
106 Tokens = TokArray;
107 OwnsTokens = ownsTokens;
108 DisableMacroExpansion = disableMacroExpansion;
109 IsReinject = isReinject;
110 NumTokens = NumToks;
111 CurTokenIdx = 0;
112 ExpandLocStart = ExpandLocEnd = SourceLocation();
113 AtStartOfLine = false;
114 HasLeadingSpace = false;
115 NextTokGetsSpace = false;
116 MacroExpansionStart = SourceLocation();
117 LexingCXXModuleDirective = false;
118
119 // Set HasLeadingSpace/AtStartOfLine so that the first token will be
120 // returned unmodified.
121 if (NumToks != 0) {
122 AtStartOfLine = TokArray[0].isAtStartOfLine();
123 HasLeadingSpace = TokArray[0].hasLeadingSpace();
124 }
125}
126
127void TokenLexer::destroy() {
128 // If this was a function-like macro that actually uses its arguments, delete
129 // the expanded tokens.
130 if (OwnsTokens) {
131 delete [] Tokens;
132 Tokens = nullptr;
133 OwnsTokens = false;
134 }
135
136 // TokenLexer owns its formal arguments.
137 if (ActualArgs) ActualArgs->destroy(PP);
138}
139
140static bool hasVaOptSupport(const LangOptions &LangOpts) {
141 return LangOpts.C23 || LangOpts.CPlusPlus20;
142}
143
144bool TokenLexer::MaybeRemoveCommaBeforeVaArgs(
145 SmallVectorImpl<Token> &ResultToks, bool HasPasteOperator, MacroInfo *Macro,
146 unsigned MacroArgNo, Preprocessor &PP) {
147 // Is the macro argument __VA_ARGS__?
148 if (!Macro->isVariadic() || MacroArgNo != Macro->getNumParams()-1)
149 return false;
150
151 // In Microsoft-compatibility mode, a comma is removed in the expansion
152 // of " ... , __VA_ARGS__ " if __VA_ARGS__ is empty. This extension is
153 // not supported by gcc.
154 if (!HasPasteOperator && !PP.getLangOpts().MSVCCompat)
155 return false;
156
157 // GCC removes the comma in the expansion of " ... , ## __VA_ARGS__ " if
158 // __VA_ARGS__ is empty, but not in strict C99 mode where there are no
159 // named arguments, where it remains. In all other modes, including C99
160 // with GNU extensions, it is removed regardless of named arguments.
161 // Microsoft also appears to support this extension, unofficially.
162 if (PP.getLangOpts().C99 && !PP.getLangOpts().GNUMode
163 && Macro->getNumParams() < 2)
164 return false;
165
166 // Is a comma available to be removed?
167 if (ResultToks.empty() || !ResultToks.back().is(tok::comma))
168 return false;
169
170 // Issue an extension diagnostic for the paste operator.
171 if (HasPasteOperator) {
172 PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma)
173 << hasVaOptSupport(PP.getLangOpts());
174 }
175
176 // Remove the comma.
177 ResultToks.pop_back();
178
179 if (!ResultToks.empty()) {
180 // If the comma was right after another paste (e.g. "X##,##__VA_ARGS__"),
181 // then removal of the comma should produce a placemarker token (in C99
182 // terms) which we model by popping off the previous ##, giving us a plain
183 // "X" when __VA_ARGS__ is empty.
184 if (ResultToks.back().is(tok::hashhash))
185 ResultToks.pop_back();
186
187 // Remember that this comma was elided.
188 ResultToks.back().setFlag(Token::CommaAfterElided);
189 }
190
191 // Never add a space, even if the comma, ##, or arg had a space.
192 NextTokGetsSpace = false;
193 return true;
194}
195
196void TokenLexer::stringifyVAOPTContents(
197 SmallVectorImpl<Token> &ResultToks, const VAOptExpansionContext &VCtx,
198 const SourceLocation VAOPTClosingParenLoc) {
199 const int NumToksPriorToVAOpt = VCtx.getNumberOfTokensPriorToVAOpt();
200 const unsigned int NumVAOptTokens = ResultToks.size() - NumToksPriorToVAOpt;
201 Token *const VAOPTTokens =
202 NumVAOptTokens ? &ResultToks[NumToksPriorToVAOpt] : nullptr;
203
204 SmallVector<Token, 64> ConcatenatedVAOPTResultToks;
205 // FIXME: Should we keep track within VCtx that we did or didnot
206 // encounter pasting - and only then perform this loop.
207
208 // Perform token pasting (concatenation) prior to stringization.
209 for (unsigned int CurTokenIdx = 0; CurTokenIdx != NumVAOptTokens;
210 ++CurTokenIdx) {
211 if (VAOPTTokens[CurTokenIdx].is(tok::hashhash)) {
212 assert(CurTokenIdx != 0 &&
213 "Can not have __VAOPT__ contents begin with a ##");
214 Token &LHS = VAOPTTokens[CurTokenIdx - 1];
215 pasteTokens(LHS, llvm::ArrayRef(VAOPTTokens, NumVAOptTokens),
216 CurTokenIdx);
217 // Replace the token prior to the first ## in this iteration.
218 ConcatenatedVAOPTResultToks.back() = LHS;
219 if (CurTokenIdx == NumVAOptTokens)
220 break;
221 }
222 ConcatenatedVAOPTResultToks.push_back(VAOPTTokens[CurTokenIdx]);
223 }
224
225 ConcatenatedVAOPTResultToks.push_back(VCtx.getEOFTok());
226 // Get the SourceLocation that represents the start location within
227 // the macro definition that marks where this string is substituted
228 // into: i.e. the __VA_OPT__ and the ')' within the spelling of the
229 // macro definition, and use it to indicate that the stringified token
230 // was generated from that location.
231 const SourceLocation ExpansionLocStartWithinMacro =
232 getExpansionLocForMacroDefLoc(VCtx.getVAOptLoc());
233 const SourceLocation ExpansionLocEndWithinMacro =
234 getExpansionLocForMacroDefLoc(VAOPTClosingParenLoc);
235
236 Token StringifiedVAOPT = MacroArgs::StringifyArgument(
237 &ConcatenatedVAOPTResultToks[0], PP, VCtx.hasCharifyBefore() /*Charify*/,
238 ExpansionLocStartWithinMacro, ExpansionLocEndWithinMacro);
239
241 StringifiedVAOPT.setFlag(Token::LeadingSpace);
242
243 StringifiedVAOPT.setFlag(Token::StringifiedInMacro);
244 // Resize (shrink) the token stream to just capture this stringified token.
245 ResultToks.resize(NumToksPriorToVAOpt + 1);
246 ResultToks.back() = StringifiedVAOPT;
247}
248
249/// Expand the arguments of a function-like macro so that we can quickly
250/// return preexpanded tokens from Tokens.
251void TokenLexer::ExpandFunctionArguments() {
252 SmallVector<Token, 128> ResultToks;
253
254 // Loop through 'Tokens', expanding them into ResultToks. Keep
255 // track of whether we change anything. If not, no need to keep them. If so,
256 // we install the newly expanded sequence as the new 'Tokens' list.
257 bool MadeChange = false;
258
259 std::optional<bool> CalledWithVariadicArguments;
260
261 VAOptExpansionContext VCtx(PP);
262
263 for (unsigned I = 0, E = NumTokens; I != E; ++I) {
264 const Token &CurTok = Tokens[I];
265 // We don't want a space for the next token after a paste
266 // operator. In valid code, the token will get smooshed onto the
267 // preceding one anyway. In assembler-with-cpp mode, invalid
268 // pastes are allowed through: in this case, we do not want the
269 // extra whitespace to be added. For example, we want ". ## foo"
270 // -> ".foo" not ". foo".
271 if (I != 0 && !Tokens[I-1].is(tok::hashhash) && CurTok.hasLeadingSpace())
272 NextTokGetsSpace = true;
273
274 if (VCtx.isVAOptToken(CurTok)) {
275 MadeChange = true;
276 assert(Tokens[I + 1].is(tok::l_paren) &&
277 "__VA_OPT__ must be followed by '('");
278
279 ++I; // Skip the l_paren
281 ResultToks.size());
282
283 continue;
284 }
285
286 // We have entered into the __VA_OPT__ context, so handle tokens
287 // appropriately.
288 if (VCtx.isInVAOpt()) {
289 // If we are about to process a token that is either an argument to
290 // __VA_OPT__ or its closing rparen, then:
291 // 1) If the token is the closing rparen that exits us out of __VA_OPT__,
292 // perform any necessary stringification or placemarker processing,
293 // and/or skip to the next token.
294 // 2) else if macro was invoked without variadic arguments skip this
295 // token.
296 // 3) else (macro was invoked with variadic arguments) process the token
297 // normally.
298
299 if (Tokens[I].is(tok::l_paren))
300 VCtx.sawOpeningParen(Tokens[I].getLocation());
301 // Continue skipping tokens within __VA_OPT__ if the macro was not
302 // called with variadic arguments, else let the rest of the loop handle
303 // this token. Note sawClosingParen() returns true only if the r_paren matches
304 // the closing r_paren of the __VA_OPT__.
305 if (!Tokens[I].is(tok::r_paren) || !VCtx.sawClosingParen()) {
306 // Lazily expand __VA_ARGS__ when we see the first __VA_OPT__.
307 if (!CalledWithVariadicArguments) {
308 CalledWithVariadicArguments =
309 ActualArgs->invokedWithVariadicArgument(Macro, PP);
310 }
311 if (!*CalledWithVariadicArguments) {
312 // Skip this token.
313 continue;
314 }
315 // ... else the macro was called with variadic arguments, and we do not
316 // have a closing rparen - so process this token normally.
317 } else {
318 // Current token is the closing r_paren which marks the end of the
319 // __VA_OPT__ invocation, so handle any place-marker pasting (if
320 // empty) by removing hashhash either before (if exists) or after. And
321 // also stringify the entire contents if VAOPT was preceded by a hash,
322 // but do so only after any token concatenation that needs to occur
323 // within the contents of VAOPT.
324
325 if (VCtx.hasStringifyOrCharifyBefore()) {
326 // Replace all the tokens just added from within VAOPT into a single
327 // stringified token. This requires token-pasting to eagerly occur
328 // within these tokens. If either the contents of VAOPT were empty
329 // or the macro wasn't called with any variadic arguments, the result
330 // is a token that represents an empty string.
331 stringifyVAOPTContents(ResultToks, VCtx,
332 /*ClosingParenLoc*/ Tokens[I].getLocation());
333
334 } else if (/*No tokens within VAOPT*/
335 ResultToks.size() == VCtx.getNumberOfTokensPriorToVAOpt()) {
336 // Treat VAOPT as a placemarker token. Eat either the '##' before the
337 // RHS/VAOPT (if one exists, suggesting that the LHS (if any) to that
338 // hashhash was not a placemarker) or the '##'
339 // after VAOPT, but not both.
340
341 if (ResultToks.size() && ResultToks.back().is(tok::hashhash)) {
342 ResultToks.pop_back();
343 } else if ((I + 1 != E) && Tokens[I + 1].is(tok::hashhash)) {
344 ++I; // Skip the following hashhash.
345 }
346 } else {
347 // If there's a ## before the __VA_OPT__, we might have discovered
348 // that the __VA_OPT__ begins with a placeholder. We delay action on
349 // that to now to avoid messing up our stashed count of tokens before
350 // __VA_OPT__.
351 if (VCtx.beginsWithPlaceholder()) {
352 assert(VCtx.getNumberOfTokensPriorToVAOpt() > 0 &&
353 ResultToks.size() >= VCtx.getNumberOfTokensPriorToVAOpt() &&
354 ResultToks[VCtx.getNumberOfTokensPriorToVAOpt() - 1].is(
355 tok::hashhash) &&
356 "no token paste before __VA_OPT__");
357 ResultToks.erase(ResultToks.begin() +
359 }
360 // If the expansion of __VA_OPT__ ends with a placeholder, eat any
361 // following '##' token.
362 if (VCtx.endsWithPlaceholder() && I + 1 != E &&
363 Tokens[I + 1].is(tok::hashhash)) {
364 ++I;
365 }
366 }
367 VCtx.reset();
368 // We processed __VA_OPT__'s closing paren (and the exit out of
369 // __VA_OPT__), so skip to the next token.
370 continue;
371 }
372 }
373
374 // If we found the stringify operator, get the argument stringified. The
375 // preprocessor already verified that the following token is a macro
376 // parameter or __VA_OPT__ when the #define was lexed.
377
378 if (CurTok.isOneOf(tok::hash, tok::hashat)) {
379 int ArgNo = Macro->getParameterNum(Tokens[I+1].getIdentifierInfo());
380 assert((ArgNo != -1 || VCtx.isVAOptToken(Tokens[I + 1])) &&
381 "Token following # is not an argument or __VA_OPT__!");
382
383 if (ArgNo == -1) {
384 // Handle the __VA_OPT__ case.
385 VCtx.sawHashOrHashAtBefore(NextTokGetsSpace,
386 CurTok.is(tok::hashat));
387 continue;
388 }
389 // Else handle the simple argument case.
390 SourceLocation ExpansionLocStart =
391 getExpansionLocForMacroDefLoc(CurTok.getLocation());
392 SourceLocation ExpansionLocEnd =
393 getExpansionLocForMacroDefLoc(Tokens[I+1].getLocation());
394
395 bool Charify = CurTok.is(tok::hashat);
396 const Token *UnexpArg = ActualArgs->getUnexpArgument(ArgNo);
398 UnexpArg, PP, Charify, ExpansionLocStart, ExpansionLocEnd);
400
401 // The stringified/charified string leading space flag gets set to match
402 // the #/#@ operator.
403 if (NextTokGetsSpace)
405
406 ResultToks.push_back(Res);
407 MadeChange = true;
408 ++I; // Skip arg name.
409 NextTokGetsSpace = false;
410 continue;
411 }
412
413 // Find out if there is a paste (##) operator before or after the token.
414 bool NonEmptyPasteBefore =
415 !ResultToks.empty() && ResultToks.back().is(tok::hashhash);
416 bool PasteBefore = I != 0 && Tokens[I-1].is(tok::hashhash);
417 bool PasteAfter = I+1 != E && Tokens[I+1].is(tok::hashhash);
418 bool RParenAfter = I+1 != E && Tokens[I+1].is(tok::r_paren);
419
420 assert((!NonEmptyPasteBefore || PasteBefore || VCtx.isInVAOpt()) &&
421 "unexpected ## in ResultToks");
422
423 // Otherwise, if this is not an argument token, just add the token to the
424 // output buffer.
425 IdentifierInfo *II = CurTok.getIdentifierInfo();
426 int ArgNo = II ? Macro->getParameterNum(II) : -1;
427 if (ArgNo == -1) {
428 // This isn't an argument, just add it.
429 ResultToks.push_back(CurTok);
430
431 if (NextTokGetsSpace) {
432 ResultToks.back().setFlag(Token::LeadingSpace);
433 NextTokGetsSpace = false;
434 } else if (PasteBefore && !NonEmptyPasteBefore)
435 ResultToks.back().clearFlag(Token::LeadingSpace);
436
437 continue;
438 }
439
440 // An argument is expanded somehow, the result is different than the
441 // input.
442 MadeChange = true;
443
444 // Otherwise, this is a use of the argument.
445
446 // In Microsoft mode, remove the comma before __VA_ARGS__ to ensure there
447 // are no trailing commas if __VA_ARGS__ is empty.
448 if (!PasteBefore && ActualArgs->isVarargsElidedUse() &&
449 MaybeRemoveCommaBeforeVaArgs(ResultToks,
450 /*HasPasteOperator=*/false,
451 Macro, ArgNo, PP))
452 continue;
453
454 // If it is not the LHS/RHS of a ## operator, we must pre-expand the
455 // argument and substitute the expanded tokens into the result. This is
456 // C99 6.10.3.1p1.
457 if (!PasteBefore && !PasteAfter) {
458 const Token *ResultArgToks;
459
460 // Only preexpand the argument if it could possibly need it. This
461 // avoids some work in common cases.
462 const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
463 if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
464 ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
465 else
466 ResultArgToks = ArgTok; // Use non-preexpanded tokens.
467
468 // If the arg token expanded into anything, append it.
469 if (ResultArgToks->isNot(tok::eof)) {
470 size_t FirstResult = ResultToks.size();
471 unsigned NumToks = MacroArgs::getArgLength(ResultArgToks);
472 ResultToks.append(ResultArgToks, ResultArgToks+NumToks);
473
474 // In Microsoft-compatibility mode, we follow MSVC's preprocessing
475 // behavior by not considering single commas from nested macro
476 // expansions as argument separators. Set a flag on the token so we can
477 // test for this later when the macro expansion is processed.
478 if (PP.getLangOpts().MSVCCompat && NumToks == 1 &&
479 ResultToks.back().is(tok::comma))
480 ResultToks.back().setFlag(Token::IgnoredComma);
481
482 // If the '##' came from expanding an argument, turn it into 'unknown'
483 // to avoid pasting.
484 for (Token &Tok : llvm::drop_begin(ResultToks, FirstResult))
485 if (Tok.is(tok::hashhash))
486 Tok.setKind(tok::unknown);
487
488 if(ExpandLocStart.isValid()) {
489 updateLocForMacroArgTokens(CurTok.getLocation(),
490 ResultToks.begin()+FirstResult,
491 ResultToks.end());
492 }
493
494 // If any tokens were substituted from the argument, the whitespace
495 // before the first token should match the whitespace of the arg
496 // identifier.
497 ResultToks[FirstResult].setFlagValue(Token::LeadingSpace,
498 NextTokGetsSpace);
499 ResultToks[FirstResult].setFlagValue(Token::StartOfLine, false);
500 NextTokGetsSpace = false;
501 } else {
502 // We're creating a placeholder token. Usually this doesn't matter,
503 // but it can affect paste behavior when at the start or end of a
504 // __VA_OPT__.
505 if (NonEmptyPasteBefore) {
506 // We're imagining a placeholder token is inserted here. If this is
507 // the first token in a __VA_OPT__ after a ##, delete the ##.
508 assert(VCtx.isInVAOpt() && "should only happen inside a __VA_OPT__");
510 } else if (RParenAfter)
512 }
513 continue;
514 }
515
516 // Okay, we have a token that is either the LHS or RHS of a paste (##)
517 // argument. It gets substituted as its non-pre-expanded tokens.
518 const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
519 unsigned NumToks = MacroArgs::getArgLength(ArgToks);
520 if (NumToks) { // Not an empty argument?
521 bool VaArgsPseudoPaste = false;
522 // If this is the GNU ", ## __VA_ARGS__" extension, and we just learned
523 // that __VA_ARGS__ expands to multiple tokens, avoid a pasting error when
524 // the expander tries to paste ',' with the first token of the __VA_ARGS__
525 // expansion.
526 if (NonEmptyPasteBefore && ResultToks.size() >= 2 &&
527 ResultToks[ResultToks.size()-2].is(tok::comma) &&
528 (unsigned)ArgNo == Macro->getNumParams()-1 &&
529 Macro->isVariadic()) {
530 VaArgsPseudoPaste = true;
531 // Remove the paste operator, report use of the extension.
532 bool VaOptSupport = hasVaOptSupport(PP.getLangOpts());
533 auto Diag = PP.Diag(ResultToks.pop_back_val().getLocation(),
534 diag::ext_paste_comma)
535 << VaOptSupport;
536 if (VaOptSupport) {
538 SourceRange(ResultToks.back().getLocation(),
539 CurTok.getLocation()),
540 " __VA_OPT__(,) __VA_ARGS__");
541 }
542 }
543
544 ResultToks.append(ArgToks, ArgToks+NumToks);
545
546 // If the '##' came from expanding an argument, turn it into 'unknown'
547 // to avoid pasting.
548 for (Token &Tok : llvm::make_range(ResultToks.end() - NumToks,
549 ResultToks.end())) {
550 if (Tok.is(tok::hashhash))
551 Tok.setKind(tok::unknown);
552 }
553
554 if (ExpandLocStart.isValid()) {
555 updateLocForMacroArgTokens(CurTok.getLocation(),
556 ResultToks.end()-NumToks, ResultToks.end());
557 }
558
559 // Transfer the leading whitespace information from the token
560 // (the macro argument) onto the first token of the
561 // expansion. Note that we don't do this for the GNU
562 // pseudo-paste extension ", ## __VA_ARGS__".
563 if (!VaArgsPseudoPaste) {
564 ResultToks[ResultToks.size() - NumToks].setFlagValue(Token::StartOfLine,
565 false);
566 ResultToks[ResultToks.size() - NumToks].setFlagValue(
567 Token::LeadingSpace, NextTokGetsSpace);
568 }
569
570 NextTokGetsSpace = false;
571 continue;
572 }
573
574 // If an empty argument is on the LHS or RHS of a paste, the standard (C99
575 // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We
576 // implement this by eating ## operators when a LHS or RHS expands to
577 // empty.
578 if (PasteAfter) {
579 // Discard the argument token and skip (don't copy to the expansion
580 // buffer) the paste operator after it.
581 ++I;
582 continue;
583 }
584
585 if (RParenAfter && !NonEmptyPasteBefore)
587
588 // If this is on the RHS of a paste operator, we've already copied the
589 // paste operator to the ResultToks list, unless the LHS was empty too.
590 // Remove it.
591 assert(PasteBefore);
592 if (NonEmptyPasteBefore) {
593 assert(ResultToks.back().is(tok::hashhash));
594 // Do not remove the paste operator if it is the one before __VA_OPT__
595 // (and we are still processing tokens within VA_OPT). We handle the case
596 // of removing the paste operator if __VA_OPT__ reduces to the notional
597 // placemarker above when we encounter the closing paren of VA_OPT.
598 if (!VCtx.isInVAOpt() ||
599 ResultToks.size() > VCtx.getNumberOfTokensPriorToVAOpt())
600 ResultToks.pop_back();
601 else
603 }
604
605 // If this is the __VA_ARGS__ token, and if the argument wasn't provided,
606 // and if the macro had at least one real argument, and if the token before
607 // the ## was a comma, remove the comma. This is a GCC extension which is
608 // disabled when using -std=c99.
609 if (ActualArgs->isVarargsElidedUse())
610 MaybeRemoveCommaBeforeVaArgs(ResultToks,
611 /*HasPasteOperator=*/true,
612 Macro, ArgNo, PP);
613 }
614
615 // If anything changed, install this as the new Tokens list.
616 if (MadeChange) {
617 assert(!OwnsTokens && "This would leak if we already own the token list");
618 // This is deleted in the dtor.
619 NumTokens = ResultToks.size();
620 // The tokens will be added to Preprocessor's cache and will be removed
621 // when this TokenLexer finishes lexing them.
622 Tokens = PP.cacheMacroExpandedTokens(this, ResultToks);
623
624 // The preprocessor cache of macro expanded tokens owns these tokens,not us.
625 OwnsTokens = false;
626 }
627}
628
629/// Checks if two tokens form wide string literal.
630static bool isWideStringLiteralFromMacro(const Token &FirstTok,
631 const Token &SecondTok) {
632 return FirstTok.is(tok::identifier) &&
633 FirstTok.getIdentifierInfo()->isStr("L") && SecondTok.isLiteral() &&
634 SecondTok.stringifiedInMacro();
635}
636
637/// Lex - Lex and return a token from this macro stream.
639 // Lexing off the end of the macro, pop this macro off the expansion stack.
640 if (isAtEnd()) {
641 // If this is a macro (not a token stream), mark the macro enabled now
642 // that it is no longer being expanded.
643 if (Macro) Macro->EnableMacro();
644
645 // CWG2947: Allow the following code:
646 //
647 // export module m; int x;
648 // extern "C++" int *y = &x;
649 //
650 // The 'extern' token should has 'StartOfLine' flag when current TokenLexer
651 // exits and propagate line start/leading space info.
652 if (!Macro && isLexingCXXModuleDirective()) {
653 AtStartOfLine = true;
655 }
656
657 Tok.startToken();
658 Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
659 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace || NextTokGetsSpace);
660 if (CurTokenIdx == 0)
662 return PP.HandleEndOfTokenLexer(Tok);
663 }
664
665 SourceManager &SM = PP.getSourceManager();
666
667 // If this is the first token of the expanded result, we inherit spacing
668 // properties later.
669 bool isFirstToken = CurTokenIdx == 0;
670
671 // Get the next token to return.
672 Tok = Tokens[CurTokenIdx++];
673 if (IsReinject)
674 Tok.setFlag(Token::IsReinjected);
675
676 bool TokenIsFromPaste = false;
677
678 // If this token is followed by a token paste (##) operator, paste the tokens!
679 // Note that ## is a normal token when not expanding a macro.
680 if (!isAtEnd() && Macro &&
681 (Tokens[CurTokenIdx].is(tok::hashhash) ||
682 // Special processing of L#x macros in -fms-compatibility mode.
683 // Microsoft compiler is able to form a wide string literal from
684 // 'L#macro_arg' construct in a function-like macro.
685 (PP.getLangOpts().MSVCCompat &&
686 isWideStringLiteralFromMacro(Tok, Tokens[CurTokenIdx])))) {
687 // When handling the microsoft /##/ extension, the final token is
688 // returned by pasteTokens, not the pasted token.
689 if (pasteTokens(Tok))
690 return true;
691
692 TokenIsFromPaste = true;
693 }
694
695 // The token's current location indicate where the token was lexed from. We
696 // need this information to compute the spelling of the token, but any
697 // diagnostics for the expanded token should appear as if they came from
698 // ExpansionLoc. Pull this information together into a new SourceLocation
699 // that captures all of this.
700 if (ExpandLocStart.isValid() && // Don't do this for token streams.
701 // Check that the token's location was not already set properly.
702 SM.isBeforeInSLocAddrSpace(Tok.getLocation(), MacroStartSLocOffset)) {
703 SourceLocation instLoc;
704 if (Tok.is(tok::comment)) {
705 instLoc = SM.createExpansionLoc(Tok.getLocation(),
706 ExpandLocStart,
707 ExpandLocEnd,
708 Tok.getLength());
709 } else {
710 instLoc = getExpansionLocForMacroDefLoc(Tok.getLocation());
711 }
712
713 Tok.setLocation(instLoc);
714 }
715
716 // If this is the first token, set the lexical properties of the token to
717 // match the lexical properties of the macro identifier.
718 if (isFirstToken) {
719 Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
720 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
721 } else {
722 // If this is not the first token, we may still need to pass through
723 // leading whitespace if we've expanded a macro.
724 if (AtStartOfLine) Tok.setFlag(Token::StartOfLine);
725 if (HasLeadingSpace) Tok.setFlag(Token::LeadingSpace);
726 }
727 AtStartOfLine = false;
728 HasLeadingSpace = false;
729
730 // Handle recursive expansion!
731 if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr &&
732 (!PP.getLangOpts().CPlusPlusModules ||
733 !Tok.isModuleContextualKeyword())) {
734 // Change the kind of this identifier to the appropriate token kind, e.g.
735 // turning "for" into a keyword.
736 IdentifierInfo *II = Tok.getIdentifierInfo();
737 Tok.setKind(II->getTokenID());
738
739 // If this identifier was poisoned and from a paste, emit an error. This
740 // won't be handled by Preprocessor::HandleIdentifier because this is coming
741 // from a macro expansion.
742 if (II->isPoisoned() && TokenIsFromPaste) {
743 PP.HandlePoisonedIdentifier(Tok);
744 }
745
746 if (!DisableMacroExpansion && II->isHandleIdentifierCase())
747 return PP.HandleIdentifier(Tok);
748 }
749
750 // Otherwise, return a normal token.
751 return true;
752}
753
754bool TokenLexer::pasteTokens(Token &Tok) {
755 return pasteTokens(Tok, llvm::ArrayRef(Tokens, NumTokens), CurTokenIdx);
756}
757
758/// LHSTok is the LHS of a ## operator, and CurTokenIdx is the ##
759/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
760/// are more ## after it, chomp them iteratively. Return the result as LHSTok.
761/// If this returns true, the caller should immediately return the token.
762bool TokenLexer::pasteTokens(Token &LHSTok, ArrayRef<Token> TokenStream,
763 unsigned int &CurIdx) {
764 assert(CurIdx > 0 && "## can not be the first token within tokens");
765 assert((TokenStream[CurIdx].is(tok::hashhash) ||
766 (PP.getLangOpts().MSVCCompat &&
767 isWideStringLiteralFromMacro(LHSTok, TokenStream[CurIdx]))) &&
768 "Token at this Index must be ## or part of the MSVC 'L "
769 "#macro-arg' pasting pair");
770
771 // MSVC: If previous token was pasted, this must be a recovery from an invalid
772 // paste operation. Ignore spaces before this token to mimic MSVC output.
773 // Required for generating valid UUID strings in some MS headers.
774 if (PP.getLangOpts().MicrosoftExt && (CurIdx >= 2) &&
775 TokenStream[CurIdx - 2].is(tok::hashhash))
777
778 SmallString<128> Buffer;
779 const char *ResultTokStrPtr = nullptr;
780 SourceLocation StartLoc = LHSTok.getLocation();
781 SourceLocation PasteOpLoc;
782 bool HasUCNs = false;
783
784 auto IsAtEnd = [&TokenStream, &CurIdx] {
785 return TokenStream.size() == CurIdx;
786 };
787
788 do {
789 // Consume the ## operator if any.
790 PasteOpLoc = TokenStream[CurIdx].getLocation();
791 if (TokenStream[CurIdx].is(tok::hashhash))
792 ++CurIdx;
793 assert(!IsAtEnd() && "No token on the RHS of a paste operator!");
794
795 // Get the RHS token.
796 const Token &RHS = TokenStream[CurIdx];
797
798 // Allocate space for the result token. This is guaranteed to be enough for
799 // the two tokens.
800 Buffer.resize(LHSTok.getLength() + RHS.getLength());
801
802 // Get the spelling of the LHS token in Buffer.
803 const char *BufPtr = &Buffer[0];
804 bool Invalid = false;
805 unsigned LHSLen = PP.getSpelling(LHSTok, BufPtr, &Invalid);
806 if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer!
807 memcpy(&Buffer[0], BufPtr, LHSLen);
808 if (Invalid)
809 return true;
810
811 BufPtr = Buffer.data() + LHSLen;
812 unsigned RHSLen = PP.getSpelling(RHS, BufPtr, &Invalid);
813 if (Invalid)
814 return true;
815 if (RHSLen && BufPtr != &Buffer[LHSLen])
816 // Really, we want the chars in Buffer!
817 memcpy(&Buffer[LHSLen], BufPtr, RHSLen);
818
819 // Trim excess space.
820 Buffer.resize(LHSLen+RHSLen);
821
822 // Plop the pasted result (including the trailing newline and null) into a
823 // scratch buffer where we can lex it.
824 Token ResultTokTmp;
825 ResultTokTmp.startToken();
826
827 // Claim that the tmp token is a string_literal so that we can get the
828 // character pointer back from CreateString in getLiteralData().
829 ResultTokTmp.setKind(tok::string_literal);
830 PP.CreateString(Buffer, ResultTokTmp);
831 SourceLocation ResultTokLoc = ResultTokTmp.getLocation();
832 ResultTokStrPtr = ResultTokTmp.getLiteralData();
833
834 // Lex the resultant pasted token into Result.
835 Token Result;
836
837 if (LHSTok.isAnyIdentifier() && RHS.isAnyIdentifier()) {
838 // Common paste case: identifier+identifier = identifier. Avoid creating
839 // a lexer and other overhead.
840 PP.IncrementPasteCounter(true);
841 Result =
842 Token::create(tok::raw_identifier, ResultTokLoc, LHSLen + RHSLen);
843 Result.setRawIdentifierData(ResultTokStrPtr);
844 } else {
845 PP.IncrementPasteCounter(false);
846
847 assert(ResultTokLoc.isFileID() &&
848 "Should be a raw location into scratch buffer");
849 SourceManager &SourceMgr = PP.getSourceManager();
850 FileID LocFileID = SourceMgr.getFileID(ResultTokLoc);
851
852 bool Invalid = false;
853 const char *ScratchBufStart
854 = SourceMgr.getBufferData(LocFileID, &Invalid).data();
855 if (Invalid)
856 return false;
857
858 // Make a lexer to lex this string from. Lex just this one token.
859 // Make a lexer object so that we lex and expand the paste result.
860 Lexer TL(SourceMgr.getLocForStartOfFile(LocFileID),
861 PP.getLangOpts(), ScratchBufStart,
862 ResultTokStrPtr, ResultTokStrPtr+LHSLen+RHSLen);
863
864 // Lex a token in raw mode. This way it won't look up identifiers
865 // automatically, lexing off the end will return an eof token, and
866 // warnings are disabled. This returns true if the result token is the
867 // entire buffer.
868 bool isInvalid = !TL.LexFromRawLexer(Result);
869
870 // If we got an EOF token, we didn't form even ONE token. For example, we
871 // did "/ ## /" to get "//".
872 isInvalid |= Result.is(tok::eof);
873
874 // If pasting the two tokens didn't form a full new token, this is an
875 // error. This occurs with "x ## +" and other stuff. Return with LHSTok
876 // unmodified and with RHS as the next token to lex.
877 if (isInvalid) {
878 // Explicitly convert the token location to have proper expansion
879 // information so that the user knows where it came from.
880 SourceManager &SM = PP.getSourceManager();
881 SourceLocation Loc =
882 SM.createExpansionLoc(PasteOpLoc, ExpandLocStart, ExpandLocEnd, 2);
883
884 // Test for the Microsoft extension of /##/ turning into // here on the
885 // error path.
886 if (PP.getLangOpts().MicrosoftExt && LHSTok.is(tok::slash) &&
887 RHS.is(tok::slash)) {
888 HandleMicrosoftCommentPaste(LHSTok, Loc);
889 return true;
890 }
891
892 // Do not emit the error when preprocessing assembler code.
893 if (!PP.getLangOpts().AsmPreprocessor) {
894 // If we're in microsoft extensions mode, downgrade this from a hard
895 // error to an extension that defaults to an error. This allows
896 // disabling it.
897 PP.Diag(Loc, PP.getLangOpts().MicrosoftExt ? diag::ext_pp_bad_paste_ms
898 : diag::err_pp_bad_paste)
899 << Buffer;
900 }
901
902 // An error has occurred so exit loop.
903 break;
904 }
905
906 // Turn ## into 'unknown' to avoid # ## # from looking like a paste
907 // operator.
908 if (Result.is(tok::hashhash))
909 Result.setKind(tok::unknown);
910 }
911
912 // Transfer properties of the LHS over the Result.
913 Result.setFlagValue(Token::StartOfLine , LHSTok.isAtStartOfLine());
914 Result.setFlagValue(Token::LeadingSpace, LHSTok.hasLeadingSpace());
915
916 // Finally, replace LHS with the result, consume the RHS, and iterate.
917 ++CurIdx;
918
919 // Set Token::HasUCN flag if LHS or RHS contains any UCNs.
920 HasUCNs = LHSTok.hasUCN() || RHS.hasUCN() || HasUCNs;
921 LHSTok = Result;
922 } while (!IsAtEnd() && TokenStream[CurIdx].is(tok::hashhash));
923
924 SourceLocation EndLoc = TokenStream[CurIdx - 1].getLocation();
925
926 // The token's current location indicate where the token was lexed from. We
927 // need this information to compute the spelling of the token, but any
928 // diagnostics for the expanded token should appear as if the token was
929 // expanded from the full ## expression. Pull this information together into
930 // a new SourceLocation that captures all of this.
931 SourceManager &SM = PP.getSourceManager();
932 if (StartLoc.isFileID())
933 StartLoc = getExpansionLocForMacroDefLoc(StartLoc);
934 if (EndLoc.isFileID())
935 EndLoc = getExpansionLocForMacroDefLoc(EndLoc);
936 FileID MacroFID = SM.getFileID(MacroExpansionStart);
937 while (SM.getFileID(StartLoc) != MacroFID)
938 StartLoc = SM.getImmediateExpansionRange(StartLoc).getBegin();
939 while (SM.getFileID(EndLoc) != MacroFID)
940 EndLoc = SM.getImmediateExpansionRange(EndLoc).getEnd();
941
942 LHSTok.setLocation(SM.createExpansionLoc(LHSTok.getLocation(), StartLoc, EndLoc,
943 LHSTok.getLength()));
944
945 // Now that we got the result token, it will be subject to expansion. Since
946 // token pasting re-lexes the result token in raw mode, identifier information
947 // isn't looked up. As such, if the result is an identifier, look up id info.
948 if (LHSTok.is(tok::raw_identifier)) {
949
950 // If there has any UNCs in concated token, we should mark this token
951 // with Token::HasUCN flag, then LookUpIdentifierInfo will expand UCNs in
952 // token.
953 if (HasUCNs)
954 LHSTok.setFlag(Token::HasUCN);
955
956 // Look up the identifier info for the token. We disabled identifier lookup
957 // by saying we're skipping contents, so we need to do this manually.
958 PP.LookUpIdentifierInfo(LHSTok);
959 }
960 return false;
961}
962
963/// isNextTokenLParen - If the next token lexed will pop this macro off the
964/// expansion stack, return std::nullopt, otherwise return the next unexpanded
965/// token.
966std::optional<Token> TokenLexer::peekNextPPToken() const {
967 // Out of tokens?
968 if (isAtEnd())
969 return std::nullopt;
970 return Tokens[CurTokenIdx];
971}
972
973/// isParsingPreprocessorDirective - Return true if we are in the middle of a
974/// preprocessor directive.
976 return Tokens[NumTokens-1].is(tok::eod) && !isAtEnd();
977}
978
979/// setLexingCXXModuleDirective - This is set to true if this TokenLexer is
980/// created when handling C++ module directive.
982 LexingCXXModuleDirective = Val;
983}
984
985/// isLexingCXXModuleDirective - Return true if we are lexing a C++ module or
986/// import directive.
988 return LexingCXXModuleDirective;
989}
990
991/// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
992/// together to form a comment that comments out everything in the current
993/// macro, other active macros, and anything left on the current physical
994/// source line of the expanded buffer. Handle this by returning the
995/// first token on the next line.
996void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok, SourceLocation OpLoc) {
997 PP.Diag(OpLoc, diag::ext_comment_paste_microsoft);
998
999 // We 'comment out' the rest of this macro by just ignoring the rest of the
1000 // tokens that have not been lexed yet, if any.
1001
1002 // Since this must be a macro, mark the macro enabled now that it is no longer
1003 // being expanded.
1004 assert(Macro && "Token streams can't paste comments");
1005 Macro->EnableMacro();
1006
1008}
1009
1010/// If \arg loc is a file ID and points inside the current macro
1011/// definition, returns the appropriate source location pointing at the
1012/// macro expansion source location entry, otherwise it returns an invalid
1013/// SourceLocation.
1015TokenLexer::getExpansionLocForMacroDefLoc(SourceLocation loc) const {
1016 assert(ExpandLocStart.isValid() && MacroExpansionStart.isValid() &&
1017 "Not appropriate for token streams");
1018 assert(loc.isValid() && loc.isFileID());
1019
1021 assert(SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) &&
1022 "Expected loc to come from the macro definition");
1023
1024 SourceLocation::UIntTy relativeOffset = 0;
1025 SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength, &relativeOffset);
1026 return MacroExpansionStart.getLocWithOffset(relativeOffset);
1027}
1028
1029/// Finds the tokens that are consecutive (from the same FileID)
1030/// creates a single SLocEntry, and assigns SourceLocations to each token that
1031/// point to that SLocEntry. e.g for
1032/// assert(foo == bar);
1033/// There will be a single SLocEntry for the "foo == bar" chunk and locations
1034/// for the 'foo', '==', 'bar' tokens will point inside that chunk.
1035///
1036/// \arg begin_tokens will be updated to a position past all the found
1037/// consecutive tokens.
1039 SourceLocation ExpandLoc,
1040 Token *&begin_tokens,
1041 Token * end_tokens) {
1042 assert(begin_tokens + 1 < end_tokens);
1043 SourceLocation BeginLoc = begin_tokens->getLocation();
1044 llvm::MutableArrayRef<Token> All(begin_tokens, end_tokens);
1046
1047 auto NearLast = [&, Last = BeginLoc](SourceLocation Loc) mutable {
1048 // The maximum distance between two consecutive tokens in a partition.
1049 // This is an important trick to avoid using too much SourceLocation address
1050 // space!
1051 static constexpr SourceLocation::IntTy MaxDistance = 50;
1052 auto Distance = Loc.getRawEncoding() - Last.getRawEncoding();
1053 Last = Loc;
1054 return Distance <= MaxDistance;
1055 };
1056
1057 // Partition the tokens by their FileID.
1058 // This is a hot function, and calling getFileID can be expensive, the
1059 // implementation is optimized by reducing the number of getFileID.
1060 if (BeginLoc.isFileID()) {
1061 // Consecutive tokens not written in macros must be from the same file.
1062 // (Neither #include nor eof can occur inside a macro argument.)
1063 Partition = All.take_while([&](const Token &T) {
1064 return T.getLocation().isFileID() && NearLast(T.getLocation());
1065 });
1066 } else {
1067 // Call getFileID once to calculate the bounds, and use the cheaper
1068 // sourcelocation-against-bounds comparison.
1069 FileID BeginFID = SM.getFileID(BeginLoc);
1070 SourceLocation Limit =
1071 SM.getComposedLoc(BeginFID, SM.getFileIDSize(BeginFID));
1072 Partition = All.take_while([&](const Token &T) {
1073 // NOTE: the Limit is included! The lexer recovery only ever inserts a
1074 // single token past the end of the FileID, specifically the ) when a
1075 // macro-arg containing a comma should be guarded by parentheses.
1076 //
1077 // It is safe to include the Limit here because SourceManager allocates
1078 // FileSize + 1 for each SLocEntry.
1079 //
1080 // See https://github.com/llvm/llvm-project/issues/60722.
1081 return T.getLocation() >= BeginLoc && T.getLocation() <= Limit
1082 && NearLast(T.getLocation());
1083 });
1084 }
1085 assert(!Partition.empty());
1086
1087 // For the consecutive tokens, find the length of the SLocEntry to contain
1088 // all of them.
1089 SourceLocation::UIntTy FullLength =
1090 Partition.back().getEndLoc().getRawEncoding() -
1091 Partition.front().getLocation().getRawEncoding();
1092 // Create a macro expansion SLocEntry that will "contain" all of the tokens.
1093 SourceLocation Expansion =
1094 SM.createMacroArgExpansionLoc(BeginLoc, ExpandLoc, FullLength);
1095
1096#ifdef EXPENSIVE_CHECKS
1097 assert(llvm::all_of(Partition.drop_front(),
1098 [&SM, ID = SM.getFileID(Partition.front().getLocation())](
1099 const Token &T) {
1100 return ID == SM.getFileID(T.getLocation());
1101 }) &&
1102 "Must have the same FIleID!");
1103#endif
1104 // Change the location of the tokens from the spelling location to the new
1105 // expanded location.
1106 for (Token& T : Partition) {
1107 SourceLocation::IntTy RelativeOffset =
1108 T.getLocation().getRawEncoding() - BeginLoc.getRawEncoding();
1109 T.setLocation(Expansion.getLocWithOffset(RelativeOffset));
1110 }
1111 begin_tokens = &Partition.back() + 1;
1112}
1113
1114/// Creates SLocEntries and updates the locations of macro argument
1115/// tokens to their new expanded locations.
1116///
1117/// \param ArgIdSpellLoc the location of the macro argument id inside the macro
1118/// definition.
1119void TokenLexer::updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc,
1120 Token *begin_tokens,
1121 Token *end_tokens) {
1122 SourceManager &SM = PP.getSourceManager();
1123
1124 SourceLocation ExpandLoc =
1125 getExpansionLocForMacroDefLoc(ArgIdSpellLoc);
1126
1127 while (begin_tokens < end_tokens) {
1128 // If there's only one token just create a SLocEntry for it.
1129 if (end_tokens - begin_tokens == 1) {
1130 Token &Tok = *begin_tokens;
1132 ExpandLoc,
1133 Tok.getLength()));
1134 return;
1135 }
1136
1137 updateConsecutiveMacroArgTokens(SM, ExpandLoc, begin_tokens, end_tokens);
1138 }
1139}
1140
1141void TokenLexer::PropagateLineStartLeadingSpaceInfo(Token &Result) {
1142 AtStartOfLine = Result.isAtStartOfLine();
1143 HasLeadingSpace = Result.hasLeadingSpace();
1144}
Defines the Diagnostic-related interfaces.
bool is(tok::TokenKind Kind) const
Token Tok
The Token.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines the clang::LangOptions interface.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines the clang::MacroInfo and clang::MacroDirective classes.
Defines the clang::Preprocessor interface.
Defines the clang::SourceLocation class and associated facilities.
static bool isInvalid(LocType Loc, bool *Invalid)
Defines the SourceManager interface.
Defines the clang::TokenKind enum and support functions.
static bool isWideStringLiteralFromMacro(const Token &FirstTok, const Token &SecondTok)
Checks if two tokens form wide string literal.
static bool hasVaOptSupport(const LangOptions &LangOpts)
static void updateConsecutiveMacroArgTokens(SourceManager &SM, SourceLocation ExpandLoc, Token *&begin_tokens, Token *end_tokens)
Finds the tokens that are consecutive (from the same FileID) creates a single SLocEntry,...
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
SourceLocation getEnd() const
SourceLocation getBegin() const
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition Diagnostic.h:140
One of these records is kept for each identifier that is lexed.
bool isHandleIdentifierCase() const
Return true if the Preprocessor::HandleIdentifier must be called on a token of this identifier.
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
bool isPoisoned() const
Return true if this token has been poisoned.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition MacroArgs.h:30
static unsigned getArgLength(const Token *ArgPtr)
getArgLength - Given a pointer to an expanded or unexpanded argument, return the number of tokens,...
static Token StringifyArgument(const Token *ArgToks, Preprocessor &PP, bool Charify, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd)
StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of tokens into the literal string...
void destroy(Preprocessor &PP)
destroy - Destroy and deallocate the memory for this object.
Definition MacroArgs.cpp:77
Encapsulates the data about a macro definition (e.g.
Definition MacroInfo.h:40
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
SourceManager & getSourceManager() const
const LangOptions & getLangOpts() const
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
void HandleMicrosoftCommentPaste(Token &Tok)
When the macro expander pastes together a comment (/##/) in Microsoft mode, this method handles updat...
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
UIntTy getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it.
This class handles loading and caching of source files into memory.
bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the "source locationaddress space".
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
SourceLocation::UIntTy getNextLocalOffset() const
bool isInSLocAddrSpace(SourceLocation Loc, SourceLocation Start, unsigned Length, SourceLocation::UIntTy *RelativeOffset=nullptr) const
Returns true if Loc is inside the [Start, +Length) chunk of the source location address space.
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
SourceLocation getComposedLoc(FileID FID, unsigned Offset) const
Form a SourceLocation from a FileID and Offset pair.
unsigned getFileIDSize(FileID FID) const
The size of the SLocEntry that FID represents.
CharSourceRange getImmediateExpansionRange(SourceLocation Loc) const
Return the start/end of the expansion information for an expansion location.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
SourceLocation createExpansionLoc(SourceLocation SpellingLoc, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd, unsigned Length, bool ExpansionIsTokenRange=true, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Creates an expansion SLocEntry for a macro use.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID.
SourceLocation createMacroArgExpansionLoc(SourceLocation SpellingLoc, SourceLocation ExpansionLoc, unsigned Length)
Creates an expansion SLocEntry for the substitution of an argument into a function-like macro's body.
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.
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),...
void Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI, MacroArgs *Actuals)
Initialize this TokenLexer to expand from the specified macro with the specified argument information...
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
IdentifierInfo * getIdentifierInfo() const
Definition Token.h:225
bool isAnyIdentifier() const
Return true if this is a raw identifier (when lexing in raw mode) or a non-keyword identifier (when l...
Definition Token.h:120
bool hasUCN() const
Returns true if this token contains a universal character name.
Definition Token.h:352
void clearFlag(TokenFlags Flag)
Unset the specified flag.
Definition Token.h:292
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Definition Token.h:126
static Token create(tok::TokenKind Kind, SourceLocation Loc, unsigned Length=0)
Definition Token.h:195
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition Token.h:142
unsigned getLength() const
Definition Token.h:145
void setKind(tok::TokenKind K)
Definition Token.h:100
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition Token.h:104
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition Token.h:314
bool isOneOf(Ts... Ks) const
Definition Token.h:105
@ IgnoredComma
Definition Token.h:84
@ IsReinjected
Definition Token.h:89
@ LeadingEmptyMacro
Definition Token.h:81
@ LeadingSpace
Definition Token.h:77
@ StartOfLine
Definition Token.h:75
@ StringifiedInMacro
Definition Token.h:85
@ CommaAfterElided
Definition Token.h:87
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition Token.h:322
void setLocation(SourceLocation L)
Definition Token.h:150
bool isNot(tok::TokenKind K) const
Definition Token.h:111
bool stringifiedInMacro() const
Returns true if this token is formed by macro by stringizing or charizing operator.
Definition Token.h:356
void startToken()
Reset all flags to cleared.
Definition Token.h:187
const char * getLiteralData() const
getLiteralData - For a literal token (numeric constant, string, etc), this returns a pointer to the s...
Definition Token.h:263
void setFlag(TokenFlags Flag)
Set the specified flag.
Definition Token.h:282
A class for tracking whether we're inside a VA_OPT during a traversal of the tokens of a macro during...
bool isInVAOpt() const
Returns true if we have seen the VA_OPT and '(' but before having seen the matching ')'.
bool isVAOptToken(const Token &T) const
void sawHashOrHashAtBefore(const bool HasLeadingSpace, const bool IsHashAt)
SourceLocation getVAOptLoc() const
unsigned int getNumberOfTokensPriorToVAOpt() const
void sawOpeningParen(SourceLocation LParenLoc)
Call this function each time an lparen is seen.
bool sawClosingParen()
Call this function each time an rparen is seen.
void sawVAOptFollowedByOpeningParens(const SourceLocation VAOptLoc, const unsigned int NumPriorTokens)
Top level wrappers for InstallAPI frontend operations.
@ Result
The result type of a method or function.
Definition TypeBase.h:906
const FunctionProtoType * T