Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 1 | //===--- TokenLexer.cpp - Lex from a token stream -------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 10 | // This file implements the TokenLexer interface. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 5d75de0 | 2008-03-09 02:22:57 +0000 | [diff] [blame] | 14 | #include "clang/Lex/TokenLexer.h" |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame^] | 15 | #include "MacroArgs.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/MacroInfo.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | using namespace clang; |
| 22 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 24 | /// Create a TokenLexer for the specified macro with the specified actual |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | /// arguments. Note that this ctor takes ownership of the ActualArgs pointer. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 26 | void TokenLexer::Init(Token &Tok, MacroArgs *Actuals) { |
| 27 | // If the client is reusing a TokenLexer, make sure to free any memory |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 28 | // associated with it. |
| 29 | destroy(); |
| 30 | |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 31 | Macro = PP.getMacroInfo(Tok.getIdentifierInfo()); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 32 | ActualArgs = Actuals; |
| 33 | CurToken = 0; |
| 34 | InstantiateLoc = Tok.getLocation(); |
| 35 | AtStartOfLine = Tok.isAtStartOfLine(); |
| 36 | HasLeadingSpace = Tok.hasLeadingSpace(); |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 37 | Tokens = &*Macro->tokens_begin(); |
| 38 | OwnsTokens = false; |
| 39 | NumTokens = Macro->tokens_end()-Macro->tokens_begin(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | |
| 41 | // If this is a function-like macro, expand the arguments and change |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 42 | // Tokens to point to the expanded tokens. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | if (Macro->isFunctionLike() && Macro->getNumArgs()) |
| 44 | ExpandFunctionArguments(); |
| 45 | |
| 46 | // Mark the macro as currently disabled, so that it is not recursively |
| 47 | // expanded. The macro must be disabled only after argument pre-expansion of |
| 48 | // function-like macro arguments occurs. |
| 49 | Macro->DisableMacro(); |
| 50 | } |
| 51 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 52 | |
| 53 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 54 | /// Create a TokenLexer for the specified token stream. This does not |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | /// take ownership of the specified token vector. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 56 | void TokenLexer::Init(const Token *TokArray, unsigned NumToks) { |
| 57 | // If the client is reusing a TokenLexer, make sure to free any memory |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 58 | // associated with it. |
| 59 | destroy(); |
| 60 | |
| 61 | Macro = 0; |
| 62 | ActualArgs = 0; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 63 | Tokens = TokArray; |
| 64 | OwnsTokens = false; |
| 65 | NumTokens = NumToks; |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 66 | CurToken = 0; |
| 67 | InstantiateLoc = SourceLocation(); |
| 68 | AtStartOfLine = false; |
| 69 | HasLeadingSpace = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | |
| 71 | // Set HasLeadingSpace/AtStartOfLine so that the first token will be |
| 72 | // returned unmodified. |
| 73 | if (NumToks != 0) { |
| 74 | AtStartOfLine = TokArray[0].isAtStartOfLine(); |
| 75 | HasLeadingSpace = TokArray[0].hasLeadingSpace(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 80 | void TokenLexer::destroy() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | // If this was a function-like macro that actually uses its arguments, delete |
| 82 | // the expanded tokens. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 83 | if (OwnsTokens) { |
| 84 | delete [] Tokens; |
| 85 | Tokens = 0; |
Chris Lattner | 9c68306 | 2007-07-22 01:16:55 +0000 | [diff] [blame] | 86 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 88 | // TokenLexer owns its formal arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | if (ActualArgs) ActualArgs->destroy(); |
| 90 | } |
| 91 | |
| 92 | /// Expand the arguments of a function-like macro so that we can quickly |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 93 | /// return preexpanded tokens from Tokens. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 94 | void TokenLexer::ExpandFunctionArguments() { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 95 | llvm::SmallVector<Token, 128> ResultToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 97 | // Loop through 'Tokens', expanding them into ResultToks. Keep |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | // track of whether we change anything. If not, no need to keep them. If so, |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 99 | // we install the newly expanded sequence as the new 'Tokens' list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | bool MadeChange = false; |
| 101 | |
| 102 | // NextTokGetsSpace - When this is true, the next token appended to the |
| 103 | // output list will get a leading space, regardless of whether it had one to |
| 104 | // begin with or not. This is used for placemarker support. |
| 105 | bool NextTokGetsSpace = false; |
| 106 | |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 107 | for (unsigned i = 0, e = NumTokens; i != e; ++i) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // If we found the stringify operator, get the argument stringified. The |
| 109 | // preprocessor already verified that the following token is a macro name |
| 110 | // when the #define was parsed. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 111 | const Token &CurTok = Tokens[i]; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 112 | if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) { |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 113 | int ArgNo = Macro->getArgumentNum(Tokens[i+1].getIdentifierInfo()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | assert(ArgNo != -1 && "Token following # is not an argument?"); |
| 115 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 116 | Token Res; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 117 | if (CurTok.is(tok::hash)) // Stringify |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | Res = ActualArgs->getStringifiedArgument(ArgNo, PP); |
| 119 | else { |
| 120 | // 'charify': don't bother caching these. |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame^] | 121 | Res = MacroArgs::StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), |
| 122 | PP, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // The stringified/charified string leading space flag gets set to match |
| 126 | // the #/#@ operator. |
| 127 | if (CurTok.hasLeadingSpace() || NextTokGetsSpace) |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 128 | Res.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | |
| 130 | ResultToks.push_back(Res); |
| 131 | MadeChange = true; |
| 132 | ++i; // Skip arg name. |
| 133 | NextTokGetsSpace = false; |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | // Otherwise, if this is not an argument token, just add the token to the |
| 138 | // output buffer. |
| 139 | IdentifierInfo *II = CurTok.getIdentifierInfo(); |
| 140 | int ArgNo = II ? Macro->getArgumentNum(II) : -1; |
| 141 | if (ArgNo == -1) { |
| 142 | // This isn't an argument, just add it. |
| 143 | ResultToks.push_back(CurTok); |
| 144 | |
| 145 | if (NextTokGetsSpace) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 146 | ResultToks.back().setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | NextTokGetsSpace = false; |
| 148 | } |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | // An argument is expanded somehow, the result is different than the |
| 153 | // input. |
| 154 | MadeChange = true; |
| 155 | |
| 156 | // Otherwise, this is a use of the argument. Find out if there is a paste |
| 157 | // (##) operator before or after the argument. |
| 158 | bool PasteBefore = |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 159 | !ResultToks.empty() && ResultToks.back().is(tok::hashhash); |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 160 | bool PasteAfter = i+1 != e && Tokens[i+1].is(tok::hashhash); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | |
| 162 | // If it is not the LHS/RHS of a ## operator, we must pre-expand the |
| 163 | // argument and substitute the expanded tokens into the result. This is |
| 164 | // C99 6.10.3.1p1. |
| 165 | if (!PasteBefore && !PasteAfter) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 166 | const Token *ResultArgToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | |
| 168 | // Only preexpand the argument if it could possibly need it. This |
| 169 | // avoids some work in common cases. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 170 | const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo); |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 171 | if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0]; |
| 173 | else |
| 174 | ResultArgToks = ArgTok; // Use non-preexpanded tokens. |
| 175 | |
| 176 | // If the arg token expanded into anything, append it. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 177 | if (ResultArgToks->isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | unsigned FirstResult = ResultToks.size(); |
| 179 | unsigned NumToks = MacroArgs::getArgLength(ResultArgToks); |
| 180 | ResultToks.append(ResultArgToks, ResultArgToks+NumToks); |
| 181 | |
| 182 | // If any tokens were substituted from the argument, the whitespace |
| 183 | // before the first token should match the whitespace of the arg |
| 184 | // identifier. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 185 | ResultToks[FirstResult].setFlagValue(Token::LeadingSpace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | CurTok.hasLeadingSpace() || |
| 187 | NextTokGetsSpace); |
| 188 | NextTokGetsSpace = false; |
| 189 | } else { |
| 190 | // If this is an empty argument, and if there was whitespace before the |
| 191 | // formal token, make sure the next token gets whitespace before it. |
| 192 | NextTokGetsSpace = CurTok.hasLeadingSpace(); |
| 193 | } |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | // Okay, we have a token that is either the LHS or RHS of a paste (##) |
| 198 | // argument. It gets substituted as its non-pre-expanded tokens. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 199 | const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | unsigned NumToks = MacroArgs::getArgLength(ArgToks); |
| 201 | if (NumToks) { // Not an empty argument? |
Chris Lattner | 71a3a8d | 2008-01-29 07:54:23 +0000 | [diff] [blame] | 202 | // If this is the GNU ", ## __VA_ARG__" extension, and we just learned |
| 203 | // that __VA_ARG__ expands to multiple tokens, avoid a pasting error when |
| 204 | // the expander trys to paste ',' with the first token of the __VA_ARG__ |
| 205 | // expansion. |
| 206 | if (PasteBefore && ResultToks.size() >= 2 && |
| 207 | ResultToks[ResultToks.size()-2].is(tok::comma) && |
| 208 | (unsigned)ArgNo == Macro->getNumArgs()-1 && |
| 209 | Macro->isVariadic()) { |
| 210 | // Remove the paste operator, report use of the extension. |
| 211 | PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma); |
| 212 | ResultToks.pop_back(); |
| 213 | } |
| 214 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 215 | ResultToks.append(ArgToks, ArgToks+NumToks); |
| 216 | |
| 217 | // If the next token was supposed to get leading whitespace, ensure it has |
| 218 | // it now. |
| 219 | if (NextTokGetsSpace) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 220 | ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | NextTokGetsSpace = false; |
| 222 | } |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | // If an empty argument is on the LHS or RHS of a paste, the standard (C99 |
| 227 | // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We |
| 228 | // implement this by eating ## operators when a LHS or RHS expands to |
| 229 | // empty. |
| 230 | NextTokGetsSpace |= CurTok.hasLeadingSpace(); |
| 231 | if (PasteAfter) { |
| 232 | // Discard the argument token and skip (don't copy to the expansion |
| 233 | // buffer) the paste operator after it. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 234 | NextTokGetsSpace |= Tokens[i+1].hasLeadingSpace(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | ++i; |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | // If this is on the RHS of a paste operator, we've already copied the |
| 240 | // paste operator to the ResultToks list. Remove it. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 241 | assert(PasteBefore && ResultToks.back().is(tok::hashhash)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | NextTokGetsSpace |= ResultToks.back().hasLeadingSpace(); |
| 243 | ResultToks.pop_back(); |
| 244 | |
| 245 | // If this is the __VA_ARGS__ token, and if the argument wasn't provided, |
| 246 | // and if the macro had at least one real argument, and if the token before |
| 247 | // the ## was a comma, remove the comma. |
| 248 | if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__ |
| 249 | ActualArgs->isVarargsElidedUse() && // Argument elided. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 250 | !ResultToks.empty() && ResultToks.back().is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | // Never add a space, even if the comma, ##, or arg had a space. |
| 252 | NextTokGetsSpace = false; |
Chris Lattner | 71a3a8d | 2008-01-29 07:54:23 +0000 | [diff] [blame] | 253 | // Remove the paste operator, report use of the extension. |
| 254 | PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | ResultToks.pop_back(); |
| 256 | } |
| 257 | continue; |
| 258 | } |
| 259 | |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 260 | // If anything changed, install this as the new Tokens list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | if (MadeChange) { |
| 262 | // This is deleted in the dtor. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 263 | NumTokens = ResultToks.size(); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 264 | Token *Res = new Token[ResultToks.size()]; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 265 | if (NumTokens) |
| 266 | memcpy(Res, &ResultToks[0], NumTokens*sizeof(Token)); |
| 267 | Tokens = Res; |
| 268 | OwnsTokens = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | /// Lex - Lex and return a token from this macro stream. |
| 273 | /// |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 274 | void TokenLexer::Lex(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | // Lexing off the end of the macro, pop this macro off the expansion stack. |
| 276 | if (isAtEnd()) { |
| 277 | // If this is a macro (not a token stream), mark the macro enabled now |
| 278 | // that it is no longer being expanded. |
| 279 | if (Macro) Macro->EnableMacro(); |
| 280 | |
| 281 | // Pop this context off the preprocessors lexer stack and get the next |
| 282 | // token. This will delete "this" so remember the PP instance var. |
| 283 | Preprocessor &PPCache = PP; |
| 284 | if (PP.HandleEndOfMacro(Tok)) |
| 285 | return; |
| 286 | |
| 287 | // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is |
| 288 | // next. |
| 289 | return PPCache.Lex(Tok); |
| 290 | } |
| 291 | |
| 292 | // If this is the first token of the expanded result, we inherit spacing |
| 293 | // properties later. |
| 294 | bool isFirstToken = CurToken == 0; |
| 295 | |
| 296 | // Get the next token to return. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 297 | Tok = Tokens[CurToken++]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | |
| 299 | // If this token is followed by a token paste (##) operator, paste the tokens! |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 300 | if (!isAtEnd() && Tokens[CurToken].is(tok::hashhash)) |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 301 | if (PasteTokens(Tok)) { |
| 302 | // When handling the microsoft /##/ extension, the final token is |
| 303 | // returned by PasteTokens, not the pasted token. |
| 304 | return; |
| 305 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | |
| 307 | // The token's current location indicate where the token was lexed from. We |
| 308 | // need this information to compute the spelling of the token, but any |
| 309 | // diagnostics for the expanded token should appear as if they came from |
| 310 | // InstantiationLoc. Pull this information together into a new SourceLocation |
| 311 | // that captures all of this. |
| 312 | if (InstantiateLoc.isValid()) { // Don't do this for token streams. |
| 313 | SourceManager &SrcMgr = PP.getSourceManager(); |
Chris Lattner | abca2bb | 2007-07-15 06:35:27 +0000 | [diff] [blame] | 314 | Tok.setLocation(SrcMgr.getInstantiationLoc(Tok.getLocation(), |
| 315 | InstantiateLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // If this is the first token, set the lexical properties of the token to |
| 319 | // match the lexical properties of the macro identifier. |
| 320 | if (isFirstToken) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 321 | Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); |
| 322 | Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Handle recursive expansion! |
| 326 | if (Tok.getIdentifierInfo()) |
| 327 | return PP.HandleIdentifier(Tok); |
| 328 | |
| 329 | // Otherwise, return a normal token. |
| 330 | } |
| 331 | |
| 332 | /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ## |
| 333 | /// operator. Read the ## and RHS, and paste the LHS/RHS together. If there |
| 334 | /// are is another ## after it, chomp it iteratively. Return the result as Tok. |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 335 | /// If this returns true, the caller should immediately return the token. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 336 | bool TokenLexer::PasteTokens(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | llvm::SmallVector<char, 128> Buffer; |
| 338 | do { |
| 339 | // Consume the ## operator. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 340 | SourceLocation PasteOpLoc = Tokens[CurToken].getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | ++CurToken; |
| 342 | assert(!isAtEnd() && "No token on the RHS of a paste operator!"); |
| 343 | |
| 344 | // Get the RHS token. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 345 | const Token &RHS = Tokens[CurToken]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | |
| 347 | bool isInvalid = false; |
| 348 | |
| 349 | // Allocate space for the result token. This is guaranteed to be enough for |
| 350 | // the two tokens and a null terminator. |
| 351 | Buffer.resize(Tok.getLength() + RHS.getLength() + 1); |
| 352 | |
| 353 | // Get the spelling of the LHS token in Buffer. |
| 354 | const char *BufPtr = &Buffer[0]; |
| 355 | unsigned LHSLen = PP.getSpelling(Tok, BufPtr); |
| 356 | if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer! |
| 357 | memcpy(&Buffer[0], BufPtr, LHSLen); |
| 358 | |
| 359 | BufPtr = &Buffer[LHSLen]; |
| 360 | unsigned RHSLen = PP.getSpelling(RHS, BufPtr); |
| 361 | if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer! |
| 362 | memcpy(&Buffer[LHSLen], BufPtr, RHSLen); |
| 363 | |
| 364 | // Add null terminator. |
| 365 | Buffer[LHSLen+RHSLen] = '\0'; |
| 366 | |
| 367 | // Trim excess space. |
| 368 | Buffer.resize(LHSLen+RHSLen+1); |
| 369 | |
| 370 | // Plop the pasted result (including the trailing newline and null) into a |
| 371 | // scratch buffer where we can lex it. |
| 372 | SourceLocation ResultTokLoc = PP.CreateString(&Buffer[0], Buffer.size()); |
| 373 | |
| 374 | // Lex the resultant pasted token into Result. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 375 | Token Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | |
| 377 | // Avoid testing /*, as the lexer would think it is the start of a comment |
| 378 | // and emit an error that it is unterminated. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 379 | if (Tok.is(tok::slash) && RHS.is(tok::star)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | isInvalid = true; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 381 | } else if (Tok.is(tok::identifier) && RHS.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | // Common paste case: identifier+identifier = identifier. Avoid creating |
| 383 | // a lexer and other overhead. |
| 384 | PP.IncrementPasteCounter(true); |
| 385 | Result.startToken(); |
| 386 | Result.setKind(tok::identifier); |
| 387 | Result.setLocation(ResultTokLoc); |
| 388 | Result.setLength(LHSLen+RHSLen); |
| 389 | } else { |
| 390 | PP.IncrementPasteCounter(false); |
| 391 | |
| 392 | // Make a lexer to lex this string from. |
| 393 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 394 | const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc); |
| 395 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | // Make a lexer object so that we lex and expand the paste result. |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame] | 397 | Lexer *TL = new Lexer(ResultTokLoc, PP, ResultStrData, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | ResultStrData+LHSLen+RHSLen /*don't include null*/); |
| 399 | |
| 400 | // Lex a token in raw mode. This way it won't look up identifiers |
| 401 | // automatically, lexing off the end will return an eof token, and |
| 402 | // warnings are disabled. This returns true if the result token is the |
| 403 | // entire buffer. |
| 404 | bool IsComplete = TL->LexRawToken(Result); |
| 405 | |
| 406 | // If we got an EOF token, we didn't form even ONE token. For example, we |
| 407 | // did "/ ## /" to get "//". |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 408 | IsComplete &= Result.isNot(tok::eof); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | isInvalid = !IsComplete; |
| 410 | |
| 411 | // We're now done with the temporary lexer. |
| 412 | delete TL; |
| 413 | } |
| 414 | |
| 415 | // If pasting the two tokens didn't form a full new token, this is an error. |
| 416 | // This occurs with "x ## +" and other stuff. Return with Tok unmodified |
| 417 | // and with RHS as the next token to lex. |
| 418 | if (isInvalid) { |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 419 | // Test for the Microsoft extension of /##/ turning into // here on the |
| 420 | // error path. |
| 421 | if (PP.getLangOptions().Microsoft && Tok.is(tok::slash) && |
| 422 | RHS.is(tok::slash)) { |
| 423 | HandleMicrosoftCommentPaste(Tok); |
| 424 | return true; |
| 425 | } else { |
| 426 | // TODO: If not in assembler language mode. |
| 427 | PP.Diag(PasteOpLoc, diag::err_pp_bad_paste, |
| 428 | std::string(Buffer.begin(), Buffer.end()-1)); |
| 429 | return false; |
| 430 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 433 | // Turn ## into 'unknown' to avoid # ## # from looking like a paste |
| 434 | // operator. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 435 | if (Result.is(tok::hashhash)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 436 | Result.setKind(tok::unknown); |
Chris Lattner | 71a3a8d | 2008-01-29 07:54:23 +0000 | [diff] [blame] | 437 | // FIXME: Turn __VA_ARGS__ into "not a token"? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | |
| 439 | // Transfer properties of the LHS over the the Result. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 440 | Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine()); |
| 441 | Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 442 | |
| 443 | // Finally, replace LHS with the result, consume the RHS, and iterate. |
| 444 | ++CurToken; |
| 445 | Tok = Result; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 446 | } while (!isAtEnd() && Tokens[CurToken].is(tok::hashhash)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 447 | |
| 448 | // Now that we got the result token, it will be subject to expansion. Since |
| 449 | // token pasting re-lexes the result token in raw mode, identifier information |
| 450 | // isn't looked up. As such, if the result is an identifier, look up id info. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 451 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | // Look up the identifier info for the token. We disabled identifier lookup |
| 453 | // by saying we're skipping contents, so we need to do this manually. |
| 454 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
| 455 | } |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 456 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /// isNextTokenLParen - If the next token lexed will pop this macro off the |
| 460 | /// expansion stack, return 2. If the next unexpanded token is a '(', return |
| 461 | /// 1, otherwise return 0. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 462 | unsigned TokenLexer::isNextTokenLParen() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | // Out of tokens? |
| 464 | if (isAtEnd()) |
| 465 | return 2; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 466 | return Tokens[CurToken].is(tok::l_paren); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | } |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 468 | |
| 469 | |
| 470 | /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes |
| 471 | /// together to form a comment that comments out everything in the current |
| 472 | /// macro, other active macros, and anything left on the current physical |
| 473 | /// source line of the instantiated buffer. Handle this by returning the |
| 474 | /// first token on the next line. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame] | 475 | void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) { |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 476 | // We 'comment out' the rest of this macro by just ignoring the rest of the |
| 477 | // tokens that have not been lexed yet, if any. |
| 478 | |
| 479 | // Since this must be a macro, mark the macro enabled now that it is no longer |
| 480 | // being expanded. |
| 481 | assert(Macro && "Token streams can't paste comments"); |
| 482 | Macro->EnableMacro(); |
| 483 | |
| 484 | PP.HandleMicrosoftCommentPaste(Tok); |
| 485 | } |