blob: ca54236b5c7d482788d5adc206e33e40c5ac679a [file] [log] [blame]
Chris Lattner89620152008-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 Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000021#include <cstdio>
Chris Lattner0725a3e2008-03-18 05:59:11 +000022#include <ctime>
Chris Lattner89620152008-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 Lattner5ec5a302009-04-10 21:17:07 +000028 if (MI) {
Chris Lattner89620152008-03-09 03:13:06 +000029 Macros[II] = MI;
30 II->setHasMacroDefinition(true);
Chris Lattner5ec5a302009-04-10 21:17:07 +000031 } else if (II->hasMacroDefinition()) {
32 Macros.erase(II);
33 II->setHasMacroDefinition(false);
Chris Lattner89620152008-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 Lattnerb6f77af2009-06-13 07:13:28 +000039static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
Chris Lattner89620152008-03-09 03:13:06 +000040 // Get the identifier.
Chris Lattnerb6f77af2009-06-13 07:13:28 +000041 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
Mike Stump11289f42009-09-09 15:08:12 +000042
Chris Lattner89620152008-03-09 03:13:06 +000043 // Mark it as being a macro that is builtin.
Chris Lattnerb6f77af2009-06-13 07:13:28 +000044 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
Chris Lattner89620152008-03-09 03:13:06 +000045 MI->setIsBuiltinMacro();
Chris Lattnerb6f77af2009-06-13 07:13:28 +000046 PP.setMacroInfo(Id, MI);
Chris Lattner89620152008-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 Lattnerb6f77af2009-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 Stump11289f42009-09-09 15:08:12 +000060
Chris Lattner89620152008-03-09 03:13:06 +000061 // GCC Extensions.
Chris Lattnerb6f77af2009-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 Stump11289f42009-09-09 15:08:12 +000065
Chris Lattnerb6f77af2009-06-13 07:13:28 +000066 // Clang Extensions.
John Thompsonac0b0982009-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 Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +000082
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattner89620152008-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 Kremeneka2c3c8d2008-11-19 22:43:49 +0000113 else if (CurPTHLexer)
114 Val = CurPTHLexer->isNextPPTokenLParen();
Chris Lattner89620152008-03-09 03:13:06 +0000115 else
116 Val = CurTokenLexer->isNextTokenLParen();
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner89620152008-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 Kremenek76c34412008-11-19 22:21:33 +0000122 if (CurPPLexer)
Chris Lattner89620152008-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 Kremenekcbc984162008-11-20 16:46:54 +0000128 else if (Entry.ThePTHLexer)
129 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
Chris Lattner89620152008-03-09 03:13:06 +0000130 else
131 Val = Entry.TheTokenLexer->isNextTokenLParen();
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattner89620152008-03-09 03:13:06 +0000133 if (Val != 2)
134 break;
Mike Stump11289f42009-09-09 15:08:12 +0000135
Chris Lattner89620152008-03-09 03:13:06 +0000136 // Ran off the end of a source file?
Ted Kremenekcbc984162008-11-20 16:46:54 +0000137 if (Entry.ThePPLexer)
Chris Lattner89620152008-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 Lattnerc17925d2009-04-18 01:13:56 +0000145 return Val == 1;
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000150bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattner89620152008-03-09 03:13:06 +0000151 MacroInfo *MI) {
Chris Lattnercf35ce92009-03-12 17:31:43 +0000152 if (Callbacks) Callbacks->MacroExpands(Identifier, MI);
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattner89620152008-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 Kremenek551c82a2008-11-18 01:12:54 +0000157 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
Mike Stump11289f42009-09-09 15:08:12 +0000158
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000169
Chris Lattner9dc9c202009-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 Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattner89620152008-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 Lattnerc17925d2009-04-18 01:13:56 +0000177 // name isn't a '(', this macro should not be expanded.
Chris Lattner89620152008-03-09 03:13:06 +0000178 if (!isNextPPTokenLParen())
179 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Chris Lattner89620152008-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 Lattner9dc9c202009-02-15 20:52:18 +0000185 Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattner89620152008-03-09 03:13:06 +0000187 // Finished parsing args.
188 InMacroArgs = false;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattner89620152008-03-09 03:13:06 +0000190 // If there was an error parsing the arguments, bail out.
191 if (Args == 0) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Chris Lattner89620152008-03-09 03:13:06 +0000193 ++NumFnMacroExpanded;
194 } else {
195 ++NumMacroExpanded;
196 }
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattner89620152008-03-09 03:13:06 +0000198 // Notice that this macro has been used.
199 MI->setIsUsed(true);
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattner89620152008-03-09 03:13:06 +0000201 // If we started lexing a macro, enter the macro expansion body.
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000213
Chris Lattner89620152008-03-09 03:13:06 +0000214 Lex(Identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000215
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattner89620152008-03-09 03:13:06 +0000227 } else if (MI->getNumTokens() == 1 &&
228 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
Chris Lattner4fa23622009-01-26 00:43:02 +0000229 *this)) {
Chris Lattner89620152008-03-09 03:13:06 +0000230 // Otherwise, if this macro expands into a single trivially-expanded
Mike Stump11289f42009-09-09 15:08:12 +0000231 // token: expand it now. This handles common cases like
Chris Lattner89620152008-03-09 03:13:06 +0000232 // "#define VAL 42".
Sam Bishop27654982008-03-21 07:13:02 +0000233
234 // No need for arg info.
235 if (Args) Args->destroy();
236
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattner89620152008-03-09 03:13:06 +0000242 // Remember where the token is instantiated.
243 SourceLocation InstantiateLoc = Identifier.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattner89620152008-03-09 03:13:06 +0000245 // Replace the result token.
246 Identifier = MI->getReplacementToken(0);
Mike Stump11289f42009-09-09 15:08:12 +0000247
Chris Lattner89620152008-03-09 03:13:06 +0000248 // Restore the StartOfLine/LeadingSpace markers.
249 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
250 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattner8a425862009-01-16 07:36:28 +0000252 // Update the tokens location to include both its instantiation and physical
Chris Lattner89620152008-03-09 03:13:06 +0000253 // locations.
254 SourceLocation Loc =
Chris Lattner4fa23622009-01-26 00:43:02 +0000255 SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000256 InstantiationEnd,Identifier.getLength());
Chris Lattner89620152008-03-09 03:13:06 +0000257 Identifier.setLocation(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000258
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000263
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000269
Chris Lattner89620152008-03-09 03:13:06 +0000270 // Start expanding the macro.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000271 EnterMacro(Identifier, InstantiationEnd, Args);
Mike Stump11289f42009-09-09 15:08:12 +0000272
Chris Lattner89620152008-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 Lattnerc17925d2009-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 Lattner89620152008-03-09 03:13:06 +0000283MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000284 MacroInfo *MI,
285 SourceLocation &MacroEnd) {
Chris Lattner89620152008-03-09 03:13:06 +0000286 // The number of fixed arguments to parse.
287 unsigned NumFixedArgsLeft = MI->getNumArgs();
288 bool isVariadic = MI->isVariadic();
Mike Stump11289f42009-09-09 15:08:12 +0000289
Chris Lattner89620152008-03-09 03:13:06 +0000290 // Outer loop, while there are more arguments, keep reading them.
291 Token Tok;
Chris Lattner89620152008-03-09 03:13:06 +0000292
Chris Lattnerc17925d2009-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 Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner89620152008-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 Lattnerc17925d2009-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 Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattnerc17925d2009-04-18 01:13:56 +0000308 unsigned ArgTokenStart = ArgTokens.size();
309 SourceLocation ArgStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000310
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000319
Chris Lattner89620152008-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 Lattner9dc9c202009-02-15 20:52:18 +0000327 if (NumParens-- == 0) {
328 MacroEnd = Tok.getLocation();
Chris Lattner89620152008-03-09 03:13:06 +0000329 break;
Chris Lattner9dc9c202009-02-15 20:52:18 +0000330 }
Chris Lattner89620152008-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 Lattnerc17925d2009-04-18 01:13:56 +0000335 // However, if this is a variadic macro, and this is part of the
Mike Stump11289f42009-09-09 15:08:12 +0000336 // variadic part, then the comma is just an argument token.
Chris Lattnerc17925d2009-04-18 01:13:56 +0000337 if (!isVariadic) break;
338 if (NumFixedArgsLeft > 1)
Chris Lattner89620152008-03-09 03:13:06 +0000339 break;
Chris Lattner89620152008-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 Lattner35dd5052009-04-18 06:44:18 +0000344 } else if (Tok.getIdentifierInfo() != 0) {
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000349 // If so, we mark the token as noexpand. This is a subtle aspect of
Chris Lattner89620152008-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 Lattner89620152008-03-09 03:13:06 +0000355 ArgTokens.push_back(Tok);
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattnerc17925d2009-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 Lattner89620152008-03-09 03:13:06 +0000362
Chris Lattnerc17925d2009-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 Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnerc17925d2009-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 Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattner89620152008-03-09 03:13:06 +0000375 // Empty arguments are standard in C99 and supported as an extension in
376 // other modes.
Chris Lattnerc17925d2009-04-18 01:13:56 +0000377 if (ArgTokens.size() == ArgTokenStart && !Features.C99)
Chris Lattner89620152008-03-09 03:13:06 +0000378 Diag(Tok, diag::ext_empty_fnmacro_arg);
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattner89620152008-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 Lattner357b57d2009-01-26 20:24:53 +0000384 EOFTok.setLocation(Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000385 EOFTok.setLength(0);
386 ArgTokens.push_back(EOFTok);
387 ++NumActuals;
Chris Lattnerc17925d2009-04-18 01:13:56 +0000388 assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
Chris Lattner89620152008-03-09 03:13:06 +0000389 --NumFixedArgsLeft;
Chris Lattner9dc9c202009-02-15 20:52:18 +0000390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattner89620152008-03-09 03:13:06 +0000396 // See MacroArgs instance var for description of this.
397 bool isVarargsElided = false;
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattner89620152008-03-09 03:13:06 +0000399 if (NumActuals < MinArgsExpected) {
400 // There are several cases where too few arguments is ok, handle them now.
Chris Lattnerf4c68742009-04-20 21:08:10 +0000401 if (NumActuals == 0 && MinArgsExpected == 1) {
402 // #define A(X) or #define A(...) ---> A()
Mike Stump11289f42009-09-09 15:08:12 +0000403
Chris Lattnerf4c68742009-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 Lattner89620152008-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 Lattnerc17925d2009-04-18 01:13:56 +0000416 // Remember this occurred, allowing us to elide the comma when used for
Chris Lattnerd3300362008-05-08 05:10:33 +0000417 // cases like:
Mike Stump11289f42009-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 Lattnerc17925d2009-04-18 01:13:56 +0000421 // A(x) B(x) C()
Chris Lattnerf4c68742009-04-20 21:08:10 +0000422 isVarargsElided = true;
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner89620152008-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 Lattnerf160b5f2009-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 Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnerc17925d2009-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 Lattner89620152008-03-09 03:13:06 +0000446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Jay Foad7d0479f2009-05-21 09:52:38 +0000448 return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
449 isVarargsElided);
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattner89620152008-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 Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattner89620152008-03-09 03:13:06 +0000464 char TmpBuffer[100];
Mike Stump11289f42009-09-09 15:08:12 +0000465 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
Chris Lattner89620152008-03-09 03:13:06 +0000466 TM->tm_year+1900);
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner5a7971e2009-01-26 19:29:26 +0000468 Token TmpTok;
469 TmpTok.startToken();
470 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
471 DATELoc = TmpTok.getLocation();
Chris Lattner89620152008-03-09 03:13:06 +0000472
473 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000474 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
475 TIMELoc = TmpTok.getLocation();
Chris Lattner89620152008-03-09 03:13:06 +0000476}
477
Chris Lattnerb6f77af2009-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 Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattnerb6f77af2009-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;
Ted Kremenekc3fe0192009-12-03 01:31:28 +0000489 case 8:
490 if (II->isStr("cxx_rtti")) return LangOpts.RTTI;
Ted Kremenek5ef26fb2009-12-03 01:34:15 +0000491 return false;
492 case 14:
493 if (II->isStr("cxx_exceptions")) return LangOpts.Exceptions;
Ted Kremenekc3fe0192009-12-03 01:31:28 +0000494 return false;
David Chisnall5778fce2009-08-31 16:41:57 +0000495 case 19:
496 if (II->isStr("objc_nonfragile_abi")) return LangOpts.ObjCNonFragileABI;
497 return false;
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000498 case 22:
499 if (II->isStr("attribute_overloadable")) return true;
500 return false;
501 case 25:
502 if (II->isStr("attribute_ext_vector_type")) return true;
503 return false;
504 case 27:
505 if (II->isStr("attribute_analyzer_noreturn")) return true;
506 return false;
507 case 29:
508 if (II->isStr("attribute_ns_returns_retained")) return true;
509 if (II->isStr("attribute_cf_returns_retained")) return true;
510 return false;
511 }
512}
513
John Thompsonac0b0982009-11-02 22:28:12 +0000514/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
515/// or '__has_include_next("path")' expression.
516/// Returns true if successful.
517static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok,
518 IdentifierInfo *II, Preprocessor &PP,
519 const DirectoryLookup *LookupFrom) {
520 SourceLocation LParenLoc;
521
522 // Get '('.
523 PP.LexNonComment(Tok);
524
525 // Ensure we have a '('.
526 if (Tok.isNot(tok::l_paren)) {
527 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
528 return false;
529 }
530
531 // Save '(' location for possible missing ')' message.
532 LParenLoc = Tok.getLocation();
533
534 // Get the file name.
535 PP.getCurrentLexer()->LexIncludeFilename(Tok);
536
537 // Reserve a buffer to get the spelling.
538 llvm::SmallVector<char, 128> FilenameBuffer;
539 const char *FilenameStart, *FilenameEnd;
540
541 switch (Tok.getKind()) {
542 case tok::eom:
543 // If the token kind is EOM, the error has already been diagnosed.
544 return false;
545
546 case tok::angle_string_literal:
547 case tok::string_literal: {
548 FilenameBuffer.resize(Tok.getLength());
549 FilenameStart = &FilenameBuffer[0];
550 unsigned Len = PP.getSpelling(Tok, FilenameStart);
551 FilenameEnd = FilenameStart+Len;
552 break;
553 }
554
555 case tok::less:
556 // This could be a <foo/bar.h> file coming from a macro expansion. In this
557 // case, glue the tokens together into FilenameBuffer and interpret those.
558 FilenameBuffer.push_back('<');
559 if (PP.ConcatenateIncludeName(FilenameBuffer))
560 return false; // Found <eom> but no ">"? Diagnostic already emitted.
561 FilenameStart = FilenameBuffer.data();
562 FilenameEnd = FilenameStart + FilenameBuffer.size();
563 break;
564 default:
565 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
566 return false;
567 }
568
569 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(),
570 FilenameStart, FilenameEnd);
571 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
572 // error.
573 if (FilenameStart == 0) {
574 return false;
575 }
576
577 // Search include directories.
578 const DirectoryLookup *CurDir;
579 const FileEntry *File = PP.LookupFile(FilenameStart, FilenameEnd,
580 isAngled, LookupFrom, CurDir);
581
582 // Get the result value. Result = true means the file exists.
583 Result = File != 0;
584
585 // Get ')'.
586 PP.LexNonComment(Tok);
587
588 // Ensure we have a trailing ).
589 if (Tok.isNot(tok::r_paren)) {
590 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
591 PP.Diag(LParenLoc, diag::note_matching) << "(";
592 return false;
593 }
594
595 return true;
596}
597
598/// EvaluateHasInclude - Process a '__has_include("path")' expression.
599/// Returns true if successful.
600static bool EvaluateHasInclude(bool &Result, Token &Tok, IdentifierInfo *II,
601 Preprocessor &PP) {
602 return(EvaluateHasIncludeCommon(Result, Tok, II, PP, NULL));
603}
604
605/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
606/// Returns true if successful.
607static bool EvaluateHasIncludeNext(bool &Result, Token &Tok,
608 IdentifierInfo *II, Preprocessor &PP) {
609 // __has_include_next is like __has_include, except that we start
610 // searching after the current found directory. If we can't do this,
611 // issue a diagnostic.
612 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
613 if (PP.isInPrimaryFile()) {
614 Lookup = 0;
615 PP.Diag(Tok, diag::pp_include_next_in_primary);
616 } else if (Lookup == 0) {
617 PP.Diag(Tok, diag::pp_include_next_absolute_path);
618 } else {
619 // Start looking up in the next directory.
620 ++Lookup;
621 }
622
623 return(EvaluateHasIncludeCommon(Result, Tok, II, PP, Lookup));
624}
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000625
Chris Lattner89620152008-03-09 03:13:06 +0000626/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
627/// as a builtin macro, handle it and return the next token as 'Tok'.
628void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
629 // Figure out which token this is.
630 IdentifierInfo *II = Tok.getIdentifierInfo();
631 assert(II && "Can't be a macro without id info!");
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattner89620152008-03-09 03:13:06 +0000633 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
634 // lex the token after it.
635 if (II == Ident_Pragma)
636 return Handle_Pragma(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000637
Chris Lattner89620152008-03-09 03:13:06 +0000638 ++NumBuiltinMacroExpanded;
639
640 char TmpBuffer[100];
641
642 // Set up the return result.
643 Tok.setIdentifierInfo(0);
644 Tok.clearFlag(Token::NeedsCleaning);
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattner89620152008-03-09 03:13:06 +0000646 if (II == Ident__LINE__) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000647 // C99 6.10.8: "__LINE__: The presumed line number (within the current
648 // source file) of the current source line (an integer constant)". This can
649 // be affected by #line.
Chris Lattner5a2e9cb2009-02-15 21:06:39 +0000650 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattnerbf78da72009-04-18 22:29:33 +0000652 // Advance to the location of the first _, this might not be the first byte
653 // of the token if it starts with an escaped newline.
654 Loc = AdvanceToTokenCharacter(Loc, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Chris Lattner5a2e9cb2009-02-15 21:06:39 +0000656 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
657 // a macro instantiation. This doesn't matter for object-like macros, but
658 // can matter for a function-like macro that expands to contain __LINE__.
659 // Skip down through instantiation points until we find a file loc for the
660 // end of the instantiation history.
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000661 Loc = SourceMgr.getInstantiationRange(Loc).second;
Chris Lattner5a2e9cb2009-02-15 21:06:39 +0000662 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000664 // __LINE__ expands to a simple numeric value.
665 sprintf(TmpBuffer, "%u", PLoc.getLine());
Chris Lattner89620152008-03-09 03:13:06 +0000666 Tok.setKind(tok::numeric_constant);
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000667 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000668 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000669 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
670 // character string literal)". This can be affected by #line.
671 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
672
673 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
674 // #include stack instead of the current file.
Chris Lattner89620152008-03-09 03:13:06 +0000675 if (II == Ident__BASE_FILE__) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000676 SourceLocation NextLoc = PLoc.getIncludeLoc();
Chris Lattner89620152008-03-09 03:13:06 +0000677 while (NextLoc.isValid()) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000678 PLoc = SourceMgr.getPresumedLoc(NextLoc);
679 NextLoc = PLoc.getIncludeLoc();
Chris Lattner89620152008-03-09 03:13:06 +0000680 }
681 }
Mike Stump11289f42009-09-09 15:08:12 +0000682
Chris Lattner89620152008-03-09 03:13:06 +0000683 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000684 std::string FN = PLoc.getFilename();
Chris Lattner89620152008-03-09 03:13:06 +0000685 FN = '"' + Lexer::Stringify(FN) + '"';
686 Tok.setKind(tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000687 CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000688 } else if (II == Ident__DATE__) {
689 if (!DATELoc.isValid())
690 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
691 Tok.setKind(tok::string_literal);
692 Tok.setLength(strlen("\"Mmm dd yyyy\""));
Chris Lattner4fa23622009-01-26 00:43:02 +0000693 Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000694 Tok.getLocation(),
Chris Lattner4fa23622009-01-26 00:43:02 +0000695 Tok.getLength()));
Chris Lattner89620152008-03-09 03:13:06 +0000696 } else if (II == Ident__TIME__) {
697 if (!TIMELoc.isValid())
698 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
699 Tok.setKind(tok::string_literal);
700 Tok.setLength(strlen("\"hh:mm:ss\""));
Chris Lattner4fa23622009-01-26 00:43:02 +0000701 Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000702 Tok.getLocation(),
Chris Lattner4fa23622009-01-26 00:43:02 +0000703 Tok.getLength()));
Chris Lattner89620152008-03-09 03:13:06 +0000704 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000705 // Compute the presumed include depth of this token. This can be affected
706 // by GNU line markers.
Chris Lattner89620152008-03-09 03:13:06 +0000707 unsigned Depth = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000708
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000709 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
710 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
711 for (; PLoc.isValid(); ++Depth)
712 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
Mike Stump11289f42009-09-09 15:08:12 +0000713
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000714 // __INCLUDE_LEVEL__ expands to a simple numeric value.
715 sprintf(TmpBuffer, "%u", Depth);
Chris Lattner89620152008-03-09 03:13:06 +0000716 Tok.setKind(tok::numeric_constant);
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000717 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000718 } else if (II == Ident__TIMESTAMP__) {
719 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
720 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
Chris Lattner89620152008-03-09 03:13:06 +0000721
722 // Get the file that we are lexing out of. If we're currently lexing from
723 // a macro, dig into the include stack.
724 const FileEntry *CurFile = 0;
Ted Kremenek6552d252008-11-20 01:35:24 +0000725 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner89620152008-03-09 03:13:06 +0000727 if (TheLexer)
Ted Kremenek2861cf42008-11-19 22:55:25 +0000728 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattner89620152008-03-09 03:13:06 +0000730 const char *Result;
731 if (CurFile) {
732 time_t TT = CurFile->getModificationTime();
733 struct tm *TM = localtime(&TT);
734 Result = asctime(TM);
735 } else {
736 Result = "??? ??? ?? ??:??:?? ????\n";
737 }
738 TmpBuffer[0] = '"';
Benjamin Kramer26ddfee2009-09-16 13:10:04 +0000739 unsigned Len = strlen(Result);
740 memcpy(TmpBuffer+1, Result, Len-1); // Copy string without the newline.
741 TmpBuffer[Len] = '"';
Chris Lattner89620152008-03-09 03:13:06 +0000742 Tok.setKind(tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000743 CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
Chris Lattner0af3ba12009-04-13 01:29:17 +0000744 } else if (II == Ident__COUNTER__) {
Chris Lattner0af3ba12009-04-13 01:29:17 +0000745 // __COUNTER__ expands to a simple numeric value.
746 sprintf(TmpBuffer, "%u", CounterValue++);
747 Tok.setKind(tok::numeric_constant);
748 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000749 } else if (II == Ident__has_feature ||
750 II == Ident__has_builtin) {
751 // The argument to these two builtins should be a parenthesized identifier.
752 SourceLocation StartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000754 bool IsValid = false;
755 IdentifierInfo *FeatureII = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000757 // Read the '('.
758 Lex(Tok);
759 if (Tok.is(tok::l_paren)) {
760 // Read the identifier
761 Lex(Tok);
762 if (Tok.is(tok::identifier)) {
763 FeatureII = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000765 // Read the ')'.
766 Lex(Tok);
767 if (Tok.is(tok::r_paren))
768 IsValid = true;
769 }
770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000772 bool Value = false;
773 if (!IsValid)
774 Diag(StartLoc, diag::err_feature_check_malformed);
775 else if (II == Ident__has_builtin) {
Mike Stump11289f42009-09-09 15:08:12 +0000776 // Check for a builtin is trivial.
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000777 Value = FeatureII->getBuiltinID() != 0;
778 } else {
779 assert(II == Ident__has_feature && "Must be feature check");
780 Value = HasFeature(*this, FeatureII);
781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000783 sprintf(TmpBuffer, "%d", (int)Value);
784 Tok.setKind(tok::numeric_constant);
785 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
John Thompsonac0b0982009-11-02 22:28:12 +0000786 } else if (II == Ident__has_include ||
787 II == Ident__has_include_next) {
788 // The argument to these two builtins should be a parenthesized
789 // file name string literal using angle brackets (<>) or
790 // double-quotes ("").
791 bool Value = false;
792 bool IsValid;
793 if (II == Ident__has_include)
794 IsValid = EvaluateHasInclude(Value, Tok, II, *this);
795 else
796 IsValid = EvaluateHasIncludeNext(Value, Tok, II, *this);
797 sprintf(TmpBuffer, "%d", (int)Value);
798 Tok.setKind(tok::numeric_constant);
799 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000800 } else {
801 assert(0 && "Unknown identifier!");
802 }
803}