blob: 4e62c947e37e47c8cbdc0808b40ba7b350880872 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- MacroExpander.cpp - Lex from a macro expansion -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MacroExpander interface.
11//
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,
28 const Token *UnexpArgTokens,
29 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) +
35 NumToks*sizeof(Token));
36 // 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)
41 memcpy(const_cast<Token*>(Result->getUnexpArgument(0)),
42 UnexpArgTokens, NumToks*sizeof(Token));
43
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.
60unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
61 unsigned NumArgTokens = 0;
Chris Lattnercb8e41c2007-10-09 18:02:16 +000062 for (; ArgPtr->isNot(tok::eof); ++ArgPtr)
Chris Lattner4b009652007-07-25 00:24:17 +000063 ++NumArgTokens;
64 return NumArgTokens;
65}
66
67
68/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
69///
70const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
71 // The unexpanded argument tokens start immediately after the MacroArgs object
72 // in memory.
73 const Token *Start = (const Token *)(this+1);
74 const Token *Result = Start;
75 // Scan to find Arg.
76 for (; Arg; ++Result) {
77 assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
Chris Lattnercb8e41c2007-10-09 18:02:16 +000078 if (Result->is(tok::eof))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner7a1b0882007-10-07 08:44:20 +000087bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
88 Preprocessor &PP) const {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnercb8e41c2007-10-09 18:02:16 +000091 for (; ArgTok->isNot(tok::eof); ++ArgTok)
Chris Lattner4b009652007-07-25 00:24:17 +000092 if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
Chris Lattner7a1b0882007-10-07 08:44:20 +000093 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled())
Chris Lattner4b009652007-07-25 00:24:17 +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.
103const std::vector<Token> &
104MacroArgs::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
111 std::vector<Token> &Result = PreExpArgTokens[Arg];
112 if (!Result.empty()) return Result;
113
114 const Token *AT = getUnexpArgument(Arg);
115 unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
116
117 // Otherwise, we have to pre-expand this argument, populating Result. To do
118 // this, we set up a fake MacroExpander to lex from the unexpanded argument
119 // 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 {
125 Result.push_back(Token());
126 PP.Lex(Result.back());
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000127 } while (Result.back().isNot(tok::eof));
Chris Lattner4b009652007-07-25 00:24:17 +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///
143static Token StringifyArgument(const Token *ArgToks,
144 Preprocessor &PP, bool Charify = false) {
145 Token Tok;
146 Tok.startToken();
147 Tok.setKind(tok::string_literal);
148
149 const Token *ArgTokStart = ArgToks;
150
151 // Stringify all the tokens.
152 std::string Result = "\"";
153 // FIXME: Optimize this loop to not use std::strings.
154 bool isFirst = true;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000155 for (; ArgToks->isNot(tok::eof); ++ArgToks) {
Chris Lattner4b009652007-07-25 00:24:17 +0000156 const Token &Tok = *ArgToks;
157 if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()))
158 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 Lattnercb8e41c2007-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'.
Chris Lattner4b009652007-07-25 00:24:17 +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.
218const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo,
219 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 Lattnercb8e41c2007-10-09 18:02:16 +0000226 if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
Chris Lattner4b009652007-07-25 00:24:17 +0000227 StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
228 return StringifiedArgs[ArgNo];
229}
230
231//===----------------------------------------------------------------------===//
232// MacroExpander Implementation
233//===----------------------------------------------------------------------===//
234
235/// Create a macro expander for the specified macro with the specified actual
236/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
237void MacroExpander::Init(Token &Tok, MacroArgs *Actuals) {
238 // If the client is reusing a macro expander, make sure to free any memory
239 // associated with it.
240 destroy();
241
Chris Lattner7a1b0882007-10-07 08:44:20 +0000242 Macro = PP.getMacroInfo(Tok.getIdentifierInfo());
Chris Lattner4b009652007-07-25 00:24:17 +0000243 ActualArgs = Actuals;
244 CurToken = 0;
245 InstantiateLoc = Tok.getLocation();
246 AtStartOfLine = Tok.isAtStartOfLine();
247 HasLeadingSpace = Tok.hasLeadingSpace();
248 MacroTokens = &*Macro->tokens_begin();
249 OwnsMacroTokens = false;
250 NumMacroTokens = Macro->tokens_end()-Macro->tokens_begin();
251
252 // If this is a function-like macro, expand the arguments and change
253 // MacroTokens to point to the expanded tokens.
254 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
263
264
265/// Create a macro expander for the specified token stream. This does not
266/// take ownership of the specified token vector.
267void MacroExpander::Init(const Token *TokArray, unsigned NumToks) {
268 // If the client is reusing a macro expander, make sure to free any memory
269 // associated with it.
270 destroy();
271
272 Macro = 0;
273 ActualArgs = 0;
274 MacroTokens = TokArray;
275 OwnsMacroTokens = false;
276 NumMacroTokens = NumToks;
277 CurToken = 0;
278 InstantiateLoc = SourceLocation();
279 AtStartOfLine = false;
280 HasLeadingSpace = false;
281
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
291void MacroExpander::destroy() {
292 // If this was a function-like macro that actually uses its arguments, delete
293 // the expanded tokens.
294 if (OwnsMacroTokens) {
295 delete [] MacroTokens;
296 MacroTokens = 0;
297 }
298
299 // MacroExpander owns its formal arguments.
300 if (ActualArgs) ActualArgs->destroy();
301}
302
303/// Expand the arguments of a function-like macro so that we can quickly
304/// return preexpanded tokens from MacroTokens.
305void MacroExpander::ExpandFunctionArguments() {
306 llvm::SmallVector<Token, 128> ResultToks;
307
308 // Loop through the MacroTokens tokens, expanding them into ResultToks. Keep
309 // track of whether we change anything. If not, no need to keep them. If so,
310 // we install the newly expanded sequence as MacroTokens.
311 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
318 for (unsigned i = 0, e = NumMacroTokens; i != e; ++i) {
319 // 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.
322 const Token &CurTok = MacroTokens[i];
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000323 if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000324 int ArgNo = Macro->getArgumentNum(MacroTokens[i+1].getIdentifierInfo());
325 assert(ArgNo != -1 && "Token following # is not an argument?");
326
327 Token Res;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000328 if (CurTok.is(tok::hash)) // Stringify
Chris Lattner4b009652007-07-25 00:24:17 +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)
338 Res.setFlag(Token::LeadingSpace);
339
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) {
356 ResultToks.back().setFlag(Token::LeadingSpace);
357 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 Lattnercb8e41c2007-10-09 18:02:16 +0000369 !ResultToks.empty() && ResultToks.back().is(tok::hashhash);
370 bool PasteAfter = i+1 != e && MacroTokens[i+1].is(tok::hashhash);
Chris Lattner4b009652007-07-25 00:24:17 +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) {
376 const Token *ResultArgToks;
377
378 // Only preexpand the argument if it could possibly need it. This
379 // avoids some work in common cases.
380 const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
Chris Lattner7a1b0882007-10-07 08:44:20 +0000381 if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnercb8e41c2007-10-09 18:02:16 +0000387 if (ResultArgToks->isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +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.
395 ResultToks[FirstResult].setFlagValue(Token::LeadingSpace,
396 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.
409 const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
410 unsigned NumToks = MacroArgs::getArgLength(ArgToks);
411 if (NumToks) { // Not an empty argument?
Chris Lattnerfb409542008-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
Chris Lattner4b009652007-07-25 00:24:17 +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) {
430 ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace);
431 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.
444 NextTokGetsSpace |= MacroTokens[i+1].hasLeadingSpace();
445 ++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 Lattnercb8e41c2007-10-09 18:02:16 +0000451 assert(PasteBefore && ResultToks.back().is(tok::hashhash));
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnercb8e41c2007-10-09 18:02:16 +0000460 !ResultToks.empty() && ResultToks.back().is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000461 // Never add a space, even if the comma, ##, or arg had a space.
462 NextTokGetsSpace = false;
Chris Lattnerfb409542008-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);
Chris Lattner4b009652007-07-25 00:24:17 +0000465 ResultToks.pop_back();
466 }
467 continue;
468 }
469
470 // If anything changed, install this as the new MacroTokens list.
471 if (MadeChange) {
472 // This is deleted in the dtor.
473 NumMacroTokens = ResultToks.size();
474 Token *Res = new Token[ResultToks.size()];
475 if (NumMacroTokens)
476 memcpy(Res, &ResultToks[0], NumMacroTokens*sizeof(Token));
477 MacroTokens = Res;
478 OwnsMacroTokens = true;
479 }
480}
481
482/// Lex - Lex and return a token from this macro stream.
483///
484void MacroExpander::Lex(Token &Tok) {
485 // 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.
507 Tok = MacroTokens[CurToken++];
508
509 // If this token is followed by a token paste (##) operator, paste the tokens!
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000510 if (!isAtEnd() && MacroTokens[CurToken].is(tok::hashhash))
Chris Lattner64b32ec2008-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 }
Chris Lattner4b009652007-07-25 00:24:17 +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();
524 Tok.setLocation(SrcMgr.getInstantiationLoc(Tok.getLocation(),
525 InstantiateLoc));
526 }
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) {
531 Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
532 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
533 }
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 Lattner64b32ec2008-02-07 06:03:59 +0000545/// If this returns true, the caller should immediately return the token.
546bool MacroExpander::PasteTokens(Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000547 llvm::SmallVector<char, 128> Buffer;
548 do {
549 // Consume the ## operator.
550 SourceLocation PasteOpLoc = MacroTokens[CurToken].getLocation();
551 ++CurToken;
552 assert(!isAtEnd() && "No token on the RHS of a paste operator!");
553
554 // Get the RHS token.
555 const Token &RHS = MacroTokens[CurToken];
556
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.
585 Token Result;
586
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 Lattnercb8e41c2007-10-09 18:02:16 +0000589 if (Tok.is(tok::slash) && RHS.is(tok::star)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000590 isInvalid = true;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000591 } else if (Tok.is(tok::identifier) && RHS.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +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
606 // Make a lexer object so that we lex and expand the paste result.
607 Lexer *TL = new Lexer(ResultTokLoc, PP, ResultStrData,
608 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 Lattnercb8e41c2007-10-09 18:02:16 +0000618 IsComplete &= Result.isNot(tok::eof);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner64b32ec2008-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 }
Chris Lattner4b009652007-07-25 00:24:17 +0000641 }
642
643 // Turn ## into 'other' to avoid # ## # from looking like a paste operator.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000644 if (Result.is(tok::hashhash))
Chris Lattner4b009652007-07-25 00:24:17 +0000645 Result.setKind(tok::unknown);
Chris Lattnerfb409542008-01-29 07:54:23 +0000646 // FIXME: Turn __VA_ARGS__ into "not a token"?
Chris Lattner4b009652007-07-25 00:24:17 +0000647
648 // Transfer properties of the LHS over the the Result.
649 Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine());
650 Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace());
651
652 // Finally, replace LHS with the result, consume the RHS, and iterate.
653 ++CurToken;
654 Tok = Result;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000655 } while (!isAtEnd() && MacroTokens[CurToken].is(tok::hashhash));
Chris Lattner4b009652007-07-25 00:24:17 +0000656
657 // Now that we got the result token, it will be subject to expansion. Since
658 // token pasting re-lexes the result token in raw mode, identifier information
659 // isn't looked up. As such, if the result is an identifier, look up id info.
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000660 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000661 // Look up the identifier info for the token. We disabled identifier lookup
662 // by saying we're skipping contents, so we need to do this manually.
663 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
664 }
Chris Lattner64b32ec2008-02-07 06:03:59 +0000665 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000666}
667
668/// isNextTokenLParen - If the next token lexed will pop this macro off the
669/// expansion stack, return 2. If the next unexpanded token is a '(', return
670/// 1, otherwise return 0.
671unsigned MacroExpander::isNextTokenLParen() const {
672 // Out of tokens?
673 if (isAtEnd())
674 return 2;
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000675 return MacroTokens[CurToken].is(tok::l_paren);
Chris Lattner4b009652007-07-25 00:24:17 +0000676}
Chris Lattner64b32ec2008-02-07 06:03:59 +0000677
678
679/// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
680/// together to form a comment that comments out everything in the current
681/// macro, other active macros, and anything left on the current physical
682/// source line of the instantiated buffer. Handle this by returning the
683/// first token on the next line.
684void MacroExpander::HandleMicrosoftCommentPaste(Token &Tok) {
685 // We 'comment out' the rest of this macro by just ignoring the rest of the
686 // tokens that have not been lexed yet, if any.
687
688 // Since this must be a macro, mark the macro enabled now that it is no longer
689 // being expanded.
690 assert(Macro && "Token streams can't paste comments");
691 Macro->EnableMacro();
692
693 PP.HandleMicrosoftCommentPaste(Tok);
694}