TokenLexer.cpp 42 KB

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