blob: 2a5d6c5cf52d72152bc3309628bc033d64eae813 [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
Chris Lattner5d75de02008-03-09 02:22:57 +000014#include "clang/Lex/TokenLexer.h"
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000015#include "MacroArgs.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Lex/MacroInfo.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/Diagnostic.h"
20#include "llvm/ADT/SmallVector.h"
21using namespace clang;
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023
Chris Lattner1543e9c2008-03-09 02:18:51 +000024/// Create a TokenLexer for the specified macro with the specified actual
Reid Spencer5f016e22007-07-11 17:01:13 +000025/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
Chris Lattner1543e9c2008-03-09 02:18:51 +000026void TokenLexer::Init(Token &Tok, MacroArgs *Actuals) {
27 // If the client is reusing a TokenLexer, make sure to free any memory
Chris Lattner9594acf2007-07-15 00:25:26 +000028 // associated with it.
29 destroy();
30
Chris Lattnercc1a8752007-10-07 08:44:20 +000031 Macro = PP.getMacroInfo(Tok.getIdentifierInfo());
Chris Lattner9594acf2007-07-15 00:25:26 +000032 ActualArgs = Actuals;
33 CurToken = 0;
34 InstantiateLoc = Tok.getLocation();
35 AtStartOfLine = Tok.isAtStartOfLine();
36 HasLeadingSpace = Tok.hasLeadingSpace();
Chris Lattner8d896432008-03-09 02:07:49 +000037 Tokens = &*Macro->tokens_begin();
38 OwnsTokens = false;
39 NumTokens = Macro->tokens_end()-Macro->tokens_begin();
Reid Spencer5f016e22007-07-11 17:01:13 +000040
41 // If this is a function-like macro, expand the arguments and change
Chris Lattner8d896432008-03-09 02:07:49 +000042 // Tokens to point to the expanded tokens.
Reid Spencer5f016e22007-07-11 17:01:13 +000043 if (Macro->isFunctionLike() && Macro->getNumArgs())
44 ExpandFunctionArguments();
45
46 // Mark the macro as currently disabled, so that it is not recursively
47 // expanded. The macro must be disabled only after argument pre-expansion of
48 // function-like macro arguments occurs.
49 Macro->DisableMacro();
50}
51
Chris Lattner9594acf2007-07-15 00:25:26 +000052
53
Chris Lattner1543e9c2008-03-09 02:18:51 +000054/// Create a TokenLexer for the specified token stream. This does not
Reid Spencer5f016e22007-07-11 17:01:13 +000055/// take ownership of the specified token vector.
Chris Lattner1543e9c2008-03-09 02:18:51 +000056void TokenLexer::Init(const Token *TokArray, unsigned NumToks) {
57 // If the client is reusing a TokenLexer, make sure to free any memory
Chris Lattner9594acf2007-07-15 00:25:26 +000058 // associated with it.
59 destroy();
60
61 Macro = 0;
62 ActualArgs = 0;
Chris Lattner8d896432008-03-09 02:07:49 +000063 Tokens = TokArray;
64 OwnsTokens = false;
65 NumTokens = NumToks;
Chris Lattner9594acf2007-07-15 00:25:26 +000066 CurToken = 0;
67 InstantiateLoc = SourceLocation();
68 AtStartOfLine = false;
69 HasLeadingSpace = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000070
71 // Set HasLeadingSpace/AtStartOfLine so that the first token will be
72 // returned unmodified.
73 if (NumToks != 0) {
74 AtStartOfLine = TokArray[0].isAtStartOfLine();
75 HasLeadingSpace = TokArray[0].hasLeadingSpace();
76 }
77}
78
79
Chris Lattner1543e9c2008-03-09 02:18:51 +000080void TokenLexer::destroy() {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // If this was a function-like macro that actually uses its arguments, delete
82 // the expanded tokens.
Chris Lattner8d896432008-03-09 02:07:49 +000083 if (OwnsTokens) {
84 delete [] Tokens;
85 Tokens = 0;
Chris Lattner9c683062007-07-22 01:16:55 +000086 }
Reid Spencer5f016e22007-07-11 17:01:13 +000087
Chris Lattner1543e9c2008-03-09 02:18:51 +000088 // TokenLexer owns its formal arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +000089 if (ActualArgs) ActualArgs->destroy();
90}
91
92/// Expand the arguments of a function-like macro so that we can quickly
Chris Lattner8d896432008-03-09 02:07:49 +000093/// return preexpanded tokens from Tokens.
Chris Lattner1543e9c2008-03-09 02:18:51 +000094void TokenLexer::ExpandFunctionArguments() {
Chris Lattnerd2177732007-07-20 16:59:19 +000095 llvm::SmallVector<Token, 128> ResultToks;
Reid Spencer5f016e22007-07-11 17:01:13 +000096
Chris Lattner8d896432008-03-09 02:07:49 +000097 // Loop through 'Tokens', expanding them into ResultToks. Keep
Reid Spencer5f016e22007-07-11 17:01:13 +000098 // track of whether we change anything. If not, no need to keep them. If so,
Chris Lattner8d896432008-03-09 02:07:49 +000099 // we install the newly expanded sequence as the new 'Tokens' list.
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 bool MadeChange = false;
101
102 // NextTokGetsSpace - When this is true, the next token appended to the
103 // output list will get a leading space, regardless of whether it had one to
104 // begin with or not. This is used for placemarker support.
105 bool NextTokGetsSpace = false;
106
Chris Lattner8d896432008-03-09 02:07:49 +0000107 for (unsigned i = 0, e = NumTokens; i != e; ++i) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // If we found the stringify operator, get the argument stringified. The
109 // preprocessor already verified that the following token is a macro name
110 // when the #define was parsed.
Chris Lattner8d896432008-03-09 02:07:49 +0000111 const Token &CurTok = Tokens[i];
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000112 if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) {
Chris Lattner8d896432008-03-09 02:07:49 +0000113 int ArgNo = Macro->getArgumentNum(Tokens[i+1].getIdentifierInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 assert(ArgNo != -1 && "Token following # is not an argument?");
115
Chris Lattnerd2177732007-07-20 16:59:19 +0000116 Token Res;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000117 if (CurTok.is(tok::hash)) // Stringify
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 Res = ActualArgs->getStringifiedArgument(ArgNo, PP);
119 else {
120 // 'charify': don't bother caching these.
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000121 Res = MacroArgs::StringifyArgument(ActualArgs->getUnexpArgument(ArgNo),
122 PP, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
124
125 // The stringified/charified string leading space flag gets set to match
126 // the #/#@ operator.
127 if (CurTok.hasLeadingSpace() || NextTokGetsSpace)
Chris Lattnerd2177732007-07-20 16:59:19 +0000128 Res.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
130 ResultToks.push_back(Res);
131 MadeChange = true;
132 ++i; // Skip arg name.
133 NextTokGetsSpace = false;
134 continue;
135 }
136
137 // Otherwise, if this is not an argument token, just add the token to the
138 // output buffer.
139 IdentifierInfo *II = CurTok.getIdentifierInfo();
140 int ArgNo = II ? Macro->getArgumentNum(II) : -1;
141 if (ArgNo == -1) {
142 // This isn't an argument, just add it.
143 ResultToks.push_back(CurTok);
144
145 if (NextTokGetsSpace) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000146 ResultToks.back().setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 NextTokGetsSpace = false;
148 }
149 continue;
150 }
151
152 // An argument is expanded somehow, the result is different than the
153 // input.
154 MadeChange = true;
155
156 // Otherwise, this is a use of the argument. Find out if there is a paste
157 // (##) operator before or after the argument.
158 bool PasteBefore =
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000159 !ResultToks.empty() && ResultToks.back().is(tok::hashhash);
Chris Lattner8d896432008-03-09 02:07:49 +0000160 bool PasteAfter = i+1 != e && Tokens[i+1].is(tok::hashhash);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
162 // If it is not the LHS/RHS of a ## operator, we must pre-expand the
163 // argument and substitute the expanded tokens into the result. This is
164 // C99 6.10.3.1p1.
165 if (!PasteBefore && !PasteAfter) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000166 const Token *ResultArgToks;
Reid Spencer5f016e22007-07-11 17:01:13 +0000167
168 // Only preexpand the argument if it could possibly need it. This
169 // avoids some work in common cases.
Chris Lattnerd2177732007-07-20 16:59:19 +0000170 const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
Chris Lattnercc1a8752007-10-07 08:44:20 +0000171 if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
173 else
174 ResultArgToks = ArgTok; // Use non-preexpanded tokens.
175
176 // If the arg token expanded into anything, append it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000177 if (ResultArgToks->isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 unsigned FirstResult = ResultToks.size();
179 unsigned NumToks = MacroArgs::getArgLength(ResultArgToks);
180 ResultToks.append(ResultArgToks, ResultArgToks+NumToks);
181
182 // If any tokens were substituted from the argument, the whitespace
183 // before the first token should match the whitespace of the arg
184 // identifier.
Chris Lattnerd2177732007-07-20 16:59:19 +0000185 ResultToks[FirstResult].setFlagValue(Token::LeadingSpace,
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 CurTok.hasLeadingSpace() ||
187 NextTokGetsSpace);
188 NextTokGetsSpace = false;
189 } else {
190 // If this is an empty argument, and if there was whitespace before the
191 // formal token, make sure the next token gets whitespace before it.
192 NextTokGetsSpace = CurTok.hasLeadingSpace();
193 }
194 continue;
195 }
196
197 // Okay, we have a token that is either the LHS or RHS of a paste (##)
198 // argument. It gets substituted as its non-pre-expanded tokens.
Chris Lattnerd2177732007-07-20 16:59:19 +0000199 const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 unsigned NumToks = MacroArgs::getArgLength(ArgToks);
201 if (NumToks) { // Not an empty argument?
Chris Lattner71a3a8d2008-01-29 07:54:23 +0000202 // If this is the GNU ", ## __VA_ARG__" extension, and we just learned
203 // that __VA_ARG__ expands to multiple tokens, avoid a pasting error when
204 // the expander trys to paste ',' with the first token of the __VA_ARG__
205 // expansion.
206 if (PasteBefore && ResultToks.size() >= 2 &&
207 ResultToks[ResultToks.size()-2].is(tok::comma) &&
208 (unsigned)ArgNo == Macro->getNumArgs()-1 &&
209 Macro->isVariadic()) {
210 // Remove the paste operator, report use of the extension.
211 PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma);
212 ResultToks.pop_back();
213 }
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 ResultToks.append(ArgToks, ArgToks+NumToks);
216
217 // If the next token was supposed to get leading whitespace, ensure it has
218 // it now.
219 if (NextTokGetsSpace) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000220 ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 NextTokGetsSpace = false;
222 }
223 continue;
224 }
225
226 // If an empty argument is on the LHS or RHS of a paste, the standard (C99
227 // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We
228 // implement this by eating ## operators when a LHS or RHS expands to
229 // empty.
230 NextTokGetsSpace |= CurTok.hasLeadingSpace();
231 if (PasteAfter) {
232 // Discard the argument token and skip (don't copy to the expansion
233 // buffer) the paste operator after it.
Chris Lattner8d896432008-03-09 02:07:49 +0000234 NextTokGetsSpace |= Tokens[i+1].hasLeadingSpace();
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 ++i;
236 continue;
237 }
238
239 // If this is on the RHS of a paste operator, we've already copied the
240 // paste operator to the ResultToks list. Remove it.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000241 assert(PasteBefore && ResultToks.back().is(tok::hashhash));
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 NextTokGetsSpace |= ResultToks.back().hasLeadingSpace();
243 ResultToks.pop_back();
244
245 // If this is the __VA_ARGS__ token, and if the argument wasn't provided,
246 // and if the macro had at least one real argument, and if the token before
247 // the ## was a comma, remove the comma.
248 if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__
249 ActualArgs->isVarargsElidedUse() && // Argument elided.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000250 !ResultToks.empty() && ResultToks.back().is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // Never add a space, even if the comma, ##, or arg had a space.
252 NextTokGetsSpace = false;
Chris Lattner71a3a8d2008-01-29 07:54:23 +0000253 // Remove the paste operator, report use of the extension.
254 PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 ResultToks.pop_back();
256 }
257 continue;
258 }
259
Chris Lattner8d896432008-03-09 02:07:49 +0000260 // If anything changed, install this as the new Tokens list.
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 if (MadeChange) {
262 // This is deleted in the dtor.
Chris Lattner8d896432008-03-09 02:07:49 +0000263 NumTokens = ResultToks.size();
Chris Lattnerd2177732007-07-20 16:59:19 +0000264 Token *Res = new Token[ResultToks.size()];
Chris Lattner8d896432008-03-09 02:07:49 +0000265 if (NumTokens)
266 memcpy(Res, &ResultToks[0], NumTokens*sizeof(Token));
267 Tokens = Res;
268 OwnsTokens = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 }
270}
271
272/// Lex - Lex and return a token from this macro stream.
273///
Chris Lattner1543e9c2008-03-09 02:18:51 +0000274void TokenLexer::Lex(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 // Lexing off the end of the macro, pop this macro off the expansion stack.
276 if (isAtEnd()) {
277 // If this is a macro (not a token stream), mark the macro enabled now
278 // that it is no longer being expanded.
279 if (Macro) Macro->EnableMacro();
280
281 // Pop this context off the preprocessors lexer stack and get the next
282 // token. This will delete "this" so remember the PP instance var.
283 Preprocessor &PPCache = PP;
284 if (PP.HandleEndOfMacro(Tok))
285 return;
286
287 // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is
288 // next.
289 return PPCache.Lex(Tok);
290 }
291
292 // If this is the first token of the expanded result, we inherit spacing
293 // properties later.
294 bool isFirstToken = CurToken == 0;
295
296 // Get the next token to return.
Chris Lattner8d896432008-03-09 02:07:49 +0000297 Tok = Tokens[CurToken++];
Reid Spencer5f016e22007-07-11 17:01:13 +0000298
299 // If this token is followed by a token paste (##) operator, paste the tokens!
Chris Lattner8d896432008-03-09 02:07:49 +0000300 if (!isAtEnd() && Tokens[CurToken].is(tok::hashhash))
Chris Lattner3f1cc832008-02-07 06:03:59 +0000301 if (PasteTokens(Tok)) {
302 // When handling the microsoft /##/ extension, the final token is
303 // returned by PasteTokens, not the pasted token.
304 return;
305 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000306
307 // The token's current location indicate where the token was lexed from. We
308 // need this information to compute the spelling of the token, but any
309 // diagnostics for the expanded token should appear as if they came from
310 // InstantiationLoc. Pull this information together into a new SourceLocation
311 // that captures all of this.
312 if (InstantiateLoc.isValid()) { // Don't do this for token streams.
313 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000314 Tok.setLocation(SrcMgr.getInstantiationLoc(Tok.getLocation(),
315 InstantiateLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 }
317
318 // If this is the first token, set the lexical properties of the token to
319 // match the lexical properties of the macro identifier.
320 if (isFirstToken) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000321 Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
322 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 }
324
325 // Handle recursive expansion!
326 if (Tok.getIdentifierInfo())
327 return PP.HandleIdentifier(Tok);
328
329 // Otherwise, return a normal token.
330}
331
332/// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
333/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
334/// are is another ## after it, chomp it iteratively. Return the result as Tok.
Chris Lattner3f1cc832008-02-07 06:03:59 +0000335/// If this returns true, the caller should immediately return the token.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000336bool TokenLexer::PasteTokens(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 llvm::SmallVector<char, 128> Buffer;
338 do {
339 // Consume the ## operator.
Chris Lattner8d896432008-03-09 02:07:49 +0000340 SourceLocation PasteOpLoc = Tokens[CurToken].getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 ++CurToken;
342 assert(!isAtEnd() && "No token on the RHS of a paste operator!");
343
344 // Get the RHS token.
Chris Lattner8d896432008-03-09 02:07:49 +0000345 const Token &RHS = Tokens[CurToken];
Reid Spencer5f016e22007-07-11 17:01:13 +0000346
347 bool isInvalid = false;
348
349 // Allocate space for the result token. This is guaranteed to be enough for
350 // the two tokens and a null terminator.
351 Buffer.resize(Tok.getLength() + RHS.getLength() + 1);
352
353 // Get the spelling of the LHS token in Buffer.
354 const char *BufPtr = &Buffer[0];
355 unsigned LHSLen = PP.getSpelling(Tok, BufPtr);
356 if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer!
357 memcpy(&Buffer[0], BufPtr, LHSLen);
358
359 BufPtr = &Buffer[LHSLen];
360 unsigned RHSLen = PP.getSpelling(RHS, BufPtr);
361 if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer!
362 memcpy(&Buffer[LHSLen], BufPtr, RHSLen);
363
364 // Add null terminator.
365 Buffer[LHSLen+RHSLen] = '\0';
366
367 // Trim excess space.
368 Buffer.resize(LHSLen+RHSLen+1);
369
370 // Plop the pasted result (including the trailing newline and null) into a
371 // scratch buffer where we can lex it.
372 SourceLocation ResultTokLoc = PP.CreateString(&Buffer[0], Buffer.size());
373
374 // Lex the resultant pasted token into Result.
Chris Lattnerd2177732007-07-20 16:59:19 +0000375 Token Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
377 // Avoid testing /*, as the lexer would think it is the start of a comment
378 // and emit an error that it is unterminated.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000379 if (Tok.is(tok::slash) && RHS.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 isInvalid = true;
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000381 } else if (Tok.is(tok::identifier) && RHS.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 // Common paste case: identifier+identifier = identifier. Avoid creating
383 // a lexer and other overhead.
384 PP.IncrementPasteCounter(true);
385 Result.startToken();
386 Result.setKind(tok::identifier);
387 Result.setLocation(ResultTokLoc);
388 Result.setLength(LHSLen+RHSLen);
389 } else {
390 PP.IncrementPasteCounter(false);
391
392 // Make a lexer to lex this string from.
393 SourceManager &SourceMgr = PP.getSourceManager();
394 const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
395
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 // Make a lexer object so that we lex and expand the paste result.
Chris Lattner25bdb512007-07-20 16:52:03 +0000397 Lexer *TL = new Lexer(ResultTokLoc, PP, ResultStrData,
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 ResultStrData+LHSLen+RHSLen /*don't include null*/);
399
400 // Lex a token in raw mode. This way it won't look up identifiers
401 // automatically, lexing off the end will return an eof token, and
402 // warnings are disabled. This returns true if the result token is the
403 // entire buffer.
404 bool IsComplete = TL->LexRawToken(Result);
405
406 // If we got an EOF token, we didn't form even ONE token. For example, we
407 // did "/ ## /" to get "//".
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000408 IsComplete &= Result.isNot(tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 isInvalid = !IsComplete;
410
411 // We're now done with the temporary lexer.
412 delete TL;
413 }
414
415 // If pasting the two tokens didn't form a full new token, this is an error.
416 // This occurs with "x ## +" and other stuff. Return with Tok unmodified
417 // and with RHS as the next token to lex.
418 if (isInvalid) {
Chris Lattner3f1cc832008-02-07 06:03:59 +0000419 // Test for the Microsoft extension of /##/ turning into // here on the
420 // error path.
421 if (PP.getLangOptions().Microsoft && Tok.is(tok::slash) &&
422 RHS.is(tok::slash)) {
423 HandleMicrosoftCommentPaste(Tok);
424 return true;
425 } else {
426 // TODO: If not in assembler language mode.
427 PP.Diag(PasteOpLoc, diag::err_pp_bad_paste,
428 std::string(Buffer.begin(), Buffer.end()-1));
429 return false;
430 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 }
432
Chris Lattner1543e9c2008-03-09 02:18:51 +0000433 // Turn ## into 'unknown' to avoid # ## # from looking like a paste
434 // operator.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000435 if (Result.is(tok::hashhash))
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 Result.setKind(tok::unknown);
Chris Lattner71a3a8d2008-01-29 07:54:23 +0000437 // FIXME: Turn __VA_ARGS__ into "not a token"?
Reid Spencer5f016e22007-07-11 17:01:13 +0000438
439 // Transfer properties of the LHS over the the Result.
Chris Lattnerd2177732007-07-20 16:59:19 +0000440 Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine());
441 Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace());
Reid Spencer5f016e22007-07-11 17:01:13 +0000442
443 // Finally, replace LHS with the result, consume the RHS, and iterate.
444 ++CurToken;
445 Tok = Result;
Chris Lattner8d896432008-03-09 02:07:49 +0000446 } while (!isAtEnd() && Tokens[CurToken].is(tok::hashhash));
Reid Spencer5f016e22007-07-11 17:01:13 +0000447
448 // Now that we got the result token, it will be subject to expansion. Since
449 // token pasting re-lexes the result token in raw mode, identifier information
450 // isn't looked up. As such, if the result is an identifier, look up id info.
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000451 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 // Look up the identifier info for the token. We disabled identifier lookup
453 // by saying we're skipping contents, so we need to do this manually.
454 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
455 }
Chris Lattner3f1cc832008-02-07 06:03:59 +0000456 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000457}
458
459/// isNextTokenLParen - If the next token lexed will pop this macro off the
460/// expansion stack, return 2. If the next unexpanded token is a '(', return
461/// 1, otherwise return 0.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000462unsigned TokenLexer::isNextTokenLParen() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 // Out of tokens?
464 if (isAtEnd())
465 return 2;
Chris Lattner8d896432008-03-09 02:07:49 +0000466 return Tokens[CurToken].is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +0000467}
Chris Lattner3f1cc832008-02-07 06:03:59 +0000468
469
470/// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes
471/// together to form a comment that comments out everything in the current
472/// macro, other active macros, and anything left on the current physical
473/// source line of the instantiated buffer. Handle this by returning the
474/// first token on the next line.
Chris Lattner1543e9c2008-03-09 02:18:51 +0000475void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) {
Chris Lattner3f1cc832008-02-07 06:03:59 +0000476 // We 'comment out' the rest of this macro by just ignoring the rest of the
477 // tokens that have not been lexed yet, if any.
478
479 // Since this must be a macro, mark the macro enabled now that it is no longer
480 // being expanded.
481 assert(Macro && "Token streams can't paste comments");
482 Macro->EnableMacro();
483
484 PP.HandleMicrosoftCommentPaste(Tok);
485}