1 //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the top level handling of macro expansion for the
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Basic/Attributes.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Lex/CodeCompletionHandler.h"
21 #include "clang/Lex/ExternalPreprocessorSource.h"
22 #include "clang/Lex/LexDiagnostic.h"
23 #include "clang/Lex/MacroArgs.h"
24 #include "clang/Lex/MacroInfo.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/raw_ostream.h"
34 using namespace clang;
37 Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
38 assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
40 macro_iterator Pos = Macros.find(II);
41 assert(Pos != Macros.end() && "Identifier macro info is missing!");
45 void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
46 assert(MD && "MacroDirective should be non-zero!");
47 assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
49 MacroDirective *&StoredMD = Macros[II];
50 MD->setPrevious(StoredMD);
52 II->setHasMacroDefinition(MD->isDefined());
53 bool isImportedMacro = isa<DefMacroDirective>(MD) &&
54 cast<DefMacroDirective>(MD)->isImported();
55 if (II->isFromAST() && !isImportedMacro)
56 II->setChangedSinceDeserialization();
59 void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
62 MacroDirective *&StoredMD = Macros[II];
64 "the macro history was modified before initializing it from a pch");
66 // Setup the identifier as having associated macro history.
67 II->setHasMacroDefinition(true);
69 II->setHasMacroDefinition(false);
72 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
73 /// table and mark it as a builtin macro to be expanded.
74 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
75 // Get the identifier.
76 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
78 // Mark it as being a macro that is builtin.
79 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
80 MI->setIsBuiltinMacro();
81 PP.appendDefMacroDirective(Id, MI);
86 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
88 void Preprocessor::RegisterBuiltinMacros() {
89 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
90 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
91 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
92 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
93 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
94 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
97 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
98 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
99 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
101 // Microsoft Extensions.
102 if (LangOpts.MicrosoftExt) {
103 Ident__identifier = RegisterBuiltinMacro(*this, "__identifier");
104 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
106 Ident__identifier = nullptr;
107 Ident__pragma = nullptr;
111 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
112 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
113 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
114 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
115 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
116 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
117 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
118 Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier");
121 if (LangOpts.Modules) {
122 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
125 if (!LangOpts.CurrentModule.empty())
126 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
128 Ident__MODULE__ = nullptr;
130 Ident__building_module = nullptr;
131 Ident__MODULE__ = nullptr;
135 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
136 /// in its expansion, currently expands to that token literally.
137 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
138 const IdentifierInfo *MacroIdent,
140 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
142 // If the token isn't an identifier, it's always literally expanded.
143 if (!II) return true;
145 // If the information about this identifier is out of date, update it from
146 // the external source.
147 if (II->isOutOfDate())
148 PP.getExternalSource()->updateOutOfDateIdentifier(*II);
150 // If the identifier is a macro, and if that macro is enabled, it may be
151 // expanded so it's not a trivial expansion.
152 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
153 // Fast expanding "#define X X" is ok, because X would be disabled.
157 // If this is an object-like macro invocation, it is safe to trivially expand
159 if (MI->isObjectLike()) return true;
161 // If this is a function-like macro invocation, it's safe to trivially expand
162 // as long as the identifier is not a macro argument.
163 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
166 return false; // Identifier is a macro argument.
172 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
173 /// lexed is a '('. If so, consume the token and return true, if not, this
174 /// method should have no observable side-effect on the lexed tokens.
175 bool Preprocessor::isNextPPTokenLParen() {
176 // Do some quick tests for rejection cases.
179 Val = CurLexer->isNextPPTokenLParen();
180 else if (CurPTHLexer)
181 Val = CurPTHLexer->isNextPPTokenLParen();
183 Val = CurTokenLexer->isNextTokenLParen();
186 // We have run off the end. If it's a source file we don't
187 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
191 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
192 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
194 Val = Entry.TheLexer->isNextPPTokenLParen();
195 else if (Entry.ThePTHLexer)
196 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
198 Val = Entry.TheTokenLexer->isNextTokenLParen();
203 // Ran off the end of a source file?
204 if (Entry.ThePPLexer)
209 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
210 // have found something that isn't a '(' or we found the end of the
211 // translation unit. In either case, return false.
215 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
216 /// expanded as a macro, handle it and return the next token as 'Identifier'.
217 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
218 MacroDirective *MD) {
219 MacroDirective::DefInfo Def = MD->getDefinition();
220 assert(Def.isValid());
221 MacroInfo *MI = Def.getMacroInfo();
223 // If this is a macro expansion in the "#if !defined(x)" line for the file,
224 // then the macro could expand to different things in other contexts, we need
225 // to disable the optimization in this case.
226 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
228 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
229 if (MI->isBuiltinMacro()) {
230 if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
231 Identifier.getLocation(),
233 ExpandBuiltinMacro(Identifier);
237 /// Args - If this is a function-like macro expansion, this contains,
238 /// for each macro argument, the list of tokens that were provided to the
240 MacroArgs *Args = nullptr;
242 // Remember where the end of the expansion occurred. For an object-like
243 // macro, this is the identifier. For a function-like macro, this is the ')'.
244 SourceLocation ExpansionEnd = Identifier.getLocation();
246 // If this is a function-like macro, read the arguments.
247 if (MI->isFunctionLike()) {
248 // Remember that we are now parsing the arguments to a macro invocation.
249 // Preprocessor directives used inside macro arguments are not portable, and
250 // this enables the warning.
252 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
254 // Finished parsing args.
257 // If there was an error parsing the arguments, bail out.
258 if (!Args) return true;
260 ++NumFnMacroExpanded;
265 // Notice that this macro has been used.
268 // Remember where the token is expanded.
269 SourceLocation ExpandLoc = Identifier.getLocation();
270 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
274 // We can have macro expansion inside a conditional directive while
275 // reading the function macro arguments. To ensure, in that case, that
276 // MacroExpands callbacks still happen in source order, queue this
277 // callback to have it happen after the function macro callback.
278 DelayedMacroExpandsCallbacks.push_back(
279 MacroExpandsInfo(Identifier, MD, ExpansionRange));
281 Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args);
282 if (!DelayedMacroExpandsCallbacks.empty()) {
283 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
284 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
285 // FIXME: We lose macro args info with delayed callback.
286 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range,
289 DelayedMacroExpandsCallbacks.clear();
294 // If the macro definition is ambiguous, complain.
295 if (Def.getDirective()->isAmbiguous()) {
296 Diag(Identifier, diag::warn_pp_ambiguous_macro)
297 << Identifier.getIdentifierInfo();
298 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
299 << Identifier.getIdentifierInfo();
300 for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition();
301 PrevDef && !PrevDef.isUndefined();
302 PrevDef = PrevDef.getPreviousDefinition()) {
303 Diag(PrevDef.getMacroInfo()->getDefinitionLoc(),
304 diag::note_pp_ambiguous_macro_other)
305 << Identifier.getIdentifierInfo();
306 if (!PrevDef.getDirective()->isAmbiguous())
311 // If we started lexing a macro, enter the macro expansion body.
313 // If this macro expands to no tokens, don't bother to push it onto the
314 // expansion stack, only to take it right back off.
315 if (MI->getNumTokens() == 0) {
316 // No need for arg info.
317 if (Args) Args->destroy(*this);
319 // Propagate whitespace info as if we had pushed, then popped,
321 Identifier.setFlag(Token::LeadingEmptyMacro);
322 PropagateLineStartLeadingSpaceInfo(Identifier);
323 ++NumFastMacroExpanded;
325 } else if (MI->getNumTokens() == 1 &&
326 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
328 // Otherwise, if this macro expands into a single trivially-expanded
329 // token: expand it now. This handles common cases like
332 // No need for arg info.
333 if (Args) Args->destroy(*this);
335 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
336 // identifier to the expanded token.
337 bool isAtStartOfLine = Identifier.isAtStartOfLine();
338 bool hasLeadingSpace = Identifier.hasLeadingSpace();
340 // Replace the result token.
341 Identifier = MI->getReplacementToken(0);
343 // Restore the StartOfLine/LeadingSpace markers.
344 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
345 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
347 // Update the tokens location to include both its expansion and physical
350 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
351 ExpansionEnd,Identifier.getLength());
352 Identifier.setLocation(Loc);
354 // If this is a disabled macro or #define X X, we must mark the result as
356 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
357 if (MacroInfo *NewMI = getMacroInfo(NewII))
358 if (!NewMI->isEnabled() || NewMI == MI) {
359 Identifier.setFlag(Token::DisableExpand);
360 // Don't warn for "#define X X" like "#define bool bool" from
362 if (NewMI != MI || MI->isFunctionLike())
363 Diag(Identifier, diag::pp_disabled_macro_expansion);
367 // Since this is not an identifier token, it can't be macro expanded, so
369 ++NumFastMacroExpanded;
373 // Start expanding the macro.
374 EnterMacro(Identifier, ExpansionEnd, MI, Args);
383 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the
384 /// token vector are properly nested.
385 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
386 SmallVector<Bracket, 8> Brackets;
387 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
390 if (I->is(tok::l_paren)) {
391 Brackets.push_back(Paren);
392 } else if (I->is(tok::r_paren)) {
393 if (Brackets.empty() || Brackets.back() == Brace)
396 } else if (I->is(tok::l_brace)) {
397 Brackets.push_back(Brace);
398 } else if (I->is(tok::r_brace)) {
399 if (Brackets.empty() || Brackets.back() == Paren)
404 if (!Brackets.empty())
409 /// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
410 /// vector of tokens in NewTokens. The new number of arguments will be placed
411 /// in NumArgs and the ranges which need to surrounded in parentheses will be
413 /// Returns false if the token stream cannot be changed. If this is because
414 /// of an initializer list starting a macro argument, the range of those
415 /// initializer lists will be place in InitLists.
416 static bool GenerateNewArgTokens(Preprocessor &PP,
417 SmallVectorImpl<Token> &OldTokens,
418 SmallVectorImpl<Token> &NewTokens,
420 SmallVectorImpl<SourceRange> &ParenHints,
421 SmallVectorImpl<SourceRange> &InitLists) {
422 if (!CheckMatchedBrackets(OldTokens))
425 // Once it is known that the brackets are matched, only a simple count of the
429 // First token of a new macro argument.
430 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
432 // First closing brace in a new macro argument. Used to generate
433 // SourceRanges for InitLists.
434 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
437 // Set to true when a macro separator token is found inside a braced list.
438 // If true, the fixed argument spans multiple old arguments and ParenHints
440 bool FoundSeparatorToken = false;
441 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
444 if (I->is(tok::l_brace)) {
446 } else if (I->is(tok::r_brace)) {
448 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
450 } else if (I->is(tok::eof)) {
451 // EOF token is used to separate macro arguments
453 // Assume comma separator is actually braced list separator and change
454 // it back to a comma.
455 FoundSeparatorToken = true;
456 I->setKind(tok::comma);
458 } else { // Braces == 0
459 // Separator token still separates arguments.
462 // If the argument starts with a brace, it can't be fixed with
463 // parentheses. A different diagnostic will be given.
464 if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
466 SourceRange(ArgStartIterator->getLocation(),
467 PP.getLocForEndOfToken(ClosingBrace->getLocation())));
472 if (FoundSeparatorToken) {
473 TempToken.startToken();
474 TempToken.setKind(tok::l_paren);
475 TempToken.setLocation(ArgStartIterator->getLocation());
476 TempToken.setLength(0);
477 NewTokens.push_back(TempToken);
480 // Copy over argument tokens
481 NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
483 // Add right paren and store the paren locations in ParenHints
484 if (FoundSeparatorToken) {
485 SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
486 TempToken.startToken();
487 TempToken.setKind(tok::r_paren);
488 TempToken.setLocation(Loc);
489 TempToken.setLength(0);
490 NewTokens.push_back(TempToken);
491 ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
495 // Copy separator token
496 NewTokens.push_back(*I);
499 ArgStartIterator = I + 1;
500 FoundSeparatorToken = false;
505 return !ParenHints.empty() && InitLists.empty();
508 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
509 /// token is the '(' of the macro, this method is invoked to read all of the
510 /// actual arguments specified for the macro invocation. This returns null on
512 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
514 SourceLocation &MacroEnd) {
515 // The number of fixed arguments to parse.
516 unsigned NumFixedArgsLeft = MI->getNumArgs();
517 bool isVariadic = MI->isVariadic();
519 // Outer loop, while there are more arguments, keep reading them.
522 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
523 // an argument value in a macro could expand to ',' or '(' or ')'.
524 LexUnexpandedToken(Tok);
525 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
527 // ArgTokens - Build up a list of tokens that make up each argument. Each
528 // argument is separated by an EOF token. Use a SmallVector so we can avoid
529 // heap allocations in the common case.
530 SmallVector<Token, 64> ArgTokens;
531 bool ContainsCodeCompletionTok = false;
533 SourceLocation TooManyArgsLoc;
535 unsigned NumActuals = 0;
536 while (Tok.isNot(tok::r_paren)) {
537 if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod)))
540 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
541 "only expect argument separators here");
543 unsigned ArgTokenStart = ArgTokens.size();
544 SourceLocation ArgStartLoc = Tok.getLocation();
546 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
547 // that we already consumed the first one.
548 unsigned NumParens = 0;
551 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
552 // an argument value in a macro could expand to ',' or '(' or ')'.
553 LexUnexpandedToken(Tok);
555 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
556 if (!ContainsCodeCompletionTok) {
557 Diag(MacroName, diag::err_unterm_macro_invoc);
558 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
559 << MacroName.getIdentifierInfo();
560 // Do not lose the EOF/EOD. Return it to the client.
564 // Do not lose the EOF/EOD.
565 Token *Toks = new Token[1];
567 EnterTokenStream(Toks, 1, true, true);
570 } else if (Tok.is(tok::r_paren)) {
571 // If we found the ) token, the macro arg list is done.
572 if (NumParens-- == 0) {
573 MacroEnd = Tok.getLocation();
576 } else if (Tok.is(tok::l_paren)) {
578 } else if (Tok.is(tok::comma) && NumParens == 0 &&
579 !(Tok.getFlags() & Token::IgnoredComma)) {
580 // In Microsoft-compatibility mode, single commas from nested macro
581 // expansions should not be considered as argument separators. We test
582 // for this with the IgnoredComma token flag above.
584 // Comma ends this argument if there are more fixed arguments expected.
585 // However, if this is a variadic macro, and this is part of the
586 // variadic part, then the comma is just an argument token.
587 if (!isVariadic) break;
588 if (NumFixedArgsLeft > 1)
590 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
591 // If this is a comment token in the argument list and we're just in
592 // -C mode (not -CC mode), discard the comment.
594 } else if (Tok.getIdentifierInfo() != nullptr) {
595 // Reading macro arguments can cause macros that we are currently
596 // expanding from to be popped off the expansion stack. Doing so causes
597 // them to be reenabled for expansion. Here we record whether any
598 // identifiers we lex as macro arguments correspond to disabled macros.
599 // If so, we mark the token as noexpand. This is a subtle aspect of
601 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
602 if (!MI->isEnabled())
603 Tok.setFlag(Token::DisableExpand);
604 } else if (Tok.is(tok::code_completion)) {
605 ContainsCodeCompletionTok = true;
607 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
609 // Don't mark that we reached the code-completion point because the
610 // parser is going to handle the token and there will be another
611 // code-completion callback.
614 ArgTokens.push_back(Tok);
617 // If this was an empty argument list foo(), don't add this as an empty
619 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
622 // If this is not a variadic macro, and too many args were specified, emit
624 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
625 if (ArgTokens.size() != ArgTokenStart)
626 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
628 TooManyArgsLoc = ArgStartLoc;
631 // Empty arguments are standard in C99 and C++0x, and are supported as an
632 // extension in other modes.
633 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
634 Diag(Tok, LangOpts.CPlusPlus11 ?
635 diag::warn_cxx98_compat_empty_fnmacro_arg :
636 diag::ext_empty_fnmacro_arg);
638 // Add a marker EOF token to the end of the token list for this argument.
641 EOFTok.setKind(tok::eof);
642 EOFTok.setLocation(Tok.getLocation());
644 ArgTokens.push_back(EOFTok);
646 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
650 // Okay, we either found the r_paren. Check to see if we parsed too few
652 unsigned MinArgsExpected = MI->getNumArgs();
654 // If this is not a variadic macro, and too many args were specified, emit
656 if (!isVariadic && NumActuals > MinArgsExpected &&
657 !ContainsCodeCompletionTok) {
658 // Emit the diagnostic at the macro name in case there is a missing ).
659 // Emitting it at the , could be far away from the macro name.
660 Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
661 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
662 << MacroName.getIdentifierInfo();
664 // Commas from braced initializer lists will be treated as argument
665 // separators inside macros. Attempt to correct for this with parentheses.
666 // TODO: See if this can be generalized to angle brackets for templates
667 // inside macro arguments.
669 SmallVector<Token, 4> FixedArgTokens;
670 unsigned FixedNumArgs = 0;
671 SmallVector<SourceRange, 4> ParenHints, InitLists;
672 if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
673 ParenHints, InitLists)) {
674 if (!InitLists.empty()) {
675 DiagnosticBuilder DB =
677 diag::note_init_list_at_beginning_of_macro_argument);
678 for (const SourceRange &Range : InitLists)
683 if (FixedNumArgs != MinArgsExpected)
686 DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
687 for (const SourceRange &ParenLocation : ParenHints) {
688 DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "(");
689 DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")");
691 ArgTokens.swap(FixedArgTokens);
692 NumActuals = FixedNumArgs;
695 // See MacroArgs instance var for description of this.
696 bool isVarargsElided = false;
698 if (ContainsCodeCompletionTok) {
699 // Recover from not-fully-formed macro invocation during code-completion.
702 EOFTok.setKind(tok::eof);
703 EOFTok.setLocation(Tok.getLocation());
705 for (; NumActuals < MinArgsExpected; ++NumActuals)
706 ArgTokens.push_back(EOFTok);
709 if (NumActuals < MinArgsExpected) {
710 // There are several cases where too few arguments is ok, handle them now.
711 if (NumActuals == 0 && MinArgsExpected == 1) {
712 // #define A(X) or #define A(...) ---> A()
714 // If there is exactly one argument, and that argument is missing,
715 // then we have an empty "()" argument empty list. This is fine, even if
716 // the macro expects one argument (the argument is just empty).
717 isVarargsElided = MI->isVariadic();
718 } else if (MI->isVariadic() &&
719 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
720 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
721 // Varargs where the named vararg parameter is missing: OK as extension.
725 // If the macro contains the comma pasting extension, the diagnostic
726 // is suppressed; we know we'll get another diagnostic later.
727 if (!MI->hasCommaPasting()) {
728 Diag(Tok, diag::ext_missing_varargs_arg);
729 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
730 << MacroName.getIdentifierInfo();
733 // Remember this occurred, allowing us to elide the comma when used for
735 // #define A(x, foo...) blah(a, ## foo)
736 // #define B(x, ...) blah(a, ## __VA_ARGS__)
737 // #define C(...) blah(a, ## __VA_ARGS__)
739 isVarargsElided = true;
740 } else if (!ContainsCodeCompletionTok) {
741 // Otherwise, emit the error.
742 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
743 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
744 << MacroName.getIdentifierInfo();
748 // Add a marker EOF token to the end of the token list for this argument.
749 SourceLocation EndLoc = Tok.getLocation();
751 Tok.setKind(tok::eof);
752 Tok.setLocation(EndLoc);
754 ArgTokens.push_back(Tok);
756 // If we expect two arguments, add both as empty.
757 if (NumActuals == 0 && MinArgsExpected == 2)
758 ArgTokens.push_back(Tok);
760 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
761 !ContainsCodeCompletionTok) {
762 // Emit the diagnostic at the macro name in case there is a missing ).
763 // Emitting it at the , could be far away from the macro name.
764 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
765 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
766 << MacroName.getIdentifierInfo();
770 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
773 /// \brief Keeps macro expanded tokens for TokenLexers.
775 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
776 /// going to lex in the cache and when it finishes the tokens are removed
777 /// from the end of the cache.
778 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
779 ArrayRef<Token> tokens) {
784 size_t newIndex = MacroExpandedTokens.size();
785 bool cacheNeedsToGrow = tokens.size() >
786 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
787 MacroExpandedTokens.append(tokens.begin(), tokens.end());
789 if (cacheNeedsToGrow) {
790 // Go through all the TokenLexers whose 'Tokens' pointer points in the
791 // buffer and update the pointers to the (potential) new buffer array.
792 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
793 TokenLexer *prevLexer;
795 std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
796 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
800 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
801 return MacroExpandedTokens.data() + newIndex;
804 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
805 assert(!MacroExpandingLexersStack.empty());
806 size_t tokIndex = MacroExpandingLexersStack.back().second;
807 assert(tokIndex < MacroExpandedTokens.size());
808 // Pop the cached macro expanded tokens from the end.
809 MacroExpandedTokens.resize(tokIndex);
810 MacroExpandingLexersStack.pop_back();
813 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
814 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
815 /// the identifier tokens inserted.
816 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
818 time_t TT = time(nullptr);
819 struct tm *TM = localtime(&TT);
821 static const char * const Months[] = {
822 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
826 SmallString<32> TmpBuffer;
827 llvm::raw_svector_ostream TmpStream(TmpBuffer);
828 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
829 TM->tm_mday, TM->tm_year + 1900);
832 PP.CreateString(TmpStream.str(), TmpTok);
833 DATELoc = TmpTok.getLocation();
837 SmallString<32> TmpBuffer;
838 llvm::raw_svector_ostream TmpStream(TmpBuffer);
839 TmpStream << llvm::format("\"%02d:%02d:%02d\"",
840 TM->tm_hour, TM->tm_min, TM->tm_sec);
843 PP.CreateString(TmpStream.str(), TmpTok);
844 TIMELoc = TmpTok.getLocation();
849 /// HasFeature - Return true if we recognize and implement the feature
850 /// specified by the identifier as a standard language feature.
851 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
852 const LangOptions &LangOpts = PP.getLangOpts();
853 StringRef Feature = II->getName();
855 // Normalize the feature name, __foo__ becomes foo.
856 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
857 Feature = Feature.substr(2, Feature.size() - 4);
859 return llvm::StringSwitch<bool>(Feature)
860 .Case("address_sanitizer", LangOpts.Sanitize.Address)
861 .Case("attribute_analyzer_noreturn", true)
862 .Case("attribute_availability", true)
863 .Case("attribute_availability_with_message", true)
864 .Case("attribute_cf_returns_not_retained", true)
865 .Case("attribute_cf_returns_retained", true)
866 .Case("attribute_deprecated_with_message", true)
867 .Case("attribute_ext_vector_type", true)
868 .Case("attribute_ns_returns_not_retained", true)
869 .Case("attribute_ns_returns_retained", true)
870 .Case("attribute_ns_consumes_self", true)
871 .Case("attribute_ns_consumed", true)
872 .Case("attribute_cf_consumed", true)
873 .Case("attribute_objc_ivar_unused", true)
874 .Case("attribute_objc_method_family", true)
875 .Case("attribute_overloadable", true)
876 .Case("attribute_unavailable_with_message", true)
877 .Case("attribute_unused_on_fields", true)
878 .Case("blocks", LangOpts.Blocks)
879 .Case("c_thread_safety_attributes", true)
880 .Case("cxx_exceptions", LangOpts.CXXExceptions)
881 .Case("cxx_rtti", LangOpts.RTTI)
882 .Case("enumerator_attributes", true)
883 .Case("memory_sanitizer", LangOpts.Sanitize.Memory)
884 .Case("thread_sanitizer", LangOpts.Sanitize.Thread)
885 .Case("dataflow_sanitizer", LangOpts.Sanitize.DataFlow)
886 // Objective-C features
887 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
888 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
889 .Case("objc_arc_weak", LangOpts.ObjCARCWeak)
890 .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
891 .Case("objc_fixed_enum", LangOpts.ObjC2)
892 .Case("objc_instancetype", LangOpts.ObjC2)
893 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
894 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
895 .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword?
896 .Case("objc_protocol_qualifier_mangling", true)
897 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
898 .Case("ownership_holds", true)
899 .Case("ownership_returns", true)
900 .Case("ownership_takes", true)
901 .Case("objc_bool", true)
902 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
903 .Case("objc_array_literals", LangOpts.ObjC2)
904 .Case("objc_dictionary_literals", LangOpts.ObjC2)
905 .Case("objc_boxed_expressions", LangOpts.ObjC2)
906 .Case("arc_cf_code_audited", true)
908 .Case("c_alignas", LangOpts.C11)
909 .Case("c_atomic", LangOpts.C11)
910 .Case("c_generic_selections", LangOpts.C11)
911 .Case("c_static_assert", LangOpts.C11)
912 .Case("c_thread_local",
913 LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
915 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
916 .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
917 .Case("cxx_alignas", LangOpts.CPlusPlus11)
918 .Case("cxx_atomic", LangOpts.CPlusPlus11)
919 .Case("cxx_attributes", LangOpts.CPlusPlus11)
920 .Case("cxx_auto_type", LangOpts.CPlusPlus11)
921 .Case("cxx_constexpr", LangOpts.CPlusPlus11)
922 .Case("cxx_decltype", LangOpts.CPlusPlus11)
923 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
924 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
925 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
926 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
927 .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
928 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
929 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
930 .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
931 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
932 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
933 .Case("cxx_lambdas", LangOpts.CPlusPlus11)
934 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
935 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
936 .Case("cxx_noexcept", LangOpts.CPlusPlus11)
937 .Case("cxx_nullptr", LangOpts.CPlusPlus11)
938 .Case("cxx_override_control", LangOpts.CPlusPlus11)
939 .Case("cxx_range_for", LangOpts.CPlusPlus11)
940 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
941 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
942 .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
943 .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
944 .Case("cxx_static_assert", LangOpts.CPlusPlus11)
945 .Case("cxx_thread_local",
946 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
947 .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
948 .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
949 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
950 .Case("cxx_user_literals", LangOpts.CPlusPlus11)
951 .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
953 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
954 .Case("cxx_binary_literals", LangOpts.CPlusPlus1y)
955 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y)
956 .Case("cxx_decltype_auto", LangOpts.CPlusPlus1y)
957 .Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y)
958 .Case("cxx_init_captures", LangOpts.CPlusPlus1y)
959 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
960 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
961 .Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
963 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays)
964 //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts)
965 // FIXME: Should this be __has_feature or __has_extension?
966 //.Case("raw_invocation_type", LangOpts.CPlusPlus)
968 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
969 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
970 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
971 .Case("has_trivial_assign", LangOpts.CPlusPlus)
972 .Case("has_trivial_copy", LangOpts.CPlusPlus)
973 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
974 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
975 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
976 .Case("is_abstract", LangOpts.CPlusPlus)
977 .Case("is_base_of", LangOpts.CPlusPlus)
978 .Case("is_class", LangOpts.CPlusPlus)
979 .Case("is_constructible", LangOpts.CPlusPlus)
980 .Case("is_convertible_to", LangOpts.CPlusPlus)
981 .Case("is_empty", LangOpts.CPlusPlus)
982 .Case("is_enum", LangOpts.CPlusPlus)
983 .Case("is_final", LangOpts.CPlusPlus)
984 .Case("is_literal", LangOpts.CPlusPlus)
985 .Case("is_standard_layout", LangOpts.CPlusPlus)
986 .Case("is_pod", LangOpts.CPlusPlus)
987 .Case("is_polymorphic", LangOpts.CPlusPlus)
988 .Case("is_sealed", LangOpts.MicrosoftExt)
989 .Case("is_trivial", LangOpts.CPlusPlus)
990 .Case("is_trivially_assignable", LangOpts.CPlusPlus)
991 .Case("is_trivially_constructible", LangOpts.CPlusPlus)
992 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
993 .Case("is_union", LangOpts.CPlusPlus)
994 .Case("modules", LangOpts.Modules)
995 .Case("tls", PP.getTargetInfo().isTLSSupported())
996 .Case("underlying_type", LangOpts.CPlusPlus)
1000 /// HasExtension - Return true if we recognize and implement the feature
1001 /// specified by the identifier, either as an extension or a standard language
1003 static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
1004 if (HasFeature(PP, II))
1007 // If the use of an extension results in an error diagnostic, extensions are
1008 // effectively unavailable, so just return false here.
1009 if (PP.getDiagnostics().getExtensionHandlingBehavior() >=
1010 diag::Severity::Error)
1013 const LangOptions &LangOpts = PP.getLangOpts();
1014 StringRef Extension = II->getName();
1016 // Normalize the extension name, __foo__ becomes foo.
1017 if (Extension.startswith("__") && Extension.endswith("__") &&
1018 Extension.size() >= 4)
1019 Extension = Extension.substr(2, Extension.size() - 4);
1021 // Because we inherit the feature list from HasFeature, this string switch
1022 // must be less restrictive than HasFeature's.
1023 return llvm::StringSwitch<bool>(Extension)
1024 // C11 features supported by other languages as extensions.
1025 .Case("c_alignas", true)
1026 .Case("c_atomic", true)
1027 .Case("c_generic_selections", true)
1028 .Case("c_static_assert", true)
1029 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
1030 // C++11 features supported by other languages as extensions.
1031 .Case("cxx_atomic", LangOpts.CPlusPlus)
1032 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
1033 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
1034 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
1035 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
1036 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
1037 .Case("cxx_override_control", LangOpts.CPlusPlus)
1038 .Case("cxx_range_for", LangOpts.CPlusPlus)
1039 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
1040 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
1041 // C++1y features supported by other languages as extensions.
1042 .Case("cxx_binary_literals", true)
1043 .Case("cxx_init_captures", LangOpts.CPlusPlus11)
1044 .Case("cxx_variable_templates", LangOpts.CPlusPlus)
1048 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1049 /// or '__has_include_next("path")' expression.
1050 /// Returns true if successful.
1051 static bool EvaluateHasIncludeCommon(Token &Tok,
1052 IdentifierInfo *II, Preprocessor &PP,
1053 const DirectoryLookup *LookupFrom) {
1054 // Save the location of the current token. If a '(' is later found, use
1055 // that location. If not, use the end of this location instead.
1056 SourceLocation LParenLoc = Tok.getLocation();
1058 // These expressions are only allowed within a preprocessor directive.
1059 if (!PP.isParsingIfOrElifDirective()) {
1060 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
1065 PP.LexNonComment(Tok);
1067 // Ensure we have a '('.
1068 if (Tok.isNot(tok::l_paren)) {
1069 // No '(', use end of last token.
1070 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
1071 PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren;
1072 // If the next token looks like a filename or the start of one,
1073 // assume it is and process it as such.
1074 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
1078 // Save '(' location for possible missing ')' message.
1079 LParenLoc = Tok.getLocation();
1081 if (PP.getCurrentLexer()) {
1082 // Get the file name.
1083 PP.getCurrentLexer()->LexIncludeFilename(Tok);
1085 // We're in a macro, so we can't use LexIncludeFilename; just
1086 // grab the next token.
1091 // Reserve a buffer to get the spelling.
1092 SmallString<128> FilenameBuffer;
1094 SourceLocation EndLoc;
1096 switch (Tok.getKind()) {
1098 // If the token kind is EOD, the error has already been diagnosed.
1101 case tok::angle_string_literal:
1102 case tok::string_literal: {
1103 bool Invalid = false;
1104 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
1111 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1112 // case, glue the tokens together into FilenameBuffer and interpret those.
1113 FilenameBuffer.push_back('<');
1114 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
1115 // Let the caller know a <eod> was found by changing the Token kind.
1116 Tok.setKind(tok::eod);
1117 return false; // Found <eod> but no ">"? Diagnostic already emitted.
1119 Filename = FilenameBuffer.str();
1122 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1126 SourceLocation FilenameLoc = Tok.getLocation();
1129 PP.LexNonComment(Tok);
1131 // Ensure we have a trailing ).
1132 if (Tok.isNot(tok::r_paren)) {
1133 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after)
1134 << II << tok::r_paren;
1135 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1139 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1140 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1142 if (Filename.empty())
1145 // Search include directories.
1146 const DirectoryLookup *CurDir;
1147 const FileEntry *File =
1148 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir,
1149 nullptr, nullptr, nullptr);
1151 // Get the result value. A result of true means the file exists.
1152 return File != nullptr;
1155 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
1156 /// Returns true if successful.
1157 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1159 return EvaluateHasIncludeCommon(Tok, II, PP, nullptr);
1162 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1163 /// Returns true if successful.
1164 static bool EvaluateHasIncludeNext(Token &Tok,
1165 IdentifierInfo *II, Preprocessor &PP) {
1166 // __has_include_next is like __has_include, except that we start
1167 // searching after the current found directory. If we can't do this,
1168 // issue a diagnostic.
1169 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1170 if (PP.isInPrimaryFile()) {
1172 PP.Diag(Tok, diag::pp_include_next_in_primary);
1173 } else if (!Lookup) {
1174 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1176 // Start looking up in the next directory.
1180 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1183 /// \brief Process __building_module(identifier) expression.
1184 /// \returns true if we are building the named module, false otherwise.
1185 static bool EvaluateBuildingModule(Token &Tok,
1186 IdentifierInfo *II, Preprocessor &PP) {
1188 PP.LexNonComment(Tok);
1190 // Ensure we have a '('.
1191 if (Tok.isNot(tok::l_paren)) {
1192 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1197 // Save '(' location for possible missing ')' message.
1198 SourceLocation LParenLoc = Tok.getLocation();
1200 // Get the module name.
1201 PP.LexNonComment(Tok);
1203 // Ensure that we have an identifier.
1204 if (Tok.isNot(tok::identifier)) {
1205 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1210 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1213 PP.LexNonComment(Tok);
1215 // Ensure we have a trailing ).
1216 if (Tok.isNot(tok::r_paren)) {
1217 PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II
1219 PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1226 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1227 /// as a builtin macro, handle it and return the next token as 'Tok'.
1228 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1229 // Figure out which token this is.
1230 IdentifierInfo *II = Tok.getIdentifierInfo();
1231 assert(II && "Can't be a macro without id info!");
1233 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1234 // invoke the pragma handler, then lex the token after it.
1235 if (II == Ident_Pragma)
1236 return Handle_Pragma(Tok);
1237 else if (II == Ident__pragma) // in non-MS mode this is null
1238 return HandleMicrosoft__pragma(Tok);
1240 ++NumBuiltinMacroExpanded;
1242 SmallString<128> TmpBuffer;
1243 llvm::raw_svector_ostream OS(TmpBuffer);
1245 // Set up the return result.
1246 Tok.setIdentifierInfo(nullptr);
1247 Tok.clearFlag(Token::NeedsCleaning);
1249 if (II == Ident__LINE__) {
1250 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1251 // source file) of the current source line (an integer constant)". This can
1252 // be affected by #line.
1253 SourceLocation Loc = Tok.getLocation();
1255 // Advance to the location of the first _, this might not be the first byte
1256 // of the token if it starts with an escaped newline.
1257 Loc = AdvanceToTokenCharacter(Loc, 0);
1259 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1260 // a macro expansion. This doesn't matter for object-like macros, but
1261 // can matter for a function-like macro that expands to contain __LINE__.
1262 // Skip down through expansion points until we find a file loc for the
1263 // end of the expansion history.
1264 Loc = SourceMgr.getExpansionRange(Loc).second;
1265 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1267 // __LINE__ expands to a simple numeric value.
1268 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1269 Tok.setKind(tok::numeric_constant);
1270 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1271 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1272 // character string literal)". This can be affected by #line.
1273 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1275 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1276 // #include stack instead of the current file.
1277 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1278 SourceLocation NextLoc = PLoc.getIncludeLoc();
1279 while (NextLoc.isValid()) {
1280 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1281 if (PLoc.isInvalid())
1284 NextLoc = PLoc.getIncludeLoc();
1288 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1289 SmallString<128> FN;
1290 if (PLoc.isValid()) {
1291 FN += PLoc.getFilename();
1292 Lexer::Stringify(FN);
1293 OS << '"' << FN.str() << '"';
1295 Tok.setKind(tok::string_literal);
1296 } else if (II == Ident__DATE__) {
1297 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1298 if (!DATELoc.isValid())
1299 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1300 Tok.setKind(tok::string_literal);
1301 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1302 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1306 } else if (II == Ident__TIME__) {
1307 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1308 if (!TIMELoc.isValid())
1309 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1310 Tok.setKind(tok::string_literal);
1311 Tok.setLength(strlen("\"hh:mm:ss\""));
1312 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1316 } else if (II == Ident__INCLUDE_LEVEL__) {
1317 // Compute the presumed include depth of this token. This can be affected
1318 // by GNU line markers.
1321 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1322 if (PLoc.isValid()) {
1323 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1324 for (; PLoc.isValid(); ++Depth)
1325 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1328 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1330 Tok.setKind(tok::numeric_constant);
1331 } else if (II == Ident__TIMESTAMP__) {
1332 Diag(Tok.getLocation(), diag::warn_pp_date_time);
1333 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1334 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1336 // Get the file that we are lexing out of. If we're currently lexing from
1337 // a macro, dig into the include stack.
1338 const FileEntry *CurFile = nullptr;
1339 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1342 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1346 time_t TT = CurFile->getModificationTime();
1347 struct tm *TM = localtime(&TT);
1348 Result = asctime(TM);
1350 Result = "??? ??? ?? ??:??:?? ????\n";
1352 // Surround the string with " and strip the trailing newline.
1353 OS << '"' << StringRef(Result).drop_back() << '"';
1354 Tok.setKind(tok::string_literal);
1355 } else if (II == Ident__COUNTER__) {
1356 // __COUNTER__ expands to a simple numeric value.
1357 OS << CounterValue++;
1358 Tok.setKind(tok::numeric_constant);
1359 } else if (II == Ident__has_feature ||
1360 II == Ident__has_extension ||
1361 II == Ident__has_builtin ||
1362 II == Ident__is_identifier ||
1363 II == Ident__has_attribute) {
1364 // The argument to these builtins should be a parenthesized identifier.
1365 SourceLocation StartLoc = Tok.getLocation();
1367 bool IsValid = false;
1368 IdentifierInfo *FeatureII = nullptr;
1371 LexUnexpandedToken(Tok);
1372 if (Tok.is(tok::l_paren)) {
1373 // Read the identifier
1374 LexUnexpandedToken(Tok);
1375 if ((FeatureII = Tok.getIdentifierInfo())) {
1377 LexUnexpandedToken(Tok);
1378 if (Tok.is(tok::r_paren))
1385 Diag(StartLoc, diag::err_feature_check_malformed);
1386 else if (II == Ident__is_identifier)
1387 Value = FeatureII->getTokenID() == tok::identifier;
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(AttrSyntax::Generic, nullptr, FeatureII,
1393 getTargetInfo().getTriple(), getLangOpts());
1394 else if (II == Ident__has_extension)
1395 Value = HasExtension(*this, FeatureII);
1397 assert(II == Ident__has_feature && "Must be feature check");
1398 Value = HasFeature(*this, FeatureII);
1403 Tok.setKind(tok::numeric_constant);
1404 } else if (II == Ident__has_include ||
1405 II == Ident__has_include_next) {
1406 // The argument to these two builtins should be a parenthesized
1407 // file name string literal using angle brackets (<>) or
1408 // double-quotes ("").
1410 if (II == Ident__has_include)
1411 Value = EvaluateHasInclude(Tok, II, *this);
1413 Value = EvaluateHasIncludeNext(Tok, II, *this);
1415 if (Tok.is(tok::r_paren))
1416 Tok.setKind(tok::numeric_constant);
1417 } else if (II == Ident__has_warning) {
1418 // The argument should be a parenthesized string literal.
1419 // The argument to these builtins should be a parenthesized identifier.
1420 SourceLocation StartLoc = Tok.getLocation();
1421 bool IsValid = false;
1424 LexUnexpandedToken(Tok);
1426 if (Tok.isNot(tok::l_paren)) {
1427 Diag(StartLoc, diag::err_warning_check_malformed);
1431 LexUnexpandedToken(Tok);
1432 std::string WarningName;
1433 SourceLocation StrStartLoc = Tok.getLocation();
1434 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1435 /*MacroExpansion=*/false)) {
1436 // Eat tokens until ')'.
1437 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
1438 Tok.isNot(tok::eof))
1439 LexUnexpandedToken(Tok);
1443 // Is the end a ')'?
1444 if (!(IsValid = Tok.is(tok::r_paren))) {
1445 Diag(StartLoc, diag::err_warning_check_malformed);
1449 // FIXME: Should we accept "-R..." flags here, or should that be handled
1450 // by a separate __has_remark?
1451 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1452 WarningName[1] != 'W') {
1453 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
1457 // Finally, check if the warning flags maps to a diagnostic group.
1458 // We construct a SmallVector here to talk to getDiagnosticIDs().
1459 // Although we don't use the result, this isn't a hot path, and not
1460 // worth special casing.
1461 SmallVector<diag::kind, 10> Diags;
1462 Value = !getDiagnostics().getDiagnosticIDs()->
1463 getDiagnosticsInGroup(diag::Flavor::WarningOrError,
1464 WarningName.substr(2), Diags);
1469 Tok.setKind(tok::numeric_constant);
1470 } 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());
1482 } else if (II == Ident__identifier) {
1483 SourceLocation Loc = Tok.getLocation();
1485 // We're expecting '__identifier' '(' identifier ')'. Try to recover
1486 // if the parens are missing.
1488 if (Tok.isNot(tok::l_paren)) {
1489 // No '(', use end of last token.
1490 Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after)
1491 << II << tok::l_paren;
1492 // If the next token isn't valid as our argument, we can't recover.
1493 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1494 Tok.setKind(tok::identifier);
1498 SourceLocation LParenLoc = Tok.getLocation();
1501 if (!Tok.isAnnotation() && Tok.getIdentifierInfo())
1502 Tok.setKind(tok::identifier);
1504 Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier)
1506 // Don't walk past anything that's not a real token.
1507 if (Tok.is(tok::eof) || Tok.is(tok::eod) || Tok.isAnnotation())
1511 // Discard the ')', preserving 'Tok' as our result.
1513 LexNonComment(RParen);
1514 if (RParen.isNot(tok::r_paren)) {
1515 Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after)
1516 << Tok.getKind() << tok::r_paren;
1517 Diag(LParenLoc, diag::note_matching) << tok::l_paren;
1521 llvm_unreachable("Unknown identifier!");
1523 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1526 void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1527 // If the 'used' status changed, and the macro requires 'unused' warning,
1528 // remove its SourceLocation from the warn-for-unused-macro locations.
1529 if (MI->isWarnIfUnused() && !MI->isUsed())
1530 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1531 MI->setIsUsed(true);