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