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 "MacroArgs.h"
17 #include "clang/Lex/MacroInfo.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/CodeCompletionHandler.h"
23 #include "clang/Lex/ExternalPreprocessorSource.h"
24 #include "clang/Lex/LiteralSupport.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/Format.h"
33 using namespace clang;
35 MacroInfo *Preprocessor::getMacroInfoHistory(IdentifierInfo *II) const {
36 assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
38 macro_iterator Pos = Macros.find(II);
39 assert(Pos != Macros.end() && "Identifier macro info is missing!");
43 /// setMacroInfo - Specify a macro for this identifier.
45 void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
46 assert(MI && "MacroInfo should be non-zero!");
47 assert(MI->getUndefLoc().isInvalid() &&
48 "Undefined macros cannot be registered");
50 MacroInfo *&StoredMI = Macros[II];
51 MI->setPreviousDefinition(StoredMI);
53 II->setHasMacroDefinition(MI->getUndefLoc().isInvalid());
55 II->setChangedSinceDeserialization();
58 void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI,
60 assert(MI && "Missing macro?");
61 assert(MI->isFromAST() && "Macro is not from an AST?");
62 assert(!MI->getPreviousDefinition() && "Macro already in chain?");
64 MacroInfo *&StoredMI = Macros[II];
66 // Easy case: this is the first macro definition for this macro.
71 II->setHasMacroDefinition(true);
75 // If this macro is a definition and this identifier has been neither
76 // defined nor undef'd in the current translation unit, add this macro
77 // to the end of the chain of definitions.
78 if (MI->isDefined() && StoredMI->isFromAST()) {
79 // Simple case: if this is the first actual definition, just put it at
81 if (!StoredMI->isDefined()) {
82 MI->setPreviousDefinition(StoredMI);
85 II->setHasMacroDefinition(true);
89 // Find the end of the definition chain.
91 MacroInfo *PrevPrev = StoredMI;
92 bool Ambiguous = StoredMI->isAmbiguous();
93 bool MatchedOther = false;
97 // If the macros are not identical, we have an ambiguity.
98 if (!Prev->isIdenticalTo(*MI, *this)) {
101 StoredMI->setAmbiguous(true);
106 } while ((PrevPrev = Prev->getPreviousDefinition()) &&
107 PrevPrev->isDefined());
109 // If there are ambiguous definitions, and we didn't match any other
110 // definition, then mark us as ambiguous.
111 if (Ambiguous && !MatchedOther)
112 MI->setAmbiguous(true);
114 // Wire this macro information into the chain.
115 MI->setPreviousDefinition(Prev->getPreviousDefinition());
116 Prev->setPreviousDefinition(MI);
120 // The macro is not a definition; put it at the end of the list.
121 MacroInfo *Prev = Hint? Hint : StoredMI;
122 while (Prev->getPreviousDefinition())
123 Prev = Prev->getPreviousDefinition();
124 Prev->setPreviousDefinition(MI);
127 void Preprocessor::makeLoadedMacroInfoVisible(IdentifierInfo *II,
129 assert(MI->isFromAST() && "Macro must be from the AST");
131 MacroInfo *&StoredMI = Macros[II];
132 if (StoredMI == MI) {
133 // Easy case: this is the first macro anyway.
134 II->setHasMacroDefinition(MI->isDefined());
138 // Go find the macro and pull it out of the list.
139 // FIXME: Yes, this is O(N), and making a pile of macros visible or hidden
140 // would be quadratic, but it's extremely rare.
141 MacroInfo *Prev = StoredMI;
142 while (Prev->getPreviousDefinition() != MI)
143 Prev = Prev->getPreviousDefinition();
144 Prev->setPreviousDefinition(MI->getPreviousDefinition());
145 MI->setPreviousDefinition(0);
147 // Add the macro back to the list.
148 addLoadedMacroInfo(II, MI);
150 II->setHasMacroDefinition(StoredMI->isDefined());
152 II->setChangedSinceDeserialization();
155 /// \brief Undefine a macro for this identifier.
156 void Preprocessor::clearMacroInfo(IdentifierInfo *II) {
157 assert(II->hasMacroDefinition() && "Macro is not defined!");
158 assert(Macros[II]->getUndefLoc().isValid() && "Macro is still defined!");
159 II->setHasMacroDefinition(false);
161 II->setChangedSinceDeserialization();
164 /// RegisterBuiltinMacro - Register the specified identifier in the identifier
165 /// table and mark it as a builtin macro to be expanded.
166 static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
167 // Get the identifier.
168 IdentifierInfo *Id = PP.getIdentifierInfo(Name);
170 // Mark it as being a macro that is builtin.
171 MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
172 MI->setIsBuiltinMacro();
173 PP.setMacroInfo(Id, MI);
178 /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
179 /// identifier table.
180 void Preprocessor::RegisterBuiltinMacros() {
181 Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
182 Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
183 Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
184 Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
185 Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
186 Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma");
189 Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__");
190 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
191 Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
194 Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature");
195 Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension");
196 Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin");
197 Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute");
198 Ident__has_include = RegisterBuiltinMacro(*this, "__has_include");
199 Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
200 Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning");
203 if (LangOpts.Modules) {
204 Ident__building_module = RegisterBuiltinMacro(*this, "__building_module");
207 if (!LangOpts.CurrentModule.empty())
208 Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
212 Ident__building_module = 0;
216 // Microsoft Extensions.
217 if (LangOpts.MicrosoftExt)
218 Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
223 /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
224 /// in its expansion, currently expands to that token literally.
225 static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
226 const IdentifierInfo *MacroIdent,
228 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
230 // If the token isn't an identifier, it's always literally expanded.
231 if (II == 0) return true;
233 // If the information about this identifier is out of date, update it from
234 // the external source.
235 if (II->isOutOfDate())
236 PP.getExternalSource()->updateOutOfDateIdentifier(*II);
238 // If the identifier is a macro, and if that macro is enabled, it may be
239 // expanded so it's not a trivial expansion.
240 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
241 // Fast expanding "#define X X" is ok, because X would be disabled.
245 // If this is an object-like macro invocation, it is safe to trivially expand
247 if (MI->isObjectLike()) return true;
249 // If this is a function-like macro invocation, it's safe to trivially expand
250 // as long as the identifier is not a macro argument.
251 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
254 return false; // Identifier is a macro argument.
260 /// isNextPPTokenLParen - Determine whether the next preprocessor token to be
261 /// lexed is a '('. If so, consume the token and return true, if not, this
262 /// method should have no observable side-effect on the lexed tokens.
263 bool Preprocessor::isNextPPTokenLParen() {
264 // Do some quick tests for rejection cases.
267 Val = CurLexer->isNextPPTokenLParen();
268 else if (CurPTHLexer)
269 Val = CurPTHLexer->isNextPPTokenLParen();
271 Val = CurTokenLexer->isNextTokenLParen();
274 // We have run off the end. If it's a source file we don't
275 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
279 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
280 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
282 Val = Entry.TheLexer->isNextPPTokenLParen();
283 else if (Entry.ThePTHLexer)
284 Val = Entry.ThePTHLexer->isNextPPTokenLParen();
286 Val = Entry.TheTokenLexer->isNextTokenLParen();
291 // Ran off the end of a source file?
292 if (Entry.ThePPLexer)
297 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
298 // have found something that isn't a '(' or we found the end of the
299 // translation unit. In either case, return false.
303 /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
304 /// expanded as a macro, handle it and return the next token as 'Identifier'.
305 bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
307 // If this is a macro expansion in the "#if !defined(x)" line for the file,
308 // then the macro could expand to different things in other contexts, we need
309 // to disable the optimization in this case.
310 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
312 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
313 if (MI->isBuiltinMacro()) {
314 if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
315 Identifier.getLocation());
316 ExpandBuiltinMacro(Identifier);
320 /// Args - If this is a function-like macro expansion, this contains,
321 /// for each macro argument, the list of tokens that were provided to the
325 // Remember where the end of the expansion occurred. For an object-like
326 // macro, this is the identifier. For a function-like macro, this is the ')'.
327 SourceLocation ExpansionEnd = Identifier.getLocation();
329 // If this is a function-like macro, read the arguments.
330 if (MI->isFunctionLike()) {
331 // C99 6.10.3p10: If the preprocessing token immediately after the macro
332 // name isn't a '(', this macro should not be expanded.
333 if (!isNextPPTokenLParen())
336 // Remember that we are now parsing the arguments to a macro invocation.
337 // Preprocessor directives used inside macro arguments are not portable, and
338 // this enables the warning.
340 Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
342 // Finished parsing args.
345 // If there was an error parsing the arguments, bail out.
346 if (Args == 0) return false;
348 ++NumFnMacroExpanded;
353 // Notice that this macro has been used.
356 // Remember where the token is expanded.
357 SourceLocation ExpandLoc = Identifier.getLocation();
358 SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
362 // We can have macro expansion inside a conditional directive while
363 // reading the function macro arguments. To ensure, in that case, that
364 // MacroExpands callbacks still happen in source order, queue this
365 // callback to have it happen after the function macro callback.
366 DelayedMacroExpandsCallbacks.push_back(
367 MacroExpandsInfo(Identifier, MI, ExpansionRange));
369 Callbacks->MacroExpands(Identifier, MI, ExpansionRange);
370 if (!DelayedMacroExpandsCallbacks.empty()) {
371 for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
372 MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
373 Callbacks->MacroExpands(Info.Tok, Info.MI, Info.Range);
375 DelayedMacroExpandsCallbacks.clear();
380 // If the macro definition is ambiguous, complain.
381 if (MI->isAmbiguous()) {
382 Diag(Identifier, diag::warn_pp_ambiguous_macro)
383 << Identifier.getIdentifierInfo();
384 Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
385 << Identifier.getIdentifierInfo();
386 for (MacroInfo *PrevMI = MI->getPreviousDefinition();
387 PrevMI && PrevMI->isDefined();
388 PrevMI = PrevMI->getPreviousDefinition()) {
389 if (PrevMI->isAmbiguous()) {
390 Diag(PrevMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other)
391 << Identifier.getIdentifierInfo();
396 // If we started lexing a macro, enter the macro expansion body.
398 // If this macro expands to no tokens, don't bother to push it onto the
399 // expansion stack, only to take it right back off.
400 if (MI->getNumTokens() == 0) {
401 // No need for arg info.
402 if (Args) Args->destroy(*this);
404 // Ignore this macro use, just return the next token in the current
406 bool HadLeadingSpace = Identifier.hasLeadingSpace();
407 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
411 // If the identifier isn't on some OTHER line, inherit the leading
412 // whitespace/first-on-a-line property of this token. This handles
413 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
415 if (!Identifier.isAtStartOfLine()) {
416 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
417 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
419 Identifier.setFlag(Token::LeadingEmptyMacro);
420 ++NumFastMacroExpanded;
423 } else if (MI->getNumTokens() == 1 &&
424 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
426 // Otherwise, if this macro expands into a single trivially-expanded
427 // token: expand it now. This handles common cases like
430 // No need for arg info.
431 if (Args) Args->destroy(*this);
433 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
434 // identifier to the expanded token.
435 bool isAtStartOfLine = Identifier.isAtStartOfLine();
436 bool hasLeadingSpace = Identifier.hasLeadingSpace();
438 // Replace the result token.
439 Identifier = MI->getReplacementToken(0);
441 // Restore the StartOfLine/LeadingSpace markers.
442 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
443 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
445 // Update the tokens location to include both its expansion and physical
448 SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
449 ExpansionEnd,Identifier.getLength());
450 Identifier.setLocation(Loc);
452 // If this is a disabled macro or #define X X, we must mark the result as
454 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
455 if (MacroInfo *NewMI = getMacroInfo(NewII))
456 if (!NewMI->isEnabled() || NewMI == MI) {
457 Identifier.setFlag(Token::DisableExpand);
458 Diag(Identifier, diag::pp_disabled_macro_expansion);
462 // Since this is not an identifier token, it can't be macro expanded, so
464 ++NumFastMacroExpanded;
468 // Start expanding the macro.
469 EnterMacro(Identifier, ExpansionEnd, MI, Args);
471 // Now that the macro is at the top of the include stack, ask the
472 // preprocessor to read the next token from it.
477 /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
478 /// token is the '(' of the macro, this method is invoked to read all of the
479 /// actual arguments specified for the macro invocation. This returns null on
481 MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
483 SourceLocation &MacroEnd) {
484 // The number of fixed arguments to parse.
485 unsigned NumFixedArgsLeft = MI->getNumArgs();
486 bool isVariadic = MI->isVariadic();
488 // Outer loop, while there are more arguments, keep reading them.
491 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
492 // an argument value in a macro could expand to ',' or '(' or ')'.
493 LexUnexpandedToken(Tok);
494 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
496 // ArgTokens - Build up a list of tokens that make up each argument. Each
497 // argument is separated by an EOF token. Use a SmallVector so we can avoid
498 // heap allocations in the common case.
499 SmallVector<Token, 64> ArgTokens;
501 unsigned NumActuals = 0;
502 while (Tok.isNot(tok::r_paren)) {
503 assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
504 "only expect argument separators here");
506 unsigned ArgTokenStart = ArgTokens.size();
507 SourceLocation ArgStartLoc = Tok.getLocation();
509 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
510 // that we already consumed the first one.
511 unsigned NumParens = 0;
514 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
515 // an argument value in a macro could expand to ',' or '(' or ')'.
516 LexUnexpandedToken(Tok);
518 if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
519 Diag(MacroName, diag::err_unterm_macro_invoc);
520 // Do not lose the EOF/EOD. Return it to the client.
523 } else if (Tok.is(tok::r_paren)) {
524 // If we found the ) token, the macro arg list is done.
525 if (NumParens-- == 0) {
526 MacroEnd = Tok.getLocation();
529 } else if (Tok.is(tok::l_paren)) {
531 } else if (Tok.is(tok::comma) && NumParens == 0) {
532 // Comma ends this argument if there are more fixed arguments expected.
533 // However, if this is a variadic macro, and this is part of the
534 // variadic part, then the comma is just an argument token.
535 if (!isVariadic) break;
536 if (NumFixedArgsLeft > 1)
538 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
539 // If this is a comment token in the argument list and we're just in
540 // -C mode (not -CC mode), discard the comment.
542 } else if (Tok.getIdentifierInfo() != 0) {
543 // Reading macro arguments can cause macros that we are currently
544 // expanding from to be popped off the expansion stack. Doing so causes
545 // them to be reenabled for expansion. Here we record whether any
546 // identifiers we lex as macro arguments correspond to disabled macros.
547 // If so, we mark the token as noexpand. This is a subtle aspect of
549 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
550 if (!MI->isEnabled())
551 Tok.setFlag(Token::DisableExpand);
552 } else if (Tok.is(tok::code_completion)) {
554 CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
556 // Don't mark that we reached the code-completion point because the
557 // parser is going to handle the token and there will be another
558 // code-completion callback.
561 ArgTokens.push_back(Tok);
564 // If this was an empty argument list foo(), don't add this as an empty
566 if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
569 // If this is not a variadic macro, and too many args were specified, emit
571 if (!isVariadic && NumFixedArgsLeft == 0) {
572 if (ArgTokens.size() != ArgTokenStart)
573 ArgStartLoc = ArgTokens[ArgTokenStart].getLocation();
575 // Emit the diagnostic at the macro name in case there is a missing ).
576 // Emitting it at the , could be far away from the macro name.
577 Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc);
581 // Empty arguments are standard in C99 and C++0x, and are supported as an extension in
583 if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
584 Diag(Tok, LangOpts.CPlusPlus0x ?
585 diag::warn_cxx98_compat_empty_fnmacro_arg :
586 diag::ext_empty_fnmacro_arg);
588 // Add a marker EOF token to the end of the token list for this argument.
591 EOFTok.setKind(tok::eof);
592 EOFTok.setLocation(Tok.getLocation());
594 ArgTokens.push_back(EOFTok);
596 assert(NumFixedArgsLeft != 0 && "Too many arguments parsed");
600 // Okay, we either found the r_paren. Check to see if we parsed too few
602 unsigned MinArgsExpected = MI->getNumArgs();
604 // See MacroArgs instance var for description of this.
605 bool isVarargsElided = false;
607 if (NumActuals < MinArgsExpected) {
608 // There are several cases where too few arguments is ok, handle them now.
609 if (NumActuals == 0 && MinArgsExpected == 1) {
610 // #define A(X) or #define A(...) ---> A()
612 // If there is exactly one argument, and that argument is missing,
613 // then we have an empty "()" argument empty list. This is fine, even if
614 // the macro expects one argument (the argument is just empty).
615 isVarargsElided = MI->isVariadic();
616 } else if (MI->isVariadic() &&
617 (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X)
618 (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
619 // Varargs where the named vararg parameter is missing: OK as extension.
622 Diag(Tok, diag::ext_missing_varargs_arg);
623 Diag(MI->getDefinitionLoc(), diag::note_macro_here)
624 << MacroName.getIdentifierInfo();
626 // Remember this occurred, allowing us to elide the comma when used for
628 // #define A(x, foo...) blah(a, ## foo)
629 // #define B(x, ...) blah(a, ## __VA_ARGS__)
630 // #define C(...) blah(a, ## __VA_ARGS__)
632 isVarargsElided = true;
634 // Otherwise, emit the error.
635 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
639 // Add a marker EOF token to the end of the token list for this argument.
640 SourceLocation EndLoc = Tok.getLocation();
642 Tok.setKind(tok::eof);
643 Tok.setLocation(EndLoc);
645 ArgTokens.push_back(Tok);
647 // If we expect two arguments, add both as empty.
648 if (NumActuals == 0 && MinArgsExpected == 2)
649 ArgTokens.push_back(Tok);
651 } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) {
652 // Emit the diagnostic at the macro name in case there is a missing ).
653 // Emitting it at the , could be far away from the macro name.
654 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
658 return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
661 /// \brief Keeps macro expanded tokens for TokenLexers.
663 /// Works like a stack; a TokenLexer adds the macro expanded tokens that is
664 /// going to lex in the cache and when it finishes the tokens are removed
665 /// from the end of the cache.
666 Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
667 ArrayRef<Token> tokens) {
672 size_t newIndex = MacroExpandedTokens.size();
673 bool cacheNeedsToGrow = tokens.size() >
674 MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
675 MacroExpandedTokens.append(tokens.begin(), tokens.end());
677 if (cacheNeedsToGrow) {
678 // Go through all the TokenLexers whose 'Tokens' pointer points in the
679 // buffer and update the pointers to the (potential) new buffer array.
680 for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
681 TokenLexer *prevLexer;
683 llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
684 prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
688 MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
689 return MacroExpandedTokens.data() + newIndex;
692 void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
693 assert(!MacroExpandingLexersStack.empty());
694 size_t tokIndex = MacroExpandingLexersStack.back().second;
695 assert(tokIndex < MacroExpandedTokens.size());
696 // Pop the cached macro expanded tokens from the end.
697 MacroExpandedTokens.resize(tokIndex);
698 MacroExpandingLexersStack.pop_back();
701 /// ComputeDATE_TIME - Compute the current time, enter it into the specified
702 /// scratch buffer, then return DATELoc/TIMELoc locations with the position of
703 /// the identifier tokens inserted.
704 static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
707 struct tm *TM = localtime(&TT);
709 static const char * const Months[] = {
710 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
714 SmallString<32> TmpBuffer;
715 llvm::raw_svector_ostream TmpStream(TmpBuffer);
716 TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
717 TM->tm_mday, TM->tm_year + 1900);
720 PP.CreateString(TmpStream.str(), TmpTok);
721 DATELoc = TmpTok.getLocation();
725 SmallString<32> TmpBuffer;
726 llvm::raw_svector_ostream TmpStream(TmpBuffer);
727 TmpStream << llvm::format("\"%02d:%02d:%02d\"",
728 TM->tm_hour, TM->tm_min, TM->tm_sec);
731 PP.CreateString(TmpStream.str(), TmpTok);
732 TIMELoc = TmpTok.getLocation();
737 /// HasFeature - Return true if we recognize and implement the feature
738 /// specified by the identifier as a standard language feature.
739 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
740 const LangOptions &LangOpts = PP.getLangOpts();
741 StringRef Feature = II->getName();
743 // Normalize the feature name, __foo__ becomes foo.
744 if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
745 Feature = Feature.substr(2, Feature.size() - 4);
747 return llvm::StringSwitch<bool>(Feature)
748 .Case("address_sanitizer", LangOpts.SanitizeAddress)
749 .Case("attribute_analyzer_noreturn", true)
750 .Case("attribute_availability", true)
751 .Case("attribute_availability_with_message", true)
752 .Case("attribute_cf_returns_not_retained", true)
753 .Case("attribute_cf_returns_retained", true)
754 .Case("attribute_deprecated_with_message", true)
755 .Case("attribute_ext_vector_type", true)
756 .Case("attribute_ns_returns_not_retained", true)
757 .Case("attribute_ns_returns_retained", true)
758 .Case("attribute_ns_consumes_self", true)
759 .Case("attribute_ns_consumed", true)
760 .Case("attribute_cf_consumed", true)
761 .Case("attribute_objc_ivar_unused", true)
762 .Case("attribute_objc_method_family", true)
763 .Case("attribute_overloadable", true)
764 .Case("attribute_unavailable_with_message", true)
765 .Case("attribute_unused_on_fields", true)
766 .Case("blocks", LangOpts.Blocks)
767 .Case("cxx_exceptions", LangOpts.Exceptions)
768 .Case("cxx_rtti", LangOpts.RTTI)
769 .Case("enumerator_attributes", true)
770 // Objective-C features
771 .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
772 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
773 .Case("objc_arc_weak", LangOpts.ObjCARCWeak)
774 .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
775 .Case("objc_fixed_enum", LangOpts.ObjC2)
776 .Case("objc_instancetype", LangOpts.ObjC2)
777 .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
778 .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
779 .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
780 .Case("ownership_holds", true)
781 .Case("ownership_returns", true)
782 .Case("ownership_takes", true)
783 .Case("objc_bool", true)
784 .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
785 .Case("objc_array_literals", LangOpts.ObjC2)
786 .Case("objc_dictionary_literals", LangOpts.ObjC2)
787 .Case("objc_boxed_expressions", LangOpts.ObjC2)
788 .Case("arc_cf_code_audited", true)
790 .Case("c_alignas", LangOpts.C11)
791 .Case("c_atomic", LangOpts.C11)
792 .Case("c_generic_selections", LangOpts.C11)
793 .Case("c_static_assert", LangOpts.C11)
795 .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus0x)
796 .Case("cxx_alias_templates", LangOpts.CPlusPlus0x)
797 .Case("cxx_alignas", LangOpts.CPlusPlus0x)
798 .Case("cxx_atomic", LangOpts.CPlusPlus0x)
799 .Case("cxx_attributes", LangOpts.CPlusPlus0x)
800 .Case("cxx_auto_type", LangOpts.CPlusPlus0x)
801 .Case("cxx_constexpr", LangOpts.CPlusPlus0x)
802 .Case("cxx_decltype", LangOpts.CPlusPlus0x)
803 .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus0x)
804 .Case("cxx_default_function_template_args", LangOpts.CPlusPlus0x)
805 .Case("cxx_defaulted_functions", LangOpts.CPlusPlus0x)
806 .Case("cxx_delegating_constructors", LangOpts.CPlusPlus0x)
807 .Case("cxx_deleted_functions", LangOpts.CPlusPlus0x)
808 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus0x)
809 .Case("cxx_generalized_initializers", LangOpts.CPlusPlus0x)
810 .Case("cxx_implicit_moves", LangOpts.CPlusPlus0x)
811 //.Case("cxx_inheriting_constructors", false)
812 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus0x)
813 .Case("cxx_lambdas", LangOpts.CPlusPlus0x)
814 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus0x)
815 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus0x)
816 .Case("cxx_noexcept", LangOpts.CPlusPlus0x)
817 .Case("cxx_nullptr", LangOpts.CPlusPlus0x)
818 .Case("cxx_override_control", LangOpts.CPlusPlus0x)
819 .Case("cxx_range_for", LangOpts.CPlusPlus0x)
820 .Case("cxx_raw_string_literals", LangOpts.CPlusPlus0x)
821 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus0x)
822 .Case("cxx_rvalue_references", LangOpts.CPlusPlus0x)
823 .Case("cxx_strong_enums", LangOpts.CPlusPlus0x)
824 .Case("cxx_static_assert", LangOpts.CPlusPlus0x)
825 .Case("cxx_trailing_return", LangOpts.CPlusPlus0x)
826 .Case("cxx_unicode_literals", LangOpts.CPlusPlus0x)
827 .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus0x)
828 .Case("cxx_user_literals", LangOpts.CPlusPlus0x)
829 .Case("cxx_variadic_templates", LangOpts.CPlusPlus0x)
831 .Case("has_nothrow_assign", LangOpts.CPlusPlus)
832 .Case("has_nothrow_copy", LangOpts.CPlusPlus)
833 .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
834 .Case("has_trivial_assign", LangOpts.CPlusPlus)
835 .Case("has_trivial_copy", LangOpts.CPlusPlus)
836 .Case("has_trivial_constructor", LangOpts.CPlusPlus)
837 .Case("has_trivial_destructor", LangOpts.CPlusPlus)
838 .Case("has_virtual_destructor", LangOpts.CPlusPlus)
839 .Case("is_abstract", LangOpts.CPlusPlus)
840 .Case("is_base_of", LangOpts.CPlusPlus)
841 .Case("is_class", LangOpts.CPlusPlus)
842 .Case("is_convertible_to", LangOpts.CPlusPlus)
843 // __is_empty is available only if the horrible
844 // "struct __is_empty" parsing hack hasn't been needed in this
845 // translation unit. If it has, __is_empty reverts to a normal
846 // identifier and __has_feature(is_empty) evaluates false.
847 .Case("is_empty", LangOpts.CPlusPlus)
848 .Case("is_enum", LangOpts.CPlusPlus)
849 .Case("is_final", LangOpts.CPlusPlus)
850 .Case("is_literal", LangOpts.CPlusPlus)
851 .Case("is_standard_layout", LangOpts.CPlusPlus)
852 .Case("is_pod", LangOpts.CPlusPlus)
853 .Case("is_polymorphic", LangOpts.CPlusPlus)
854 .Case("is_trivial", LangOpts.CPlusPlus)
855 .Case("is_trivially_assignable", LangOpts.CPlusPlus)
856 .Case("is_trivially_constructible", LangOpts.CPlusPlus)
857 .Case("is_trivially_copyable", LangOpts.CPlusPlus)
858 .Case("is_union", LangOpts.CPlusPlus)
859 .Case("modules", LangOpts.Modules)
860 .Case("tls", PP.getTargetInfo().isTLSSupported())
861 .Case("underlying_type", LangOpts.CPlusPlus)
865 /// HasExtension - Return true if we recognize and implement the feature
866 /// specified by the identifier, either as an extension or a standard language
868 static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
869 if (HasFeature(PP, II))
872 // If the use of an extension results in an error diagnostic, extensions are
873 // effectively unavailable, so just return false here.
874 if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
875 DiagnosticsEngine::Ext_Error)
878 const LangOptions &LangOpts = PP.getLangOpts();
879 StringRef Extension = II->getName();
881 // Normalize the extension name, __foo__ becomes foo.
882 if (Extension.startswith("__") && Extension.endswith("__") &&
883 Extension.size() >= 4)
884 Extension = Extension.substr(2, Extension.size() - 4);
886 // Because we inherit the feature list from HasFeature, this string switch
887 // must be less restrictive than HasFeature's.
888 return llvm::StringSwitch<bool>(Extension)
889 // C11 features supported by other languages as extensions.
890 .Case("c_alignas", true)
891 .Case("c_atomic", true)
892 .Case("c_generic_selections", true)
893 .Case("c_static_assert", true)
894 // C++0x features supported by other languages as extensions.
895 .Case("cxx_atomic", LangOpts.CPlusPlus)
896 .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
897 .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
898 .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
899 .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
900 .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
901 .Case("cxx_override_control", LangOpts.CPlusPlus)
902 .Case("cxx_range_for", LangOpts.CPlusPlus)
903 .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
904 .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
908 /// HasAttribute - Return true if we recognize and implement the attribute
909 /// specified by the given identifier.
910 static bool HasAttribute(const IdentifierInfo *II) {
911 StringRef Name = II->getName();
912 // Normalize the attribute name, __foo__ becomes foo.
913 if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4)
914 Name = Name.substr(2, Name.size() - 4);
916 // FIXME: Do we need to handle namespaces here?
917 return llvm::StringSwitch<bool>(Name)
918 #include "clang/Lex/AttrSpellings.inc"
922 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
923 /// or '__has_include_next("path")' expression.
924 /// Returns true if successful.
925 static bool EvaluateHasIncludeCommon(Token &Tok,
926 IdentifierInfo *II, Preprocessor &PP,
927 const DirectoryLookup *LookupFrom) {
928 // Save the location of the current token. If a '(' is later found, use
929 // that location. If no, use the end of this location instead.
930 SourceLocation LParenLoc = Tok.getLocation();
933 PP.LexNonComment(Tok);
935 // Ensure we have a '('.
936 if (Tok.isNot(tok::l_paren)) {
937 // No '(', use end of last token.
938 LParenLoc = PP.getLocForEndOfToken(LParenLoc);
939 PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName();
940 // If the next token looks like a filename or the start of one,
941 // assume it is and process it as such.
942 if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
946 // Save '(' location for possible missing ')' message.
947 LParenLoc = Tok.getLocation();
949 // Get the file name.
950 PP.getCurrentLexer()->LexIncludeFilename(Tok);
953 // Reserve a buffer to get the spelling.
954 SmallString<128> FilenameBuffer;
956 SourceLocation EndLoc;
958 switch (Tok.getKind()) {
960 // If the token kind is EOD, the error has already been diagnosed.
963 case tok::angle_string_literal:
964 case tok::string_literal: {
965 bool Invalid = false;
966 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
973 // This could be a <foo/bar.h> file coming from a macro expansion. In this
974 // case, glue the tokens together into FilenameBuffer and interpret those.
975 FilenameBuffer.push_back('<');
976 if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
977 // Let the caller know a <eod> was found by changing the Token kind.
978 Tok.setKind(tok::eod);
979 return false; // Found <eod> but no ">"? Diagnostic already emitted.
981 Filename = FilenameBuffer.str();
984 PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
988 SourceLocation FilenameLoc = Tok.getLocation();
991 PP.LexNonComment(Tok);
993 // Ensure we have a trailing ).
994 if (Tok.isNot(tok::r_paren)) {
995 PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen)
997 PP.Diag(LParenLoc, diag::note_matching) << "(";
1001 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
1002 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1004 if (Filename.empty())
1007 // Search include directories.
1008 const DirectoryLookup *CurDir;
1009 const FileEntry *File =
1010 PP.LookupFile(Filename, isAngled, LookupFrom, CurDir, NULL, NULL, NULL);
1012 // Get the result value. A result of true means the file exists.
1016 /// EvaluateHasInclude - Process a '__has_include("path")' expression.
1017 /// Returns true if successful.
1018 static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
1020 return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
1023 /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
1024 /// Returns true if successful.
1025 static bool EvaluateHasIncludeNext(Token &Tok,
1026 IdentifierInfo *II, Preprocessor &PP) {
1027 // __has_include_next is like __has_include, except that we start
1028 // searching after the current found directory. If we can't do this,
1029 // issue a diagnostic.
1030 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
1031 if (PP.isInPrimaryFile()) {
1033 PP.Diag(Tok, diag::pp_include_next_in_primary);
1034 } else if (Lookup == 0) {
1035 PP.Diag(Tok, diag::pp_include_next_absolute_path);
1037 // Start looking up in the next directory.
1041 return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
1044 /// \brief Process __building_module(identifier) expression.
1045 /// \returns true if we are building the named module, false otherwise.
1046 static bool EvaluateBuildingModule(Token &Tok,
1047 IdentifierInfo *II, Preprocessor &PP) {
1049 PP.LexNonComment(Tok);
1051 // Ensure we have a '('.
1052 if (Tok.isNot(tok::l_paren)) {
1053 PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
1057 // Save '(' location for possible missing ')' message.
1058 SourceLocation LParenLoc = Tok.getLocation();
1060 // Get the module name.
1061 PP.LexNonComment(Tok);
1063 // Ensure that we have an identifier.
1064 if (Tok.isNot(tok::identifier)) {
1065 PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1070 = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1073 PP.LexNonComment(Tok);
1075 // Ensure we have a trailing ).
1076 if (Tok.isNot(tok::r_paren)) {
1077 PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
1078 PP.Diag(LParenLoc, diag::note_matching) << "(";
1085 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
1086 /// as a builtin macro, handle it and return the next token as 'Tok'.
1087 void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
1088 // Figure out which token this is.
1089 IdentifierInfo *II = Tok.getIdentifierInfo();
1090 assert(II && "Can't be a macro without id info!");
1092 // If this is an _Pragma or Microsoft __pragma directive, expand it,
1093 // invoke the pragma handler, then lex the token after it.
1094 if (II == Ident_Pragma)
1095 return Handle_Pragma(Tok);
1096 else if (II == Ident__pragma) // in non-MS mode this is null
1097 return HandleMicrosoft__pragma(Tok);
1099 ++NumBuiltinMacroExpanded;
1101 SmallString<128> TmpBuffer;
1102 llvm::raw_svector_ostream OS(TmpBuffer);
1104 // Set up the return result.
1105 Tok.setIdentifierInfo(0);
1106 Tok.clearFlag(Token::NeedsCleaning);
1108 if (II == Ident__LINE__) {
1109 // C99 6.10.8: "__LINE__: The presumed line number (within the current
1110 // source file) of the current source line (an integer constant)". This can
1111 // be affected by #line.
1112 SourceLocation Loc = Tok.getLocation();
1114 // Advance to the location of the first _, this might not be the first byte
1115 // of the token if it starts with an escaped newline.
1116 Loc = AdvanceToTokenCharacter(Loc, 0);
1118 // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
1119 // a macro expansion. This doesn't matter for object-like macros, but
1120 // can matter for a function-like macro that expands to contain __LINE__.
1121 // Skip down through expansion points until we find a file loc for the
1122 // end of the expansion history.
1123 Loc = SourceMgr.getExpansionRange(Loc).second;
1124 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
1126 // __LINE__ expands to a simple numeric value.
1127 OS << (PLoc.isValid()? PLoc.getLine() : 1);
1128 Tok.setKind(tok::numeric_constant);
1129 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
1130 // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
1131 // character string literal)". This can be affected by #line.
1132 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1134 // __BASE_FILE__ is a GNU extension that returns the top of the presumed
1135 // #include stack instead of the current file.
1136 if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
1137 SourceLocation NextLoc = PLoc.getIncludeLoc();
1138 while (NextLoc.isValid()) {
1139 PLoc = SourceMgr.getPresumedLoc(NextLoc);
1140 if (PLoc.isInvalid())
1143 NextLoc = PLoc.getIncludeLoc();
1147 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
1148 SmallString<128> FN;
1149 if (PLoc.isValid()) {
1150 FN += PLoc.getFilename();
1151 Lexer::Stringify(FN);
1152 OS << '"' << FN.str() << '"';
1154 Tok.setKind(tok::string_literal);
1155 } else if (II == Ident__DATE__) {
1156 if (!DATELoc.isValid())
1157 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1158 Tok.setKind(tok::string_literal);
1159 Tok.setLength(strlen("\"Mmm dd yyyy\""));
1160 Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
1164 } else if (II == Ident__TIME__) {
1165 if (!TIMELoc.isValid())
1166 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
1167 Tok.setKind(tok::string_literal);
1168 Tok.setLength(strlen("\"hh:mm:ss\""));
1169 Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
1173 } else if (II == Ident__INCLUDE_LEVEL__) {
1174 // Compute the presumed include depth of this token. This can be affected
1175 // by GNU line markers.
1178 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1179 if (PLoc.isValid()) {
1180 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1181 for (; PLoc.isValid(); ++Depth)
1182 PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
1185 // __INCLUDE_LEVEL__ expands to a simple numeric value.
1187 Tok.setKind(tok::numeric_constant);
1188 } else if (II == Ident__TIMESTAMP__) {
1189 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
1190 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1192 // Get the file that we are lexing out of. If we're currently lexing from
1193 // a macro, dig into the include stack.
1194 const FileEntry *CurFile = 0;
1195 PreprocessorLexer *TheLexer = getCurrentFileLexer();
1198 CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
1202 time_t TT = CurFile->getModificationTime();
1203 struct tm *TM = localtime(&TT);
1204 Result = asctime(TM);
1206 Result = "??? ??? ?? ??:??:?? ????\n";
1208 // Surround the string with " and strip the trailing newline.
1209 OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
1210 Tok.setKind(tok::string_literal);
1211 } else if (II == Ident__COUNTER__) {
1212 // __COUNTER__ expands to a simple numeric value.
1213 OS << CounterValue++;
1214 Tok.setKind(tok::numeric_constant);
1215 } else if (II == Ident__has_feature ||
1216 II == Ident__has_extension ||
1217 II == Ident__has_builtin ||
1218 II == Ident__has_attribute) {
1219 // The argument to these builtins should be a parenthesized identifier.
1220 SourceLocation StartLoc = Tok.getLocation();
1222 bool IsValid = false;
1223 IdentifierInfo *FeatureII = 0;
1227 if (Tok.is(tok::l_paren)) {
1228 // Read the identifier
1230 if (Tok.is(tok::identifier) || Tok.is(tok::kw_const)) {
1231 FeatureII = Tok.getIdentifierInfo();
1235 if (Tok.is(tok::r_paren))
1242 Diag(StartLoc, diag::err_feature_check_malformed);
1243 else if (II == Ident__has_builtin) {
1244 // Check for a builtin is trivial.
1245 Value = FeatureII->getBuiltinID() != 0;
1246 } else if (II == Ident__has_attribute)
1247 Value = HasAttribute(FeatureII);
1248 else if (II == Ident__has_extension)
1249 Value = HasExtension(*this, FeatureII);
1251 assert(II == Ident__has_feature && "Must be feature check");
1252 Value = HasFeature(*this, FeatureII);
1257 Tok.setKind(tok::numeric_constant);
1258 } else if (II == Ident__has_include ||
1259 II == Ident__has_include_next) {
1260 // The argument to these two builtins should be a parenthesized
1261 // file name string literal using angle brackets (<>) or
1262 // double-quotes ("").
1264 if (II == Ident__has_include)
1265 Value = EvaluateHasInclude(Tok, II, *this);
1267 Value = EvaluateHasIncludeNext(Tok, II, *this);
1269 if (Tok.is(tok::r_paren))
1270 Tok.setKind(tok::numeric_constant);
1271 } else if (II == Ident__has_warning) {
1272 // The argument should be a parenthesized string literal.
1273 // The argument to these builtins should be a parenthesized identifier.
1274 SourceLocation StartLoc = Tok.getLocation();
1275 bool IsValid = false;
1280 if (Tok.is(tok::l_paren)) {
1284 // We need at least one string literal.
1285 if (!Tok.is(tok::string_literal)) {
1286 StartLoc = Tok.getLocation();
1288 // Eat tokens until ')'.
1289 do Lex(Tok); while (!(Tok.is(tok::r_paren) || Tok.is(tok::eod)));
1293 // String concatenation allows multiple strings, which can even come
1294 // from macro expansion.
1295 SmallVector<Token, 4> StrToks;
1296 while (Tok.is(tok::string_literal)) {
1297 // Complain about, and drop, any ud-suffix.
1298 if (Tok.hasUDSuffix())
1299 Diag(Tok, diag::err_invalid_string_udl);
1300 StrToks.push_back(Tok);
1301 LexUnexpandedToken(Tok);
1304 // Is the end a ')'?
1305 if (!(IsValid = Tok.is(tok::r_paren)))
1308 // Concatenate and parse the strings.
1309 StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this);
1310 assert(Literal.isAscii() && "Didn't allow wide strings in");
1311 if (Literal.hadError)
1313 if (Literal.Pascal) {
1314 Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1318 StringRef WarningName(Literal.GetString());
1320 if (WarningName.size() < 3 || WarningName[0] != '-' ||
1321 WarningName[1] != 'W') {
1322 Diag(StrToks[0].getLocation(), diag::warn_has_warning_invalid_option);
1326 // Finally, check if the warning flags maps to a diagnostic group.
1327 // We construct a SmallVector here to talk to getDiagnosticIDs().
1328 // Although we don't use the result, this isn't a hot path, and not
1329 // worth special casing.
1330 llvm::SmallVector<diag::kind, 10> Diags;
1331 Value = !getDiagnostics().getDiagnosticIDs()->
1332 getDiagnosticsInGroup(WarningName.substr(2), Diags);
1337 Diag(StartLoc, diag::err_warning_check_malformed);
1340 Tok.setKind(tok::numeric_constant);
1341 } else if (II == Ident__building_module) {
1342 // The argument to this builtin should be an identifier. The
1343 // builtin evaluates to 1 when that identifier names the module we are
1344 // currently building.
1345 OS << (int)EvaluateBuildingModule(Tok, II, *this);
1346 Tok.setKind(tok::numeric_constant);
1347 } else if (II == Ident__MODULE__) {
1348 // The current module as an identifier.
1349 OS << getLangOpts().CurrentModule;
1350 IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1351 Tok.setIdentifierInfo(ModuleII);
1352 Tok.setKind(ModuleII->getTokenID());
1354 llvm_unreachable("Unknown identifier!");
1356 CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
1359 void Preprocessor::markMacroAsUsed(MacroInfo *MI) {
1360 // If the 'used' status changed, and the macro requires 'unused' warning,
1361 // remove its SourceLocation from the warn-for-unused-macro locations.
1362 if (MI->isWarnIfUnused() && !MI->isUsed())
1363 WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
1364 MI->setIsUsed(true);