blob: e903973336eba8e72e3de5c5ad718cdaf4e15c24 [file] [log] [blame]
Chris Lattner1543e9c2008-03-09 02:18:51 +00001//===--- TokenLexer.cpp - Lex from a token stream -------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner1543e9c2008-03-09 02:18:51 +000010// This file implements the TokenLexer interface.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/MacroExpander.h"
15#include "clang/Lex/MacroInfo.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/Diagnostic.h"
19#include "llvm/ADT/SmallVector.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// MacroArgs Implementation
24//===----------------------------------------------------------------------===//
25
26/// MacroArgs ctor function - This destroys the vector passed in.
27MacroArgs *MacroArgs::create(const MacroInfo *MI,
Chris Lattnerd2177732007-07-20 16:59:19 +000028 const Token *UnexpArgTokens,
Reid Spencer5f016e22007-07-11 17:01:13 +000029 unsigned NumToks, bool VarargsElided) {
30 assert(MI->isFunctionLike() &&
31 "Can't have args for an object-like macro!");
32
33 // Allocate memory for the MacroArgs object with the lexer tokens at the end.
34 MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) +
Chris Lattnerd2177732007-07-20 16:59:19 +000035 NumToks*sizeof(Token));
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Construct the macroargs object.
37 new (Result) MacroArgs(NumToks, VarargsElided);
38
39 // Copy the actual unexpanded tokens to immediately after the result ptr.
40 if (NumToks)
Chris Lattnerd2177732007-07-20 16:59:19 +000041 memcpy(const_cast<Token*>(Result->getUnexpArgument(0)),
42 UnexpArgTokens, NumToks*sizeof(Token));
Reid Spencer5f016e22007-07-11 17:01:13 +000043
44 return Result;
45}
46
47/// destroy - Destroy and deallocate the memory for this object.
48///
49void MacroArgs::destroy() {
50 // Run the dtor to deallocate the vectors.
51 this->~MacroArgs();
52 // Release the memory for the object.
53 free(this);
54}
55
56
57/// getArgLength - Given a pointer to an expanded or unexpanded argument,
58/// return the number of tokens, not counting the EOF, that make up the
59/// argument.
Chris Lattnerd2177732007-07-20 16:59:19 +000060unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 unsigned NumArgTokens = 0;
Chris Lattner22f6bbc2007-10-09 18:02:16 +000062 for (; ArgPtr->isNot(tok::eof); ++ArgPtr)
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ++NumArgTokens;
64 return NumArgTokens;
65}
66
67
68/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
69///
Chris Lattnerd2177732007-07-20 16:59:19 +000070const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
Reid Spencer5f016e22007-07-11 17:01:13 +000071 // The unexpanded argument tokens start immediately after the MacroArgs object
72 // in memory.
Chris Lattnerd2177732007-07-20 16:59:19 +000073 const Token *Start = (const Token *)(this+1);
74 const Token *Result = Start;
Reid Spencer5f016e22007-07-11 17:01:13 +000075 // Scan to find Arg.
76 for (; Arg; ++Result) {
77 assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
Chris Lattner22f6bbc2007-10-09 18:02:16 +000078 if (Result->is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +000079 --Arg;
80 }
81 return Result;
82}
83
84
85/// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
86/// by pre-expansion, return false. Otherwise, conservatively return true.
Chris Lattnercc1a8752007-10-07 08:44:20 +000087bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
88 Preprocessor &PP) const {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 // If there are no identifiers in the argument list, or if the identifiers are
90 // known to not be macros, pre-expansion won't modify it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +000091 for (; ArgTok->isNot(tok::eof); ++ArgTok)
Reid Spencer5f016e22007-07-11 17:01:13 +000092 if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
Chris Lattnercc1a8752007-10-07 08:44:20 +000093 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled())
Reid Spencer5f016e22007-07-11 17:01:13 +000094 // Return true even though the macro could be a function-like macro
95 // without a following '(' token.
96 return true;
97 }
98 return false;
99}
100
101/// getPreExpArgument - Return the pre-expanded form of the specified
102/// argument.
Chris Lattnerd2177732007-07-20 16:59:19 +0000103const std::vector<Token> &
Reid Spencer5f016e22007-07-11 17:01:13 +0000104MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
105 assert(Arg < NumUnexpArgTokens && "Invalid argument number!");
106
107 // If we have already computed this, return it.
108 if (PreExpArgTokens.empty())
109 PreExpArgTokens.resize(NumUnexpArgTokens);
110
Chris Lattnerd2177732007-07-20 16:59:19 +0000111 std::vector<Token> &Result = PreExpArgTokens[Arg];
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 if (!Result.empty()) return Result;
113
Chris Lattnerd2177732007-07-20 16:59:19 +0000114 const Token *AT = getUnexpArgument(Arg);
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
116
117 // Otherwise, we have to pre-expand this argument, populating Result. To do
Chris Lattner1543e9c2008-03-09 02:18:51 +0000118 // this, we set up a fake TokenLexer to lex from the unexpanded argument
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // list. With this installed, we lex expanded tokens until we hit the EOF
120 // token at the end of the unexp list.
121 PP.EnterTokenStream(AT, NumToks);
122
123 // Lex all of the macro-expanded tokens into Result.
124 do {
Chris Lattnerd2177732007-07-20 16:59:19 +0000125 Result.push_back(Token());
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 PP.Lex(Result.back());
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000127 } while (Result.back().isNot(tok::eof));
Reid Spencer5f016e22007-07-11 17:01:13 +0000128
129 // Pop the token stream off the top of the stack. We know that the internal
130 // pointer inside of it is to the "end" of the token stream, but the stack
131 // will not otherwise be popped until the next token is lexed. The problem is
132 // that the token may be lexed sometime after the vector of tokens itself is
133 // destroyed, which would be badness.
134 PP.RemoveTopOfLexerStack();
135 return Result;
136}
137
138
139/// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
140/// tokens into the literal string token that should be produced by the C #
141/// preprocessor operator.
142///
Chris Lattnerd2177732007-07-20 16:59:19 +0000143static Token StringifyArgument(const Token *ArgToks,
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 Preprocessor &PP, bool Charify = false) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000145 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 Tok.startToken();
147 Tok.setKind(tok::string_literal);
148
Chris Lattnerd2177732007-07-20 16:59:19 +0000149 const Token *ArgTokStart = ArgToks;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150
151 // Stringify all the tokens.
152 std::string Result = "\"";
153 // FIXME: Optimize this loop to not use std::strings.
154 bool isFirst = true;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000155 for (; ArgToks->isNot(tok::eof); ++ArgToks) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000156 const Token &Tok = *ArgToks;
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000157 if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 Result += ' ';
159 isFirst = false;
160
161 // If this is a string or character constant, escape the token as specified
162 // by 6.10.3.2p2.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000163 if (Tok.is(tok::string_literal) || // "foo"
164 Tok.is(tok::wide_string_literal) || // L"foo"
165 Tok.is(tok::char_constant)) { // 'x' and L'x'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 Result += Lexer::Stringify(PP.getSpelling(Tok));
167 } else {
168 // Otherwise, just append the token.
169 Result += PP.getSpelling(Tok);
170 }
171 }
172
173 // If the last character of the string is a \, and if it isn't escaped, this
174 // is an invalid string literal, diagnose it as specified in C99.
175 if (Result[Result.size()-1] == '\\') {
176 // Count the number of consequtive \ characters. If even, then they are
177 // just escaped backslashes, otherwise it's an error.
178 unsigned FirstNonSlash = Result.size()-2;
179 // Guaranteed to find the starting " if nothing else.
180 while (Result[FirstNonSlash] == '\\')
181 --FirstNonSlash;
182 if ((Result.size()-1-FirstNonSlash) & 1) {
183 // Diagnose errors for things like: #define F(X) #X / F(\)
184 PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
185 Result.erase(Result.end()-1); // remove one of the \'s.
186 }
187 }
188 Result += '"';
189
190 // If this is the charify operation and the result is not a legal character
191 // constant, diagnose it.
192 if (Charify) {
193 // First step, turn double quotes into single quotes:
194 Result[0] = '\'';
195 Result[Result.size()-1] = '\'';
196
197 // Check for bogus character.
198 bool isBad = false;
199 if (Result.size() == 3) {
200 isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
201 } else {
202 isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
203 }
204
205 if (isBad) {
206 PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
207 Result = "' '"; // Use something arbitrary, but legal.
208 }
209 }
210
211 Tok.setLength(Result.size());
212 Tok.setLocation(PP.CreateString(&Result[0], Result.size()));
213 return Tok;
214}
215
216/// getStringifiedArgument - Compute, cache, and return the specified argument
217/// that has been 'stringified' as required by the # operator.
Chris Lattnerd2177732007-07-20 16:59:19 +0000218const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo,
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 Preprocessor &PP) {
220 assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
221 if (StringifiedArgs.empty()) {
222 StringifiedArgs.resize(getNumArguments());
223 memset(&StringifiedArgs[0], 0,
224 sizeof(StringifiedArgs[0])*getNumArguments());
225 }
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000226 if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
228 return StringifiedArgs[ArgNo];
229}
230
231//===----------------------------------------------------------------------===//
Chris Lattner1543e9c2008-03-09 02:18:51 +0000232// TokenLexer Implementation
Reid Spencer5f016e22007-07-11 17:01:13 +0000233//===----------------------------------------------------------------------===//
234
Chris Lattner1543e9c2008-03-09 02:18:51 +0000235/// Create a TokenLexer for the specified macro with the specified actual
Reid Spencer5f016e22007-07-11 17:01:13 +0000236/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000237void TokenLexer::Init(Token &Tok, MacroArgs *Actuals) {
238 // If the client is reusing a TokenLexer, make sure to free any memory
Chris Lattner9594acf2007-07-15 00:25:26 +0000239 // associated with it.
240 destroy();
241
Chris Lattnercc1a8752007-10-07 08:44:20 +0000242 Macro = PP.getMacroInfo(Tok.getIdentifierInfo());
Chris Lattner9594acf2007-07-15 00:25:26 +0000243 ActualArgs = Actuals;
244 CurToken = 0;
245 InstantiateLoc = Tok.getLocation();
246 AtStartOfLine = Tok.isAtStartOfLine();
247 HasLeadingSpace = Tok.hasLeadingSpace();
Chris Lattner8d896432008-03-09 02:07:49 +0000248 Tokens = &*Macro->tokens_begin();
249 OwnsTokens = false;
250 NumTokens = Macro->tokens_end()-Macro->tokens_begin();
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
252 // If this is a function-like macro, expand the arguments and change
Chris Lattner8d896432008-03-09 02:07:49 +0000253 // Tokens to point to the expanded tokens.
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 if (Macro->isFunctionLike() && Macro->getNumArgs())
255 ExpandFunctionArguments();
256
257 // Mark the macro as currently disabled, so that it is not recursively
258 // expanded. The macro must be disabled only after argument pre-expansion of
259 // function-like macro arguments occurs.
260 Macro->DisableMacro();
261}
262
Chris Lattner9594acf2007-07-15 00:25:26 +0000263
264
Chris Lattner1543e9c2008-03-09 02:18:51 +0000265/// Create a TokenLexer for the specified token stream. This does not
Reid Spencer5f016e22007-07-11 17:01:13 +0000266/// take ownership of the specified token vector.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000267void TokenLexer::Init(const Token *TokArray, unsigned NumToks) {
268 // If the client is reusing a TokenLexer, make sure to free any memory
Chris Lattner9594acf2007-07-15 00:25:26 +0000269 // associated with it.
270 destroy();
271
272 Macro = 0;
273 ActualArgs = 0;
Chris Lattner8d896432008-03-09 02:07:49 +0000274 Tokens = TokArray;
275 OwnsTokens = false;
276 NumTokens = NumToks;
Chris Lattner9594acf2007-07-15 00:25:26 +0000277 CurToken = 0;
278 InstantiateLoc = SourceLocation();
279 AtStartOfLine = false;
280 HasLeadingSpace = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
282 // Set HasLeadingSpace/AtStartOfLine so that the first token will be
283 // returned unmodified.
284 if (NumToks != 0) {
285 AtStartOfLine = TokArray[0].isAtStartOfLine();
286 HasLeadingSpace = TokArray[0].hasLeadingSpace();
287 }
288}
289
290
Chris Lattner1543e9c2008-03-09 02:18:51 +0000291void TokenLexer::destroy() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // If this was a function-like macro that actually uses its arguments, delete
293 // the expanded tokens.
Chris Lattner8d896432008-03-09 02:07:49 +0000294 if (OwnsTokens) {
295 delete [] Tokens;
296 Tokens = 0;
Chris Lattner9c683062007-07-22 01:16:55 +0000297 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000298
Chris Lattner1543e9c2008-03-09 02:18:51 +0000299 // TokenLexer owns its formal arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 if (ActualArgs) ActualArgs->destroy();
301}
302
303/// Expand the arguments of a function-like macro so that we can quickly
Chris Lattner8d896432008-03-09 02:07:49 +0000304/// return preexpanded tokens from Tokens.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000305void TokenLexer::ExpandFunctionArguments() {
Chris Lattnerd2177732007-07-20 16:59:19 +0000306 llvm::SmallVector<Token, 128> ResultToks;
Reid Spencer5f016e22007-07-11 17:01:13 +0000307
Chris Lattner8d896432008-03-09 02:07:49 +0000308 // Loop through 'Tokens', expanding them into ResultToks. Keep
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // track of whether we change anything. If not, no need to keep them. If so,
Chris Lattner8d896432008-03-09 02:07:49 +0000310 // we install the newly expanded sequence as the new 'Tokens' list.
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 bool MadeChange = false;
312
313 // NextTokGetsSpace - When this is true, the next token appended to the
314 // output list will get a leading space, regardless of whether it had one to
315 // begin with or not. This is used for placemarker support.
316 bool NextTokGetsSpace = false;
317
Chris Lattner8d896432008-03-09 02:07:49 +0000318 for (unsigned i = 0, e = NumTokens; i != e; ++i) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // If we found the stringify operator, get the argument stringified. The
320 // preprocessor already verified that the following token is a macro name
321 // when the #define was parsed.
Chris Lattner8d896432008-03-09 02:07:49 +0000322 const Token &CurTok = Tokens[i];
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000323 if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) {
Chris Lattner8d896432008-03-09 02:07:49 +0000324 int ArgNo = Macro->getArgumentNum(Tokens[i+1].getIdentifierInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 assert(ArgNo != -1 && "Token following # is not an argument?");
326
Chris Lattnerd2177732007-07-20 16:59:19 +0000327 Token Res;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000328 if (CurTok.is(tok::hash)) // Stringify
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 Res = ActualArgs->getStringifiedArgument(ArgNo, PP);
330 else {
331 // 'charify': don't bother caching these.
332 Res = StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), PP, true);
333 }
334
335 // The stringified/charified string leading space flag gets set to match
336 // the #/#@ operator.
337 if (CurTok.hasLeadingSpace() || NextTokGetsSpace)
Chris Lattnerd2177732007-07-20 16:59:19 +0000338 Res.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
340 ResultToks.push_back(Res);
341 MadeChange = true;
342 ++i; // Skip arg name.
343 NextTokGetsSpace = false;
344 continue;
345 }
346
347 // Otherwise, if this is not an argument token, just add the token to the
348 // output buffer.
349 IdentifierInfo *II = CurTok.getIdentifierInfo();
350 int ArgNo = II ? Macro->getArgumentNum(II) : -1;
351 if (ArgNo == -1) {
352 // This isn't an argument, just add it.
353 ResultToks.push_back(CurTok);
354
355 if (NextTokGetsSpace) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000356 ResultToks.back().setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 NextTokGetsSpace = false;
358 }
359 continue;
360 }
361
362 // An argument is expanded somehow, the result is different than the
363 // input.
364 MadeChange = true;
365
366 // Otherwise, this is a use of the argument. Find out if there is a paste
367 // (##) operator before or after the argument.
368 bool PasteBefore =
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000369 !ResultToks.empty() && ResultToks.back().is(tok::hashhash);
Chris Lattner8d896432008-03-09 02:07:49 +0000370 bool PasteAfter = i+1 != e && Tokens[i+1].is(tok::hashhash);
Reid Spencer5f016e22007-07-11 17:01:13 +0000371
372 // If it is not the LHS/RHS of a ## operator, we must pre-expand the
373 // argument and substitute the expanded tokens into the result. This is
374 // C99 6.10.3.1p1.
375 if (!PasteBefore && !PasteAfter) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000376 const Token *ResultArgToks;
Reid Spencer5f016e22007-07-11 17:01:13 +0000377
378 // Only preexpand the argument if it could possibly need it. This
379 // avoids some work in common cases.
Chris Lattnerd2177732007-07-20 16:59:19 +0000380 const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
Chris Lattnercc1a8752007-10-07 08:44:20 +0000381 if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
383 else
384 ResultArgToks = ArgTok; // Use non-preexpanded tokens.
385
386 // If the arg token expanded into anything, append it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000387 if (ResultArgToks->isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 unsigned FirstResult = ResultToks.size();
389 unsigned NumToks = MacroArgs::getArgLength(ResultArgToks);
390 ResultToks.append(ResultArgToks, ResultArgToks+NumToks);
391
392 // If any tokens were substituted from the argument, the whitespace
393 // before the first token should match the whitespace of the arg
394 // identifier.
Chris Lattnerd2177732007-07-20 16:59:19 +0000395 ResultToks[FirstResult].setFlagValue(Token::LeadingSpace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 CurTok.hasLeadingSpace() ||
397 NextTokGetsSpace);
398 NextTokGetsSpace = false;
399 } else {
400 // If this is an empty argument, and if there was whitespace before the
401 // formal token, make sure the next token gets whitespace before it.
402 NextTokGetsSpace = CurTok.hasLeadingSpace();
403 }
404 continue;
405 }
406
407 // Okay, we have a token that is either the LHS or RHS of a paste (##)
408 // argument. It gets substituted as its non-pre-expanded tokens.
Chris Lattnerd2177732007-07-20 16:59:19 +0000409 const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 unsigned NumToks = MacroArgs::getArgLength(ArgToks);
411 if (NumToks) { // Not an empty argument?
Chris Lattner71a3a8d2008-01-29 07:54:23 +0000412 // If this is the GNU ", ## __VA_ARG__" extension, and we just learned
413 // that __VA_ARG__ expands to multiple tokens, avoid a pasting error when
414 // the expander trys to paste ',' with the first token of the __VA_ARG__
415 // expansion.
416 if (PasteBefore && ResultToks.size() >= 2 &&
417 ResultToks[ResultToks.size()-2].is(tok::comma) &&
418 (unsigned)ArgNo == Macro->getNumArgs()-1 &&
419 Macro->isVariadic()) {
420 // Remove the paste operator, report use of the extension.
421 PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma);
422 ResultToks.pop_back();
423 }
424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 ResultToks.append(ArgToks, ArgToks+NumToks);
426
427 // If the next token was supposed to get leading whitespace, ensure it has
428 // it now.
429 if (NextTokGetsSpace) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000430 ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 NextTokGetsSpace = false;
432 }
433 continue;
434 }
435
436 // If an empty argument is on the LHS or RHS of a paste, the standard (C99
437 // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We
438 // implement this by eating ## operators when a LHS or RHS expands to
439 // empty.
440 NextTokGetsSpace |= CurTok.hasLeadingSpace();
441 if (PasteAfter) {
442 // Discard the argument token and skip (don't copy to the expansion
443 // buffer) the paste operator after it.
Chris Lattner8d896432008-03-09 02:07:49 +0000444 NextTokGetsSpace |= Tokens[i+1].hasLeadingSpace();
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 ++i;
446 continue;
447 }
448
449 // If this is on the RHS of a paste operator, we've already copied the
450 // paste operator to the ResultToks list. Remove it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000451 assert(PasteBefore && ResultToks.back().is(tok::hashhash));
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 NextTokGetsSpace |= ResultToks.back().hasLeadingSpace();
453 ResultToks.pop_back();
454
455 // If this is the __VA_ARGS__ token, and if the argument wasn't provided,
456 // and if the macro had at least one real argument, and if the token before
457 // the ## was a comma, remove the comma.
458 if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__
459 ActualArgs->isVarargsElidedUse() && // Argument elided.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000460 !ResultToks.empty() && ResultToks.back().is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 // Never add a space, even if the comma, ##, or arg had a space.
462 NextTokGetsSpace = false;
Chris Lattner71a3a8d2008-01-29 07:54:23 +0000463 // Remove the paste operator, report use of the extension.
464 PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 ResultToks.pop_back();
466 }
467 continue;
468 }
469
Chris Lattner8d896432008-03-09 02:07:49 +0000470 // If anything changed, install this as the new Tokens list.
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 if (MadeChange) {
472 // This is deleted in the dtor.
Chris Lattner8d896432008-03-09 02:07:49 +0000473 NumTokens = ResultToks.size();
Chris Lattnerd2177732007-07-20 16:59:19 +0000474 Token *Res = new Token[ResultToks.size()];
Chris Lattner8d896432008-03-09 02:07:49 +0000475 if (NumTokens)
476 memcpy(Res, &ResultToks[0], NumTokens*sizeof(Token));
477 Tokens = Res;
478 OwnsTokens = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 }
480}
481
482/// Lex - Lex and return a token from this macro stream.
483///
Chris Lattner1543e9c2008-03-09 02:18:51 +0000484void TokenLexer::Lex(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 // Lexing off the end of the macro, pop this macro off the expansion stack.
486 if (isAtEnd()) {
487 // If this is a macro (not a token stream), mark the macro enabled now
488 // that it is no longer being expanded.
489 if (Macro) Macro->EnableMacro();
490
491 // Pop this context off the preprocessors lexer stack and get the next
492 // token. This will delete "this" so remember the PP instance var.
493 Preprocessor &PPCache = PP;
494 if (PP.HandleEndOfMacro(Tok))
495 return;
496
497 // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is
498 // next.
499 return PPCache.Lex(Tok);
500 }
501
502 // If this is the first token of the expanded result, we inherit spacing
503 // properties later.
504 bool isFirstToken = CurToken == 0;
505
506 // Get the next token to return.
Chris Lattner8d896432008-03-09 02:07:49 +0000507 Tok = Tokens[CurToken++];
Reid Spencer5f016e22007-07-11 17:01:13 +0000508
509 // If this token is followed by a token paste (##) operator, paste the tokens!
Chris Lattner8d896432008-03-09 02:07:49 +0000510 if (!isAtEnd() && Tokens[CurToken].is(tok::hashhash))
Chris Lattner3f1cc832008-02-07 06:03:59 +0000511 if (PasteTokens(Tok)) {
512 // When handling the microsoft /##/ extension, the final token is
513 // returned by PasteTokens, not the pasted token.
514 return;
515 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000516
517 // The token's current location indicate where the token was lexed from. We
518 // need this information to compute the spelling of the token, but any
519 // diagnostics for the expanded token should appear as if they came from
520 // InstantiationLoc. Pull this information together into a new SourceLocation
521 // that captures all of this.
522 if (InstantiateLoc.isValid()) { // Don't do this for token streams.
523 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000524 Tok.setLocation(SrcMgr.getInstantiationLoc(Tok.getLocation(),
525 InstantiateLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 }
527
528 // If this is the first token, set the lexical properties of the token to
529 // match the lexical properties of the macro identifier.
530 if (isFirstToken) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000531 Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
532 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 }
534
535 // Handle recursive expansion!
536 if (Tok.getIdentifierInfo())
537 return PP.HandleIdentifier(Tok);
538
539 // Otherwise, return a normal token.
540}
541
542/// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
543/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
544/// are is another ## after it, chomp it iteratively. Return the result as Tok.
Chris Lattner3f1cc832008-02-07 06:03:59 +0000545/// If this returns true, the caller should immediately return the token.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000546bool TokenLexer::PasteTokens(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 llvm::SmallVector<char, 128> Buffer;
548 do {
549 // Consume the ## operator.
Chris Lattner8d896432008-03-09 02:07:49 +0000550 SourceLocation PasteOpLoc = Tokens[CurToken].getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 ++CurToken;
552 assert(!isAtEnd() && "No token on the RHS of a paste operator!");
553
554 // Get the RHS token.
Chris Lattner8d896432008-03-09 02:07:49 +0000555 const Token &RHS = Tokens[CurToken];
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
557 bool isInvalid = false;
558
559 // Allocate space for the result token. This is guaranteed to be enough for
560 // the two tokens and a null terminator.
561 Buffer.resize(Tok.getLength() + RHS.getLength() + 1);
562
563 // Get the spelling of the LHS token in Buffer.
564 const char *BufPtr = &Buffer[0];
565 unsigned LHSLen = PP.getSpelling(Tok, BufPtr);
566 if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer!
567 memcpy(&Buffer[0], BufPtr, LHSLen);
568
569 BufPtr = &Buffer[LHSLen];
570 unsigned RHSLen = PP.getSpelling(RHS, BufPtr);
571 if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer!
572 memcpy(&Buffer[LHSLen], BufPtr, RHSLen);
573
574 // Add null terminator.
575 Buffer[LHSLen+RHSLen] = '\0';
576
577 // Trim excess space.
578 Buffer.resize(LHSLen+RHSLen+1);
579
580 // Plop the pasted result (including the trailing newline and null) into a
581 // scratch buffer where we can lex it.
582 SourceLocation ResultTokLoc = PP.CreateString(&Buffer[0], Buffer.size());
583
584 // Lex the resultant pasted token into Result.
Chris Lattnerd2177732007-07-20 16:59:19 +0000585 Token Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586
587 // Avoid testing /*, as the lexer would think it is the start of a comment
588 // and emit an error that it is unterminated.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000589 if (Tok.is(tok::slash) && RHS.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 isInvalid = true;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000591 } else if (Tok.is(tok::identifier) && RHS.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 // Common paste case: identifier+identifier = identifier. Avoid creating
593 // a lexer and other overhead.
594 PP.IncrementPasteCounter(true);
595 Result.startToken();
596 Result.setKind(tok::identifier);
597 Result.setLocation(ResultTokLoc);
598 Result.setLength(LHSLen+RHSLen);
599 } else {
600 PP.IncrementPasteCounter(false);
601
602 // Make a lexer to lex this string from.
603 SourceManager &SourceMgr = PP.getSourceManager();
604 const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 // Make a lexer object so that we lex and expand the paste result.
Chris Lattner25bdb512007-07-20 16:52:03 +0000607 Lexer *TL = new Lexer(ResultTokLoc, PP, ResultStrData,
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 ResultStrData+LHSLen+RHSLen /*don't include null*/);
609
610 // Lex a token in raw mode. This way it won't look up identifiers
611 // automatically, lexing off the end will return an eof token, and
612 // warnings are disabled. This returns true if the result token is the
613 // entire buffer.
614 bool IsComplete = TL->LexRawToken(Result);
615
616 // If we got an EOF token, we didn't form even ONE token. For example, we
617 // did "/ ## /" to get "//".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000618 IsComplete &= Result.isNot(tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 isInvalid = !IsComplete;
620
621 // We're now done with the temporary lexer.
622 delete TL;
623 }
624
625 // If pasting the two tokens didn't form a full new token, this is an error.
626 // This occurs with "x ## +" and other stuff. Return with Tok unmodified
627 // and with RHS as the next token to lex.
628 if (isInvalid) {
Chris Lattner3f1cc832008-02-07 06:03:59 +0000629 // Test for the Microsoft extension of /##/ turning into // here on the
630 // error path.
631 if (PP.getLangOptions().Microsoft && Tok.is(tok::slash) &&
632 RHS.is(tok::slash)) {
633 HandleMicrosoftCommentPaste(Tok);
634 return true;
635 } else {
636 // TODO: If not in assembler language mode.
637 PP.Diag(PasteOpLoc, diag::err_pp_bad_paste,
638 std::string(Buffer.begin(), Buffer.end()-1));
639 return false;
640 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 }
642
Chris Lattner1543e9c2008-03-09 02:18:51 +0000643 // Turn ## into 'unknown' to avoid # ## # from looking like a paste
644 // operator.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000645 if (Result.is(tok::hashhash))
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 Result.setKind(tok::unknown);
Chris Lattner71a3a8d2008-01-29 07:54:23 +0000647 // FIXME: Turn __VA_ARGS__ into "not a token"?
Reid Spencer5f016e22007-07-11 17:01:13 +0000648
649 // Transfer properties of the LHS over the the Result.
Chris Lattnerd2177732007-07-20 16:59:19 +0000650 Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine());
651 Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000652
653 // Finally, replace LHS with the result, consume the RHS, and iterate.
654 ++CurToken;
655 Tok = Result;
Chris Lattner8d896432008-03-09 02:07:49 +0000656 } while (!isAtEnd() && Tokens[CurToken].is(tok::hashhash));
Reid Spencer5f016e22007-07-11 17:01:13 +0000657
658 // Now that we got the result token, it will be subject to expansion. Since
659 // token pasting re-lexes the result token in raw mode, identifier information
660 // isn't looked up. As such, if the result is an identifier, look up id info.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000661 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // Look up the identifier info for the token. We disabled identifier lookup
663 // by saying we're skipping contents, so we need to do this manually.
664 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
665 }
Chris Lattner3f1cc832008-02-07 06:03:59 +0000666 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000667}
668
669/// isNextTokenLParen - If the next token lexed will pop this macro off the
670/// expansion stack, return 2. If the next unexpanded token is a '(', return
671/// 1, otherwise return 0.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000672unsigned TokenLexer::isNextTokenLParen() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 // Out of tokens?
674 if (isAtEnd())
675 return 2;
Chris Lattner8d896432008-03-09 02:07:49 +0000676 return Tokens[CurToken].is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000677}
Chris Lattner3f1cc832008-02-07 06:03:59 +0000678
679
680/// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
681/// together to form a comment that comments out everything in the current
682/// macro, other active macros, and anything left on the current physical
683/// source line of the instantiated buffer. Handle this by returning the
684/// first token on the next line.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000685void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) {
Chris Lattner3f1cc832008-02-07 06:03:59 +0000686 // We 'comment out' the rest of this macro by just ignoring the rest of the
687 // tokens that have not been lexed yet, if any.
688
689 // Since this must be a macro, mark the macro enabled now that it is no longer
690 // being expanded.
691 assert(Macro && "Token streams can't paste comments");
692 Macro->EnableMacro();
693
694 PP.HandleMicrosoftCommentPaste(Tok);
695}