blob: 699b701ea870736b9bd7d6431e52fb3dea9baae9 [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.
John Thompson92bd8c72009-11-02 22:28:12 +000067 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
68 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
69 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
70 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
Chris Lattnera3b605e2008-03-09 03:13:06 +000071}
72
73/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
74/// in its expansion, currently expands to that token literally.
75static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
76 const IdentifierInfo *MacroIdent,
77 Preprocessor &PP) {
78 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
79
80 // If the token isn't an identifier, it's always literally expanded.
81 if (II == 0) return true;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Chris Lattnera3b605e2008-03-09 03:13:06 +000083 // If the identifier is a macro, and if that macro is enabled, it may be
84 // expanded so it's not a trivial expansion.
85 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
86 // Fast expanding "#define X X" is ok, because X would be disabled.
87 II != MacroIdent)
88 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattnera3b605e2008-03-09 03:13:06 +000090 // If this is an object-like macro invocation, it is safe to trivially expand
91 // it.
92 if (MI->isObjectLike()) return true;
93
94 // If this is a function-like macro invocation, it's safe to trivially expand
95 // as long as the identifier is not a macro argument.
96 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
97 I != E; ++I)
98 if (*I == II)
99 return false; // Identifier is a macro argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattnera3b605e2008-03-09 03:13:06 +0000101 return true;
102}
103
104
105/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
106/// lexed is a '('. If so, consume the token and return true, if not, this
107/// method should have no observable side-effect on the lexed tokens.
108bool Preprocessor::isNextPPTokenLParen() {
109 // Do some quick tests for rejection cases.
110 unsigned Val;
111 if (CurLexer)
112 Val = CurLexer->isNextPPTokenLParen();
Ted Kremenek1a531572008-11-19 22:43:49 +0000113 else if (CurPTHLexer)
114 Val = CurPTHLexer->isNextPPTokenLParen();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000115 else
116 Val = CurTokenLexer->isNextTokenLParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattnera3b605e2008-03-09 03:13:06 +0000118 if (Val == 2) {
119 // We have run off the end. If it's a source file we don't
120 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
121 // macro stack.
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000122 if (CurPPLexer)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000123 return false;
124 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
125 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
126 if (Entry.TheLexer)
127 Val = Entry.TheLexer->isNextPPTokenLParen();
Ted Kremenekdd95d6c2008-11-20 16:46:54 +0000128 else if (Entry.ThePTHLexer)
129 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000130 else
131 Val = Entry.TheTokenLexer->isNextTokenLParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattnera3b605e2008-03-09 03:13:06 +0000133 if (Val != 2)
134 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattnera3b605e2008-03-09 03:13:06 +0000136 // Ran off the end of a source file?
Ted Kremenekdd95d6c2008-11-20 16:46:54 +0000137 if (Entry.ThePPLexer)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000138 return false;
139 }
140 }
141
142 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
143 // have found something that isn't a '(' or we found the end of the
144 // translation unit. In either case, return false.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000145 return Val == 1;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000146}
147
148/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
149/// expanded as a macro, handle it and return the next token as 'Identifier'.
Mike Stump1eb44332009-09-09 15:08:12 +0000150bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000151 MacroInfo *MI) {
Chris Lattnerba9eee32009-03-12 17:31:43 +0000152 if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chris Lattnera3b605e2008-03-09 03:13:06 +0000154 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
155 // then the macro could expand to different things in other contexts, we need
156 // to disable the optimization in this case.
Ted Kremenek68a91d52008-11-18 01:12:54 +0000157 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Chris Lattnera3b605e2008-03-09 03:13:06 +0000159 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
160 if (MI->isBuiltinMacro()) {
161 ExpandBuiltinMacro(Identifier);
162 return false;
163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattnera3b605e2008-03-09 03:13:06 +0000165 /// Args - If this is a function-like macro expansion, this contains,
166 /// for each macro argument, the list of tokens that were provided to the
167 /// invocation.
168 MacroArgs *Args = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattnere7fb4842009-02-15 20:52:18 +0000170 // Remember where the end of the instantiation occurred. For an object-like
171 // macro, this is the identifier. For a function-like macro, this is the ')'.
172 SourceLocation InstantiationEnd = Identifier.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattnera3b605e2008-03-09 03:13:06 +0000174 // If this is a function-like macro, read the arguments.
175 if (MI->isFunctionLike()) {
176 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000177 // name isn't a '(', this macro should not be expanded.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000178 if (!isNextPPTokenLParen())
179 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattnera3b605e2008-03-09 03:13:06 +0000181 // Remember that we are now parsing the arguments to a macro invocation.
182 // Preprocessor directives used inside macro arguments are not portable, and
183 // this enables the warning.
184 InMacroArgs = true;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000185 Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattnera3b605e2008-03-09 03:13:06 +0000187 // Finished parsing args.
188 InMacroArgs = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattnera3b605e2008-03-09 03:13:06 +0000190 // If there was an error parsing the arguments, bail out.
191 if (Args == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattnera3b605e2008-03-09 03:13:06 +0000193 ++NumFnMacroExpanded;
194 } else {
195 ++NumMacroExpanded;
196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattnera3b605e2008-03-09 03:13:06 +0000198 // Notice that this macro has been used.
199 MI->setIsUsed(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattnera3b605e2008-03-09 03:13:06 +0000201 // If we started lexing a macro, enter the macro expansion body.
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattnera3b605e2008-03-09 03:13:06 +0000203 // If this macro expands to no tokens, don't bother to push it onto the
204 // expansion stack, only to take it right back off.
205 if (MI->getNumTokens() == 0) {
206 // No need for arg info.
207 if (Args) Args->destroy();
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattnera3b605e2008-03-09 03:13:06 +0000209 // Ignore this macro use, just return the next token in the current
210 // buffer.
211 bool HadLeadingSpace = Identifier.hasLeadingSpace();
212 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Chris Lattnera3b605e2008-03-09 03:13:06 +0000214 Lex(Identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattnera3b605e2008-03-09 03:13:06 +0000216 // If the identifier isn't on some OTHER line, inherit the leading
217 // whitespace/first-on-a-line property of this token. This handles
218 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
219 // empty.
220 if (!Identifier.isAtStartOfLine()) {
221 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
222 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
223 }
224 ++NumFastMacroExpanded;
225 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattnera3b605e2008-03-09 03:13:06 +0000227 } else if (MI->getNumTokens() == 1 &&
228 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000229 *this)) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000230 // Otherwise, if this macro expands into a single trivially-expanded
Mike Stump1eb44332009-09-09 15:08:12 +0000231 // token: expand it now. This handles common cases like
Chris Lattnera3b605e2008-03-09 03:13:06 +0000232 // "#define VAL 42".
Sam Bishop9a4939f2008-03-21 07:13:02 +0000233
234 // No need for arg info.
235 if (Args) Args->destroy();
236
Chris Lattnera3b605e2008-03-09 03:13:06 +0000237 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
238 // identifier to the expanded token.
239 bool isAtStartOfLine = Identifier.isAtStartOfLine();
240 bool hasLeadingSpace = Identifier.hasLeadingSpace();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattnera3b605e2008-03-09 03:13:06 +0000242 // Remember where the token is instantiated.
243 SourceLocation InstantiateLoc = Identifier.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattnera3b605e2008-03-09 03:13:06 +0000245 // Replace the result token.
246 Identifier = MI->getReplacementToken(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattnera3b605e2008-03-09 03:13:06 +0000248 // Restore the StartOfLine/LeadingSpace markers.
249 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
250 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000252 // Update the tokens location to include both its instantiation and physical
Chris Lattnera3b605e2008-03-09 03:13:06 +0000253 // locations.
254 SourceLocation Loc =
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000255 SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000256 InstantiationEnd,Identifier.getLength());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000257 Identifier.setLocation(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattnera3b605e2008-03-09 03:13:06 +0000259 // If this is #define X X, we must mark the result as unexpandible.
260 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
261 if (getMacroInfo(NewII) == MI)
262 Identifier.setFlag(Token::DisableExpand);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattnera3b605e2008-03-09 03:13:06 +0000264 // Since this is not an identifier token, it can't be macro expanded, so
265 // we're done.
266 ++NumFastMacroExpanded;
267 return false;
268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattnera3b605e2008-03-09 03:13:06 +0000270 // Start expanding the macro.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000271 EnterMacro(Identifier, InstantiationEnd, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattnera3b605e2008-03-09 03:13:06 +0000273 // Now that the macro is at the top of the include stack, ask the
274 // preprocessor to read the next token from it.
275 Lex(Identifier);
276 return false;
277}
278
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000279/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
280/// token is the '(' of the macro, this method is invoked to read all of the
281/// actual arguments specified for the macro invocation. This returns null on
282/// error.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000283MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000284 MacroInfo *MI,
285 SourceLocation &MacroEnd) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000286 // The number of fixed arguments to parse.
287 unsigned NumFixedArgsLeft = MI->getNumArgs();
288 bool isVariadic = MI->isVariadic();
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattnera3b605e2008-03-09 03:13:06 +0000290 // Outer loop, while there are more arguments, keep reading them.
291 Token Tok;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000292
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000293 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
294 // an argument value in a macro could expand to ',' or '(' or ')'.
295 LexUnexpandedToken(Tok);
296 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattnera3b605e2008-03-09 03:13:06 +0000298 // ArgTokens - Build up a list of tokens that make up each argument. Each
299 // argument is separated by an EOF token. Use a SmallVector so we can avoid
300 // heap allocations in the common case.
301 llvm::SmallVector<Token, 64> ArgTokens;
302
303 unsigned NumActuals = 0;
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000304 while (Tok.isNot(tok::r_paren)) {
305 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
306 "only expect argument separators here");
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000308 unsigned ArgTokenStart = ArgTokens.size();
309 SourceLocation ArgStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Chris Lattnera3b605e2008-03-09 03:13:06 +0000311 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
312 // that we already consumed the first one.
313 unsigned NumParens = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattnera3b605e2008-03-09 03:13:06 +0000315 while (1) {
316 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
317 // an argument value in a macro could expand to ',' or '(' or ')'.
318 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattnera3b605e2008-03-09 03:13:06 +0000320 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
321 Diag(MacroName, diag::err_unterm_macro_invoc);
322 // Do not lose the EOF/EOM. Return it to the client.
323 MacroName = Tok;
324 return 0;
325 } else if (Tok.is(tok::r_paren)) {
326 // If we found the ) token, the macro arg list is done.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000327 if (NumParens-- == 0) {
328 MacroEnd = Tok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000329 break;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000330 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000331 } else if (Tok.is(tok::l_paren)) {
332 ++NumParens;
333 } else if (Tok.is(tok::comma) && NumParens == 0) {
334 // Comma ends this argument if there are more fixed arguments expected.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000335 // However, if this is a variadic macro, and this is part of the
Mike Stump1eb44332009-09-09 15:08:12 +0000336 // variadic part, then the comma is just an argument token.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000337 if (!isVariadic) break;
338 if (NumFixedArgsLeft > 1)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000339 break;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000340 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
341 // If this is a comment token in the argument list and we're just in
342 // -C mode (not -CC mode), discard the comment.
343 continue;
Chris Lattner5c497a82009-04-18 06:44:18 +0000344 } else if (Tok.getIdentifierInfo() != 0) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000345 // Reading macro arguments can cause macros that we are currently
346 // expanding from to be popped off the expansion stack. Doing so causes
347 // them to be reenabled for expansion. Here we record whether any
348 // identifiers we lex as macro arguments correspond to disabled macros.
Mike Stump1eb44332009-09-09 15:08:12 +0000349 // If so, we mark the token as noexpand. This is a subtle aspect of
Chris Lattnera3b605e2008-03-09 03:13:06 +0000350 // C99 6.10.3.4p2.
351 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
352 if (!MI->isEnabled())
353 Tok.setFlag(Token::DisableExpand);
354 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000355 ArgTokens.push_back(Tok);
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000358 // If this was an empty argument list foo(), don't add this as an empty
359 // argument.
360 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
361 break;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000362
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000363 // If this is not a variadic macro, and too many args were specified, emit
364 // an error.
365 if (!isVariadic && NumFixedArgsLeft == 0) {
366 if (ArgTokens.size() != ArgTokenStart)
367 ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000369 // Emit the diagnostic at the macro name in case there is a missing ).
370 // Emitting it at the , could be far away from the macro name.
371 Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
372 return 0;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattnera3b605e2008-03-09 03:13:06 +0000375 // Empty arguments are standard in C99 and supported as an extension in
376 // other modes.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000377 if (ArgTokens.size() == ArgTokenStart && !Features.C99)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000378 Diag(Tok, diag::ext_empty_fnmacro_arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattnera3b605e2008-03-09 03:13:06 +0000380 // Add a marker EOF token to the end of the token list for this argument.
381 Token EOFTok;
382 EOFTok.startToken();
383 EOFTok.setKind(tok::eof);
Chris Lattnere7689882009-01-26 20:24:53 +0000384 EOFTok.setLocation(Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000385 EOFTok.setLength(0);
386 ArgTokens.push_back(EOFTok);
387 ++NumActuals;
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000388 assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
Chris Lattnera3b605e2008-03-09 03:13:06 +0000389 --NumFixedArgsLeft;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnera3b605e2008-03-09 03:13:06 +0000392 // Okay, we either found the r_paren. Check to see if we parsed too few
393 // arguments.
394 unsigned MinArgsExpected = MI->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattnera3b605e2008-03-09 03:13:06 +0000396 // See MacroArgs instance var for description of this.
397 bool isVarargsElided = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnera3b605e2008-03-09 03:13:06 +0000399 if (NumActuals < MinArgsExpected) {
400 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner97e2de12009-04-20 21:08:10 +0000401 if (NumActuals == 0 && MinArgsExpected == 1) {
402 // #define A(X) or #define A(...) ---> A()
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner97e2de12009-04-20 21:08:10 +0000404 // If there is exactly one argument, and that argument is missing,
405 // then we have an empty "()" argument empty list. This is fine, even if
406 // the macro expects one argument (the argument is just empty).
407 isVarargsElided = MI->isVariadic();
408 } else if (MI->isVariadic() &&
409 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
410 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
Chris Lattnera3b605e2008-03-09 03:13:06 +0000411 // Varargs where the named vararg parameter is missing: ok as extension.
412 // #define A(x, ...)
413 // A("blah")
414 Diag(Tok, diag::ext_missing_varargs_arg);
415
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000416 // Remember this occurred, allowing us to elide the comma when used for
Chris Lattner63bc0352008-05-08 05:10:33 +0000417 // cases like:
Mike Stump1eb44332009-09-09 15:08:12 +0000418 // #define A(x, foo...) blah(a, ## foo)
419 // #define B(x, ...) blah(a, ## __VA_ARGS__)
420 // #define C(...) blah(a, ## __VA_ARGS__)
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000421 // A(x) B(x) C()
Chris Lattner97e2de12009-04-20 21:08:10 +0000422 isVarargsElided = true;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000423 } else {
424 // Otherwise, emit the error.
425 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
426 return 0;
427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattnera3b605e2008-03-09 03:13:06 +0000429 // Add a marker EOF token to the end of the token list for this argument.
430 SourceLocation EndLoc = Tok.getLocation();
431 Tok.startToken();
432 Tok.setKind(tok::eof);
433 Tok.setLocation(EndLoc);
434 Tok.setLength(0);
435 ArgTokens.push_back(Tok);
Chris Lattner9fc9e772009-05-13 00:55:26 +0000436
437 // If we expect two arguments, add both as empty.
438 if (NumActuals == 0 && MinArgsExpected == 2)
439 ArgTokens.push_back(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000441 } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
442 // Emit the diagnostic at the macro name in case there is a missing ).
443 // Emitting it at the , could be far away from the macro name.
444 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
445 return 0;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Jay Foadbeaaccd2009-05-21 09:52:38 +0000448 return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
449 isVarargsElided);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000450}
451
452/// ComputeDATE_TIME - Compute the current time, enter it into the specified
453/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
454/// the identifier tokens inserted.
455static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
456 Preprocessor &PP) {
457 time_t TT = time(0);
458 struct tm *TM = localtime(&TT);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattnera3b605e2008-03-09 03:13:06 +0000460 static const char * const Months[] = {
461 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
462 };
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattnera3b605e2008-03-09 03:13:06 +0000464 char TmpBuffer[100];
Mike Stump1eb44332009-09-09 15:08:12 +0000465 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000466 TM->tm_year+1900);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattner47246be2009-01-26 19:29:26 +0000468 Token TmpTok;
469 TmpTok.startToken();
470 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
471 DATELoc = TmpTok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000472
473 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattner47246be2009-01-26 19:29:26 +0000474 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
475 TIMELoc = TmpTok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000476}
477
Chris Lattner148772a2009-06-13 07:13:28 +0000478
479/// HasFeature - Return true if we recognize and implement the specified feature
480/// specified by the identifier.
481static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
482 const LangOptions &LangOpts = PP.getLangOptions();
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner148772a2009-06-13 07:13:28 +0000484 switch (II->getLength()) {
485 default: return false;
486 case 6:
487 if (II->isStr("blocks")) return LangOpts.Blocks;
488 return false;
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000489 case 19:
490 if (II->isStr("objc_nonfragile_abi")) return LangOpts.ObjCNonFragileABI;
491 return false;
Chris Lattner148772a2009-06-13 07:13:28 +0000492 case 22:
493 if (II->isStr("attribute_overloadable")) return true;
494 return false;
495 case 25:
496 if (II->isStr("attribute_ext_vector_type")) return true;
497 return false;
498 case 27:
499 if (II->isStr("attribute_analyzer_noreturn")) return true;
500 return false;
501 case 29:
502 if (II->isStr("attribute_ns_returns_retained")) return true;
503 if (II->isStr("attribute_cf_returns_retained")) return true;
504 return false;
505 }
506}
507
John Thompson92bd8c72009-11-02 22:28:12 +0000508/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
509/// or '__has_include_next("path")' expression.
510/// Returns true if successful.
511static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok,
512 IdentifierInfo *II, Preprocessor &PP,
513 const DirectoryLookup *LookupFrom) {
514 SourceLocation LParenLoc;
515
516 // Get '('.
517 PP.LexNonComment(Tok);
518
519 // Ensure we have a '('.
520 if (Tok.isNot(tok::l_paren)) {
521 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
522 return false;
523 }
524
525 // Save '(' location for possible missing ')' message.
526 LParenLoc = Tok.getLocation();
527
528 // Get the file name.
529 PP.getCurrentLexer()->LexIncludeFilename(Tok);
530
531 // Reserve a buffer to get the spelling.
532 llvm::SmallVector<char, 128> FilenameBuffer;
533 const char *FilenameStart, *FilenameEnd;
534
535 switch (Tok.getKind()) {
536 case tok::eom:
537 // If the token kind is EOM, the error has already been diagnosed.
538 return false;
539
540 case tok::angle_string_literal:
541 case tok::string_literal: {
542 FilenameBuffer.resize(Tok.getLength());
543 FilenameStart = &FilenameBuffer[0];
544 unsigned Len = PP.getSpelling(Tok, FilenameStart);
545 FilenameEnd = FilenameStart+Len;
546 break;
547 }
548
549 case tok::less:
550 // This could be a <foo/bar.h> file coming from a macro expansion. In this
551 // case, glue the tokens together into FilenameBuffer and interpret those.
552 FilenameBuffer.push_back('<');
553 if (PP.ConcatenateIncludeName(FilenameBuffer))
554 return false; // Found <eom> but no ">"? Diagnostic already emitted.
555 FilenameStart = FilenameBuffer.data();
556 FilenameEnd = FilenameStart + FilenameBuffer.size();
557 break;
558 default:
559 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
560 return false;
561 }
562
563 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(),
564 FilenameStart, FilenameEnd);
565 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
566 // error.
567 if (FilenameStart == 0) {
568 return false;
569 }
570
571 // Search include directories.
572 const DirectoryLookup *CurDir;
573 const FileEntry *File = PP.LookupFile(FilenameStart, FilenameEnd,
574 isAngled, LookupFrom, CurDir);
575
576 // Get the result value. Result = true means the file exists.
577 Result = File != 0;
578
579 // Get ')'.
580 PP.LexNonComment(Tok);
581
582 // Ensure we have a trailing ).
583 if (Tok.isNot(tok::r_paren)) {
584 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
585 PP.Diag(LParenLoc, diag::note_matching) << "(";
586 return false;
587 }
588
589 return true;
590}
591
592/// EvaluateHasInclude - Process a '__has_include("path")' expression.
593/// Returns true if successful.
594static bool EvaluateHasInclude(bool &Result, Token &Tok, IdentifierInfo *II,
595 Preprocessor &PP) {
596 return(EvaluateHasIncludeCommon(Result, Tok, II, PP, NULL));
597}
598
599/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
600/// Returns true if successful.
601static bool EvaluateHasIncludeNext(bool &Result, Token &Tok,
602 IdentifierInfo *II, Preprocessor &PP) {
603 // __has_include_next is like __has_include, except that we start
604 // searching after the current found directory. If we can't do this,
605 // issue a diagnostic.
606 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
607 if (PP.isInPrimaryFile()) {
608 Lookup = 0;
609 PP.Diag(Tok, diag::pp_include_next_in_primary);
610 } else if (Lookup == 0) {
611 PP.Diag(Tok, diag::pp_include_next_absolute_path);
612 } else {
613 // Start looking up in the next directory.
614 ++Lookup;
615 }
616
617 return(EvaluateHasIncludeCommon(Result, Tok, II, PP, Lookup));
618}
Chris Lattner148772a2009-06-13 07:13:28 +0000619
Chris Lattnera3b605e2008-03-09 03:13:06 +0000620/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
621/// as a builtin macro, handle it and return the next token as 'Tok'.
622void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
623 // Figure out which token this is.
624 IdentifierInfo *II = Tok.getIdentifierInfo();
625 assert(II && "Can't be a macro without id info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Chris Lattnera3b605e2008-03-09 03:13:06 +0000627 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
628 // lex the token after it.
629 if (II == Ident_Pragma)
630 return Handle_Pragma(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattnera3b605e2008-03-09 03:13:06 +0000632 ++NumBuiltinMacroExpanded;
633
634 char TmpBuffer[100];
635
636 // Set up the return result.
637 Tok.setIdentifierInfo(0);
638 Tok.clearFlag(Token::NeedsCleaning);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Chris Lattnera3b605e2008-03-09 03:13:06 +0000640 if (II == Ident__LINE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000641 // C99 6.10.8: "__LINE__: The presumed line number (within the current
642 // source file) of the current source line (an integer constant)". This can
643 // be affected by #line.
Chris Lattner081927b2009-02-15 21:06:39 +0000644 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattnerdff070f2009-04-18 22:29:33 +0000646 // Advance to the location of the first _, this might not be the first byte
647 // of the token if it starts with an escaped newline.
648 Loc = AdvanceToTokenCharacter(Loc, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattner081927b2009-02-15 21:06:39 +0000650 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
651 // a macro instantiation. This doesn't matter for object-like macros, but
652 // can matter for a function-like macro that expands to contain __LINE__.
653 // Skip down through instantiation points until we find a file loc for the
654 // end of the instantiation history.
Chris Lattner66781332009-02-15 21:26:50 +0000655 Loc = SourceMgr.getInstantiationRange(Loc).second;
Chris Lattner081927b2009-02-15 21:06:39 +0000656 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Chris Lattner1fa49532009-03-08 08:08:45 +0000658 // __LINE__ expands to a simple numeric value.
659 sprintf(TmpBuffer, "%u", PLoc.getLine());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000660 Tok.setKind(tok::numeric_constant);
Chris Lattner1fa49532009-03-08 08:08:45 +0000661 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000662 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000663 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
664 // character string literal)". This can be affected by #line.
665 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
666
667 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
668 // #include stack instead of the current file.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000669 if (II == Ident__BASE_FILE__) {
670 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000671 SourceLocation NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000672 while (NextLoc.isValid()) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000673 PLoc = SourceMgr.getPresumedLoc(NextLoc);
674 NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000675 }
676 }
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Chris Lattnera3b605e2008-03-09 03:13:06 +0000678 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000679 std::string FN = PLoc.getFilename();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000680 FN = '"' + Lexer::Stringify(FN) + '"';
681 Tok.setKind(tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000682 CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000683 } else if (II == Ident__DATE__) {
684 if (!DATELoc.isValid())
685 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
686 Tok.setKind(tok::string_literal);
687 Tok.setLength(strlen("\"Mmm dd yyyy\""));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000688 Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000689 Tok.getLocation(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000690 Tok.getLength()));
Chris Lattnera3b605e2008-03-09 03:13:06 +0000691 } else if (II == Ident__TIME__) {
692 if (!TIMELoc.isValid())
693 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
694 Tok.setKind(tok::string_literal);
695 Tok.setLength(strlen("\"hh:mm:ss\""));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000696 Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000697 Tok.getLocation(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000698 Tok.getLength()));
Chris Lattnera3b605e2008-03-09 03:13:06 +0000699 } else if (II == Ident__INCLUDE_LEVEL__) {
700 Diag(Tok, diag::ext_pp_include_level);
701
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000702 // Compute the presumed include depth of this token. This can be affected
703 // by GNU line markers.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000704 unsigned Depth = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000706 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
707 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
708 for (; PLoc.isValid(); ++Depth)
709 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Chris Lattner1fa49532009-03-08 08:08:45 +0000711 // __INCLUDE_LEVEL__ expands to a simple numeric value.
712 sprintf(TmpBuffer, "%u", Depth);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000713 Tok.setKind(tok::numeric_constant);
Chris Lattner1fa49532009-03-08 08:08:45 +0000714 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000715 } else if (II == Ident__TIMESTAMP__) {
716 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
717 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
718 Diag(Tok, diag::ext_pp_timestamp);
719
720 // Get the file that we are lexing out of. If we're currently lexing from
721 // a macro, dig into the include stack.
722 const FileEntry *CurFile = 0;
Ted Kremeneka275a192008-11-20 01:35:24 +0000723 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattnera3b605e2008-03-09 03:13:06 +0000725 if (TheLexer)
Ted Kremenekac80c6e2008-11-19 22:55:25 +0000726 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattnera3b605e2008-03-09 03:13:06 +0000728 // If this file is older than the file it depends on, emit a diagnostic.
729 const char *Result;
730 if (CurFile) {
731 time_t TT = CurFile->getModificationTime();
732 struct tm *TM = localtime(&TT);
733 Result = asctime(TM);
734 } else {
735 Result = "??? ??? ?? ??:??:?? ????\n";
736 }
737 TmpBuffer[0] = '"';
Benjamin Kramer37473822009-09-16 13:10:04 +0000738 unsigned Len = strlen(Result);
739 memcpy(TmpBuffer+1, Result, Len-1); // Copy string without the newline.
740 TmpBuffer[Len] = '"';
Chris Lattnera3b605e2008-03-09 03:13:06 +0000741 Tok.setKind(tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000742 CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000743 } else if (II == Ident__COUNTER__) {
744 Diag(Tok, diag::ext_pp_counter);
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000746 // __COUNTER__ expands to a simple numeric value.
747 sprintf(TmpBuffer, "%u", CounterValue++);
748 Tok.setKind(tok::numeric_constant);
749 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner148772a2009-06-13 07:13:28 +0000750 } else if (II == Ident__has_feature ||
751 II == Ident__has_builtin) {
752 // The argument to these two builtins should be a parenthesized identifier.
753 SourceLocation StartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner148772a2009-06-13 07:13:28 +0000755 bool IsValid = false;
756 IdentifierInfo *FeatureII = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattner148772a2009-06-13 07:13:28 +0000758 // Read the '('.
759 Lex(Tok);
760 if (Tok.is(tok::l_paren)) {
761 // Read the identifier
762 Lex(Tok);
763 if (Tok.is(tok::identifier)) {
764 FeatureII = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattner148772a2009-06-13 07:13:28 +0000766 // Read the ')'.
767 Lex(Tok);
768 if (Tok.is(tok::r_paren))
769 IsValid = true;
770 }
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner148772a2009-06-13 07:13:28 +0000773 bool Value = false;
774 if (!IsValid)
775 Diag(StartLoc, diag::err_feature_check_malformed);
776 else if (II == Ident__has_builtin) {
Mike Stump1eb44332009-09-09 15:08:12 +0000777 // Check for a builtin is trivial.
Chris Lattner148772a2009-06-13 07:13:28 +0000778 Value = FeatureII->getBuiltinID() != 0;
779 } else {
780 assert(II == Ident__has_feature && "Must be feature check");
781 Value = HasFeature(*this, FeatureII);
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattner148772a2009-06-13 07:13:28 +0000784 sprintf(TmpBuffer, "%d", (int)Value);
785 Tok.setKind(tok::numeric_constant);
786 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
John Thompson92bd8c72009-11-02 22:28:12 +0000787 } else if (II == Ident__has_include ||
788 II == Ident__has_include_next) {
789 // The argument to these two builtins should be a parenthesized
790 // file name string literal using angle brackets (<>) or
791 // double-quotes ("").
792 bool Value = false;
793 bool IsValid;
794 if (II == Ident__has_include)
795 IsValid = EvaluateHasInclude(Value, Tok, II, *this);
796 else
797 IsValid = EvaluateHasIncludeNext(Value, Tok, II, *this);
798 sprintf(TmpBuffer, "%d", (int)Value);
799 Tok.setKind(tok::numeric_constant);
800 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000801 } else {
802 assert(0 && "Unknown identifier!");
803 }
804}