blob: e903973336eba8e72e3de5c5ad718cdaf4e15c24 [file] [log] [blame]
Chris Lattner95d72cd2008-03-09 02:18:51 +00001//===--- TokenLexer.cpp - Lex from a token stream -------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner95d72cd2008-03-09 02:18:51 +000010// This file implements the TokenLexer interface.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/MacroExpander.h"
15#include "clang/Lex/MacroInfo.h"
16#include "clang/Lex/Preprocessor.h"
Chris Lattner30709b032006-06-21 03:01:55 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner0707bd32006-07-15 05:23:58 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattnerb57aa462006-07-27 03:42:15 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000020using namespace clang;
21
Chris Lattner78186052006-07-09 00:45:31 +000022//===----------------------------------------------------------------------===//
Chris Lattneree8760b2006-07-15 07:42:55 +000023// MacroArgs Implementation
Chris Lattner78186052006-07-09 00:45:31 +000024//===----------------------------------------------------------------------===//
25
Chris Lattnerc1410dc2006-07-26 05:22:49 +000026/// MacroArgs ctor function - This destroys the vector passed in.
27MacroArgs *MacroArgs::create(const MacroInfo *MI,
Chris Lattner146762e2007-07-20 16:59:19 +000028 const Token *UnexpArgTokens,
Chris Lattner775d8322006-07-29 04:39:41 +000029 unsigned NumToks, bool VarargsElided) {
Chris Lattner78186052006-07-09 00:45:31 +000030 assert(MI->isFunctionLike() &&
Chris Lattneree8760b2006-07-15 07:42:55 +000031 "Can't have args for an object-like macro!");
Chris Lattnerc1410dc2006-07-26 05:22:49 +000032
33 // Allocate memory for the MacroArgs object with the lexer tokens at the end.
Chris Lattnerc1410dc2006-07-26 05:22:49 +000034 MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) +
Chris Lattner146762e2007-07-20 16:59:19 +000035 NumToks*sizeof(Token));
Chris Lattnerc1410dc2006-07-26 05:22:49 +000036 // Construct the macroargs object.
Chris Lattner775d8322006-07-29 04:39:41 +000037 new (Result) MacroArgs(NumToks, VarargsElided);
Chris Lattnerc1410dc2006-07-26 05:22:49 +000038
39 // Copy the actual unexpanded tokens to immediately after the result ptr.
40 if (NumToks)
Chris Lattner146762e2007-07-20 16:59:19 +000041 memcpy(const_cast<Token*>(Result->getUnexpArgument(0)),
42 UnexpArgTokens, NumToks*sizeof(Token));
Chris Lattnerc1410dc2006-07-26 05:22:49 +000043
44 return Result;
Chris Lattner78186052006-07-09 00:45:31 +000045}
46
Chris Lattnerc1410dc2006-07-26 05:22:49 +000047/// 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
Chris Lattner6fc08bc2006-07-26 04:55:32 +000057/// 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 Lattner146762e2007-07-20 16:59:19 +000060unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
Chris Lattner6fc08bc2006-07-26 04:55:32 +000061 unsigned NumArgTokens = 0;
Chris Lattner98c1f7c2007-10-09 18:02:16 +000062 for (; ArgPtr->isNot(tok::eof); ++ArgPtr)
Chris Lattner6fc08bc2006-07-26 04:55:32 +000063 ++NumArgTokens;
64 return NumArgTokens;
65}
66
67
Chris Lattner36b6e812006-07-21 06:38:30 +000068/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
69///
Chris Lattner146762e2007-07-20 16:59:19 +000070const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
Chris Lattnerc1410dc2006-07-26 05:22:49 +000071 // The unexpanded argument tokens start immediately after the MacroArgs object
72 // in memory.
Chris Lattner146762e2007-07-20 16:59:19 +000073 const Token *Start = (const Token *)(this+1);
74 const Token *Result = Start;
Chris Lattnerc1410dc2006-07-26 05:22:49 +000075 // Scan to find Arg.
Chris Lattner36b6e812006-07-21 06:38:30 +000076 for (; Arg; ++Result) {
Chris Lattnerc1410dc2006-07-26 05:22:49 +000077 assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
Chris Lattner98c1f7c2007-10-09 18:02:16 +000078 if (Result->is(tok::eof))
Chris Lattner36b6e812006-07-21 06:38:30 +000079 --Arg;
80 }
81 return Result;
Chris Lattneree8760b2006-07-15 07:42:55 +000082}
83
Chris Lattner36b6e812006-07-21 06:38:30 +000084
Chris Lattner203b4562006-07-15 21:07:40 +000085/// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
86/// by pre-expansion, return false. Otherwise, conservatively return true.
Chris Lattnerc43ddc82007-10-07 08:44:20 +000087bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
88 Preprocessor &PP) const {
Chris Lattner203b4562006-07-15 21:07:40 +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 Lattner98c1f7c2007-10-09 18:02:16 +000091 for (; ArgTok->isNot(tok::eof); ++ArgTok)
Chris Lattner36b6e812006-07-21 06:38:30 +000092 if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
Chris Lattnerc43ddc82007-10-07 08:44:20 +000093 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled())
Chris Lattner203b4562006-07-15 21:07:40 +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
Chris Lattner7667d0d2006-07-16 18:16:58 +0000101/// getPreExpArgument - Return the pre-expanded form of the specified
102/// argument.
Chris Lattner146762e2007-07-20 16:59:19 +0000103const std::vector<Token> &
Chris Lattner7667d0d2006-07-16 18:16:58 +0000104MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000105 assert(Arg < NumUnexpArgTokens && "Invalid argument number!");
Chris Lattner7667d0d2006-07-16 18:16:58 +0000106
107 // If we have already computed this, return it.
108 if (PreExpArgTokens.empty())
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000109 PreExpArgTokens.resize(NumUnexpArgTokens);
Chris Lattner7667d0d2006-07-16 18:16:58 +0000110
Chris Lattner146762e2007-07-20 16:59:19 +0000111 std::vector<Token> &Result = PreExpArgTokens[Arg];
Chris Lattner7667d0d2006-07-16 18:16:58 +0000112 if (!Result.empty()) return Result;
113
Chris Lattner146762e2007-07-20 16:59:19 +0000114 const Token *AT = getUnexpArgument(Arg);
Chris Lattner6fc08bc2006-07-26 04:55:32 +0000115 unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
Chris Lattner36b6e812006-07-21 06:38:30 +0000116
Chris Lattner7667d0d2006-07-16 18:16:58 +0000117 // Otherwise, we have to pre-expand this argument, populating Result. To do
Chris Lattner95d72cd2008-03-09 02:18:51 +0000118 // this, we set up a fake TokenLexer to lex from the unexpanded argument
Chris Lattner7667d0d2006-07-16 18:16:58 +0000119 // list. With this installed, we lex expanded tokens until we hit the EOF
120 // token at the end of the unexp list.
Chris Lattner70216572006-07-26 03:50:40 +0000121 PP.EnterTokenStream(AT, NumToks);
Chris Lattner7667d0d2006-07-16 18:16:58 +0000122
123 // Lex all of the macro-expanded tokens into Result.
124 do {
Chris Lattner146762e2007-07-20 16:59:19 +0000125 Result.push_back(Token());
Chris Lattner7667d0d2006-07-16 18:16:58 +0000126 PP.Lex(Result.back());
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000127 } while (Result.back().isNot(tok::eof));
Chris Lattner7667d0d2006-07-16 18:16:58 +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
Chris Lattneree8760b2006-07-15 07:42:55 +0000138
Chris Lattner0707bd32006-07-15 05:23:58 +0000139/// 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 Lattner146762e2007-07-20 16:59:19 +0000143static Token StringifyArgument(const Token *ArgToks,
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000144 Preprocessor &PP, bool Charify = false) {
Chris Lattner146762e2007-07-20 16:59:19 +0000145 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +0000146 Tok.startToken();
147 Tok.setKind(tok::string_literal);
Chris Lattner0707bd32006-07-15 05:23:58 +0000148
Chris Lattner146762e2007-07-20 16:59:19 +0000149 const Token *ArgTokStart = ArgToks;
Chris Lattner36b6e812006-07-21 06:38:30 +0000150
Chris Lattner0707bd32006-07-15 05:23:58 +0000151 // Stringify all the tokens.
152 std::string Result = "\"";
Chris Lattner7667d0d2006-07-16 18:16:58 +0000153 // FIXME: Optimize this loop to not use std::strings.
Chris Lattner36b6e812006-07-21 06:38:30 +0000154 bool isFirst = true;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000155 for (; ArgToks->isNot(tok::eof); ++ArgToks) {
Chris Lattner146762e2007-07-20 16:59:19 +0000156 const Token &Tok = *ArgToks;
Chris Lattner24dbee72007-07-19 16:11:58 +0000157 if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()))
Chris Lattner0707bd32006-07-15 05:23:58 +0000158 Result += ' ';
Chris Lattner36b6e812006-07-21 06:38:30 +0000159 isFirst = false;
Chris Lattner0707bd32006-07-15 05:23:58 +0000160
161 // If this is a string or character constant, escape the token as specified
162 // by 6.10.3.2p2.
Chris Lattner98c1f7c2007-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 Lattner0707bd32006-07-15 05:23:58 +0000166 Result += Lexer::Stringify(PP.getSpelling(Tok));
167 } else {
168 // Otherwise, just append the token.
169 Result += PP.getSpelling(Tok);
170 }
171 }
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000172
Chris Lattner0707bd32006-07-15 05:23:58 +0000173 // 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) {
Chris Lattnerf2781502006-07-15 05:27:44 +0000183 // Diagnose errors for things like: #define F(X) #X / F(\)
Chris Lattner36b6e812006-07-21 06:38:30 +0000184 PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
Chris Lattner0707bd32006-07-15 05:23:58 +0000185 Result.erase(Result.end()-1); // remove one of the \'s.
186 }
187 }
Chris Lattner0707bd32006-07-15 05:23:58 +0000188 Result += '"';
189
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000190 // 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;
Chris Lattner2ada5d32006-07-15 07:51:24 +0000199 if (Result.size() == 3) {
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000200 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) {
Chris Lattner36b6e812006-07-21 06:38:30 +0000206 PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
Chris Lattner7c581492006-07-15 07:56:31 +0000207 Result = "' '"; // Use something arbitrary, but legal.
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000208 }
209 }
210
Chris Lattner8c204872006-10-14 05:19:21 +0000211 Tok.setLength(Result.size());
212 Tok.setLocation(PP.CreateString(&Result[0], Result.size()));
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000213 return Tok;
214}
215
216/// getStringifiedArgument - Compute, cache, and return the specified argument
217/// that has been 'stringified' as required by the # operator.
Chris Lattner146762e2007-07-20 16:59:19 +0000218const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo,
Chris Lattneree8760b2006-07-15 07:42:55 +0000219 Preprocessor &PP) {
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000220 assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000221 if (StringifiedArgs.empty()) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000222 StringifiedArgs.resize(getNumArguments());
Chris Lattneree8760b2006-07-15 07:42:55 +0000223 memset(&StringifiedArgs[0], 0,
224 sizeof(StringifiedArgs[0])*getNumArguments());
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000225 }
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000226 if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
Chris Lattner36b6e812006-07-21 06:38:30 +0000227 StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000228 return StringifiedArgs[ArgNo];
229}
230
Chris Lattner78186052006-07-09 00:45:31 +0000231//===----------------------------------------------------------------------===//
Chris Lattner95d72cd2008-03-09 02:18:51 +0000232// TokenLexer Implementation
Chris Lattner78186052006-07-09 00:45:31 +0000233//===----------------------------------------------------------------------===//
234
Chris Lattner95d72cd2008-03-09 02:18:51 +0000235/// Create a TokenLexer for the specified macro with the specified actual
Chris Lattner7667d0d2006-07-16 18:16:58 +0000236/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
Chris Lattner95d72cd2008-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 Lattnerc02c4ab2007-07-15 00:25:26 +0000239 // associated with it.
240 destroy();
241
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000242 Macro = PP.getMacroInfo(Tok.getIdentifierInfo());
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000243 ActualArgs = Actuals;
244 CurToken = 0;
245 InstantiateLoc = Tok.getLocation();
246 AtStartOfLine = Tok.isAtStartOfLine();
247 HasLeadingSpace = Tok.hasLeadingSpace();
Chris Lattnerd7daed12008-03-09 02:07:49 +0000248 Tokens = &*Macro->tokens_begin();
249 OwnsTokens = false;
250 NumTokens = Macro->tokens_end()-Macro->tokens_begin();
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000251
252 // If this is a function-like macro, expand the arguments and change
Chris Lattnerd7daed12008-03-09 02:07:49 +0000253 // Tokens to point to the expanded tokens.
Chris Lattner95a06b32006-07-30 08:40:43 +0000254 if (Macro->isFunctionLike() && Macro->getNumArgs())
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000255 ExpandFunctionArguments();
Chris Lattner7667d0d2006-07-16 18:16:58 +0000256
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();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000261}
262
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000263
264
Chris Lattner95d72cd2008-03-09 02:18:51 +0000265/// Create a TokenLexer for the specified token stream. This does not
Chris Lattner7667d0d2006-07-16 18:16:58 +0000266/// take ownership of the specified token vector.
Chris Lattner95d72cd2008-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 Lattnerc02c4ab2007-07-15 00:25:26 +0000269 // associated with it.
270 destroy();
271
272 Macro = 0;
273 ActualArgs = 0;
Chris Lattnerd7daed12008-03-09 02:07:49 +0000274 Tokens = TokArray;
275 OwnsTokens = false;
276 NumTokens = NumToks;
Chris Lattnerc02c4ab2007-07-15 00:25:26 +0000277 CurToken = 0;
278 InstantiateLoc = SourceLocation();
279 AtStartOfLine = false;
280 HasLeadingSpace = false;
Chris Lattner7667d0d2006-07-16 18:16:58 +0000281
282 // Set HasLeadingSpace/AtStartOfLine so that the first token will be
283 // returned unmodified.
Chris Lattner70216572006-07-26 03:50:40 +0000284 if (NumToks != 0) {
285 AtStartOfLine = TokArray[0].isAtStartOfLine();
286 HasLeadingSpace = TokArray[0].hasLeadingSpace();
Chris Lattner7667d0d2006-07-16 18:16:58 +0000287 }
288}
289
290
Chris Lattner95d72cd2008-03-09 02:18:51 +0000291void TokenLexer::destroy() {
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000292 // If this was a function-like macro that actually uses its arguments, delete
293 // the expanded tokens.
Chris Lattnerd7daed12008-03-09 02:07:49 +0000294 if (OwnsTokens) {
295 delete [] Tokens;
296 Tokens = 0;
Chris Lattner9c724c42007-07-22 01:16:55 +0000297 }
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000298
Chris Lattner95d72cd2008-03-09 02:18:51 +0000299 // TokenLexer owns its formal arguments.
Chris Lattnerc1410dc2006-07-26 05:22:49 +0000300 if (ActualArgs) ActualArgs->destroy();
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000301}
302
303/// Expand the arguments of a function-like macro so that we can quickly
Chris Lattnerd7daed12008-03-09 02:07:49 +0000304/// return preexpanded tokens from Tokens.
Chris Lattner95d72cd2008-03-09 02:18:51 +0000305void TokenLexer::ExpandFunctionArguments() {
Chris Lattner146762e2007-07-20 16:59:19 +0000306 llvm::SmallVector<Token, 128> ResultToks;
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000307
Chris Lattnerd7daed12008-03-09 02:07:49 +0000308 // Loop through 'Tokens', expanding them into ResultToks. Keep
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000309 // track of whether we change anything. If not, no need to keep them. If so,
Chris Lattnerd7daed12008-03-09 02:07:49 +0000310 // we install the newly expanded sequence as the new 'Tokens' list.
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000311 bool MadeChange = false;
Chris Lattner479b0af2006-07-29 04:16:20 +0000312
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 Lattnerd7daed12008-03-09 02:07:49 +0000318 for (unsigned i = 0, e = NumTokens; i != e; ++i) {
Chris Lattnerb935d8c2006-07-14 06:54:44 +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 Lattnerd7daed12008-03-09 02:07:49 +0000322 const Token &CurTok = Tokens[i];
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000323 if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) {
Chris Lattnerd7daed12008-03-09 02:07:49 +0000324 int ArgNo = Macro->getArgumentNum(Tokens[i+1].getIdentifierInfo());
Chris Lattner95a06b32006-07-30 08:40:43 +0000325 assert(ArgNo != -1 && "Token following # is not an argument?");
326
Chris Lattner146762e2007-07-20 16:59:19 +0000327 Token Res;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000328 if (CurTok.is(tok::hash)) // Stringify
Chris Lattner6fc08bc2006-07-26 04:55:32 +0000329 Res = ActualArgs->getStringifiedArgument(ArgNo, PP);
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000330 else {
331 // 'charify': don't bother caching these.
Chris Lattner6fc08bc2006-07-26 04:55:32 +0000332 Res = StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), PP, true);
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000333 }
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000334
Chris Lattner60161692006-07-15 06:48:02 +0000335 // The stringified/charified string leading space flag gets set to match
336 // the #/#@ operator.
Chris Lattner479b0af2006-07-29 04:16:20 +0000337 if (CurTok.hasLeadingSpace() || NextTokGetsSpace)
Chris Lattner146762e2007-07-20 16:59:19 +0000338 Res.setFlag(Token::LeadingSpace);
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000339
Chris Lattner6fc08bc2006-07-26 04:55:32 +0000340 ResultToks.push_back(Res);
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000341 MadeChange = true;
342 ++i; // Skip arg name.
Chris Lattner479b0af2006-07-29 04:16:20 +0000343 NextTokGetsSpace = false;
Chris Lattnera9dc5972006-07-28 05:07:04 +0000344 continue;
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000345 }
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000346
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) {
Chris Lattner95a06b32006-07-30 08:40:43 +0000352 // This isn't an argument, just add it.
353 ResultToks.push_back(CurTok);
Chris Lattner479b0af2006-07-29 04:16:20 +0000354
Chris Lattner95a06b32006-07-30 08:40:43 +0000355 if (NextTokGetsSpace) {
Chris Lattner146762e2007-07-20 16:59:19 +0000356 ResultToks.back().setFlag(Token::LeadingSpace);
Chris Lattner95a06b32006-07-30 08:40:43 +0000357 NextTokGetsSpace = false;
Chris Lattner21c8b8f2006-07-29 04:04:22 +0000358 }
Chris Lattner95a06b32006-07-30 08:40:43 +0000359 continue;
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000360 }
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 Lattner98c1f7c2007-10-09 18:02:16 +0000369 !ResultToks.empty() && ResultToks.back().is(tok::hashhash);
Chris Lattnerd7daed12008-03-09 02:07:49 +0000370 bool PasteAfter = i+1 != e && Tokens[i+1].is(tok::hashhash);
Chris Lattnerf3f1c702006-07-28 05:10:36 +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 Lattner146762e2007-07-20 16:59:19 +0000376 const Token *ResultArgToks;
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000377
378 // Only preexpand the argument if it could possibly need it. This
379 // avoids some work in common cases.
Chris Lattner146762e2007-07-20 16:59:19 +0000380 const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
Chris Lattnerc43ddc82007-10-07 08:44:20 +0000381 if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP))
Chris Lattnerf3f1c702006-07-28 05:10:36 +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 Lattner98c1f7c2007-10-09 18:02:16 +0000387 if (ResultArgToks->isNot(tok::eof)) {
Chris Lattnerf3f1c702006-07-28 05:10:36 +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 Lattner146762e2007-07-20 16:59:19 +0000395 ResultToks[FirstResult].setFlagValue(Token::LeadingSpace,
Chris Lattner479b0af2006-07-29 04:16:20 +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();
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000403 }
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 Lattner146762e2007-07-20 16:59:19 +0000409 const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000410 unsigned NumToks = MacroArgs::getArgLength(ArgToks);
411 if (NumToks) { // Not an empty argument?
Chris Lattner0c8a1ed2008-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 Lattnerf3f1c702006-07-28 05:10:36 +0000425 ResultToks.append(ArgToks, ArgToks+NumToks);
Chris Lattner479b0af2006-07-29 04:16:20 +0000426
427 // If the next token was supposed to get leading whitespace, ensure it has
428 // it now.
429 if (NextTokGetsSpace) {
Chris Lattner146762e2007-07-20 16:59:19 +0000430 ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace);
Chris Lattner479b0af2006-07-29 04:16:20 +0000431 NextTokGetsSpace = false;
432 }
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000433 continue;
434 }
435
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000436 // 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.
Chris Lattner479b0af2006-07-29 04:16:20 +0000440 NextTokGetsSpace |= CurTok.hasLeadingSpace();
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000441 if (PasteAfter) {
442 // Discard the argument token and skip (don't copy to the expansion
443 // buffer) the paste operator after it.
Chris Lattnerd7daed12008-03-09 02:07:49 +0000444 NextTokGetsSpace |= Tokens[i+1].hasLeadingSpace();
Chris Lattnerf3f1c702006-07-28 05:10:36 +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 Lattner98c1f7c2007-10-09 18:02:16 +0000451 assert(PasteBefore && ResultToks.back().is(tok::hashhash));
Chris Lattner479b0af2006-07-29 04:16:20 +0000452 NextTokGetsSpace |= ResultToks.back().hasLeadingSpace();
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000453 ResultToks.pop_back();
Chris Lattner775d8322006-07-29 04:39:41 +0000454
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.
Chris Lattner95a06b32006-07-30 08:40:43 +0000458 if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__
Chris Lattner775d8322006-07-29 04:39:41 +0000459 ActualArgs->isVarargsElidedUse() && // Argument elided.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000460 !ResultToks.empty() && ResultToks.back().is(tok::comma)) {
Chris Lattner775d8322006-07-29 04:39:41 +0000461 // Never add a space, even if the comma, ##, or arg had a space.
462 NextTokGetsSpace = false;
Chris Lattner0c8a1ed2008-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 Lattner775d8322006-07-29 04:39:41 +0000465 ResultToks.pop_back();
466 }
Chris Lattnerf3f1c702006-07-28 05:10:36 +0000467 continue;
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000468 }
469
Chris Lattnerd7daed12008-03-09 02:07:49 +0000470 // If anything changed, install this as the new Tokens list.
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000471 if (MadeChange) {
472 // This is deleted in the dtor.
Chris Lattnerd7daed12008-03-09 02:07:49 +0000473 NumTokens = ResultToks.size();
Chris Lattner146762e2007-07-20 16:59:19 +0000474 Token *Res = new Token[ResultToks.size()];
Chris Lattnerd7daed12008-03-09 02:07:49 +0000475 if (NumTokens)
476 memcpy(Res, &ResultToks[0], NumTokens*sizeof(Token));
477 Tokens = Res;
478 OwnsTokens = true;
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000479 }
480}
Chris Lattner67b07cb2006-06-26 02:03:42 +0000481
Chris Lattner22eb9722006-06-18 05:43:12 +0000482/// Lex - Lex and return a token from this macro stream.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000483///
Chris Lattner95d72cd2008-03-09 02:18:51 +0000484void TokenLexer::Lex(Token &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000485 // Lexing off the end of the macro, pop this macro off the expansion stack.
Chris Lattner7667d0d2006-07-16 18:16:58 +0000486 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
Chris Lattner2183a6e2006-07-18 06:36:12 +0000492 // 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);
Chris Lattner7667d0d2006-07-16 18:16:58 +0000500 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000501
Chris Lattnere8dcfef2006-07-19 05:45:55 +0000502 // If this is the first token of the expanded result, we inherit spacing
503 // properties later.
504 bool isFirstToken = CurToken == 0;
505
Chris Lattner22eb9722006-06-18 05:43:12 +0000506 // Get the next token to return.
Chris Lattnerd7daed12008-03-09 02:07:49 +0000507 Tok = Tokens[CurToken++];
Chris Lattner01ecf832006-07-19 05:42:48 +0000508
509 // If this token is followed by a token paste (##) operator, paste the tokens!
Chris Lattnerd7daed12008-03-09 02:07:49 +0000510 if (!isAtEnd() && Tokens[CurToken].is(tok::hashhash))
Chris Lattner3b5054d2008-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 Lattner22eb9722006-06-18 05:43:12 +0000516
Chris Lattnerc673f902006-06-30 06:10:41 +0000517 // 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.
Chris Lattner7667d0d2006-07-16 18:16:58 +0000522 if (InstantiateLoc.isValid()) { // Don't do this for token streams.
523 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattner3fc74e22007-07-15 06:35:27 +0000524 Tok.setLocation(SrcMgr.getInstantiationLoc(Tok.getLocation(),
525 InstantiateLoc));
Chris Lattner7667d0d2006-07-16 18:16:58 +0000526 }
527
Chris Lattner22eb9722006-06-18 05:43:12 +0000528 // If this is the first token, set the lexical properties of the token to
529 // match the lexical properties of the macro identifier.
Chris Lattnere8dcfef2006-07-19 05:45:55 +0000530 if (isFirstToken) {
Chris Lattner146762e2007-07-20 16:59:19 +0000531 Tok.setFlagValue(Token::StartOfLine , AtStartOfLine);
532 Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000533 }
534
535 // Handle recursive expansion!
536 if (Tok.getIdentifierInfo())
537 return PP.HandleIdentifier(Tok);
538
539 // Otherwise, return a normal token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000540}
Chris Lattnerafe603f2006-07-11 04:02:46 +0000541
Chris Lattner01ecf832006-07-19 05:42:48 +0000542/// 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 Lattner3b5054d2008-02-07 06:03:59 +0000545/// If this returns true, the caller should immediately return the token.
Chris Lattner95d72cd2008-03-09 02:18:51 +0000546bool TokenLexer::PasteTokens(Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000547 llvm::SmallVector<char, 128> Buffer;
Chris Lattner01ecf832006-07-19 05:42:48 +0000548 do {
549 // Consume the ## operator.
Chris Lattnerd7daed12008-03-09 02:07:49 +0000550 SourceLocation PasteOpLoc = Tokens[CurToken].getLocation();
Chris Lattner01ecf832006-07-19 05:42:48 +0000551 ++CurToken;
552 assert(!isAtEnd() && "No token on the RHS of a paste operator!");
553
554 // Get the RHS token.
Chris Lattnerd7daed12008-03-09 02:07:49 +0000555 const Token &RHS = Tokens[CurToken];
Chris Lattner01ecf832006-07-19 05:42:48 +0000556
557 bool isInvalid = false;
558
Chris Lattner01ecf832006-07-19 05:42:48 +0000559 // Allocate space for the result token. This is guaranteed to be enough for
560 // the two tokens and a null terminator.
Chris Lattner57dd8362006-11-03 07:45:04 +0000561 Buffer.resize(Tok.getLength() + RHS.getLength() + 1);
Chris Lattner01ecf832006-07-19 05:42:48 +0000562
563 // Get the spelling of the LHS token in Buffer.
Chris Lattner57dd8362006-11-03 07:45:04 +0000564 const char *BufPtr = &Buffer[0];
Chris Lattner01ecf832006-07-19 05:42:48 +0000565 unsigned LHSLen = PP.getSpelling(Tok, BufPtr);
Chris Lattner57dd8362006-11-03 07:45:04 +0000566 if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer!
567 memcpy(&Buffer[0], BufPtr, LHSLen);
Chris Lattner01ecf832006-07-19 05:42:48 +0000568
Chris Lattner57dd8362006-11-03 07:45:04 +0000569 BufPtr = &Buffer[LHSLen];
Chris Lattner01ecf832006-07-19 05:42:48 +0000570 unsigned RHSLen = PP.getSpelling(RHS, BufPtr);
Chris Lattner57dd8362006-11-03 07:45:04 +0000571 if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer!
572 memcpy(&Buffer[LHSLen], BufPtr, RHSLen);
Chris Lattner01ecf832006-07-19 05:42:48 +0000573
574 // Add null terminator.
575 Buffer[LHSLen+RHSLen] = '\0';
576
Chris Lattner57dd8362006-11-03 07:45:04 +0000577 // Trim excess space.
578 Buffer.resize(LHSLen+RHSLen+1);
579
Chris Lattner01ecf832006-07-19 05:42:48 +0000580 // Plop the pasted result (including the trailing newline and null) into a
581 // scratch buffer where we can lex it.
Chris Lattner57dd8362006-11-03 07:45:04 +0000582 SourceLocation ResultTokLoc = PP.CreateString(&Buffer[0], Buffer.size());
Chris Lattner01ecf832006-07-19 05:42:48 +0000583
584 // Lex the resultant pasted token into Result.
Chris Lattner146762e2007-07-20 16:59:19 +0000585 Token Result;
Chris Lattner01ecf832006-07-19 05:42:48 +0000586
Chris Lattnera7e2e742006-07-19 06:32:35 +0000587 // Avoid testing /*, as the lexer would think it is the start of a comment
588 // and emit an error that it is unterminated.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000589 if (Tok.is(tok::slash) && RHS.is(tok::star)) {
Chris Lattnera7e2e742006-07-19 06:32:35 +0000590 isInvalid = true;
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000591 } else if (Tok.is(tok::identifier) && RHS.is(tok::identifier)) {
Chris Lattner510ab612006-07-20 04:47:30 +0000592 // Common paste case: identifier+identifier = identifier. Avoid creating
593 // a lexer and other overhead.
594 PP.IncrementPasteCounter(true);
Chris Lattner8c204872006-10-14 05:19:21 +0000595 Result.startToken();
596 Result.setKind(tok::identifier);
597 Result.setLocation(ResultTokLoc);
598 Result.setLength(LHSLen+RHSLen);
Chris Lattnera7e2e742006-07-19 06:32:35 +0000599 } else {
Chris Lattner510ab612006-07-20 04:47:30 +0000600 PP.IncrementPasteCounter(false);
601
Chris Lattnera7e2e742006-07-19 06:32:35 +0000602 // Make a lexer to lex this string from.
603 SourceManager &SourceMgr = PP.getSourceManager();
604 const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
605
Chris Lattnera7e2e742006-07-19 06:32:35 +0000606 // Make a lexer object so that we lex and expand the paste result.
Chris Lattner77e9de52007-07-20 16:52:03 +0000607 Lexer *TL = new Lexer(ResultTokLoc, PP, ResultStrData,
Chris Lattnera7e2e742006-07-19 06:32:35 +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 Lattner98c1f7c2007-10-09 18:02:16 +0000618 IsComplete &= Result.isNot(tok::eof);
Chris Lattnera7e2e742006-07-19 06:32:35 +0000619 isInvalid = !IsComplete;
620
621 // We're now done with the temporary lexer.
622 delete TL;
623 }
Chris Lattner01ecf832006-07-19 05:42:48 +0000624
625 // If pasting the two tokens didn't form a full new token, this is an error.
Chris Lattnera7e2e742006-07-19 06:32:35 +0000626 // 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 Lattner3b5054d2008-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 Lattner01ecf832006-07-19 05:42:48 +0000641 }
642
Chris Lattner95d72cd2008-03-09 02:18:51 +0000643 // Turn ## into 'unknown' to avoid # ## # from looking like a paste
644 // operator.
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000645 if (Result.is(tok::hashhash))
Chris Lattner8c204872006-10-14 05:19:21 +0000646 Result.setKind(tok::unknown);
Chris Lattner0c8a1ed2008-01-29 07:54:23 +0000647 // FIXME: Turn __VA_ARGS__ into "not a token"?
Chris Lattner01ecf832006-07-19 05:42:48 +0000648
649 // Transfer properties of the LHS over the the Result.
Chris Lattner146762e2007-07-20 16:59:19 +0000650 Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine());
651 Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace());
Chris Lattner01ecf832006-07-19 05:42:48 +0000652
653 // Finally, replace LHS with the result, consume the RHS, and iterate.
654 ++CurToken;
655 Tok = Result;
Chris Lattnerd7daed12008-03-09 02:07:49 +0000656 } while (!isAtEnd() && Tokens[CurToken].is(tok::hashhash));
Chris Lattner0f1f5052006-07-20 04:16:23 +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 Lattner98c1f7c2007-10-09 18:02:16 +0000661 if (Tok.is(tok::identifier)) {
Chris Lattner0f1f5052006-07-20 04:16:23 +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.
Chris Lattner8c204872006-10-14 05:19:21 +0000664 Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
Chris Lattner0f1f5052006-07-20 04:16:23 +0000665 }
Chris Lattner3b5054d2008-02-07 06:03:59 +0000666 return false;
Chris Lattner01ecf832006-07-19 05:42:48 +0000667}
668
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000669/// 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 Lattner95d72cd2008-03-09 02:18:51 +0000672unsigned TokenLexer::isNextTokenLParen() const {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000673 // Out of tokens?
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000674 if (isAtEnd())
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000675 return 2;
Chris Lattnerd7daed12008-03-09 02:07:49 +0000676 return Tokens[CurToken].is(tok::l_paren);
Chris Lattnerafe603f2006-07-11 04:02:46 +0000677}
Chris Lattner3b5054d2008-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 Lattner95d72cd2008-03-09 02:18:51 +0000685void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) {
Chris Lattner3b5054d2008-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}