blob: 865a89e8962fc5ac2d0b8528e3cf86b5b376d3b4 [file] [log] [blame]
Joao Matos3e1ec722012-08-31 21:34:27 +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"
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +000016#include "clang/Lex/MacroArgs.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000017#include "clang/Basic/FileManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Basic/SourceManager.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000019#include "clang/Basic/TargetInfo.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000020#include "clang/Lex/CodeCompletionHandler.h"
21#include "clang/Lex/ExternalPreprocessorSource.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Lex/LexDiagnostic.h"
23#include "clang/Lex/MacroInfo.h"
24#include "llvm/ADT/STLExtras.h"
Andy Gibbs02a17682012-11-17 19:15:38 +000025#include "llvm/ADT/SmallString.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000026#include "llvm/ADT/StringSwitch.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000027#include "llvm/Config/llvm-config.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000028#include "llvm/Support/ErrorHandling.h"
Dmitri Gribenko33d054b2012-09-24 20:56:28 +000029#include "llvm/Support/Format.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/Support/raw_ostream.h"
Joao Matos3e1ec722012-08-31 21:34:27 +000031#include <cstdio>
32#include <ctime>
33using namespace clang;
34
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +000035MacroDirective *
36Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +000037 assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
Joao Matos3e1ec722012-08-31 21:34:27 +000038
39 macro_iterator Pos = Macros.find(II);
Joao Matos3e1ec722012-08-31 21:34:27 +000040 assert(Pos != Macros.end() && "Identifier macro info is missing!");
Joao Matos3e1ec722012-08-31 21:34:27 +000041 return Pos->second;
42}
43
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +000044void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +000045 assert(MD && "MacroDirective should be non-zero!");
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +000046 assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
Douglas Gregor6c6c54a2012-10-11 00:46:49 +000047
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +000048 MacroDirective *&StoredMD = Macros[II];
49 MD->setPrevious(StoredMD);
50 StoredMD = MD;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +000051 II->setHasMacroDefinition(MD->isDefined());
52 bool isImportedMacro = isa<DefMacroDirective>(MD) &&
53 cast<DefMacroDirective>(MD)->isImported();
54 if (II->isFromAST() && !isImportedMacro)
Joao Matos3e1ec722012-08-31 21:34:27 +000055 II->setChangedSinceDeserialization();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +000056}
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +000057
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +000058void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
59 MacroDirective *MD) {
60 assert(II && MD);
61 MacroDirective *&StoredMD = Macros[II];
62 assert(!StoredMD &&
63 "the macro history was modified before initializing it from a pch");
64 StoredMD = MD;
65 // Setup the identifier as having associated macro history.
66 II->setHasMacroDefinition(true);
67 if (!MD->isDefined())
68 II->setHasMacroDefinition(false);
Joao Matos3e1ec722012-08-31 21:34:27 +000069}
70
Joao Matos3e1ec722012-08-31 21:34:27 +000071/// RegisterBuiltinMacro - Register the specified identifier in the identifier
72/// table and mark it as a builtin macro to be expanded.
73static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
74 // Get the identifier.
75 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
76
77 // Mark it as being a macro that is builtin.
78 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
79 MI->setIsBuiltinMacro();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +000080 PP.appendDefMacroDirective(Id, MI);
Joao Matos3e1ec722012-08-31 21:34:27 +000081 return Id;
82}
83
84
85/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
86/// identifier table.
87void Preprocessor::RegisterBuiltinMacros() {
88 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
89 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
90 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
91 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
92 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
93 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
94
95 // GCC Extensions.
96 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
97 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
98 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
99
100 // Clang Extensions.
101 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
102 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
103 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
104 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
105 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
106 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
107 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
108
Douglas Gregorb09de512012-09-25 15:44:52 +0000109 // Modules.
110 if (LangOpts.Modules) {
111 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
112
113 // __MODULE__
114 if (!LangOpts.CurrentModule.empty())
115 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
116 else
117 Ident__MODULE__ = 0;
118 } else {
119 Ident__building_module = 0;
120 Ident__MODULE__ = 0;
121 }
122
Joao Matos3e1ec722012-08-31 21:34:27 +0000123 // Microsoft Extensions.
124 if (LangOpts.MicrosoftExt)
125 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
126 else
127 Ident__pragma = 0;
128}
129
130/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
131/// in its expansion, currently expands to that token literally.
132static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
133 const IdentifierInfo *MacroIdent,
134 Preprocessor &PP) {
135 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
136
137 // If the token isn't an identifier, it's always literally expanded.
138 if (II == 0) return true;
139
140 // If the information about this identifier is out of date, update it from
141 // the external source.
142 if (II->isOutOfDate())
143 PP.getExternalSource()->updateOutOfDateIdentifier(*II);
144
145 // If the identifier is a macro, and if that macro is enabled, it may be
146 // expanded so it's not a trivial expansion.
147 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
148 // Fast expanding "#define X X" is ok, because X would be disabled.
149 II != MacroIdent)
150 return false;
151
152 // If this is an object-like macro invocation, it is safe to trivially expand
153 // it.
154 if (MI->isObjectLike()) return true;
155
156 // If this is a function-like macro invocation, it's safe to trivially expand
157 // as long as the identifier is not a macro argument.
158 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
159 I != E; ++I)
160 if (*I == II)
161 return false; // Identifier is a macro argument.
162
163 return true;
164}
165
166
167/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
168/// lexed is a '('. If so, consume the token and return true, if not, this
169/// method should have no observable side-effect on the lexed tokens.
170bool Preprocessor::isNextPPTokenLParen() {
171 // Do some quick tests for rejection cases.
172 unsigned Val;
173 if (CurLexer)
174 Val = CurLexer->isNextPPTokenLParen();
175 else if (CurPTHLexer)
176 Val = CurPTHLexer->isNextPPTokenLParen();
177 else
178 Val = CurTokenLexer->isNextTokenLParen();
179
180 if (Val == 2) {
181 // We have run off the end. If it's a source file we don't
182 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
183 // macro stack.
184 if (CurPPLexer)
185 return false;
186 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
187 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
188 if (Entry.TheLexer)
189 Val = Entry.TheLexer->isNextPPTokenLParen();
190 else if (Entry.ThePTHLexer)
191 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
192 else
193 Val = Entry.TheTokenLexer->isNextTokenLParen();
194
195 if (Val != 2)
196 break;
197
198 // Ran off the end of a source file?
199 if (Entry.ThePPLexer)
200 return false;
201 }
202 }
203
204 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
205 // have found something that isn't a '(' or we found the end of the
206 // translation unit. In either case, return false.
207 return Val == 1;
208}
209
210/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
211/// expanded as a macro, handle it and return the next token as 'Identifier'.
212bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000213 MacroDirective *MD) {
Argyrios Kyrtzidis35803282013-03-27 01:25:19 +0000214 MacroDirective::DefInfo Def = MD->getDefinition();
215 assert(Def.isValid());
216 MacroInfo *MI = Def.getMacroInfo();
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000217
Joao Matos3e1ec722012-08-31 21:34:27 +0000218 // If this is a macro expansion in the "#if !defined(x)" line for the file,
219 // then the macro could expand to different things in other contexts, we need
220 // to disable the optimization in this case.
221 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
222
223 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
224 if (MI->isBuiltinMacro()) {
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000225 if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +0000226 Identifier.getLocation(),/*Args=*/0);
Joao Matos3e1ec722012-08-31 21:34:27 +0000227 ExpandBuiltinMacro(Identifier);
Eli Friedmand2f93082013-09-19 00:41:32 +0000228 return true;
Joao Matos3e1ec722012-08-31 21:34:27 +0000229 }
230
231 /// Args - If this is a function-like macro expansion, this contains,
232 /// for each macro argument, the list of tokens that were provided to the
233 /// invocation.
234 MacroArgs *Args = 0;
235
236 // Remember where the end of the expansion occurred. For an object-like
237 // macro, this is the identifier. For a function-like macro, this is the ')'.
238 SourceLocation ExpansionEnd = Identifier.getLocation();
239
240 // If this is a function-like macro, read the arguments.
241 if (MI->isFunctionLike()) {
Joao Matos3e1ec722012-08-31 21:34:27 +0000242 // Remember that we are now parsing the arguments to a macro invocation.
243 // Preprocessor directives used inside macro arguments are not portable, and
244 // this enables the warning.
245 InMacroArgs = true;
246 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
247
248 // Finished parsing args.
249 InMacroArgs = false;
250
251 // If there was an error parsing the arguments, bail out.
Eli Friedmand2f93082013-09-19 00:41:32 +0000252 if (Args == 0) return true;
Joao Matos3e1ec722012-08-31 21:34:27 +0000253
254 ++NumFnMacroExpanded;
255 } else {
256 ++NumMacroExpanded;
257 }
258
259 // Notice that this macro has been used.
260 markMacroAsUsed(MI);
261
262 // Remember where the token is expanded.
263 SourceLocation ExpandLoc = Identifier.getLocation();
264 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
265
266 if (Callbacks) {
267 if (InMacroArgs) {
268 // We can have macro expansion inside a conditional directive while
269 // reading the function macro arguments. To ensure, in that case, that
270 // MacroExpands callbacks still happen in source order, queue this
271 // callback to have it happen after the function macro callback.
272 DelayedMacroExpandsCallbacks.push_back(
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000273 MacroExpandsInfo(Identifier, MD, ExpansionRange));
Joao Matos3e1ec722012-08-31 21:34:27 +0000274 } else {
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +0000275 Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args);
Joao Matos3e1ec722012-08-31 21:34:27 +0000276 if (!DelayedMacroExpandsCallbacks.empty()) {
277 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
278 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
Argyrios Kyrtzidisdd08a0c2013-05-03 22:31:32 +0000279 // FIXME: We lose macro args info with delayed callback.
280 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, /*Args=*/0);
Joao Matos3e1ec722012-08-31 21:34:27 +0000281 }
282 DelayedMacroExpandsCallbacks.clear();
283 }
284 }
285 }
Douglas Gregore8219a62012-10-11 21:07:39 +0000286
287 // If the macro definition is ambiguous, complain.
Argyrios Kyrtzidis35803282013-03-27 01:25:19 +0000288 if (Def.getDirective()->isAmbiguous()) {
Douglas Gregore8219a62012-10-11 21:07:39 +0000289 Diag(Identifier, diag::warn_pp_ambiguous_macro)
290 << Identifier.getIdentifierInfo();
291 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
292 << Identifier.getIdentifierInfo();
Argyrios Kyrtzidis35803282013-03-27 01:25:19 +0000293 for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition();
294 PrevDef && !PrevDef.isUndefined();
295 PrevDef = PrevDef.getPreviousDefinition()) {
296 if (PrevDef.getDirective()->isAmbiguous()) {
297 Diag(PrevDef.getMacroInfo()->getDefinitionLoc(),
298 diag::note_pp_ambiguous_macro_other)
Douglas Gregore8219a62012-10-11 21:07:39 +0000299 << Identifier.getIdentifierInfo();
300 }
301 }
302 }
303
Joao Matos3e1ec722012-08-31 21:34:27 +0000304 // If we started lexing a macro, enter the macro expansion body.
305
306 // If this macro expands to no tokens, don't bother to push it onto the
307 // expansion stack, only to take it right back off.
308 if (MI->getNumTokens() == 0) {
309 // No need for arg info.
310 if (Args) Args->destroy(*this);
311
Eli Friedmand2f93082013-09-19 00:41:32 +0000312 // Propagate whitespace info as if we had pushed, then popped,
313 // a macro context.
Joao Matos3e1ec722012-08-31 21:34:27 +0000314 Identifier.setFlag(Token::LeadingEmptyMacro);
Eli Friedmand2f93082013-09-19 00:41:32 +0000315 PropagateLineStartLeadingSpaceInfo(Identifier);
Joao Matos3e1ec722012-08-31 21:34:27 +0000316 ++NumFastMacroExpanded;
317 return false;
Joao Matos3e1ec722012-08-31 21:34:27 +0000318 } else if (MI->getNumTokens() == 1 &&
319 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
320 *this)) {
321 // Otherwise, if this macro expands into a single trivially-expanded
322 // token: expand it now. This handles common cases like
323 // "#define VAL 42".
324
325 // No need for arg info.
326 if (Args) Args->destroy(*this);
327
328 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
329 // identifier to the expanded token.
330 bool isAtStartOfLine = Identifier.isAtStartOfLine();
331 bool hasLeadingSpace = Identifier.hasLeadingSpace();
332
333 // Replace the result token.
334 Identifier = MI->getReplacementToken(0);
335
336 // Restore the StartOfLine/LeadingSpace markers.
337 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
338 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
339
340 // Update the tokens location to include both its expansion and physical
341 // locations.
342 SourceLocation Loc =
343 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
344 ExpansionEnd,Identifier.getLength());
345 Identifier.setLocation(Loc);
346
347 // If this is a disabled macro or #define X X, we must mark the result as
348 // unexpandable.
349 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
350 if (MacroInfo *NewMI = getMacroInfo(NewII))
351 if (!NewMI->isEnabled() || NewMI == MI) {
352 Identifier.setFlag(Token::DisableExpand);
Douglas Gregor40f56e52013-01-30 23:10:17 +0000353 // Don't warn for "#define X X" like "#define bool bool" from
354 // stdbool.h.
355 if (NewMI != MI || MI->isFunctionLike())
356 Diag(Identifier, diag::pp_disabled_macro_expansion);
Joao Matos3e1ec722012-08-31 21:34:27 +0000357 }
358 }
359
360 // Since this is not an identifier token, it can't be macro expanded, so
361 // we're done.
362 ++NumFastMacroExpanded;
Eli Friedmand2f93082013-09-19 00:41:32 +0000363 return true;
Joao Matos3e1ec722012-08-31 21:34:27 +0000364 }
365
366 // Start expanding the macro.
367 EnterMacro(Identifier, ExpansionEnd, MI, Args);
Joao Matos3e1ec722012-08-31 21:34:27 +0000368 return false;
369}
370
Richard Trieu5940bf32013-07-23 18:01:49 +0000371enum Bracket {
372 Brace,
373 Paren
374};
375
376/// CheckMatchedBrackets - Returns true if the braces and parentheses in the
377/// token vector are properly nested.
378static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
379 SmallVector<Bracket, 8> Brackets;
380 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
381 E = Tokens.end();
382 I != E; ++I) {
383 if (I->is(tok::l_paren)) {
384 Brackets.push_back(Paren);
385 } else if (I->is(tok::r_paren)) {
386 if (Brackets.empty() || Brackets.back() == Brace)
387 return false;
388 Brackets.pop_back();
389 } else if (I->is(tok::l_brace)) {
390 Brackets.push_back(Brace);
391 } else if (I->is(tok::r_brace)) {
392 if (Brackets.empty() || Brackets.back() == Paren)
393 return false;
394 Brackets.pop_back();
395 }
396 }
397 if (!Brackets.empty())
398 return false;
399 return true;
400}
401
402/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
403/// vector of tokens in NewTokens. The new number of arguments will be placed
404/// in NumArgs and the ranges which need to surrounded in parentheses will be
405/// in ParenHints.
406/// Returns false if the token stream cannot be changed. If this is because
407/// of an initializer list starting a macro argument, the range of those
408/// initializer lists will be place in InitLists.
409static bool GenerateNewArgTokens(Preprocessor &PP,
410 SmallVectorImpl<Token> &OldTokens,
411 SmallVectorImpl<Token> &NewTokens,
412 unsigned &NumArgs,
413 SmallVectorImpl<SourceRange> &ParenHints,
414 SmallVectorImpl<SourceRange> &InitLists) {
415 if (!CheckMatchedBrackets(OldTokens))
416 return false;
417
418 // Once it is known that the brackets are matched, only a simple count of the
419 // braces is needed.
420 unsigned Braces = 0;
421
422 // First token of a new macro argument.
423 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
424
425 // First closing brace in a new macro argument. Used to generate
426 // SourceRanges for InitLists.
427 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
428 NumArgs = 0;
429 Token TempToken;
430 // Set to true when a macro separator token is found inside a braced list.
431 // If true, the fixed argument spans multiple old arguments and ParenHints
432 // will be updated.
433 bool FoundSeparatorToken = false;
434 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
435 E = OldTokens.end();
436 I != E; ++I) {
437 if (I->is(tok::l_brace)) {
438 ++Braces;
439 } else if (I->is(tok::r_brace)) {
440 --Braces;
441 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
442 ClosingBrace = I;
443 } else if (I->is(tok::eof)) {
444 // EOF token is used to separate macro arguments
445 if (Braces != 0) {
446 // Assume comma separator is actually braced list separator and change
447 // it back to a comma.
448 FoundSeparatorToken = true;
449 I->setKind(tok::comma);
450 I->setLength(1);
451 } else { // Braces == 0
452 // Separator token still separates arguments.
453 ++NumArgs;
454
455 // If the argument starts with a brace, it can't be fixed with
456 // parentheses. A different diagnostic will be given.
457 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
458 InitLists.push_back(
459 SourceRange(ArgStartIterator->getLocation(),
460 PP.getLocForEndOfToken(ClosingBrace->getLocation())));
461 ClosingBrace = E;
462 }
463
464 // Add left paren
465 if (FoundSeparatorToken) {
466 TempToken.startToken();
467 TempToken.setKind(tok::l_paren);
468 TempToken.setLocation(ArgStartIterator->getLocation());
469 TempToken.setLength(0);
470 NewTokens.push_back(TempToken);
471 }
472
473 // Copy over argument tokens
474 NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
475
476 // Add right paren and store the paren locations in ParenHints
477 if (FoundSeparatorToken) {
478 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
479 TempToken.startToken();
480 TempToken.setKind(tok::r_paren);
481 TempToken.setLocation(Loc);
482 TempToken.setLength(0);
483 NewTokens.push_back(TempToken);
484 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
485 Loc));
486 }
487
488 // Copy separator token
489 NewTokens.push_back(*I);
490
491 // Reset values
492 ArgStartIterator = I + 1;
493 FoundSeparatorToken = false;
494 }
495 }
496 }
497
498 return !ParenHints.empty() && InitLists.empty();
499}
500
Joao Matos3e1ec722012-08-31 21:34:27 +0000501/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
502/// token is the '(' of the macro, this method is invoked to read all of the
503/// actual arguments specified for the macro invocation. This returns null on
504/// error.
505MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
506 MacroInfo *MI,
507 SourceLocation &MacroEnd) {
508 // The number of fixed arguments to parse.
509 unsigned NumFixedArgsLeft = MI->getNumArgs();
510 bool isVariadic = MI->isVariadic();
511
512 // Outer loop, while there are more arguments, keep reading them.
513 Token Tok;
514
515 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
516 // an argument value in a macro could expand to ',' or '(' or ')'.
517 LexUnexpandedToken(Tok);
518 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
519
520 // ArgTokens - Build up a list of tokens that make up each argument. Each
521 // argument is separated by an EOF token. Use a SmallVector so we can avoid
522 // heap allocations in the common case.
523 SmallVector<Token, 64> ArgTokens;
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000524 bool ContainsCodeCompletionTok = false;
Joao Matos3e1ec722012-08-31 21:34:27 +0000525
Richard Trieu5940bf32013-07-23 18:01:49 +0000526 SourceLocation TooManyArgsLoc;
527
Joao Matos3e1ec722012-08-31 21:34:27 +0000528 unsigned NumActuals = 0;
529 while (Tok.isNot(tok::r_paren)) {
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000530 if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod)))
531 break;
532
Joao Matos3e1ec722012-08-31 21:34:27 +0000533 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
534 "only expect argument separators here");
535
536 unsigned ArgTokenStart = ArgTokens.size();
537 SourceLocation ArgStartLoc = Tok.getLocation();
538
539 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
540 // that we already consumed the first one.
541 unsigned NumParens = 0;
542
543 while (1) {
544 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
545 // an argument value in a macro could expand to ',' or '(' or ')'.
546 LexUnexpandedToken(Tok);
547
548 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000549 if (!ContainsCodeCompletionTok) {
550 Diag(MacroName, diag::err_unterm_macro_invoc);
551 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
552 << MacroName.getIdentifierInfo();
553 // Do not lose the EOF/EOD. Return it to the client.
554 MacroName = Tok;
555 return 0;
556 } else {
Argyrios Kyrtzidisbb06b502012-12-22 04:48:10 +0000557 // Do not lose the EOF/EOD.
558 Token *Toks = new Token[1];
559 Toks[0] = Tok;
560 EnterTokenStream(Toks, 1, true, true);
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000561 break;
562 }
Joao Matos3e1ec722012-08-31 21:34:27 +0000563 } else if (Tok.is(tok::r_paren)) {
564 // If we found the ) token, the macro arg list is done.
565 if (NumParens-- == 0) {
566 MacroEnd = Tok.getLocation();
567 break;
568 }
569 } else if (Tok.is(tok::l_paren)) {
570 ++NumParens;
Reid Kleckner11be0642013-06-26 17:16:08 +0000571 } else if (Tok.is(tok::comma) && NumParens == 0 &&
572 !(Tok.getFlags() & Token::IgnoredComma)) {
573 // In Microsoft-compatibility mode, single commas from nested macro
574 // expansions should not be considered as argument separators. We test
575 // for this with the IgnoredComma token flag above.
576
Joao Matos3e1ec722012-08-31 21:34:27 +0000577 // Comma ends this argument if there are more fixed arguments expected.
578 // However, if this is a variadic macro, and this is part of the
579 // variadic part, then the comma is just an argument token.
580 if (!isVariadic) break;
581 if (NumFixedArgsLeft > 1)
582 break;
583 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
584 // If this is a comment token in the argument list and we're just in
585 // -C mode (not -CC mode), discard the comment.
586 continue;
587 } else if (Tok.getIdentifierInfo() != 0) {
588 // Reading macro arguments can cause macros that we are currently
589 // expanding from to be popped off the expansion stack. Doing so causes
590 // them to be reenabled for expansion. Here we record whether any
591 // identifiers we lex as macro arguments correspond to disabled macros.
592 // If so, we mark the token as noexpand. This is a subtle aspect of
593 // C99 6.10.3.4p2.
594 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
595 if (!MI->isEnabled())
596 Tok.setFlag(Token::DisableExpand);
597 } else if (Tok.is(tok::code_completion)) {
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000598 ContainsCodeCompletionTok = true;
Joao Matos3e1ec722012-08-31 21:34:27 +0000599 if (CodeComplete)
600 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
601 MI, NumActuals);
602 // Don't mark that we reached the code-completion point because the
603 // parser is going to handle the token and there will be another
604 // code-completion callback.
605 }
606
607 ArgTokens.push_back(Tok);
608 }
609
610 // If this was an empty argument list foo(), don't add this as an empty
611 // argument.
612 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
613 break;
614
615 // If this is not a variadic macro, and too many args were specified, emit
616 // an error.
Richard Trieu5940bf32013-07-23 18:01:49 +0000617 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
Joao Matos3e1ec722012-08-31 21:34:27 +0000618 if (ArgTokens.size() != ArgTokenStart)
Richard Trieu5940bf32013-07-23 18:01:49 +0000619 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
620 else
621 TooManyArgsLoc = ArgStartLoc;
Joao Matos3e1ec722012-08-31 21:34:27 +0000622 }
623
Richard Trieu5940bf32013-07-23 18:01:49 +0000624 // Empty arguments are standard in C99 and C++0x, and are supported as an
625 // extension in other modes.
Joao Matos3e1ec722012-08-31 21:34:27 +0000626 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
Richard Smith80ad52f2013-01-02 11:42:31 +0000627 Diag(Tok, LangOpts.CPlusPlus11 ?
Joao Matos3e1ec722012-08-31 21:34:27 +0000628 diag::warn_cxx98_compat_empty_fnmacro_arg :
629 diag::ext_empty_fnmacro_arg);
630
631 // Add a marker EOF token to the end of the token list for this argument.
632 Token EOFTok;
633 EOFTok.startToken();
634 EOFTok.setKind(tok::eof);
635 EOFTok.setLocation(Tok.getLocation());
636 EOFTok.setLength(0);
637 ArgTokens.push_back(EOFTok);
638 ++NumActuals;
Richard Trieu5940bf32013-07-23 18:01:49 +0000639 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
Argyrios Kyrtzidisfdf57062013-02-22 22:28:58 +0000640 --NumFixedArgsLeft;
Joao Matos3e1ec722012-08-31 21:34:27 +0000641 }
642
643 // Okay, we either found the r_paren. Check to see if we parsed too few
644 // arguments.
645 unsigned MinArgsExpected = MI->getNumArgs();
646
Richard Trieu5940bf32013-07-23 18:01:49 +0000647 // If this is not a variadic macro, and too many args were specified, emit
648 // an error.
649 if (!isVariadic && NumActuals > MinArgsExpected &&
650 !ContainsCodeCompletionTok) {
651 // Emit the diagnostic at the macro name in case there is a missing ).
652 // Emitting it at the , could be far away from the macro name.
653 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
654 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
655 << MacroName.getIdentifierInfo();
656
657 // Commas from braced initializer lists will be treated as argument
658 // separators inside macros. Attempt to correct for this with parentheses.
659 // TODO: See if this can be generalized to angle brackets for templates
660 // inside macro arguments.
661
Bob Wilson40ec4d22013-07-27 21:59:57 +0000662 SmallVector<Token, 4> FixedArgTokens;
Richard Trieu5940bf32013-07-23 18:01:49 +0000663 unsigned FixedNumArgs = 0;
664 SmallVector<SourceRange, 4> ParenHints, InitLists;
665 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
666 ParenHints, InitLists)) {
667 if (!InitLists.empty()) {
668 DiagnosticBuilder DB =
669 Diag(MacroName,
670 diag::note_init_list_at_beginning_of_macro_argument);
671 for (SmallVector<SourceRange, 4>::iterator
672 Range = InitLists.begin(), RangeEnd = InitLists.end();
673 Range != RangeEnd; ++Range) {
674 if (DB.hasMaxRanges())
675 break;
676 DB << *Range;
677 }
678 }
679 return 0;
680 }
681 if (FixedNumArgs != MinArgsExpected)
682 return 0;
683
684 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
685 for (SmallVector<SourceRange, 4>::iterator
686 ParenLocation = ParenHints.begin(), ParenEnd = ParenHints.end();
687 ParenLocation != ParenEnd; ++ParenLocation) {
688 if (DB.hasMaxFixItHints())
689 break;
690 DB << FixItHint::CreateInsertion(ParenLocation->getBegin(), "(");
691 if (DB.hasMaxFixItHints())
692 break;
693 DB << FixItHint::CreateInsertion(ParenLocation->getEnd(), ")");
694 }
695 ArgTokens.swap(FixedArgTokens);
696 NumActuals = FixedNumArgs;
697 }
698
Joao Matos3e1ec722012-08-31 21:34:27 +0000699 // See MacroArgs instance var for description of this.
700 bool isVarargsElided = false;
701
Argyrios Kyrtzidisf1e5b152012-12-21 01:51:12 +0000702 if (ContainsCodeCompletionTok) {
703 // Recover from not-fully-formed macro invocation during code-completion.
704 Token EOFTok;
705 EOFTok.startToken();
706 EOFTok.setKind(tok::eof);
707 EOFTok.setLocation(Tok.getLocation());
708 EOFTok.setLength(0);
709 for (; NumActuals < MinArgsExpected; ++NumActuals)
710 ArgTokens.push_back(EOFTok);
711 }
712
Joao Matos3e1ec722012-08-31 21:34:27 +0000713 if (NumActuals < MinArgsExpected) {
714 // There are several cases where too few arguments is ok, handle them now.
715 if (NumActuals == 0 && MinArgsExpected == 1) {
716 // #define A(X) or #define A(...) ---> A()
717
718 // If there is exactly one argument, and that argument is missing,
719 // then we have an empty "()" argument empty list. This is fine, even if
720 // the macro expects one argument (the argument is just empty).
721 isVarargsElided = MI->isVariadic();
722 } else if (MI->isVariadic() &&
723 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
724 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
725 // Varargs where the named vararg parameter is missing: OK as extension.
726 // #define A(x, ...)
727 // A("blah")
Eli Friedman4fa4b482012-11-14 02:18:46 +0000728 //
729 // If the macro contains the comma pasting extension, the diagnostic
730 // is suppressed; we know we'll get another diagnostic later.
731 if (!MI->hasCommaPasting()) {
732 Diag(Tok, diag::ext_missing_varargs_arg);
733 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
734 << MacroName.getIdentifierInfo();
735 }
Joao Matos3e1ec722012-08-31 21:34:27 +0000736
737 // Remember this occurred, allowing us to elide the comma when used for
738 // cases like:
739 // #define A(x, foo...) blah(a, ## foo)
740 // #define B(x, ...) blah(a, ## __VA_ARGS__)
741 // #define C(...) blah(a, ## __VA_ARGS__)
742 // A(x) B(x) C()
743 isVarargsElided = true;
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000744 } else if (!ContainsCodeCompletionTok) {
Joao Matos3e1ec722012-08-31 21:34:27 +0000745 // Otherwise, emit the error.
746 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
Argyrios Kyrtzidis0ee8de72012-12-14 18:53:47 +0000747 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
748 << MacroName.getIdentifierInfo();
Joao Matos3e1ec722012-08-31 21:34:27 +0000749 return 0;
750 }
751
752 // Add a marker EOF token to the end of the token list for this argument.
753 SourceLocation EndLoc = Tok.getLocation();
754 Tok.startToken();
755 Tok.setKind(tok::eof);
756 Tok.setLocation(EndLoc);
757 Tok.setLength(0);
758 ArgTokens.push_back(Tok);
759
760 // If we expect two arguments, add both as empty.
761 if (NumActuals == 0 && MinArgsExpected == 2)
762 ArgTokens.push_back(Tok);
763
Argyrios Kyrtzidiscd0fd182012-12-21 01:17:20 +0000764 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
765 !ContainsCodeCompletionTok) {
Joao Matos3e1ec722012-08-31 21:34:27 +0000766 // Emit the diagnostic at the macro name in case there is a missing ).
767 // Emitting it at the , could be far away from the macro name.
768 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
Argyrios Kyrtzidis0ee8de72012-12-14 18:53:47 +0000769 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
770 << MacroName.getIdentifierInfo();
Joao Matos3e1ec722012-08-31 21:34:27 +0000771 return 0;
772 }
773
774 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
775}
776
777/// \brief Keeps macro expanded tokens for TokenLexers.
778//
779/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
780/// going to lex in the cache and when it finishes the tokens are removed
781/// from the end of the cache.
782Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
783 ArrayRef<Token> tokens) {
784 assert(tokLexer);
785 if (tokens.empty())
786 return 0;
787
788 size_t newIndex = MacroExpandedTokens.size();
789 bool cacheNeedsToGrow = tokens.size() >
790 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
791 MacroExpandedTokens.append(tokens.begin(), tokens.end());
792
793 if (cacheNeedsToGrow) {
794 // Go through all the TokenLexers whose 'Tokens' pointer points in the
795 // buffer and update the pointers to the (potential) new buffer array.
796 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
797 TokenLexer *prevLexer;
798 size_t tokIndex;
799 llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
800 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
801 }
802 }
803
804 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
805 return MacroExpandedTokens.data() + newIndex;
806}
807
808void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
809 assert(!MacroExpandingLexersStack.empty());
810 size_t tokIndex = MacroExpandingLexersStack.back().second;
811 assert(tokIndex < MacroExpandedTokens.size());
812 // Pop the cached macro expanded tokens from the end.
813 MacroExpandedTokens.resize(tokIndex);
814 MacroExpandingLexersStack.pop_back();
815}
816
817/// ComputeDATE_TIME - Compute the current time, enter it into the specified
818/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
819/// the identifier tokens inserted.
820static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
821 Preprocessor &PP) {
822 time_t TT = time(0);
823 struct tm *TM = localtime(&TT);
824
825 static const char * const Months[] = {
826 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
827 };
828
Dmitri Gribenko33d054b2012-09-24 20:56:28 +0000829 {
830 SmallString<32> TmpBuffer;
831 llvm::raw_svector_ostream TmpStream(TmpBuffer);
832 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
833 TM->tm_mday, TM->tm_year + 1900);
Dmitri Gribenko33d054b2012-09-24 20:56:28 +0000834 Token TmpTok;
835 TmpTok.startToken();
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000836 PP.CreateString(TmpStream.str(), TmpTok);
Dmitri Gribenko33d054b2012-09-24 20:56:28 +0000837 DATELoc = TmpTok.getLocation();
838 }
Joao Matos3e1ec722012-08-31 21:34:27 +0000839
Dmitri Gribenko33d054b2012-09-24 20:56:28 +0000840 {
841 SmallString<32> TmpBuffer;
842 llvm::raw_svector_ostream TmpStream(TmpBuffer);
843 TmpStream << llvm::format("\"%02d:%02d:%02d\"",
844 TM->tm_hour, TM->tm_min, TM->tm_sec);
Dmitri Gribenko33d054b2012-09-24 20:56:28 +0000845 Token TmpTok;
846 TmpTok.startToken();
Dmitri Gribenko374b3832012-09-24 21:07:17 +0000847 PP.CreateString(TmpStream.str(), TmpTok);
Dmitri Gribenko33d054b2012-09-24 20:56:28 +0000848 TIMELoc = TmpTok.getLocation();
849 }
Joao Matos3e1ec722012-08-31 21:34:27 +0000850}
851
852
853/// HasFeature - Return true if we recognize and implement the feature
854/// specified by the identifier as a standard language feature.
855static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
856 const LangOptions &LangOpts = PP.getLangOpts();
857 StringRef Feature = II->getName();
858
859 // Normalize the feature name, __foo__ becomes foo.
860 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
861 Feature = Feature.substr(2, Feature.size() - 4);
862
863 return llvm::StringSwitch<bool>(Feature)
Will Dietz4f45bc02013-01-18 11:30:38 +0000864 .Case("address_sanitizer", LangOpts.Sanitize.Address)
Joao Matos3e1ec722012-08-31 21:34:27 +0000865 .Case("attribute_analyzer_noreturn", true)
866 .Case("attribute_availability", true)
867 .Case("attribute_availability_with_message", true)
868 .Case("attribute_cf_returns_not_retained", true)
869 .Case("attribute_cf_returns_retained", true)
870 .Case("attribute_deprecated_with_message", true)
871 .Case("attribute_ext_vector_type", true)
872 .Case("attribute_ns_returns_not_retained", true)
873 .Case("attribute_ns_returns_retained", true)
874 .Case("attribute_ns_consumes_self", true)
875 .Case("attribute_ns_consumed", true)
876 .Case("attribute_cf_consumed", true)
877 .Case("attribute_objc_ivar_unused", true)
878 .Case("attribute_objc_method_family", true)
879 .Case("attribute_overloadable", true)
880 .Case("attribute_unavailable_with_message", true)
881 .Case("attribute_unused_on_fields", true)
882 .Case("blocks", LangOpts.Blocks)
David Blaikie88c4b5e2013-07-29 18:24:03 +0000883 .Case("c_thread_safety_attributes", true)
Joao Matos3e1ec722012-08-31 21:34:27 +0000884 .Case("cxx_exceptions", LangOpts.Exceptions)
885 .Case("cxx_rtti", LangOpts.RTTI)
886 .Case("enumerator_attributes", true)
Will Dietz4f45bc02013-01-18 11:30:38 +0000887 .Case("memory_sanitizer", LangOpts.Sanitize.Memory)
888 .Case("thread_sanitizer", LangOpts.Sanitize.Thread)
Peter Collingbourne2eeed712013-08-07 22:47:34 +0000889 .Case("dataflow_sanitizer", LangOpts.Sanitize.DataFlow)
Joao Matos3e1ec722012-08-31 21:34:27 +0000890 // Objective-C features
891 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
892 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
893 .Case("objc_arc_weak", LangOpts.ObjCARCWeak)
894 .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
895 .Case("objc_fixed_enum", LangOpts.ObjC2)
896 .Case("objc_instancetype", LangOpts.ObjC2)
897 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
898 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
Ted Kremeneke4057c22013-01-04 19:04:44 +0000899 .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword?
Joao Matos3e1ec722012-08-31 21:34:27 +0000900 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
901 .Case("ownership_holds", true)
902 .Case("ownership_returns", true)
903 .Case("ownership_takes", true)
904 .Case("objc_bool", true)
905 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
906 .Case("objc_array_literals", LangOpts.ObjC2)
907 .Case("objc_dictionary_literals", LangOpts.ObjC2)
908 .Case("objc_boxed_expressions", LangOpts.ObjC2)
909 .Case("arc_cf_code_audited", true)
910 // C11 features
911 .Case("c_alignas", LangOpts.C11)
912 .Case("c_atomic", LangOpts.C11)
913 .Case("c_generic_selections", LangOpts.C11)
914 .Case("c_static_assert", LangOpts.C11)
Douglas Gregore87c5bd2013-05-02 05:28:32 +0000915 .Case("c_thread_local",
916 LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
Joao Matos3e1ec722012-08-31 21:34:27 +0000917 // C++11 features
Richard Smith80ad52f2013-01-02 11:42:31 +0000918 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
919 .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
920 .Case("cxx_alignas", LangOpts.CPlusPlus11)
921 .Case("cxx_atomic", LangOpts.CPlusPlus11)
922 .Case("cxx_attributes", LangOpts.CPlusPlus11)
923 .Case("cxx_auto_type", LangOpts.CPlusPlus11)
924 .Case("cxx_constexpr", LangOpts.CPlusPlus11)
925 .Case("cxx_decltype", LangOpts.CPlusPlus11)
926 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
927 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
928 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
929 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
930 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
931 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
932 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
933 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
Richard Smithe6e68b52013-04-19 17:00:31 +0000934 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
Richard Smith80ad52f2013-01-02 11:42:31 +0000935 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
936 .Case("cxx_lambdas", LangOpts.CPlusPlus11)
937 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
938 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
939 .Case("cxx_noexcept", LangOpts.CPlusPlus11)
940 .Case("cxx_nullptr", LangOpts.CPlusPlus11)
941 .Case("cxx_override_control", LangOpts.CPlusPlus11)
942 .Case("cxx_range_for", LangOpts.CPlusPlus11)
943 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
944 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
945 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
946 .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
947 .Case("cxx_static_assert", LangOpts.CPlusPlus11)
Douglas Gregore87c5bd2013-05-02 05:28:32 +0000948 .Case("cxx_thread_local",
949 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
Richard Smith80ad52f2013-01-02 11:42:31 +0000950 .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
951 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
952 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
953 .Case("cxx_user_literals", LangOpts.CPlusPlus11)
954 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000955 // C++1y features
Richard Smith3c3a5222013-07-24 17:51:13 +0000956 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000957 .Case("cxx_binary_literals", LangOpts.CPlusPlus1y)
Richard Smitha4fb3392013-07-24 17:41:31 +0000958 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000959 //.Case("cxx_generic_lambda", LangOpts.CPlusPlus1y)
Richard Smith3c3a5222013-07-24 17:51:13 +0000960 //.Case("cxx_init_capture", LangOpts.CPlusPlus1y)
Richard Smitha4fb3392013-07-24 17:41:31 +0000961 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
Richard Smithf45c2992013-05-12 03:09:35 +0000962 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000963 //.Case("cxx_runtime_array", LangOpts.CPlusPlus1y)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000964 //.Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
Joao Matos3e1ec722012-08-31 21:34:27 +0000965 // Type traits
966 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
967 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
968 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
969 .Case("has_trivial_assign", LangOpts.CPlusPlus)
970 .Case("has_trivial_copy", LangOpts.CPlusPlus)
971 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
972 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
973 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
974 .Case("is_abstract", LangOpts.CPlusPlus)
975 .Case("is_base_of", LangOpts.CPlusPlus)
976 .Case("is_class", LangOpts.CPlusPlus)
977 .Case("is_convertible_to", LangOpts.CPlusPlus)
Joao Matos3e1ec722012-08-31 21:34:27 +0000978 .Case("is_empty", LangOpts.CPlusPlus)
979 .Case("is_enum", LangOpts.CPlusPlus)
980 .Case("is_final", LangOpts.CPlusPlus)
981 .Case("is_literal", LangOpts.CPlusPlus)
982 .Case("is_standard_layout", LangOpts.CPlusPlus)
983 .Case("is_pod", LangOpts.CPlusPlus)
984 .Case("is_polymorphic", LangOpts.CPlusPlus)
985 .Case("is_trivial", LangOpts.CPlusPlus)
986 .Case("is_trivially_assignable", LangOpts.CPlusPlus)
987 .Case("is_trivially_constructible", LangOpts.CPlusPlus)
988 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
989 .Case("is_union", LangOpts.CPlusPlus)
990 .Case("modules", LangOpts.Modules)
991 .Case("tls", PP.getTargetInfo().isTLSSupported())
992 .Case("underlying_type", LangOpts.CPlusPlus)
993 .Default(false);
994}
995
996/// HasExtension - Return true if we recognize and implement the feature
997/// specified by the identifier, either as an extension or a standard language
998/// feature.
999static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
1000 if (HasFeature(PP, II))
1001 return true;
1002
1003 // If the use of an extension results in an error diagnostic, extensions are
1004 // effectively unavailable, so just return false here.
1005 if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
1006 DiagnosticsEngine::Ext_Error)
1007 return false;
1008
1009 const LangOptions &LangOpts = PP.getLangOpts();
1010 StringRef Extension = II->getName();
1011
1012 // Normalize the extension name, __foo__ becomes foo.
1013 if (Extension.startswith("__") && Extension.endswith("__") &&
1014 Extension.size() >= 4)
1015 Extension = Extension.substr(2, Extension.size() - 4);
1016
1017 // Because we inherit the feature list from HasFeature, this string switch
1018 // must be less restrictive than HasFeature's.
1019 return llvm::StringSwitch<bool>(Extension)
1020 // C11 features supported by other languages as extensions.
1021 .Case("c_alignas", true)
1022 .Case("c_atomic", true)
1023 .Case("c_generic_selections", true)
1024 .Case("c_static_assert", true)
Ed Schoutenfa7d53f2013-09-14 16:17:20 +00001025 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
Richard Smith7f0ffb32013-05-07 19:32:56 +00001026 // C++11 features supported by other languages as extensions.
Joao Matos3e1ec722012-08-31 21:34:27 +00001027 .Case("cxx_atomic", LangOpts.CPlusPlus)
1028 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
1029 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
1030 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
1031 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
1032 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
1033 .Case("cxx_override_control", LangOpts.CPlusPlus)
1034 .Case("cxx_range_for", LangOpts.CPlusPlus)
1035 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
1036 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
Richard Smith7f0ffb32013-05-07 19:32:56 +00001037 // C++1y features supported by other languages as extensions.
1038 .Case("cxx_binary_literals", true)
Joao Matos3e1ec722012-08-31 21:34:27 +00001039 .Default(false);
1040}
1041
1042/// HasAttribute - Return true if we recognize and implement the attribute
1043/// specified by the given identifier.
1044static bool HasAttribute(const IdentifierInfo *II) {
1045 StringRef Name = II->getName();
1046 // Normalize the attribute name, __foo__ becomes foo.
1047 if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4)
1048 Name = Name.substr(2, Name.size() - 4);
1049
1050 // FIXME: Do we need to handle namespaces here?
1051 return llvm::StringSwitch<bool>(Name)
1052#include "clang/Lex/AttrSpellings.inc"
1053 .Default(false);
1054}
1055
1056/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1057/// or '__has_include_next("path")' expression.
1058/// Returns true if successful.
1059static bool EvaluateHasIncludeCommon(Token &Tok,
1060 IdentifierInfo *II, Preprocessor &PP,
1061 const DirectoryLookup *LookupFrom) {
Richard Trieu97bc3d52012-10-22 20:28:48 +00001062 // Save the location of the current token. If a '(' is later found, use
Aaron Ballman6b716c52013-01-15 21:59:46 +00001063 // that location. If not, use the end of this location instead.
Richard Trieu97bc3d52012-10-22 20:28:48 +00001064 SourceLocation LParenLoc = Tok.getLocation();
Joao Matos3e1ec722012-08-31 21:34:27 +00001065
Aaron Ballman31672b12013-01-16 19:32:21 +00001066 // These expressions are only allowed within a preprocessor directive.
1067 if (!PP.isParsingIfOrElifDirective()) {
1068 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
1069 return false;
1070 }
1071
Joao Matos3e1ec722012-08-31 21:34:27 +00001072 // Get '('.
1073 PP.LexNonComment(Tok);
1074
1075 // Ensure we have a '('.
1076 if (Tok.isNot(tok::l_paren)) {
Richard Trieu97bc3d52012-10-22 20:28:48 +00001077 // No '(', use end of last token.
1078 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1079 PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName();
1080 // If the next token looks like a filename or the start of one,
1081 // assume it is and process it as such.
1082 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
1083 !Tok.is(tok::less))
1084 return false;
1085 } else {
1086 // Save '(' location for possible missing ')' message.
1087 LParenLoc = Tok.getLocation();
1088
Eli Friedmana0f2d022013-01-09 02:20:00 +00001089 if (PP.getCurrentLexer()) {
1090 // Get the file name.
1091 PP.getCurrentLexer()->LexIncludeFilename(Tok);
1092 } else {
1093 // We're in a macro, so we can't use LexIncludeFilename; just
1094 // grab the next token.
1095 PP.Lex(Tok);
1096 }
Joao Matos3e1ec722012-08-31 21:34:27 +00001097 }
1098
Joao Matos3e1ec722012-08-31 21:34:27 +00001099 // Reserve a buffer to get the spelling.
1100 SmallString<128> FilenameBuffer;
1101 StringRef Filename;
1102 SourceLocation EndLoc;
1103
1104 switch (Tok.getKind()) {
1105 case tok::eod:
1106 // If the token kind is EOD, the error has already been diagnosed.
1107 return false;
1108
1109 case tok::angle_string_literal:
1110 case tok::string_literal: {
1111 bool Invalid = false;
1112 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1113 if (Invalid)
1114 return false;
1115 break;
1116 }
1117
1118 case tok::less:
1119 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1120 // case, glue the tokens together into FilenameBuffer and interpret those.
1121 FilenameBuffer.push_back('<');
Richard Trieu97bc3d52012-10-22 20:28:48 +00001122 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
1123 // Let the caller know a <eod> was found by changing the Token kind.
1124 Tok.setKind(tok::eod);
Joao Matos3e1ec722012-08-31 21:34:27 +00001125 return false; // Found <eod> but no ">"? Diagnostic already emitted.
Richard Trieu97bc3d52012-10-22 20:28:48 +00001126 }
Joao Matos3e1ec722012-08-31 21:34:27 +00001127 Filename = FilenameBuffer.str();
1128 break;
1129 default:
1130 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1131 return false;
1132 }
1133
Richard Trieu97bc3d52012-10-22 20:28:48 +00001134 SourceLocation FilenameLoc = Tok.getLocation();
1135
Joao Matos3e1ec722012-08-31 21:34:27 +00001136 // Get ')'.
1137 PP.LexNonComment(Tok);
1138
1139 // Ensure we have a trailing ).
1140 if (Tok.isNot(tok::r_paren)) {
Richard Trieu97bc3d52012-10-22 20:28:48 +00001141 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen)
1142 << II->getName();
Joao Matos3e1ec722012-08-31 21:34:27 +00001143 PP.Diag(LParenLoc, diag::note_matching) << "(";
1144 return false;
1145 }
1146
1147 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1148 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1149 // error.
1150 if (Filename.empty())
1151 return false;
1152
1153 // Search include directories.
1154 const DirectoryLookup *CurDir;
1155 const FileEntry *File =
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001156 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL,
1157 NULL, NULL);
Joao Matos3e1ec722012-08-31 21:34:27 +00001158
1159 // Get the result value. A result of true means the file exists.
1160 return File != 0;
1161}
1162
1163/// EvaluateHasInclude - Process a '__has_include("path")' expression.
1164/// Returns true if successful.
1165static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1166 Preprocessor &PP) {
1167 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
1168}
1169
1170/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1171/// Returns true if successful.
1172static bool EvaluateHasIncludeNext(Token &Tok,
1173 IdentifierInfo *II, Preprocessor &PP) {
1174 // __has_include_next is like __has_include, except that we start
1175 // searching after the current found directory. If we can't do this,
1176 // issue a diagnostic.
1177 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1178 if (PP.isInPrimaryFile()) {
1179 Lookup = 0;
1180 PP.Diag(Tok, diag::pp_include_next_in_primary);
1181 } else if (Lookup == 0) {
1182 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1183 } else {
1184 // Start looking up in the next directory.
1185 ++Lookup;
1186 }
1187
1188 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1189}
1190
Douglas Gregorb09de512012-09-25 15:44:52 +00001191/// \brief Process __building_module(identifier) expression.
1192/// \returns true if we are building the named module, false otherwise.
1193static bool EvaluateBuildingModule(Token &Tok,
1194 IdentifierInfo *II, Preprocessor &PP) {
1195 // Get '('.
1196 PP.LexNonComment(Tok);
1197
1198 // Ensure we have a '('.
1199 if (Tok.isNot(tok::l_paren)) {
1200 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
1201 return false;
1202 }
1203
1204 // Save '(' location for possible missing ')' message.
1205 SourceLocation LParenLoc = Tok.getLocation();
1206
1207 // Get the module name.
1208 PP.LexNonComment(Tok);
1209
1210 // Ensure that we have an identifier.
1211 if (Tok.isNot(tok::identifier)) {
1212 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1213 return false;
1214 }
1215
1216 bool Result
1217 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1218
1219 // Get ')'.
1220 PP.LexNonComment(Tok);
1221
1222 // Ensure we have a trailing ).
1223 if (Tok.isNot(tok::r_paren)) {
1224 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
1225 PP.Diag(LParenLoc, diag::note_matching) << "(";
1226 return false;
1227 }
1228
1229 return Result;
1230}
1231
Joao Matos3e1ec722012-08-31 21:34:27 +00001232/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1233/// as a builtin macro, handle it and return the next token as 'Tok'.
1234void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1235 // Figure out which token this is.
1236 IdentifierInfo *II = Tok.getIdentifierInfo();
1237 assert(II && "Can't be a macro without id info!");
1238
1239 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1240 // invoke the pragma handler, then lex the token after it.
1241 if (II == Ident_Pragma)
1242 return Handle_Pragma(Tok);
1243 else if (II == Ident__pragma) // in non-MS mode this is null
1244 return HandleMicrosoft__pragma(Tok);
1245
1246 ++NumBuiltinMacroExpanded;
1247
1248 SmallString<128> TmpBuffer;
1249 llvm::raw_svector_ostream OS(TmpBuffer);
1250
1251 // Set up the return result.
1252 Tok.setIdentifierInfo(0);
1253 Tok.clearFlag(Token::NeedsCleaning);
1254
1255 if (II == Ident__LINE__) {
1256 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1257 // source file) of the current source line (an integer constant)". This can
1258 // be affected by #line.
1259 SourceLocation Loc = Tok.getLocation();
1260
1261 // Advance to the location of the first _, this might not be the first byte
1262 // of the token if it starts with an escaped newline.
1263 Loc = AdvanceToTokenCharacter(Loc, 0);
1264
1265 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1266 // a macro expansion. This doesn't matter for object-like macros, but
1267 // can matter for a function-like macro that expands to contain __LINE__.
1268 // Skip down through expansion points until we find a file loc for the
1269 // end of the expansion history.
1270 Loc = SourceMgr.getExpansionRange(Loc).second;
1271 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1272
1273 // __LINE__ expands to a simple numeric value.
1274 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1275 Tok.setKind(tok::numeric_constant);
1276 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1277 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1278 // character string literal)". This can be affected by #line.
1279 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1280
1281 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1282 // #include stack instead of the current file.
1283 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1284 SourceLocation NextLoc = PLoc.getIncludeLoc();
1285 while (NextLoc.isValid()) {
1286 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1287 if (PLoc.isInvalid())
1288 break;
1289
1290 NextLoc = PLoc.getIncludeLoc();
1291 }
1292 }
1293
1294 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1295 SmallString<128> FN;
1296 if (PLoc.isValid()) {
1297 FN += PLoc.getFilename();
1298 Lexer::Stringify(FN);
1299 OS << '"' << FN.str() << '"';
1300 }
1301 Tok.setKind(tok::string_literal);
1302 } else if (II == Ident__DATE__) {
1303 if (!DATELoc.isValid())
1304 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1305 Tok.setKind(tok::string_literal);
1306 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1307 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1308 Tok.getLocation(),
1309 Tok.getLength()));
1310 return;
1311 } else if (II == Ident__TIME__) {
1312 if (!TIMELoc.isValid())
1313 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1314 Tok.setKind(tok::string_literal);
1315 Tok.setLength(strlen("\"hh:mm:ss\""));
1316 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1317 Tok.getLocation(),
1318 Tok.getLength()));
1319 return;
1320 } else if (II == Ident__INCLUDE_LEVEL__) {
1321 // Compute the presumed include depth of this token. This can be affected
1322 // by GNU line markers.
1323 unsigned Depth = 0;
1324
1325 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1326 if (PLoc.isValid()) {
1327 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1328 for (; PLoc.isValid(); ++Depth)
1329 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1330 }
1331
1332 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1333 OS << Depth;
1334 Tok.setKind(tok::numeric_constant);
1335 } else if (II == Ident__TIMESTAMP__) {
1336 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1337 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1338
1339 // Get the file that we are lexing out of. If we're currently lexing from
1340 // a macro, dig into the include stack.
1341 const FileEntry *CurFile = 0;
1342 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1343
1344 if (TheLexer)
1345 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1346
1347 const char *Result;
1348 if (CurFile) {
1349 time_t TT = CurFile->getModificationTime();
1350 struct tm *TM = localtime(&TT);
1351 Result = asctime(TM);
1352 } else {
1353 Result = "??? ??? ?? ??:??:?? ????\n";
1354 }
1355 // Surround the string with " and strip the trailing newline.
1356 OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
1357 Tok.setKind(tok::string_literal);
1358 } else if (II == Ident__COUNTER__) {
1359 // __COUNTER__ expands to a simple numeric value.
1360 OS << CounterValue++;
1361 Tok.setKind(tok::numeric_constant);
1362 } else if (II == Ident__has_feature ||
1363 II == Ident__has_extension ||
1364 II == Ident__has_builtin ||
1365 II == Ident__has_attribute) {
1366 // The argument to these builtins should be a parenthesized identifier.
1367 SourceLocation StartLoc = Tok.getLocation();
1368
1369 bool IsValid = false;
1370 IdentifierInfo *FeatureII = 0;
1371
1372 // Read the '('.
Andy Gibbs3f03b582012-11-17 19:18:27 +00001373 LexUnexpandedToken(Tok);
Joao Matos3e1ec722012-08-31 21:34:27 +00001374 if (Tok.is(tok::l_paren)) {
1375 // Read the identifier
Andy Gibbs3f03b582012-11-17 19:18:27 +00001376 LexUnexpandedToken(Tok);
Richard Smith899022b2013-07-09 00:57:56 +00001377 if ((FeatureII = Tok.getIdentifierInfo())) {
Joao Matos3e1ec722012-08-31 21:34:27 +00001378 // Read the ')'.
Andy Gibbs3f03b582012-11-17 19:18:27 +00001379 LexUnexpandedToken(Tok);
Joao Matos3e1ec722012-08-31 21:34:27 +00001380 if (Tok.is(tok::r_paren))
1381 IsValid = true;
1382 }
1383 }
1384
1385 bool Value = false;
1386 if (!IsValid)
1387 Diag(StartLoc, diag::err_feature_check_malformed);
1388 else if (II == Ident__has_builtin) {
1389 // Check for a builtin is trivial.
1390 Value = FeatureII->getBuiltinID() != 0;
1391 } else if (II == Ident__has_attribute)
1392 Value = HasAttribute(FeatureII);
1393 else if (II == Ident__has_extension)
1394 Value = HasExtension(*this, FeatureII);
1395 else {
1396 assert(II == Ident__has_feature && "Must be feature check");
1397 Value = HasFeature(*this, FeatureII);
1398 }
1399
1400 OS << (int)Value;
1401 if (IsValid)
1402 Tok.setKind(tok::numeric_constant);
1403 } else if (II == Ident__has_include ||
1404 II == Ident__has_include_next) {
1405 // The argument to these two builtins should be a parenthesized
1406 // file name string literal using angle brackets (<>) or
1407 // double-quotes ("").
1408 bool Value;
1409 if (II == Ident__has_include)
1410 Value = EvaluateHasInclude(Tok, II, *this);
1411 else
1412 Value = EvaluateHasIncludeNext(Tok, II, *this);
1413 OS << (int)Value;
Richard Trieu97bc3d52012-10-22 20:28:48 +00001414 if (Tok.is(tok::r_paren))
1415 Tok.setKind(tok::numeric_constant);
Joao Matos3e1ec722012-08-31 21:34:27 +00001416 } else if (II == Ident__has_warning) {
1417 // The argument should be a parenthesized string literal.
1418 // The argument to these builtins should be a parenthesized identifier.
1419 SourceLocation StartLoc = Tok.getLocation();
1420 bool IsValid = false;
1421 bool Value = false;
1422 // Read the '('.
Andy Gibbs02a17682012-11-17 19:15:38 +00001423 LexUnexpandedToken(Tok);
Joao Matos3e1ec722012-08-31 21:34:27 +00001424 do {
Andy Gibbs02a17682012-11-17 19:15:38 +00001425 if (Tok.isNot(tok::l_paren)) {
1426 Diag(StartLoc, diag::err_warning_check_malformed);
1427 break;
Joao Matos3e1ec722012-08-31 21:34:27 +00001428 }
Andy Gibbs02a17682012-11-17 19:15:38 +00001429
1430 LexUnexpandedToken(Tok);
1431 std::string WarningName;
1432 SourceLocation StrStartLoc = Tok.getLocation();
Andy Gibbs97f84612012-11-17 19:16:52 +00001433 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1434 /*MacroExpansion=*/false)) {
Andy Gibbs02a17682012-11-17 19:15:38 +00001435 // Eat tokens until ')'.
Andy Gibbs6d534d42012-11-17 22:17:28 +00001436 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
1437 Tok.isNot(tok::eof))
Andy Gibbs02a17682012-11-17 19:15:38 +00001438 LexUnexpandedToken(Tok);
1439 break;
1440 }
1441
1442 // Is the end a ')'?
1443 if (!(IsValid = Tok.is(tok::r_paren))) {
1444 Diag(StartLoc, diag::err_warning_check_malformed);
1445 break;
1446 }
1447
1448 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1449 WarningName[1] != 'W') {
1450 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1451 break;
1452 }
1453
1454 // Finally, check if the warning flags maps to a diagnostic group.
1455 // We construct a SmallVector here to talk to getDiagnosticIDs().
1456 // Although we don't use the result, this isn't a hot path, and not
1457 // worth special casing.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001458 SmallVector<diag::kind, 10> Diags;
Andy Gibbs02a17682012-11-17 19:15:38 +00001459 Value = !getDiagnostics().getDiagnosticIDs()->
1460 getDiagnosticsInGroup(WarningName.substr(2), Diags);
Joao Matos3e1ec722012-08-31 21:34:27 +00001461 } while (false);
Joao Matos3e1ec722012-08-31 21:34:27 +00001462
1463 OS << (int)Value;
Andy Gibbsb9971ba2012-11-17 19:14:53 +00001464 if (IsValid)
1465 Tok.setKind(tok::numeric_constant);
Douglas Gregorb09de512012-09-25 15:44:52 +00001466 } else if (II == Ident__building_module) {
1467 // The argument to this builtin should be an identifier. The
1468 // builtin evaluates to 1 when that identifier names the module we are
1469 // currently building.
1470 OS << (int)EvaluateBuildingModule(Tok, II, *this);
1471 Tok.setKind(tok::numeric_constant);
1472 } else if (II == Ident__MODULE__) {
1473 // The current module as an identifier.
1474 OS << getLangOpts().CurrentModule;
1475 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1476 Tok.setIdentifierInfo(ModuleII);
1477 Tok.setKind(ModuleII->getTokenID());
Joao Matos3e1ec722012-08-31 21:34:27 +00001478 } else {
1479 llvm_unreachable("Unknown identifier!");
1480 }
Dmitri Gribenko374b3832012-09-24 21:07:17 +00001481 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
Joao Matos3e1ec722012-08-31 21:34:27 +00001482}
1483
1484void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1485 // If the 'used' status changed, and the macro requires 'unused' warning,
1486 // remove its SourceLocation from the warn-for-unused-macro locations.
1487 if (MI->isWarnIfUnused() && !MI->isUsed())
1488 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1489 MI->setIsUsed(true);
1490}