1*f4a2713aSLionel Sambuc //===--- TokenLexer.cpp - Lex from a token stream -------------------------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements the TokenLexer interface. 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc #include "clang/Lex/TokenLexer.h" 15*f4a2713aSLionel Sambuc #include "clang/Lex/MacroArgs.h" 16*f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h" 17*f4a2713aSLionel Sambuc #include "clang/Lex/LexDiagnostic.h" 18*f4a2713aSLionel Sambuc #include "clang/Lex/MacroInfo.h" 19*f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h" 20*f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h" 21*f4a2713aSLionel Sambuc using namespace clang; 22*f4a2713aSLionel Sambuc 23*f4a2713aSLionel Sambuc 24*f4a2713aSLionel Sambuc /// Create a TokenLexer for the specified macro with the specified actual 25*f4a2713aSLionel Sambuc /// arguments. Note that this ctor takes ownership of the ActualArgs pointer. 26*f4a2713aSLionel Sambuc void TokenLexer::Init(Token &Tok, SourceLocation ELEnd, MacroInfo *MI, 27*f4a2713aSLionel Sambuc MacroArgs *Actuals) { 28*f4a2713aSLionel Sambuc // If the client is reusing a TokenLexer, make sure to free any memory 29*f4a2713aSLionel Sambuc // associated with it. 30*f4a2713aSLionel Sambuc destroy(); 31*f4a2713aSLionel Sambuc 32*f4a2713aSLionel Sambuc Macro = MI; 33*f4a2713aSLionel Sambuc ActualArgs = Actuals; 34*f4a2713aSLionel Sambuc CurToken = 0; 35*f4a2713aSLionel Sambuc 36*f4a2713aSLionel Sambuc ExpandLocStart = Tok.getLocation(); 37*f4a2713aSLionel Sambuc ExpandLocEnd = ELEnd; 38*f4a2713aSLionel Sambuc AtStartOfLine = Tok.isAtStartOfLine(); 39*f4a2713aSLionel Sambuc HasLeadingSpace = Tok.hasLeadingSpace(); 40*f4a2713aSLionel Sambuc Tokens = &*Macro->tokens_begin(); 41*f4a2713aSLionel Sambuc OwnsTokens = false; 42*f4a2713aSLionel Sambuc DisableMacroExpansion = false; 43*f4a2713aSLionel Sambuc NumTokens = Macro->tokens_end()-Macro->tokens_begin(); 44*f4a2713aSLionel Sambuc MacroExpansionStart = SourceLocation(); 45*f4a2713aSLionel Sambuc 46*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 47*f4a2713aSLionel Sambuc MacroStartSLocOffset = SM.getNextLocalOffset(); 48*f4a2713aSLionel Sambuc 49*f4a2713aSLionel Sambuc if (NumTokens > 0) { 50*f4a2713aSLionel Sambuc assert(Tokens[0].getLocation().isValid()); 51*f4a2713aSLionel Sambuc assert((Tokens[0].getLocation().isFileID() || Tokens[0].is(tok::comment)) && 52*f4a2713aSLionel Sambuc "Macro defined in macro?"); 53*f4a2713aSLionel Sambuc assert(ExpandLocStart.isValid()); 54*f4a2713aSLionel Sambuc 55*f4a2713aSLionel Sambuc // Reserve a source location entry chunk for the length of the macro 56*f4a2713aSLionel Sambuc // definition. Tokens that get lexed directly from the definition will 57*f4a2713aSLionel Sambuc // have their locations pointing inside this chunk. This is to avoid 58*f4a2713aSLionel Sambuc // creating separate source location entries for each token. 59*f4a2713aSLionel Sambuc MacroDefStart = SM.getExpansionLoc(Tokens[0].getLocation()); 60*f4a2713aSLionel Sambuc MacroDefLength = Macro->getDefinitionLength(SM); 61*f4a2713aSLionel Sambuc MacroExpansionStart = SM.createExpansionLoc(MacroDefStart, 62*f4a2713aSLionel Sambuc ExpandLocStart, 63*f4a2713aSLionel Sambuc ExpandLocEnd, 64*f4a2713aSLionel Sambuc MacroDefLength); 65*f4a2713aSLionel Sambuc } 66*f4a2713aSLionel Sambuc 67*f4a2713aSLionel Sambuc // If this is a function-like macro, expand the arguments and change 68*f4a2713aSLionel Sambuc // Tokens to point to the expanded tokens. 69*f4a2713aSLionel Sambuc if (Macro->isFunctionLike() && Macro->getNumArgs()) 70*f4a2713aSLionel Sambuc ExpandFunctionArguments(); 71*f4a2713aSLionel Sambuc 72*f4a2713aSLionel Sambuc // Mark the macro as currently disabled, so that it is not recursively 73*f4a2713aSLionel Sambuc // expanded. The macro must be disabled only after argument pre-expansion of 74*f4a2713aSLionel Sambuc // function-like macro arguments occurs. 75*f4a2713aSLionel Sambuc Macro->DisableMacro(); 76*f4a2713aSLionel Sambuc } 77*f4a2713aSLionel Sambuc 78*f4a2713aSLionel Sambuc 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel Sambuc /// Create a TokenLexer for the specified token stream. This does not 81*f4a2713aSLionel Sambuc /// take ownership of the specified token vector. 82*f4a2713aSLionel Sambuc void TokenLexer::Init(const Token *TokArray, unsigned NumToks, 83*f4a2713aSLionel Sambuc bool disableMacroExpansion, bool ownsTokens) { 84*f4a2713aSLionel Sambuc // If the client is reusing a TokenLexer, make sure to free any memory 85*f4a2713aSLionel Sambuc // associated with it. 86*f4a2713aSLionel Sambuc destroy(); 87*f4a2713aSLionel Sambuc 88*f4a2713aSLionel Sambuc Macro = 0; 89*f4a2713aSLionel Sambuc ActualArgs = 0; 90*f4a2713aSLionel Sambuc Tokens = TokArray; 91*f4a2713aSLionel Sambuc OwnsTokens = ownsTokens; 92*f4a2713aSLionel Sambuc DisableMacroExpansion = disableMacroExpansion; 93*f4a2713aSLionel Sambuc NumTokens = NumToks; 94*f4a2713aSLionel Sambuc CurToken = 0; 95*f4a2713aSLionel Sambuc ExpandLocStart = ExpandLocEnd = SourceLocation(); 96*f4a2713aSLionel Sambuc AtStartOfLine = false; 97*f4a2713aSLionel Sambuc HasLeadingSpace = false; 98*f4a2713aSLionel Sambuc MacroExpansionStart = SourceLocation(); 99*f4a2713aSLionel Sambuc 100*f4a2713aSLionel Sambuc // Set HasLeadingSpace/AtStartOfLine so that the first token will be 101*f4a2713aSLionel Sambuc // returned unmodified. 102*f4a2713aSLionel Sambuc if (NumToks != 0) { 103*f4a2713aSLionel Sambuc AtStartOfLine = TokArray[0].isAtStartOfLine(); 104*f4a2713aSLionel Sambuc HasLeadingSpace = TokArray[0].hasLeadingSpace(); 105*f4a2713aSLionel Sambuc } 106*f4a2713aSLionel Sambuc } 107*f4a2713aSLionel Sambuc 108*f4a2713aSLionel Sambuc 109*f4a2713aSLionel Sambuc void TokenLexer::destroy() { 110*f4a2713aSLionel Sambuc // If this was a function-like macro that actually uses its arguments, delete 111*f4a2713aSLionel Sambuc // the expanded tokens. 112*f4a2713aSLionel Sambuc if (OwnsTokens) { 113*f4a2713aSLionel Sambuc delete [] Tokens; 114*f4a2713aSLionel Sambuc Tokens = 0; 115*f4a2713aSLionel Sambuc OwnsTokens = false; 116*f4a2713aSLionel Sambuc } 117*f4a2713aSLionel Sambuc 118*f4a2713aSLionel Sambuc // TokenLexer owns its formal arguments. 119*f4a2713aSLionel Sambuc if (ActualArgs) ActualArgs->destroy(PP); 120*f4a2713aSLionel Sambuc } 121*f4a2713aSLionel Sambuc 122*f4a2713aSLionel Sambuc /// Remove comma ahead of __VA_ARGS__, if present, according to compiler dialect 123*f4a2713aSLionel Sambuc /// settings. Returns true if the comma is removed. 124*f4a2713aSLionel Sambuc static bool MaybeRemoveCommaBeforeVaArgs(SmallVectorImpl<Token> &ResultToks, 125*f4a2713aSLionel Sambuc bool &NextTokGetsSpace, 126*f4a2713aSLionel Sambuc bool HasPasteOperator, 127*f4a2713aSLionel Sambuc MacroInfo *Macro, unsigned MacroArgNo, 128*f4a2713aSLionel Sambuc Preprocessor &PP) { 129*f4a2713aSLionel Sambuc // Is the macro argument __VA_ARGS__? 130*f4a2713aSLionel Sambuc if (!Macro->isVariadic() || MacroArgNo != Macro->getNumArgs()-1) 131*f4a2713aSLionel Sambuc return false; 132*f4a2713aSLionel Sambuc 133*f4a2713aSLionel Sambuc // In Microsoft-compatibility mode, a comma is removed in the expansion 134*f4a2713aSLionel Sambuc // of " ... , __VA_ARGS__ " if __VA_ARGS__ is empty. This extension is 135*f4a2713aSLionel Sambuc // not supported by gcc. 136*f4a2713aSLionel Sambuc if (!HasPasteOperator && !PP.getLangOpts().MicrosoftMode) 137*f4a2713aSLionel Sambuc return false; 138*f4a2713aSLionel Sambuc 139*f4a2713aSLionel Sambuc // GCC removes the comma in the expansion of " ... , ## __VA_ARGS__ " if 140*f4a2713aSLionel Sambuc // __VA_ARGS__ is empty, but not in strict C99 mode where there are no 141*f4a2713aSLionel Sambuc // named arguments, where it remains. In all other modes, including C99 142*f4a2713aSLionel Sambuc // with GNU extensions, it is removed regardless of named arguments. 143*f4a2713aSLionel Sambuc // Microsoft also appears to support this extension, unofficially. 144*f4a2713aSLionel Sambuc if (PP.getLangOpts().C99 && !PP.getLangOpts().GNUMode 145*f4a2713aSLionel Sambuc && Macro->getNumArgs() < 2) 146*f4a2713aSLionel Sambuc return false; 147*f4a2713aSLionel Sambuc 148*f4a2713aSLionel Sambuc // Is a comma available to be removed? 149*f4a2713aSLionel Sambuc if (ResultToks.empty() || !ResultToks.back().is(tok::comma)) 150*f4a2713aSLionel Sambuc return false; 151*f4a2713aSLionel Sambuc 152*f4a2713aSLionel Sambuc // Issue an extension diagnostic for the paste operator. 153*f4a2713aSLionel Sambuc if (HasPasteOperator) 154*f4a2713aSLionel Sambuc PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma); 155*f4a2713aSLionel Sambuc 156*f4a2713aSLionel Sambuc // Remove the comma. 157*f4a2713aSLionel Sambuc ResultToks.pop_back(); 158*f4a2713aSLionel Sambuc 159*f4a2713aSLionel Sambuc // If the comma was right after another paste (e.g. "X##,##__VA_ARGS__"), 160*f4a2713aSLionel Sambuc // then removal of the comma should produce a placemarker token (in C99 161*f4a2713aSLionel Sambuc // terms) which we model by popping off the previous ##, giving us a plain 162*f4a2713aSLionel Sambuc // "X" when __VA_ARGS__ is empty. 163*f4a2713aSLionel Sambuc if (!ResultToks.empty() && ResultToks.back().is(tok::hashhash)) 164*f4a2713aSLionel Sambuc ResultToks.pop_back(); 165*f4a2713aSLionel Sambuc 166*f4a2713aSLionel Sambuc // Never add a space, even if the comma, ##, or arg had a space. 167*f4a2713aSLionel Sambuc NextTokGetsSpace = false; 168*f4a2713aSLionel Sambuc return true; 169*f4a2713aSLionel Sambuc } 170*f4a2713aSLionel Sambuc 171*f4a2713aSLionel Sambuc /// Expand the arguments of a function-like macro so that we can quickly 172*f4a2713aSLionel Sambuc /// return preexpanded tokens from Tokens. 173*f4a2713aSLionel Sambuc void TokenLexer::ExpandFunctionArguments() { 174*f4a2713aSLionel Sambuc 175*f4a2713aSLionel Sambuc SmallVector<Token, 128> ResultToks; 176*f4a2713aSLionel Sambuc 177*f4a2713aSLionel Sambuc // Loop through 'Tokens', expanding them into ResultToks. Keep 178*f4a2713aSLionel Sambuc // track of whether we change anything. If not, no need to keep them. If so, 179*f4a2713aSLionel Sambuc // we install the newly expanded sequence as the new 'Tokens' list. 180*f4a2713aSLionel Sambuc bool MadeChange = false; 181*f4a2713aSLionel Sambuc 182*f4a2713aSLionel Sambuc // NextTokGetsSpace - When this is true, the next token appended to the 183*f4a2713aSLionel Sambuc // output list will get a leading space, regardless of whether it had one to 184*f4a2713aSLionel Sambuc // begin with or not. This is used for placemarker support. 185*f4a2713aSLionel Sambuc bool NextTokGetsSpace = false; 186*f4a2713aSLionel Sambuc 187*f4a2713aSLionel Sambuc for (unsigned i = 0, e = NumTokens; i != e; ++i) { 188*f4a2713aSLionel Sambuc // If we found the stringify operator, get the argument stringified. The 189*f4a2713aSLionel Sambuc // preprocessor already verified that the following token is a macro name 190*f4a2713aSLionel Sambuc // when the #define was parsed. 191*f4a2713aSLionel Sambuc const Token &CurTok = Tokens[i]; 192*f4a2713aSLionel Sambuc if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) { 193*f4a2713aSLionel Sambuc int ArgNo = Macro->getArgumentNum(Tokens[i+1].getIdentifierInfo()); 194*f4a2713aSLionel Sambuc assert(ArgNo != -1 && "Token following # is not an argument?"); 195*f4a2713aSLionel Sambuc 196*f4a2713aSLionel Sambuc SourceLocation ExpansionLocStart = 197*f4a2713aSLionel Sambuc getExpansionLocForMacroDefLoc(CurTok.getLocation()); 198*f4a2713aSLionel Sambuc SourceLocation ExpansionLocEnd = 199*f4a2713aSLionel Sambuc getExpansionLocForMacroDefLoc(Tokens[i+1].getLocation()); 200*f4a2713aSLionel Sambuc 201*f4a2713aSLionel Sambuc Token Res; 202*f4a2713aSLionel Sambuc if (CurTok.is(tok::hash)) // Stringify 203*f4a2713aSLionel Sambuc Res = ActualArgs->getStringifiedArgument(ArgNo, PP, 204*f4a2713aSLionel Sambuc ExpansionLocStart, 205*f4a2713aSLionel Sambuc ExpansionLocEnd); 206*f4a2713aSLionel Sambuc else { 207*f4a2713aSLionel Sambuc // 'charify': don't bother caching these. 208*f4a2713aSLionel Sambuc Res = MacroArgs::StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), 209*f4a2713aSLionel Sambuc PP, true, 210*f4a2713aSLionel Sambuc ExpansionLocStart, 211*f4a2713aSLionel Sambuc ExpansionLocEnd); 212*f4a2713aSLionel Sambuc } 213*f4a2713aSLionel Sambuc 214*f4a2713aSLionel Sambuc // The stringified/charified string leading space flag gets set to match 215*f4a2713aSLionel Sambuc // the #/#@ operator. 216*f4a2713aSLionel Sambuc if (CurTok.hasLeadingSpace() || NextTokGetsSpace) 217*f4a2713aSLionel Sambuc Res.setFlag(Token::LeadingSpace); 218*f4a2713aSLionel Sambuc 219*f4a2713aSLionel Sambuc ResultToks.push_back(Res); 220*f4a2713aSLionel Sambuc MadeChange = true; 221*f4a2713aSLionel Sambuc ++i; // Skip arg name. 222*f4a2713aSLionel Sambuc NextTokGetsSpace = false; 223*f4a2713aSLionel Sambuc continue; 224*f4a2713aSLionel Sambuc } 225*f4a2713aSLionel Sambuc 226*f4a2713aSLionel Sambuc // Otherwise, if this is not an argument token, just add the token to the 227*f4a2713aSLionel Sambuc // output buffer. 228*f4a2713aSLionel Sambuc IdentifierInfo *II = CurTok.getIdentifierInfo(); 229*f4a2713aSLionel Sambuc int ArgNo = II ? Macro->getArgumentNum(II) : -1; 230*f4a2713aSLionel Sambuc if (ArgNo == -1) { 231*f4a2713aSLionel Sambuc // This isn't an argument, just add it. 232*f4a2713aSLionel Sambuc ResultToks.push_back(CurTok); 233*f4a2713aSLionel Sambuc 234*f4a2713aSLionel Sambuc if (NextTokGetsSpace) { 235*f4a2713aSLionel Sambuc ResultToks.back().setFlag(Token::LeadingSpace); 236*f4a2713aSLionel Sambuc NextTokGetsSpace = false; 237*f4a2713aSLionel Sambuc } 238*f4a2713aSLionel Sambuc continue; 239*f4a2713aSLionel Sambuc } 240*f4a2713aSLionel Sambuc 241*f4a2713aSLionel Sambuc // An argument is expanded somehow, the result is different than the 242*f4a2713aSLionel Sambuc // input. 243*f4a2713aSLionel Sambuc MadeChange = true; 244*f4a2713aSLionel Sambuc 245*f4a2713aSLionel Sambuc // Otherwise, this is a use of the argument. Find out if there is a paste 246*f4a2713aSLionel Sambuc // (##) operator before or after the argument. 247*f4a2713aSLionel Sambuc bool NonEmptyPasteBefore = 248*f4a2713aSLionel Sambuc !ResultToks.empty() && ResultToks.back().is(tok::hashhash); 249*f4a2713aSLionel Sambuc bool PasteBefore = i != 0 && Tokens[i-1].is(tok::hashhash); 250*f4a2713aSLionel Sambuc bool PasteAfter = i+1 != e && Tokens[i+1].is(tok::hashhash); 251*f4a2713aSLionel Sambuc assert(!NonEmptyPasteBefore || PasteBefore); 252*f4a2713aSLionel Sambuc 253*f4a2713aSLionel Sambuc // In Microsoft mode, remove the comma before __VA_ARGS__ to ensure there 254*f4a2713aSLionel Sambuc // are no trailing commas if __VA_ARGS__ is empty. 255*f4a2713aSLionel Sambuc if (!PasteBefore && ActualArgs->isVarargsElidedUse() && 256*f4a2713aSLionel Sambuc MaybeRemoveCommaBeforeVaArgs(ResultToks, NextTokGetsSpace, 257*f4a2713aSLionel Sambuc /*HasPasteOperator=*/false, 258*f4a2713aSLionel Sambuc Macro, ArgNo, PP)) 259*f4a2713aSLionel Sambuc continue; 260*f4a2713aSLionel Sambuc 261*f4a2713aSLionel Sambuc // If it is not the LHS/RHS of a ## operator, we must pre-expand the 262*f4a2713aSLionel Sambuc // argument and substitute the expanded tokens into the result. This is 263*f4a2713aSLionel Sambuc // C99 6.10.3.1p1. 264*f4a2713aSLionel Sambuc if (!PasteBefore && !PasteAfter) { 265*f4a2713aSLionel Sambuc const Token *ResultArgToks; 266*f4a2713aSLionel Sambuc 267*f4a2713aSLionel Sambuc // Only preexpand the argument if it could possibly need it. This 268*f4a2713aSLionel Sambuc // avoids some work in common cases. 269*f4a2713aSLionel Sambuc const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo); 270*f4a2713aSLionel Sambuc if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP)) 271*f4a2713aSLionel Sambuc ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, Macro, PP)[0]; 272*f4a2713aSLionel Sambuc else 273*f4a2713aSLionel Sambuc ResultArgToks = ArgTok; // Use non-preexpanded tokens. 274*f4a2713aSLionel Sambuc 275*f4a2713aSLionel Sambuc // If the arg token expanded into anything, append it. 276*f4a2713aSLionel Sambuc if (ResultArgToks->isNot(tok::eof)) { 277*f4a2713aSLionel Sambuc unsigned FirstResult = ResultToks.size(); 278*f4a2713aSLionel Sambuc unsigned NumToks = MacroArgs::getArgLength(ResultArgToks); 279*f4a2713aSLionel Sambuc ResultToks.append(ResultArgToks, ResultArgToks+NumToks); 280*f4a2713aSLionel Sambuc 281*f4a2713aSLionel Sambuc // In Microsoft-compatibility mode, we follow MSVC's preprocessing 282*f4a2713aSLionel Sambuc // behavior by not considering single commas from nested macro 283*f4a2713aSLionel Sambuc // expansions as argument separators. Set a flag on the token so we can 284*f4a2713aSLionel Sambuc // test for this later when the macro expansion is processed. 285*f4a2713aSLionel Sambuc if (PP.getLangOpts().MicrosoftMode && NumToks == 1 && 286*f4a2713aSLionel Sambuc ResultToks.back().is(tok::comma)) 287*f4a2713aSLionel Sambuc ResultToks.back().setFlag(Token::IgnoredComma); 288*f4a2713aSLionel Sambuc 289*f4a2713aSLionel Sambuc // If the '##' came from expanding an argument, turn it into 'unknown' 290*f4a2713aSLionel Sambuc // to avoid pasting. 291*f4a2713aSLionel Sambuc for (unsigned i = FirstResult, e = ResultToks.size(); i != e; ++i) { 292*f4a2713aSLionel Sambuc Token &Tok = ResultToks[i]; 293*f4a2713aSLionel Sambuc if (Tok.is(tok::hashhash)) 294*f4a2713aSLionel Sambuc Tok.setKind(tok::unknown); 295*f4a2713aSLionel Sambuc } 296*f4a2713aSLionel Sambuc 297*f4a2713aSLionel Sambuc if(ExpandLocStart.isValid()) { 298*f4a2713aSLionel Sambuc updateLocForMacroArgTokens(CurTok.getLocation(), 299*f4a2713aSLionel Sambuc ResultToks.begin()+FirstResult, 300*f4a2713aSLionel Sambuc ResultToks.end()); 301*f4a2713aSLionel Sambuc } 302*f4a2713aSLionel Sambuc 303*f4a2713aSLionel Sambuc // If any tokens were substituted from the argument, the whitespace 304*f4a2713aSLionel Sambuc // before the first token should match the whitespace of the arg 305*f4a2713aSLionel Sambuc // identifier. 306*f4a2713aSLionel Sambuc ResultToks[FirstResult].setFlagValue(Token::LeadingSpace, 307*f4a2713aSLionel Sambuc CurTok.hasLeadingSpace() || 308*f4a2713aSLionel Sambuc NextTokGetsSpace); 309*f4a2713aSLionel Sambuc NextTokGetsSpace = false; 310*f4a2713aSLionel Sambuc } else { 311*f4a2713aSLionel Sambuc // If this is an empty argument, and if there was whitespace before the 312*f4a2713aSLionel Sambuc // formal token, make sure the next token gets whitespace before it. 313*f4a2713aSLionel Sambuc NextTokGetsSpace = CurTok.hasLeadingSpace(); 314*f4a2713aSLionel Sambuc } 315*f4a2713aSLionel Sambuc continue; 316*f4a2713aSLionel Sambuc } 317*f4a2713aSLionel Sambuc 318*f4a2713aSLionel Sambuc // Okay, we have a token that is either the LHS or RHS of a paste (##) 319*f4a2713aSLionel Sambuc // argument. It gets substituted as its non-pre-expanded tokens. 320*f4a2713aSLionel Sambuc const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo); 321*f4a2713aSLionel Sambuc unsigned NumToks = MacroArgs::getArgLength(ArgToks); 322*f4a2713aSLionel Sambuc if (NumToks) { // Not an empty argument? 323*f4a2713aSLionel Sambuc // If this is the GNU ", ## __VA_ARGS__" extension, and we just learned 324*f4a2713aSLionel Sambuc // that __VA_ARGS__ expands to multiple tokens, avoid a pasting error when 325*f4a2713aSLionel Sambuc // the expander trys to paste ',' with the first token of the __VA_ARGS__ 326*f4a2713aSLionel Sambuc // expansion. 327*f4a2713aSLionel Sambuc if (NonEmptyPasteBefore && ResultToks.size() >= 2 && 328*f4a2713aSLionel Sambuc ResultToks[ResultToks.size()-2].is(tok::comma) && 329*f4a2713aSLionel Sambuc (unsigned)ArgNo == Macro->getNumArgs()-1 && 330*f4a2713aSLionel Sambuc Macro->isVariadic()) { 331*f4a2713aSLionel Sambuc // Remove the paste operator, report use of the extension. 332*f4a2713aSLionel Sambuc PP.Diag(ResultToks.pop_back_val().getLocation(), diag::ext_paste_comma); 333*f4a2713aSLionel Sambuc } 334*f4a2713aSLionel Sambuc 335*f4a2713aSLionel Sambuc ResultToks.append(ArgToks, ArgToks+NumToks); 336*f4a2713aSLionel Sambuc 337*f4a2713aSLionel Sambuc // If the '##' came from expanding an argument, turn it into 'unknown' 338*f4a2713aSLionel Sambuc // to avoid pasting. 339*f4a2713aSLionel Sambuc for (unsigned i = ResultToks.size() - NumToks, e = ResultToks.size(); 340*f4a2713aSLionel Sambuc i != e; ++i) { 341*f4a2713aSLionel Sambuc Token &Tok = ResultToks[i]; 342*f4a2713aSLionel Sambuc if (Tok.is(tok::hashhash)) 343*f4a2713aSLionel Sambuc Tok.setKind(tok::unknown); 344*f4a2713aSLionel Sambuc } 345*f4a2713aSLionel Sambuc 346*f4a2713aSLionel Sambuc if (ExpandLocStart.isValid()) { 347*f4a2713aSLionel Sambuc updateLocForMacroArgTokens(CurTok.getLocation(), 348*f4a2713aSLionel Sambuc ResultToks.end()-NumToks, ResultToks.end()); 349*f4a2713aSLionel Sambuc } 350*f4a2713aSLionel Sambuc 351*f4a2713aSLionel Sambuc // If this token (the macro argument) was supposed to get leading 352*f4a2713aSLionel Sambuc // whitespace, transfer this information onto the first token of the 353*f4a2713aSLionel Sambuc // expansion. 354*f4a2713aSLionel Sambuc // 355*f4a2713aSLionel Sambuc // Do not do this if the paste operator occurs before the macro argument, 356*f4a2713aSLionel Sambuc // as in "A ## MACROARG". In valid code, the first token will get 357*f4a2713aSLionel Sambuc // smooshed onto the preceding one anyway (forming AMACROARG). In 358*f4a2713aSLionel Sambuc // assembler-with-cpp mode, invalid pastes are allowed through: in this 359*f4a2713aSLionel Sambuc // case, we do not want the extra whitespace to be added. For example, 360*f4a2713aSLionel Sambuc // we want ". ## foo" -> ".foo" not ". foo". 361*f4a2713aSLionel Sambuc if ((CurTok.hasLeadingSpace() || NextTokGetsSpace) && 362*f4a2713aSLionel Sambuc !NonEmptyPasteBefore) 363*f4a2713aSLionel Sambuc ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace); 364*f4a2713aSLionel Sambuc 365*f4a2713aSLionel Sambuc NextTokGetsSpace = false; 366*f4a2713aSLionel Sambuc continue; 367*f4a2713aSLionel Sambuc } 368*f4a2713aSLionel Sambuc 369*f4a2713aSLionel Sambuc // If an empty argument is on the LHS or RHS of a paste, the standard (C99 370*f4a2713aSLionel Sambuc // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We 371*f4a2713aSLionel Sambuc // implement this by eating ## operators when a LHS or RHS expands to 372*f4a2713aSLionel Sambuc // empty. 373*f4a2713aSLionel Sambuc NextTokGetsSpace |= CurTok.hasLeadingSpace(); 374*f4a2713aSLionel Sambuc if (PasteAfter) { 375*f4a2713aSLionel Sambuc // Discard the argument token and skip (don't copy to the expansion 376*f4a2713aSLionel Sambuc // buffer) the paste operator after it. 377*f4a2713aSLionel Sambuc NextTokGetsSpace |= Tokens[i+1].hasLeadingSpace(); 378*f4a2713aSLionel Sambuc ++i; 379*f4a2713aSLionel Sambuc continue; 380*f4a2713aSLionel Sambuc } 381*f4a2713aSLionel Sambuc 382*f4a2713aSLionel Sambuc // If this is on the RHS of a paste operator, we've already copied the 383*f4a2713aSLionel Sambuc // paste operator to the ResultToks list, unless the LHS was empty too. 384*f4a2713aSLionel Sambuc // Remove it. 385*f4a2713aSLionel Sambuc assert(PasteBefore); 386*f4a2713aSLionel Sambuc if (NonEmptyPasteBefore) { 387*f4a2713aSLionel Sambuc assert(ResultToks.back().is(tok::hashhash)); 388*f4a2713aSLionel Sambuc NextTokGetsSpace |= ResultToks.pop_back_val().hasLeadingSpace(); 389*f4a2713aSLionel Sambuc } 390*f4a2713aSLionel Sambuc 391*f4a2713aSLionel Sambuc // If this is the __VA_ARGS__ token, and if the argument wasn't provided, 392*f4a2713aSLionel Sambuc // and if the macro had at least one real argument, and if the token before 393*f4a2713aSLionel Sambuc // the ## was a comma, remove the comma. This is a GCC extension which is 394*f4a2713aSLionel Sambuc // disabled when using -std=c99. 395*f4a2713aSLionel Sambuc if (ActualArgs->isVarargsElidedUse()) 396*f4a2713aSLionel Sambuc MaybeRemoveCommaBeforeVaArgs(ResultToks, NextTokGetsSpace, 397*f4a2713aSLionel Sambuc /*HasPasteOperator=*/true, 398*f4a2713aSLionel Sambuc Macro, ArgNo, PP); 399*f4a2713aSLionel Sambuc 400*f4a2713aSLionel Sambuc continue; 401*f4a2713aSLionel Sambuc } 402*f4a2713aSLionel Sambuc 403*f4a2713aSLionel Sambuc // If anything changed, install this as the new Tokens list. 404*f4a2713aSLionel Sambuc if (MadeChange) { 405*f4a2713aSLionel Sambuc assert(!OwnsTokens && "This would leak if we already own the token list"); 406*f4a2713aSLionel Sambuc // This is deleted in the dtor. 407*f4a2713aSLionel Sambuc NumTokens = ResultToks.size(); 408*f4a2713aSLionel Sambuc // The tokens will be added to Preprocessor's cache and will be removed 409*f4a2713aSLionel Sambuc // when this TokenLexer finishes lexing them. 410*f4a2713aSLionel Sambuc Tokens = PP.cacheMacroExpandedTokens(this, ResultToks); 411*f4a2713aSLionel Sambuc 412*f4a2713aSLionel Sambuc // The preprocessor cache of macro expanded tokens owns these tokens,not us. 413*f4a2713aSLionel Sambuc OwnsTokens = false; 414*f4a2713aSLionel Sambuc } 415*f4a2713aSLionel Sambuc } 416*f4a2713aSLionel Sambuc 417*f4a2713aSLionel Sambuc /// Lex - Lex and return a token from this macro stream. 418*f4a2713aSLionel Sambuc /// 419*f4a2713aSLionel Sambuc bool TokenLexer::Lex(Token &Tok) { 420*f4a2713aSLionel Sambuc // Lexing off the end of the macro, pop this macro off the expansion stack. 421*f4a2713aSLionel Sambuc if (isAtEnd()) { 422*f4a2713aSLionel Sambuc // If this is a macro (not a token stream), mark the macro enabled now 423*f4a2713aSLionel Sambuc // that it is no longer being expanded. 424*f4a2713aSLionel Sambuc if (Macro) Macro->EnableMacro(); 425*f4a2713aSLionel Sambuc 426*f4a2713aSLionel Sambuc Tok.startToken(); 427*f4a2713aSLionel Sambuc Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); 428*f4a2713aSLionel Sambuc Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); 429*f4a2713aSLionel Sambuc if (CurToken == 0) 430*f4a2713aSLionel Sambuc Tok.setFlag(Token::LeadingEmptyMacro); 431*f4a2713aSLionel Sambuc return PP.HandleEndOfTokenLexer(Tok); 432*f4a2713aSLionel Sambuc } 433*f4a2713aSLionel Sambuc 434*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 435*f4a2713aSLionel Sambuc 436*f4a2713aSLionel Sambuc // If this is the first token of the expanded result, we inherit spacing 437*f4a2713aSLionel Sambuc // properties later. 438*f4a2713aSLionel Sambuc bool isFirstToken = CurToken == 0; 439*f4a2713aSLionel Sambuc 440*f4a2713aSLionel Sambuc // Get the next token to return. 441*f4a2713aSLionel Sambuc Tok = Tokens[CurToken++]; 442*f4a2713aSLionel Sambuc 443*f4a2713aSLionel Sambuc bool TokenIsFromPaste = false; 444*f4a2713aSLionel Sambuc 445*f4a2713aSLionel Sambuc // If this token is followed by a token paste (##) operator, paste the tokens! 446*f4a2713aSLionel Sambuc // Note that ## is a normal token when not expanding a macro. 447*f4a2713aSLionel Sambuc if (!isAtEnd() && Tokens[CurToken].is(tok::hashhash) && Macro) { 448*f4a2713aSLionel Sambuc // When handling the microsoft /##/ extension, the final token is 449*f4a2713aSLionel Sambuc // returned by PasteTokens, not the pasted token. 450*f4a2713aSLionel Sambuc if (PasteTokens(Tok)) 451*f4a2713aSLionel Sambuc return true; 452*f4a2713aSLionel Sambuc 453*f4a2713aSLionel Sambuc TokenIsFromPaste = true; 454*f4a2713aSLionel Sambuc } 455*f4a2713aSLionel Sambuc 456*f4a2713aSLionel Sambuc // The token's current location indicate where the token was lexed from. We 457*f4a2713aSLionel Sambuc // need this information to compute the spelling of the token, but any 458*f4a2713aSLionel Sambuc // diagnostics for the expanded token should appear as if they came from 459*f4a2713aSLionel Sambuc // ExpansionLoc. Pull this information together into a new SourceLocation 460*f4a2713aSLionel Sambuc // that captures all of this. 461*f4a2713aSLionel Sambuc if (ExpandLocStart.isValid() && // Don't do this for token streams. 462*f4a2713aSLionel Sambuc // Check that the token's location was not already set properly. 463*f4a2713aSLionel Sambuc SM.isBeforeInSLocAddrSpace(Tok.getLocation(), MacroStartSLocOffset)) { 464*f4a2713aSLionel Sambuc SourceLocation instLoc; 465*f4a2713aSLionel Sambuc if (Tok.is(tok::comment)) { 466*f4a2713aSLionel Sambuc instLoc = SM.createExpansionLoc(Tok.getLocation(), 467*f4a2713aSLionel Sambuc ExpandLocStart, 468*f4a2713aSLionel Sambuc ExpandLocEnd, 469*f4a2713aSLionel Sambuc Tok.getLength()); 470*f4a2713aSLionel Sambuc } else { 471*f4a2713aSLionel Sambuc instLoc = getExpansionLocForMacroDefLoc(Tok.getLocation()); 472*f4a2713aSLionel Sambuc } 473*f4a2713aSLionel Sambuc 474*f4a2713aSLionel Sambuc Tok.setLocation(instLoc); 475*f4a2713aSLionel Sambuc } 476*f4a2713aSLionel Sambuc 477*f4a2713aSLionel Sambuc // If this is the first token, set the lexical properties of the token to 478*f4a2713aSLionel Sambuc // match the lexical properties of the macro identifier. 479*f4a2713aSLionel Sambuc if (isFirstToken) { 480*f4a2713aSLionel Sambuc Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); 481*f4a2713aSLionel Sambuc Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); 482*f4a2713aSLionel Sambuc AtStartOfLine = false; 483*f4a2713aSLionel Sambuc HasLeadingSpace = false; 484*f4a2713aSLionel Sambuc } 485*f4a2713aSLionel Sambuc 486*f4a2713aSLionel Sambuc // Handle recursive expansion! 487*f4a2713aSLionel Sambuc if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != 0) { 488*f4a2713aSLionel Sambuc // Change the kind of this identifier to the appropriate token kind, e.g. 489*f4a2713aSLionel Sambuc // turning "for" into a keyword. 490*f4a2713aSLionel Sambuc IdentifierInfo *II = Tok.getIdentifierInfo(); 491*f4a2713aSLionel Sambuc Tok.setKind(II->getTokenID()); 492*f4a2713aSLionel Sambuc 493*f4a2713aSLionel Sambuc // If this identifier was poisoned and from a paste, emit an error. This 494*f4a2713aSLionel Sambuc // won't be handled by Preprocessor::HandleIdentifier because this is coming 495*f4a2713aSLionel Sambuc // from a macro expansion. 496*f4a2713aSLionel Sambuc if (II->isPoisoned() && TokenIsFromPaste) { 497*f4a2713aSLionel Sambuc PP.HandlePoisonedIdentifier(Tok); 498*f4a2713aSLionel Sambuc } 499*f4a2713aSLionel Sambuc 500*f4a2713aSLionel Sambuc if (!DisableMacroExpansion && II->isHandleIdentifierCase()) 501*f4a2713aSLionel Sambuc return PP.HandleIdentifier(Tok); 502*f4a2713aSLionel Sambuc } 503*f4a2713aSLionel Sambuc 504*f4a2713aSLionel Sambuc // Otherwise, return a normal token. 505*f4a2713aSLionel Sambuc return true; 506*f4a2713aSLionel Sambuc } 507*f4a2713aSLionel Sambuc 508*f4a2713aSLionel Sambuc /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ## 509*f4a2713aSLionel Sambuc /// operator. Read the ## and RHS, and paste the LHS/RHS together. If there 510*f4a2713aSLionel Sambuc /// are more ## after it, chomp them iteratively. Return the result as Tok. 511*f4a2713aSLionel Sambuc /// If this returns true, the caller should immediately return the token. 512*f4a2713aSLionel Sambuc bool TokenLexer::PasteTokens(Token &Tok) { 513*f4a2713aSLionel Sambuc SmallString<128> Buffer; 514*f4a2713aSLionel Sambuc const char *ResultTokStrPtr = 0; 515*f4a2713aSLionel Sambuc SourceLocation StartLoc = Tok.getLocation(); 516*f4a2713aSLionel Sambuc SourceLocation PasteOpLoc; 517*f4a2713aSLionel Sambuc do { 518*f4a2713aSLionel Sambuc // Consume the ## operator. 519*f4a2713aSLionel Sambuc PasteOpLoc = Tokens[CurToken].getLocation(); 520*f4a2713aSLionel Sambuc ++CurToken; 521*f4a2713aSLionel Sambuc assert(!isAtEnd() && "No token on the RHS of a paste operator!"); 522*f4a2713aSLionel Sambuc 523*f4a2713aSLionel Sambuc // Get the RHS token. 524*f4a2713aSLionel Sambuc const Token &RHS = Tokens[CurToken]; 525*f4a2713aSLionel Sambuc 526*f4a2713aSLionel Sambuc // Allocate space for the result token. This is guaranteed to be enough for 527*f4a2713aSLionel Sambuc // the two tokens. 528*f4a2713aSLionel Sambuc Buffer.resize(Tok.getLength() + RHS.getLength()); 529*f4a2713aSLionel Sambuc 530*f4a2713aSLionel Sambuc // Get the spelling of the LHS token in Buffer. 531*f4a2713aSLionel Sambuc const char *BufPtr = &Buffer[0]; 532*f4a2713aSLionel Sambuc bool Invalid = false; 533*f4a2713aSLionel Sambuc unsigned LHSLen = PP.getSpelling(Tok, BufPtr, &Invalid); 534*f4a2713aSLionel Sambuc if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer! 535*f4a2713aSLionel Sambuc memcpy(&Buffer[0], BufPtr, LHSLen); 536*f4a2713aSLionel Sambuc if (Invalid) 537*f4a2713aSLionel Sambuc return true; 538*f4a2713aSLionel Sambuc 539*f4a2713aSLionel Sambuc BufPtr = &Buffer[LHSLen]; 540*f4a2713aSLionel Sambuc unsigned RHSLen = PP.getSpelling(RHS, BufPtr, &Invalid); 541*f4a2713aSLionel Sambuc if (Invalid) 542*f4a2713aSLionel Sambuc return true; 543*f4a2713aSLionel Sambuc if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer! 544*f4a2713aSLionel Sambuc memcpy(&Buffer[LHSLen], BufPtr, RHSLen); 545*f4a2713aSLionel Sambuc 546*f4a2713aSLionel Sambuc // Trim excess space. 547*f4a2713aSLionel Sambuc Buffer.resize(LHSLen+RHSLen); 548*f4a2713aSLionel Sambuc 549*f4a2713aSLionel Sambuc // Plop the pasted result (including the trailing newline and null) into a 550*f4a2713aSLionel Sambuc // scratch buffer where we can lex it. 551*f4a2713aSLionel Sambuc Token ResultTokTmp; 552*f4a2713aSLionel Sambuc ResultTokTmp.startToken(); 553*f4a2713aSLionel Sambuc 554*f4a2713aSLionel Sambuc // Claim that the tmp token is a string_literal so that we can get the 555*f4a2713aSLionel Sambuc // character pointer back from CreateString in getLiteralData(). 556*f4a2713aSLionel Sambuc ResultTokTmp.setKind(tok::string_literal); 557*f4a2713aSLionel Sambuc PP.CreateString(Buffer, ResultTokTmp); 558*f4a2713aSLionel Sambuc SourceLocation ResultTokLoc = ResultTokTmp.getLocation(); 559*f4a2713aSLionel Sambuc ResultTokStrPtr = ResultTokTmp.getLiteralData(); 560*f4a2713aSLionel Sambuc 561*f4a2713aSLionel Sambuc // Lex the resultant pasted token into Result. 562*f4a2713aSLionel Sambuc Token Result; 563*f4a2713aSLionel Sambuc 564*f4a2713aSLionel Sambuc if (Tok.isAnyIdentifier() && RHS.isAnyIdentifier()) { 565*f4a2713aSLionel Sambuc // Common paste case: identifier+identifier = identifier. Avoid creating 566*f4a2713aSLionel Sambuc // a lexer and other overhead. 567*f4a2713aSLionel Sambuc PP.IncrementPasteCounter(true); 568*f4a2713aSLionel Sambuc Result.startToken(); 569*f4a2713aSLionel Sambuc Result.setKind(tok::raw_identifier); 570*f4a2713aSLionel Sambuc Result.setRawIdentifierData(ResultTokStrPtr); 571*f4a2713aSLionel Sambuc Result.setLocation(ResultTokLoc); 572*f4a2713aSLionel Sambuc Result.setLength(LHSLen+RHSLen); 573*f4a2713aSLionel Sambuc } else { 574*f4a2713aSLionel Sambuc PP.IncrementPasteCounter(false); 575*f4a2713aSLionel Sambuc 576*f4a2713aSLionel Sambuc assert(ResultTokLoc.isFileID() && 577*f4a2713aSLionel Sambuc "Should be a raw location into scratch buffer"); 578*f4a2713aSLionel Sambuc SourceManager &SourceMgr = PP.getSourceManager(); 579*f4a2713aSLionel Sambuc FileID LocFileID = SourceMgr.getFileID(ResultTokLoc); 580*f4a2713aSLionel Sambuc 581*f4a2713aSLionel Sambuc bool Invalid = false; 582*f4a2713aSLionel Sambuc const char *ScratchBufStart 583*f4a2713aSLionel Sambuc = SourceMgr.getBufferData(LocFileID, &Invalid).data(); 584*f4a2713aSLionel Sambuc if (Invalid) 585*f4a2713aSLionel Sambuc return false; 586*f4a2713aSLionel Sambuc 587*f4a2713aSLionel Sambuc // Make a lexer to lex this string from. Lex just this one token. 588*f4a2713aSLionel Sambuc // Make a lexer object so that we lex and expand the paste result. 589*f4a2713aSLionel Sambuc Lexer TL(SourceMgr.getLocForStartOfFile(LocFileID), 590*f4a2713aSLionel Sambuc PP.getLangOpts(), ScratchBufStart, 591*f4a2713aSLionel Sambuc ResultTokStrPtr, ResultTokStrPtr+LHSLen+RHSLen); 592*f4a2713aSLionel Sambuc 593*f4a2713aSLionel Sambuc // Lex a token in raw mode. This way it won't look up identifiers 594*f4a2713aSLionel Sambuc // automatically, lexing off the end will return an eof token, and 595*f4a2713aSLionel Sambuc // warnings are disabled. This returns true if the result token is the 596*f4a2713aSLionel Sambuc // entire buffer. 597*f4a2713aSLionel Sambuc bool isInvalid = !TL.LexFromRawLexer(Result); 598*f4a2713aSLionel Sambuc 599*f4a2713aSLionel Sambuc // If we got an EOF token, we didn't form even ONE token. For example, we 600*f4a2713aSLionel Sambuc // did "/ ## /" to get "//". 601*f4a2713aSLionel Sambuc isInvalid |= Result.is(tok::eof); 602*f4a2713aSLionel Sambuc 603*f4a2713aSLionel Sambuc // If pasting the two tokens didn't form a full new token, this is an 604*f4a2713aSLionel Sambuc // error. This occurs with "x ## +" and other stuff. Return with Tok 605*f4a2713aSLionel Sambuc // unmodified and with RHS as the next token to lex. 606*f4a2713aSLionel Sambuc if (isInvalid) { 607*f4a2713aSLionel Sambuc // Test for the Microsoft extension of /##/ turning into // here on the 608*f4a2713aSLionel Sambuc // error path. 609*f4a2713aSLionel Sambuc if (PP.getLangOpts().MicrosoftExt && Tok.is(tok::slash) && 610*f4a2713aSLionel Sambuc RHS.is(tok::slash)) { 611*f4a2713aSLionel Sambuc HandleMicrosoftCommentPaste(Tok); 612*f4a2713aSLionel Sambuc return true; 613*f4a2713aSLionel Sambuc } 614*f4a2713aSLionel Sambuc 615*f4a2713aSLionel Sambuc // Do not emit the error when preprocessing assembler code. 616*f4a2713aSLionel Sambuc if (!PP.getLangOpts().AsmPreprocessor) { 617*f4a2713aSLionel Sambuc // Explicitly convert the token location to have proper expansion 618*f4a2713aSLionel Sambuc // information so that the user knows where it came from. 619*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 620*f4a2713aSLionel Sambuc SourceLocation Loc = 621*f4a2713aSLionel Sambuc SM.createExpansionLoc(PasteOpLoc, ExpandLocStart, ExpandLocEnd, 2); 622*f4a2713aSLionel Sambuc // If we're in microsoft extensions mode, downgrade this from a hard 623*f4a2713aSLionel Sambuc // error to a warning that defaults to an error. This allows 624*f4a2713aSLionel Sambuc // disabling it. 625*f4a2713aSLionel Sambuc PP.Diag(Loc, 626*f4a2713aSLionel Sambuc PP.getLangOpts().MicrosoftExt ? diag::err_pp_bad_paste_ms 627*f4a2713aSLionel Sambuc : diag::err_pp_bad_paste) 628*f4a2713aSLionel Sambuc << Buffer.str(); 629*f4a2713aSLionel Sambuc } 630*f4a2713aSLionel Sambuc 631*f4a2713aSLionel Sambuc // An error has occurred so exit loop. 632*f4a2713aSLionel Sambuc break; 633*f4a2713aSLionel Sambuc } 634*f4a2713aSLionel Sambuc 635*f4a2713aSLionel Sambuc // Turn ## into 'unknown' to avoid # ## # from looking like a paste 636*f4a2713aSLionel Sambuc // operator. 637*f4a2713aSLionel Sambuc if (Result.is(tok::hashhash)) 638*f4a2713aSLionel Sambuc Result.setKind(tok::unknown); 639*f4a2713aSLionel Sambuc } 640*f4a2713aSLionel Sambuc 641*f4a2713aSLionel Sambuc // Transfer properties of the LHS over the Result. 642*f4a2713aSLionel Sambuc Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine()); 643*f4a2713aSLionel Sambuc Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace()); 644*f4a2713aSLionel Sambuc 645*f4a2713aSLionel Sambuc // Finally, replace LHS with the result, consume the RHS, and iterate. 646*f4a2713aSLionel Sambuc ++CurToken; 647*f4a2713aSLionel Sambuc Tok = Result; 648*f4a2713aSLionel Sambuc } while (!isAtEnd() && Tokens[CurToken].is(tok::hashhash)); 649*f4a2713aSLionel Sambuc 650*f4a2713aSLionel Sambuc SourceLocation EndLoc = Tokens[CurToken - 1].getLocation(); 651*f4a2713aSLionel Sambuc 652*f4a2713aSLionel Sambuc // The token's current location indicate where the token was lexed from. We 653*f4a2713aSLionel Sambuc // need this information to compute the spelling of the token, but any 654*f4a2713aSLionel Sambuc // diagnostics for the expanded token should appear as if the token was 655*f4a2713aSLionel Sambuc // expanded from the full ## expression. Pull this information together into 656*f4a2713aSLionel Sambuc // a new SourceLocation that captures all of this. 657*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 658*f4a2713aSLionel Sambuc if (StartLoc.isFileID()) 659*f4a2713aSLionel Sambuc StartLoc = getExpansionLocForMacroDefLoc(StartLoc); 660*f4a2713aSLionel Sambuc if (EndLoc.isFileID()) 661*f4a2713aSLionel Sambuc EndLoc = getExpansionLocForMacroDefLoc(EndLoc); 662*f4a2713aSLionel Sambuc FileID MacroFID = SM.getFileID(MacroExpansionStart); 663*f4a2713aSLionel Sambuc while (SM.getFileID(StartLoc) != MacroFID) 664*f4a2713aSLionel Sambuc StartLoc = SM.getImmediateExpansionRange(StartLoc).first; 665*f4a2713aSLionel Sambuc while (SM.getFileID(EndLoc) != MacroFID) 666*f4a2713aSLionel Sambuc EndLoc = SM.getImmediateExpansionRange(EndLoc).second; 667*f4a2713aSLionel Sambuc 668*f4a2713aSLionel Sambuc Tok.setLocation(SM.createExpansionLoc(Tok.getLocation(), StartLoc, EndLoc, 669*f4a2713aSLionel Sambuc Tok.getLength())); 670*f4a2713aSLionel Sambuc 671*f4a2713aSLionel Sambuc // Now that we got the result token, it will be subject to expansion. Since 672*f4a2713aSLionel Sambuc // token pasting re-lexes the result token in raw mode, identifier information 673*f4a2713aSLionel Sambuc // isn't looked up. As such, if the result is an identifier, look up id info. 674*f4a2713aSLionel Sambuc if (Tok.is(tok::raw_identifier)) { 675*f4a2713aSLionel Sambuc // Look up the identifier info for the token. We disabled identifier lookup 676*f4a2713aSLionel Sambuc // by saying we're skipping contents, so we need to do this manually. 677*f4a2713aSLionel Sambuc PP.LookUpIdentifierInfo(Tok); 678*f4a2713aSLionel Sambuc } 679*f4a2713aSLionel Sambuc return false; 680*f4a2713aSLionel Sambuc } 681*f4a2713aSLionel Sambuc 682*f4a2713aSLionel Sambuc /// isNextTokenLParen - If the next token lexed will pop this macro off the 683*f4a2713aSLionel Sambuc /// expansion stack, return 2. If the next unexpanded token is a '(', return 684*f4a2713aSLionel Sambuc /// 1, otherwise return 0. 685*f4a2713aSLionel Sambuc unsigned TokenLexer::isNextTokenLParen() const { 686*f4a2713aSLionel Sambuc // Out of tokens? 687*f4a2713aSLionel Sambuc if (isAtEnd()) 688*f4a2713aSLionel Sambuc return 2; 689*f4a2713aSLionel Sambuc return Tokens[CurToken].is(tok::l_paren); 690*f4a2713aSLionel Sambuc } 691*f4a2713aSLionel Sambuc 692*f4a2713aSLionel Sambuc /// isParsingPreprocessorDirective - Return true if we are in the middle of a 693*f4a2713aSLionel Sambuc /// preprocessor directive. 694*f4a2713aSLionel Sambuc bool TokenLexer::isParsingPreprocessorDirective() const { 695*f4a2713aSLionel Sambuc return Tokens[NumTokens-1].is(tok::eod) && !isAtEnd(); 696*f4a2713aSLionel Sambuc } 697*f4a2713aSLionel Sambuc 698*f4a2713aSLionel Sambuc /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes 699*f4a2713aSLionel Sambuc /// together to form a comment that comments out everything in the current 700*f4a2713aSLionel Sambuc /// macro, other active macros, and anything left on the current physical 701*f4a2713aSLionel Sambuc /// source line of the expanded buffer. Handle this by returning the 702*f4a2713aSLionel Sambuc /// first token on the next line. 703*f4a2713aSLionel Sambuc void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) { 704*f4a2713aSLionel Sambuc // We 'comment out' the rest of this macro by just ignoring the rest of the 705*f4a2713aSLionel Sambuc // tokens that have not been lexed yet, if any. 706*f4a2713aSLionel Sambuc 707*f4a2713aSLionel Sambuc // Since this must be a macro, mark the macro enabled now that it is no longer 708*f4a2713aSLionel Sambuc // being expanded. 709*f4a2713aSLionel Sambuc assert(Macro && "Token streams can't paste comments"); 710*f4a2713aSLionel Sambuc Macro->EnableMacro(); 711*f4a2713aSLionel Sambuc 712*f4a2713aSLionel Sambuc PP.HandleMicrosoftCommentPaste(Tok); 713*f4a2713aSLionel Sambuc } 714*f4a2713aSLionel Sambuc 715*f4a2713aSLionel Sambuc /// \brief If \arg loc is a file ID and points inside the current macro 716*f4a2713aSLionel Sambuc /// definition, returns the appropriate source location pointing at the 717*f4a2713aSLionel Sambuc /// macro expansion source location entry, otherwise it returns an invalid 718*f4a2713aSLionel Sambuc /// SourceLocation. 719*f4a2713aSLionel Sambuc SourceLocation 720*f4a2713aSLionel Sambuc TokenLexer::getExpansionLocForMacroDefLoc(SourceLocation loc) const { 721*f4a2713aSLionel Sambuc assert(ExpandLocStart.isValid() && MacroExpansionStart.isValid() && 722*f4a2713aSLionel Sambuc "Not appropriate for token streams"); 723*f4a2713aSLionel Sambuc assert(loc.isValid() && loc.isFileID()); 724*f4a2713aSLionel Sambuc 725*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 726*f4a2713aSLionel Sambuc assert(SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) && 727*f4a2713aSLionel Sambuc "Expected loc to come from the macro definition"); 728*f4a2713aSLionel Sambuc 729*f4a2713aSLionel Sambuc unsigned relativeOffset = 0; 730*f4a2713aSLionel Sambuc SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength, &relativeOffset); 731*f4a2713aSLionel Sambuc return MacroExpansionStart.getLocWithOffset(relativeOffset); 732*f4a2713aSLionel Sambuc } 733*f4a2713aSLionel Sambuc 734*f4a2713aSLionel Sambuc /// \brief Finds the tokens that are consecutive (from the same FileID) 735*f4a2713aSLionel Sambuc /// creates a single SLocEntry, and assigns SourceLocations to each token that 736*f4a2713aSLionel Sambuc /// point to that SLocEntry. e.g for 737*f4a2713aSLionel Sambuc /// assert(foo == bar); 738*f4a2713aSLionel Sambuc /// There will be a single SLocEntry for the "foo == bar" chunk and locations 739*f4a2713aSLionel Sambuc /// for the 'foo', '==', 'bar' tokens will point inside that chunk. 740*f4a2713aSLionel Sambuc /// 741*f4a2713aSLionel Sambuc /// \arg begin_tokens will be updated to a position past all the found 742*f4a2713aSLionel Sambuc /// consecutive tokens. 743*f4a2713aSLionel Sambuc static void updateConsecutiveMacroArgTokens(SourceManager &SM, 744*f4a2713aSLionel Sambuc SourceLocation InstLoc, 745*f4a2713aSLionel Sambuc Token *&begin_tokens, 746*f4a2713aSLionel Sambuc Token * end_tokens) { 747*f4a2713aSLionel Sambuc assert(begin_tokens < end_tokens); 748*f4a2713aSLionel Sambuc 749*f4a2713aSLionel Sambuc SourceLocation FirstLoc = begin_tokens->getLocation(); 750*f4a2713aSLionel Sambuc SourceLocation CurLoc = FirstLoc; 751*f4a2713aSLionel Sambuc 752*f4a2713aSLionel Sambuc // Compare the source location offset of tokens and group together tokens that 753*f4a2713aSLionel Sambuc // are close, even if their locations point to different FileIDs. e.g. 754*f4a2713aSLionel Sambuc // 755*f4a2713aSLionel Sambuc // |bar | foo | cake | (3 tokens from 3 consecutive FileIDs) 756*f4a2713aSLionel Sambuc // ^ ^ 757*f4a2713aSLionel Sambuc // |bar foo cake| (one SLocEntry chunk for all tokens) 758*f4a2713aSLionel Sambuc // 759*f4a2713aSLionel Sambuc // we can perform this "merge" since the token's spelling location depends 760*f4a2713aSLionel Sambuc // on the relative offset. 761*f4a2713aSLionel Sambuc 762*f4a2713aSLionel Sambuc Token *NextTok = begin_tokens + 1; 763*f4a2713aSLionel Sambuc for (; NextTok < end_tokens; ++NextTok) { 764*f4a2713aSLionel Sambuc SourceLocation NextLoc = NextTok->getLocation(); 765*f4a2713aSLionel Sambuc if (CurLoc.isFileID() != NextLoc.isFileID()) 766*f4a2713aSLionel Sambuc break; // Token from different kind of FileID. 767*f4a2713aSLionel Sambuc 768*f4a2713aSLionel Sambuc int RelOffs; 769*f4a2713aSLionel Sambuc if (!SM.isInSameSLocAddrSpace(CurLoc, NextLoc, &RelOffs)) 770*f4a2713aSLionel Sambuc break; // Token from different local/loaded location. 771*f4a2713aSLionel Sambuc // Check that token is not before the previous token or more than 50 772*f4a2713aSLionel Sambuc // "characters" away. 773*f4a2713aSLionel Sambuc if (RelOffs < 0 || RelOffs > 50) 774*f4a2713aSLionel Sambuc break; 775*f4a2713aSLionel Sambuc CurLoc = NextLoc; 776*f4a2713aSLionel Sambuc } 777*f4a2713aSLionel Sambuc 778*f4a2713aSLionel Sambuc // For the consecutive tokens, find the length of the SLocEntry to contain 779*f4a2713aSLionel Sambuc // all of them. 780*f4a2713aSLionel Sambuc Token &LastConsecutiveTok = *(NextTok-1); 781*f4a2713aSLionel Sambuc int LastRelOffs = 0; 782*f4a2713aSLionel Sambuc SM.isInSameSLocAddrSpace(FirstLoc, LastConsecutiveTok.getLocation(), 783*f4a2713aSLionel Sambuc &LastRelOffs); 784*f4a2713aSLionel Sambuc unsigned FullLength = LastRelOffs + LastConsecutiveTok.getLength(); 785*f4a2713aSLionel Sambuc 786*f4a2713aSLionel Sambuc // Create a macro expansion SLocEntry that will "contain" all of the tokens. 787*f4a2713aSLionel Sambuc SourceLocation Expansion = 788*f4a2713aSLionel Sambuc SM.createMacroArgExpansionLoc(FirstLoc, InstLoc,FullLength); 789*f4a2713aSLionel Sambuc 790*f4a2713aSLionel Sambuc // Change the location of the tokens from the spelling location to the new 791*f4a2713aSLionel Sambuc // expanded location. 792*f4a2713aSLionel Sambuc for (; begin_tokens < NextTok; ++begin_tokens) { 793*f4a2713aSLionel Sambuc Token &Tok = *begin_tokens; 794*f4a2713aSLionel Sambuc int RelOffs = 0; 795*f4a2713aSLionel Sambuc SM.isInSameSLocAddrSpace(FirstLoc, Tok.getLocation(), &RelOffs); 796*f4a2713aSLionel Sambuc Tok.setLocation(Expansion.getLocWithOffset(RelOffs)); 797*f4a2713aSLionel Sambuc } 798*f4a2713aSLionel Sambuc } 799*f4a2713aSLionel Sambuc 800*f4a2713aSLionel Sambuc /// \brief Creates SLocEntries and updates the locations of macro argument 801*f4a2713aSLionel Sambuc /// tokens to their new expanded locations. 802*f4a2713aSLionel Sambuc /// 803*f4a2713aSLionel Sambuc /// \param ArgIdDefLoc the location of the macro argument id inside the macro 804*f4a2713aSLionel Sambuc /// definition. 805*f4a2713aSLionel Sambuc /// \param Tokens the macro argument tokens to update. 806*f4a2713aSLionel Sambuc void TokenLexer::updateLocForMacroArgTokens(SourceLocation ArgIdSpellLoc, 807*f4a2713aSLionel Sambuc Token *begin_tokens, 808*f4a2713aSLionel Sambuc Token *end_tokens) { 809*f4a2713aSLionel Sambuc SourceManager &SM = PP.getSourceManager(); 810*f4a2713aSLionel Sambuc 811*f4a2713aSLionel Sambuc SourceLocation InstLoc = 812*f4a2713aSLionel Sambuc getExpansionLocForMacroDefLoc(ArgIdSpellLoc); 813*f4a2713aSLionel Sambuc 814*f4a2713aSLionel Sambuc while (begin_tokens < end_tokens) { 815*f4a2713aSLionel Sambuc // If there's only one token just create a SLocEntry for it. 816*f4a2713aSLionel Sambuc if (end_tokens - begin_tokens == 1) { 817*f4a2713aSLionel Sambuc Token &Tok = *begin_tokens; 818*f4a2713aSLionel Sambuc Tok.setLocation(SM.createMacroArgExpansionLoc(Tok.getLocation(), 819*f4a2713aSLionel Sambuc InstLoc, 820*f4a2713aSLionel Sambuc Tok.getLength())); 821*f4a2713aSLionel Sambuc return; 822*f4a2713aSLionel Sambuc } 823*f4a2713aSLionel Sambuc 824*f4a2713aSLionel Sambuc updateConsecutiveMacroArgTokens(SM, InstLoc, begin_tokens, end_tokens); 825*f4a2713aSLionel Sambuc } 826*f4a2713aSLionel Sambuc } 827*f4a2713aSLionel Sambuc 828*f4a2713aSLionel Sambuc void TokenLexer::PropagateLineStartLeadingSpaceInfo(Token &Result) { 829*f4a2713aSLionel Sambuc AtStartOfLine = Result.isAtStartOfLine(); 830*f4a2713aSLionel Sambuc HasLeadingSpace = Result.hasLeadingSpace(); 831*f4a2713aSLionel Sambuc } 832