blob: 45d4611233d461973afccbc506a9f2a8e48ca486 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- MacroExpander.cpp - Lex from a macro expansion -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 LexerToken *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(LexerToken));
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<LexerToken*>(Result->getUnexpArgument(0)),
42 UnexpArgTokens, NumToks*sizeof(LexerToken));
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 LexerToken *ArgPtr) {
61 unsigned NumArgTokens = 0;
62 for (; ArgPtr->getKind() != tok::eof; ++ArgPtr)
63 ++NumArgTokens;
64 return NumArgTokens;
65}
66
67
68/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
69///
70const LexerToken *MacroArgs::getUnexpArgument(unsigned Arg) const {
71 // The unexpanded argument tokens start immediately after the MacroArgs object
72 // in memory.
73 const LexerToken *Start = (const LexerToken *)(this+1);
74 const LexerToken *Result = Start;
75 // Scan to find Arg.
76 for (; Arg; ++Result) {
77 assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
78 if (Result->getKind() == tok::eof)
79 --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.
87bool MacroArgs::ArgNeedsPreexpansion(const LexerToken *ArgTok) const {
88 // If there are no identifiers in the argument list, or if the identifiers are
89 // known to not be macros, pre-expansion won't modify it.
90 for (; ArgTok->getKind() != tok::eof; ++ArgTok)
91 if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
92 if (II->getMacroInfo() && II->getMacroInfo()->isEnabled())
93 // Return true even though the macro could be a function-like macro
94 // without a following '(' token.
95 return true;
96 }
97 return false;
98}
99
100/// getPreExpArgument - Return the pre-expanded form of the specified
101/// argument.
102const std::vector<LexerToken> &
103MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
104 assert(Arg < NumUnexpArgTokens && "Invalid argument number!");
105
106 // If we have already computed this, return it.
107 if (PreExpArgTokens.empty())
108 PreExpArgTokens.resize(NumUnexpArgTokens);
109
110 std::vector<LexerToken> &Result = PreExpArgTokens[Arg];
111 if (!Result.empty()) return Result;
112
113 const LexerToken *AT = getUnexpArgument(Arg);
114 unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
115
116 // Otherwise, we have to pre-expand this argument, populating Result. To do
117 // this, we set up a fake MacroExpander to lex from the unexpanded argument
118 // list. With this installed, we lex expanded tokens until we hit the EOF
119 // token at the end of the unexp list.
120 PP.EnterTokenStream(AT, NumToks);
121
122 // Lex all of the macro-expanded tokens into Result.
123 do {
124 Result.push_back(LexerToken());
125 PP.Lex(Result.back());
126 } while (Result.back().getKind() != tok::eof);
127
128 // Pop the token stream off the top of the stack. We know that the internal
129 // pointer inside of it is to the "end" of the token stream, but the stack
130 // will not otherwise be popped until the next token is lexed. The problem is
131 // that the token may be lexed sometime after the vector of tokens itself is
132 // destroyed, which would be badness.
133 PP.RemoveTopOfLexerStack();
134 return Result;
135}
136
137
138/// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
139/// tokens into the literal string token that should be produced by the C #
140/// preprocessor operator.
141///
142static LexerToken StringifyArgument(const LexerToken *ArgToks,
143 Preprocessor &PP, bool Charify = false) {
144 LexerToken Tok;
145 Tok.startToken();
146 Tok.setKind(tok::string_literal);
147
148 const LexerToken *ArgTokStart = ArgToks;
149
150 // Stringify all the tokens.
151 std::string Result = "\"";
152 // FIXME: Optimize this loop to not use std::strings.
153 bool isFirst = true;
154 for (; ArgToks->getKind() != tok::eof; ++ArgToks) {
155 const LexerToken &Tok = *ArgToks;
156 if (!isFirst && Tok.hasLeadingSpace())
157 Result += ' ';
158 isFirst = false;
159
160 // If this is a string or character constant, escape the token as specified
161 // by 6.10.3.2p2.
162 if (Tok.getKind() == tok::string_literal || // "foo"
163 Tok.getKind() == tok::wide_string_literal || // L"foo"
164 Tok.getKind() == tok::char_constant) { // 'x' and L'x'.
165 Result += Lexer::Stringify(PP.getSpelling(Tok));
166 } else {
167 // Otherwise, just append the token.
168 Result += PP.getSpelling(Tok);
169 }
170 }
171
172 // If the last character of the string is a \, and if it isn't escaped, this
173 // is an invalid string literal, diagnose it as specified in C99.
174 if (Result[Result.size()-1] == '\\') {
175 // Count the number of consequtive \ characters. If even, then they are
176 // just escaped backslashes, otherwise it's an error.
177 unsigned FirstNonSlash = Result.size()-2;
178 // Guaranteed to find the starting " if nothing else.
179 while (Result[FirstNonSlash] == '\\')
180 --FirstNonSlash;
181 if ((Result.size()-1-FirstNonSlash) & 1) {
182 // Diagnose errors for things like: #define F(X) #X / F(\)
183 PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
184 Result.erase(Result.end()-1); // remove one of the \'s.
185 }
186 }
187 Result += '"';
188
189 // If this is the charify operation and the result is not a legal character
190 // constant, diagnose it.
191 if (Charify) {
192 // First step, turn double quotes into single quotes:
193 Result[0] = '\'';
194 Result[Result.size()-1] = '\'';
195
196 // Check for bogus character.
197 bool isBad = false;
198 if (Result.size() == 3) {
199 isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
200 } else {
201 isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
202 }
203
204 if (isBad) {
205 PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
206 Result = "' '"; // Use something arbitrary, but legal.
207 }
208 }
209
210 Tok.setLength(Result.size());
211 Tok.setLocation(PP.CreateString(&Result[0], Result.size()));
212 return Tok;
213}
214
215/// getStringifiedArgument - Compute, cache, and return the specified argument
216/// that has been 'stringified' as required by the # operator.
217const LexerToken &MacroArgs::getStringifiedArgument(unsigned ArgNo,
218 Preprocessor &PP) {
219 assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
220 if (StringifiedArgs.empty()) {
221 StringifiedArgs.resize(getNumArguments());
222 memset(&StringifiedArgs[0], 0,
223 sizeof(StringifiedArgs[0])*getNumArguments());
224 }
225 if (StringifiedArgs[ArgNo].getKind() != tok::string_literal)
226 StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
227 return StringifiedArgs[ArgNo];
228}
229
230//===----------------------------------------------------------------------===//
231// MacroExpander Implementation
232//===----------------------------------------------------------------------===//
233
234/// Create a macro expander for the specified macro with the specified actual
235/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
236MacroExpander::MacroExpander(LexerToken &Tok, MacroArgs *Actuals,
237 Preprocessor &pp)
238 : Macro(Tok.getIdentifierInfo()->getMacroInfo()),
239 ActualArgs(Actuals), PP(pp), CurToken(0),
240 InstantiateLoc(Tok.getLocation()),
241 AtStartOfLine(Tok.isAtStartOfLine()),
242 HasLeadingSpace(Tok.hasLeadingSpace()) {
Chris Lattnerc215bd62007-07-14 22:11:41 +0000243 MacroTokens = &*Macro->tokens_begin();
244 NumMacroTokens = Macro->tokens_end()-Macro->tokens_begin();
Reid Spencer5f016e22007-07-11 17:01:13 +0000245
246 // If this is a function-like macro, expand the arguments and change
247 // MacroTokens to point to the expanded tokens.
248 if (Macro->isFunctionLike() && Macro->getNumArgs())
249 ExpandFunctionArguments();
250
251 // Mark the macro as currently disabled, so that it is not recursively
252 // expanded. The macro must be disabled only after argument pre-expansion of
253 // function-like macro arguments occurs.
254 Macro->DisableMacro();
255}
256
257/// Create a macro expander for the specified token stream. This does not
258/// take ownership of the specified token vector.
259MacroExpander::MacroExpander(const LexerToken *TokArray, unsigned NumToks,
260 Preprocessor &pp)
261 : Macro(0), ActualArgs(0), PP(pp), MacroTokens(TokArray),
262 NumMacroTokens(NumToks), CurToken(0),
263 InstantiateLoc(SourceLocation()), AtStartOfLine(false),
264 HasLeadingSpace(false) {
265
266 // Set HasLeadingSpace/AtStartOfLine so that the first token will be
267 // returned unmodified.
268 if (NumToks != 0) {
269 AtStartOfLine = TokArray[0].isAtStartOfLine();
270 HasLeadingSpace = TokArray[0].hasLeadingSpace();
271 }
272}
273
274
275MacroExpander::~MacroExpander() {
276 // If this was a function-like macro that actually uses its arguments, delete
277 // the expanded tokens.
Chris Lattnerc215bd62007-07-14 22:11:41 +0000278 if (Macro && MacroTokens != &*Macro->tokens_begin())
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 delete [] MacroTokens;
280
281 // MacroExpander owns its formal arguments.
282 if (ActualArgs) ActualArgs->destroy();
283}
284
285/// Expand the arguments of a function-like macro so that we can quickly
286/// return preexpanded tokens from MacroTokens.
287void MacroExpander::ExpandFunctionArguments() {
288 llvm::SmallVector<LexerToken, 128> ResultToks;
289
290 // Loop through the MacroTokens tokens, expanding them into ResultToks. Keep
291 // track of whether we change anything. If not, no need to keep them. If so,
292 // we install the newly expanded sequence as MacroTokens.
293 bool MadeChange = false;
294
295 // NextTokGetsSpace - When this is true, the next token appended to the
296 // output list will get a leading space, regardless of whether it had one to
297 // begin with or not. This is used for placemarker support.
298 bool NextTokGetsSpace = false;
299
300 for (unsigned i = 0, e = NumMacroTokens; i != e; ++i) {
301 // If we found the stringify operator, get the argument stringified. The
302 // preprocessor already verified that the following token is a macro name
303 // when the #define was parsed.
304 const LexerToken &CurTok = MacroTokens[i];
305 if (CurTok.getKind() == tok::hash || CurTok.getKind() == tok::hashat) {
306 int ArgNo = Macro->getArgumentNum(MacroTokens[i+1].getIdentifierInfo());
307 assert(ArgNo != -1 && "Token following # is not an argument?");
308
309 LexerToken Res;
310 if (CurTok.getKind() == tok::hash) // Stringify
311 Res = ActualArgs->getStringifiedArgument(ArgNo, PP);
312 else {
313 // 'charify': don't bother caching these.
314 Res = StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), PP, true);
315 }
316
317 // The stringified/charified string leading space flag gets set to match
318 // the #/#@ operator.
319 if (CurTok.hasLeadingSpace() || NextTokGetsSpace)
320 Res.setFlag(LexerToken::LeadingSpace);
321
322 ResultToks.push_back(Res);
323 MadeChange = true;
324 ++i; // Skip arg name.
325 NextTokGetsSpace = false;
326 continue;
327 }
328
329 // Otherwise, if this is not an argument token, just add the token to the
330 // output buffer.
331 IdentifierInfo *II = CurTok.getIdentifierInfo();
332 int ArgNo = II ? Macro->getArgumentNum(II) : -1;
333 if (ArgNo == -1) {
334 // This isn't an argument, just add it.
335 ResultToks.push_back(CurTok);
336
337 if (NextTokGetsSpace) {
338 ResultToks.back().setFlag(LexerToken::LeadingSpace);
339 NextTokGetsSpace = false;
340 }
341 continue;
342 }
343
344 // An argument is expanded somehow, the result is different than the
345 // input.
346 MadeChange = true;
347
348 // Otherwise, this is a use of the argument. Find out if there is a paste
349 // (##) operator before or after the argument.
350 bool PasteBefore =
351 !ResultToks.empty() && ResultToks.back().getKind() == tok::hashhash;
352 bool PasteAfter = i+1 != e && MacroTokens[i+1].getKind() == tok::hashhash;
353
354 // If it is not the LHS/RHS of a ## operator, we must pre-expand the
355 // argument and substitute the expanded tokens into the result. This is
356 // C99 6.10.3.1p1.
357 if (!PasteBefore && !PasteAfter) {
358 const LexerToken *ResultArgToks;
359
360 // Only preexpand the argument if it could possibly need it. This
361 // avoids some work in common cases.
362 const LexerToken *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
363 if (ActualArgs->ArgNeedsPreexpansion(ArgTok))
364 ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
365 else
366 ResultArgToks = ArgTok; // Use non-preexpanded tokens.
367
368 // If the arg token expanded into anything, append it.
369 if (ResultArgToks->getKind() != tok::eof) {
370 unsigned FirstResult = ResultToks.size();
371 unsigned NumToks = MacroArgs::getArgLength(ResultArgToks);
372 ResultToks.append(ResultArgToks, ResultArgToks+NumToks);
373
374 // If any tokens were substituted from the argument, the whitespace
375 // before the first token should match the whitespace of the arg
376 // identifier.
377 ResultToks[FirstResult].setFlagValue(LexerToken::LeadingSpace,
378 CurTok.hasLeadingSpace() ||
379 NextTokGetsSpace);
380 NextTokGetsSpace = false;
381 } else {
382 // If this is an empty argument, and if there was whitespace before the
383 // formal token, make sure the next token gets whitespace before it.
384 NextTokGetsSpace = CurTok.hasLeadingSpace();
385 }
386 continue;
387 }
388
389 // Okay, we have a token that is either the LHS or RHS of a paste (##)
390 // argument. It gets substituted as its non-pre-expanded tokens.
391 const LexerToken *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
392 unsigned NumToks = MacroArgs::getArgLength(ArgToks);
393 if (NumToks) { // Not an empty argument?
394 ResultToks.append(ArgToks, ArgToks+NumToks);
395
396 // If the next token was supposed to get leading whitespace, ensure it has
397 // it now.
398 if (NextTokGetsSpace) {
399 ResultToks[ResultToks.size()-NumToks].setFlag(LexerToken::LeadingSpace);
400 NextTokGetsSpace = false;
401 }
402 continue;
403 }
404
405 // If an empty argument is on the LHS or RHS of a paste, the standard (C99
406 // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We
407 // implement this by eating ## operators when a LHS or RHS expands to
408 // empty.
409 NextTokGetsSpace |= CurTok.hasLeadingSpace();
410 if (PasteAfter) {
411 // Discard the argument token and skip (don't copy to the expansion
412 // buffer) the paste operator after it.
413 NextTokGetsSpace |= MacroTokens[i+1].hasLeadingSpace();
414 ++i;
415 continue;
416 }
417
418 // If this is on the RHS of a paste operator, we've already copied the
419 // paste operator to the ResultToks list. Remove it.
420 assert(PasteBefore && ResultToks.back().getKind() == tok::hashhash);
421 NextTokGetsSpace |= ResultToks.back().hasLeadingSpace();
422 ResultToks.pop_back();
423
424 // If this is the __VA_ARGS__ token, and if the argument wasn't provided,
425 // and if the macro had at least one real argument, and if the token before
426 // the ## was a comma, remove the comma.
427 if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__
428 ActualArgs->isVarargsElidedUse() && // Argument elided.
429 !ResultToks.empty() && ResultToks.back().getKind() == tok::comma) {
430 // Never add a space, even if the comma, ##, or arg had a space.
431 NextTokGetsSpace = false;
432 ResultToks.pop_back();
433 }
434 continue;
435 }
436
437 // If anything changed, install this as the new MacroTokens list.
438 if (MadeChange) {
439 // This is deleted in the dtor.
440 NumMacroTokens = ResultToks.size();
441 LexerToken *Res = new LexerToken[ResultToks.size()];
442 if (NumMacroTokens)
443 memcpy(Res, &ResultToks[0], NumMacroTokens*sizeof(LexerToken));
444 MacroTokens = Res;
445 }
446}
447
448/// Lex - Lex and return a token from this macro stream.
449///
450void MacroExpander::Lex(LexerToken &Tok) {
451 // Lexing off the end of the macro, pop this macro off the expansion stack.
452 if (isAtEnd()) {
453 // If this is a macro (not a token stream), mark the macro enabled now
454 // that it is no longer being expanded.
455 if (Macro) Macro->EnableMacro();
456
457 // Pop this context off the preprocessors lexer stack and get the next
458 // token. This will delete "this" so remember the PP instance var.
459 Preprocessor &PPCache = PP;
460 if (PP.HandleEndOfMacro(Tok))
461 return;
462
463 // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is
464 // next.
465 return PPCache.Lex(Tok);
466 }
467
468 // If this is the first token of the expanded result, we inherit spacing
469 // properties later.
470 bool isFirstToken = CurToken == 0;
471
472 // Get the next token to return.
473 Tok = MacroTokens[CurToken++];
474
475 // If this token is followed by a token paste (##) operator, paste the tokens!
476 if (!isAtEnd() && MacroTokens[CurToken].getKind() == tok::hashhash)
477 PasteTokens(Tok);
478
479 // The token's current location indicate where the token was lexed from. We
480 // need this information to compute the spelling of the token, but any
481 // diagnostics for the expanded token should appear as if they came from
482 // InstantiationLoc. Pull this information together into a new SourceLocation
483 // that captures all of this.
484 if (InstantiateLoc.isValid()) { // Don't do this for token streams.
485 SourceManager &SrcMgr = PP.getSourceManager();
486 // The token could have come from a prior macro expansion. In that case,
487 // ignore the macro expand part to get to the physloc. This happens for
488 // stuff like: #define A(X) X A(A(X)) A(1)
489 SourceLocation PhysLoc = SrcMgr.getPhysicalLoc(Tok.getLocation());
490 Tok.setLocation(SrcMgr.getInstantiationLoc(PhysLoc, InstantiateLoc));
491 }
492
493 // If this is the first token, set the lexical properties of the token to
494 // match the lexical properties of the macro identifier.
495 if (isFirstToken) {
496 Tok.setFlagValue(LexerToken::StartOfLine , AtStartOfLine);
497 Tok.setFlagValue(LexerToken::LeadingSpace, HasLeadingSpace);
498 }
499
500 // Handle recursive expansion!
501 if (Tok.getIdentifierInfo())
502 return PP.HandleIdentifier(Tok);
503
504 // Otherwise, return a normal token.
505}
506
507/// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
508/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
509/// are is another ## after it, chomp it iteratively. Return the result as Tok.
510void MacroExpander::PasteTokens(LexerToken &Tok) {
511 llvm::SmallVector<char, 128> Buffer;
512 do {
513 // Consume the ## operator.
514 SourceLocation PasteOpLoc = MacroTokens[CurToken].getLocation();
515 ++CurToken;
516 assert(!isAtEnd() && "No token on the RHS of a paste operator!");
517
518 // Get the RHS token.
519 const LexerToken &RHS = MacroTokens[CurToken];
520
521 bool isInvalid = false;
522
523 // Allocate space for the result token. This is guaranteed to be enough for
524 // the two tokens and a null terminator.
525 Buffer.resize(Tok.getLength() + RHS.getLength() + 1);
526
527 // Get the spelling of the LHS token in Buffer.
528 const char *BufPtr = &Buffer[0];
529 unsigned LHSLen = PP.getSpelling(Tok, BufPtr);
530 if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer!
531 memcpy(&Buffer[0], BufPtr, LHSLen);
532
533 BufPtr = &Buffer[LHSLen];
534 unsigned RHSLen = PP.getSpelling(RHS, BufPtr);
535 if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer!
536 memcpy(&Buffer[LHSLen], BufPtr, RHSLen);
537
538 // Add null terminator.
539 Buffer[LHSLen+RHSLen] = '\0';
540
541 // Trim excess space.
542 Buffer.resize(LHSLen+RHSLen+1);
543
544 // Plop the pasted result (including the trailing newline and null) into a
545 // scratch buffer where we can lex it.
546 SourceLocation ResultTokLoc = PP.CreateString(&Buffer[0], Buffer.size());
547
548 // Lex the resultant pasted token into Result.
549 LexerToken Result;
550
551 // Avoid testing /*, as the lexer would think it is the start of a comment
552 // and emit an error that it is unterminated.
553 if (Tok.getKind() == tok::slash && RHS.getKind() == tok::star) {
554 isInvalid = true;
555 } else if (Tok.getKind() == tok::identifier &&
556 RHS.getKind() == tok::identifier) {
557 // Common paste case: identifier+identifier = identifier. Avoid creating
558 // a lexer and other overhead.
559 PP.IncrementPasteCounter(true);
560 Result.startToken();
561 Result.setKind(tok::identifier);
562 Result.setLocation(ResultTokLoc);
563 Result.setLength(LHSLen+RHSLen);
564 } else {
565 PP.IncrementPasteCounter(false);
566
567 // Make a lexer to lex this string from.
568 SourceManager &SourceMgr = PP.getSourceManager();
569 const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
570
571 unsigned FileID = ResultTokLoc.getFileID();
572 assert(FileID && "Could not get FileID for paste?");
573
574 // Make a lexer object so that we lex and expand the paste result.
575 Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, PP,
576 ResultStrData,
577 ResultStrData+LHSLen+RHSLen /*don't include null*/);
578
579 // Lex a token in raw mode. This way it won't look up identifiers
580 // automatically, lexing off the end will return an eof token, and
581 // warnings are disabled. This returns true if the result token is the
582 // entire buffer.
583 bool IsComplete = TL->LexRawToken(Result);
584
585 // If we got an EOF token, we didn't form even ONE token. For example, we
586 // did "/ ## /" to get "//".
587 IsComplete &= Result.getKind() != tok::eof;
588 isInvalid = !IsComplete;
589
590 // We're now done with the temporary lexer.
591 delete TL;
592 }
593
594 // If pasting the two tokens didn't form a full new token, this is an error.
595 // This occurs with "x ## +" and other stuff. Return with Tok unmodified
596 // and with RHS as the next token to lex.
597 if (isInvalid) {
598 // If not in assembler language mode.
599 PP.Diag(PasteOpLoc, diag::err_pp_bad_paste,
600 std::string(Buffer.begin(), Buffer.end()-1));
601 return;
602 }
603
604 // Turn ## into 'other' to avoid # ## # from looking like a paste operator.
605 if (Result.getKind() == tok::hashhash)
606 Result.setKind(tok::unknown);
607 // FIXME: Turn __VARRGS__ into "not a token"?
608
609 // Transfer properties of the LHS over the the Result.
610 Result.setFlagValue(LexerToken::StartOfLine , Tok.isAtStartOfLine());
611 Result.setFlagValue(LexerToken::LeadingSpace, Tok.hasLeadingSpace());
612
613 // Finally, replace LHS with the result, consume the RHS, and iterate.
614 ++CurToken;
615 Tok = Result;
616 } while (!isAtEnd() && MacroTokens[CurToken].getKind() == tok::hashhash);
617
618 // Now that we got the result token, it will be subject to expansion. Since
619 // token pasting re-lexes the result token in raw mode, identifier information
620 // isn't looked up. As such, if the result is an identifier, look up id info.
621 if (Tok.getKind() == tok::identifier) {
622 // Look up the identifier info for the token. We disabled identifier lookup
623 // by saying we're skipping contents, so we need to do this manually.
624 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
625 }
626}
627
628/// isNextTokenLParen - If the next token lexed will pop this macro off the
629/// expansion stack, return 2. If the next unexpanded token is a '(', return
630/// 1, otherwise return 0.
631unsigned MacroExpander::isNextTokenLParen() const {
632 // Out of tokens?
633 if (isAtEnd())
634 return 2;
635 return MacroTokens[CurToken].getKind() == tok::l_paren;
636}