blob: 7ddf215020d019de0ce20170eb9230555c744c5c [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);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattnera3b605e2008-03-09 03:13:06 +000043 // 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");
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattnera3b605e2008-03-09 03:13:06 +000061 // 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__");
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner148772a2009-06-13 07:13:28 +000066 // 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;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattnera3b605e2008-03-09 03:13:06 +000081 // 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;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattnera3b605e2008-03-09 03:13:06 +000088 // 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.
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattnera3b605e2008-03-09 03:13:06 +000099 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattnera3b605e2008-03-09 03:13:06 +0000116 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Chris Lattnera3b605e2008-03-09 03:13:06 +0000131 if (Val != 2)
132 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattnera3b605e2008-03-09 03:13:06 +0000134 // 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'.
Mike Stump1eb44332009-09-09 15:08:12 +0000148bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000149 MacroInfo *MI) {
Chris Lattnerba9eee32009-03-12 17:31:43 +0000150 if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
Mike Stump1eb44332009-09-09 15:08:12 +0000151
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();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattnera3b605e2008-03-09 03:13:06 +0000157 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
158 if (MI->isBuiltinMacro()) {
159 ExpandBuiltinMacro(Identifier);
160 return false;
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattnera3b605e2008-03-09 03:13:06 +0000163 /// 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
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();
Mike Stump1eb44332009-09-09 15:08:12 +0000171
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;
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattnera3b605e2008-03-09 03:13:06 +0000179 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnera3b605e2008-03-09 03:13:06 +0000185 // Finished parsing args.
186 InMacroArgs = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattnera3b605e2008-03-09 03:13:06 +0000188 // If there was an error parsing the arguments, bail out.
189 if (Args == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Chris Lattnera3b605e2008-03-09 03:13:06 +0000191 ++NumFnMacroExpanded;
192 } else {
193 ++NumMacroExpanded;
194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Chris Lattnera3b605e2008-03-09 03:13:06 +0000196 // Notice that this macro has been used.
197 MI->setIsUsed(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattnera3b605e2008-03-09 03:13:06 +0000199 // If we started lexing a macro, enter the macro expansion body.
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattnera3b605e2008-03-09 03:13:06 +0000201 // 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattnera3b605e2008-03-09 03:13:06 +0000207 // Ignore this macro use, just return the next token in the current
208 // buffer.
209 bool HadLeadingSpace = Identifier.hasLeadingSpace();
210 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattnera3b605e2008-03-09 03:13:06 +0000212 Lex(Identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattnera3b605e2008-03-09 03:13:06 +0000214 // 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Chris Lattnera3b605e2008-03-09 03:13:06 +0000225 } 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
Mike Stump1eb44332009-09-09 15:08:12 +0000229 // token: expand it now. This handles common cases like
Chris Lattnera3b605e2008-03-09 03:13:06 +0000230 // "#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();
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Chris Lattnera3b605e2008-03-09 03:13:06 +0000240 // Remember where the token is instantiated.
241 SourceLocation InstantiateLoc = Identifier.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattnera3b605e2008-03-09 03:13:06 +0000243 // Replace the result token.
244 Identifier = MI->getReplacementToken(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattnera3b605e2008-03-09 03:13:06 +0000246 // Restore the StartOfLine/LeadingSpace markers.
247 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
248 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +0000249
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnera3b605e2008-03-09 03:13:06 +0000257 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chris Lattnera3b605e2008-03-09 03:13:06 +0000262 // Since this is not an identifier token, it can't be macro expanded, so
263 // we're done.
264 ++NumFastMacroExpanded;
265 return false;
266 }
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattnera3b605e2008-03-09 03:13:06 +0000268 // Start expanding the macro.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000269 EnterMacro(Identifier, InstantiationEnd, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnera3b605e2008-03-09 03:13:06 +0000271 // 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattnera3b605e2008-03-09 03:13:06 +0000288 // 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?");
Mike Stump1eb44332009-09-09 15:08:12 +0000295
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");
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000306 unsigned ArgTokenStart = ArgTokens.size();
307 SourceLocation ArgStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000308
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;
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattnera3b605e2008-03-09 03:13:06 +0000313 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattnera3b605e2008-03-09 03:13:06 +0000318 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
Mike Stump1eb44332009-09-09 15:08:12 +0000334 // variadic part, then the comma is just an argument token.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000335 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.
Mike Stump1eb44332009-09-09 15:08:12 +0000347 // If so, we mark the token as noexpand. This is a subtle aspect of
Chris Lattnera3b605e2008-03-09 03:13:06 +0000348 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000356 // 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000367 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattnera3b605e2008-03-09 03:13:06 +0000378 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattnera3b605e2008-03-09 03:13:06 +0000390 // Okay, we either found the r_paren. Check to see if we parsed too few
391 // arguments.
392 unsigned MinArgsExpected = MI->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattnera3b605e2008-03-09 03:13:06 +0000394 // See MacroArgs instance var for description of this.
395 bool isVarargsElided = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Chris Lattnera3b605e2008-03-09 03:13:06 +0000397 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()
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner97e2de12009-04-20 21:08:10 +0000402 // 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:
Mike Stump1eb44332009-09-09 15:08:12 +0000416 // #define A(x, foo...) blah(a, ## foo)
417 // #define B(x, ...) blah(a, ## __VA_ARGS__)
418 // #define C(...) blah(a, ## __VA_ARGS__)
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000419 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattnera3b605e2008-03-09 03:13:06 +0000427 // 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);
Mike Stump1eb44332009-09-09 15:08:12 +0000438
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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Chris Lattnera3b605e2008-03-09 03:13:06 +0000458 static const char * const Months[] = {
459 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
460 };
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattnera3b605e2008-03-09 03:13:06 +0000462 char TmpBuffer[100];
Mike Stump1eb44332009-09-09 15:08:12 +0000463 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000464 TM->tm_year+1900);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Chris Lattner47246be2009-01-26 19:29:26 +0000466 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattner148772a2009-06-13 07:13:28 +0000482 switch (II->getLength()) {
483 default: return false;
484 case 6:
485 if (II->isStr("blocks")) return LangOpts.Blocks;
486 return false;
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000487 case 19:
488 if (II->isStr("objc_nonfragile_abi")) return LangOpts.ObjCNonFragileABI;
489 return false;
Chris Lattner148772a2009-06-13 07:13:28 +0000490 case 22:
491 if (II->isStr("attribute_overloadable")) return true;
492 return false;
493 case 25:
494 if (II->isStr("attribute_ext_vector_type")) return true;
495 return false;
496 case 27:
497 if (II->isStr("attribute_analyzer_noreturn")) return true;
498 return false;
499 case 29:
500 if (II->isStr("attribute_ns_returns_retained")) return true;
501 if (II->isStr("attribute_cf_returns_retained")) return true;
502 return false;
503 }
504}
505
506
Chris Lattnera3b605e2008-03-09 03:13:06 +0000507/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
508/// as a builtin macro, handle it and return the next token as 'Tok'.
509void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
510 // Figure out which token this is.
511 IdentifierInfo *II = Tok.getIdentifierInfo();
512 assert(II && "Can't be a macro without id info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattnera3b605e2008-03-09 03:13:06 +0000514 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
515 // lex the token after it.
516 if (II == Ident_Pragma)
517 return Handle_Pragma(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chris Lattnera3b605e2008-03-09 03:13:06 +0000519 ++NumBuiltinMacroExpanded;
520
521 char TmpBuffer[100];
522
523 // Set up the return result.
524 Tok.setIdentifierInfo(0);
525 Tok.clearFlag(Token::NeedsCleaning);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnera3b605e2008-03-09 03:13:06 +0000527 if (II == Ident__LINE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000528 // C99 6.10.8: "__LINE__: The presumed line number (within the current
529 // source file) of the current source line (an integer constant)". This can
530 // be affected by #line.
Chris Lattner081927b2009-02-15 21:06:39 +0000531 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattnerdff070f2009-04-18 22:29:33 +0000533 // Advance to the location of the first _, this might not be the first byte
534 // of the token if it starts with an escaped newline.
535 Loc = AdvanceToTokenCharacter(Loc, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattner081927b2009-02-15 21:06:39 +0000537 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
538 // a macro instantiation. This doesn't matter for object-like macros, but
539 // can matter for a function-like macro that expands to contain __LINE__.
540 // Skip down through instantiation points until we find a file loc for the
541 // end of the instantiation history.
Chris Lattner66781332009-02-15 21:26:50 +0000542 Loc = SourceMgr.getInstantiationRange(Loc).second;
Chris Lattner081927b2009-02-15 21:06:39 +0000543 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner1fa49532009-03-08 08:08:45 +0000545 // __LINE__ expands to a simple numeric value.
546 sprintf(TmpBuffer, "%u", PLoc.getLine());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000547 Tok.setKind(tok::numeric_constant);
Chris Lattner1fa49532009-03-08 08:08:45 +0000548 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000549 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000550 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
551 // character string literal)". This can be affected by #line.
552 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
553
554 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
555 // #include stack instead of the current file.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000556 if (II == Ident__BASE_FILE__) {
557 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000558 SourceLocation NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000559 while (NextLoc.isValid()) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000560 PLoc = SourceMgr.getPresumedLoc(NextLoc);
561 NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000562 }
563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Chris Lattnera3b605e2008-03-09 03:13:06 +0000565 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000566 std::string FN = PLoc.getFilename();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000567 FN = '"' + Lexer::Stringify(FN) + '"';
568 Tok.setKind(tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000569 CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000570 } else if (II == Ident__DATE__) {
571 if (!DATELoc.isValid())
572 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
573 Tok.setKind(tok::string_literal);
574 Tok.setLength(strlen("\"Mmm dd yyyy\""));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000575 Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000576 Tok.getLocation(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000577 Tok.getLength()));
Chris Lattnera3b605e2008-03-09 03:13:06 +0000578 } else if (II == Ident__TIME__) {
579 if (!TIMELoc.isValid())
580 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
581 Tok.setKind(tok::string_literal);
582 Tok.setLength(strlen("\"hh:mm:ss\""));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000583 Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000584 Tok.getLocation(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000585 Tok.getLength()));
Chris Lattnera3b605e2008-03-09 03:13:06 +0000586 } else if (II == Ident__INCLUDE_LEVEL__) {
587 Diag(Tok, diag::ext_pp_include_level);
588
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000589 // Compute the presumed include depth of this token. This can be affected
590 // by GNU line markers.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000591 unsigned Depth = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000593 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
594 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
595 for (; PLoc.isValid(); ++Depth)
596 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattner1fa49532009-03-08 08:08:45 +0000598 // __INCLUDE_LEVEL__ expands to a simple numeric value.
599 sprintf(TmpBuffer, "%u", Depth);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000600 Tok.setKind(tok::numeric_constant);
Chris Lattner1fa49532009-03-08 08:08:45 +0000601 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000602 } else if (II == Ident__TIMESTAMP__) {
603 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
604 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
605 Diag(Tok, diag::ext_pp_timestamp);
606
607 // Get the file that we are lexing out of. If we're currently lexing from
608 // a macro, dig into the include stack.
609 const FileEntry *CurFile = 0;
Ted Kremeneka275a192008-11-20 01:35:24 +0000610 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattnera3b605e2008-03-09 03:13:06 +0000612 if (TheLexer)
Ted Kremenekac80c6e2008-11-19 22:55:25 +0000613 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattnera3b605e2008-03-09 03:13:06 +0000615 // If this file is older than the file it depends on, emit a diagnostic.
616 const char *Result;
617 if (CurFile) {
618 time_t TT = CurFile->getModificationTime();
619 struct tm *TM = localtime(&TT);
620 Result = asctime(TM);
621 } else {
622 Result = "??? ??? ?? ??:??:?? ????\n";
623 }
624 TmpBuffer[0] = '"';
Benjamin Kramer37473822009-09-16 13:10:04 +0000625 unsigned Len = strlen(Result);
626 memcpy(TmpBuffer+1, Result, Len-1); // Copy string without the newline.
627 TmpBuffer[Len] = '"';
Chris Lattnera3b605e2008-03-09 03:13:06 +0000628 Tok.setKind(tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000629 CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000630 } else if (II == Ident__COUNTER__) {
631 Diag(Tok, diag::ext_pp_counter);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000633 // __COUNTER__ expands to a simple numeric value.
634 sprintf(TmpBuffer, "%u", CounterValue++);
635 Tok.setKind(tok::numeric_constant);
636 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner148772a2009-06-13 07:13:28 +0000637 } else if (II == Ident__has_feature ||
638 II == Ident__has_builtin) {
639 // The argument to these two builtins should be a parenthesized identifier.
640 SourceLocation StartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Chris Lattner148772a2009-06-13 07:13:28 +0000642 bool IsValid = false;
643 IdentifierInfo *FeatureII = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Chris Lattner148772a2009-06-13 07:13:28 +0000645 // Read the '('.
646 Lex(Tok);
647 if (Tok.is(tok::l_paren)) {
648 // Read the identifier
649 Lex(Tok);
650 if (Tok.is(tok::identifier)) {
651 FeatureII = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Chris Lattner148772a2009-06-13 07:13:28 +0000653 // Read the ')'.
654 Lex(Tok);
655 if (Tok.is(tok::r_paren))
656 IsValid = true;
657 }
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Chris Lattner148772a2009-06-13 07:13:28 +0000660 bool Value = false;
661 if (!IsValid)
662 Diag(StartLoc, diag::err_feature_check_malformed);
663 else if (II == Ident__has_builtin) {
Mike Stump1eb44332009-09-09 15:08:12 +0000664 // Check for a builtin is trivial.
Chris Lattner148772a2009-06-13 07:13:28 +0000665 Value = FeatureII->getBuiltinID() != 0;
666 } else {
667 assert(II == Ident__has_feature && "Must be feature check");
668 Value = HasFeature(*this, FeatureII);
669 }
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Chris Lattner148772a2009-06-13 07:13:28 +0000671 sprintf(TmpBuffer, "%d", (int)Value);
672 Tok.setKind(tok::numeric_constant);
673 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000674 } else {
675 assert(0 && "Unknown identifier!");
676 }
677}