blob: f20633fda8c9ec887e82f95f949e1c83d61bd1d0 [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?
Ted Kremenek89ca1fb2013-10-14 23:48:27 +0000900 .Case("objc_protocol_qualifier_mangling", true)
Joao Matos3e1ec722012-08-31 21:34:27 +0000901 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
902 .Case("ownership_holds", true)
903 .Case("ownership_returns", true)
904 .Case("ownership_takes", true)
905 .Case("objc_bool", true)
906 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
907 .Case("objc_array_literals", LangOpts.ObjC2)
908 .Case("objc_dictionary_literals", LangOpts.ObjC2)
909 .Case("objc_boxed_expressions", LangOpts.ObjC2)
910 .Case("arc_cf_code_audited", true)
911 // C11 features
912 .Case("c_alignas", LangOpts.C11)
913 .Case("c_atomic", LangOpts.C11)
914 .Case("c_generic_selections", LangOpts.C11)
915 .Case("c_static_assert", LangOpts.C11)
Douglas Gregore87c5bd2013-05-02 05:28:32 +0000916 .Case("c_thread_local",
917 LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
Joao Matos3e1ec722012-08-31 21:34:27 +0000918 // C++11 features
Richard Smith80ad52f2013-01-02 11:42:31 +0000919 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
920 .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
921 .Case("cxx_alignas", LangOpts.CPlusPlus11)
922 .Case("cxx_atomic", LangOpts.CPlusPlus11)
923 .Case("cxx_attributes", LangOpts.CPlusPlus11)
924 .Case("cxx_auto_type", LangOpts.CPlusPlus11)
925 .Case("cxx_constexpr", LangOpts.CPlusPlus11)
926 .Case("cxx_decltype", LangOpts.CPlusPlus11)
927 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
928 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
929 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
930 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
931 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
932 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
933 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
934 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
Richard Smithe6e68b52013-04-19 17:00:31 +0000935 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
Richard Smith80ad52f2013-01-02 11:42:31 +0000936 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
937 .Case("cxx_lambdas", LangOpts.CPlusPlus11)
938 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
939 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
940 .Case("cxx_noexcept", LangOpts.CPlusPlus11)
941 .Case("cxx_nullptr", LangOpts.CPlusPlus11)
942 .Case("cxx_override_control", LangOpts.CPlusPlus11)
943 .Case("cxx_range_for", LangOpts.CPlusPlus11)
944 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
945 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
946 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
947 .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
948 .Case("cxx_static_assert", LangOpts.CPlusPlus11)
Richard Smith44868f52013-09-28 04:37:56 +0000949 .Case("cxx_thread_local",
Douglas Gregore87c5bd2013-05-02 05:28:32 +0000950 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
Richard Smith80ad52f2013-01-02 11:42:31 +0000951 .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
952 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
953 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
954 .Case("cxx_user_literals", LangOpts.CPlusPlus11)
955 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000956 // C++1y features
Richard Smith3c3a5222013-07-24 17:51:13 +0000957 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
Richard Smith7f0ffb32013-05-07 19:32:56 +0000958 .Case("cxx_binary_literals", LangOpts.CPlusPlus1y)
Richard Smitha4fb3392013-07-24 17:41:31 +0000959 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y)
Richard Smith44868f52013-09-28 04:37:56 +0000960 //.Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y)
961 .Case("cxx_init_captures", LangOpts.CPlusPlus1y)
Richard Smitha4fb3392013-07-24 17:41:31 +0000962 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
Richard Smithf45c2992013-05-12 03:09:35 +0000963 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
Richard Smith44868f52013-09-28 04:37:56 +0000964 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlus1y)
Richard Smith629bc522013-09-27 20:19:41 +0000965 .Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
Joao Matos3e1ec722012-08-31 21:34:27 +0000966 // Type traits
967 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
968 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
969 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
970 .Case("has_trivial_assign", LangOpts.CPlusPlus)
971 .Case("has_trivial_copy", LangOpts.CPlusPlus)
972 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
973 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
974 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
975 .Case("is_abstract", LangOpts.CPlusPlus)
976 .Case("is_base_of", LangOpts.CPlusPlus)
977 .Case("is_class", LangOpts.CPlusPlus)
978 .Case("is_convertible_to", LangOpts.CPlusPlus)
Joao Matos3e1ec722012-08-31 21:34:27 +0000979 .Case("is_empty", LangOpts.CPlusPlus)
980 .Case("is_enum", LangOpts.CPlusPlus)
981 .Case("is_final", LangOpts.CPlusPlus)
982 .Case("is_literal", LangOpts.CPlusPlus)
983 .Case("is_standard_layout", LangOpts.CPlusPlus)
984 .Case("is_pod", LangOpts.CPlusPlus)
985 .Case("is_polymorphic", LangOpts.CPlusPlus)
David Majnemer7121bdb2013-10-18 00:33:31 +0000986 .Case("is_sealed", LangOpts.MicrosoftExt)
Joao Matos3e1ec722012-08-31 21:34:27 +0000987 .Case("is_trivial", LangOpts.CPlusPlus)
988 .Case("is_trivially_assignable", LangOpts.CPlusPlus)
989 .Case("is_trivially_constructible", LangOpts.CPlusPlus)
990 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
991 .Case("is_union", LangOpts.CPlusPlus)
992 .Case("modules", LangOpts.Modules)
993 .Case("tls", PP.getTargetInfo().isTLSSupported())
994 .Case("underlying_type", LangOpts.CPlusPlus)
995 .Default(false);
996}
997
998/// HasExtension - Return true if we recognize and implement the feature
999/// specified by the identifier, either as an extension or a standard language
1000/// feature.
1001static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
1002 if (HasFeature(PP, II))
1003 return true;
1004
1005 // If the use of an extension results in an error diagnostic, extensions are
1006 // effectively unavailable, so just return false here.
1007 if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
1008 DiagnosticsEngine::Ext_Error)
1009 return false;
1010
1011 const LangOptions &LangOpts = PP.getLangOpts();
1012 StringRef Extension = II->getName();
1013
1014 // Normalize the extension name, __foo__ becomes foo.
1015 if (Extension.startswith("__") && Extension.endswith("__") &&
1016 Extension.size() >= 4)
1017 Extension = Extension.substr(2, Extension.size() - 4);
1018
1019 // Because we inherit the feature list from HasFeature, this string switch
1020 // must be less restrictive than HasFeature's.
1021 return llvm::StringSwitch<bool>(Extension)
1022 // C11 features supported by other languages as extensions.
1023 .Case("c_alignas", true)
1024 .Case("c_atomic", true)
1025 .Case("c_generic_selections", true)
1026 .Case("c_static_assert", true)
Ed Schoutenfa7d53f2013-09-14 16:17:20 +00001027 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
Richard Smith7f0ffb32013-05-07 19:32:56 +00001028 // C++11 features supported by other languages as extensions.
Joao Matos3e1ec722012-08-31 21:34:27 +00001029 .Case("cxx_atomic", LangOpts.CPlusPlus)
1030 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
1031 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
1032 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
1033 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
1034 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
1035 .Case("cxx_override_control", LangOpts.CPlusPlus)
1036 .Case("cxx_range_for", LangOpts.CPlusPlus)
1037 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
1038 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
Richard Smith7f0ffb32013-05-07 19:32:56 +00001039 // C++1y features supported by other languages as extensions.
1040 .Case("cxx_binary_literals", true)
Richard Smith44868f52013-09-28 04:37:56 +00001041 .Case("cxx_init_captures", LangOpts.CPlusPlus11)
Richard Smith629bc522013-09-27 20:19:41 +00001042 .Case("cxx_variable_templates", true)
Joao Matos3e1ec722012-08-31 21:34:27 +00001043 .Default(false);
1044}
1045
1046/// HasAttribute - Return true if we recognize and implement the attribute
1047/// specified by the given identifier.
1048static bool HasAttribute(const IdentifierInfo *II) {
1049 StringRef Name = II->getName();
1050 // Normalize the attribute name, __foo__ becomes foo.
1051 if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4)
1052 Name = Name.substr(2, Name.size() - 4);
1053
1054 // FIXME: Do we need to handle namespaces here?
1055 return llvm::StringSwitch<bool>(Name)
1056#include "clang/Lex/AttrSpellings.inc"
1057 .Default(false);
1058}
1059
1060/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1061/// or '__has_include_next("path")' expression.
1062/// Returns true if successful.
1063static bool EvaluateHasIncludeCommon(Token &Tok,
1064 IdentifierInfo *II, Preprocessor &PP,
1065 const DirectoryLookup *LookupFrom) {
Richard Trieu97bc3d52012-10-22 20:28:48 +00001066 // Save the location of the current token. If a '(' is later found, use
Aaron Ballman6b716c52013-01-15 21:59:46 +00001067 // that location. If not, use the end of this location instead.
Richard Trieu97bc3d52012-10-22 20:28:48 +00001068 SourceLocation LParenLoc = Tok.getLocation();
Joao Matos3e1ec722012-08-31 21:34:27 +00001069
Aaron Ballman31672b12013-01-16 19:32:21 +00001070 // These expressions are only allowed within a preprocessor directive.
1071 if (!PP.isParsingIfOrElifDirective()) {
1072 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
1073 return false;
1074 }
1075
Joao Matos3e1ec722012-08-31 21:34:27 +00001076 // Get '('.
1077 PP.LexNonComment(Tok);
1078
1079 // Ensure we have a '('.
1080 if (Tok.isNot(tok::l_paren)) {
Richard Trieu97bc3d52012-10-22 20:28:48 +00001081 // No '(', use end of last token.
1082 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1083 PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName();
1084 // If the next token looks like a filename or the start of one,
1085 // assume it is and process it as such.
1086 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
1087 !Tok.is(tok::less))
1088 return false;
1089 } else {
1090 // Save '(' location for possible missing ')' message.
1091 LParenLoc = Tok.getLocation();
1092
Eli Friedmana0f2d022013-01-09 02:20:00 +00001093 if (PP.getCurrentLexer()) {
1094 // Get the file name.
1095 PP.getCurrentLexer()->LexIncludeFilename(Tok);
1096 } else {
1097 // We're in a macro, so we can't use LexIncludeFilename; just
1098 // grab the next token.
1099 PP.Lex(Tok);
1100 }
Joao Matos3e1ec722012-08-31 21:34:27 +00001101 }
1102
Joao Matos3e1ec722012-08-31 21:34:27 +00001103 // Reserve a buffer to get the spelling.
1104 SmallString<128> FilenameBuffer;
1105 StringRef Filename;
1106 SourceLocation EndLoc;
1107
1108 switch (Tok.getKind()) {
1109 case tok::eod:
1110 // If the token kind is EOD, the error has already been diagnosed.
1111 return false;
1112
1113 case tok::angle_string_literal:
1114 case tok::string_literal: {
1115 bool Invalid = false;
1116 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1117 if (Invalid)
1118 return false;
1119 break;
1120 }
1121
1122 case tok::less:
1123 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1124 // case, glue the tokens together into FilenameBuffer and interpret those.
1125 FilenameBuffer.push_back('<');
Richard Trieu97bc3d52012-10-22 20:28:48 +00001126 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
1127 // Let the caller know a <eod> was found by changing the Token kind.
1128 Tok.setKind(tok::eod);
Joao Matos3e1ec722012-08-31 21:34:27 +00001129 return false; // Found <eod> but no ">"? Diagnostic already emitted.
Richard Trieu97bc3d52012-10-22 20:28:48 +00001130 }
Joao Matos3e1ec722012-08-31 21:34:27 +00001131 Filename = FilenameBuffer.str();
1132 break;
1133 default:
1134 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1135 return false;
1136 }
1137
Richard Trieu97bc3d52012-10-22 20:28:48 +00001138 SourceLocation FilenameLoc = Tok.getLocation();
1139
Joao Matos3e1ec722012-08-31 21:34:27 +00001140 // Get ')'.
1141 PP.LexNonComment(Tok);
1142
1143 // Ensure we have a trailing ).
1144 if (Tok.isNot(tok::r_paren)) {
Richard Trieu97bc3d52012-10-22 20:28:48 +00001145 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen)
1146 << II->getName();
Joao Matos3e1ec722012-08-31 21:34:27 +00001147 PP.Diag(LParenLoc, diag::note_matching) << "(";
1148 return false;
1149 }
1150
1151 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1152 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1153 // error.
1154 if (Filename.empty())
1155 return false;
1156
1157 // Search include directories.
1158 const DirectoryLookup *CurDir;
1159 const FileEntry *File =
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001160 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL,
1161 NULL, NULL);
Joao Matos3e1ec722012-08-31 21:34:27 +00001162
1163 // Get the result value. A result of true means the file exists.
1164 return File != 0;
1165}
1166
1167/// EvaluateHasInclude - Process a '__has_include("path")' expression.
1168/// Returns true if successful.
1169static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1170 Preprocessor &PP) {
1171 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
1172}
1173
1174/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1175/// Returns true if successful.
1176static bool EvaluateHasIncludeNext(Token &Tok,
1177 IdentifierInfo *II, Preprocessor &PP) {
1178 // __has_include_next is like __has_include, except that we start
1179 // searching after the current found directory. If we can't do this,
1180 // issue a diagnostic.
1181 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1182 if (PP.isInPrimaryFile()) {
1183 Lookup = 0;
1184 PP.Diag(Tok, diag::pp_include_next_in_primary);
1185 } else if (Lookup == 0) {
1186 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1187 } else {
1188 // Start looking up in the next directory.
1189 ++Lookup;
1190 }
1191
1192 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1193}
1194
Douglas Gregorb09de512012-09-25 15:44:52 +00001195/// \brief Process __building_module(identifier) expression.
1196/// \returns true if we are building the named module, false otherwise.
1197static bool EvaluateBuildingModule(Token &Tok,
1198 IdentifierInfo *II, Preprocessor &PP) {
1199 // Get '('.
1200 PP.LexNonComment(Tok);
1201
1202 // Ensure we have a '('.
1203 if (Tok.isNot(tok::l_paren)) {
1204 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
1205 return false;
1206 }
1207
1208 // Save '(' location for possible missing ')' message.
1209 SourceLocation LParenLoc = Tok.getLocation();
1210
1211 // Get the module name.
1212 PP.LexNonComment(Tok);
1213
1214 // Ensure that we have an identifier.
1215 if (Tok.isNot(tok::identifier)) {
1216 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1217 return false;
1218 }
1219
1220 bool Result
1221 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1222
1223 // Get ')'.
1224 PP.LexNonComment(Tok);
1225
1226 // Ensure we have a trailing ).
1227 if (Tok.isNot(tok::r_paren)) {
1228 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
1229 PP.Diag(LParenLoc, diag::note_matching) << "(";
1230 return false;
1231 }
1232
1233 return Result;
1234}
1235
Joao Matos3e1ec722012-08-31 21:34:27 +00001236/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1237/// as a builtin macro, handle it and return the next token as 'Tok'.
1238void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1239 // Figure out which token this is.
1240 IdentifierInfo *II = Tok.getIdentifierInfo();
1241 assert(II && "Can't be a macro without id info!");
1242
1243 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1244 // invoke the pragma handler, then lex the token after it.
1245 if (II == Ident_Pragma)
1246 return Handle_Pragma(Tok);
1247 else if (II == Ident__pragma) // in non-MS mode this is null
1248 return HandleMicrosoft__pragma(Tok);
1249
1250 ++NumBuiltinMacroExpanded;
1251
1252 SmallString<128> TmpBuffer;
1253 llvm::raw_svector_ostream OS(TmpBuffer);
1254
1255 // Set up the return result.
1256 Tok.setIdentifierInfo(0);
1257 Tok.clearFlag(Token::NeedsCleaning);
1258
1259 if (II == Ident__LINE__) {
1260 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1261 // source file) of the current source line (an integer constant)". This can
1262 // be affected by #line.
1263 SourceLocation Loc = Tok.getLocation();
1264
1265 // Advance to the location of the first _, this might not be the first byte
1266 // of the token if it starts with an escaped newline.
1267 Loc = AdvanceToTokenCharacter(Loc, 0);
1268
1269 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1270 // a macro expansion. This doesn't matter for object-like macros, but
1271 // can matter for a function-like macro that expands to contain __LINE__.
1272 // Skip down through expansion points until we find a file loc for the
1273 // end of the expansion history.
1274 Loc = SourceMgr.getExpansionRange(Loc).second;
1275 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1276
1277 // __LINE__ expands to a simple numeric value.
1278 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1279 Tok.setKind(tok::numeric_constant);
1280 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1281 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1282 // character string literal)". This can be affected by #line.
1283 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1284
1285 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1286 // #include stack instead of the current file.
1287 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1288 SourceLocation NextLoc = PLoc.getIncludeLoc();
1289 while (NextLoc.isValid()) {
1290 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1291 if (PLoc.isInvalid())
1292 break;
1293
1294 NextLoc = PLoc.getIncludeLoc();
1295 }
1296 }
1297
1298 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1299 SmallString<128> FN;
1300 if (PLoc.isValid()) {
1301 FN += PLoc.getFilename();
1302 Lexer::Stringify(FN);
1303 OS << '"' << FN.str() << '"';
1304 }
1305 Tok.setKind(tok::string_literal);
1306 } else if (II == Ident__DATE__) {
1307 if (!DATELoc.isValid())
1308 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1309 Tok.setKind(tok::string_literal);
1310 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1311 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1312 Tok.getLocation(),
1313 Tok.getLength()));
1314 return;
1315 } else if (II == Ident__TIME__) {
1316 if (!TIMELoc.isValid())
1317 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1318 Tok.setKind(tok::string_literal);
1319 Tok.setLength(strlen("\"hh:mm:ss\""));
1320 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1321 Tok.getLocation(),
1322 Tok.getLength()));
1323 return;
1324 } else if (II == Ident__INCLUDE_LEVEL__) {
1325 // Compute the presumed include depth of this token. This can be affected
1326 // by GNU line markers.
1327 unsigned Depth = 0;
1328
1329 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1330 if (PLoc.isValid()) {
1331 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1332 for (; PLoc.isValid(); ++Depth)
1333 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1334 }
1335
1336 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1337 OS << Depth;
1338 Tok.setKind(tok::numeric_constant);
1339 } else if (II == Ident__TIMESTAMP__) {
1340 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1341 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1342
1343 // Get the file that we are lexing out of. If we're currently lexing from
1344 // a macro, dig into the include stack.
1345 const FileEntry *CurFile = 0;
1346 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1347
1348 if (TheLexer)
1349 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1350
1351 const char *Result;
1352 if (CurFile) {
1353 time_t TT = CurFile->getModificationTime();
1354 struct tm *TM = localtime(&TT);
1355 Result = asctime(TM);
1356 } else {
1357 Result = "??? ??? ?? ??:??:?? ????\n";
1358 }
1359 // Surround the string with " and strip the trailing newline.
1360 OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
1361 Tok.setKind(tok::string_literal);
1362 } else if (II == Ident__COUNTER__) {
1363 // __COUNTER__ expands to a simple numeric value.
1364 OS << CounterValue++;
1365 Tok.setKind(tok::numeric_constant);
1366 } else if (II == Ident__has_feature ||
1367 II == Ident__has_extension ||
1368 II == Ident__has_builtin ||
1369 II == Ident__has_attribute) {
1370 // The argument to these builtins should be a parenthesized identifier.
1371 SourceLocation StartLoc = Tok.getLocation();
1372
1373 bool IsValid = false;
1374 IdentifierInfo *FeatureII = 0;
1375
1376 // Read the '('.
Andy Gibbs3f03b582012-11-17 19:18:27 +00001377 LexUnexpandedToken(Tok);
Joao Matos3e1ec722012-08-31 21:34:27 +00001378 if (Tok.is(tok::l_paren)) {
1379 // Read the identifier
Andy Gibbs3f03b582012-11-17 19:18:27 +00001380 LexUnexpandedToken(Tok);
Richard Smith899022b2013-07-09 00:57:56 +00001381 if ((FeatureII = Tok.getIdentifierInfo())) {
Joao Matos3e1ec722012-08-31 21:34:27 +00001382 // Read the ')'.
Andy Gibbs3f03b582012-11-17 19:18:27 +00001383 LexUnexpandedToken(Tok);
Joao Matos3e1ec722012-08-31 21:34:27 +00001384 if (Tok.is(tok::r_paren))
1385 IsValid = true;
1386 }
1387 }
1388
1389 bool Value = false;
1390 if (!IsValid)
1391 Diag(StartLoc, diag::err_feature_check_malformed);
1392 else if (II == Ident__has_builtin) {
1393 // Check for a builtin is trivial.
1394 Value = FeatureII->getBuiltinID() != 0;
1395 } else if (II == Ident__has_attribute)
1396 Value = HasAttribute(FeatureII);
1397 else if (II == Ident__has_extension)
1398 Value = HasExtension(*this, FeatureII);
1399 else {
1400 assert(II == Ident__has_feature && "Must be feature check");
1401 Value = HasFeature(*this, FeatureII);
1402 }
1403
1404 OS << (int)Value;
1405 if (IsValid)
1406 Tok.setKind(tok::numeric_constant);
1407 } else if (II == Ident__has_include ||
1408 II == Ident__has_include_next) {
1409 // The argument to these two builtins should be a parenthesized
1410 // file name string literal using angle brackets (<>) or
1411 // double-quotes ("").
1412 bool Value;
1413 if (II == Ident__has_include)
1414 Value = EvaluateHasInclude(Tok, II, *this);
1415 else
1416 Value = EvaluateHasIncludeNext(Tok, II, *this);
1417 OS << (int)Value;
Richard Trieu97bc3d52012-10-22 20:28:48 +00001418 if (Tok.is(tok::r_paren))
1419 Tok.setKind(tok::numeric_constant);
Joao Matos3e1ec722012-08-31 21:34:27 +00001420 } else if (II == Ident__has_warning) {
1421 // The argument should be a parenthesized string literal.
1422 // The argument to these builtins should be a parenthesized identifier.
1423 SourceLocation StartLoc = Tok.getLocation();
1424 bool IsValid = false;
1425 bool Value = false;
1426 // Read the '('.
Andy Gibbs02a17682012-11-17 19:15:38 +00001427 LexUnexpandedToken(Tok);
Joao Matos3e1ec722012-08-31 21:34:27 +00001428 do {
Andy Gibbs02a17682012-11-17 19:15:38 +00001429 if (Tok.isNot(tok::l_paren)) {
1430 Diag(StartLoc, diag::err_warning_check_malformed);
1431 break;
Joao Matos3e1ec722012-08-31 21:34:27 +00001432 }
Andy Gibbs02a17682012-11-17 19:15:38 +00001433
1434 LexUnexpandedToken(Tok);
1435 std::string WarningName;
1436 SourceLocation StrStartLoc = Tok.getLocation();
Andy Gibbs97f84612012-11-17 19:16:52 +00001437 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1438 /*MacroExpansion=*/false)) {
Andy Gibbs02a17682012-11-17 19:15:38 +00001439 // Eat tokens until ')'.
Andy Gibbs6d534d42012-11-17 22:17:28 +00001440 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
1441 Tok.isNot(tok::eof))
Andy Gibbs02a17682012-11-17 19:15:38 +00001442 LexUnexpandedToken(Tok);
1443 break;
1444 }
1445
1446 // Is the end a ')'?
1447 if (!(IsValid = Tok.is(tok::r_paren))) {
1448 Diag(StartLoc, diag::err_warning_check_malformed);
1449 break;
1450 }
1451
1452 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1453 WarningName[1] != 'W') {
1454 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1455 break;
1456 }
1457
1458 // Finally, check if the warning flags maps to a diagnostic group.
1459 // We construct a SmallVector here to talk to getDiagnosticIDs().
1460 // Although we don't use the result, this isn't a hot path, and not
1461 // worth special casing.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001462 SmallVector<diag::kind, 10> Diags;
Andy Gibbs02a17682012-11-17 19:15:38 +00001463 Value = !getDiagnostics().getDiagnosticIDs()->
1464 getDiagnosticsInGroup(WarningName.substr(2), Diags);
Joao Matos3e1ec722012-08-31 21:34:27 +00001465 } while (false);
Joao Matos3e1ec722012-08-31 21:34:27 +00001466
1467 OS << (int)Value;
Andy Gibbsb9971ba2012-11-17 19:14:53 +00001468 if (IsValid)
1469 Tok.setKind(tok::numeric_constant);
Douglas Gregorb09de512012-09-25 15:44:52 +00001470 } else if (II == Ident__building_module) {
1471 // The argument to this builtin should be an identifier. The
1472 // builtin evaluates to 1 when that identifier names the module we are
1473 // currently building.
1474 OS << (int)EvaluateBuildingModule(Tok, II, *this);
1475 Tok.setKind(tok::numeric_constant);
1476 } else if (II == Ident__MODULE__) {
1477 // The current module as an identifier.
1478 OS << getLangOpts().CurrentModule;
1479 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1480 Tok.setIdentifierInfo(ModuleII);
1481 Tok.setKind(ModuleII->getTokenID());
Joao Matos3e1ec722012-08-31 21:34:27 +00001482 } else {
1483 llvm_unreachable("Unknown identifier!");
1484 }
Dmitri Gribenko374b3832012-09-24 21:07:17 +00001485 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
Joao Matos3e1ec722012-08-31 21:34:27 +00001486}
1487
1488void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1489 // If the 'used' status changed, and the macro requires 'unused' warning,
1490 // remove its SourceLocation from the warn-for-unused-macro locations.
1491 if (MI->isWarnIfUnused() && !MI->isUsed())
1492 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1493 MI->setIsUsed(true);
1494}