blob: 6181e17e602d753d7fc9d75c0eb1399924263cf2 [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;
491 return false;
David Chisnall5778fce2009-08-31 16:41:57 +0000492 case 19:
493 if (II->isStr("objc_nonfragile_abi")) return LangOpts.ObjCNonFragileABI;
494 return false;
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000495 case 22:
496 if (II->isStr("attribute_overloadable")) return true;
497 return false;
498 case 25:
499 if (II->isStr("attribute_ext_vector_type")) return true;
500 return false;
501 case 27:
502 if (II->isStr("attribute_analyzer_noreturn")) return true;
503 return false;
504 case 29:
505 if (II->isStr("attribute_ns_returns_retained")) return true;
506 if (II->isStr("attribute_cf_returns_retained")) return true;
507 return false;
508 }
509}
510
John Thompsonac0b0982009-11-02 22:28:12 +0000511/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
512/// or '__has_include_next("path")' expression.
513/// Returns true if successful.
514static bool EvaluateHasIncludeCommon(bool &Result, Token &Tok,
515 IdentifierInfo *II, Preprocessor &PP,
516 const DirectoryLookup *LookupFrom) {
517 SourceLocation LParenLoc;
518
519 // Get '('.
520 PP.LexNonComment(Tok);
521
522 // Ensure we have a '('.
523 if (Tok.isNot(tok::l_paren)) {
524 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
525 return false;
526 }
527
528 // Save '(' location for possible missing ')' message.
529 LParenLoc = Tok.getLocation();
530
531 // Get the file name.
532 PP.getCurrentLexer()->LexIncludeFilename(Tok);
533
534 // Reserve a buffer to get the spelling.
535 llvm::SmallVector<char, 128> FilenameBuffer;
536 const char *FilenameStart, *FilenameEnd;
537
538 switch (Tok.getKind()) {
539 case tok::eom:
540 // If the token kind is EOM, the error has already been diagnosed.
541 return false;
542
543 case tok::angle_string_literal:
544 case tok::string_literal: {
545 FilenameBuffer.resize(Tok.getLength());
546 FilenameStart = &FilenameBuffer[0];
547 unsigned Len = PP.getSpelling(Tok, FilenameStart);
548 FilenameEnd = FilenameStart+Len;
549 break;
550 }
551
552 case tok::less:
553 // This could be a <foo/bar.h> file coming from a macro expansion. In this
554 // case, glue the tokens together into FilenameBuffer and interpret those.
555 FilenameBuffer.push_back('<');
556 if (PP.ConcatenateIncludeName(FilenameBuffer))
557 return false; // Found <eom> but no ">"? Diagnostic already emitted.
558 FilenameStart = FilenameBuffer.data();
559 FilenameEnd = FilenameStart + FilenameBuffer.size();
560 break;
561 default:
562 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
563 return false;
564 }
565
566 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(),
567 FilenameStart, FilenameEnd);
568 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
569 // error.
570 if (FilenameStart == 0) {
571 return false;
572 }
573
574 // Search include directories.
575 const DirectoryLookup *CurDir;
576 const FileEntry *File = PP.LookupFile(FilenameStart, FilenameEnd,
577 isAngled, LookupFrom, CurDir);
578
579 // Get the result value. Result = true means the file exists.
580 Result = File != 0;
581
582 // Get ')'.
583 PP.LexNonComment(Tok);
584
585 // Ensure we have a trailing ).
586 if (Tok.isNot(tok::r_paren)) {
587 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
588 PP.Diag(LParenLoc, diag::note_matching) << "(";
589 return false;
590 }
591
592 return true;
593}
594
595/// EvaluateHasInclude - Process a '__has_include("path")' expression.
596/// Returns true if successful.
597static bool EvaluateHasInclude(bool &Result, Token &Tok, IdentifierInfo *II,
598 Preprocessor &PP) {
599 return(EvaluateHasIncludeCommon(Result, Tok, II, PP, NULL));
600}
601
602/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
603/// Returns true if successful.
604static bool EvaluateHasIncludeNext(bool &Result, Token &Tok,
605 IdentifierInfo *II, Preprocessor &PP) {
606 // __has_include_next is like __has_include, except that we start
607 // searching after the current found directory. If we can't do this,
608 // issue a diagnostic.
609 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
610 if (PP.isInPrimaryFile()) {
611 Lookup = 0;
612 PP.Diag(Tok, diag::pp_include_next_in_primary);
613 } else if (Lookup == 0) {
614 PP.Diag(Tok, diag::pp_include_next_absolute_path);
615 } else {
616 // Start looking up in the next directory.
617 ++Lookup;
618 }
619
620 return(EvaluateHasIncludeCommon(Result, Tok, II, PP, Lookup));
621}
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000622
Chris Lattner89620152008-03-09 03:13:06 +0000623/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
624/// as a builtin macro, handle it and return the next token as 'Tok'.
625void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
626 // Figure out which token this is.
627 IdentifierInfo *II = Tok.getIdentifierInfo();
628 assert(II && "Can't be a macro without id info!");
Mike Stump11289f42009-09-09 15:08:12 +0000629
Chris Lattner89620152008-03-09 03:13:06 +0000630 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
631 // lex the token after it.
632 if (II == Ident_Pragma)
633 return Handle_Pragma(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner89620152008-03-09 03:13:06 +0000635 ++NumBuiltinMacroExpanded;
636
637 char TmpBuffer[100];
638
639 // Set up the return result.
640 Tok.setIdentifierInfo(0);
641 Tok.clearFlag(Token::NeedsCleaning);
Mike Stump11289f42009-09-09 15:08:12 +0000642
Chris Lattner89620152008-03-09 03:13:06 +0000643 if (II == Ident__LINE__) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000644 // C99 6.10.8: "__LINE__: The presumed line number (within the current
645 // source file) of the current source line (an integer constant)". This can
646 // be affected by #line.
Chris Lattner5a2e9cb2009-02-15 21:06:39 +0000647 SourceLocation Loc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000648
Chris Lattnerbf78da72009-04-18 22:29:33 +0000649 // Advance to the location of the first _, this might not be the first byte
650 // of the token if it starts with an escaped newline.
651 Loc = AdvanceToTokenCharacter(Loc, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner5a2e9cb2009-02-15 21:06:39 +0000653 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
654 // a macro instantiation. This doesn't matter for object-like macros, but
655 // can matter for a function-like macro that expands to contain __LINE__.
656 // Skip down through instantiation points until we find a file loc for the
657 // end of the instantiation history.
Chris Lattnerf52c0b22009-02-15 21:26:50 +0000658 Loc = SourceMgr.getInstantiationRange(Loc).second;
Chris Lattner5a2e9cb2009-02-15 21:06:39 +0000659 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000661 // __LINE__ expands to a simple numeric value.
662 sprintf(TmpBuffer, "%u", PLoc.getLine());
Chris Lattner89620152008-03-09 03:13:06 +0000663 Tok.setKind(tok::numeric_constant);
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000664 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000665 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000666 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
667 // character string literal)". This can be affected by #line.
668 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
669
670 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
671 // #include stack instead of the current file.
Chris Lattner89620152008-03-09 03:13:06 +0000672 if (II == Ident__BASE_FILE__) {
673 Diag(Tok, diag::ext_pp_base_file);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000674 SourceLocation NextLoc = PLoc.getIncludeLoc();
Chris Lattner89620152008-03-09 03:13:06 +0000675 while (NextLoc.isValid()) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000676 PLoc = SourceMgr.getPresumedLoc(NextLoc);
677 NextLoc = PLoc.getIncludeLoc();
Chris Lattner89620152008-03-09 03:13:06 +0000678 }
679 }
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner89620152008-03-09 03:13:06 +0000681 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000682 std::string FN = PLoc.getFilename();
Chris Lattner89620152008-03-09 03:13:06 +0000683 FN = '"' + Lexer::Stringify(FN) + '"';
684 Tok.setKind(tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000685 CreateString(&FN[0], FN.size(), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000686 } else if (II == Ident__DATE__) {
687 if (!DATELoc.isValid())
688 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
689 Tok.setKind(tok::string_literal);
690 Tok.setLength(strlen("\"Mmm dd yyyy\""));
Chris Lattner4fa23622009-01-26 00:43:02 +0000691 Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000692 Tok.getLocation(),
Chris Lattner4fa23622009-01-26 00:43:02 +0000693 Tok.getLength()));
Chris Lattner89620152008-03-09 03:13:06 +0000694 } else if (II == Ident__TIME__) {
695 if (!TIMELoc.isValid())
696 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
697 Tok.setKind(tok::string_literal);
698 Tok.setLength(strlen("\"hh:mm:ss\""));
Chris Lattner4fa23622009-01-26 00:43:02 +0000699 Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000700 Tok.getLocation(),
Chris Lattner4fa23622009-01-26 00:43:02 +0000701 Tok.getLength()));
Chris Lattner89620152008-03-09 03:13:06 +0000702 } else if (II == Ident__INCLUDE_LEVEL__) {
703 Diag(Tok, diag::ext_pp_include_level);
704
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.
721 Diag(Tok, diag::ext_pp_timestamp);
722
723 // Get the file that we are lexing out of. If we're currently lexing from
724 // a macro, dig into the include stack.
725 const FileEntry *CurFile = 0;
Ted Kremenek6552d252008-11-20 01:35:24 +0000726 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattner89620152008-03-09 03:13:06 +0000728 if (TheLexer)
Ted Kremenek2861cf42008-11-19 22:55:25 +0000729 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Mike Stump11289f42009-09-09 15:08:12 +0000730
Chris Lattner89620152008-03-09 03:13:06 +0000731 // If this file is older than the file it depends on, emit a diagnostic.
732 const char *Result;
733 if (CurFile) {
734 time_t TT = CurFile->getModificationTime();
735 struct tm *TM = localtime(&TT);
736 Result = asctime(TM);
737 } else {
738 Result = "??? ??? ?? ??:??:?? ????\n";
739 }
740 TmpBuffer[0] = '"';
Benjamin Kramer26ddfee2009-09-16 13:10:04 +0000741 unsigned Len = strlen(Result);
742 memcpy(TmpBuffer+1, Result, Len-1); // Copy string without the newline.
743 TmpBuffer[Len] = '"';
Chris Lattner89620152008-03-09 03:13:06 +0000744 Tok.setKind(tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000745 CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation());
Chris Lattner0af3ba12009-04-13 01:29:17 +0000746 } else if (II == Ident__COUNTER__) {
747 Diag(Tok, diag::ext_pp_counter);
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner0af3ba12009-04-13 01:29:17 +0000749 // __COUNTER__ expands to a simple numeric value.
750 sprintf(TmpBuffer, "%u", CounterValue++);
751 Tok.setKind(tok::numeric_constant);
752 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000753 } else if (II == Ident__has_feature ||
754 II == Ident__has_builtin) {
755 // The argument to these two builtins should be a parenthesized identifier.
756 SourceLocation StartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000758 bool IsValid = false;
759 IdentifierInfo *FeatureII = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000761 // Read the '('.
762 Lex(Tok);
763 if (Tok.is(tok::l_paren)) {
764 // Read the identifier
765 Lex(Tok);
766 if (Tok.is(tok::identifier)) {
767 FeatureII = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000769 // Read the ')'.
770 Lex(Tok);
771 if (Tok.is(tok::r_paren))
772 IsValid = true;
773 }
774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000776 bool Value = false;
777 if (!IsValid)
778 Diag(StartLoc, diag::err_feature_check_malformed);
779 else if (II == Ident__has_builtin) {
Mike Stump11289f42009-09-09 15:08:12 +0000780 // Check for a builtin is trivial.
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000781 Value = FeatureII->getBuiltinID() != 0;
782 } else {
783 assert(II == Ident__has_feature && "Must be feature check");
784 Value = HasFeature(*this, FeatureII);
785 }
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattnerb6f77af2009-06-13 07:13:28 +0000787 sprintf(TmpBuffer, "%d", (int)Value);
788 Tok.setKind(tok::numeric_constant);
789 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
John Thompsonac0b0982009-11-02 22:28:12 +0000790 } else if (II == Ident__has_include ||
791 II == Ident__has_include_next) {
792 // The argument to these two builtins should be a parenthesized
793 // file name string literal using angle brackets (<>) or
794 // double-quotes ("").
795 bool Value = false;
796 bool IsValid;
797 if (II == Ident__has_include)
798 IsValid = EvaluateHasInclude(Value, Tok, II, *this);
799 else
800 IsValid = EvaluateHasIncludeNext(Value, Tok, II, *this);
801 sprintf(TmpBuffer, "%d", (int)Value);
802 Tok.setKind(tok::numeric_constant);
803 CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation());
Chris Lattner89620152008-03-09 03:13:06 +0000804 } else {
805 assert(0 && "Unknown identifier!");
806 }
807}