13e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
23e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//
33e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//                     The LLVM Compiler Infrastructure
43e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//
53e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// This file is distributed under the University of Illinois Open Source
63e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// License. See LICENSE.TXT for details.
73e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//
83e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//===----------------------------------------------------------------------===//
93e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//
103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// This file implements the top level handling of macro expasion for the
113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// preprocessor.
123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//
133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//===----------------------------------------------------------------------===//
143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Lex/Preprocessor.h"
16dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis#include "clang/Lex/MacroArgs.h"
173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Basic/FileManager.h"
1855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/SourceManager.h"
193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Basic/TargetInfo.h"
203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Lex/CodeCompletionHandler.h"
213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Lex/ExternalPreprocessorSource.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/LexDiagnostic.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/MacroInfo.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/STLExtras.h"
2502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs#include "llvm/ADT/SmallString.h"
263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "llvm/ADT/StringSwitch.h"
273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "llvm/Config/llvm-config.h"
283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "llvm/Support/ErrorHandling.h"
2933d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko#include "llvm/Support/Format.h"
3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include <cstdio>
323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include <ctime>
333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosusing namespace clang;
343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
359818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios KyrtzidisMacroDirective *
369818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios KyrtzidisPreprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
374d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko  assert(II->hadMacroDefinition() && "Identifier has not been not a macro!");
383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  macro_iterator Pos = Macros.find(II);
403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  assert(Pos != Macros.end() && "Identifier macro info is missing!");
413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return Pos->second;
423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
44c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidisvoid Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){
459317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  assert(MD && "MacroDirective should be non-zero!");
46c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  assert(!MD->getPrevious() && "Already attached to a MacroDirective history.");
476c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor
489818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  MacroDirective *&StoredMD = Macros[II];
499818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  MD->setPrevious(StoredMD);
509818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis  StoredMD = MD;
51c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  II->setHasMacroDefinition(MD->isDefined());
52c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  bool isImportedMacro = isa<DefMacroDirective>(MD) &&
53c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis                         cast<DefMacroDirective>(MD)->isImported();
54c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  if (II->isFromAST() && !isImportedMacro)
553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    II->setChangedSinceDeserialization();
569317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis}
57c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis
589317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidisvoid Preprocessor::setLoadedMacroDirective(IdentifierInfo *II,
599317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis                                           MacroDirective *MD) {
609317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  assert(II && MD);
619317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  MacroDirective *&StoredMD = Macros[II];
629317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  assert(!StoredMD &&
639317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis         "the macro history was modified before initializing it from a pch");
649317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  StoredMD = MD;
659317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  // Setup the identifier as having associated macro history.
669317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  II->setHasMacroDefinition(true);
679317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis  if (!MD->isDefined())
689317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis    II->setHasMacroDefinition(false);
693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// RegisterBuiltinMacro - Register the specified identifier in the identifier
723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// table and mark it as a builtin macro to be expanded.
733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){
743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Get the identifier.
753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  IdentifierInfo *Id = PP.getIdentifierInfo(Name);
763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Mark it as being a macro that is builtin.
783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation());
793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MI->setIsBuiltinMacro();
80c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis  PP.appendDefMacroDirective(Id, MI);
813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return Id;
823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// identifier table.
873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::RegisterBuiltinMacros() {
883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__");
893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__");
903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__");
913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__");
923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__");
933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident_Pragma  = RegisterBuiltinMacro(*this, "_Pragma");
943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // GCC Extensions.
963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__BASE_FILE__     = RegisterBuiltinMacro(*this, "__BASE_FILE__");
973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__");
983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__TIMESTAMP__     = RegisterBuiltinMacro(*this, "__TIMESTAMP__");
993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Clang Extensions.
1013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
1023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_extension    = RegisterBuiltinMacro(*this, "__has_extension");
1033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
1043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
1053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
1063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
1073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");
1083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
109b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Modules.
110b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  if (LangOpts.Modules) {
111b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    Ident__building_module  = RegisterBuiltinMacro(*this, "__building_module");
112b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
113b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    // __MODULE__
114b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    if (!LangOpts.CurrentModule.empty())
115b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor      Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__");
116b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    else
117b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor      Ident__MODULE__ = 0;
118b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  } else {
119b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    Ident__building_module = 0;
120b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    Ident__MODULE__ = 0;
121b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  }
122b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Microsoft Extensions.
1243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (LangOpts.MicrosoftExt)
1253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Ident__pragma = RegisterBuiltinMacro(*this, "__pragma");
1263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  else
1273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Ident__pragma = 0;
1283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
1293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
1313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// in its expansion, currently expands to that token literally.
1323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
1333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                          const IdentifierInfo *MacroIdent,
1343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                          Preprocessor &PP) {
1353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
1363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If the token isn't an identifier, it's always literally expanded.
1383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (II == 0) return true;
1393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If the information about this identifier is out of date, update it from
1413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // the external source.
1423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (II->isOutOfDate())
1433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PP.getExternalSource()->updateOutOfDateIdentifier(*II);
1443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If the identifier is a macro, and if that macro is enabled, it may be
1463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // expanded so it's not a trivial expansion.
1473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
1483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Fast expanding "#define X X" is ok, because X would be disabled.
1493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      II != MacroIdent)
1503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
1513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this is an object-like macro invocation, it is safe to trivially expand
1533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // it.
1543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (MI->isObjectLike()) return true;
1553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this is a function-like macro invocation, it's safe to trivially expand
1573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // as long as the identifier is not a macro argument.
1583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos       I != E; ++I)
1603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (*I == II)
1613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      return false;   // Identifier is a macro argument.
1623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return true;
1643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
1653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
1683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// lexed is a '('.  If so, consume the token and return true, if not, this
1693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// method should have no observable side-effect on the lexed tokens.
1703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosbool Preprocessor::isNextPPTokenLParen() {
1713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Do some quick tests for rejection cases.
1723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  unsigned Val;
1733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (CurLexer)
1743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Val = CurLexer->isNextPPTokenLParen();
1753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  else if (CurPTHLexer)
1763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Val = CurPTHLexer->isNextPPTokenLParen();
1773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  else
1783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Val = CurTokenLexer->isNextTokenLParen();
1793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Val == 2) {
1813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // We have run off the end.  If it's a source file we don't
1823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // examine enclosing ones (C99 5.1.1.2p4).  Otherwise walk up the
1833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // macro stack.
1843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (CurPPLexer)
1853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      return false;
1863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
1873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      IncludeStackInfo &Entry = IncludeMacroStack[i-1];
1883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (Entry.TheLexer)
1893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        Val = Entry.TheLexer->isNextPPTokenLParen();
1903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      else if (Entry.ThePTHLexer)
1913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        Val = Entry.ThePTHLexer->isNextPPTokenLParen();
1923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      else
1933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        Val = Entry.TheTokenLexer->isNextTokenLParen();
1943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (Val != 2)
1963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        break;
1973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Ran off the end of a source file?
1993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (Entry.ThePPLexer)
2003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        return false;
2013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
2023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
2033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Okay, if we know that the token is a '(', lex it and return.  Otherwise we
2053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // have found something that isn't a '(' or we found the end of the
2063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // translation unit.  In either case, return false.
2073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return Val == 1;
2083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
2093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
2113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// expanded as a macro, handle it and return the next token as 'Identifier'.
2123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosbool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
213c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis                                                 MacroDirective *MD) {
21435803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  MacroDirective::DefInfo Def = MD->getDefinition();
21535803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  assert(Def.isValid());
21635803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  MacroInfo *MI = Def.getMacroInfo();
217c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis
2183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this is a macro expansion in the "#if !defined(x)" line for the file,
2193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // then the macro could expand to different things in other contexts, we need
2203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // to disable the optimization in this case.
2213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
2223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
2243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (MI->isBuiltinMacro()) {
225c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis    if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
226dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis                                           Identifier.getLocation(),/*Args=*/0);
2273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ExpandBuiltinMacro(Identifier);
2283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
2293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
2303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  /// Args - If this is a function-like macro expansion, this contains,
2323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  /// for each macro argument, the list of tokens that were provided to the
2333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  /// invocation.
2343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MacroArgs *Args = 0;
2353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Remember where the end of the expansion occurred.  For an object-like
2373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // macro, this is the identifier.  For a function-like macro, this is the ')'.
2383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SourceLocation ExpansionEnd = Identifier.getLocation();
2393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this is a function-like macro, read the arguments.
2413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (MI->isFunctionLike()) {
2423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // C99 6.10.3p10: If the preprocessing token immediately after the macro
2433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // name isn't a '(', this macro should not be expanded.
2443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (!isNextPPTokenLParen())
2453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      return true;
2463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Remember that we are now parsing the arguments to a macro invocation.
2483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Preprocessor directives used inside macro arguments are not portable, and
2493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // this enables the warning.
2503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    InMacroArgs = true;
2513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd);
2523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Finished parsing args.
2543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    InMacroArgs = false;
2553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If there was an error parsing the arguments, bail out.
2573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (Args == 0) return false;
2583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ++NumFnMacroExpanded;
2603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else {
2613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ++NumMacroExpanded;
2623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
2633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Notice that this macro has been used.
2653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  markMacroAsUsed(MI);
2663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Remember where the token is expanded.
2683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SourceLocation ExpandLoc = Identifier.getLocation();
2693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SourceRange ExpansionRange(ExpandLoc, ExpansionEnd);
2703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
2713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Callbacks) {
2723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (InMacroArgs) {
2733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // We can have macro expansion inside a conditional directive while
2743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // reading the function macro arguments. To ensure, in that case, that
2753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // MacroExpands callbacks still happen in source order, queue this
2763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // callback to have it happen after the function macro callback.
2773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      DelayedMacroExpandsCallbacks.push_back(
278c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis                              MacroExpandsInfo(Identifier, MD, ExpansionRange));
2793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    } else {
280dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis      Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args);
2813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (!DelayedMacroExpandsCallbacks.empty()) {
2823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
2833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
284dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis          // FIXME: We lose macro args info with delayed callback.
285dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis          Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, /*Args=*/0);
2863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        }
2873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        DelayedMacroExpandsCallbacks.clear();
2883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      }
2893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
2903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
291e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor
292e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor  // If the macro definition is ambiguous, complain.
29335803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis  if (Def.getDirective()->isAmbiguous()) {
294e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor    Diag(Identifier, diag::warn_pp_ambiguous_macro)
295e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor      << Identifier.getIdentifierInfo();
296e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor    Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen)
297e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor      << Identifier.getIdentifierInfo();
29835803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis    for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition();
29935803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis         PrevDef && !PrevDef.isUndefined();
30035803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis         PrevDef = PrevDef.getPreviousDefinition()) {
30135803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis      if (PrevDef.getDirective()->isAmbiguous()) {
30235803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis        Diag(PrevDef.getMacroInfo()->getDefinitionLoc(),
30335803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis             diag::note_pp_ambiguous_macro_other)
304e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor          << Identifier.getIdentifierInfo();
305e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor      }
306e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor    }
307e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor  }
308e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor
3093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If we started lexing a macro, enter the macro expansion body.
3103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this macro expands to no tokens, don't bother to push it onto the
3123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // expansion stack, only to take it right back off.
3133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (MI->getNumTokens() == 0) {
3143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // No need for arg info.
3153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (Args) Args->destroy(*this);
3163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Ignore this macro use, just return the next token in the current
3183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // buffer.
3193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool HadLeadingSpace = Identifier.hasLeadingSpace();
3203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool IsAtStartOfLine = Identifier.isAtStartOfLine();
3213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Lex(Identifier);
3233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If the identifier isn't on some OTHER line, inherit the leading
3253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // whitespace/first-on-a-line property of this token.  This handles
3263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // stuff like "! XX," -> "! ," and "   XX," -> "    ,", when XX is
3273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // empty.
3283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (!Identifier.isAtStartOfLine()) {
3293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
3303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
3313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
3323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Identifier.setFlag(Token::LeadingEmptyMacro);
3333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ++NumFastMacroExpanded;
3343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
3353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (MI->getNumTokens() == 1 &&
3373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos             isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
3383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                           *this)) {
3393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Otherwise, if this macro expands into a single trivially-expanded
3403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // token: expand it now.  This handles common cases like
3413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // "#define VAL 42".
3423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // No need for arg info.
3443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (Args) Args->destroy(*this);
3453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
3473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // identifier to the expanded token.
3483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool isAtStartOfLine = Identifier.isAtStartOfLine();
3493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool hasLeadingSpace = Identifier.hasLeadingSpace();
3503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Replace the result token.
3523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Identifier = MI->getReplacementToken(0);
3533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Restore the StartOfLine/LeadingSpace markers.
3553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
3563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
3573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Update the tokens location to include both its expansion and physical
3593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // locations.
3603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SourceLocation Loc =
3613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc,
3623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                   ExpansionEnd,Identifier.getLength());
3633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Identifier.setLocation(Loc);
3643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If this is a disabled macro or #define X X, we must mark the result as
3663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // unexpandable.
3673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) {
3683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (MacroInfo *NewMI = getMacroInfo(NewII))
3693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (!NewMI->isEnabled() || NewMI == MI) {
3703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          Identifier.setFlag(Token::DisableExpand);
37140f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor          // Don't warn for "#define X X" like "#define bool bool" from
37240f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor          // stdbool.h.
37340f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor          if (NewMI != MI || MI->isFunctionLike())
37440f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor            Diag(Identifier, diag::pp_disabled_macro_expansion);
3753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        }
3763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
3773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Since this is not an identifier token, it can't be macro expanded, so
3793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // we're done.
3803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ++NumFastMacroExpanded;
3813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
3823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
3833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Start expanding the macro.
3853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  EnterMacro(Identifier, ExpansionEnd, MI, Args);
3863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Now that the macro is at the top of the include stack, ask the
3883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // preprocessor to read the next token from it.
3893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Lex(Identifier);
3903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return false;
3913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
3923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
3935940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieuenum Bracket {
3945940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  Brace,
3955940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  Paren
3965940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu};
3975940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
3985940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// CheckMatchedBrackets - Returns true if the braces and parentheses in the
3995940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// token vector are properly nested.
4005940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieustatic bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) {
4015940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  SmallVector<Bracket, 8> Brackets;
4025940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(),
4035940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                              E = Tokens.end();
4045940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu       I != E; ++I) {
4055940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    if (I->is(tok::l_paren)) {
4065940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      Brackets.push_back(Paren);
4075940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    } else if (I->is(tok::r_paren)) {
4085940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (Brackets.empty() || Brackets.back() == Brace)
4095940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        return false;
4105940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      Brackets.pop_back();
4115940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    } else if (I->is(tok::l_brace)) {
4125940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      Brackets.push_back(Brace);
4135940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    } else if (I->is(tok::r_brace)) {
4145940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (Brackets.empty() || Brackets.back() == Paren)
4155940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        return false;
4165940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      Brackets.pop_back();
4175940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    }
4185940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  }
4195940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  if (!Brackets.empty())
4205940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    return false;
4215940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  return true;
4225940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu}
4235940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4245940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new
4255940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// vector of tokens in NewTokens.  The new number of arguments will be placed
4265940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// in NumArgs and the ranges which need to surrounded in parentheses will be
4275940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// in ParenHints.
4285940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// Returns false if the token stream cannot be changed.  If this is because
4295940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// of an initializer list starting a macro argument, the range of those
4305940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// initializer lists will be place in InitLists.
4315940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieustatic bool GenerateNewArgTokens(Preprocessor &PP,
4325940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                 SmallVectorImpl<Token> &OldTokens,
4335940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                 SmallVectorImpl<Token> &NewTokens,
4345940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                 unsigned &NumArgs,
4355940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                 SmallVectorImpl<SourceRange> &ParenHints,
4365940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                 SmallVectorImpl<SourceRange> &InitLists) {
4375940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  if (!CheckMatchedBrackets(OldTokens))
4385940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    return false;
4395940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4405940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // Once it is known that the brackets are matched, only a simple count of the
4415940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // braces is needed.
4425940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  unsigned Braces = 0;
4435940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4445940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // First token of a new macro argument.
4455940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin();
4465940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4475940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // First closing brace in a new macro argument.  Used to generate
4485940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // SourceRanges for InitLists.
4495940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end();
4505940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  NumArgs = 0;
4515940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  Token TempToken;
4525940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // Set to true when a macro separator token is found inside a braced list.
4535940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // If true, the fixed argument spans multiple old arguments and ParenHints
4545940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // will be updated.
4555940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  bool FoundSeparatorToken = false;
4565940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(),
4575940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                        E = OldTokens.end();
4585940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu       I != E; ++I) {
4595940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    if (I->is(tok::l_brace)) {
4605940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      ++Braces;
4615940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    } else if (I->is(tok::r_brace)) {
4625940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      --Braces;
4635940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken)
4645940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        ClosingBrace = I;
4655940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    } else if (I->is(tok::eof)) {
4665940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      // EOF token is used to separate macro arguments
4675940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (Braces != 0) {
4685940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Assume comma separator is actually braced list separator and change
4695940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // it back to a comma.
4705940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        FoundSeparatorToken = true;
4715940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        I->setKind(tok::comma);
4725940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        I->setLength(1);
4735940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      } else { // Braces == 0
4745940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Separator token still separates arguments.
4755940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        ++NumArgs;
4765940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4775940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // If the argument starts with a brace, it can't be fixed with
4785940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // parentheses.  A different diagnostic will be given.
4795940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) {
4805940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          InitLists.push_back(
4815940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu              SourceRange(ArgStartIterator->getLocation(),
4825940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                          PP.getLocForEndOfToken(ClosingBrace->getLocation())));
4835940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          ClosingBrace = E;
4845940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        }
4855940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4865940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Add left paren
4875940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        if (FoundSeparatorToken) {
4885940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.startToken();
4895940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.setKind(tok::l_paren);
4905940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.setLocation(ArgStartIterator->getLocation());
4915940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.setLength(0);
4925940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          NewTokens.push_back(TempToken);
4935940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        }
4945940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4955940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Copy over argument tokens
4965940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        NewTokens.insert(NewTokens.end(), ArgStartIterator, I);
4975940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
4985940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Add right paren and store the paren locations in ParenHints
4995940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        if (FoundSeparatorToken) {
5005940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation());
5015940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.startToken();
5025940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.setKind(tok::r_paren);
5035940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.setLocation(Loc);
5045940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          TempToken.setLength(0);
5055940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          NewTokens.push_back(TempToken);
5065940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(),
5075940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                                           Loc));
5085940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        }
5095940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
5105940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Copy separator token
5115940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        NewTokens.push_back(*I);
5125940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
5135940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        // Reset values
5145940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        ArgStartIterator = I + 1;
5155940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        FoundSeparatorToken = false;
5165940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      }
5175940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    }
5185940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  }
5195940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
5205940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  return !ParenHints.empty() && InitLists.empty();
5215940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu}
5225940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
5233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next
5243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// token is the '(' of the macro, this method is invoked to read all of the
5253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// actual arguments specified for the macro invocation.  This returns null on
5263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// error.
5273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao MatosMacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
5283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                   MacroInfo *MI,
5293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                   SourceLocation &MacroEnd) {
5303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // The number of fixed arguments to parse.
5313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  unsigned NumFixedArgsLeft = MI->getNumArgs();
5323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  bool isVariadic = MI->isVariadic();
5333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Outer loop, while there are more arguments, keep reading them.
5353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Token Tok;
5363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
5383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // an argument value in a macro could expand to ',' or '(' or ')'.
5393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  LexUnexpandedToken(Tok);
5403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
5413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // ArgTokens - Build up a list of tokens that make up each argument.  Each
5433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // argument is separated by an EOF token.  Use a SmallVector so we can avoid
5443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // heap allocations in the common case.
5453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SmallVector<Token, 64> ArgTokens;
546cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis  bool ContainsCodeCompletionTok = false;
5473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5485940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  SourceLocation TooManyArgsLoc;
5495940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
5503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  unsigned NumActuals = 0;
5513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  while (Tok.isNot(tok::r_paren)) {
552cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis    if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod)))
553cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis      break;
554cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis
5553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) &&
5563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           "only expect argument separators here");
5573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    unsigned ArgTokenStart = ArgTokens.size();
5593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SourceLocation ArgStartLoc = Tok.getLocation();
5603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // C99 6.10.3p11: Keep track of the number of l_parens we have seen.  Note
5623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // that we already consumed the first one.
5633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    unsigned NumParens = 0;
5643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    while (1) {
5663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Read arguments as unexpanded tokens.  This avoids issues, e.g., where
5673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // an argument value in a macro could expand to ',' or '(' or ')'.
5683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      LexUnexpandedToken(Tok);
5693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
5703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n"
571cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis        if (!ContainsCodeCompletionTok) {
572cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis          Diag(MacroName, diag::err_unterm_macro_invoc);
573cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis          Diag(MI->getDefinitionLoc(), diag::note_macro_here)
574cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis            << MacroName.getIdentifierInfo();
575cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis          // Do not lose the EOF/EOD.  Return it to the client.
576cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis          MacroName = Tok;
577cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis          return 0;
578cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis        } else {
579bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis          // Do not lose the EOF/EOD.
580bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis          Token *Toks = new Token[1];
581bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis          Toks[0] = Tok;
582bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis          EnterTokenStream(Toks, 1, true, true);
583cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis          break;
584cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis        }
5853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      } else if (Tok.is(tok::r_paren)) {
5863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // If we found the ) token, the macro arg list is done.
5873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (NumParens-- == 0) {
5883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          MacroEnd = Tok.getLocation();
5893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          break;
5903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        }
5913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      } else if (Tok.is(tok::l_paren)) {
5923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        ++NumParens;
59311be06464a338b7c6e007898a7709498dc456db2Reid Kleckner      } else if (Tok.is(tok::comma) && NumParens == 0 &&
59411be06464a338b7c6e007898a7709498dc456db2Reid Kleckner                 !(Tok.getFlags() & Token::IgnoredComma)) {
59511be06464a338b7c6e007898a7709498dc456db2Reid Kleckner        // In Microsoft-compatibility mode, single commas from nested macro
59611be06464a338b7c6e007898a7709498dc456db2Reid Kleckner        // expansions should not be considered as argument separators. We test
59711be06464a338b7c6e007898a7709498dc456db2Reid Kleckner        // for this with the IgnoredComma token flag above.
59811be06464a338b7c6e007898a7709498dc456db2Reid Kleckner
5993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // Comma ends this argument if there are more fixed arguments expected.
6003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // However, if this is a variadic macro, and this is part of the
6013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // variadic part, then the comma is just an argument token.
6023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (!isVariadic) break;
6033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (NumFixedArgsLeft > 1)
6043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          break;
6053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      } else if (Tok.is(tok::comment) && !KeepMacroComments) {
6063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // If this is a comment token in the argument list and we're just in
6073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // -C mode (not -CC mode), discard the comment.
6083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        continue;
6093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      } else if (Tok.getIdentifierInfo() != 0) {
6103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // Reading macro arguments can cause macros that we are currently
6113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // expanding from to be popped off the expansion stack.  Doing so causes
6123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // them to be reenabled for expansion.  Here we record whether any
6133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // identifiers we lex as macro arguments correspond to disabled macros.
6143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // If so, we mark the token as noexpand.  This is a subtle aspect of
6153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // C99 6.10.3.4p2.
6163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
6173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          if (!MI->isEnabled())
6183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos            Tok.setFlag(Token::DisableExpand);
6193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      } else if (Tok.is(tok::code_completion)) {
620cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis        ContainsCodeCompletionTok = true;
6213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (CodeComplete)
6223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(),
6233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                  MI, NumActuals);
6243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // Don't mark that we reached the code-completion point because the
6253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // parser is going to handle the token and there will be another
6263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // code-completion callback.
6273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      }
6283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      ArgTokens.push_back(Tok);
6303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
6313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If this was an empty argument list foo(), don't add this as an empty
6333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // argument.
6343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (ArgTokens.empty() && Tok.getKind() == tok::r_paren)
6353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      break;
6363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If this is not a variadic macro, and too many args were specified, emit
6383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // an error.
6395940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) {
6403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      if (ArgTokens.size() != ArgTokenStart)
6415940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation();
6425940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      else
6435940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        TooManyArgsLoc = ArgStartLoc;
6443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
6453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6465940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // Empty arguments are standard in C99 and C++0x, and are supported as an
6475940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // extension in other modes.
6483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99)
64980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith      Diag(Tok, LangOpts.CPlusPlus11 ?
6503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           diag::warn_cxx98_compat_empty_fnmacro_arg :
6513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           diag::ext_empty_fnmacro_arg);
6523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Add a marker EOF token to the end of the token list for this argument.
6543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Token EOFTok;
6553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    EOFTok.startToken();
6563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    EOFTok.setKind(tok::eof);
6573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    EOFTok.setLocation(Tok.getLocation());
6583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    EOFTok.setLength(0);
6593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ArgTokens.push_back(EOFTok);
6603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ++NumActuals;
6615940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0)
662fdf5706440543c744cd9faf55235a757a7278daaArgyrios Kyrtzidis      --NumFixedArgsLeft;
6633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
6643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Okay, we either found the r_paren.  Check to see if we parsed too few
6663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // arguments.
6673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  unsigned MinArgsExpected = MI->getNumArgs();
6683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
6695940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // If this is not a variadic macro, and too many args were specified, emit
6705940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  // an error.
6715940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  if (!isVariadic && NumActuals > MinArgsExpected &&
6725940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      !ContainsCodeCompletionTok) {
6735940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // Emit the diagnostic at the macro name in case there is a missing ).
6745940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // Emitting it at the , could be far away from the macro name.
6755940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc);
6765940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    Diag(MI->getDefinitionLoc(), diag::note_macro_here)
6775940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      << MacroName.getIdentifierInfo();
6785940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
6795940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // Commas from braced initializer lists will be treated as argument
6805940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // separators inside macros.  Attempt to correct for this with parentheses.
6815940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // TODO: See if this can be generalized to angle brackets for templates
6825940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    // inside macro arguments.
6835940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
68440ec4d26b7622eb07ad8d5f02da0dd43a1dc74b5Bob Wilson    SmallVector<Token, 4> FixedArgTokens;
6855940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    unsigned FixedNumArgs = 0;
6865940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    SmallVector<SourceRange, 4> ParenHints, InitLists;
6875940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs,
6885940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                              ParenHints, InitLists)) {
6895940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (!InitLists.empty()) {
6905940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        DiagnosticBuilder DB =
6915940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu            Diag(MacroName,
6925940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                 diag::note_init_list_at_beginning_of_macro_argument);
6935940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        for (SmallVector<SourceRange, 4>::iterator
6945940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                 Range = InitLists.begin(), RangeEnd = InitLists.end();
6955940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu                 Range != RangeEnd; ++Range) {
6965940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          if (DB.hasMaxRanges())
6975940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu            break;
6985940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu          DB << *Range;
6995940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        }
7005940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      }
7015940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      return 0;
7025940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    }
7035940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    if (FixedNumArgs != MinArgsExpected)
7045940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      return 0;
7055940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
7065940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro);
7075940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    for (SmallVector<SourceRange, 4>::iterator
7085940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu             ParenLocation = ParenHints.begin(), ParenEnd = ParenHints.end();
7095940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu         ParenLocation != ParenEnd; ++ParenLocation) {
7105940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (DB.hasMaxFixItHints())
7115940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        break;
7125940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      DB << FixItHint::CreateInsertion(ParenLocation->getBegin(), "(");
7135940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      if (DB.hasMaxFixItHints())
7145940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu        break;
7155940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu      DB << FixItHint::CreateInsertion(ParenLocation->getEnd(), ")");
7165940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    }
7175940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    ArgTokens.swap(FixedArgTokens);
7185940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu    NumActuals = FixedNumArgs;
7195940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu  }
7205940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu
7213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // See MacroArgs instance var for description of this.
7223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  bool isVarargsElided = false;
7233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
724f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis  if (ContainsCodeCompletionTok) {
725f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    // Recover from not-fully-formed macro invocation during code-completion.
726f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    Token EOFTok;
727f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    EOFTok.startToken();
728f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    EOFTok.setKind(tok::eof);
729f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    EOFTok.setLocation(Tok.getLocation());
730f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    EOFTok.setLength(0);
731f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis    for (; NumActuals < MinArgsExpected; ++NumActuals)
732f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis      ArgTokens.push_back(EOFTok);
733f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis  }
734f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis
7353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (NumActuals < MinArgsExpected) {
7363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // There are several cases where too few arguments is ok, handle them now.
7373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (NumActuals == 0 && MinArgsExpected == 1) {
7383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // #define A(X)  or  #define A(...)   ---> A()
7393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
7403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // If there is exactly one argument, and that argument is missing,
7413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // then we have an empty "()" argument empty list.  This is fine, even if
7423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // the macro expects one argument (the argument is just empty).
7433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      isVarargsElided = MI->isVariadic();
7443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    } else if (MI->isVariadic() &&
7453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos               (NumActuals+1 == MinArgsExpected ||  // A(x, ...) -> A(X)
7463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A()
7473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Varargs where the named vararg parameter is missing: OK as extension.
7483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      //   #define A(x, ...)
7493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      //   A("blah")
7504fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman      //
7514fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman      // If the macro contains the comma pasting extension, the diagnostic
7524fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman      // is suppressed; we know we'll get another diagnostic later.
7534fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman      if (!MI->hasCommaPasting()) {
7544fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman        Diag(Tok, diag::ext_missing_varargs_arg);
7554fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman        Diag(MI->getDefinitionLoc(), diag::note_macro_here)
7564fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman          << MacroName.getIdentifierInfo();
7574fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman      }
7583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
7593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Remember this occurred, allowing us to elide the comma when used for
7603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // cases like:
7613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      //   #define A(x, foo...) blah(a, ## foo)
7623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      //   #define B(x, ...) blah(a, ## __VA_ARGS__)
7633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      //   #define C(...) blah(a, ## __VA_ARGS__)
7643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      //  A(x) B(x) C()
7653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      isVarargsElided = true;
766cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis    } else if (!ContainsCodeCompletionTok) {
7673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Otherwise, emit the error.
7683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Diag(Tok, diag::err_too_few_args_in_macro_invoc);
7690ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis      Diag(MI->getDefinitionLoc(), diag::note_macro_here)
7700ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis        << MacroName.getIdentifierInfo();
7713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      return 0;
7723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
7733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
7743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Add a marker EOF token to the end of the token list for this argument.
7753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SourceLocation EndLoc = Tok.getLocation();
7763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.startToken();
7773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::eof);
7783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setLocation(EndLoc);
7793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setLength(0);
7803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ArgTokens.push_back(Tok);
7813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
7823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If we expect two arguments, add both as empty.
7833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (NumActuals == 0 && MinArgsExpected == 2)
7843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      ArgTokens.push_back(Tok);
7853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
786cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis  } else if (NumActuals > MinArgsExpected && !MI->isVariadic() &&
787cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis             !ContainsCodeCompletionTok) {
7883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Emit the diagnostic at the macro name in case there is a missing ).
7893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Emitting it at the , could be far away from the macro name.
7903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
7910ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis    Diag(MI->getDefinitionLoc(), diag::note_macro_here)
7920ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis      << MacroName.getIdentifierInfo();
7933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return 0;
7943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
7953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
7963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this);
7973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
7983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
7993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// \brief Keeps macro expanded tokens for TokenLexers.
8003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//
8013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Works like a stack; a TokenLexer adds the macro expanded tokens that is
8023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// going to lex in the cache and when it finishes the tokens are removed
8033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// from the end of the cache.
8043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao MatosToken *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer,
8053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                              ArrayRef<Token> tokens) {
8063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  assert(tokLexer);
8073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (tokens.empty())
8083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return 0;
8093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  size_t newIndex = MacroExpandedTokens.size();
8113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  bool cacheNeedsToGrow = tokens.size() >
8123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                      MacroExpandedTokens.capacity()-MacroExpandedTokens.size();
8133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MacroExpandedTokens.append(tokens.begin(), tokens.end());
8143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (cacheNeedsToGrow) {
8163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Go through all the TokenLexers whose 'Tokens' pointer points in the
8173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // buffer and update the pointers to the (potential) new buffer array.
8183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) {
8193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      TokenLexer *prevLexer;
8203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      size_t tokIndex;
8213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      llvm::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i];
8223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex;
8233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
8243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
8253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex));
8273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return MacroExpandedTokens.data() + newIndex;
8283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
8293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() {
8313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  assert(!MacroExpandingLexersStack.empty());
8323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  size_t tokIndex = MacroExpandingLexersStack.back().second;
8333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  assert(tokIndex < MacroExpandedTokens.size());
8343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Pop the cached macro expanded tokens from the end.
8353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MacroExpandedTokens.resize(tokIndex);
8363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MacroExpandingLexersStack.pop_back();
8373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
8383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// ComputeDATE_TIME - Compute the current time, enter it into the specified
8403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
8413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// the identifier tokens inserted.
8423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
8433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                             Preprocessor &PP) {
8443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  time_t TT = time(0);
8453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  struct tm *TM = localtime(&TT);
8463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  static const char * const Months[] = {
8483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
8493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  };
8503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
85133d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko  {
85233d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    SmallString<32> TmpBuffer;
85333d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    llvm::raw_svector_ostream TmpStream(TmpBuffer);
85433d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon],
85533d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko                              TM->tm_mday, TM->tm_year + 1900);
85633d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    Token TmpTok;
85733d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    TmpTok.startToken();
858374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko    PP.CreateString(TmpStream.str(), TmpTok);
85933d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    DATELoc = TmpTok.getLocation();
86033d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko  }
86133d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko
86233d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko  {
86333d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    SmallString<32> TmpBuffer;
86433d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    llvm::raw_svector_ostream TmpStream(TmpBuffer);
86533d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    TmpStream << llvm::format("\"%02d:%02d:%02d\"",
86633d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko                              TM->tm_hour, TM->tm_min, TM->tm_sec);
86733d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    Token TmpTok;
86833d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    TmpTok.startToken();
869374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko    PP.CreateString(TmpStream.str(), TmpTok);
87033d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko    TIMELoc = TmpTok.getLocation();
87133d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko  }
8723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
8733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HasFeature - Return true if we recognize and implement the feature
8763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// specified by the identifier as a standard language feature.
8773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
8783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  const LangOptions &LangOpts = PP.getLangOpts();
8793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  StringRef Feature = II->getName();
8803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Normalize the feature name, __foo__ becomes foo.
8823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
8833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Feature = Feature.substr(2, Feature.size() - 4);
8843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
8853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return llvm::StringSwitch<bool>(Feature)
8864f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz           .Case("address_sanitizer", LangOpts.Sanitize.Address)
8873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_analyzer_noreturn", true)
8883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_availability", true)
8893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_availability_with_message", true)
8903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_cf_returns_not_retained", true)
8913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_cf_returns_retained", true)
8923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_deprecated_with_message", true)
8933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_ext_vector_type", true)
8943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_ns_returns_not_retained", true)
8953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_ns_returns_retained", true)
8963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_ns_consumes_self", true)
8973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_ns_consumed", true)
8983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_cf_consumed", true)
8993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_objc_ivar_unused", true)
9003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_objc_method_family", true)
9013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_overloadable", true)
9023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_unavailable_with_message", true)
9033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("attribute_unused_on_fields", true)
9043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("blocks", LangOpts.Blocks)
90588c4b5efc604b982ca9083548d568b02b1abd2abDavid Blaikie           .Case("c_thread_safety_attributes", true)
9063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_exceptions", LangOpts.Exceptions)
9073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_rtti", LangOpts.RTTI)
9083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("enumerator_attributes", true)
9094f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz           .Case("memory_sanitizer", LangOpts.Sanitize.Memory)
9104f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz           .Case("thread_sanitizer", LangOpts.Sanitize.Thread)
9113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           // Objective-C features
9123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
9133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_arc", LangOpts.ObjCAutoRefCount)
9143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_arc_weak", LangOpts.ObjCARCWeak)
9153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_default_synthesize_properties", LangOpts.ObjC2)
9163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_fixed_enum", LangOpts.ObjC2)
9173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_instancetype", LangOpts.ObjC2)
9183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules)
9193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile())
920e4057c203c71873d93aead38d626b3cd29584d8aTed Kremenek           .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword?
9213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport())
92211311eaf6093a5374f94047df354d4c62d68c611Eli Friedman           .Case("objc_msg_lookup_stret", LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW)
9233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("ownership_holds", true)
9243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("ownership_returns", true)
9253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("ownership_takes", true)
9263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_bool", true)
9273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile())
9283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_array_literals", LangOpts.ObjC2)
9293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_dictionary_literals", LangOpts.ObjC2)
9303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("objc_boxed_expressions", LangOpts.ObjC2)
9313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("arc_cf_code_audited", true)
9323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           // C11 features
9333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_alignas", LangOpts.C11)
9343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_atomic", LangOpts.C11)
9353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_generic_selections", LangOpts.C11)
9363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_static_assert", LangOpts.C11)
937e87c5bd593b038baec7cfa6075f26cd0ac256042Douglas Gregor           .Case("c_thread_local",
938e87c5bd593b038baec7cfa6075f26cd0ac256042Douglas Gregor                 LangOpts.C11 && PP.getTargetInfo().isTLSSupported())
9393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           // C++11 features
94080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11)
94180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_alias_templates", LangOpts.CPlusPlus11)
94280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_alignas", LangOpts.CPlusPlus11)
94380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_atomic", LangOpts.CPlusPlus11)
94480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_attributes", LangOpts.CPlusPlus11)
94580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_auto_type", LangOpts.CPlusPlus11)
94680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_constexpr", LangOpts.CPlusPlus11)
94780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_decltype", LangOpts.CPlusPlus11)
94880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11)
94980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11)
95080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11)
95180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11)
95280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_deleted_functions", LangOpts.CPlusPlus11)
95380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11)
95480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11)
95580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_implicit_moves", LangOpts.CPlusPlus11)
956e6e68b53778bb5a15c10a73a5bf18d8ab73f75e3Richard Smith           .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11)
95780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11)
95880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_lambdas", LangOpts.CPlusPlus11)
95980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11)
96080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11)
96180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_noexcept", LangOpts.CPlusPlus11)
96280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_nullptr", LangOpts.CPlusPlus11)
96380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_override_control", LangOpts.CPlusPlus11)
96480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_range_for", LangOpts.CPlusPlus11)
96580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11)
96680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11)
96780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_rvalue_references", LangOpts.CPlusPlus11)
96880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_strong_enums", LangOpts.CPlusPlus11)
96980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_static_assert", LangOpts.CPlusPlus11)
970e87c5bd593b038baec7cfa6075f26cd0ac256042Douglas Gregor           .Case("cxx_thread_local",
971e87c5bd593b038baec7cfa6075f26cd0ac256042Douglas Gregor                 LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
97280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_trailing_return", LangOpts.CPlusPlus11)
97380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_unicode_literals", LangOpts.CPlusPlus11)
97480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11)
97580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_user_literals", LangOpts.CPlusPlus11)
97680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith           .Case("cxx_variadic_templates", LangOpts.CPlusPlus11)
9777f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           // C++1y features
9783c3a522712ef11a5e5836e2df6466fbd446bb2eaRichard Smith           .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y)
9797f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           .Case("cxx_binary_literals", LangOpts.CPlusPlus1y)
980a4fb3390853356524416bc28d6044e7a41c87863Richard Smith           .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y)
9817f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           //.Case("cxx_generic_lambda", LangOpts.CPlusPlus1y)
9823c3a522712ef11a5e5836e2df6466fbd446bb2eaRichard Smith           //.Case("cxx_init_capture", LangOpts.CPlusPlus1y)
983a4fb3390853356524416bc28d6044e7a41c87863Richard Smith           .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y)
984f45c2992a3aac7591310cd824b7c7319afd432fcRichard Smith           .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y)
9857f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           //.Case("cxx_runtime_array", LangOpts.CPlusPlus1y)
9867f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           //.Case("cxx_variable_templates", LangOpts.CPlusPlus1y)
9873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           // Type traits
9883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_nothrow_assign", LangOpts.CPlusPlus)
9893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_nothrow_copy", LangOpts.CPlusPlus)
9903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_nothrow_constructor", LangOpts.CPlusPlus)
9913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_trivial_assign", LangOpts.CPlusPlus)
9923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_trivial_copy", LangOpts.CPlusPlus)
9933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_trivial_constructor", LangOpts.CPlusPlus)
9943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_trivial_destructor", LangOpts.CPlusPlus)
9953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("has_virtual_destructor", LangOpts.CPlusPlus)
9963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_abstract", LangOpts.CPlusPlus)
9973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_base_of", LangOpts.CPlusPlus)
9983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_class", LangOpts.CPlusPlus)
9993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_convertible_to", LangOpts.CPlusPlus)
10003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_empty", LangOpts.CPlusPlus)
10013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_enum", LangOpts.CPlusPlus)
10023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_final", LangOpts.CPlusPlus)
10033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_literal", LangOpts.CPlusPlus)
10043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_standard_layout", LangOpts.CPlusPlus)
10053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_pod", LangOpts.CPlusPlus)
10063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_polymorphic", LangOpts.CPlusPlus)
10073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_trivial", LangOpts.CPlusPlus)
10083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_trivially_assignable", LangOpts.CPlusPlus)
10093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_trivially_constructible", LangOpts.CPlusPlus)
10103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_trivially_copyable", LangOpts.CPlusPlus)
10113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("is_union", LangOpts.CPlusPlus)
10123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("modules", LangOpts.Modules)
10133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("tls", PP.getTargetInfo().isTLSSupported())
10143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("underlying_type", LangOpts.CPlusPlus)
10153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Default(false);
10163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
10173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HasExtension - Return true if we recognize and implement the feature
10193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// specified by the identifier, either as an extension or a standard language
10203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// feature.
10213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
10223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (HasFeature(PP, II))
10233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return true;
10243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If the use of an extension results in an error diagnostic, extensions are
10263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // effectively unavailable, so just return false here.
10273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (PP.getDiagnostics().getExtensionHandlingBehavior() ==
10283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      DiagnosticsEngine::Ext_Error)
10293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
10303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  const LangOptions &LangOpts = PP.getLangOpts();
10323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  StringRef Extension = II->getName();
10333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Normalize the extension name, __foo__ becomes foo.
10353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Extension.startswith("__") && Extension.endswith("__") &&
10363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Extension.size() >= 4)
10373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Extension = Extension.substr(2, Extension.size() - 4);
10383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Because we inherit the feature list from HasFeature, this string switch
10403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // must be less restrictive than HasFeature's.
10413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return llvm::StringSwitch<bool>(Extension)
10423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           // C11 features supported by other languages as extensions.
10433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_alignas", true)
10443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_atomic", true)
10453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_generic_selections", true)
10463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("c_static_assert", true)
10477f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           // C++11 features supported by other languages as extensions.
10483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_atomic", LangOpts.CPlusPlus)
10493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_deleted_functions", LangOpts.CPlusPlus)
10503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_explicit_conversions", LangOpts.CPlusPlus)
10513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_inline_namespaces", LangOpts.CPlusPlus)
10523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_local_type_template_args", LangOpts.CPlusPlus)
10533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus)
10543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_override_control", LangOpts.CPlusPlus)
10553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_range_for", LangOpts.CPlusPlus)
10563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus)
10573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Case("cxx_rvalue_references", LangOpts.CPlusPlus)
10587f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           // C++1y features supported by other languages as extensions.
10597f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith           .Case("cxx_binary_literals", true)
10603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos           .Default(false);
10613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
10623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HasAttribute -  Return true if we recognize and implement the attribute
10643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// specified by the given identifier.
10653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool HasAttribute(const IdentifierInfo *II) {
10663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  StringRef Name = II->getName();
10673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Normalize the attribute name, __foo__ becomes foo.
10683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4)
10693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Name = Name.substr(2, Name.size() - 4);
10703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // FIXME: Do we need to handle namespaces here?
10723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return llvm::StringSwitch<bool>(Name)
10733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Lex/AttrSpellings.inc"
10743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        .Default(false);
10753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
10763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// EvaluateHasIncludeCommon - Process a '__has_include("path")'
10783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// or '__has_include_next("path")' expression.
10793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Returns true if successful.
10803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool EvaluateHasIncludeCommon(Token &Tok,
10813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                     IdentifierInfo *II, Preprocessor &PP,
10823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                     const DirectoryLookup *LookupFrom) {
108397bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu  // Save the location of the current token.  If a '(' is later found, use
10846b716c58565ebd325f3fb989cfa92ed0934db8c5Aaron Ballman  // that location.  If not, use the end of this location instead.
108597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu  SourceLocation LParenLoc = Tok.getLocation();
10863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
108731672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  // These expressions are only allowed within a preprocessor directive.
108831672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  if (!PP.isParsingIfOrElifDirective()) {
108931672b1745195d709b641f1c0f44e203742fa73bAaron Ballman    PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName();
109031672b1745195d709b641f1c0f44e203742fa73bAaron Ballman    return false;
109131672b1745195d709b641f1c0f44e203742fa73bAaron Ballman  }
109231672b1745195d709b641f1c0f44e203742fa73bAaron Ballman
10933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Get '('.
10943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  PP.LexNonComment(Tok);
10953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
10963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Ensure we have a '('.
10973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Tok.isNot(tok::l_paren)) {
109897bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    // No '(', use end of last token.
109997bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    LParenLoc = PP.getLocForEndOfToken(LParenLoc);
110097bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    PP.Diag(LParenLoc, diag::err_pp_missing_lparen) << II->getName();
110197bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    // If the next token looks like a filename or the start of one,
110297bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    // assume it is and process it as such.
110397bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) &&
110497bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu        !Tok.is(tok::less))
110597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu      return false;
110697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu  } else {
110797bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    // Save '(' location for possible missing ')' message.
110897bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    LParenLoc = Tok.getLocation();
11093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1110a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman    if (PP.getCurrentLexer()) {
1111a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman      // Get the file name.
1112a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman      PP.getCurrentLexer()->LexIncludeFilename(Tok);
1113a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman    } else {
1114a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman      // We're in a macro, so we can't use LexIncludeFilename; just
1115a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman      // grab the next token.
1116a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman      PP.Lex(Tok);
1117a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman    }
111897bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu  }
11193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Reserve a buffer to get the spelling.
11213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SmallString<128> FilenameBuffer;
11223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  StringRef Filename;
11233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SourceLocation EndLoc;
11243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  switch (Tok.getKind()) {
11263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  case tok::eod:
11273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // If the token kind is EOD, the error has already been diagnosed.
11283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
11293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  case tok::angle_string_literal:
11313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  case tok::string_literal: {
11323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool Invalid = false;
11333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
11343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (Invalid)
11353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      return false;
11363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    break;
11373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
11383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  case tok::less:
11403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // This could be a <foo/bar.h> file coming from a macro expansion.  In this
11413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // case, glue the tokens together into FilenameBuffer and interpret those.
11423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    FilenameBuffer.push_back('<');
114397bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) {
114497bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu      // Let the caller know a <eod> was found by changing the Token kind.
114597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu      Tok.setKind(tok::eod);
11463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      return false;   // Found <eod> but no ">"?  Diagnostic already emitted.
114797bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    }
11483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Filename = FilenameBuffer.str();
11493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    break;
11503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  default:
11513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename);
11523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
11533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
11543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
115597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu  SourceLocation FilenameLoc = Tok.getLocation();
115697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu
11573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Get ')'.
11583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  PP.LexNonComment(Tok);
11593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Ensure we have a trailing ).
11613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Tok.isNot(tok::r_paren)) {
116297bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_missing_rparen)
116397bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu        << II->getName();
11643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PP.Diag(LParenLoc, diag::note_matching) << "(";
11653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
11663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
11673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
11693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
11703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // error.
11713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (Filename.empty())
11723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return false;
11733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Search include directories.
11753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  const DirectoryLookup *CurDir;
11763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  const FileEntry *File =
1177bc3f628815b3841dc99109e7f67f9afa7793bc94Lawrence Crowl      PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, NULL,
1178bc3f628815b3841dc99109e7f67f9afa7793bc94Lawrence Crowl                    NULL, NULL);
11793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Get the result value.  A result of true means the file exists.
11813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return File != 0;
11823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
11833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// EvaluateHasInclude - Process a '__has_include("path")' expression.
11853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Returns true if successful.
11863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II,
11873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                               Preprocessor &PP) {
11883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return EvaluateHasIncludeCommon(Tok, II, PP, NULL);
11893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
11903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
11913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression.
11923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Returns true if successful.
11933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool EvaluateHasIncludeNext(Token &Tok,
11943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                   IdentifierInfo *II, Preprocessor &PP) {
11953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // __has_include_next is like __has_include, except that we start
11963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // searching after the current found directory.  If we can't do this,
11973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // issue a diagnostic.
11983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  const DirectoryLookup *Lookup = PP.GetCurDirLookup();
11993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (PP.isInPrimaryFile()) {
12003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Lookup = 0;
12013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PP.Diag(Tok, diag::pp_include_next_in_primary);
12023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (Lookup == 0) {
12033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PP.Diag(Tok, diag::pp_include_next_absolute_path);
12043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else {
12053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Start looking up in the next directory.
12063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    ++Lookup;
12073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
12083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  return EvaluateHasIncludeCommon(Tok, II, PP, Lookup);
12103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
12113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
1212b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor/// \brief Process __building_module(identifier) expression.
1213b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor/// \returns true if we are building the named module, false otherwise.
1214b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregorstatic bool EvaluateBuildingModule(Token &Tok,
1215b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor                                   IdentifierInfo *II, Preprocessor &PP) {
1216b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Get '('.
1217b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  PP.LexNonComment(Tok);
1218b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1219b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Ensure we have a '('.
1220b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  if (Tok.isNot(tok::l_paren)) {
1221b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    PP.Diag(Tok.getLocation(), diag::err_pp_missing_lparen) << II->getName();
1222b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    return false;
1223b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  }
1224b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1225b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Save '(' location for possible missing ')' message.
1226b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  SourceLocation LParenLoc = Tok.getLocation();
1227b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1228b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Get the module name.
1229b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  PP.LexNonComment(Tok);
1230b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1231b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Ensure that we have an identifier.
1232b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  if (Tok.isNot(tok::identifier)) {
1233b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module);
1234b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    return false;
1235b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  }
1236b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1237b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  bool Result
1238b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
1239b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1240b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Get ')'.
1241b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  PP.LexNonComment(Tok);
1242b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1243b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  // Ensure we have a trailing ).
1244b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  if (Tok.isNot(tok::r_paren)) {
1245b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    PP.Diag(Tok.getLocation(), diag::err_pp_missing_rparen) << II->getName();
1246b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    PP.Diag(LParenLoc, diag::note_matching) << "(";
1247b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    return false;
1248b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  }
1249b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
1250b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  return Result;
1251b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor}
1252b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor
12533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
12543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// as a builtin macro, handle it and return the next token as 'Tok'.
12553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::ExpandBuiltinMacro(Token &Tok) {
12563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Figure out which token this is.
12573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  IdentifierInfo *II = Tok.getIdentifierInfo();
12583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  assert(II && "Can't be a macro without id info!");
12593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If this is an _Pragma or Microsoft __pragma directive, expand it,
12613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // invoke the pragma handler, then lex the token after it.
12623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (II == Ident_Pragma)
12633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return Handle_Pragma(Tok);
12643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  else if (II == Ident__pragma) // in non-MS mode this is null
12653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return HandleMicrosoft__pragma(Tok);
12663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  ++NumBuiltinMacroExpanded;
12683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  SmallString<128> TmpBuffer;
12703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  llvm::raw_svector_ostream OS(TmpBuffer);
12713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // Set up the return result.
12733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Tok.setIdentifierInfo(0);
12743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  Tok.clearFlag(Token::NeedsCleaning);
12753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (II == Ident__LINE__) {
12773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // C99 6.10.8: "__LINE__: The presumed line number (within the current
12783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // source file) of the current source line (an integer constant)".  This can
12793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // be affected by #line.
12803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SourceLocation Loc = Tok.getLocation();
12813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Advance to the location of the first _, this might not be the first byte
12833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // of the token if it starts with an escaped newline.
12843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Loc = AdvanceToTokenCharacter(Loc, 0);
12853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // One wrinkle here is that GCC expands __LINE__ to location of the *end* of
12873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // a macro expansion.  This doesn't matter for object-like macros, but
12883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // can matter for a function-like macro that expands to contain __LINE__.
12893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Skip down through expansion points until we find a file loc for the
12903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // end of the expansion history.
12913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Loc = SourceMgr.getExpansionRange(Loc).second;
12923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc);
12933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
12943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // __LINE__ expands to a simple numeric value.
12953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << (PLoc.isValid()? PLoc.getLine() : 1);
12963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::numeric_constant);
12973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
12983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // C99 6.10.8: "__FILE__: The presumed name of the current source file (a
12993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // character string literal)". This can be affected by #line.
13003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
13013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // __BASE_FILE__ is a GNU extension that returns the top of the presumed
13033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // #include stack instead of the current file.
13043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (II == Ident__BASE_FILE__ && PLoc.isValid()) {
13053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      SourceLocation NextLoc = PLoc.getIncludeLoc();
13063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      while (NextLoc.isValid()) {
13073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        PLoc = SourceMgr.getPresumedLoc(NextLoc);
13083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (PLoc.isInvalid())
13093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          break;
13103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        NextLoc = PLoc.getIncludeLoc();
13123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      }
13133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
13143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Escape this filename.  Turn '\' -> '\\' '"' -> '\"'
13163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SmallString<128> FN;
13173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (PLoc.isValid()) {
13183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      FN += PLoc.getFilename();
13193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Lexer::Stringify(FN);
13203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      OS << '"' << FN.str() << '"';
13213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
13223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::string_literal);
13233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__DATE__) {
13243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (!DATELoc.isValid())
13253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
13263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::string_literal);
13273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setLength(strlen("\"Mmm dd yyyy\""));
13283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(),
13293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                 Tok.getLocation(),
13303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                 Tok.getLength()));
13313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return;
13323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__TIME__) {
13333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (!TIMELoc.isValid())
13343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      ComputeDATE_TIME(DATELoc, TIMELoc, *this);
13353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::string_literal);
13363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setLength(strlen("\"hh:mm:ss\""));
13373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(),
13383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                 Tok.getLocation(),
13393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos                                                 Tok.getLength()));
13403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    return;
13413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__INCLUDE_LEVEL__) {
13423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Compute the presumed include depth of this token.  This can be affected
13433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // by GNU line markers.
13443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    unsigned Depth = 0;
13453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
13473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (PLoc.isValid()) {
13483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
13493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      for (; PLoc.isValid(); ++Depth)
13503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc());
13513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
13523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // __INCLUDE_LEVEL__ expands to a simple numeric value.
13543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << Depth;
13553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::numeric_constant);
13563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__TIMESTAMP__) {
13573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // MSVC, ICC, GCC, VisualAge C++ extension.  The generated string should be
13583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
13593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Get the file that we are lexing out of.  If we're currently lexing from
13613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // a macro, dig into the include stack.
13623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    const FileEntry *CurFile = 0;
13633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    PreprocessorLexer *TheLexer = getCurrentFileLexer();
13643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (TheLexer)
13663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID());
13673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    const char *Result;
13693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (CurFile) {
13703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      time_t TT = CurFile->getModificationTime();
13713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      struct tm *TM = localtime(&TT);
13723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Result = asctime(TM);
13733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    } else {
13743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Result = "??? ??? ?? ??:??:?? ????\n";
13753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
13763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Surround the string with " and strip the trailing newline.
13773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << '"' << StringRef(Result, strlen(Result)-1) << '"';
13783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::string_literal);
13793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__COUNTER__) {
13803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // __COUNTER__ expands to a simple numeric value.
13813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << CounterValue++;
13823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    Tok.setKind(tok::numeric_constant);
13833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__has_feature   ||
13843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos             II == Ident__has_extension ||
13853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos             II == Ident__has_builtin   ||
13863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos             II == Ident__has_attribute) {
13873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // The argument to these builtins should be a parenthesized identifier.
13883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SourceLocation StartLoc = Tok.getLocation();
13893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool IsValid = false;
13913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    IdentifierInfo *FeatureII = 0;
13923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
13933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Read the '('.
13943f03b586351779be6947466f530f22c491b1b70fAndy Gibbs    LexUnexpandedToken(Tok);
13953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (Tok.is(tok::l_paren)) {
13963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Read the identifier
13973f03b586351779be6947466f530f22c491b1b70fAndy Gibbs      LexUnexpandedToken(Tok);
1398899022b14bc3bde19a949fb042711864ca3a629bRichard Smith      if ((FeatureII = Tok.getIdentifierInfo())) {
13993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        // Read the ')'.
14003f03b586351779be6947466f530f22c491b1b70fAndy Gibbs        LexUnexpandedToken(Tok);
14013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos        if (Tok.is(tok::r_paren))
14023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          IsValid = true;
14033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      }
14043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
14053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
14063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool Value = false;
14073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (!IsValid)
14083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Diag(StartLoc, diag::err_feature_check_malformed);
14093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    else if (II == Ident__has_builtin) {
14103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      // Check for a builtin is trivial.
14113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Value = FeatureII->getBuiltinID() != 0;
14123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    } else if (II == Ident__has_attribute)
14133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Value = HasAttribute(FeatureII);
14143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    else if (II == Ident__has_extension)
14153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Value = HasExtension(*this, FeatureII);
14163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    else {
14173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      assert(II == Ident__has_feature && "Must be feature check");
14183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Value = HasFeature(*this, FeatureII);
14193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    }
14203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
14213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << (int)Value;
14223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (IsValid)
14233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Tok.setKind(tok::numeric_constant);
14243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__has_include ||
14253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos             II == Ident__has_include_next) {
14263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // The argument to these two builtins should be a parenthesized
14273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // file name string literal using angle brackets (<>) or
14283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // double-quotes ("").
14293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool Value;
14303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    if (II == Ident__has_include)
14313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Value = EvaluateHasInclude(Tok, II, *this);
14323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    else
14333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      Value = EvaluateHasIncludeNext(Tok, II, *this);
14343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << (int)Value;
143597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu    if (Tok.is(tok::r_paren))
143697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu      Tok.setKind(tok::numeric_constant);
14373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else if (II == Ident__has_warning) {
14383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // The argument should be a parenthesized string literal.
14393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // The argument to these builtins should be a parenthesized identifier.
14403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    SourceLocation StartLoc = Tok.getLocation();
14413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool IsValid = false;
14423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    bool Value = false;
14433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    // Read the '('.
144402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs    LexUnexpandedToken(Tok);
14453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    do {
144602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      if (Tok.isNot(tok::l_paren)) {
144702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        Diag(StartLoc, diag::err_warning_check_malformed);
144802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        break;
144902a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      }
145002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs
145102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      LexUnexpandedToken(Tok);
145202a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      std::string WarningName;
145302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      SourceLocation StrStartLoc = Tok.getLocation();
145497f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs      if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
145597f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs                                  /*MacroExpansion=*/false)) {
145602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        // Eat tokens until ')'.
14576d534d45a598e2ee1c24e5b286f8fab4ad89fb56Andy Gibbs        while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) &&
14586d534d45a598e2ee1c24e5b286f8fab4ad89fb56Andy Gibbs               Tok.isNot(tok::eof))
14593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos          LexUnexpandedToken(Tok);
146002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        break;
14613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos      }
146202a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs
146302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      // Is the end a ')'?
146402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      if (!(IsValid = Tok.is(tok::r_paren))) {
146502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        Diag(StartLoc, diag::err_warning_check_malformed);
146602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        break;
146702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      }
146802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs
146902a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      if (WarningName.size() < 3 || WarningName[0] != '-' ||
147002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs          WarningName[1] != 'W') {
147102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        Diag(StrStartLoc, diag::warn_has_warning_invalid_option);
147202a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        break;
147302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      }
147402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs
147502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      // Finally, check if the warning flags maps to a diagnostic group.
147602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      // We construct a SmallVector here to talk to getDiagnosticIDs().
147702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      // Although we don't use the result, this isn't a hot path, and not
147802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      // worth special casing.
1479cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko      SmallVector<diag::kind, 10> Diags;
148002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs      Value = !getDiagnostics().getDiagnosticIDs()->
148102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs        getDiagnosticsInGroup(WarningName.substr(2), Diags);
14823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    } while (false);
14833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
14843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    OS << (int)Value;
1485b9971bada4eeae74883b61ba96fc6d983b6b0e7fAndy Gibbs    if (IsValid)
1486b9971bada4eeae74883b61ba96fc6d983b6b0e7fAndy Gibbs      Tok.setKind(tok::numeric_constant);
1487b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  } else if (II == Ident__building_module) {
1488b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    // The argument to this builtin should be an identifier. The
1489b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    // builtin evaluates to 1 when that identifier names the module we are
1490b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    // currently building.
1491b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    OS << (int)EvaluateBuildingModule(Tok, II, *this);
1492b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    Tok.setKind(tok::numeric_constant);
1493b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor  } else if (II == Ident__MODULE__) {
1494b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    // The current module as an identifier.
1495b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    OS << getLangOpts().CurrentModule;
1496b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule);
1497b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    Tok.setIdentifierInfo(ModuleII);
1498b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor    Tok.setKind(ModuleII->getTokenID());
14993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  } else {
15003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    llvm_unreachable("Unknown identifier!");
15013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  }
1502374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko  CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation());
15033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
15043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos
15053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::markMacroAsUsed(MacroInfo *MI) {
15063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // If the 'used' status changed, and the macro requires 'unused' warning,
15073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  // remove its SourceLocation from the warn-for-unused-macro locations.
15083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  if (MI->isWarnIfUnused() && !MI->isUsed())
15093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos    WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
15103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos  MI->setIsUsed(true);
15113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos}
1512