blob: 817f68aa9521f2725ab0d151cace79ff5803c23b [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +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"
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 Lattner01ecf832006-07-19 05:42:48 +000019#include "llvm/Config/Alloca.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000020using namespace llvm;
21using namespace clang;
22
Chris Lattner78186052006-07-09 00:45:31 +000023//===----------------------------------------------------------------------===//
Chris Lattneree8760b2006-07-15 07:42:55 +000024// MacroArgs Implementation
Chris Lattner78186052006-07-09 00:45:31 +000025//===----------------------------------------------------------------------===//
26
Chris Lattner36b6e812006-07-21 06:38:30 +000027MacroArgs::MacroArgs(const MacroInfo *MI, std::vector<LexerToken> &UnexpArgs) {
Chris Lattner78186052006-07-09 00:45:31 +000028 assert(MI->isFunctionLike() &&
Chris Lattneree8760b2006-07-15 07:42:55 +000029 "Can't have args for an object-like macro!");
Chris Lattner36b6e812006-07-21 06:38:30 +000030 UnexpArgTokens.swap(UnexpArgs);
Chris Lattner78186052006-07-09 00:45:31 +000031}
32
Chris Lattner36b6e812006-07-21 06:38:30 +000033/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
34///
35const LexerToken *MacroArgs::getUnexpArgument(unsigned Arg) const {
36 // Scan to find Arg.
37 const LexerToken *Start = &UnexpArgTokens[0];
38 const LexerToken *Result = Start;
39 for (; Arg; ++Result) {
40 assert(Result < Start+UnexpArgTokens.size() && "Invalid arg #");
41 if (Result->getKind() == tok::eof)
42 --Arg;
43 }
44 return Result;
Chris Lattneree8760b2006-07-15 07:42:55 +000045}
46
Chris Lattner36b6e812006-07-21 06:38:30 +000047
Chris Lattner203b4562006-07-15 21:07:40 +000048/// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
49/// by pre-expansion, return false. Otherwise, conservatively return true.
Chris Lattner36b6e812006-07-21 06:38:30 +000050bool MacroArgs::ArgNeedsPreexpansion(const LexerToken *ArgTok) const {
Chris Lattner203b4562006-07-15 21:07:40 +000051 // If there are no identifiers in the argument list, or if the identifiers are
52 // known to not be macros, pre-expansion won't modify it.
Chris Lattner36b6e812006-07-21 06:38:30 +000053 for (; ArgTok->getKind() != tok::eof; ++ArgTok)
54 if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
Chris Lattner203b4562006-07-15 21:07:40 +000055 if (II->getMacroInfo() && II->getMacroInfo()->isEnabled())
56 // Return true even though the macro could be a function-like macro
57 // without a following '(' token.
58 return true;
59 }
60 return false;
61}
62
Chris Lattner7667d0d2006-07-16 18:16:58 +000063/// getPreExpArgument - Return the pre-expanded form of the specified
64/// argument.
65const std::vector<LexerToken> &
66MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
67 assert(Arg < UnexpArgTokens.size() && "Invalid argument number!");
68
69 // If we have already computed this, return it.
70 if (PreExpArgTokens.empty())
71 PreExpArgTokens.resize(UnexpArgTokens.size());
72
73 std::vector<LexerToken> &Result = PreExpArgTokens[Arg];
74 if (!Result.empty()) return Result;
75
Chris Lattner36b6e812006-07-21 06:38:30 +000076 // FIXME
77 // FIXME: Don't require copying into a temporary vector!!!
78 // FIXME
79
80 std::vector<LexerToken> UnexpArgToks;
81 const LexerToken *AT = getUnexpArgument(Arg);
82 for (; AT->getKind() != tok::eof; ++AT)
83 UnexpArgToks.push_back(*AT);
84 UnexpArgToks.push_back(*AT); // push the EOF too.
85
Chris Lattner7667d0d2006-07-16 18:16:58 +000086 // Otherwise, we have to pre-expand this argument, populating Result. To do
87 // this, we set up a fake MacroExpander to lex from the unexpanded argument
88 // list. With this installed, we lex expanded tokens until we hit the EOF
89 // token at the end of the unexp list.
Chris Lattner36b6e812006-07-21 06:38:30 +000090 PP.EnterTokenStream(UnexpArgToks);
Chris Lattner7667d0d2006-07-16 18:16:58 +000091
92 // Lex all of the macro-expanded tokens into Result.
93 do {
94 Result.push_back(LexerToken());
95 PP.Lex(Result.back());
96 } while (Result.back().getKind() != tok::eof);
97
98 // Pop the token stream off the top of the stack. We know that the internal
99 // pointer inside of it is to the "end" of the token stream, but the stack
100 // will not otherwise be popped until the next token is lexed. The problem is
101 // that the token may be lexed sometime after the vector of tokens itself is
102 // destroyed, which would be badness.
103 PP.RemoveTopOfLexerStack();
104 return Result;
105}
106
Chris Lattneree8760b2006-07-15 07:42:55 +0000107
Chris Lattner0707bd32006-07-15 05:23:58 +0000108/// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
109/// tokens into the literal string token that should be produced by the C #
110/// preprocessor operator.
111///
Chris Lattner36b6e812006-07-21 06:38:30 +0000112static LexerToken StringifyArgument(const LexerToken *ArgToks,
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000113 Preprocessor &PP, bool Charify = false) {
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000114 LexerToken Tok;
115 Tok.StartToken();
116 Tok.SetKind(tok::string_literal);
Chris Lattner0707bd32006-07-15 05:23:58 +0000117
Chris Lattner36b6e812006-07-21 06:38:30 +0000118 const LexerToken *ArgTokStart = ArgToks;
119
Chris Lattner0707bd32006-07-15 05:23:58 +0000120 // Stringify all the tokens.
121 std::string Result = "\"";
Chris Lattner7667d0d2006-07-16 18:16:58 +0000122 // FIXME: Optimize this loop to not use std::strings.
Chris Lattner36b6e812006-07-21 06:38:30 +0000123 bool isFirst = true;
124 for (; ArgToks->getKind() != tok::eof; ++ArgToks) {
125 const LexerToken &Tok = *ArgToks;
126 if (!isFirst && Tok.hasLeadingSpace())
Chris Lattner0707bd32006-07-15 05:23:58 +0000127 Result += ' ';
Chris Lattner36b6e812006-07-21 06:38:30 +0000128 isFirst = false;
Chris Lattner0707bd32006-07-15 05:23:58 +0000129
130 // If this is a string or character constant, escape the token as specified
131 // by 6.10.3.2p2.
132 if (Tok.getKind() == tok::string_literal || // "foo" and L"foo".
133 Tok.getKind() == tok::char_constant) { // 'x' and L'x'.
134 Result += Lexer::Stringify(PP.getSpelling(Tok));
135 } else {
136 // Otherwise, just append the token.
137 Result += PP.getSpelling(Tok);
138 }
139 }
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000140
Chris Lattner0707bd32006-07-15 05:23:58 +0000141 // If the last character of the string is a \, and if it isn't escaped, this
142 // is an invalid string literal, diagnose it as specified in C99.
143 if (Result[Result.size()-1] == '\\') {
144 // Count the number of consequtive \ characters. If even, then they are
145 // just escaped backslashes, otherwise it's an error.
146 unsigned FirstNonSlash = Result.size()-2;
147 // Guaranteed to find the starting " if nothing else.
148 while (Result[FirstNonSlash] == '\\')
149 --FirstNonSlash;
150 if ((Result.size()-1-FirstNonSlash) & 1) {
Chris Lattnerf2781502006-07-15 05:27:44 +0000151 // Diagnose errors for things like: #define F(X) #X / F(\)
Chris Lattner36b6e812006-07-21 06:38:30 +0000152 PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
Chris Lattner0707bd32006-07-15 05:23:58 +0000153 Result.erase(Result.end()-1); // remove one of the \'s.
154 }
155 }
Chris Lattner0707bd32006-07-15 05:23:58 +0000156 Result += '"';
157
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000158 // If this is the charify operation and the result is not a legal character
159 // constant, diagnose it.
160 if (Charify) {
161 // First step, turn double quotes into single quotes:
162 Result[0] = '\'';
163 Result[Result.size()-1] = '\'';
164
165 // Check for bogus character.
166 bool isBad = false;
Chris Lattner2ada5d32006-07-15 07:51:24 +0000167 if (Result.size() == 3) {
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000168 isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
169 } else {
170 isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
171 }
172
173 if (isBad) {
Chris Lattner36b6e812006-07-21 06:38:30 +0000174 PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
Chris Lattner7c581492006-07-15 07:56:31 +0000175 Result = "' '"; // Use something arbitrary, but legal.
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000176 }
177 }
178
Chris Lattner0707bd32006-07-15 05:23:58 +0000179 Tok.SetLength(Result.size());
180 Tok.SetLocation(PP.CreateString(&Result[0], Result.size()));
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000181 return Tok;
182}
183
184/// getStringifiedArgument - Compute, cache, and return the specified argument
185/// that has been 'stringified' as required by the # operator.
Chris Lattneree8760b2006-07-15 07:42:55 +0000186const LexerToken &MacroArgs::getStringifiedArgument(unsigned ArgNo,
187 Preprocessor &PP) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000188 assert(ArgNo < UnexpArgTokens.size() && "Invalid argument number!");
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000189 if (StringifiedArgs.empty()) {
Chris Lattner2ada5d32006-07-15 07:51:24 +0000190 StringifiedArgs.resize(getNumArguments());
Chris Lattneree8760b2006-07-15 07:42:55 +0000191 memset(&StringifiedArgs[0], 0,
192 sizeof(StringifiedArgs[0])*getNumArguments());
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000193 }
194 if (StringifiedArgs[ArgNo].getKind() != tok::string_literal)
Chris Lattner36b6e812006-07-21 06:38:30 +0000195 StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000196 return StringifiedArgs[ArgNo];
197}
198
Chris Lattner78186052006-07-09 00:45:31 +0000199//===----------------------------------------------------------------------===//
200// MacroExpander Implementation
201//===----------------------------------------------------------------------===//
202
Chris Lattner7667d0d2006-07-16 18:16:58 +0000203/// Create a macro expander for the specified macro with the specified actual
204/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
Chris Lattneree8760b2006-07-15 07:42:55 +0000205MacroExpander::MacroExpander(LexerToken &Tok, MacroArgs *Actuals,
Chris Lattner78186052006-07-09 00:45:31 +0000206 Preprocessor &pp)
Chris Lattner7667d0d2006-07-16 18:16:58 +0000207 : Macro(Tok.getIdentifierInfo()->getMacroInfo()),
Chris Lattneree8760b2006-07-15 07:42:55 +0000208 ActualArgs(Actuals), PP(pp), CurToken(0),
Chris Lattner50b497e2006-06-18 16:32:35 +0000209 InstantiateLoc(Tok.getLocation()),
Chris Lattnerd01e2912006-06-18 16:22:51 +0000210 AtStartOfLine(Tok.isAtStartOfLine()),
211 HasLeadingSpace(Tok.hasLeadingSpace()) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000212 MacroTokens = &Macro->getReplacementTokens();
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000213
214 // If this is a function-like macro, expand the arguments and change
215 // MacroTokens to point to the expanded tokens.
Chris Lattner7667d0d2006-07-16 18:16:58 +0000216 if (Macro->isFunctionLike() && Macro->getNumArgs())
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000217 ExpandFunctionArguments();
Chris Lattner7667d0d2006-07-16 18:16:58 +0000218
219 // Mark the macro as currently disabled, so that it is not recursively
220 // expanded. The macro must be disabled only after argument pre-expansion of
221 // function-like macro arguments occurs.
222 Macro->DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000223}
224
Chris Lattner7667d0d2006-07-16 18:16:58 +0000225/// Create a macro expander for the specified token stream. This does not
226/// take ownership of the specified token vector.
227MacroExpander::MacroExpander(const std::vector<LexerToken> &TokStream,
228 Preprocessor &pp)
229 : Macro(0), ActualArgs(0), PP(pp), MacroTokens(&TokStream), CurToken(0),
230 InstantiateLoc(SourceLocation()), AtStartOfLine(false),
231 HasLeadingSpace(false) {
232
233 // Set HasLeadingSpace/AtStartOfLine so that the first token will be
234 // returned unmodified.
235 if (!TokStream.empty()) {
236 AtStartOfLine = TokStream[0].isAtStartOfLine();
237 HasLeadingSpace = TokStream[0].hasLeadingSpace();
238 }
239}
240
241
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000242MacroExpander::~MacroExpander() {
243 // If this was a function-like macro that actually uses its arguments, delete
244 // the expanded tokens.
Chris Lattner7667d0d2006-07-16 18:16:58 +0000245 if (Macro && MacroTokens != &Macro->getReplacementTokens())
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000246 delete MacroTokens;
247
248 // MacroExpander owns its formal arguments.
Chris Lattneree8760b2006-07-15 07:42:55 +0000249 delete ActualArgs;
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000250}
251
252/// Expand the arguments of a function-like macro so that we can quickly
253/// return preexpanded tokens from MacroTokens.
254void MacroExpander::ExpandFunctionArguments() {
255 std::vector<LexerToken> ResultToks;
256
257 // Loop through the MacroTokens tokens, expanding them into ResultToks. Keep
258 // track of whether we change anything. If not, no need to keep them. If so,
259 // we install the newly expanded sequence as MacroTokens.
260 bool MadeChange = false;
261 for (unsigned i = 0, e = MacroTokens->size(); i != e; ++i) {
262 // If we found the stringify operator, get the argument stringified. The
263 // preprocessor already verified that the following token is a macro name
264 // when the #define was parsed.
265 const LexerToken &CurTok = (*MacroTokens)[i];
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000266 if (CurTok.getKind() == tok::hash || CurTok.getKind() == tok::hashat) {
Chris Lattner7667d0d2006-07-16 18:16:58 +0000267 int ArgNo =Macro->getArgumentNum((*MacroTokens)[i+1].getIdentifierInfo());
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000268 assert(ArgNo != -1 && "Token following # is not an argument?");
269
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000270 if (CurTok.getKind() == tok::hash) // Stringify
Chris Lattneree8760b2006-07-15 07:42:55 +0000271 ResultToks.push_back(ActualArgs->getStringifiedArgument(ArgNo, PP));
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000272 else {
273 // 'charify': don't bother caching these.
274 ResultToks.push_back(StringifyArgument(
Chris Lattneree8760b2006-07-15 07:42:55 +0000275 ActualArgs->getUnexpArgument(ArgNo), PP, true));
Chris Lattnerc783d1d2006-07-15 06:11:25 +0000276 }
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000277
Chris Lattner60161692006-07-15 06:48:02 +0000278 // The stringified/charified string leading space flag gets set to match
279 // the #/#@ operator.
280 if (CurTok.hasLeadingSpace())
281 ResultToks.back().SetFlag(LexerToken::LeadingSpace);
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000282
283 MadeChange = true;
284 ++i; // Skip arg name.
285 } else {
Chris Lattner203b4562006-07-15 21:07:40 +0000286 // Otherwise, if this is not an argument token, just add the token to the
287 // output buffer.
288 IdentifierInfo *II = CurTok.getIdentifierInfo();
Chris Lattner7667d0d2006-07-16 18:16:58 +0000289 int ArgNo = II ? Macro->getArgumentNum(II) : -1;
Chris Lattner203b4562006-07-15 21:07:40 +0000290 if (ArgNo == -1) {
291 ResultToks.push_back(CurTok);
292 continue;
293 }
294
295 // An argument is expanded somehow, the result is different than the
296 // input.
297 MadeChange = true;
298
299 // Otherwise, this is a use of the argument. Find out if there is a paste
300 // (##) operator before or after the argument.
301 bool PasteBefore =
302 !ResultToks.empty() && ResultToks.back().getKind() == tok::hashhash;
303 bool PasteAfter =
304 i+1 != e && (*MacroTokens)[i+1].getKind() == tok::hashhash;
305
306 // If it is not the LHS/RHS of a ## operator, we must pre-expand the
307 // argument and substitute the expanded tokens into the result. This is
308 // C99 6.10.3.1p1.
309 if (!PasteBefore && !PasteAfter) {
Chris Lattner36b6e812006-07-21 06:38:30 +0000310 const LexerToken *ResultArgToks;
311
Chris Lattner203b4562006-07-15 21:07:40 +0000312 // Only preexpand the argument if it could possibly need it. This
313 // avoids some work in common cases.
Chris Lattner36b6e812006-07-21 06:38:30 +0000314 const LexerToken *ArgTok = ActualArgs->getUnexpArgument(ArgNo);
315 if (ActualArgs->ArgNeedsPreexpansion(ArgTok))
316 ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0];
Chris Lattner7667d0d2006-07-16 18:16:58 +0000317 else
Chris Lattner36b6e812006-07-21 06:38:30 +0000318 ResultArgToks = ArgTok; // Use non-preexpanded tokens.
Chris Lattner203b4562006-07-15 21:07:40 +0000319
Chris Lattner36b6e812006-07-21 06:38:30 +0000320 if (ResultArgToks->getKind() != tok::eof) {
321 unsigned FirstResult = ResultToks.size();
322 for (; ResultArgToks->getKind() != tok::eof; ++ResultArgToks)
323 ResultToks.push_back(*ResultArgToks);
Chris Lattner203b4562006-07-15 21:07:40 +0000324
Chris Lattner36b6e812006-07-21 06:38:30 +0000325 // If any tokens were substituted from the argument, the whitespace
326 // before the first token should match the whitespace of the arg
327 // identifier.
328 ResultToks[FirstResult].SetFlagValue(LexerToken::LeadingSpace,
329 CurTok.hasLeadingSpace());
330 }
Chris Lattner203b4562006-07-15 21:07:40 +0000331 continue;
332 }
333
Chris Lattner7e2e6692006-07-19 03:51:26 +0000334 // Okay, we have a token that is either the LHS or RHS of a paste (##)
335 // argument. It gets substituted as its non-pre-expanded tokens.
Chris Lattner36b6e812006-07-21 06:38:30 +0000336 const LexerToken *ArgToks = ActualArgs->getUnexpArgument(ArgNo);
Chris Lattner7e2e6692006-07-19 03:51:26 +0000337
Chris Lattner36b6e812006-07-21 06:38:30 +0000338 if (ArgToks->getKind() != tok::eof) { // Not an empty argument?
339 for (; ArgToks->getKind() != tok::eof; ++ArgToks)
340 ResultToks.push_back(*ArgToks);
Chris Lattner7e2e6692006-07-19 03:51:26 +0000341 continue;
342 }
Chris Lattner7667d0d2006-07-16 18:16:58 +0000343
Chris Lattner7e2e6692006-07-19 03:51:26 +0000344 // FIXME: Handle comma swallowing GNU extension.
345 // FIXME: Handle 'placemarker' stuff.
346 assert(0 && "FIXME: handle empty arguments!");
347 //ResultToks.push_back(CurTok);
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000348 }
349 }
350
351 // If anything changed, install this as the new MacroTokens list.
352 if (MadeChange) {
353 // This is deleted in the dtor.
354 std::vector<LexerToken> *Res = new std::vector<LexerToken>();
355 Res->swap(ResultToks);
356 MacroTokens = Res;
357 }
358}
Chris Lattner67b07cb2006-06-26 02:03:42 +0000359
Chris Lattner22eb9722006-06-18 05:43:12 +0000360/// Lex - Lex and return a token from this macro stream.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000361///
Chris Lattnercb283342006-06-18 06:48:37 +0000362void MacroExpander::Lex(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000363 // Lexing off the end of the macro, pop this macro off the expansion stack.
Chris Lattner7667d0d2006-07-16 18:16:58 +0000364 if (isAtEnd()) {
365 // If this is a macro (not a token stream), mark the macro enabled now
366 // that it is no longer being expanded.
367 if (Macro) Macro->EnableMacro();
368
369 // Pop this context off the preprocessors lexer stack and get the next
Chris Lattner2183a6e2006-07-18 06:36:12 +0000370 // token. This will delete "this" so remember the PP instance var.
371 Preprocessor &PPCache = PP;
372 if (PP.HandleEndOfMacro(Tok))
373 return;
374
375 // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is
376 // next.
377 return PPCache.Lex(Tok);
Chris Lattner7667d0d2006-07-16 18:16:58 +0000378 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000379
Chris Lattnere8dcfef2006-07-19 05:45:55 +0000380 // If this is the first token of the expanded result, we inherit spacing
381 // properties later.
382 bool isFirstToken = CurToken == 0;
383
Chris Lattner22eb9722006-06-18 05:43:12 +0000384 // Get the next token to return.
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000385 Tok = (*MacroTokens)[CurToken++];
Chris Lattner01ecf832006-07-19 05:42:48 +0000386
387 // If this token is followed by a token paste (##) operator, paste the tokens!
388 if (!isAtEnd() && (*MacroTokens)[CurToken].getKind() == tok::hashhash)
389 PasteTokens(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000390
Chris Lattnerc673f902006-06-30 06:10:41 +0000391 // The token's current location indicate where the token was lexed from. We
392 // need this information to compute the spelling of the token, but any
393 // diagnostics for the expanded token should appear as if they came from
394 // InstantiationLoc. Pull this information together into a new SourceLocation
395 // that captures all of this.
Chris Lattner7667d0d2006-07-16 18:16:58 +0000396 if (InstantiateLoc.isValid()) { // Don't do this for token streams.
397 SourceManager &SrcMgr = PP.getSourceManager();
398 // The token could have come from a prior macro expansion. In that case,
399 // ignore the macro expand part to get to the physloc. This happens for
400 // stuff like: #define A(X) X A(A(X)) A(1)
401 SourceLocation PhysLoc = SrcMgr.getPhysicalLoc(Tok.getLocation());
402 Tok.SetLocation(SrcMgr.getInstantiationLoc(PhysLoc, InstantiateLoc));
403 }
404
Chris Lattner22eb9722006-06-18 05:43:12 +0000405 // If this is the first token, set the lexical properties of the token to
406 // match the lexical properties of the macro identifier.
Chris Lattnere8dcfef2006-07-19 05:45:55 +0000407 if (isFirstToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000408 Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine);
409 Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace);
410 }
411
412 // Handle recursive expansion!
413 if (Tok.getIdentifierInfo())
414 return PP.HandleIdentifier(Tok);
415
416 // Otherwise, return a normal token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000417}
Chris Lattnerafe603f2006-07-11 04:02:46 +0000418
Chris Lattner01ecf832006-07-19 05:42:48 +0000419/// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ##
420/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
421/// are is another ## after it, chomp it iteratively. Return the result as Tok.
422void MacroExpander::PasteTokens(LexerToken &Tok) {
423 do {
424 // Consume the ## operator.
425 SourceLocation PasteOpLoc = (*MacroTokens)[CurToken].getLocation();
426 ++CurToken;
427 assert(!isAtEnd() && "No token on the RHS of a paste operator!");
428
429 // Get the RHS token.
430 const LexerToken &RHS = (*MacroTokens)[CurToken];
431
432 bool isInvalid = false;
433
Chris Lattner01ecf832006-07-19 05:42:48 +0000434 // Allocate space for the result token. This is guaranteed to be enough for
435 // the two tokens and a null terminator.
436 char *Buffer = (char*)alloca(Tok.getLength() + RHS.getLength() + 1);
437
438 // Get the spelling of the LHS token in Buffer.
439 const char *BufPtr = Buffer;
440 unsigned LHSLen = PP.getSpelling(Tok, BufPtr);
441 if (BufPtr != Buffer) // Really, we want the chars in Buffer!
442 memcpy(Buffer, BufPtr, LHSLen);
443
444 BufPtr = Buffer+LHSLen;
445 unsigned RHSLen = PP.getSpelling(RHS, BufPtr);
446 if (BufPtr != Buffer+LHSLen) // Really, we want the chars in Buffer!
447 memcpy(Buffer+LHSLen, BufPtr, RHSLen);
448
449 // Add null terminator.
450 Buffer[LHSLen+RHSLen] = '\0';
451
452 // Plop the pasted result (including the trailing newline and null) into a
453 // scratch buffer where we can lex it.
454 SourceLocation ResultTokLoc = PP.CreateString(Buffer, LHSLen+RHSLen+1);
455
456 // Lex the resultant pasted token into Result.
457 LexerToken Result;
458
Chris Lattnera7e2e742006-07-19 06:32:35 +0000459 // Avoid testing /*, as the lexer would think it is the start of a comment
460 // and emit an error that it is unterminated.
461 if (Tok.getKind() == tok::slash && RHS.getKind() == tok::star) {
462 isInvalid = true;
Chris Lattner510ab612006-07-20 04:47:30 +0000463 } else if (Tok.getKind() == tok::identifier &&
Chris Lattner08ba4c02006-07-20 04:52:59 +0000464 RHS.getKind() == tok::identifier) {
Chris Lattner510ab612006-07-20 04:47:30 +0000465 // Common paste case: identifier+identifier = identifier. Avoid creating
466 // a lexer and other overhead.
467 PP.IncrementPasteCounter(true);
468 Result.StartToken();
469 Result.SetKind(tok::identifier);
470 Result.SetLocation(ResultTokLoc);
471 Result.SetLength(LHSLen+RHSLen);
Chris Lattnera7e2e742006-07-19 06:32:35 +0000472 } else {
Chris Lattner510ab612006-07-20 04:47:30 +0000473 PP.IncrementPasteCounter(false);
474
Chris Lattnera7e2e742006-07-19 06:32:35 +0000475 // Make a lexer to lex this string from.
476 SourceManager &SourceMgr = PP.getSourceManager();
477 const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc);
478
479 unsigned FileID = ResultTokLoc.getFileID();
480 assert(FileID && "Could not get FileID for paste?");
481
482 // Make a lexer object so that we lex and expand the paste result.
483 Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, PP,
484 ResultStrData,
485 ResultStrData+LHSLen+RHSLen /*don't include null*/);
486
487 // Lex a token in raw mode. This way it won't look up identifiers
488 // automatically, lexing off the end will return an eof token, and
489 // warnings are disabled. This returns true if the result token is the
490 // entire buffer.
491 bool IsComplete = TL->LexRawToken(Result);
492
493 // If we got an EOF token, we didn't form even ONE token. For example, we
494 // did "/ ## /" to get "//".
495 IsComplete &= Result.getKind() != tok::eof;
496 isInvalid = !IsComplete;
497
498 // We're now done with the temporary lexer.
499 delete TL;
500 }
Chris Lattner01ecf832006-07-19 05:42:48 +0000501
502 // If pasting the two tokens didn't form a full new token, this is an error.
Chris Lattnera7e2e742006-07-19 06:32:35 +0000503 // This occurs with "x ## +" and other stuff. Return with Tok unmodified
504 // and with RHS as the next token to lex.
505 if (isInvalid) {
Chris Lattner01ecf832006-07-19 05:42:48 +0000506 // If not in assembler language mode.
507 PP.Diag(PasteOpLoc, diag::err_pp_bad_paste,
508 std::string(Buffer, Buffer+LHSLen+RHSLen));
509 return;
510 }
511
512 // Turn ## into 'other' to avoid # ## # from looking like a paste operator.
513 if (Result.getKind() == tok::hashhash)
514 Result.SetKind(tok::unknown);
515 // FIXME: Turn __VARRGS__ into "not a token"?
516
517 // Transfer properties of the LHS over the the Result.
518 Result.SetFlagValue(LexerToken::StartOfLine , Tok.isAtStartOfLine());
519 Result.SetFlagValue(LexerToken::LeadingSpace, Tok.hasLeadingSpace());
520
521 // Finally, replace LHS with the result, consume the RHS, and iterate.
522 ++CurToken;
523 Tok = Result;
524 } while (!isAtEnd() && (*MacroTokens)[CurToken].getKind() == tok::hashhash);
Chris Lattner0f1f5052006-07-20 04:16:23 +0000525
526 // Now that we got the result token, it will be subject to expansion. Since
527 // token pasting re-lexes the result token in raw mode, identifier information
528 // isn't looked up. As such, if the result is an identifier, look up id info.
529 if (Tok.getKind() == tok::identifier) {
530 // Look up the identifier info for the token. We disabled identifier lookup
531 // by saying we're skipping contents, so we need to do this manually.
532 Tok.SetIdentifierInfo(PP.LookUpIdentifierInfo(Tok));
533 }
Chris Lattner01ecf832006-07-19 05:42:48 +0000534}
535
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000536/// isNextTokenLParen - If the next token lexed will pop this macro off the
537/// expansion stack, return 2. If the next unexpanded token is a '(', return
538/// 1, otherwise return 0.
539unsigned MacroExpander::isNextTokenLParen() const {
Chris Lattnerafe603f2006-07-11 04:02:46 +0000540 // Out of tokens?
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000541 if (isAtEnd())
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000542 return 2;
Chris Lattnerb935d8c2006-07-14 06:54:44 +0000543 return (*MacroTokens)[CurToken].getKind() == tok::l_paren;
Chris Lattnerafe603f2006-07-11 04:02:46 +0000544}