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 expasion for the
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/Preprocessor.h"
16 #include "clang/Lex/MacroArgs.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/MacroInfo.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/Format.h"
30 #include "llvm/Support/raw_ostream.h"
33 using namespace clang;
36 Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
37 assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
39 macro_iterator Pos = Macros.find(II);
40 assert(Pos != Macros.end() && "Identifier macro info is missing!");
44 void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
45 assert(MD && "MacroDirective should be non-zero!");
46 assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
48 MacroDirective *&StoredMD = Macros[II];
49 MD->setPrevious(StoredMD);
51 II->setHasMacroDefinition(MD->isDefined());
52 bool isImportedMacro = isa<DefMacroDirective>(MD) &&
53 cast<DefMacroDirective>(MD)->isImported();
54 if (II->isFromAST() && !isImportedMacro)
55 II->setChangedSinceDeserialization();
58 void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
61 MacroDirective *&StoredMD = Macros[II];
63 "the macro history was modified before initializing it from a pch");
65 // Setup the identifier as having associated macro history.
66 II->setHasMacroDefinition(true);
68 II->setHasMacroDefinition(false);
71 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
72 /// table and mark it as a builtin macro to be expanded.
73 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
74 // Get the identifier.
75 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
77 // Mark it as being a macro that is builtin.
78 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
79 MI->setIsBuiltinMacro();
80 PP.appendDefMacroDirective(Id, MI);
85 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
87 void 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");
96 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
97 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
98 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
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");
110 if (LangOpts.Modules) {
111 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
114 if (!LangOpts.CurrentModule.empty())
115 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
119 Ident__building_module = 0;
123 // Microsoft Extensions.
124 if (LangOpts.MicrosoftExt)
125 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
130 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
131 /// in its expansion, currently expands to that token literally.
132 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
133 const IdentifierInfo *MacroIdent,
135 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
137 // If the token isn't an identifier, it's always literally expanded.
138 if (II == 0) return true;
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);
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.
152 // If this is an object-like macro invocation, it is safe to trivially expand
154 if (MI->isObjectLike()) return true;
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();
161 return false; // Identifier is a macro argument.
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.
170 bool Preprocessor::isNextPPTokenLParen() {
171 // Do some quick tests for rejection cases.
174 Val = CurLexer->isNextPPTokenLParen();
175 else if (CurPTHLexer)
176 Val = CurPTHLexer->isNextPPTokenLParen();
178 Val = CurTokenLexer->isNextTokenLParen();
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
186 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
187 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
189 Val = Entry.TheLexer->isNextPPTokenLParen();
190 else if (Entry.ThePTHLexer)
191 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
193 Val = Entry.TheTokenLexer->isNextTokenLParen();
198 // Ran off the end of a source file?
199 if (Entry.ThePPLexer)
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.
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'.
212 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
213 MacroDirective *MD) {
214 MacroDirective::DefInfo Def = MD->getDefinition();
215 assert(Def.isValid());
216 MacroInfo *MI = Def.getMacroInfo();
218 // 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();
223 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
224 if (MI->isBuiltinMacro()) {
225 if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
226 Identifier.getLocation(),/*Args=*/0);
227 ExpandBuiltinMacro(Identifier);
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
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();
240 // If this is a function-like macro, read the arguments.
241 if (MI->isFunctionLike()) {
242 // 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.
246 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
248 // Finished parsing args.
251 // If there was an error parsing the arguments, bail out.
252 if (Args == 0) return true;
254 ++NumFnMacroExpanded;
259 // Notice that this macro has been used.
262 // Remember where the token is expanded.
263 SourceLocation ExpandLoc = Identifier.getLocation();
264 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
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(
273 MacroExpandsInfo(Identifier, MD, ExpansionRange));
275 Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args);
276 if (!DelayedMacroExpandsCallbacks.empty()) {
277 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
278 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
279 // FIXME: We lose macro args info with delayed callback.
280 Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, /*Args=*/0);
282 DelayedMacroExpandsCallbacks.clear();
287 // If the macro definition is ambiguous, complain.
288 if (Def.getDirective()->isAmbiguous()) {
289 Diag(Identifier, diag::warn_pp_ambiguous_macro)
290 << Identifier.getIdentifierInfo();
291 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
292 << Identifier.getIdentifierInfo();
293 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)
299 << Identifier.getIdentifierInfo();
304 // If we started lexing a macro, enter the macro expansion body.
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);
312 // Propagate whitespace info as if we had pushed, then popped,
314 Identifier.setFlag(Token::LeadingEmptyMacro);
315 PropagateLineStartLeadingSpaceInfo(Identifier);
316 ++NumFastMacroExpanded;
318 } else if (MI->getNumTokens() == 1 &&
319 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
321 // Otherwise, if this macro expands into a single trivially-expanded
322 // token: expand it now. This handles common cases like
325 // No need for arg info.
326 if (Args) Args->destroy(*this);
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();
333 // Replace the result token.
334 Identifier = MI->getReplacementToken(0);
336 // Restore the StartOfLine/LeadingSpace markers.
337 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
338 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
340 // Update the tokens location to include both its expansion and physical
343 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
344 ExpansionEnd,Identifier.getLength());
345 Identifier.setLocation(Loc);
347 // If this is a disabled macro or #define X X, we must mark the result as
349 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
350 if (MacroInfo *NewMI = getMacroInfo(NewII))
351 if (!NewMI->isEnabled() || NewMI == MI) {
352 Identifier.setFlag(Token::DisableExpand);
353 // Don't warn for "#define X X" like "#define bool bool" from
355 if (NewMI != MI || MI->isFunctionLike())
356 Diag(Identifier, diag::pp_disabled_macro_expansion);
360 // Since this is not an identifier token, it can't be macro expanded, so
362 ++NumFastMacroExpanded;
366 // Start expanding the macro.
367 EnterMacro(Identifier, ExpansionEnd, MI, Args);
376 /// CheckMatchedBrackets - Returns true if the braces and parentheses in the
377 /// token vector are properly nested.
378 static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
379 SmallVector<Bracket, 8> Brackets;
380 for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
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)
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)
397 if (!Brackets.empty())
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
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.
409 static bool GenerateNewArgTokens(Preprocessor &PP,
410 SmallVectorImpl<Token> &OldTokens,
411 SmallVectorImpl<Token> &NewTokens,
413 SmallVectorImpl<SourceRange> &ParenHints,
414 SmallVectorImpl<SourceRange> &InitLists) {
415 if (!CheckMatchedBrackets(OldTokens))
418 // Once it is known that the brackets are matched, only a simple count of the
422 // First token of a new macro argument.
423 SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
425 // First closing brace in a new macro argument. Used to generate
426 // SourceRanges for InitLists.
427 SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
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
433 bool FoundSeparatorToken = false;
434 for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
437 if (I->is(tok::l_brace)) {
439 } else if (I->is(tok::r_brace)) {
441 if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
443 } else if (I->is(tok::eof)) {
444 // EOF token is used to separate macro arguments
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);
451 } else { // Braces == 0
452 // Separator token still separates arguments.
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)) {
459 SourceRange(ArgStartIterator->getLocation(),
460 PP.getLocForEndOfToken(ClosingBrace->getLocation())));
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);
473 // Copy over argument tokens
474 NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
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(),
488 // Copy separator token
489 NewTokens.push_back(*I);
492 ArgStartIterator = I + 1;
493 FoundSeparatorToken = false;
498 return !ParenHints.empty() && InitLists.empty();
501 /// 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
505 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
507 SourceLocation &MacroEnd) {
508 // The number of fixed arguments to parse.
509 unsigned NumFixedArgsLeft = MI->getNumArgs();
510 bool isVariadic = MI->isVariadic();
512 // Outer loop, while there are more arguments, keep reading them.
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?");
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;
524 bool ContainsCodeCompletionTok = false;
526 SourceLocation TooManyArgsLoc;
528 unsigned NumActuals = 0;
529 while (Tok.isNot(tok::r_paren)) {
530 if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod)))
533 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
534 "only expect argument separators here");
536 unsigned ArgTokenStart = ArgTokens.size();
537 SourceLocation ArgStartLoc = Tok.getLocation();
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;
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);
548 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
549 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.
557 // Do not lose the EOF/EOD.
558 Token *Toks = new Token[1];
560 EnterTokenStream(Toks, 1, true, true);
563 } 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();
569 } else if (Tok.is(tok::l_paren)) {
571 } 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.
577 // 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)
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.
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
594 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
595 if (!MI->isEnabled())
596 Tok.setFlag(Token::DisableExpand);
597 } else if (Tok.is(tok::code_completion)) {
598 ContainsCodeCompletionTok = true;
600 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
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.
607 ArgTokens.push_back(Tok);
610 // If this was an empty argument list foo(), don't add this as an empty
612 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
615 // If this is not a variadic macro, and too many args were specified, emit
617 if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
618 if (ArgTokens.size() != ArgTokenStart)
619 TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
621 TooManyArgsLoc = ArgStartLoc;
624 // Empty arguments are standard in C99 and C++0x, and are supported as an
625 // extension in other modes.
626 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
627 Diag(Tok, LangOpts.CPlusPlus11 ?
628 diag::warn_cxx98_compat_empty_fnmacro_arg :
629 diag::ext_empty_fnmacro_arg);
631 // Add a marker EOF token to the end of the token list for this argument.
634 EOFTok.setKind(tok::eof);
635 EOFTok.setLocation(Tok.getLocation());
637 ArgTokens.push_back(EOFTok);
639 if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
643 // Okay, we either found the r_paren. Check to see if we parsed too few
645 unsigned MinArgsExpected = MI->getNumArgs();
647 // If this is not a variadic macro, and too many args were specified, emit
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();
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.
662 SmallVector<Token, 4> FixedArgTokens;
663 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 =
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())
681 if (FixedNumArgs != MinArgsExpected)
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())
690 DB << FixItHint::CreateInsertion(ParenLocation->getBegin(), "(");
691 if (DB.hasMaxFixItHints())
693 DB << FixItHint::CreateInsertion(ParenLocation->getEnd(), ")");
695 ArgTokens.swap(FixedArgTokens);
696 NumActuals = FixedNumArgs;
699 // See MacroArgs instance var for description of this.
700 bool isVarargsElided = false;
702 if (ContainsCodeCompletionTok) {
703 // Recover from not-fully-formed macro invocation during code-completion.
706 EOFTok.setKind(tok::eof);
707 EOFTok.setLocation(Tok.getLocation());
709 for (; NumActuals < MinArgsExpected; ++NumActuals)
710 ArgTokens.push_back(EOFTok);
713 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()
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.
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();
737 // Remember this occurred, allowing us to elide the comma when used for
739 // #define A(x, foo...) blah(a, ## foo)
740 // #define B(x, ...) blah(a, ## __VA_ARGS__)
741 // #define C(...) blah(a, ## __VA_ARGS__)
743 isVarargsElided = true;
744 } else if (!ContainsCodeCompletionTok) {
745 // Otherwise, emit the error.
746 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
747 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
748 << MacroName.getIdentifierInfo();
752 // Add a marker EOF token to the end of the token list for this argument.
753 SourceLocation EndLoc = Tok.getLocation();
755 Tok.setKind(tok::eof);
756 Tok.setLocation(EndLoc);
758 ArgTokens.push_back(Tok);
760 // If we expect two arguments, add both as empty.
761 if (NumActuals == 0 && MinArgsExpected == 2)
762 ArgTokens.push_back(Tok);
764 } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
765 !ContainsCodeCompletionTok) {
766 // 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);
769 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
770 << MacroName.getIdentifierInfo();
774 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
777 /// \brief Keeps macro expanded tokens for TokenLexers.
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.
782 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
783 ArrayRef<Token> tokens) {
788 size_t newIndex = MacroExpandedTokens.size();
789 bool cacheNeedsToGrow = tokens.size() >
790 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
791 MacroExpandedTokens.append(tokens.begin(), tokens.end());
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;
799 llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
800 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
804 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
805 return MacroExpandedTokens.data() + newIndex;
808 void 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();
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.
820 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
823 struct tm *TM = localtime(&TT);
825 static const char * const Months[] = {
826 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
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);
836 PP.CreateString(TmpStream.str(), TmpTok);
837 DATELoc = TmpTok.getLocation();
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);
847 PP.CreateString(TmpStream.str(), TmpTok);
848 TIMELoc = TmpTok.getLocation();
853 /// HasFeature - Return true if we recognize and implement the feature
854 /// specified by the identifier as a standard language feature.
855 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
856 const LangOptions &LangOpts = PP.getLangOpts();
857 StringRef Feature = II->getName();
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);
863 return llvm::StringSwitch<bool>(Feature)
864 .Case("address_sanitizer", LangOpts.Sanitize.Address)
865 .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)
883 .Case("c_thread_safety_attributes", true)
884 .Case("cxx_exceptions", LangOpts.Exceptions)
885 .Case("cxx_rtti", LangOpts.RTTI)
886 .Case("enumerator_attributes", true)
887 .Case("memory_sanitizer", LangOpts.Sanitize.Memory)
888 .Case("thread_sanitizer", LangOpts.Sanitize.Thread)
889 .Case("dataflow_sanitizer", LangOpts.Sanitize.DataFlow)
890 // 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())
899 .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword?
900 .Case("objc_protocol_qualifier_mangling", true)
901 .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)
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)
916 .Case("c_thread_local",
917 LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
919 .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)
935 .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
936 .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)
949 .Case("cxx_thread_local",
950 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
951 .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)
957 .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
958 .Case("cxx_binary_literals", LangOpts.CPlusPlus1y)
959 .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y)
960 //.Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y)
961 .Case("cxx_init_captures", LangOpts.CPlusPlus1y)
962 .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
963 .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
964 //.Case("cxx_runtime_arrays", LangOpts.CPlusPlus1y)
965 .Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
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)
979 .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)
986 .Case("is_sealed", LangOpts.MicrosoftExt)
987 .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)
998 /// HasExtension - Return true if we recognize and implement the feature
999 /// specified by the identifier, either as an extension or a standard language
1001 static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
1002 if (HasFeature(PP, II))
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)
1011 const LangOptions &LangOpts = PP.getLangOpts();
1012 StringRef Extension = II->getName();
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);
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)
1027 .Case("c_thread_local", PP.getTargetInfo().isTLSSupported())
1028 // C++11 features supported by other languages as extensions.
1029 .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)
1039 // C++1y features supported by other languages as extensions.
1040 .Case("cxx_binary_literals", true)
1041 .Case("cxx_init_captures", LangOpts.CPlusPlus11)
1042 .Case("cxx_variable_templates", true)
1046 /// HasAttribute - Return true if we recognize and implement the attribute
1047 /// specified by the given identifier.
1048 static 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);
1054 // FIXME: Do we need to handle namespaces here?
1055 return llvm::StringSwitch<bool>(Name)
1056 #include "clang/Lex/AttrSpellings.inc"
1060 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
1061 /// or '__has_include_next("path")' expression.
1062 /// Returns true if successful.
1063 static bool EvaluateHasIncludeCommon(Token &Tok,
1064 IdentifierInfo *II, Preprocessor &PP,
1065 const DirectoryLookup *LookupFrom) {
1066 // Save the location of the current token. If a '(' is later found, use
1067 // that location. If not, use the end of this location instead.
1068 SourceLocation LParenLoc = Tok.getLocation();
1070 // These expressions are only allowed within a preprocessor directive.
1071 if (!PP.isParsingIfOrElifDirective()) {
1072 PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
1077 PP.LexNonComment(Tok);
1079 // Ensure we have a '('.
1080 if (Tok.isNot(tok::l_paren)) {
1081 // 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) &&
1090 // Save '(' location for possible missing ')' message.
1091 LParenLoc = Tok.getLocation();
1093 if (PP.getCurrentLexer()) {
1094 // Get the file name.
1095 PP.getCurrentLexer()->LexIncludeFilename(Tok);
1097 // We're in a macro, so we can't use LexIncludeFilename; just
1098 // grab the next token.
1103 // Reserve a buffer to get the spelling.
1104 SmallString<128> FilenameBuffer;
1106 SourceLocation EndLoc;
1108 switch (Tok.getKind()) {
1110 // If the token kind is EOD, the error has already been diagnosed.
1113 case tok::angle_string_literal:
1114 case tok::string_literal: {
1115 bool Invalid = false;
1116 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
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('<');
1126 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
1127 // Let the caller know a <eod> was found by changing the Token kind.
1128 Tok.setKind(tok::eod);
1129 return false; // Found <eod> but no ">"? Diagnostic already emitted.
1131 Filename = FilenameBuffer.str();
1134 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
1138 SourceLocation FilenameLoc = Tok.getLocation();
1141 PP.LexNonComment(Tok);
1143 // Ensure we have a trailing ).
1144 if (Tok.isNot(tok::r_paren)) {
1145 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen)
1147 PP.Diag(LParenLoc, diag::note_matching) << "(";
1151 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1152 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1154 if (Filename.empty())
1157 // Search include directories.
1158 const DirectoryLookup *CurDir;
1159 const FileEntry *File =
1160 PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL,
1163 // Get the result value. A result of true means the file exists.
1167 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
1168 /// Returns true if successful.
1169 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1171 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
1174 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1175 /// Returns true if successful.
1176 static 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()) {
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);
1188 // Start looking up in the next directory.
1192 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1195 /// \brief Process __building_module(identifier) expression.
1196 /// \returns true if we are building the named module, false otherwise.
1197 static bool EvaluateBuildingModule(Token &Tok,
1198 IdentifierInfo *II, Preprocessor &PP) {
1200 PP.LexNonComment(Tok);
1202 // Ensure we have a '('.
1203 if (Tok.isNot(tok::l_paren)) {
1204 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
1208 // Save '(' location for possible missing ')' message.
1209 SourceLocation LParenLoc = Tok.getLocation();
1211 // Get the module name.
1212 PP.LexNonComment(Tok);
1214 // Ensure that we have an identifier.
1215 if (Tok.isNot(tok::identifier)) {
1216 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1221 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1224 PP.LexNonComment(Tok);
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) << "(";
1236 /// 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'.
1238 void 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!");
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);
1250 ++NumBuiltinMacroExpanded;
1252 SmallString<128> TmpBuffer;
1253 llvm::raw_svector_ostream OS(TmpBuffer);
1255 // Set up the return result.
1256 Tok.setIdentifierInfo(0);
1257 Tok.clearFlag(Token::NeedsCleaning);
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();
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);
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);
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());
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())
1294 NextLoc = PLoc.getIncludeLoc();
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() << '"';
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(),
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(),
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.
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());
1336 // __INCLUDE_LEVEL__ expands to a simple numeric value.
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.
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();
1349 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1353 time_t TT = CurFile->getModificationTime();
1354 struct tm *TM = localtime(&TT);
1355 Result = asctime(TM);
1357 Result = "??? ??? ?? ??:??:?? ????\n";
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();
1373 bool IsValid = false;
1374 IdentifierInfo *FeatureII = 0;
1377 LexUnexpandedToken(Tok);
1378 if (Tok.is(tok::l_paren)) {
1379 // Read the identifier
1380 LexUnexpandedToken(Tok);
1381 if ((FeatureII = Tok.getIdentifierInfo())) {
1383 LexUnexpandedToken(Tok);
1384 if (Tok.is(tok::r_paren))
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);
1400 assert(II == Ident__has_feature && "Must be feature check");
1401 Value = HasFeature(*this, FeatureII);
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 ("").
1413 if (II == Ident__has_include)
1414 Value = EvaluateHasInclude(Tok, II, *this);
1416 Value = EvaluateHasIncludeNext(Tok, II, *this);
1418 if (Tok.is(tok::r_paren))
1419 Tok.setKind(tok::numeric_constant);
1420 } 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;
1427 LexUnexpandedToken(Tok);
1429 if (Tok.isNot(tok::l_paren)) {
1430 Diag(StartLoc, diag::err_warning_check_malformed);
1434 LexUnexpandedToken(Tok);
1435 std::string WarningName;
1436 SourceLocation StrStartLoc = Tok.getLocation();
1437 if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
1438 /*MacroExpansion=*/false)) {
1439 // Eat tokens until ')'.
1440 while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
1441 Tok.isNot(tok::eof))
1442 LexUnexpandedToken(Tok);
1446 // Is the end a ')'?
1447 if (!(IsValid = Tok.is(tok::r_paren))) {
1448 Diag(StartLoc, diag::err_warning_check_malformed);
1452 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1453 WarningName[1] != 'W') {
1454 Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
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.
1462 SmallVector<diag::kind, 10> Diags;
1463 Value = !getDiagnostics().getDiagnosticIDs()->
1464 getDiagnosticsInGroup(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());
1483 llvm_unreachable("Unknown identifier!");
1485 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1488 void 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);