blob: c7b00ea5c34d7f9e12816afe26b2d7ecd6e636d8 [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"
Eric Christopher1f84f8d2010-06-24 02:02:00 +000020#include "clang/Basic/TargetInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000021#include "clang/Lex/LexDiagnostic.h"
Douglas Gregorf29c5232010-08-24 22:20:20 +000022#include "clang/Lex/CodeCompletionHandler.h"
Douglas Gregor295a2a62010-10-30 00:23:06 +000023#include "clang/Lex/ExternalPreprocessorSource.h"
Benjamin Kramer32592e82010-01-09 18:53:11 +000024#include "llvm/ADT/StringSwitch.h"
Argyrios Kyrtzidis5b3284a2011-06-29 22:20:11 +000025#include "llvm/ADT/STLExtras.h"
Douglas Gregor6665ffb2010-11-09 05:43:53 +000026#include "llvm/Config/config.h"
Benjamin Kramerb1765912010-01-27 16:38:22 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattner3daed522009-03-02 22:20:04 +000028#include <cstdio>
Chris Lattnerf90a2482008-03-18 05:59:11 +000029#include <ctime>
Chris Lattnera3b605e2008-03-09 03:13:06 +000030using namespace clang;
31
Douglas Gregor295a2a62010-10-30 00:23:06 +000032MacroInfo *Preprocessor::getInfoForMacro(IdentifierInfo *II) const {
33 assert(II->hasMacroDefinition() && "Identifier is not a macro!");
34
35 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::const_iterator Pos
36 = Macros.find(II);
37 if (Pos == Macros.end()) {
38 // Load this macro from the external source.
39 getExternalSource()->LoadMacroDefinition(II);
40 Pos = Macros.find(II);
41 }
42 assert(Pos != Macros.end() && "Identifier macro info is missing!");
43 return Pos->second;
44}
45
Chris Lattnera3b605e2008-03-09 03:13:06 +000046/// setMacroInfo - Specify a macro for this identifier.
47///
48void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
Chris Lattner555589d2009-04-10 21:17:07 +000049 if (MI) {
Chris Lattnera3b605e2008-03-09 03:13:06 +000050 Macros[II] = MI;
51 II->setHasMacroDefinition(true);
Chris Lattner555589d2009-04-10 21:17:07 +000052 } else if (II->hasMacroDefinition()) {
53 Macros.erase(II);
54 II->setHasMacroDefinition(false);
Chris Lattnera3b605e2008-03-09 03:13:06 +000055 }
56}
57
58/// RegisterBuiltinMacro - Register the specified identifier in the identifier
59/// table and mark it as a builtin macro to be expanded.
Chris Lattner148772a2009-06-13 07:13:28 +000060static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
Chris Lattnera3b605e2008-03-09 03:13:06 +000061 // Get the identifier.
Chris Lattner148772a2009-06-13 07:13:28 +000062 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattnera3b605e2008-03-09 03:13:06 +000064 // Mark it as being a macro that is builtin.
Chris Lattner148772a2009-06-13 07:13:28 +000065 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +000066 MI->setIsBuiltinMacro();
Chris Lattner148772a2009-06-13 07:13:28 +000067 PP.setMacroInfo(Id, MI);
Chris Lattnera3b605e2008-03-09 03:13:06 +000068 return Id;
69}
70
71
72/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
73/// identifier table.
74void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner148772a2009-06-13 07:13:28 +000075 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
76 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
77 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
78 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
79 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
80 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattnera3b605e2008-03-09 03:13:06 +000082 // GCC Extensions.
Chris Lattner148772a2009-06-13 07:13:28 +000083 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
84 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
85 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner148772a2009-06-13 07:13:28 +000087 // Clang Extensions.
John Thompson92bd8c72009-11-02 22:28:12 +000088 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
Peter Collingbournec1b5fa42011-05-13 20:54:45 +000089 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
John Thompson92bd8c72009-11-02 22:28:12 +000090 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
Anders Carlssoncae50952010-10-20 02:31:43 +000091 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
John Thompson92bd8c72009-11-02 22:28:12 +000092 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
93 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
John McCall1ef8a2e2010-08-28 22:34:47 +000094
95 // Microsoft Extensions.
96 if (Features.Microsoft)
97 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
98 else
99 Ident__pragma = 0;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000100}
101
102/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
103/// in its expansion, currently expands to that token literally.
104static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
105 const IdentifierInfo *MacroIdent,
106 Preprocessor &PP) {
107 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
108
109 // If the token isn't an identifier, it's always literally expanded.
110 if (II == 0) return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattnera3b605e2008-03-09 03:13:06 +0000112 // If the identifier is a macro, and if that macro is enabled, it may be
113 // expanded so it's not a trivial expansion.
114 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
115 // Fast expanding "#define X X" is ok, because X would be disabled.
116 II != MacroIdent)
117 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattnera3b605e2008-03-09 03:13:06 +0000119 // If this is an object-like macro invocation, it is safe to trivially expand
120 // it.
121 if (MI->isObjectLike()) return true;
122
123 // If this is a function-like macro invocation, it's safe to trivially expand
124 // as long as the identifier is not a macro argument.
125 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
126 I != E; ++I)
127 if (*I == II)
128 return false; // Identifier is a macro argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattnera3b605e2008-03-09 03:13:06 +0000130 return true;
131}
132
133
134/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
135/// lexed is a '('. If so, consume the token and return true, if not, this
136/// method should have no observable side-effect on the lexed tokens.
137bool Preprocessor::isNextPPTokenLParen() {
138 // Do some quick tests for rejection cases.
139 unsigned Val;
140 if (CurLexer)
141 Val = CurLexer->isNextPPTokenLParen();
Ted Kremenek1a531572008-11-19 22:43:49 +0000142 else if (CurPTHLexer)
143 Val = CurPTHLexer->isNextPPTokenLParen();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000144 else
145 Val = CurTokenLexer->isNextTokenLParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Chris Lattnera3b605e2008-03-09 03:13:06 +0000147 if (Val == 2) {
148 // We have run off the end. If it's a source file we don't
149 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
150 // macro stack.
Ted Kremenek17ff58a2008-11-19 22:21:33 +0000151 if (CurPPLexer)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000152 return false;
153 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
154 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
155 if (Entry.TheLexer)
156 Val = Entry.TheLexer->isNextPPTokenLParen();
Ted Kremenekdd95d6c2008-11-20 16:46:54 +0000157 else if (Entry.ThePTHLexer)
158 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000159 else
160 Val = Entry.TheTokenLexer->isNextTokenLParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattnera3b605e2008-03-09 03:13:06 +0000162 if (Val != 2)
163 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattnera3b605e2008-03-09 03:13:06 +0000165 // Ran off the end of a source file?
Ted Kremenekdd95d6c2008-11-20 16:46:54 +0000166 if (Entry.ThePPLexer)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000167 return false;
168 }
169 }
170
171 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
172 // have found something that isn't a '(' or we found the end of the
173 // translation unit. In either case, return false.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000174 return Val == 1;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000175}
176
177/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
178/// expanded as a macro, handle it and return the next token as 'Identifier'.
Mike Stump1eb44332009-09-09 15:08:12 +0000179bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000180 MacroInfo *MI) {
Douglas Gregor13678972010-01-26 19:43:43 +0000181 // If this is a macro expansion in the "#if !defined(x)" line for the file,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000182 // then the macro could expand to different things in other contexts, we need
183 // to disable the optimization in this case.
Ted Kremenek68a91d52008-11-18 01:12:54 +0000184 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattnera3b605e2008-03-09 03:13:06 +0000186 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
187 if (MI->isBuiltinMacro()) {
Argyrios Kyrtzidis1b2d5362011-08-18 01:05:45 +0000188 if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
189 Identifier.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000190 ExpandBuiltinMacro(Identifier);
191 return false;
192 }
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattnera3b605e2008-03-09 03:13:06 +0000194 /// Args - If this is a function-like macro expansion, this contains,
195 /// for each macro argument, the list of tokens that were provided to the
196 /// invocation.
197 MacroArgs *Args = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000199 // Remember where the end of the expansion occurred. For an object-like
Chris Lattnere7fb4842009-02-15 20:52:18 +0000200 // macro, this is the identifier. For a function-like macro, this is the ')'.
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000201 SourceLocation ExpansionEnd = Identifier.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Chris Lattnera3b605e2008-03-09 03:13:06 +0000203 // If this is a function-like macro, read the arguments.
204 if (MI->isFunctionLike()) {
205 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000206 // name isn't a '(', this macro should not be expanded.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000207 if (!isNextPPTokenLParen())
208 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Chris Lattnera3b605e2008-03-09 03:13:06 +0000210 // Remember that we are now parsing the arguments to a macro invocation.
211 // Preprocessor directives used inside macro arguments are not portable, and
212 // this enables the warning.
213 InMacroArgs = true;
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000214 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattnera3b605e2008-03-09 03:13:06 +0000216 // Finished parsing args.
217 InMacroArgs = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattnera3b605e2008-03-09 03:13:06 +0000219 // If there was an error parsing the arguments, bail out.
220 if (Args == 0) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattnera3b605e2008-03-09 03:13:06 +0000222 ++NumFnMacroExpanded;
223 } else {
224 ++NumMacroExpanded;
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattnera3b605e2008-03-09 03:13:06 +0000227 // Notice that this macro has been used.
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000228 markMacroAsUsed(MI);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000230 // Remember where the token is expanded.
231 SourceLocation ExpandLoc = Identifier.getLocation();
Argyrios Kyrtzidisb7d98d32011-04-27 05:04:02 +0000232
Argyrios Kyrtzidis1b2d5362011-08-18 01:05:45 +0000233 if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
234 SourceRange(ExpandLoc, ExpansionEnd));
235
236 // If we started lexing a macro, enter the macro expansion body.
237
Chris Lattnera3b605e2008-03-09 03:13:06 +0000238 // If this macro expands to no tokens, don't bother to push it onto the
239 // expansion stack, only to take it right back off.
240 if (MI->getNumTokens() == 0) {
241 // No need for arg info.
Chris Lattner561395b2009-12-14 22:12:52 +0000242 if (Args) Args->destroy(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattnera3b605e2008-03-09 03:13:06 +0000244 // Ignore this macro use, just return the next token in the current
245 // buffer.
246 bool HadLeadingSpace = Identifier.hasLeadingSpace();
247 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnera3b605e2008-03-09 03:13:06 +0000249 Lex(Identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Chris Lattnera3b605e2008-03-09 03:13:06 +0000251 // If the identifier isn't on some OTHER line, inherit the leading
252 // whitespace/first-on-a-line property of this token. This handles
253 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
254 // empty.
255 if (!Identifier.isAtStartOfLine()) {
256 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
257 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
258 }
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000259 Identifier.setFlag(Token::LeadingEmptyMacro);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000260 ++NumFastMacroExpanded;
261 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattnera3b605e2008-03-09 03:13:06 +0000263 } else if (MI->getNumTokens() == 1 &&
264 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000265 *this)) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000266 // Otherwise, if this macro expands into a single trivially-expanded
Mike Stump1eb44332009-09-09 15:08:12 +0000267 // token: expand it now. This handles common cases like
Chris Lattnera3b605e2008-03-09 03:13:06 +0000268 // "#define VAL 42".
Sam Bishop9a4939f2008-03-21 07:13:02 +0000269
270 // No need for arg info.
Chris Lattner561395b2009-12-14 22:12:52 +0000271 if (Args) Args->destroy(*this);
Sam Bishop9a4939f2008-03-21 07:13:02 +0000272
Chris Lattnera3b605e2008-03-09 03:13:06 +0000273 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
274 // identifier to the expanded token.
275 bool isAtStartOfLine = Identifier.isAtStartOfLine();
276 bool hasLeadingSpace = Identifier.hasLeadingSpace();
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattnera3b605e2008-03-09 03:13:06 +0000278 // Replace the result token.
279 Identifier = MI->getReplacementToken(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattnera3b605e2008-03-09 03:13:06 +0000281 // Restore the StartOfLine/LeadingSpace markers.
282 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
283 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000285 // Update the tokens location to include both its expansion and physical
Chris Lattnera3b605e2008-03-09 03:13:06 +0000286 // locations.
287 SourceLocation Loc =
Chandler Carruthbf340e42011-07-26 03:03:05 +0000288 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
289 ExpansionEnd,Identifier.getLength());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000290 Identifier.setLocation(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Chris Lattner8ff66de2010-03-26 17:49:16 +0000292 // If this is a disabled macro or #define X X, we must mark the result as
293 // unexpandable.
294 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
295 if (MacroInfo *NewMI = getMacroInfo(NewII))
296 if (!NewMI->isEnabled() || NewMI == MI)
297 Identifier.setFlag(Token::DisableExpand);
298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Chris Lattnera3b605e2008-03-09 03:13:06 +0000300 // Since this is not an identifier token, it can't be macro expanded, so
301 // we're done.
302 ++NumFastMacroExpanded;
303 return false;
304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattnera3b605e2008-03-09 03:13:06 +0000306 // Start expanding the macro.
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000307 EnterMacro(Identifier, ExpansionEnd, Args);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattnera3b605e2008-03-09 03:13:06 +0000309 // Now that the macro is at the top of the include stack, ask the
310 // preprocessor to read the next token from it.
311 Lex(Identifier);
312 return false;
313}
314
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000315/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
316/// token is the '(' of the macro, this method is invoked to read all of the
317/// actual arguments specified for the macro invocation. This returns null on
318/// error.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000319MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000320 MacroInfo *MI,
321 SourceLocation &MacroEnd) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000322 // The number of fixed arguments to parse.
323 unsigned NumFixedArgsLeft = MI->getNumArgs();
324 bool isVariadic = MI->isVariadic();
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattnera3b605e2008-03-09 03:13:06 +0000326 // Outer loop, while there are more arguments, keep reading them.
327 Token Tok;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000328
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000329 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
330 // an argument value in a macro could expand to ',' or '(' or ')'.
331 LexUnexpandedToken(Tok);
332 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattnera3b605e2008-03-09 03:13:06 +0000334 // ArgTokens - Build up a list of tokens that make up each argument. Each
335 // argument is separated by an EOF token. Use a SmallVector so we can avoid
336 // heap allocations in the common case.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000337 SmallVector<Token, 64> ArgTokens;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000338
339 unsigned NumActuals = 0;
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000340 while (Tok.isNot(tok::r_paren)) {
341 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
342 "only expect argument separators here");
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000344 unsigned ArgTokenStart = ArgTokens.size();
345 SourceLocation ArgStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattnera3b605e2008-03-09 03:13:06 +0000347 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
348 // that we already consumed the first one.
349 unsigned NumParens = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattnera3b605e2008-03-09 03:13:06 +0000351 while (1) {
352 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
353 // an argument value in a macro could expand to ',' or '(' or ')'.
354 LexUnexpandedToken(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Peter Collingbourne84021552011-02-28 02:37:51 +0000356 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
Chris Lattnera3b605e2008-03-09 03:13:06 +0000357 Diag(MacroName, diag::err_unterm_macro_invoc);
Peter Collingbourne84021552011-02-28 02:37:51 +0000358 // Do not lose the EOF/EOD. Return it to the client.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000359 MacroName = Tok;
360 return 0;
361 } else if (Tok.is(tok::r_paren)) {
362 // If we found the ) token, the macro arg list is done.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000363 if (NumParens-- == 0) {
364 MacroEnd = Tok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000365 break;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000366 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000367 } else if (Tok.is(tok::l_paren)) {
368 ++NumParens;
369 } else if (Tok.is(tok::comma) && NumParens == 0) {
370 // Comma ends this argument if there are more fixed arguments expected.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000371 // However, if this is a variadic macro, and this is part of the
Mike Stump1eb44332009-09-09 15:08:12 +0000372 // variadic part, then the comma is just an argument token.
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000373 if (!isVariadic) break;
374 if (NumFixedArgsLeft > 1)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000375 break;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000376 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
377 // If this is a comment token in the argument list and we're just in
378 // -C mode (not -CC mode), discard the comment.
379 continue;
Chris Lattner5c497a82009-04-18 06:44:18 +0000380 } else if (Tok.getIdentifierInfo() != 0) {
Chris Lattnera3b605e2008-03-09 03:13:06 +0000381 // Reading macro arguments can cause macros that we are currently
382 // expanding from to be popped off the expansion stack. Doing so causes
383 // them to be reenabled for expansion. Here we record whether any
384 // identifiers we lex as macro arguments correspond to disabled macros.
Mike Stump1eb44332009-09-09 15:08:12 +0000385 // If so, we mark the token as noexpand. This is a subtle aspect of
Chris Lattnera3b605e2008-03-09 03:13:06 +0000386 // C99 6.10.3.4p2.
387 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
388 if (!MI->isEnabled())
389 Tok.setFlag(Token::DisableExpand);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000390 } else if (Tok.is(tok::code_completion)) {
391 if (CodeComplete)
392 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
393 MI, NumActuals);
394 // Don't mark that we reached the code-completion point because the
395 // parser is going to handle the token and there will be another
396 // code-completion callback.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000397 }
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000398
Chris Lattnera3b605e2008-03-09 03:13:06 +0000399 ArgTokens.push_back(Tok);
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000402 // If this was an empty argument list foo(), don't add this as an empty
403 // argument.
404 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
405 break;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000406
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000407 // If this is not a variadic macro, and too many args were specified, emit
408 // an error.
409 if (!isVariadic && NumFixedArgsLeft == 0) {
410 if (ArgTokens.size() != ArgTokenStart)
411 ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000413 // Emit the diagnostic at the macro name in case there is a missing ).
414 // Emitting it at the , could be far away from the macro name.
415 Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
416 return 0;
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Chris Lattner32c13882011-04-22 23:25:09 +0000419 // Empty arguments are standard in C99 and C++0x, and are supported as an extension in
Chris Lattnera3b605e2008-03-09 03:13:06 +0000420 // other modes.
Chris Lattner32c13882011-04-22 23:25:09 +0000421 if (ArgTokens.size() == ArgTokenStart && !Features.C99 && !Features.CPlusPlus0x)
Chris Lattnera3b605e2008-03-09 03:13:06 +0000422 Diag(Tok, diag::ext_empty_fnmacro_arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnera3b605e2008-03-09 03:13:06 +0000424 // Add a marker EOF token to the end of the token list for this argument.
425 Token EOFTok;
426 EOFTok.startToken();
427 EOFTok.setKind(tok::eof);
Chris Lattnere7689882009-01-26 20:24:53 +0000428 EOFTok.setLocation(Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +0000429 EOFTok.setLength(0);
430 ArgTokens.push_back(EOFTok);
431 ++NumActuals;
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000432 assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
Chris Lattnera3b605e2008-03-09 03:13:06 +0000433 --NumFixedArgsLeft;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattnera3b605e2008-03-09 03:13:06 +0000436 // Okay, we either found the r_paren. Check to see if we parsed too few
437 // arguments.
438 unsigned MinArgsExpected = MI->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattnera3b605e2008-03-09 03:13:06 +0000440 // See MacroArgs instance var for description of this.
441 bool isVarargsElided = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattnera3b605e2008-03-09 03:13:06 +0000443 if (NumActuals < MinArgsExpected) {
444 // There are several cases where too few arguments is ok, handle them now.
Chris Lattner97e2de12009-04-20 21:08:10 +0000445 if (NumActuals == 0 && MinArgsExpected == 1) {
446 // #define A(X) or #define A(...) ---> A()
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner97e2de12009-04-20 21:08:10 +0000448 // If there is exactly one argument, and that argument is missing,
449 // then we have an empty "()" argument empty list. This is fine, even if
450 // the macro expects one argument (the argument is just empty).
451 isVarargsElided = MI->isVariadic();
452 } else if (MI->isVariadic() &&
453 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
454 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
Chris Lattnera3b605e2008-03-09 03:13:06 +0000455 // Varargs where the named vararg parameter is missing: ok as extension.
456 // #define A(x, ...)
457 // A("blah")
458 Diag(Tok, diag::ext_missing_varargs_arg);
459
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000460 // Remember this occurred, allowing us to elide the comma when used for
Chris Lattner63bc0352008-05-08 05:10:33 +0000461 // cases like:
Mike Stump1eb44332009-09-09 15:08:12 +0000462 // #define A(x, foo...) blah(a, ## foo)
463 // #define B(x, ...) blah(a, ## __VA_ARGS__)
464 // #define C(...) blah(a, ## __VA_ARGS__)
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000465 // A(x) B(x) C()
Chris Lattner97e2de12009-04-20 21:08:10 +0000466 isVarargsElided = true;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000467 } else {
468 // Otherwise, emit the error.
469 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
470 return 0;
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattnera3b605e2008-03-09 03:13:06 +0000473 // Add a marker EOF token to the end of the token list for this argument.
474 SourceLocation EndLoc = Tok.getLocation();
475 Tok.startToken();
476 Tok.setKind(tok::eof);
477 Tok.setLocation(EndLoc);
478 Tok.setLength(0);
479 ArgTokens.push_back(Tok);
Chris Lattner9fc9e772009-05-13 00:55:26 +0000480
481 // If we expect two arguments, add both as empty.
482 if (NumActuals == 0 && MinArgsExpected == 2)
483 ArgTokens.push_back(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattner0a4f1b92009-04-18 01:13:56 +0000485 } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
486 // Emit the diagnostic at the macro name in case there is a missing ).
487 // Emitting it at the , could be far away from the macro name.
488 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
489 return 0;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Jay Foadbeaaccd2009-05-21 09:52:38 +0000492 return MacroArgs::create(MI, ArgTokens.data(), ArgTokens.size(),
Chris Lattner561395b2009-12-14 22:12:52 +0000493 isVarargsElided, *this);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000494}
495
Argyrios Kyrtzidis5b3284a2011-06-29 22:20:11 +0000496/// \brief Keeps macro expanded tokens for TokenLexers.
497//
498/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
499/// going to lex in the cache and when it finishes the tokens are removed
500/// from the end of the cache.
501Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +0000502 ArrayRef<Token> tokens) {
Argyrios Kyrtzidis5b3284a2011-06-29 22:20:11 +0000503 assert(tokLexer);
504 if (tokens.empty())
505 return 0;
506
507 size_t newIndex = MacroExpandedTokens.size();
508 bool cacheNeedsToGrow = tokens.size() >
509 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
510 MacroExpandedTokens.append(tokens.begin(), tokens.end());
511
512 if (cacheNeedsToGrow) {
513 // Go through all the TokenLexers whose 'Tokens' pointer points in the
514 // buffer and update the pointers to the (potential) new buffer array.
515 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
516 TokenLexer *prevLexer;
517 size_t tokIndex;
518 llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
519 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
520 }
521 }
522
523 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
524 return MacroExpandedTokens.data() + newIndex;
525}
526
527void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
528 assert(!MacroExpandingLexersStack.empty());
529 size_t tokIndex = MacroExpandingLexersStack.back().second;
530 assert(tokIndex < MacroExpandedTokens.size());
531 // Pop the cached macro expanded tokens from the end.
532 MacroExpandedTokens.resize(tokIndex);
533 MacroExpandingLexersStack.pop_back();
534}
535
Chris Lattnera3b605e2008-03-09 03:13:06 +0000536/// ComputeDATE_TIME - Compute the current time, enter it into the specified
537/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
538/// the identifier tokens inserted.
539static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
540 Preprocessor &PP) {
541 time_t TT = time(0);
542 struct tm *TM = localtime(&TT);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnera3b605e2008-03-09 03:13:06 +0000544 static const char * const Months[] = {
545 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
546 };
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor5e0fb352010-11-09 03:20:07 +0000548 char TmpBuffer[32];
Douglas Gregorb87b29e2010-11-09 04:38:09 +0000549#ifdef LLVM_ON_WIN32
550 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
551 TM->tm_year+1900);
552#else
Douglas Gregor5e0fb352010-11-09 03:20:07 +0000553 snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
Chris Lattnera3b605e2008-03-09 03:13:06 +0000554 TM->tm_year+1900);
Douglas Gregorb87b29e2010-11-09 04:38:09 +0000555#endif
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner47246be2009-01-26 19:29:26 +0000557 Token TmpTok;
558 TmpTok.startToken();
559 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
560 DATELoc = TmpTok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000561
NAKAMURA Takumi513038d2010-11-09 06:27:32 +0000562#ifdef LLVM_ON_WIN32
563 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
564#else
Douglas Gregor5e0fb352010-11-09 03:20:07 +0000565 snprintf(TmpBuffer, sizeof(TmpBuffer), "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
NAKAMURA Takumi513038d2010-11-09 06:27:32 +0000566#endif
Chris Lattner47246be2009-01-26 19:29:26 +0000567 PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok);
568 TIMELoc = TmpTok.getLocation();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000569}
570
Chris Lattner148772a2009-06-13 07:13:28 +0000571
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000572/// HasFeature - Return true if we recognize and implement the feature
573/// specified by the identifier as a standard language feature.
Chris Lattner148772a2009-06-13 07:13:28 +0000574static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
575 const LangOptions &LangOpts = PP.getLangOptions();
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Benjamin Kramer32592e82010-01-09 18:53:11 +0000577 return llvm::StringSwitch<bool>(II->getName())
Benjamin Kramer32592e82010-01-09 18:53:11 +0000578 .Case("attribute_analyzer_noreturn", true)
Douglas Gregordceb5312011-03-26 12:16:15 +0000579 .Case("attribute_availability", true)
Ted Kremenek13593002010-02-18 00:06:04 +0000580 .Case("attribute_cf_returns_not_retained", true)
Benjamin Kramer32592e82010-01-09 18:53:11 +0000581 .Case("attribute_cf_returns_retained", true)
John McCall48209082010-11-08 19:48:17 +0000582 .Case("attribute_deprecated_with_message", true)
Ted Kremenek6d9afd92010-04-29 02:06:42 +0000583 .Case("attribute_ext_vector_type", true)
Ted Kremenek13593002010-02-18 00:06:04 +0000584 .Case("attribute_ns_returns_not_retained", true)
585 .Case("attribute_ns_returns_retained", true)
Ted Kremenek12b94342011-01-27 06:54:14 +0000586 .Case("attribute_ns_consumes_self", true)
Ted Kremenek11fe1752011-01-27 18:43:03 +0000587 .Case("attribute_ns_consumed", true)
588 .Case("attribute_cf_consumed", true)
Ted Kremenek444b0352010-03-05 22:43:32 +0000589 .Case("attribute_objc_ivar_unused", true)
John McCalld5313b02011-03-02 11:33:24 +0000590 .Case("attribute_objc_method_family", true)
Ted Kremenek6d9afd92010-04-29 02:06:42 +0000591 .Case("attribute_overloadable", true)
John McCall48209082010-11-08 19:48:17 +0000592 .Case("attribute_unavailable_with_message", true)
Ted Kremenek6d9afd92010-04-29 02:06:42 +0000593 .Case("blocks", LangOpts.Blocks)
Ted Kremenek6d9afd92010-04-29 02:06:42 +0000594 .Case("cxx_exceptions", LangOpts.Exceptions)
595 .Case("cxx_rtti", LangOpts.RTTI)
John McCall48209082010-11-08 19:48:17 +0000596 .Case("enumerator_attributes", true)
John McCallf85e1932011-06-15 23:02:42 +0000597 // Objective-C features
598 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
599 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
600 .Case("objc_arc_weak", LangOpts.ObjCAutoRefCount &&
John McCall9f084a32011-07-06 00:26:06 +0000601 LangOpts.ObjCRuntimeHasWeak)
Douglas Gregor5471bc82011-09-08 17:18:35 +0000602 .Case("objc_fixed_enum", LangOpts.ObjC2)
Douglas Gregore97179c2011-09-08 01:46:34 +0000603 .Case("objc_instancetype", LangOpts.ObjC2)
Ted Kremenek6d9afd92010-04-29 02:06:42 +0000604 .Case("objc_nonfragile_abi", LangOpts.ObjCNonFragileABI)
Ted Kremenek3ff9d112010-04-29 02:06:46 +0000605 .Case("objc_weak_class", LangOpts.ObjCNonFragileABI)
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000606 .Case("ownership_holds", true)
607 .Case("ownership_returns", true)
608 .Case("ownership_takes", true)
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000609 // C1X features
610 .Case("c_generic_selections", LangOpts.C1X)
611 .Case("c_static_assert", LangOpts.C1X)
Douglas Gregorc78e2592011-01-26 15:36:03 +0000612 // C++0x features
Douglas Gregor7822ee32011-05-11 23:45:11 +0000613 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000614 .Case("cxx_alias_templates", LangOpts.CPlusPlus0x)
Douglas Gregorc78e2592011-01-26 15:36:03 +0000615 .Case("cxx_attributes", LangOpts.CPlusPlus0x)
Richard Smith738291e2011-02-20 12:13:05 +0000616 .Case("cxx_auto_type", LangOpts.CPlusPlus0x)
Douglas Gregorece38942011-08-29 17:28:38 +0000617 //.Case("cxx_constexpr", false);
Douglas Gregorc78e2592011-01-26 15:36:03 +0000618 .Case("cxx_decltype", LangOpts.CPlusPlus0x)
Douglas Gregor07508002011-02-05 20:35:30 +0000619 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x)
Sean Hunt059ce0d2011-05-01 07:04:31 +0000620 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x)
Douglas Gregorc78e2592011-01-26 15:36:03 +0000621 .Case("cxx_deleted_functions", LangOpts.CPlusPlus0x)
Douglas Gregorece38942011-08-29 17:28:38 +0000622 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x)
Sean Hunte1f6dea2011-08-07 00:34:32 +0000623 //.Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x)
Sebastian Redl74e611a2011-09-04 18:14:28 +0000624 .Case("cxx_implicit_moves", LangOpts.CPlusPlus0x)
Douglas Gregorece38942011-08-29 17:28:38 +0000625 //.Case("cxx_inheriting_constructors", false)
Douglas Gregorc78e2592011-01-26 15:36:03 +0000626 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x)
Ted Kremenek6d9afd92010-04-29 02:06:42 +0000627 //.Case("cxx_lambdas", false)
Douglas Gregorece38942011-08-29 17:28:38 +0000628 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x)
Sebastian Redl4561ecd2011-03-15 21:17:12 +0000629 .Case("cxx_noexcept", LangOpts.CPlusPlus0x)
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000630 .Case("cxx_nullptr", LangOpts.CPlusPlus0x)
Anders Carlssonc8b9f792011-03-25 15:04:23 +0000631 .Case("cxx_override_control", LangOpts.CPlusPlus0x)
Richard Smitha391a462011-04-15 15:14:40 +0000632 .Case("cxx_range_for", LangOpts.CPlusPlus0x)
Douglas Gregorece38942011-08-29 17:28:38 +0000633 //.Case("cxx_raw_string_literals", false)
Douglas Gregor56209ff2011-01-26 21:25:54 +0000634 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x)
Douglas Gregorc78e2592011-01-26 15:36:03 +0000635 .Case("cxx_rvalue_references", LangOpts.CPlusPlus0x)
636 .Case("cxx_strong_enums", LangOpts.CPlusPlus0x)
637 .Case("cxx_static_assert", LangOpts.CPlusPlus0x)
638 .Case("cxx_trailing_return", LangOpts.CPlusPlus0x)
Douglas Gregorece38942011-08-29 17:28:38 +0000639 //.Case("cxx_unicode_literals", false)
640 //.Case("cxx_unrestricted_unions", false)
641 //.Case("cxx_user_literals", false)
Douglas Gregorc78e2592011-01-26 15:36:03 +0000642 .Case("cxx_variadic_templates", LangOpts.CPlusPlus0x)
Douglas Gregorafdf1372011-02-03 21:57:35 +0000643 // Type traits
644 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
645 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
646 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
647 .Case("has_trivial_assign", LangOpts.CPlusPlus)
648 .Case("has_trivial_copy", LangOpts.CPlusPlus)
649 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
650 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
651 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
652 .Case("is_abstract", LangOpts.CPlusPlus)
653 .Case("is_base_of", LangOpts.CPlusPlus)
654 .Case("is_class", LangOpts.CPlusPlus)
655 .Case("is_convertible_to", LangOpts.CPlusPlus)
Douglas Gregorb3f8c242011-08-03 17:01:05 +0000656 // __is_empty is available only if the horrible
657 // "struct __is_empty" parsing hack hasn't been needed in this
658 // translation unit. If it has, __is_empty reverts to a normal
659 // identifier and __has_feature(is_empty) evaluates false.
Douglas Gregor68876142011-07-30 07:01:49 +0000660 .Case("is_empty",
Douglas Gregor9a14ecb2011-07-30 07:08:19 +0000661 LangOpts.CPlusPlus &&
662 PP.getIdentifierInfo("__is_empty")->getTokenID()
663 != tok::identifier)
Douglas Gregorafdf1372011-02-03 21:57:35 +0000664 .Case("is_enum", LangOpts.CPlusPlus)
Chandler Carruth4e61ddd2011-04-23 10:47:20 +0000665 .Case("is_literal", LangOpts.CPlusPlus)
Howard Hinnanta55e68b2011-05-12 19:52:14 +0000666 .Case("is_standard_layout", LangOpts.CPlusPlus)
Douglas Gregorb3f8c242011-08-03 17:01:05 +0000667 // __is_pod is available only if the horrible
668 // "struct __is_pod" parsing hack hasn't been needed in this
669 // translation unit. If it has, __is_pod reverts to a normal
670 // identifier and __has_feature(is_pod) evaluates false.
Douglas Gregor68876142011-07-30 07:01:49 +0000671 .Case("is_pod",
Douglas Gregor9a14ecb2011-07-30 07:08:19 +0000672 LangOpts.CPlusPlus &&
673 PP.getIdentifierInfo("__is_pod")->getTokenID()
674 != tok::identifier)
Douglas Gregorafdf1372011-02-03 21:57:35 +0000675 .Case("is_polymorphic", LangOpts.CPlusPlus)
Chandler Carruthb7e95892011-04-23 10:47:28 +0000676 .Case("is_trivial", LangOpts.CPlusPlus)
Sean Huntfeb375d2011-05-13 00:31:07 +0000677 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
Douglas Gregorafdf1372011-02-03 21:57:35 +0000678 .Case("is_union", LangOpts.CPlusPlus)
Eric Christopher1f84f8d2010-06-24 02:02:00 +0000679 .Case("tls", PP.getTargetInfo().isTLSSupported())
Sean Hunt858a3252011-07-18 17:08:00 +0000680 .Case("underlying_type", LangOpts.CPlusPlus)
Benjamin Kramer32592e82010-01-09 18:53:11 +0000681 .Default(false);
Chris Lattner148772a2009-06-13 07:13:28 +0000682}
683
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000684/// HasExtension - Return true if we recognize and implement the feature
685/// specified by the identifier, either as an extension or a standard language
686/// feature.
687static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
688 if (HasFeature(PP, II))
689 return true;
690
691 // If the use of an extension results in an error diagnostic, extensions are
692 // effectively unavailable, so just return false here.
693 if (PP.getDiagnostics().getExtensionHandlingBehavior()==Diagnostic::Ext_Error)
694 return false;
695
696 const LangOptions &LangOpts = PP.getLangOptions();
697
698 // Because we inherit the feature list from HasFeature, this string switch
699 // must be less restrictive than HasFeature's.
700 return llvm::StringSwitch<bool>(II->getName())
701 // C1X features supported by other languages as extensions.
702 .Case("c_generic_selections", true)
703 .Case("c_static_assert", true)
704 // C++0x features supported by other languages as extensions.
705 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
Douglas Gregorece38942011-08-29 17:28:38 +0000706 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000707 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
Douglas Gregorece38942011-08-29 17:28:38 +0000708 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000709 .Case("cxx_override_control", LangOpts.CPlusPlus)
Richard Smith7640c002011-09-06 18:03:41 +0000710 .Case("cxx_range_for", LangOpts.CPlusPlus)
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000711 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
712 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
713 .Default(false);
714}
715
Anders Carlssoncae50952010-10-20 02:31:43 +0000716/// HasAttribute - Return true if we recognize and implement the attribute
717/// specified by the given identifier.
718static bool HasAttribute(const IdentifierInfo *II) {
719 return llvm::StringSwitch<bool>(II->getName())
720#include "clang/Lex/AttrSpellings.inc"
721 .Default(false);
722}
723
John Thompson92bd8c72009-11-02 22:28:12 +0000724/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
725/// or '__has_include_next("path")' expression.
726/// Returns true if successful.
Chris Lattner3ed572e2011-01-15 06:57:04 +0000727static bool EvaluateHasIncludeCommon(Token &Tok,
728 IdentifierInfo *II, Preprocessor &PP,
729 const DirectoryLookup *LookupFrom) {
John Thompson92bd8c72009-11-02 22:28:12 +0000730 SourceLocation LParenLoc;
731
732 // Get '('.
733 PP.LexNonComment(Tok);
734
735 // Ensure we have a '('.
736 if (Tok.isNot(tok::l_paren)) {
737 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
738 return false;
739 }
740
741 // Save '(' location for possible missing ')' message.
742 LParenLoc = Tok.getLocation();
743
744 // Get the file name.
745 PP.getCurrentLexer()->LexIncludeFilename(Tok);
746
747 // Reserve a buffer to get the spelling.
Chris Lattnera1394812010-01-10 01:35:12 +0000748 llvm::SmallString<128> FilenameBuffer;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000749 StringRef Filename;
Douglas Gregorecdcb882010-10-20 22:00:55 +0000750 SourceLocation EndLoc;
751
John Thompson92bd8c72009-11-02 22:28:12 +0000752 switch (Tok.getKind()) {
Peter Collingbourne84021552011-02-28 02:37:51 +0000753 case tok::eod:
754 // If the token kind is EOD, the error has already been diagnosed.
John Thompson92bd8c72009-11-02 22:28:12 +0000755 return false;
756
757 case tok::angle_string_literal:
Douglas Gregor453091c2010-03-16 22:30:13 +0000758 case tok::string_literal: {
759 bool Invalid = false;
760 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
761 if (Invalid)
762 return false;
John Thompson92bd8c72009-11-02 22:28:12 +0000763 break;
Douglas Gregor453091c2010-03-16 22:30:13 +0000764 }
John Thompson92bd8c72009-11-02 22:28:12 +0000765
766 case tok::less:
767 // This could be a <foo/bar.h> file coming from a macro expansion. In this
768 // case, glue the tokens together into FilenameBuffer and interpret those.
769 FilenameBuffer.push_back('<');
Douglas Gregorecdcb882010-10-20 22:00:55 +0000770 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc))
Peter Collingbourne84021552011-02-28 02:37:51 +0000771 return false; // Found <eod> but no ">"? Diagnostic already emitted.
Chris Lattnera1394812010-01-10 01:35:12 +0000772 Filename = FilenameBuffer.str();
John Thompson92bd8c72009-11-02 22:28:12 +0000773 break;
774 default:
775 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
776 return false;
777 }
778
Chris Lattnera1394812010-01-10 01:35:12 +0000779 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
John Thompson92bd8c72009-11-02 22:28:12 +0000780 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
781 // error.
Chris Lattnera1394812010-01-10 01:35:12 +0000782 if (Filename.empty())
John Thompson92bd8c72009-11-02 22:28:12 +0000783 return false;
John Thompson92bd8c72009-11-02 22:28:12 +0000784
785 // Search include directories.
786 const DirectoryLookup *CurDir;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000787 const FileEntry *File =
Manuel Klimek74124942011-04-26 21:50:03 +0000788 PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL);
John Thompson92bd8c72009-11-02 22:28:12 +0000789
790 // Get the result value. Result = true means the file exists.
Chris Lattner3ed572e2011-01-15 06:57:04 +0000791 bool Result = File != 0;
John Thompson92bd8c72009-11-02 22:28:12 +0000792
793 // Get ')'.
794 PP.LexNonComment(Tok);
795
796 // Ensure we have a trailing ).
797 if (Tok.isNot(tok::r_paren)) {
798 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
799 PP.Diag(LParenLoc, diag::note_matching) << "(";
800 return false;
801 }
802
Chris Lattner3ed572e2011-01-15 06:57:04 +0000803 return Result;
John Thompson92bd8c72009-11-02 22:28:12 +0000804}
805
806/// EvaluateHasInclude - Process a '__has_include("path")' expression.
807/// Returns true if successful.
Chris Lattner3ed572e2011-01-15 06:57:04 +0000808static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
John Thompson92bd8c72009-11-02 22:28:12 +0000809 Preprocessor &PP) {
Chris Lattner3ed572e2011-01-15 06:57:04 +0000810 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
John Thompson92bd8c72009-11-02 22:28:12 +0000811}
812
813/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
814/// Returns true if successful.
Chris Lattner3ed572e2011-01-15 06:57:04 +0000815static bool EvaluateHasIncludeNext(Token &Tok,
John Thompson92bd8c72009-11-02 22:28:12 +0000816 IdentifierInfo *II, Preprocessor &PP) {
817 // __has_include_next is like __has_include, except that we start
818 // searching after the current found directory. If we can't do this,
819 // issue a diagnostic.
820 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
821 if (PP.isInPrimaryFile()) {
822 Lookup = 0;
823 PP.Diag(Tok, diag::pp_include_next_in_primary);
824 } else if (Lookup == 0) {
825 PP.Diag(Tok, diag::pp_include_next_absolute_path);
826 } else {
827 // Start looking up in the next directory.
828 ++Lookup;
829 }
830
Chris Lattner3ed572e2011-01-15 06:57:04 +0000831 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
John Thompson92bd8c72009-11-02 22:28:12 +0000832}
Chris Lattner148772a2009-06-13 07:13:28 +0000833
Chris Lattnera3b605e2008-03-09 03:13:06 +0000834/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
835/// as a builtin macro, handle it and return the next token as 'Tok'.
836void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
837 // Figure out which token this is.
838 IdentifierInfo *II = Tok.getIdentifierInfo();
839 assert(II && "Can't be a macro without id info!");
Mike Stump1eb44332009-09-09 15:08:12 +0000840
John McCall1ef8a2e2010-08-28 22:34:47 +0000841 // If this is an _Pragma or Microsoft __pragma directive, expand it,
842 // invoke the pragma handler, then lex the token after it.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000843 if (II == Ident_Pragma)
844 return Handle_Pragma(Tok);
John McCall1ef8a2e2010-08-28 22:34:47 +0000845 else if (II == Ident__pragma) // in non-MS mode this is null
846 return HandleMicrosoft__pragma(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnera3b605e2008-03-09 03:13:06 +0000848 ++NumBuiltinMacroExpanded;
849
Benjamin Kramerb1765912010-01-27 16:38:22 +0000850 llvm::SmallString<128> TmpBuffer;
851 llvm::raw_svector_ostream OS(TmpBuffer);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000852
853 // Set up the return result.
854 Tok.setIdentifierInfo(0);
855 Tok.clearFlag(Token::NeedsCleaning);
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Chris Lattnera3b605e2008-03-09 03:13:06 +0000857 if (II == Ident__LINE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000858 // C99 6.10.8: "__LINE__: The presumed line number (within the current
859 // source file) of the current source line (an integer constant)". This can
860 // be affected by #line.
Chris Lattner081927b2009-02-15 21:06:39 +0000861 SourceLocation Loc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chris Lattnerdff070f2009-04-18 22:29:33 +0000863 // Advance to the location of the first _, this might not be the first byte
864 // of the token if it starts with an escaped newline.
865 Loc = AdvanceToTokenCharacter(Loc, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner081927b2009-02-15 21:06:39 +0000867 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000868 // a macro expansion. This doesn't matter for object-like macros, but
Chris Lattner081927b2009-02-15 21:06:39 +0000869 // can matter for a function-like macro that expands to contain __LINE__.
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000870 // Skip down through expansion points until we find a file loc for the
871 // end of the expansion history.
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000872 Loc = SourceMgr.getExpansionRange(Loc).second;
Chris Lattner081927b2009-02-15 21:06:39 +0000873 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattner1fa49532009-03-08 08:08:45 +0000875 // __LINE__ expands to a simple numeric value.
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000876 OS << (PLoc.isValid()? PLoc.getLine() : 1);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000877 Tok.setKind(tok::numeric_constant);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000878 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000879 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
880 // character string literal)". This can be affected by #line.
881 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
882
883 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
884 // #include stack instead of the current file.
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000885 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000886 SourceLocation NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000887 while (NextLoc.isValid()) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000888 PLoc = SourceMgr.getPresumedLoc(NextLoc);
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000889 if (PLoc.isInvalid())
890 break;
891
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000892 NextLoc = PLoc.getIncludeLoc();
Chris Lattnera3b605e2008-03-09 03:13:06 +0000893 }
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattnera3b605e2008-03-09 03:13:06 +0000896 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Benjamin Kramerb1765912010-01-27 16:38:22 +0000897 llvm::SmallString<128> FN;
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000898 if (PLoc.isValid()) {
899 FN += PLoc.getFilename();
900 Lexer::Stringify(FN);
901 OS << '"' << FN.str() << '"';
902 }
Chris Lattnera3b605e2008-03-09 03:13:06 +0000903 Tok.setKind(tok::string_literal);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000904 } else if (II == Ident__DATE__) {
905 if (!DATELoc.isValid())
906 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
907 Tok.setKind(tok::string_literal);
908 Tok.setLength(strlen("\"Mmm dd yyyy\""));
Chandler Carruthbf340e42011-07-26 03:03:05 +0000909 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
910 Tok.getLocation(),
911 Tok.getLength()));
Benjamin Kramerb1765912010-01-27 16:38:22 +0000912 return;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000913 } else if (II == Ident__TIME__) {
914 if (!TIMELoc.isValid())
915 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
916 Tok.setKind(tok::string_literal);
917 Tok.setLength(strlen("\"hh:mm:ss\""));
Chandler Carruthbf340e42011-07-26 03:03:05 +0000918 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
919 Tok.getLocation(),
920 Tok.getLength()));
Benjamin Kramerb1765912010-01-27 16:38:22 +0000921 return;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000922 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000923 // Compute the presumed include depth of this token. This can be affected
924 // by GNU line markers.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000925 unsigned Depth = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000927 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000928 if (PLoc.isValid()) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000929 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000930 for (; PLoc.isValid(); ++Depth)
931 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner1fa49532009-03-08 08:08:45 +0000934 // __INCLUDE_LEVEL__ expands to a simple numeric value.
Benjamin Kramerb1765912010-01-27 16:38:22 +0000935 OS << Depth;
Chris Lattnera3b605e2008-03-09 03:13:06 +0000936 Tok.setKind(tok::numeric_constant);
Chris Lattnera3b605e2008-03-09 03:13:06 +0000937 } else if (II == Ident__TIMESTAMP__) {
938 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
939 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
Chris Lattnera3b605e2008-03-09 03:13:06 +0000940
941 // Get the file that we are lexing out of. If we're currently lexing from
942 // a macro, dig into the include stack.
943 const FileEntry *CurFile = 0;
Ted Kremeneka275a192008-11-20 01:35:24 +0000944 PreprocessorLexer *TheLexer = getCurrentFileLexer();
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Chris Lattnera3b605e2008-03-09 03:13:06 +0000946 if (TheLexer)
Ted Kremenekac80c6e2008-11-19 22:55:25 +0000947 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattnera3b605e2008-03-09 03:13:06 +0000949 const char *Result;
950 if (CurFile) {
951 time_t TT = CurFile->getModificationTime();
952 struct tm *TM = localtime(&TT);
953 Result = asctime(TM);
954 } else {
955 Result = "??? ??? ?? ??:??:?? ????\n";
956 }
Benjamin Kramerb1765912010-01-27 16:38:22 +0000957 // Surround the string with " and strip the trailing newline.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000958 OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
Chris Lattnera3b605e2008-03-09 03:13:06 +0000959 Tok.setKind(tok::string_literal);
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000960 } else if (II == Ident__COUNTER__) {
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000961 // __COUNTER__ expands to a simple numeric value.
Benjamin Kramerb1765912010-01-27 16:38:22 +0000962 OS << CounterValue++;
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000963 Tok.setKind(tok::numeric_constant);
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000964 } else if (II == Ident__has_feature ||
965 II == Ident__has_extension ||
966 II == Ident__has_builtin ||
Anders Carlssoncae50952010-10-20 02:31:43 +0000967 II == Ident__has_attribute) {
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000968 // The argument to these builtins should be a parenthesized identifier.
Chris Lattner148772a2009-06-13 07:13:28 +0000969 SourceLocation StartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Chris Lattner148772a2009-06-13 07:13:28 +0000971 bool IsValid = false;
972 IdentifierInfo *FeatureII = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattner148772a2009-06-13 07:13:28 +0000974 // Read the '('.
975 Lex(Tok);
976 if (Tok.is(tok::l_paren)) {
977 // Read the identifier
978 Lex(Tok);
979 if (Tok.is(tok::identifier)) {
980 FeatureII = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattner148772a2009-06-13 07:13:28 +0000982 // Read the ')'.
983 Lex(Tok);
984 if (Tok.is(tok::r_paren))
985 IsValid = true;
986 }
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattner148772a2009-06-13 07:13:28 +0000989 bool Value = false;
990 if (!IsValid)
991 Diag(StartLoc, diag::err_feature_check_malformed);
992 else if (II == Ident__has_builtin) {
Mike Stump1eb44332009-09-09 15:08:12 +0000993 // Check for a builtin is trivial.
Chris Lattner148772a2009-06-13 07:13:28 +0000994 Value = FeatureII->getBuiltinID() != 0;
Anders Carlssoncae50952010-10-20 02:31:43 +0000995 } else if (II == Ident__has_attribute)
996 Value = HasAttribute(FeatureII);
Peter Collingbournec1b5fa42011-05-13 20:54:45 +0000997 else if (II == Ident__has_extension)
998 Value = HasExtension(*this, FeatureII);
Anders Carlssoncae50952010-10-20 02:31:43 +0000999 else {
Chris Lattner148772a2009-06-13 07:13:28 +00001000 assert(II == Ident__has_feature && "Must be feature check");
1001 Value = HasFeature(*this, FeatureII);
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Benjamin Kramerb1765912010-01-27 16:38:22 +00001004 OS << (int)Value;
Chris Lattner148772a2009-06-13 07:13:28 +00001005 Tok.setKind(tok::numeric_constant);
John Thompson92bd8c72009-11-02 22:28:12 +00001006 } else if (II == Ident__has_include ||
1007 II == Ident__has_include_next) {
1008 // The argument to these two builtins should be a parenthesized
1009 // file name string literal using angle brackets (<>) or
1010 // double-quotes ("").
Chris Lattner3ed572e2011-01-15 06:57:04 +00001011 bool Value;
John Thompson92bd8c72009-11-02 22:28:12 +00001012 if (II == Ident__has_include)
Chris Lattner3ed572e2011-01-15 06:57:04 +00001013 Value = EvaluateHasInclude(Tok, II, *this);
John Thompson92bd8c72009-11-02 22:28:12 +00001014 else
Chris Lattner3ed572e2011-01-15 06:57:04 +00001015 Value = EvaluateHasIncludeNext(Tok, II, *this);
Benjamin Kramerb1765912010-01-27 16:38:22 +00001016 OS << (int)Value;
John Thompson92bd8c72009-11-02 22:28:12 +00001017 Tok.setKind(tok::numeric_constant);
Chris Lattnera3b605e2008-03-09 03:13:06 +00001018 } else {
1019 assert(0 && "Unknown identifier!");
1020 }
Benjamin Kramerb1765912010-01-27 16:38:22 +00001021 CreateString(OS.str().data(), OS.str().size(), Tok, Tok.getLocation());
Chris Lattnera3b605e2008-03-09 03:13:06 +00001022}
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001023
1024void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1025 // If the 'used' status changed, and the macro requires 'unused' warning,
1026 // remove its SourceLocation from the warn-for-unused-macro locations.
1027 if (MI->isWarnIfUnused() && !MI->isUsed())
1028 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1029 MI->setIsUsed(true);
1030}