blob: 286705181ccedf2a66173b21be0d48b6f53ca91f [file] [log] [blame]
Chris Lattnera3b605e2008-03-09 03:13:06 +00001//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the top level handling of macro expasion for the
11// preprocessor.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Preprocessor.h"
16#include "MacroArgs.h"
17#include "clang/Lex/MacroInfo.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
Chris Lattner500d3292009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Chris Lattner3daed522009-03-02 22:20:04 +000021#include <cstdio>
Chris Lattnerf90a2482008-03-18 05:59:11 +000022#include <ctime>
Chris Lattnera3b605e2008-03-09 03:13:06 +000023using namespace clang;
24
25/// setMacroInfo - Specify a macro for this identifier.
26///
27void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
Chris Lattner555589d2009-04-10 21:17:07 +000028 if (MI) {
Chris Lattnera3b605e2008-03-09 03:13:06 +000029 Macros[II] = MI;
30 II->setHasMacroDefinition(true);
Chris Lattner555589d2009-04-10 21:17:07 +000031 } else if (II->hasMacroDefinition()) {
32 Macros.erase(II);
33 II->setHasMacroDefinition(false);
Chris Lattnera3b605e2008-03-09 03:13:06 +000034 }
35}
36
37/// RegisterBuiltinMacro - Register the specified identifier in the identifier
38/// table and mark it as a builtin macro to be expanded.
Chris Lattner148772a2009-06-13 07:13:28 +000039static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
Chris Lattnera3b605e2008-03-09 03:13:06 +000040 // Get the identifier.
Chris Lattner148772a2009-06-13 07:13:28 +000041 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
Chris Lattnera3b605e2008-03-09 03:13:06 +000042
43 // Mark it as being a macro that is builtin.
Chris Lattner148772a2009-06-13 07:13:28 +000044 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +000045 MI->setIsBuiltinMacro();
Chris Lattner148772a2009-06-13 07:13:28 +000046 PP.setMacroInfo(Id, MI);
Chris Lattnera3b605e2008-03-09 03:13:06 +000047 return Id;
48}
49
50
51/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
52/// identifier table.
53void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner148772a2009-06-13 07:13:28 +000054 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
55 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
56 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
57 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
58 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
59 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
Chris Lattnera3b605e2008-03-09 03:13:06 +000060
61 // GCC Extensions.
Chris Lattner148772a2009-06-13 07:13:28 +000062 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
63 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
64 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
65
66 // Clang Extensions.
67 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
68 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
Chris Lattnera3b605e2008-03-09 03:13:06 +000069}
70
71/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
72/// in its expansion, currently expands to that token literally.
73static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
74 const IdentifierInfo *MacroIdent,
75 Preprocessor &PP) {
76 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
77
78 // If the token isn't an identifier, it's always literally expanded.
79 if (II == 0) return true;
80
81 // If the identifier is a macro, and if that macro is enabled, it may be
82 // expanded so it's not a trivial expansion.
83 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
84 // Fast expanding "#define X X" is ok, because X would be disabled.
85 II != MacroIdent)
86 return false;
87
88 // If this is an object-like macro invocation, it is safe to trivially expand
89 // it.
90 if (MI->isObjectLike()) return true;
91
92 // If this is a function-like macro invocation, it's safe to trivially expand
93 // as long as the identifier is not a macro argument.
94 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
95 I != E; ++I)
96 if (*I == II)
97 return false; // Identifier is a macro argument.
98
99 return true;
100}
101
102
103/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
104/// lexed is a '('. If so, consume the token and return true, if not, this
105/// method should have no observable side-effect on the lexed tokens.
106bool Preprocessor::isNextPPTokenLParen() {
107 // Do some quick tests for rejection cases.
108 unsigned Val;
109 if (CurLexer)
110 Val = CurLexer->isNextPPTokenLParen();
Ted Kremenek1a531572008-11-19 22:43:49 +0000111 else if (CurPTHLexer)
112 Val = CurPTHLexer->isNextPPTokenLParen();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000113 else
114 Val = CurTokenLexer->isNextTokenLParen();
115
116 if (Val == 2) {
117 // We have run off the end. If it's a source file we don't
118 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
119 // macro stack.
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000120 if (CurPPLexer)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000121 return false;
122 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
123 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
124 if (Entry.TheLexer)
125 Val = Entry.TheLexer->isNextPPTokenLParen();
Ted Kremenekdd95d6c2008-11-20 16:46:54 +0000126 else if (Entry.ThePTHLexer)
127 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000128 else
129 Val = Entry.TheTokenLexer->isNextTokenLParen();
130
131 if (Val != 2)
132 break;
133
134 // Ran off the end of a source file?
Ted Kremenekdd95d6c2008-11-20 16:46:54 +0000135 if (Entry.ThePPLexer)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000136 return false;
137 }
138 }
139
140 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
141 // have found something that isn't a '(' or we found the end of the
142 // translation unit. In either case, return false.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000143 return Val == 1;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000144}
145
146/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
147/// expanded as a macro, handle it and return the next token as 'Identifier'.
148bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
149 MacroInfo *MI) {
Chris Lattnerba9eee32009-03-12 17:31:43 +0000150 if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
151
Chris Lattnera3b605e2008-03-09 03:13:06 +0000152 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
153 // then the macro could expand to different things in other contexts, we need
154 // to disable the optimization in this case.
Ted Kremenek68a91d52008-11-18 01:12:54 +0000155 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000156
157 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
158 if (MI->isBuiltinMacro()) {
159 ExpandBuiltinMacro(Identifier);
160 return false;
161 }
162
163 /// Args - If this is a function-like macro expansion, this contains,
164 /// for each macro argument, the list of tokens that were provided to the
165 /// invocation.
166 MacroArgs *Args = 0;
167
Chris Lattnere7fb4842009-02-15 20:52:18 +0000168 // Remember where the end of the instantiation occurred. For an object-like
169 // macro, this is the identifier. For a function-like macro, this is the ')'.
170 SourceLocation InstantiationEnd = Identifier.getLocation();
171
Chris Lattnera3b605e2008-03-09 03:13:06 +0000172 // If this is a function-like macro, read the arguments.
173 if (MI->isFunctionLike()) {
174 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000175 // name isn't a '(', this macro should not be expanded.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000176 if (!isNextPPTokenLParen())
177 return true;
178
179 // Remember that we are now parsing the arguments to a macro invocation.
180 // Preprocessor directives used inside macro arguments are not portable, and
181 // this enables the warning.
182 InMacroArgs = true;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000183 Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000184
185 // Finished parsing args.
186 InMacroArgs = false;
187
188 // If there was an error parsing the arguments, bail out.
189 if (Args == 0) return false;
190
191 ++NumFnMacroExpanded;
192 } else {
193 ++NumMacroExpanded;
194 }
195
196 // Notice that this macro has been used.
197 MI->setIsUsed(true);
198
199 // If we started lexing a macro, enter the macro expansion body.
200
201 // If this macro expands to no tokens, don't bother to push it onto the
202 // expansion stack, only to take it right back off.
203 if (MI->getNumTokens() == 0) {
204 // No need for arg info.
205 if (Args) Args->destroy();
206
207 // Ignore this macro use, just return the next token in the current
208 // buffer.
209 bool HadLeadingSpace = Identifier.hasLeadingSpace();
210 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
211
212 Lex(Identifier);
213
214 // If the identifier isn't on some OTHER line, inherit the leading
215 // whitespace/first-on-a-line property of this token. This handles
216 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
217 // empty.
218 if (!Identifier.isAtStartOfLine()) {
219 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
220 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
221 }
222 ++NumFastMacroExpanded;
223 return false;
224
225 } else if (MI->getNumTokens() == 1 &&
226 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227 *this)) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000228 // Otherwise, if this macro expands into a single trivially-expanded
229 // token: expand it now. This handles common cases like
230 // "#define VAL 42".
Sam Bishop9a4939f2008-03-21 07:13:02 +0000231
232 // No need for arg info.
233 if (Args) Args->destroy();
234
Chris Lattnera3b605e2008-03-09 03:13:06 +0000235 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
236 // identifier to the expanded token.
237 bool isAtStartOfLine = Identifier.isAtStartOfLine();
238 bool hasLeadingSpace = Identifier.hasLeadingSpace();
239
240 // Remember where the token is instantiated.
241 SourceLocation InstantiateLoc = Identifier.getLocation();
242
243 // Replace the result token.
244 Identifier = MI->getReplacementToken(0);
245
246 // Restore the StartOfLine/LeadingSpace markers.
247 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
248 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
249
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000250 // Update the tokens location to include both its instantiation and physical
Chris Lattnera3b605e2008-03-09 03:13:06 +0000251 // locations.
252 SourceLocation Loc =
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000253 SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000254 InstantiationEnd,Identifier.getLength());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000255 Identifier.setLocation(Loc);
256
257 // If this is #define X X, we must mark the result as unexpandible.
258 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
259 if (getMacroInfo(NewII) == MI)
260 Identifier.setFlag(Token::DisableExpand);
261
262 // Since this is not an identifier token, it can't be macro expanded, so
263 // we're done.
264 ++NumFastMacroExpanded;
265 return false;
266 }
267
268 // Start expanding the macro.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000269 EnterMacro(Identifier, InstantiationEnd, Args);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000270
271 // Now that the macro is at the top of the include stack, ask the
272 // preprocessor to read the next token from it.
273 Lex(Identifier);
274 return false;
275}
276
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000277/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
278/// token is the '(' of the macro, this method is invoked to read all of the
279/// actual arguments specified for the macro invocation. This returns null on
280/// error.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000281MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000282 MacroInfo *MI,
283 SourceLocation &MacroEnd) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000284 // The number of fixed arguments to parse.
285 unsigned NumFixedArgsLeft = MI->getNumArgs();
286 bool isVariadic = MI->isVariadic();
287
288 // Outer loop, while there are more arguments, keep reading them.
289 Token Tok;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000290
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000291 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
292 // an argument value in a macro could expand to ',' or '(' or ')'.
293 LexUnexpandedToken(Tok);
294 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
295
Chris Lattnera3b605e2008-03-09 03:13:06 +0000296 // ArgTokens - Build up a list of tokens that make up each argument. Each
297 // argument is separated by an EOF token. Use a SmallVector so we can avoid
298 // heap allocations in the common case.
299 llvm::SmallVector<Token, 64> ArgTokens;
300
301 unsigned NumActuals = 0;
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000302 while (Tok.isNot(tok::r_paren)) {
303 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
304 "only expect argument separators here");
305
306 unsigned ArgTokenStart = ArgTokens.size();
307 SourceLocation ArgStartLoc = Tok.getLocation();
308
Chris Lattnera3b605e2008-03-09 03:13:06 +0000309 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
310 // that we already consumed the first one.
311 unsigned NumParens = 0;
312
313 while (1) {
314 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
315 // an argument value in a macro could expand to ',' or '(' or ')'.
316 LexUnexpandedToken(Tok);
317
318 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
319 Diag(MacroName, diag::err_unterm_macro_invoc);
320 // Do not lose the EOF/EOM. Return it to the client.
321 MacroName = Tok;
322 return 0;
323 } else if (Tok.is(tok::r_paren)) {
324 // If we found the ) token, the macro arg list is done.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000325 if (NumParens-- == 0) {
326 MacroEnd = Tok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000327 break;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000328 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000329 } else if (Tok.is(tok::l_paren)) {
330 ++NumParens;
331 } else if (Tok.is(tok::comma) && NumParens == 0) {
332 // Comma ends this argument if there are more fixed arguments expected.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000333 // However, if this is a variadic macro, and this is part of the
334 // variadic part, then the comma is just an argument token.
335 if (!isVariadic) break;
336 if (NumFixedArgsLeft > 1)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000337 break;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000338 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
339 // If this is a comment token in the argument list and we're just in
340 // -C mode (not -CC mode), discard the comment.
341 continue;
Chris Lattner5c497a82009-04-18 06:44:18 +0000342 } else if (Tok.getIdentifierInfo() != 0) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000343 // Reading macro arguments can cause macros that we are currently
344 // expanding from to be popped off the expansion stack. Doing so causes
345 // them to be reenabled for expansion. Here we record whether any
346 // identifiers we lex as macro arguments correspond to disabled macros.
347 // If so, we mark the token as noexpand. This is a subtle aspect of
348 // C99 6.10.3.4p2.
349 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
350 if (!MI->isEnabled())
351 Tok.setFlag(Token::DisableExpand);
352 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000353 ArgTokens.push_back(Tok);
354 }
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000355
356 // If this was an empty argument list foo(), don't add this as an empty
357 // argument.
358 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
359 break;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000360
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000361 // If this is not a variadic macro, and too many args were specified, emit
362 // an error.
363 if (!isVariadic && NumFixedArgsLeft == 0) {
364 if (ArgTokens.size() != ArgTokenStart)
365 ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
366
367 // Emit the diagnostic at the macro name in case there is a missing ).
368 // Emitting it at the , could be far away from the macro name.
369 Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
370 return 0;
371 }
372
Chris Lattnera3b605e2008-03-09 03:13:06 +0000373 // Empty arguments are standard in C99 and supported as an extension in
374 // other modes.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000375 if (ArgTokens.size() == ArgTokenStart && !Features.C99)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000376 Diag(Tok, diag::ext_empty_fnmacro_arg);
377
378 // Add a marker EOF token to the end of the token list for this argument.
379 Token EOFTok;
380 EOFTok.startToken();
381 EOFTok.setKind(tok::eof);
Chris Lattnere7689882009-01-26 20:24:53 +0000382 EOFTok.setLocation(Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000383 EOFTok.setLength(0);
384 ArgTokens.push_back(EOFTok);
385 ++NumActuals;
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000386 assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
Chris Lattnera3b605e2008-03-09 03:13:06 +0000387 --NumFixedArgsLeft;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000388 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000389
390 // Okay, we either found the r_paren. Check to see if we parsed too few
391 // arguments.
392 unsigned MinArgsExpected = MI->getNumArgs();
393
394 // See MacroArgs instance var for description of this.
395 bool isVarargsElided = false;
396
397 if (NumActuals < MinArgsExpected) {
398 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner97e2de12009-04-20 21:08:10 +0000399 if (NumActuals == 0 && MinArgsExpected == 1) {
400 // #define A(X) or #define A(...) ---> A()
401
402 // If there is exactly one argument, and that argument is missing,
403 // then we have an empty "()" argument empty list. This is fine, even if
404 // the macro expects one argument (the argument is just empty).
405 isVarargsElided = MI->isVariadic();
406 } else if (MI->isVariadic() &&
407 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
408 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
Chris Lattnera3b605e2008-03-09 03:13:06 +0000409 // Varargs where the named vararg parameter is missing: ok as extension.
410 // #define A(x, ...)
411 // A("blah")
412 Diag(Tok, diag::ext_missing_varargs_arg);
413
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000414 // Remember this occurred, allowing us to elide the comma when used for
Chris Lattner63bc0352008-05-08 05:10:33 +0000415 // cases like:
416 // #define A(x, foo...) blah(a, ## foo)
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000417 // #define B(x, ...) blah(a, ## __VA_ARGS__)
418 // #define C(...) blah(a, ## __VA_ARGS__)
419 // A(x) B(x) C()
Chris Lattner97e2de12009-04-20 21:08:10 +0000420 isVarargsElided = true;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000421 } else {
422 // Otherwise, emit the error.
423 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
424 return 0;
425 }
426
427 // Add a marker EOF token to the end of the token list for this argument.
428 SourceLocation EndLoc = Tok.getLocation();
429 Tok.startToken();
430 Tok.setKind(tok::eof);
431 Tok.setLocation(EndLoc);
432 Tok.setLength(0);
433 ArgTokens.push_back(Tok);
Chris Lattner9fc9e772009-05-13 00:55:26 +0000434
435 // If we expect two arguments, add both as empty.
436 if (NumActuals == 0 && MinArgsExpected == 2)
437 ArgTokens.push_back(Tok);
438
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000439 } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
440 // Emit the diagnostic at the macro name in case there is a missing ).
441 // Emitting it at the , could be far away from the macro name.
442 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
443 return 0;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000444 }
445
Jay Foadbeaaccd2009-05-21 09:52:38 +0000446 return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
447 isVarargsElided);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000448}
449
450/// ComputeDATE_TIME - Compute the current time, enter it into the specified
451/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
452/// the identifier tokens inserted.
453static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
454 Preprocessor &PP) {
455 time_t TT = time(0);
456 struct tm *TM = localtime(&TT);
457
458 static const char * const Months[] = {
459 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
460 };
461
462 char TmpBuffer[100];
463 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
464 TM->tm_year+1900);
Chris Lattner47246be2009-01-26 19:29:26 +0000465
466 Token TmpTok;
467 TmpTok.startToken();
468 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
469 DATELoc = TmpTok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000470
471 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattner47246be2009-01-26 19:29:26 +0000472 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
473 TIMELoc = TmpTok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000474}
475
Chris Lattner148772a2009-06-13 07:13:28 +0000476
477/// HasFeature - Return true if we recognize and implement the specified feature
478/// specified by the identifier.
479static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
480 const LangOptions &LangOpts = PP.getLangOptions();
481
482 switch (II->getLength()) {
483 default: return false;
484 case 6:
485 if (II->isStr("blocks")) return LangOpts.Blocks;
486 return false;
487 case 22:
488 if (II->isStr("attribute_overloadable")) return true;
489 return false;
490 case 25:
491 if (II->isStr("attribute_ext_vector_type")) return true;
492 return false;
493 case 27:
494 if (II->isStr("attribute_analyzer_noreturn")) return true;
495 return false;
496 case 29:
497 if (II->isStr("attribute_ns_returns_retained")) return true;
498 if (II->isStr("attribute_cf_returns_retained")) return true;
499 return false;
500 }
501}
502
503
Chris Lattnera3b605e2008-03-09 03:13:06 +0000504/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
505/// as a builtin macro, handle it and return the next token as 'Tok'.
506void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
507 // Figure out which token this is.
508 IdentifierInfo *II = Tok.getIdentifierInfo();
509 assert(II && "Can't be a macro without id info!");
510
511 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
512 // lex the token after it.
513 if (II == Ident_Pragma)
514 return Handle_Pragma(Tok);
515
516 ++NumBuiltinMacroExpanded;
517
518 char TmpBuffer[100];
519
520 // Set up the return result.
521 Tok.setIdentifierInfo(0);
522 Tok.clearFlag(Token::NeedsCleaning);
523
524 if (II == Ident__LINE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000525 // C99 6.10.8: "__LINE__: The presumed line number (within the current
526 // source file) of the current source line (an integer constant)". This can
527 // be affected by #line.
Chris Lattner081927b2009-02-15 21:06:39 +0000528 SourceLocation Loc = Tok.getLocation();
529
Chris Lattnerdff070f2009-04-18 22:29:33 +0000530 // Advance to the location of the first _, this might not be the first byte
531 // of the token if it starts with an escaped newline.
532 Loc = AdvanceToTokenCharacter(Loc, 0);
533
Chris Lattner081927b2009-02-15 21:06:39 +0000534 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
535 // a macro instantiation. This doesn't matter for object-like macros, but
536 // can matter for a function-like macro that expands to contain __LINE__.
537 // Skip down through instantiation points until we find a file loc for the
538 // end of the instantiation history.
Chris Lattner66781332009-02-15 21:26:50 +0000539 Loc = SourceMgr.getInstantiationRange(Loc).second;
Chris Lattner081927b2009-02-15 21:06:39 +0000540 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000541
Chris Lattner1fa49532009-03-08 08:08:45 +0000542 // __LINE__ expands to a simple numeric value.
543 sprintf(TmpBuffer, "%u", PLoc.getLine());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000544 Tok.setKind(tok::numeric_constant);
Chris Lattner1fa49532009-03-08 08:08:45 +0000545 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000546 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000547 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
548 // character string literal)". This can be affected by #line.
549 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
550
551 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
552 // #include stack instead of the current file.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000553 if (II == Ident__BASE_FILE__) {
554 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000555 SourceLocation NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000556 while (NextLoc.isValid()) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000557 PLoc = SourceMgr.getPresumedLoc(NextLoc);
558 NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000559 }
560 }
561
562 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000563 std::string FN = PLoc.getFilename();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000564 FN = '"' + Lexer::Stringify(FN) + '"';
565 Tok.setKind(tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000566 CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000567 } else if (II == Ident__DATE__) {
568 if (!DATELoc.isValid())
569 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
570 Tok.setKind(tok::string_literal);
571 Tok.setLength(strlen("\"Mmm dd yyyy\""));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000572 Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000573 Tok.getLocation(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000574 Tok.getLength()));
Chris Lattnera3b605e2008-03-09 03:13:06 +0000575 } else if (II == Ident__TIME__) {
576 if (!TIMELoc.isValid())
577 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
578 Tok.setKind(tok::string_literal);
579 Tok.setLength(strlen("\"hh:mm:ss\""));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000580 Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000581 Tok.getLocation(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000582 Tok.getLength()));
Chris Lattnera3b605e2008-03-09 03:13:06 +0000583 } else if (II == Ident__INCLUDE_LEVEL__) {
584 Diag(Tok, diag::ext_pp_include_level);
585
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000586 // Compute the presumed include depth of this token. This can be affected
587 // by GNU line markers.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000588 unsigned Depth = 0;
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000589
590 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
591 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
592 for (; PLoc.isValid(); ++Depth)
593 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000594
Chris Lattner1fa49532009-03-08 08:08:45 +0000595 // __INCLUDE_LEVEL__ expands to a simple numeric value.
596 sprintf(TmpBuffer, "%u", Depth);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000597 Tok.setKind(tok::numeric_constant);
Chris Lattner1fa49532009-03-08 08:08:45 +0000598 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000599 } else if (II == Ident__TIMESTAMP__) {
600 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
601 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
602 Diag(Tok, diag::ext_pp_timestamp);
603
604 // Get the file that we are lexing out of. If we're currently lexing from
605 // a macro, dig into the include stack.
606 const FileEntry *CurFile = 0;
Ted Kremeneka275a192008-11-20 01:35:24 +0000607 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000608
609 if (TheLexer)
Ted Kremenekac80c6e2008-11-19 22:55:25 +0000610 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000611
612 // If this file is older than the file it depends on, emit a diagnostic.
613 const char *Result;
614 if (CurFile) {
615 time_t TT = CurFile->getModificationTime();
616 struct tm *TM = localtime(&TT);
617 Result = asctime(TM);
618 } else {
619 Result = "??? ??? ?? ??:??:?? ????\n";
620 }
621 TmpBuffer[0] = '"';
622 strcpy(TmpBuffer+1, Result);
623 unsigned Len = strlen(TmpBuffer);
Chris Lattner1fa49532009-03-08 08:08:45 +0000624 TmpBuffer[Len] = '"'; // Replace the newline with a quote.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000625 Tok.setKind(tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000626 CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000627 } else if (II == Ident__COUNTER__) {
628 Diag(Tok, diag::ext_pp_counter);
629
630 // __COUNTER__ expands to a simple numeric value.
631 sprintf(TmpBuffer, "%u", CounterValue++);
632 Tok.setKind(tok::numeric_constant);
633 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner148772a2009-06-13 07:13:28 +0000634 } else if (II == Ident__has_feature ||
635 II == Ident__has_builtin) {
636 // The argument to these two builtins should be a parenthesized identifier.
637 SourceLocation StartLoc = Tok.getLocation();
638
639 bool IsValid = false;
640 IdentifierInfo *FeatureII = 0;
641
642 // Read the '('.
643 Lex(Tok);
644 if (Tok.is(tok::l_paren)) {
645 // Read the identifier
646 Lex(Tok);
647 if (Tok.is(tok::identifier)) {
648 FeatureII = Tok.getIdentifierInfo();
649
650 // Read the ')'.
651 Lex(Tok);
652 if (Tok.is(tok::r_paren))
653 IsValid = true;
654 }
655 }
656
657 bool Value = false;
658 if (!IsValid)
659 Diag(StartLoc, diag::err_feature_check_malformed);
660 else if (II == Ident__has_builtin) {
661 // Check for a builtin is trivial.
662 Value = FeatureII->getBuiltinID() != 0;
663 } else {
664 assert(II == Ident__has_feature && "Must be feature check");
665 Value = HasFeature(*this, FeatureII);
666 }
667
668 sprintf(TmpBuffer, "%d", (int)Value);
669 Tok.setKind(tok::numeric_constant);
670 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000671 } else {
672 assert(0 && "Unknown identifier!");
673 }
674}