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