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// 10651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// This file implements the top level handling of macro expansion for the 113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// preprocessor. 123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// 133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos//===----------------------------------------------------------------------===// 143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "clang/Lex/Preprocessor.h" 16651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Basic/Attributes.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" 23651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/Lex/MacroArgs.h" 2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/MacroInfo.h" 2555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/STLExtras.h" 2602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs#include "llvm/ADT/SmallString.h" 273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "llvm/ADT/StringSwitch.h" 283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "llvm/Config/llvm-config.h" 293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include "llvm/Support/ErrorHandling.h" 3033d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko#include "llvm/Support/Format.h" 3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h" 323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include <cstdio> 333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos#include <ctime> 343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosusing namespace clang; 353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 369818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios KyrtzidisMacroDirective * 379818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios KyrtzidisPreprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { 384d7e0ced7f16a04aabe2d8d91cbbb52fb1162810Alexander Kornienko assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); 393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos macro_iterator Pos = Macros.find(II); 413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(Pos != Macros.end() && "Identifier macro info is missing!"); 423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return Pos->second; 433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 45c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidisvoid Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ 469317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis assert(MD && "MacroDirective should be non-zero!"); 47c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis assert(!MD->getPrevious() && "Already attached to a MacroDirective history."); 486c6c54a59a6e7dbe63ff6a9bbab76f6e0c7c8462Douglas Gregor 499818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis MacroDirective *&StoredMD = Macros[II]; 509818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis MD->setPrevious(StoredMD); 519818a1d443e97677dd3422305de9cc2b1fb2a8c1Argyrios Kyrtzidis StoredMD = MD; 52c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis II->setHasMacroDefinition(MD->isDefined()); 53c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis bool isImportedMacro = isa<DefMacroDirective>(MD) && 54c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis cast<DefMacroDirective>(MD)->isImported(); 55c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis if (II->isFromAST() && !isImportedMacro) 563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos II->setChangedSinceDeserialization(); 579317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis} 58c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis 599317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidisvoid Preprocessor::setLoadedMacroDirective(IdentifierInfo *II, 609317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis MacroDirective *MD) { 619317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis assert(II && MD); 629317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis MacroDirective *&StoredMD = Macros[II]; 639317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis assert(!StoredMD && 649317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis "the macro history was modified before initializing it from a pch"); 659317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis StoredMD = MD; 669317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis // Setup the identifier as having associated macro history. 679317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis II->setHasMacroDefinition(true); 689317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis if (!MD->isDefined()) 699317ab94bb68122ba6fc728eb73c1308fb913cd1Argyrios Kyrtzidis II->setHasMacroDefinition(false); 703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// RegisterBuiltinMacro - Register the specified identifier in the identifier 733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// table and mark it as a builtin macro to be expanded. 743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ 753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Get the identifier. 763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IdentifierInfo *Id = PP.getIdentifierInfo(Name); 773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Mark it as being a macro that is builtin. 793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); 803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MI->setIsBuiltinMacro(); 81c56fff7fd231aebf4b152f60f8f11ef91835c48aArgyrios Kyrtzidis PP.appendDefMacroDirective(Id, MI); 823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return Id; 833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the 873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// identifier table. 883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::RegisterBuiltinMacros() { 893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); 903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); 913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); 923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); 933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); 943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); 953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // GCC Extensions. 973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); 983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); 993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); 1003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Microsoft Extensions. 102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (LangOpts.MicrosoftExt) { 103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Ident__identifier = RegisterBuiltinMacro(*this, "__identifier"); 104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); 105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } else { 1066bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ident__identifier = nullptr; 1076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ident__pragma = nullptr; 108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 109651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Clang Extensions. 1113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); 1123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); 1133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); 1143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); 1153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); 1163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); 1173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); 1186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier"); 1193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 120b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Modules. 121b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor if (LangOpts.Modules) { 122b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor Ident__building_module = RegisterBuiltinMacro(*this, "__building_module"); 123b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 124b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // __MODULE__ 125b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor if (!LangOpts.CurrentModule.empty()) 126b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__"); 127b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor else 1286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ident__MODULE__ = nullptr; 129b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } else { 1306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ident__building_module = nullptr; 1316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Ident__MODULE__ = nullptr; 132b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } 1333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 1343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token 1363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// in its expansion, currently expands to that token literally. 1373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool isTrivialSingleTokenExpansion(const MacroInfo *MI, 1383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const IdentifierInfo *MacroIdent, 1393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Preprocessor &PP) { 1403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); 1413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If the token isn't an identifier, it's always literally expanded. 1436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!II) return true; 1443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If the information about this identifier is out of date, update it from 1463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // the external source. 1473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (II->isOutOfDate()) 1483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PP.getExternalSource()->updateOutOfDateIdentifier(*II); 1493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If the identifier is a macro, and if that macro is enabled, it may be 1513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // expanded so it's not a trivial expansion. 1523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && 1533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Fast expanding "#define X X" is ok, because X would be disabled. 1543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos II != MacroIdent) 1553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 1563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is an object-like macro invocation, it is safe to trivially expand 1583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // it. 1593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MI->isObjectLike()) return true; 1603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is a function-like macro invocation, it's safe to trivially expand 1623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // as long as the identifier is not a macro argument. 1633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); 1643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos I != E; ++I) 1653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (*I == II) 1663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; // Identifier is a macro argument. 1673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return true; 1693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 1703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// isNextPPTokenLParen - Determine whether the next preprocessor token to be 1733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// lexed is a '('. If so, consume the token and return true, if not, this 1743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// method should have no observable side-effect on the lexed tokens. 1753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosbool Preprocessor::isNextPPTokenLParen() { 1763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Do some quick tests for rejection cases. 1773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned Val; 1783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (CurLexer) 1793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Val = CurLexer->isNextPPTokenLParen(); 1803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else if (CurPTHLexer) 1813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Val = CurPTHLexer->isNextPPTokenLParen(); 1823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else 1833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Val = CurTokenLexer->isNextTokenLParen(); 1843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Val == 2) { 1863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // We have run off the end. If it's a source file we don't 1873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the 1883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // macro stack. 1893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (CurPPLexer) 1903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 1913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { 1923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IncludeStackInfo &Entry = IncludeMacroStack[i-1]; 1933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Entry.TheLexer) 1943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Val = Entry.TheLexer->isNextPPTokenLParen(); 1953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else if (Entry.ThePTHLexer) 1963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Val = Entry.ThePTHLexer->isNextPPTokenLParen(); 1973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else 1983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Val = Entry.TheTokenLexer->isNextTokenLParen(); 1993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Val != 2) 2013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 2023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Ran off the end of a source file? 2043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Entry.ThePPLexer) 2053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 2063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Okay, if we know that the token is a '(', lex it and return. Otherwise we 2103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // have found something that isn't a '(' or we found the end of the 2113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // translation unit. In either case, return false. 2123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return Val == 1; 2133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 2143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be 2163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// expanded as a macro, handle it and return the next token as 'Identifier'. 2173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosbool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, 218c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis MacroDirective *MD) { 21935803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis MacroDirective::DefInfo Def = MD->getDefinition(); 22035803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis assert(Def.isValid()); 22135803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis MacroInfo *MI = Def.getMacroInfo(); 222c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis 2233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is a macro expansion in the "#if !defined(x)" line for the file, 2243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // then the macro could expand to different things in other contexts, we need 2253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // to disable the optimization in this case. 2263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); 2273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. 2293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MI->isBuiltinMacro()) { 230c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis if (Callbacks) Callbacks->MacroExpands(Identifier, MD, 2316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Identifier.getLocation(), 2326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines /*Args=*/nullptr); 2333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ExpandBuiltinMacro(Identifier); 234d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman return true; 2353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos /// Args - If this is a function-like macro expansion, this contains, 2383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos /// for each macro argument, the list of tokens that were provided to the 2393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos /// invocation. 2406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines MacroArgs *Args = nullptr; 2413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Remember where the end of the expansion occurred. For an object-like 2433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // macro, this is the identifier. For a function-like macro, this is the ')'. 2443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation ExpansionEnd = Identifier.getLocation(); 2453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is a function-like macro, read the arguments. 2473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MI->isFunctionLike()) { 2483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Remember that we are now parsing the arguments to a macro invocation. 2493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Preprocessor directives used inside macro arguments are not portable, and 2503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // this enables the warning. 2513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos InMacroArgs = true; 2523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); 2533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Finished parsing args. 2553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos InMacroArgs = false; 2563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If there was an error parsing the arguments, bail out. 2586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!Args) return true; 2593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumFnMacroExpanded; 2613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else { 2623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumMacroExpanded; 2633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Notice that this macro has been used. 2663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos markMacroAsUsed(MI); 2673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Remember where the token is expanded. 2693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation ExpandLoc = Identifier.getLocation(); 2703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); 2713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 2723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Callbacks) { 2733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (InMacroArgs) { 2743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // We can have macro expansion inside a conditional directive while 2753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // reading the function macro arguments. To ensure, in that case, that 2763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // MacroExpands callbacks still happen in source order, queue this 2773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // callback to have it happen after the function macro callback. 2783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos DelayedMacroExpandsCallbacks.push_back( 279c515978bd3a703aa733f846a0094ffa84d149074Argyrios Kyrtzidis MacroExpandsInfo(Identifier, MD, ExpansionRange)); 2803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else { 281dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args); 2823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!DelayedMacroExpandsCallbacks.empty()) { 2833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { 2843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; 285dd08a0c178329ec16cb9e494e6880f3991708b93Argyrios Kyrtzidis // FIXME: We lose macro args info with delayed callback. 2866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, 2876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines /*Args=*/nullptr); 2883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos DelayedMacroExpandsCallbacks.clear(); 2903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 2923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 293e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor 294e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor // If the macro definition is ambiguous, complain. 29535803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis if (Def.getDirective()->isAmbiguous()) { 296e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor Diag(Identifier, diag::warn_pp_ambiguous_macro) 297e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor << Identifier.getIdentifierInfo(); 298e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) 299e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor << Identifier.getIdentifierInfo(); 30035803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition(); 30135803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis PrevDef && !PrevDef.isUndefined(); 30235803282ef0282467fe1c09aa8284d734030dc3fArgyrios Kyrtzidis PrevDef = PrevDef.getPreviousDefinition()) { 303651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Diag(PrevDef.getMacroInfo()->getDefinitionLoc(), 304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines diag::note_pp_ambiguous_macro_other) 305651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << Identifier.getIdentifierInfo(); 306651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!PrevDef.getDirective()->isAmbiguous()) 307651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines break; 308e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor } 309e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor } 310e8219a655128b98d0573658a139de5d848451fdaDouglas Gregor 3113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If we started lexing a macro, enter the macro expansion body. 3123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this macro expands to no tokens, don't bother to push it onto the 3143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // expansion stack, only to take it right back off. 3153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MI->getNumTokens() == 0) { 3163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // No need for arg info. 3173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Args) Args->destroy(*this); 3183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 319d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman // Propagate whitespace info as if we had pushed, then popped, 320d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman // a macro context. 3213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Identifier.setFlag(Token::LeadingEmptyMacro); 322d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman PropagateLineStartLeadingSpaceInfo(Identifier); 3233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumFastMacroExpanded; 3243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 3253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (MI->getNumTokens() == 1 && 3263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), 3273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos *this)) { 3283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Otherwise, if this macro expands into a single trivially-expanded 3293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // token: expand it now. This handles common cases like 3303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // "#define VAL 42". 3313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // No need for arg info. 3333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Args) Args->destroy(*this); 3343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro 3363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // identifier to the expanded token. 3373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool isAtStartOfLine = Identifier.isAtStartOfLine(); 3383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool hasLeadingSpace = Identifier.hasLeadingSpace(); 3393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Replace the result token. 3413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Identifier = MI->getReplacementToken(0); 3423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Restore the StartOfLine/LeadingSpace markers. 3443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); 3453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); 3463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Update the tokens location to include both its expansion and physical 3483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // locations. 3493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation Loc = 3503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, 3513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ExpansionEnd,Identifier.getLength()); 3523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Identifier.setLocation(Loc); 3533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is a disabled macro or #define X X, we must mark the result as 3553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // unexpandable. 3563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { 3573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MacroInfo *NewMI = getMacroInfo(NewII)) 3583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!NewMI->isEnabled() || NewMI == MI) { 3593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Identifier.setFlag(Token::DisableExpand); 36040f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor // Don't warn for "#define X X" like "#define bool bool" from 36140f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor // stdbool.h. 36240f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor if (NewMI != MI || MI->isFunctionLike()) 36340f56e5b677761c8258324222d62fce3e4f6bca4Douglas Gregor Diag(Identifier, diag::pp_disabled_macro_expansion); 3643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 3653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 3663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Since this is not an identifier token, it can't be macro expanded, so 3683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // we're done. 3693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumFastMacroExpanded; 370d2f9308220af22bfc1bcd3bc2cad118dbd8be013Eli Friedman return true; 3713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 3723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Start expanding the macro. 3743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos EnterMacro(Identifier, ExpansionEnd, MI, Args); 3753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 3763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 3773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 3785940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieuenum Bracket { 3795940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Brace, 3805940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Paren 3815940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu}; 3825940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 3835940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// CheckMatchedBrackets - Returns true if the braces and parentheses in the 3845940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// token vector are properly nested. 3855940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieustatic bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) { 3865940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVector<Bracket, 8> Brackets; 3875940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(), 3885940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu E = Tokens.end(); 3895940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu I != E; ++I) { 3905940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (I->is(tok::l_paren)) { 3915940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Brackets.push_back(Paren); 3925940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } else if (I->is(tok::r_paren)) { 3935940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (Brackets.empty() || Brackets.back() == Brace) 3945940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu return false; 3955940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Brackets.pop_back(); 3965940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } else if (I->is(tok::l_brace)) { 3975940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Brackets.push_back(Brace); 3985940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } else if (I->is(tok::r_brace)) { 3995940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (Brackets.empty() || Brackets.back() == Paren) 4005940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu return false; 4015940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Brackets.pop_back(); 4025940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 4035940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 4045940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!Brackets.empty()) 4055940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu return false; 4065940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu return true; 4075940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu} 4085940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4095940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new 4105940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// vector of tokens in NewTokens. The new number of arguments will be placed 4115940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// in NumArgs and the ranges which need to surrounded in parentheses will be 4125940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// in ParenHints. 4135940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// Returns false if the token stream cannot be changed. If this is because 4145940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// of an initializer list starting a macro argument, the range of those 4155940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu/// initializer lists will be place in InitLists. 4165940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieustatic bool GenerateNewArgTokens(Preprocessor &PP, 4175940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVectorImpl<Token> &OldTokens, 4185940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVectorImpl<Token> &NewTokens, 4195940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu unsigned &NumArgs, 4205940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVectorImpl<SourceRange> &ParenHints, 4215940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVectorImpl<SourceRange> &InitLists) { 4225940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!CheckMatchedBrackets(OldTokens)) 4235940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu return false; 4245940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4255940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Once it is known that the brackets are matched, only a simple count of the 4265940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // braces is needed. 4275940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu unsigned Braces = 0; 4285940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4295940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // First token of a new macro argument. 4305940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin(); 4315940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4325940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // First closing brace in a new macro argument. Used to generate 4335940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // SourceRanges for InitLists. 4345940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end(); 4355940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu NumArgs = 0; 4365940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Token TempToken; 4375940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Set to true when a macro separator token is found inside a braced list. 4385940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // If true, the fixed argument spans multiple old arguments and ParenHints 4395940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // will be updated. 4405940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu bool FoundSeparatorToken = false; 4415940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(), 4425940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu E = OldTokens.end(); 4435940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu I != E; ++I) { 4445940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (I->is(tok::l_brace)) { 4455940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ++Braces; 4465940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } else if (I->is(tok::r_brace)) { 4475940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu --Braces; 4485940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken) 4495940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ClosingBrace = I; 4505940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } else if (I->is(tok::eof)) { 4515940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // EOF token is used to separate macro arguments 4525940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (Braces != 0) { 4535940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Assume comma separator is actually braced list separator and change 4545940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // it back to a comma. 4555940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu FoundSeparatorToken = true; 4565940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu I->setKind(tok::comma); 4575940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu I->setLength(1); 4585940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } else { // Braces == 0 4595940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Separator token still separates arguments. 4605940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ++NumArgs; 4615940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4625940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // If the argument starts with a brace, it can't be fixed with 4635940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // parentheses. A different diagnostic will be given. 4645940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) { 4655940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu InitLists.push_back( 4665940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SourceRange(ArgStartIterator->getLocation(), 4675940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu PP.getLocForEndOfToken(ClosingBrace->getLocation()))); 4685940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ClosingBrace = E; 4695940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 4705940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4715940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Add left paren 4725940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (FoundSeparatorToken) { 4735940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.startToken(); 4745940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.setKind(tok::l_paren); 4755940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.setLocation(ArgStartIterator->getLocation()); 4765940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.setLength(0); 4775940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu NewTokens.push_back(TempToken); 4785940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 4795940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4805940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Copy over argument tokens 4815940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu NewTokens.insert(NewTokens.end(), ArgStartIterator, I); 4825940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4835940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Add right paren and store the paren locations in ParenHints 4845940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (FoundSeparatorToken) { 4855940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation()); 4865940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.startToken(); 4875940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.setKind(tok::r_paren); 4885940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.setLocation(Loc); 4895940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TempToken.setLength(0); 4905940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu NewTokens.push_back(TempToken); 4915940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(), 4925940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Loc)); 4935940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 4945940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4955940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Copy separator token 4965940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu NewTokens.push_back(*I); 4975940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 4985940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Reset values 4995940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ArgStartIterator = I + 1; 5005940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu FoundSeparatorToken = false; 5015940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 5025940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 5035940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 5045940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 5055940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu return !ParenHints.empty() && InitLists.empty(); 5065940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu} 5075940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 5083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next 5093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// token is the '(' of the macro, this method is invoked to read all of the 5103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// actual arguments specified for the macro invocation. This returns null on 5113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// error. 5123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao MatosMacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, 5133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroInfo *MI, 5143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation &MacroEnd) { 5153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // The number of fixed arguments to parse. 5163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned NumFixedArgsLeft = MI->getNumArgs(); 5173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool isVariadic = MI->isVariadic(); 5183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Outer loop, while there are more arguments, keep reading them. 5203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Token Tok; 5213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Read arguments as unexpanded tokens. This avoids issues, e.g., where 5233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // an argument value in a macro could expand to ',' or '(' or ')'. 5243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos LexUnexpandedToken(Tok); 5253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); 5263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // ArgTokens - Build up a list of tokens that make up each argument. Each 5283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // argument is separated by an EOF token. Use a SmallVector so we can avoid 5293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // heap allocations in the common case. 5303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SmallVector<Token, 64> ArgTokens; 531cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis bool ContainsCodeCompletionTok = false; 5323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5335940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SourceLocation TooManyArgsLoc; 5345940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 5353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned NumActuals = 0; 5363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos while (Tok.isNot(tok::r_paren)) { 537cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis if (ContainsCodeCompletionTok && (Tok.is(tok::eof) || Tok.is(tok::eod))) 538cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis break; 539cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis 5403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && 5413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos "only expect argument separators here"); 5423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned ArgTokenStart = ArgTokens.size(); 5443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation ArgStartLoc = Tok.getLocation(); 5453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note 5473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // that we already consumed the first one. 5483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned NumParens = 0; 5493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos while (1) { 5513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Read arguments as unexpanded tokens. This avoids issues, e.g., where 5523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // an argument value in a macro could expand to ',' or '(' or ')'. 5533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos LexUnexpandedToken(Tok); 5543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 5553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Tok.is(tok::eof) || Tok.is(tok::eod)) { // "#if f(<eof>" & "#if f(\n" 556cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis if (!ContainsCodeCompletionTok) { 557cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis Diag(MacroName, diag::err_unterm_macro_invoc); 558cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis Diag(MI->getDefinitionLoc(), diag::note_macro_here) 559cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis << MacroName.getIdentifierInfo(); 560cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis // Do not lose the EOF/EOD. Return it to the client. 561cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis MacroName = Tok; 5626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 563cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis } else { 564bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis // Do not lose the EOF/EOD. 565bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis Token *Toks = new Token[1]; 566bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis Toks[0] = Tok; 567bb06b5000b9639a92e92c368ff3e5e5f8bb1e724Argyrios Kyrtzidis EnterTokenStream(Toks, 1, true, true); 568cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis break; 569cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis } 5703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (Tok.is(tok::r_paren)) { 5713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If we found the ) token, the macro arg list is done. 5723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (NumParens-- == 0) { 5733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroEnd = Tok.getLocation(); 5743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 5753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 5763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (Tok.is(tok::l_paren)) { 5773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumParens; 57811be06464a338b7c6e007898a7709498dc456db2Reid Kleckner } else if (Tok.is(tok::comma) && NumParens == 0 && 57911be06464a338b7c6e007898a7709498dc456db2Reid Kleckner !(Tok.getFlags() & Token::IgnoredComma)) { 58011be06464a338b7c6e007898a7709498dc456db2Reid Kleckner // In Microsoft-compatibility mode, single commas from nested macro 58111be06464a338b7c6e007898a7709498dc456db2Reid Kleckner // expansions should not be considered as argument separators. We test 58211be06464a338b7c6e007898a7709498dc456db2Reid Kleckner // for this with the IgnoredComma token flag above. 58311be06464a338b7c6e007898a7709498dc456db2Reid Kleckner 5843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Comma ends this argument if there are more fixed arguments expected. 5853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // However, if this is a variadic macro, and this is part of the 5863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // variadic part, then the comma is just an argument token. 5873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!isVariadic) break; 5883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (NumFixedArgsLeft > 1) 5893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 5903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (Tok.is(tok::comment) && !KeepMacroComments) { 5913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is a comment token in the argument list and we're just in 5923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // -C mode (not -CC mode), discard the comment. 5933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos continue; 5946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines } else if (Tok.getIdentifierInfo() != nullptr) { 5953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Reading macro arguments can cause macros that we are currently 5963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // expanding from to be popped off the expansion stack. Doing so causes 5973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // them to be reenabled for expansion. Here we record whether any 5983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // identifiers we lex as macro arguments correspond to disabled macros. 5993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If so, we mark the token as noexpand. This is a subtle aspect of 6003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C99 6.10.3.4p2. 6013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) 6023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!MI->isEnabled()) 6033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setFlag(Token::DisableExpand); 6043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (Tok.is(tok::code_completion)) { 605cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis ContainsCodeCompletionTok = true; 6063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (CodeComplete) 6073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), 6083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MI, NumActuals); 6093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Don't mark that we reached the code-completion point because the 6103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // parser is going to handle the token and there will be another 6113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // code-completion callback. 6123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 6133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ArgTokens.push_back(Tok); 6153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 6163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this was an empty argument list foo(), don't add this as an empty 6183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // argument. 6193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) 6203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 6213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is not a variadic macro, and too many args were specified, emit 6233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // an error. 6245940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) { 6253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (ArgTokens.size() != ArgTokenStart) 6265940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation(); 6275940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu else 6285940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu TooManyArgsLoc = ArgStartLoc; 6293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 6303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6315940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Empty arguments are standard in C99 and C++0x, and are supported as an 6325940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // extension in other modes. 6333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) 63480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith Diag(Tok, LangOpts.CPlusPlus11 ? 6353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos diag::warn_cxx98_compat_empty_fnmacro_arg : 6363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos diag::ext_empty_fnmacro_arg); 6373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Add a marker EOF token to the end of the token list for this argument. 6393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Token EOFTok; 6403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos EOFTok.startToken(); 6413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos EOFTok.setKind(tok::eof); 6423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos EOFTok.setLocation(Tok.getLocation()); 6433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos EOFTok.setLength(0); 6443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ArgTokens.push_back(EOFTok); 6453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumActuals; 6465940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0) 647fdf5706440543c744cd9faf55235a757a7278daaArgyrios Kyrtzidis --NumFixedArgsLeft; 6483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 6493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Okay, we either found the r_paren. Check to see if we parsed too few 6513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // arguments. 6523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned MinArgsExpected = MI->getNumArgs(); 6533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 6545940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // If this is not a variadic macro, and too many args were specified, emit 6555940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // an error. 6565940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!isVariadic && NumActuals > MinArgsExpected && 6575940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu !ContainsCodeCompletionTok) { 6585940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Emit the diagnostic at the macro name in case there is a missing ). 6595940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Emitting it at the , could be far away from the macro name. 6605940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc); 6615940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Diag(MI->getDefinitionLoc(), diag::note_macro_here) 6625940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu << MacroName.getIdentifierInfo(); 6635940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 6645940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // Commas from braced initializer lists will be treated as argument 6655940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // separators inside macros. Attempt to correct for this with parentheses. 6665940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // TODO: See if this can be generalized to angle brackets for templates 6675940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu // inside macro arguments. 6685940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 66940ec4d26b7622eb07ad8d5f02da0dd43a1dc74b5Bob Wilson SmallVector<Token, 4> FixedArgTokens; 6705940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu unsigned FixedNumArgs = 0; 6715940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu SmallVector<SourceRange, 4> ParenHints, InitLists; 6725940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs, 6735940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ParenHints, InitLists)) { 6745940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (!InitLists.empty()) { 6755940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu DiagnosticBuilder DB = 6765940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu Diag(MacroName, 6775940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu diag::note_init_list_at_beginning_of_macro_argument); 6786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines for (const SourceRange &Range : InitLists) 6796bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DB << Range; 6805940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 6816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 6825940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 6835940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu if (FixedNumArgs != MinArgsExpected) 6846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 6855940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 6865940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro); 6876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines for (const SourceRange &ParenLocation : ParenHints) { 6886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "("); 6896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")"); 6905940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 6915940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu ArgTokens.swap(FixedArgTokens); 6925940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu NumActuals = FixedNumArgs; 6935940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu } 6945940bf33061844c9a61d4d5c1230df9cf9e90342Richard Trieu 6953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // See MacroArgs instance var for description of this. 6963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool isVarargsElided = false; 6973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 698f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis if (ContainsCodeCompletionTok) { 699f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis // Recover from not-fully-formed macro invocation during code-completion. 700f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis Token EOFTok; 701f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis EOFTok.startToken(); 702f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis EOFTok.setKind(tok::eof); 703f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis EOFTok.setLocation(Tok.getLocation()); 704f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis EOFTok.setLength(0); 705f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis for (; NumActuals < MinArgsExpected; ++NumActuals) 706f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis ArgTokens.push_back(EOFTok); 707f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis } 708f1e5b155088203f88a8172aa61c1d84185966bf0Argyrios Kyrtzidis 7093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (NumActuals < MinArgsExpected) { 7103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // There are several cases where too few arguments is ok, handle them now. 7113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (NumActuals == 0 && MinArgsExpected == 1) { 7123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // #define A(X) or #define A(...) ---> A() 7133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If there is exactly one argument, and that argument is missing, 7153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // then we have an empty "()" argument empty list. This is fine, even if 7163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // the macro expects one argument (the argument is just empty). 7173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos isVarargsElided = MI->isVariadic(); 7183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (MI->isVariadic() && 7193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) 7203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() 7213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Varargs where the named vararg parameter is missing: OK as extension. 7223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // #define A(x, ...) 7233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // A("blah") 7244fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman // 7254fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman // If the macro contains the comma pasting extension, the diagnostic 7264fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman // is suppressed; we know we'll get another diagnostic later. 7274fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman if (!MI->hasCommaPasting()) { 7284fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman Diag(Tok, diag::ext_missing_varargs_arg); 7294fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman Diag(MI->getDefinitionLoc(), diag::note_macro_here) 7304fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman << MacroName.getIdentifierInfo(); 7314fa4b480270c14dfdcd0dfd4f76938e973082e3bEli Friedman } 7323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Remember this occurred, allowing us to elide the comma when used for 7343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // cases like: 7353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // #define A(x, foo...) blah(a, ## foo) 7363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // #define B(x, ...) blah(a, ## __VA_ARGS__) 7373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // #define C(...) blah(a, ## __VA_ARGS__) 7383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // A(x) B(x) C() 7393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos isVarargsElided = true; 740cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis } else if (!ContainsCodeCompletionTok) { 7413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Otherwise, emit the error. 7423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Diag(Tok, diag::err_too_few_args_in_macro_invoc); 7430ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis Diag(MI->getDefinitionLoc(), diag::note_macro_here) 7440ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis << MacroName.getIdentifierInfo(); 7456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 7463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 7473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Add a marker EOF token to the end of the token list for this argument. 7493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation EndLoc = Tok.getLocation(); 7503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.startToken(); 7513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::eof); 7523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setLocation(EndLoc); 7533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setLength(0); 7543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ArgTokens.push_back(Tok); 7553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If we expect two arguments, add both as empty. 7573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (NumActuals == 0 && MinArgsExpected == 2) 7583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ArgTokens.push_back(Tok); 7593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 760cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis } else if (NumActuals > MinArgsExpected && !MI->isVariadic() && 761cd0fd18909e3b89ed6f2cc1118809003db64e67aArgyrios Kyrtzidis !ContainsCodeCompletionTok) { 7623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Emit the diagnostic at the macro name in case there is a missing ). 7633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Emitting it at the , could be far away from the macro name. 7643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Diag(MacroName, diag::err_too_many_args_in_macro_invoc); 7650ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis Diag(MI->getDefinitionLoc(), diag::note_macro_here) 7660ee8de7fe9c1fb74574c62b9d59ffe46a94435f4Argyrios Kyrtzidis << MacroName.getIdentifierInfo(); 7676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 7683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 7693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); 7713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 7723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// \brief Keeps macro expanded tokens for TokenLexers. 7743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos// 7753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Works like a stack; a TokenLexer adds the macro expanded tokens that is 7763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// going to lex in the cache and when it finishes the tokens are removed 7773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// from the end of the cache. 7783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao MatosToken *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, 7793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ArrayRef<Token> tokens) { 7803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(tokLexer); 7813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (tokens.empty()) 7826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return nullptr; 7833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos size_t newIndex = MacroExpandedTokens.size(); 7853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool cacheNeedsToGrow = tokens.size() > 7863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); 7873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroExpandedTokens.append(tokens.begin(), tokens.end()); 7883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 7893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (cacheNeedsToGrow) { 7903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Go through all the TokenLexers whose 'Tokens' pointer points in the 7913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // buffer and update the pointers to the (potential) new buffer array. 7923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { 7933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos TokenLexer *prevLexer; 7943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos size_t tokIndex; 795651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; 7963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; 7973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 7983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 7993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); 8013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return MacroExpandedTokens.data() + newIndex; 8023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 8033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { 8053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(!MacroExpandingLexersStack.empty()); 8063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos size_t tokIndex = MacroExpandingLexersStack.back().second; 8073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(tokIndex < MacroExpandedTokens.size()); 8083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Pop the cached macro expanded tokens from the end. 8093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroExpandedTokens.resize(tokIndex); 8103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MacroExpandingLexersStack.pop_back(); 8113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 8123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// ComputeDATE_TIME - Compute the current time, enter it into the specified 8143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// scratch buffer, then return DATELoc/TIMELoc locations with the position of 8153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// the identifier tokens inserted. 8163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, 8173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Preprocessor &PP) { 8186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines time_t TT = time(nullptr); 8193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos struct tm *TM = localtime(&TT); 8203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos static const char * const Months[] = { 8223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" 8233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos }; 8243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 82533d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko { 82633d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko SmallString<32> TmpBuffer; 82733d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko llvm::raw_svector_ostream TmpStream(TmpBuffer); 82833d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon], 82933d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TM->tm_mday, TM->tm_year + 1900); 83033d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko Token TmpTok; 83133d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TmpTok.startToken(); 832374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko PP.CreateString(TmpStream.str(), TmpTok); 83333d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko DATELoc = TmpTok.getLocation(); 83433d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko } 83533d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko 83633d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko { 83733d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko SmallString<32> TmpBuffer; 83833d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko llvm::raw_svector_ostream TmpStream(TmpBuffer); 83933d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TmpStream << llvm::format("\"%02d:%02d:%02d\"", 84033d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TM->tm_hour, TM->tm_min, TM->tm_sec); 84133d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko Token TmpTok; 84233d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TmpTok.startToken(); 843374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko PP.CreateString(TmpStream.str(), TmpTok); 84433d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko TIMELoc = TmpTok.getLocation(); 84533d054b0932b1ed5d2b30e41946f6aec5030c12eDmitri Gribenko } 8463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 8473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HasFeature - Return true if we recognize and implement the feature 8503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// specified by the identifier as a standard language feature. 8513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) { 8523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const LangOptions &LangOpts = PP.getLangOpts(); 8533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos StringRef Feature = II->getName(); 8543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Normalize the feature name, __foo__ becomes foo. 8563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) 8573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Feature = Feature.substr(2, Feature.size() - 4); 8583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 8593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return llvm::StringSwitch<bool>(Feature) 8604f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz .Case("address_sanitizer", LangOpts.Sanitize.Address) 8613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_analyzer_noreturn", true) 8623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_availability", true) 8633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_availability_with_message", true) 8643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_cf_returns_not_retained", true) 8653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_cf_returns_retained", true) 8663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_deprecated_with_message", true) 8673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_ext_vector_type", true) 8683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_ns_returns_not_retained", true) 8693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_ns_returns_retained", true) 8703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_ns_consumes_self", true) 8713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_ns_consumed", true) 8723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_cf_consumed", true) 8733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_objc_ivar_unused", true) 8743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_objc_method_family", true) 8753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_overloadable", true) 8763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_unavailable_with_message", true) 8773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("attribute_unused_on_fields", true) 8783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("blocks", LangOpts.Blocks) 87988c4b5efc604b982ca9083548d568b02b1abd2abDavid Blaikie .Case("c_thread_safety_attributes", true) 8806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines .Case("cxx_exceptions", LangOpts.CXXExceptions) 8813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_rtti", LangOpts.RTTI) 8823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("enumerator_attributes", true) 8834f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz .Case("memory_sanitizer", LangOpts.Sanitize.Memory) 8844f45bc099f2665bc6e4bcbb169aa452390dbf3feWill Dietz .Case("thread_sanitizer", LangOpts.Sanitize.Thread) 8852eeed711beec49dfad5d3a3f16fdfca4b2f3acf0Peter Collingbourne .Case("dataflow_sanitizer", LangOpts.Sanitize.DataFlow) 8863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Objective-C features 8873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? 8883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_arc", LangOpts.ObjCAutoRefCount) 8893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_arc_weak", LangOpts.ObjCARCWeak) 8903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_default_synthesize_properties", LangOpts.ObjC2) 8913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_fixed_enum", LangOpts.ObjC2) 8923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_instancetype", LangOpts.ObjC2) 8933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) 8943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) 895e4057c203c71873d93aead38d626b3cd29584d8aTed Kremenek .Case("objc_property_explicit_atomic", true) // Does clang support explicit "atomic" keyword? 89689ca1fb13849f6e12dddc1ee562c5cd21ed9dd7aTed Kremenek .Case("objc_protocol_qualifier_mangling", true) 8973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) 8983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("ownership_holds", true) 8993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("ownership_returns", true) 9003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("ownership_takes", true) 9013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_bool", true) 9023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) 9033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_array_literals", LangOpts.ObjC2) 9043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_dictionary_literals", LangOpts.ObjC2) 9053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("objc_boxed_expressions", LangOpts.ObjC2) 9063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("arc_cf_code_audited", true) 9073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C11 features 9083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_alignas", LangOpts.C11) 9093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_atomic", LangOpts.C11) 9103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_generic_selections", LangOpts.C11) 9113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_static_assert", LangOpts.C11) 9126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines .Case("c_thread_local", 913e87c5bd593b038baec7cfa6075f26cd0ac256042Douglas Gregor LangOpts.C11 && PP.getTargetInfo().isTLSSupported()) 9143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C++11 features 91580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11) 91680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_alias_templates", LangOpts.CPlusPlus11) 91780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_alignas", LangOpts.CPlusPlus11) 91880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_atomic", LangOpts.CPlusPlus11) 91980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_attributes", LangOpts.CPlusPlus11) 92080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_auto_type", LangOpts.CPlusPlus11) 92180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_constexpr", LangOpts.CPlusPlus11) 92280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_decltype", LangOpts.CPlusPlus11) 92380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11) 92480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11) 92580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11) 92680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11) 92780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_deleted_functions", LangOpts.CPlusPlus11) 92880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11) 92980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11) 93080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_implicit_moves", LangOpts.CPlusPlus11) 931e6e68b53778bb5a15c10a73a5bf18d8ab73f75e3Richard Smith .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11) 93280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11) 93380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_lambdas", LangOpts.CPlusPlus11) 93480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11) 93580ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11) 93680ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_noexcept", LangOpts.CPlusPlus11) 93780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_nullptr", LangOpts.CPlusPlus11) 93880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_override_control", LangOpts.CPlusPlus11) 93980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_range_for", LangOpts.CPlusPlus11) 94080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11) 94180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11) 94280ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_rvalue_references", LangOpts.CPlusPlus11) 94380ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_strong_enums", LangOpts.CPlusPlus11) 94480ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_static_assert", LangOpts.CPlusPlus11) 94544868f5b6a0980744de08e9fa45193b5a29ff990Richard Smith .Case("cxx_thread_local", 946e87c5bd593b038baec7cfa6075f26cd0ac256042Douglas Gregor LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported()) 94780ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_trailing_return", LangOpts.CPlusPlus11) 94880ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_unicode_literals", LangOpts.CPlusPlus11) 94980ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11) 95080ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_user_literals", LangOpts.CPlusPlus11) 95180ad52f327b532bded5c5b0ee38779d841c6cd35Richard Smith .Case("cxx_variadic_templates", LangOpts.CPlusPlus11) 9527f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith // C++1y features 9533c3a522712ef11a5e5836e2df6466fbd446bb2eaRichard Smith .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus1y) 9547f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith .Case("cxx_binary_literals", LangOpts.CPlusPlus1y) 955a4fb3390853356524416bc28d6044e7a41c87863Richard Smith .Case("cxx_contextual_conversions", LangOpts.CPlusPlus1y) 9566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines .Case("cxx_decltype_auto", LangOpts.CPlusPlus1y) 9576bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines .Case("cxx_generic_lambdas", LangOpts.CPlusPlus1y) 95844868f5b6a0980744de08e9fa45193b5a29ff990Richard Smith .Case("cxx_init_captures", LangOpts.CPlusPlus1y) 959a4fb3390853356524416bc28d6044e7a41c87863Richard Smith .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus1y) 960f45c2992a3aac7591310cd824b7c7319afd432fcRichard Smith .Case("cxx_return_type_deduction", LangOpts.CPlusPlus1y) 961629bc52604e62f016474314077193ed30f8782f9Richard Smith .Case("cxx_variable_templates", LangOpts.CPlusPlus1y) 9626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // C++ TSes 9636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays) 9646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts) 9656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // FIXME: Should this be __has_feature or __has_extension? 9666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines //.Case("raw_invocation_type", LangOpts.CPlusPlus) 9673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Type traits 9683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_nothrow_assign", LangOpts.CPlusPlus) 9693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_nothrow_copy", LangOpts.CPlusPlus) 9703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_nothrow_constructor", LangOpts.CPlusPlus) 9713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_trivial_assign", LangOpts.CPlusPlus) 9723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_trivial_copy", LangOpts.CPlusPlus) 9733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_trivial_constructor", LangOpts.CPlusPlus) 9743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_trivial_destructor", LangOpts.CPlusPlus) 9753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("has_virtual_destructor", LangOpts.CPlusPlus) 9763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_abstract", LangOpts.CPlusPlus) 9773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_base_of", LangOpts.CPlusPlus) 9783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_class", LangOpts.CPlusPlus) 979651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines .Case("is_constructible", LangOpts.CPlusPlus) 9803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_convertible_to", LangOpts.CPlusPlus) 9813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_empty", LangOpts.CPlusPlus) 9823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_enum", LangOpts.CPlusPlus) 9833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_final", LangOpts.CPlusPlus) 9843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_literal", LangOpts.CPlusPlus) 9853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_standard_layout", LangOpts.CPlusPlus) 9863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_pod", LangOpts.CPlusPlus) 9873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_polymorphic", LangOpts.CPlusPlus) 9887121bdb91b86f6053765bda18dd0a8a118929aceDavid Majnemer .Case("is_sealed", LangOpts.MicrosoftExt) 9893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_trivial", LangOpts.CPlusPlus) 9903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_trivially_assignable", LangOpts.CPlusPlus) 9913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_trivially_constructible", LangOpts.CPlusPlus) 9923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_trivially_copyable", LangOpts.CPlusPlus) 9933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("is_union", LangOpts.CPlusPlus) 9943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("modules", LangOpts.Modules) 9953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("tls", PP.getTargetInfo().isTLSSupported()) 9963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("underlying_type", LangOpts.CPlusPlus) 9973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Default(false); 9983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 9993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// HasExtension - Return true if we recognize and implement the feature 10013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// specified by the identifier, either as an extension or a standard language 10023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// feature. 10033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) { 10043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (HasFeature(PP, II)) 10053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return true; 10063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If the use of an extension results in an error diagnostic, extensions are 10083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // effectively unavailable, so just return false here. 1009ef8225444452a1486bd721f3285301fe84643b00Stephen Hines if (PP.getDiagnostics().getExtensionHandlingBehavior() >= 1010ef8225444452a1486bd721f3285301fe84643b00Stephen Hines diag::Severity::Error) 10113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 10123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const LangOptions &LangOpts = PP.getLangOpts(); 10143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos StringRef Extension = II->getName(); 10153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Normalize the extension name, __foo__ becomes foo. 10173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Extension.startswith("__") && Extension.endswith("__") && 10183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Extension.size() >= 4) 10193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Extension = Extension.substr(2, Extension.size() - 4); 10203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Because we inherit the feature list from HasFeature, this string switch 10223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // must be less restrictive than HasFeature's. 10233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return llvm::StringSwitch<bool>(Extension) 10243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C11 features supported by other languages as extensions. 10253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_alignas", true) 10263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_atomic", true) 10273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_generic_selections", true) 10283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("c_static_assert", true) 1029fa7d53fa31516920d3425f5b2095d3fefb38ede2Ed Schouten .Case("c_thread_local", PP.getTargetInfo().isTLSSupported()) 10307f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith // C++11 features supported by other languages as extensions. 10313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_atomic", LangOpts.CPlusPlus) 10323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_deleted_functions", LangOpts.CPlusPlus) 10333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) 10343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) 10353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) 10363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) 10373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_override_control", LangOpts.CPlusPlus) 10383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_range_for", LangOpts.CPlusPlus) 10393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) 10403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Case("cxx_rvalue_references", LangOpts.CPlusPlus) 10417f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith // C++1y features supported by other languages as extensions. 10427f0ffb3a9f4ce7428fd4f6eb3c3274c867c134f8Richard Smith .Case("cxx_binary_literals", true) 104344868f5b6a0980744de08e9fa45193b5a29ff990Richard Smith .Case("cxx_init_captures", LangOpts.CPlusPlus11) 1044651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines .Case("cxx_variable_templates", LangOpts.CPlusPlus) 10453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos .Default(false); 10463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 10473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// EvaluateHasIncludeCommon - Process a '__has_include("path")' 10493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// or '__has_include_next("path")' expression. 10503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Returns true if successful. 10513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool EvaluateHasIncludeCommon(Token &Tok, 10523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IdentifierInfo *II, Preprocessor &PP, 10533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const DirectoryLookup *LookupFrom) { 105497bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu // Save the location of the current token. If a '(' is later found, use 10556b716c58565ebd325f3fb989cfa92ed0934db8c5Aaron Ballman // that location. If not, use the end of this location instead. 105697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu SourceLocation LParenLoc = Tok.getLocation(); 10573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 105831672b1745195d709b641f1c0f44e203742fa73bAaron Ballman // These expressions are only allowed within a preprocessor directive. 105931672b1745195d709b641f1c0f44e203742fa73bAaron Ballman if (!PP.isParsingIfOrElifDirective()) { 106031672b1745195d709b641f1c0f44e203742fa73bAaron Ballman PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName(); 106131672b1745195d709b641f1c0f44e203742fa73bAaron Ballman return false; 106231672b1745195d709b641f1c0f44e203742fa73bAaron Ballman } 106331672b1745195d709b641f1c0f44e203742fa73bAaron Ballman 10643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Get '('. 10653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PP.LexNonComment(Tok); 10663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Ensure we have a '('. 10683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Tok.isNot(tok::l_paren)) { 106997bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu // No '(', use end of last token. 107097bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu LParenLoc = PP.getLocForEndOfToken(LParenLoc); 1071651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren; 107297bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu // If the next token looks like a filename or the start of one, 107397bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu // assume it is and process it as such. 107497bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) && 107597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu !Tok.is(tok::less)) 107697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu return false; 107797bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu } else { 107897bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu // Save '(' location for possible missing ')' message. 107997bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu LParenLoc = Tok.getLocation(); 10803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1081a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman if (PP.getCurrentLexer()) { 1082a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman // Get the file name. 1083a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman PP.getCurrentLexer()->LexIncludeFilename(Tok); 1084a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman } else { 1085a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman // We're in a macro, so we can't use LexIncludeFilename; just 1086a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman // grab the next token. 1087a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman PP.Lex(Tok); 1088a0f2d02d166880e5d826074b6c2ecbf64f8ca70dEli Friedman } 108997bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu } 10903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Reserve a buffer to get the spelling. 10923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SmallString<128> FilenameBuffer; 10933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos StringRef Filename; 10943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation EndLoc; 10953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 10963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos switch (Tok.getKind()) { 10973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos case tok::eod: 10983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If the token kind is EOD, the error has already been diagnosed. 10993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 11003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos case tok::angle_string_literal: 11023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos case tok::string_literal: { 11033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool Invalid = false; 11043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); 11053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Invalid) 11063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 11073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 11083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 11093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos case tok::less: 11113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // This could be a <foo/bar.h> file coming from a macro expansion. In this 11123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // case, glue the tokens together into FilenameBuffer and interpret those. 11133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos FilenameBuffer.push_back('<'); 111497bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) { 111597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu // Let the caller know a <eod> was found by changing the Token kind. 111697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu Tok.setKind(tok::eod); 11173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; // Found <eod> but no ">"? Diagnostic already emitted. 111897bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu } 11193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Filename = FilenameBuffer.str(); 11203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 11213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos default: 11223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); 11233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 11243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 11253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 112697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu SourceLocation FilenameLoc = Tok.getLocation(); 112797bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu 11283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Get ')'. 11293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PP.LexNonComment(Tok); 11303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Ensure we have a trailing ). 11323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Tok.isNot(tok::r_paren)) { 1133651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after) 1134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << II << tok::r_paren; 1135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 11363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 11373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 11383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); 11403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If GetIncludeFilenameSpelling set the start ptr to null, there was an 11413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // error. 11423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Filename.empty()) 11433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return false; 11443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Search include directories. 11463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const DirectoryLookup *CurDir; 11473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const FileEntry *File = 11486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, CurDir, 11496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines nullptr, nullptr, nullptr); 11503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Get the result value. A result of true means the file exists. 11526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return File != nullptr; 11533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 11543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// EvaluateHasInclude - Process a '__has_include("path")' expression. 11563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Returns true if successful. 11573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, 11583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Preprocessor &PP) { 11596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return EvaluateHasIncludeCommon(Tok, II, PP, nullptr); 11603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 11613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. 11633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// Returns true if successful. 11643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosstatic bool EvaluateHasIncludeNext(Token &Tok, 11653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IdentifierInfo *II, Preprocessor &PP) { 11663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // __has_include_next is like __has_include, except that we start 11673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // searching after the current found directory. If we can't do this, 11683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // issue a diagnostic. 11693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const DirectoryLookup *Lookup = PP.GetCurDirLookup(); 11703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (PP.isInPrimaryFile()) { 11716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Lookup = nullptr; 11723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PP.Diag(Tok, diag::pp_include_next_in_primary); 11736bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines } else if (!Lookup) { 11743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PP.Diag(Tok, diag::pp_include_next_absolute_path); 11753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else { 11763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Start looking up in the next directory. 11773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++Lookup; 11783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 11793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 11803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return EvaluateHasIncludeCommon(Tok, II, PP, Lookup); 11813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 11823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 1183b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor/// \brief Process __building_module(identifier) expression. 1184b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor/// \returns true if we are building the named module, false otherwise. 1185b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregorstatic bool EvaluateBuildingModule(Token &Tok, 1186b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor IdentifierInfo *II, Preprocessor &PP) { 1187b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Get '('. 1188b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor PP.LexNonComment(Tok); 1189b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1190b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Ensure we have a '('. 1191b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor if (Tok.isNot(tok::l_paren)) { 1192651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1193651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << tok::l_paren; 1194b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor return false; 1195b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } 1196b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1197b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Save '(' location for possible missing ')' message. 1198b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor SourceLocation LParenLoc = Tok.getLocation(); 1199b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1200b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Get the module name. 1201b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor PP.LexNonComment(Tok); 1202b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1203b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Ensure that we have an identifier. 1204b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor if (Tok.isNot(tok::identifier)) { 1205b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor PP.Diag(Tok.getLocation(), diag::err_expected_id_building_module); 1206b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor return false; 1207b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } 1208b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1209b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor bool Result 1210b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor = Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule; 1211b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1212b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Get ')'. 1213b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor PP.LexNonComment(Tok); 1214b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1215b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // Ensure we have a trailing ). 1216b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor if (Tok.isNot(tok::r_paren)) { 1217651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II 1218651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << tok::r_paren; 1219651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1220b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor return false; 1221b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } 1222b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 1223b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor return Result; 1224b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor} 1225b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor 12263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded 12273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos/// as a builtin macro, handle it and return the next token as 'Tok'. 12283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::ExpandBuiltinMacro(Token &Tok) { 12293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Figure out which token this is. 12303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IdentifierInfo *II = Tok.getIdentifierInfo(); 12313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(II && "Can't be a macro without id info!"); 12323e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If this is an _Pragma or Microsoft __pragma directive, expand it, 12343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // invoke the pragma handler, then lex the token after it. 12353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (II == Ident_Pragma) 12363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return Handle_Pragma(Tok); 12373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else if (II == Ident__pragma) // in non-MS mode this is null 12383e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return HandleMicrosoft__pragma(Tok); 12393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ++NumBuiltinMacroExpanded; 12413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SmallString<128> TmpBuffer; 12433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos llvm::raw_svector_ostream OS(TmpBuffer); 12443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Set up the return result. 12466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Tok.setIdentifierInfo(nullptr); 12473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.clearFlag(Token::NeedsCleaning); 12483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (II == Ident__LINE__) { 12503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C99 6.10.8: "__LINE__: The presumed line number (within the current 12513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // source file) of the current source line (an integer constant)". This can 12523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // be affected by #line. 12533e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation Loc = Tok.getLocation(); 12543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Advance to the location of the first _, this might not be the first byte 12563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // of the token if it starts with an escaped newline. 12573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Loc = AdvanceToTokenCharacter(Loc, 0); 12583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // One wrinkle here is that GCC expands __LINE__ to location of the *end* of 12603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // a macro expansion. This doesn't matter for object-like macros, but 12613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // can matter for a function-like macro that expands to contain __LINE__. 12623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Skip down through expansion points until we find a file loc for the 12633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // end of the expansion history. 12643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Loc = SourceMgr.getExpansionRange(Loc).second; 12653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); 12663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // __LINE__ expands to a simple numeric value. 12683e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << (PLoc.isValid()? PLoc.getLine() : 1); 12693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::numeric_constant); 12703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { 12713e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // C99 6.10.8: "__FILE__: The presumed name of the current source file (a 12723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // character string literal)". This can be affected by #line. 12733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 12743e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12753e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // __BASE_FILE__ is a GNU extension that returns the top of the presumed 12763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // #include stack instead of the current file. 12773e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (II == Ident__BASE_FILE__ && PLoc.isValid()) { 12783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation NextLoc = PLoc.getIncludeLoc(); 12793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos while (NextLoc.isValid()) { 12803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PLoc = SourceMgr.getPresumedLoc(NextLoc); 12813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (PLoc.isInvalid()) 12823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos break; 12833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos NextLoc = PLoc.getIncludeLoc(); 12853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 12863e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 12873e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 12883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Escape this filename. Turn '\' -> '\\' '"' -> '\"' 12893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SmallString<128> FN; 12903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (PLoc.isValid()) { 12913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos FN += PLoc.getFilename(); 12923e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Lexer::Stringify(FN); 12933e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << '"' << FN.str() << '"'; 12943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 12953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::string_literal); 12963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__DATE__) { 1297ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Diag(Tok.getLocation(), diag::warn_pp_date_time); 12983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!DATELoc.isValid()) 12993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ComputeDATE_TIME(DATELoc, TIMELoc, *this); 13003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::string_literal); 13013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setLength(strlen("\"Mmm dd yyyy\"")); 13023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), 13033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.getLocation(), 13043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.getLength())); 13053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return; 13063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__TIME__) { 1307ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Diag(Tok.getLocation(), diag::warn_pp_date_time); 13083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!TIMELoc.isValid()) 13093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos ComputeDATE_TIME(DATELoc, TIMELoc, *this); 13103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::string_literal); 13113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setLength(strlen("\"hh:mm:ss\"")); 13123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), 13133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.getLocation(), 13143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.getLength())); 13153e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos return; 13163e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__INCLUDE_LEVEL__) { 13173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Compute the presumed include depth of this token. This can be affected 13183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // by GNU line markers. 13193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos unsigned Depth = 0; 13203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 13223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (PLoc.isValid()) { 13233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 13243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos for (; PLoc.isValid(); ++Depth) 13253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); 13263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 13273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // __INCLUDE_LEVEL__ expands to a simple numeric value. 13293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << Depth; 13303e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::numeric_constant); 13313e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__TIMESTAMP__) { 1332ef8225444452a1486bd721f3285301fe84643b00Stephen Hines Diag(Tok.getLocation(), diag::warn_pp_date_time); 13333e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be 13343e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. 13353e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13363e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Get the file that we are lexing out of. If we're currently lexing from 13373e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // a macro, dig into the include stack. 13386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const FileEntry *CurFile = nullptr; 13393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos PreprocessorLexer *TheLexer = getCurrentFileLexer(); 13403e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (TheLexer) 13423e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); 13433e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13443e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos const char *Result; 13453e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (CurFile) { 13463e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos time_t TT = CurFile->getModificationTime(); 13473e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos struct tm *TM = localtime(&TT); 13483e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Result = asctime(TM); 13493e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else { 13503e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Result = "??? ??? ?? ??:??:?? ????\n"; 13513e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 13523e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Surround the string with " and strip the trailing newline. 1353ef8225444452a1486bd721f3285301fe84643b00Stephen Hines OS << '"' << StringRef(Result).drop_back() << '"'; 13543e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::string_literal); 13553e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__COUNTER__) { 13563e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // __COUNTER__ expands to a simple numeric value. 13573e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << CounterValue++; 13583e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::numeric_constant); 13593e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__has_feature || 13603e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos II == Ident__has_extension || 13613e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos II == Ident__has_builtin || 13626bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines II == Ident__is_identifier || 13633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos II == Ident__has_attribute) { 13643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // The argument to these builtins should be a parenthesized identifier. 13653e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation StartLoc = Tok.getLocation(); 13663e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13673e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool IsValid = false; 13686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines IdentifierInfo *FeatureII = nullptr; 13693e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13703e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Read the '('. 13713f03b586351779be6947466f530f22c491b1b70fAndy Gibbs LexUnexpandedToken(Tok); 13723e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Tok.is(tok::l_paren)) { 13733e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Read the identifier 13743f03b586351779be6947466f530f22c491b1b70fAndy Gibbs LexUnexpandedToken(Tok); 1375899022b14bc3bde19a949fb042711864ca3a629bRichard Smith if ((FeatureII = Tok.getIdentifierInfo())) { 13763e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Read the ')'. 13773f03b586351779be6947466f530f22c491b1b70fAndy Gibbs LexUnexpandedToken(Tok); 13783e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (Tok.is(tok::r_paren)) 13793e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos IsValid = true; 13803e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 13813e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 13823e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 13833e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool Value = false; 13843e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (!IsValid) 13853e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Diag(StartLoc, diag::err_feature_check_malformed); 13866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines else if (II == Ident__is_identifier) 13876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines Value = FeatureII->getTokenID() == tok::identifier; 13883e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else if (II == Ident__has_builtin) { 13893e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Check for a builtin is trivial. 13903e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Value = FeatureII->getBuiltinID() != 0; 13913e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__has_attribute) 1392651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Value = hasAttribute(AttrSyntax::Generic, nullptr, FeatureII, 1393651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines getTargetInfo().getTriple(), getLangOpts()); 13943e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else if (II == Ident__has_extension) 13953e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Value = HasExtension(*this, FeatureII); 13963e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else { 13973e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos assert(II == Ident__has_feature && "Must be feature check"); 13983e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Value = HasFeature(*this, FeatureII); 13993e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 14003e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 14013e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << (int)Value; 14023e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (IsValid) 14033e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Tok.setKind(tok::numeric_constant); 14043e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__has_include || 14053e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos II == Ident__has_include_next) { 14063e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // The argument to these two builtins should be a parenthesized 14073e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // file name string literal using angle brackets (<>) or 14083e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // double-quotes (""). 14093e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool Value; 14103e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (II == Ident__has_include) 14113e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Value = EvaluateHasInclude(Tok, II, *this); 14123e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos else 14133e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos Value = EvaluateHasIncludeNext(Tok, II, *this); 14143e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << (int)Value; 141597bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu if (Tok.is(tok::r_paren)) 141697bc3d54a95153945d14b1ee812837160d45ed90Richard Trieu Tok.setKind(tok::numeric_constant); 14173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else if (II == Ident__has_warning) { 14183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // The argument should be a parenthesized string literal. 14193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // The argument to these builtins should be a parenthesized identifier. 14203e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos SourceLocation StartLoc = Tok.getLocation(); 14213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool IsValid = false; 14223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos bool Value = false; 14233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // Read the '('. 142402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs LexUnexpandedToken(Tok); 14253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos do { 142602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs if (Tok.isNot(tok::l_paren)) { 142702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs Diag(StartLoc, diag::err_warning_check_malformed); 142802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs break; 142902a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs } 143002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs 143102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs LexUnexpandedToken(Tok); 143202a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs std::string WarningName; 143302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs SourceLocation StrStartLoc = Tok.getLocation(); 143497f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", 143597f8461a2c553f68a258612d2322e4281c3f0915Andy Gibbs /*MacroExpansion=*/false)) { 143602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs // Eat tokens until ')'. 14376d534d45a598e2ee1c24e5b286f8fab4ad89fb56Andy Gibbs while (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eod) && 14386d534d45a598e2ee1c24e5b286f8fab4ad89fb56Andy Gibbs Tok.isNot(tok::eof)) 14393e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos LexUnexpandedToken(Tok); 144002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs break; 14413e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 144202a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs 144302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs // Is the end a ')'? 144402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs if (!(IsValid = Tok.is(tok::r_paren))) { 144502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs Diag(StartLoc, diag::err_warning_check_malformed); 144602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs break; 144702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs } 144802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs 144902a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs if (WarningName.size() < 3 || WarningName[0] != '-' || 145002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs WarningName[1] != 'W') { 145102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs Diag(StrStartLoc, diag::warn_has_warning_invalid_option); 145202a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs break; 145302a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs } 145402a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs 145502a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs // Finally, check if the warning flags maps to a diagnostic group. 145602a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs // We construct a SmallVector here to talk to getDiagnosticIDs(). 145702a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs // Although we don't use the result, this isn't a hot path, and not 145802a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs // worth special casing. 1459cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<diag::kind, 10> Diags; 146002a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs Value = !getDiagnostics().getDiagnosticIDs()-> 146102a176871d91bba3004e4f94b2d4d588ae4b2122Andy Gibbs getDiagnosticsInGroup(WarningName.substr(2), Diags); 14623e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } while (false); 14633e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 14643e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos OS << (int)Value; 1465b9971bada4eeae74883b61ba96fc6d983b6b0e7fAndy Gibbs if (IsValid) 1466b9971bada4eeae74883b61ba96fc6d983b6b0e7fAndy Gibbs Tok.setKind(tok::numeric_constant); 1467b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } else if (II == Ident__building_module) { 1468b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // The argument to this builtin should be an identifier. The 1469b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // builtin evaluates to 1 when that identifier names the module we are 1470b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // currently building. 1471b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor OS << (int)EvaluateBuildingModule(Tok, II, *this); 1472b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor Tok.setKind(tok::numeric_constant); 1473b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor } else if (II == Ident__MODULE__) { 1474b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor // The current module as an identifier. 1475b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor OS << getLangOpts().CurrentModule; 1476b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); 1477b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor Tok.setIdentifierInfo(ModuleII); 1478b09de5177ee8101818a59dcd0038c75b190a2509Douglas Gregor Tok.setKind(ModuleII->getTokenID()); 1479651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } else if (II == Ident__identifier) { 1480651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines SourceLocation Loc = Tok.getLocation(); 1481651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1482651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // We're expecting '__identifier' '(' identifier ')'. Try to recover 1483651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // if the parens are missing. 1484651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LexNonComment(Tok); 1485651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (Tok.isNot(tok::l_paren)) { 1486651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // No '(', use end of last token. 1487651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after) 1488651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << II << tok::l_paren; 1489651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // If the next token isn't valid as our argument, we can't recover. 1490651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1491651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Tok.setKind(tok::identifier); 1492651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return; 1493651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 1494651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1495651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines SourceLocation LParenLoc = Tok.getLocation(); 1496651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LexNonComment(Tok); 1497651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1498651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) 1499651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Tok.setKind(tok::identifier); 1500651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines else { 1501651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier) 1502651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << Tok.getKind(); 1503651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Don't walk past anything that's not a real token. 1504651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (Tok.is(tok::eof) || Tok.is(tok::eod) || Tok.isAnnotation()) 1505651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return; 1506651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 1507651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1508651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Discard the ')', preserving 'Tok' as our result. 1509651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Token RParen; 1510651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LexNonComment(RParen); 1511651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (RParen.isNot(tok::r_paren)) { 1512651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after) 1513651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines << Tok.getKind() << tok::r_paren; 1514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Diag(LParenLoc, diag::note_matching) << tok::l_paren; 1515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines } 1516651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines return; 15173e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } else { 15183e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos llvm_unreachable("Unknown identifier!"); 15193e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos } 1520374b3837d676133fcc1eb70a25c8baf8ec4a5c4aDmitri Gribenko CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); 15213e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 15223e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos 15233e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matosvoid Preprocessor::markMacroAsUsed(MacroInfo *MI) { 15243e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // If the 'used' status changed, and the macro requires 'unused' warning, 15253e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos // remove its SourceLocation from the warn-for-unused-macro locations. 15263e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos if (MI->isWarnIfUnused() && !MI->isUsed()) 15273e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); 15283e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos MI->setIsUsed(true); 15293e1ec72e9ef097562774e43bd2847aac57b73d3dJoao Matos} 1530