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 | |
| 14 | #include "clang/Lex/MacroExpander.h" |
| 15 | #include "clang/Lex/MacroInfo.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // MacroArgs Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | /// MacroArgs ctor function - This destroys the vector passed in. |
| 27 | MacroArgs *MacroArgs::create(const MacroInfo *MI, |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 28 | const Token *UnexpArgTokens, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | unsigned NumToks, bool VarargsElided) { |
| 30 | assert(MI->isFunctionLike() && |
| 31 | "Can't have args for an object-like macro!"); |
| 32 | |
| 33 | // Allocate memory for the MacroArgs object with the lexer tokens at the end. |
| 34 | MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) + |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 35 | NumToks*sizeof(Token)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | // Construct the macroargs object. |
| 37 | new (Result) MacroArgs(NumToks, VarargsElided); |
| 38 | |
| 39 | // Copy the actual unexpanded tokens to immediately after the result ptr. |
| 40 | if (NumToks) |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 41 | memcpy(const_cast<Token*>(Result->getUnexpArgument(0)), |
| 42 | UnexpArgTokens, NumToks*sizeof(Token)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | |
| 44 | return Result; |
| 45 | } |
| 46 | |
| 47 | /// destroy - Destroy and deallocate the memory for this object. |
| 48 | /// |
| 49 | void MacroArgs::destroy() { |
| 50 | // Run the dtor to deallocate the vectors. |
| 51 | this->~MacroArgs(); |
| 52 | // Release the memory for the object. |
| 53 | free(this); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /// getArgLength - Given a pointer to an expanded or unexpanded argument, |
| 58 | /// return the number of tokens, not counting the EOF, that make up the |
| 59 | /// argument. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 60 | unsigned MacroArgs::getArgLength(const Token *ArgPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | unsigned NumArgTokens = 0; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 62 | for (; ArgPtr->isNot(tok::eof); ++ArgPtr) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | ++NumArgTokens; |
| 64 | return NumArgTokens; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /// getUnexpArgument - Return the unexpanded tokens for the specified formal. |
| 69 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 70 | const Token *MacroArgs::getUnexpArgument(unsigned Arg) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | // The unexpanded argument tokens start immediately after the MacroArgs object |
| 72 | // in memory. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 73 | const Token *Start = (const Token *)(this+1); |
| 74 | const Token *Result = Start; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | // Scan to find Arg. |
| 76 | for (; Arg; ++Result) { |
| 77 | assert(Result < Start+NumUnexpArgTokens && "Invalid arg #"); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 78 | if (Result->is(tok::eof)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | --Arg; |
| 80 | } |
| 81 | return Result; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected |
| 86 | /// by pre-expansion, return false. Otherwise, conservatively return true. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 87 | bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok, |
| 88 | Preprocessor &PP) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | // If there are no identifiers in the argument list, or if the identifiers are |
| 90 | // known to not be macros, pre-expansion won't modify it. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 91 | for (; ArgTok->isNot(tok::eof); ++ArgTok) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) { |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 93 | if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | // Return true even though the macro could be a function-like macro |
| 95 | // without a following '(' token. |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /// getPreExpArgument - Return the pre-expanded form of the specified |
| 102 | /// argument. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 103 | const std::vector<Token> & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) { |
| 105 | assert(Arg < NumUnexpArgTokens && "Invalid argument number!"); |
| 106 | |
| 107 | // If we have already computed this, return it. |
| 108 | if (PreExpArgTokens.empty()) |
| 109 | PreExpArgTokens.resize(NumUnexpArgTokens); |
| 110 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 111 | std::vector<Token> &Result = PreExpArgTokens[Arg]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | if (!Result.empty()) return Result; |
| 113 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 114 | const Token *AT = getUnexpArgument(Arg); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | unsigned NumToks = getArgLength(AT)+1; // Include the EOF. |
| 116 | |
| 117 | // Otherwise, we have to pre-expand this argument, populating Result. To do |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 118 | // this, we set up a fake TokenLexer to lex from the unexpanded argument |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | // list. With this installed, we lex expanded tokens until we hit the EOF |
| 120 | // token at the end of the unexp list. |
| 121 | PP.EnterTokenStream(AT, NumToks); |
| 122 | |
| 123 | // Lex all of the macro-expanded tokens into Result. |
| 124 | do { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 125 | Result.push_back(Token()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | PP.Lex(Result.back()); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 127 | } while (Result.back().isNot(tok::eof)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | |
| 129 | // Pop the token stream off the top of the stack. We know that the internal |
| 130 | // pointer inside of it is to the "end" of the token stream, but the stack |
| 131 | // will not otherwise be popped until the next token is lexed. The problem is |
| 132 | // that the token may be lexed sometime after the vector of tokens itself is |
| 133 | // destroyed, which would be badness. |
| 134 | PP.RemoveTopOfLexerStack(); |
| 135 | return Result; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of |
| 140 | /// tokens into the literal string token that should be produced by the C # |
| 141 | /// preprocessor operator. |
| 142 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 143 | static Token StringifyArgument(const Token *ArgToks, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | Preprocessor &PP, bool Charify = false) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 145 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | Tok.startToken(); |
| 147 | Tok.setKind(tok::string_literal); |
| 148 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 149 | const Token *ArgTokStart = ArgToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | |
| 151 | // Stringify all the tokens. |
| 152 | std::string Result = "\""; |
| 153 | // FIXME: Optimize this loop to not use std::strings. |
| 154 | bool isFirst = true; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 155 | for (; ArgToks->isNot(tok::eof); ++ArgToks) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 156 | const Token &Tok = *ArgToks; |
Chris Lattner | 2b64fdc | 2007-07-19 16:11:58 +0000 | [diff] [blame] | 157 | if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | Result += ' '; |
| 159 | isFirst = false; |
| 160 | |
| 161 | // If this is a string or character constant, escape the token as specified |
| 162 | // by 6.10.3.2p2. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 163 | if (Tok.is(tok::string_literal) || // "foo" |
| 164 | Tok.is(tok::wide_string_literal) || // L"foo" |
| 165 | Tok.is(tok::char_constant)) { // 'x' and L'x'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | Result += Lexer::Stringify(PP.getSpelling(Tok)); |
| 167 | } else { |
| 168 | // Otherwise, just append the token. |
| 169 | Result += PP.getSpelling(Tok); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // If the last character of the string is a \, and if it isn't escaped, this |
| 174 | // is an invalid string literal, diagnose it as specified in C99. |
| 175 | if (Result[Result.size()-1] == '\\') { |
| 176 | // Count the number of consequtive \ characters. If even, then they are |
| 177 | // just escaped backslashes, otherwise it's an error. |
| 178 | unsigned FirstNonSlash = Result.size()-2; |
| 179 | // Guaranteed to find the starting " if nothing else. |
| 180 | while (Result[FirstNonSlash] == '\\') |
| 181 | --FirstNonSlash; |
| 182 | if ((Result.size()-1-FirstNonSlash) & 1) { |
| 183 | // Diagnose errors for things like: #define F(X) #X / F(\) |
| 184 | PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal); |
| 185 | Result.erase(Result.end()-1); // remove one of the \'s. |
| 186 | } |
| 187 | } |
| 188 | Result += '"'; |
| 189 | |
| 190 | // If this is the charify operation and the result is not a legal character |
| 191 | // constant, diagnose it. |
| 192 | if (Charify) { |
| 193 | // First step, turn double quotes into single quotes: |
| 194 | Result[0] = '\''; |
| 195 | Result[Result.size()-1] = '\''; |
| 196 | |
| 197 | // Check for bogus character. |
| 198 | bool isBad = false; |
| 199 | if (Result.size() == 3) { |
| 200 | isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above. |
| 201 | } else { |
| 202 | isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x' |
| 203 | } |
| 204 | |
| 205 | if (isBad) { |
| 206 | PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify); |
| 207 | Result = "' '"; // Use something arbitrary, but legal. |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | Tok.setLength(Result.size()); |
| 212 | Tok.setLocation(PP.CreateString(&Result[0], Result.size())); |
| 213 | return Tok; |
| 214 | } |
| 215 | |
| 216 | /// getStringifiedArgument - Compute, cache, and return the specified argument |
| 217 | /// that has been 'stringified' as required by the # operator. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 218 | const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | Preprocessor &PP) { |
| 220 | assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!"); |
| 221 | if (StringifiedArgs.empty()) { |
| 222 | StringifiedArgs.resize(getNumArguments()); |
| 223 | memset(&StringifiedArgs[0], 0, |
| 224 | sizeof(StringifiedArgs[0])*getNumArguments()); |
| 225 | } |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 226 | if (StringifiedArgs[ArgNo].isNot(tok::string_literal)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 227 | StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP); |
| 228 | return StringifiedArgs[ArgNo]; |
| 229 | } |
| 230 | |
| 231 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 232 | // TokenLexer Implementation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | //===----------------------------------------------------------------------===// |
| 234 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 235 | /// Create a TokenLexer for the specified macro with the specified actual |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | /// arguments. Note that this ctor takes ownership of the ActualArgs pointer. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 237 | void TokenLexer::Init(Token &Tok, MacroArgs *Actuals) { |
| 238 | // 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] | 239 | // associated with it. |
| 240 | destroy(); |
| 241 | |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 242 | Macro = PP.getMacroInfo(Tok.getIdentifierInfo()); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 243 | ActualArgs = Actuals; |
| 244 | CurToken = 0; |
| 245 | InstantiateLoc = Tok.getLocation(); |
| 246 | AtStartOfLine = Tok.isAtStartOfLine(); |
| 247 | HasLeadingSpace = Tok.hasLeadingSpace(); |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 248 | Tokens = &*Macro->tokens_begin(); |
| 249 | OwnsTokens = false; |
| 250 | NumTokens = Macro->tokens_end()-Macro->tokens_begin(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | |
| 252 | // If this is a function-like macro, expand the arguments and change |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 253 | // Tokens to point to the expanded tokens. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | if (Macro->isFunctionLike() && Macro->getNumArgs()) |
| 255 | ExpandFunctionArguments(); |
| 256 | |
| 257 | // Mark the macro as currently disabled, so that it is not recursively |
| 258 | // expanded. The macro must be disabled only after argument pre-expansion of |
| 259 | // function-like macro arguments occurs. |
| 260 | Macro->DisableMacro(); |
| 261 | } |
| 262 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 263 | |
| 264 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 265 | /// Create a TokenLexer for the specified token stream. This does not |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | /// take ownership of the specified token vector. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 267 | void TokenLexer::Init(const Token *TokArray, unsigned NumToks) { |
| 268 | // 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] | 269 | // associated with it. |
| 270 | destroy(); |
| 271 | |
| 272 | Macro = 0; |
| 273 | ActualArgs = 0; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 274 | Tokens = TokArray; |
| 275 | OwnsTokens = false; |
| 276 | NumTokens = NumToks; |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 277 | CurToken = 0; |
| 278 | InstantiateLoc = SourceLocation(); |
| 279 | AtStartOfLine = false; |
| 280 | HasLeadingSpace = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | |
| 282 | // Set HasLeadingSpace/AtStartOfLine so that the first token will be |
| 283 | // returned unmodified. |
| 284 | if (NumToks != 0) { |
| 285 | AtStartOfLine = TokArray[0].isAtStartOfLine(); |
| 286 | HasLeadingSpace = TokArray[0].hasLeadingSpace(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 291 | void TokenLexer::destroy() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | // If this was a function-like macro that actually uses its arguments, delete |
| 293 | // the expanded tokens. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 294 | if (OwnsTokens) { |
| 295 | delete [] Tokens; |
| 296 | Tokens = 0; |
Chris Lattner | 9c68306 | 2007-07-22 01:16:55 +0000 | [diff] [blame] | 297 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 299 | // TokenLexer owns its formal arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | if (ActualArgs) ActualArgs->destroy(); |
| 301 | } |
| 302 | |
| 303 | /// 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] | 304 | /// return preexpanded tokens from Tokens. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 305 | void TokenLexer::ExpandFunctionArguments() { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 306 | llvm::SmallVector<Token, 128> ResultToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 308 | // Loop through 'Tokens', expanding them into ResultToks. Keep |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | // 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] | 310 | // we install the newly expanded sequence as the new 'Tokens' list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | bool MadeChange = false; |
| 312 | |
| 313 | // NextTokGetsSpace - When this is true, the next token appended to the |
| 314 | // output list will get a leading space, regardless of whether it had one to |
| 315 | // begin with or not. This is used for placemarker support. |
| 316 | bool NextTokGetsSpace = false; |
| 317 | |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 318 | for (unsigned i = 0, e = NumTokens; i != e; ++i) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | // If we found the stringify operator, get the argument stringified. The |
| 320 | // preprocessor already verified that the following token is a macro name |
| 321 | // when the #define was parsed. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 322 | const Token &CurTok = Tokens[i]; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 323 | if (CurTok.is(tok::hash) || CurTok.is(tok::hashat)) { |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 324 | int ArgNo = Macro->getArgumentNum(Tokens[i+1].getIdentifierInfo()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 325 | assert(ArgNo != -1 && "Token following # is not an argument?"); |
| 326 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 327 | Token Res; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 328 | if (CurTok.is(tok::hash)) // Stringify |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | Res = ActualArgs->getStringifiedArgument(ArgNo, PP); |
| 330 | else { |
| 331 | // 'charify': don't bother caching these. |
| 332 | Res = StringifyArgument(ActualArgs->getUnexpArgument(ArgNo), PP, true); |
| 333 | } |
| 334 | |
| 335 | // The stringified/charified string leading space flag gets set to match |
| 336 | // the #/#@ operator. |
| 337 | if (CurTok.hasLeadingSpace() || NextTokGetsSpace) |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 338 | Res.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | |
| 340 | ResultToks.push_back(Res); |
| 341 | MadeChange = true; |
| 342 | ++i; // Skip arg name. |
| 343 | NextTokGetsSpace = false; |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | // Otherwise, if this is not an argument token, just add the token to the |
| 348 | // output buffer. |
| 349 | IdentifierInfo *II = CurTok.getIdentifierInfo(); |
| 350 | int ArgNo = II ? Macro->getArgumentNum(II) : -1; |
| 351 | if (ArgNo == -1) { |
| 352 | // This isn't an argument, just add it. |
| 353 | ResultToks.push_back(CurTok); |
| 354 | |
| 355 | if (NextTokGetsSpace) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 356 | ResultToks.back().setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | NextTokGetsSpace = false; |
| 358 | } |
| 359 | continue; |
| 360 | } |
| 361 | |
| 362 | // An argument is expanded somehow, the result is different than the |
| 363 | // input. |
| 364 | MadeChange = true; |
| 365 | |
| 366 | // Otherwise, this is a use of the argument. Find out if there is a paste |
| 367 | // (##) operator before or after the argument. |
| 368 | bool PasteBefore = |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 369 | !ResultToks.empty() && ResultToks.back().is(tok::hashhash); |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 370 | bool PasteAfter = i+1 != e && Tokens[i+1].is(tok::hashhash); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 371 | |
| 372 | // If it is not the LHS/RHS of a ## operator, we must pre-expand the |
| 373 | // argument and substitute the expanded tokens into the result. This is |
| 374 | // C99 6.10.3.1p1. |
| 375 | if (!PasteBefore && !PasteAfter) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 376 | const Token *ResultArgToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | |
| 378 | // Only preexpand the argument if it could possibly need it. This |
| 379 | // avoids some work in common cases. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 380 | const Token *ArgTok = ActualArgs->getUnexpArgument(ArgNo); |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 381 | if (ActualArgs->ArgNeedsPreexpansion(ArgTok, PP)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | ResultArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP)[0]; |
| 383 | else |
| 384 | ResultArgToks = ArgTok; // Use non-preexpanded tokens. |
| 385 | |
| 386 | // If the arg token expanded into anything, append it. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 387 | if (ResultArgToks->isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | unsigned FirstResult = ResultToks.size(); |
| 389 | unsigned NumToks = MacroArgs::getArgLength(ResultArgToks); |
| 390 | ResultToks.append(ResultArgToks, ResultArgToks+NumToks); |
| 391 | |
| 392 | // If any tokens were substituted from the argument, the whitespace |
| 393 | // before the first token should match the whitespace of the arg |
| 394 | // identifier. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 395 | ResultToks[FirstResult].setFlagValue(Token::LeadingSpace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | CurTok.hasLeadingSpace() || |
| 397 | NextTokGetsSpace); |
| 398 | NextTokGetsSpace = false; |
| 399 | } else { |
| 400 | // If this is an empty argument, and if there was whitespace before the |
| 401 | // formal token, make sure the next token gets whitespace before it. |
| 402 | NextTokGetsSpace = CurTok.hasLeadingSpace(); |
| 403 | } |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | // Okay, we have a token that is either the LHS or RHS of a paste (##) |
| 408 | // argument. It gets substituted as its non-pre-expanded tokens. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 409 | const Token *ArgToks = ActualArgs->getUnexpArgument(ArgNo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | unsigned NumToks = MacroArgs::getArgLength(ArgToks); |
| 411 | if (NumToks) { // Not an empty argument? |
Chris Lattner | 71a3a8d | 2008-01-29 07:54:23 +0000 | [diff] [blame] | 412 | // If this is the GNU ", ## __VA_ARG__" extension, and we just learned |
| 413 | // that __VA_ARG__ expands to multiple tokens, avoid a pasting error when |
| 414 | // the expander trys to paste ',' with the first token of the __VA_ARG__ |
| 415 | // expansion. |
| 416 | if (PasteBefore && ResultToks.size() >= 2 && |
| 417 | ResultToks[ResultToks.size()-2].is(tok::comma) && |
| 418 | (unsigned)ArgNo == Macro->getNumArgs()-1 && |
| 419 | Macro->isVariadic()) { |
| 420 | // Remove the paste operator, report use of the extension. |
| 421 | PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma); |
| 422 | ResultToks.pop_back(); |
| 423 | } |
| 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | ResultToks.append(ArgToks, ArgToks+NumToks); |
| 426 | |
| 427 | // If the next token was supposed to get leading whitespace, ensure it has |
| 428 | // it now. |
| 429 | if (NextTokGetsSpace) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 430 | ResultToks[ResultToks.size()-NumToks].setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 431 | NextTokGetsSpace = false; |
| 432 | } |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | // If an empty argument is on the LHS or RHS of a paste, the standard (C99 |
| 437 | // 6.10.3.3p2,3) calls for a bunch of placemarker stuff to occur. We |
| 438 | // implement this by eating ## operators when a LHS or RHS expands to |
| 439 | // empty. |
| 440 | NextTokGetsSpace |= CurTok.hasLeadingSpace(); |
| 441 | if (PasteAfter) { |
| 442 | // Discard the argument token and skip (don't copy to the expansion |
| 443 | // buffer) the paste operator after it. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 444 | NextTokGetsSpace |= Tokens[i+1].hasLeadingSpace(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | ++i; |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | // If this is on the RHS of a paste operator, we've already copied the |
| 450 | // paste operator to the ResultToks list. Remove it. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 451 | assert(PasteBefore && ResultToks.back().is(tok::hashhash)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | NextTokGetsSpace |= ResultToks.back().hasLeadingSpace(); |
| 453 | ResultToks.pop_back(); |
| 454 | |
| 455 | // If this is the __VA_ARGS__ token, and if the argument wasn't provided, |
| 456 | // and if the macro had at least one real argument, and if the token before |
| 457 | // the ## was a comma, remove the comma. |
| 458 | if ((unsigned)ArgNo == Macro->getNumArgs()-1 && // is __VA_ARGS__ |
| 459 | ActualArgs->isVarargsElidedUse() && // Argument elided. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 460 | !ResultToks.empty() && ResultToks.back().is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 461 | // Never add a space, even if the comma, ##, or arg had a space. |
| 462 | NextTokGetsSpace = false; |
Chris Lattner | 71a3a8d | 2008-01-29 07:54:23 +0000 | [diff] [blame] | 463 | // Remove the paste operator, report use of the extension. |
| 464 | PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 465 | ResultToks.pop_back(); |
| 466 | } |
| 467 | continue; |
| 468 | } |
| 469 | |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 470 | // If anything changed, install this as the new Tokens list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 471 | if (MadeChange) { |
| 472 | // This is deleted in the dtor. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 473 | NumTokens = ResultToks.size(); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 474 | Token *Res = new Token[ResultToks.size()]; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 475 | if (NumTokens) |
| 476 | memcpy(Res, &ResultToks[0], NumTokens*sizeof(Token)); |
| 477 | Tokens = Res; |
| 478 | OwnsTokens = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | /// Lex - Lex and return a token from this macro stream. |
| 483 | /// |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 484 | void TokenLexer::Lex(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | // Lexing off the end of the macro, pop this macro off the expansion stack. |
| 486 | if (isAtEnd()) { |
| 487 | // If this is a macro (not a token stream), mark the macro enabled now |
| 488 | // that it is no longer being expanded. |
| 489 | if (Macro) Macro->EnableMacro(); |
| 490 | |
| 491 | // Pop this context off the preprocessors lexer stack and get the next |
| 492 | // token. This will delete "this" so remember the PP instance var. |
| 493 | Preprocessor &PPCache = PP; |
| 494 | if (PP.HandleEndOfMacro(Tok)) |
| 495 | return; |
| 496 | |
| 497 | // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is |
| 498 | // next. |
| 499 | return PPCache.Lex(Tok); |
| 500 | } |
| 501 | |
| 502 | // If this is the first token of the expanded result, we inherit spacing |
| 503 | // properties later. |
| 504 | bool isFirstToken = CurToken == 0; |
| 505 | |
| 506 | // Get the next token to return. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 507 | Tok = Tokens[CurToken++]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | |
| 509 | // 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] | 510 | if (!isAtEnd() && Tokens[CurToken].is(tok::hashhash)) |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 511 | if (PasteTokens(Tok)) { |
| 512 | // When handling the microsoft /##/ extension, the final token is |
| 513 | // returned by PasteTokens, not the pasted token. |
| 514 | return; |
| 515 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 516 | |
| 517 | // The token's current location indicate where the token was lexed from. We |
| 518 | // need this information to compute the spelling of the token, but any |
| 519 | // diagnostics for the expanded token should appear as if they came from |
| 520 | // InstantiationLoc. Pull this information together into a new SourceLocation |
| 521 | // that captures all of this. |
| 522 | if (InstantiateLoc.isValid()) { // Don't do this for token streams. |
| 523 | SourceManager &SrcMgr = PP.getSourceManager(); |
Chris Lattner | abca2bb | 2007-07-15 06:35:27 +0000 | [diff] [blame] | 524 | Tok.setLocation(SrcMgr.getInstantiationLoc(Tok.getLocation(), |
| 525 | InstantiateLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | // If this is the first token, set the lexical properties of the token to |
| 529 | // match the lexical properties of the macro identifier. |
| 530 | if (isFirstToken) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 531 | Tok.setFlagValue(Token::StartOfLine , AtStartOfLine); |
| 532 | Tok.setFlagValue(Token::LeadingSpace, HasLeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Handle recursive expansion! |
| 536 | if (Tok.getIdentifierInfo()) |
| 537 | return PP.HandleIdentifier(Tok); |
| 538 | |
| 539 | // Otherwise, return a normal token. |
| 540 | } |
| 541 | |
| 542 | /// PasteTokens - Tok is the LHS of a ## operator, and CurToken is the ## |
| 543 | /// operator. Read the ## and RHS, and paste the LHS/RHS together. If there |
| 544 | /// 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] | 545 | /// If this returns true, the caller should immediately return the token. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 546 | bool TokenLexer::PasteTokens(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | llvm::SmallVector<char, 128> Buffer; |
| 548 | do { |
| 549 | // Consume the ## operator. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 550 | SourceLocation PasteOpLoc = Tokens[CurToken].getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | ++CurToken; |
| 552 | assert(!isAtEnd() && "No token on the RHS of a paste operator!"); |
| 553 | |
| 554 | // Get the RHS token. |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 555 | const Token &RHS = Tokens[CurToken]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 556 | |
| 557 | bool isInvalid = false; |
| 558 | |
| 559 | // Allocate space for the result token. This is guaranteed to be enough for |
| 560 | // the two tokens and a null terminator. |
| 561 | Buffer.resize(Tok.getLength() + RHS.getLength() + 1); |
| 562 | |
| 563 | // Get the spelling of the LHS token in Buffer. |
| 564 | const char *BufPtr = &Buffer[0]; |
| 565 | unsigned LHSLen = PP.getSpelling(Tok, BufPtr); |
| 566 | if (BufPtr != &Buffer[0]) // Really, we want the chars in Buffer! |
| 567 | memcpy(&Buffer[0], BufPtr, LHSLen); |
| 568 | |
| 569 | BufPtr = &Buffer[LHSLen]; |
| 570 | unsigned RHSLen = PP.getSpelling(RHS, BufPtr); |
| 571 | if (BufPtr != &Buffer[LHSLen]) // Really, we want the chars in Buffer! |
| 572 | memcpy(&Buffer[LHSLen], BufPtr, RHSLen); |
| 573 | |
| 574 | // Add null terminator. |
| 575 | Buffer[LHSLen+RHSLen] = '\0'; |
| 576 | |
| 577 | // Trim excess space. |
| 578 | Buffer.resize(LHSLen+RHSLen+1); |
| 579 | |
| 580 | // Plop the pasted result (including the trailing newline and null) into a |
| 581 | // scratch buffer where we can lex it. |
| 582 | SourceLocation ResultTokLoc = PP.CreateString(&Buffer[0], Buffer.size()); |
| 583 | |
| 584 | // Lex the resultant pasted token into Result. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 585 | Token Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 586 | |
| 587 | // Avoid testing /*, as the lexer would think it is the start of a comment |
| 588 | // and emit an error that it is unterminated. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 589 | if (Tok.is(tok::slash) && RHS.is(tok::star)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 590 | isInvalid = true; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 591 | } else if (Tok.is(tok::identifier) && RHS.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | // Common paste case: identifier+identifier = identifier. Avoid creating |
| 593 | // a lexer and other overhead. |
| 594 | PP.IncrementPasteCounter(true); |
| 595 | Result.startToken(); |
| 596 | Result.setKind(tok::identifier); |
| 597 | Result.setLocation(ResultTokLoc); |
| 598 | Result.setLength(LHSLen+RHSLen); |
| 599 | } else { |
| 600 | PP.IncrementPasteCounter(false); |
| 601 | |
| 602 | // Make a lexer to lex this string from. |
| 603 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 604 | const char *ResultStrData = SourceMgr.getCharacterData(ResultTokLoc); |
| 605 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 606 | // 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] | 607 | Lexer *TL = new Lexer(ResultTokLoc, PP, ResultStrData, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 608 | ResultStrData+LHSLen+RHSLen /*don't include null*/); |
| 609 | |
| 610 | // Lex a token in raw mode. This way it won't look up identifiers |
| 611 | // automatically, lexing off the end will return an eof token, and |
| 612 | // warnings are disabled. This returns true if the result token is the |
| 613 | // entire buffer. |
| 614 | bool IsComplete = TL->LexRawToken(Result); |
| 615 | |
| 616 | // If we got an EOF token, we didn't form even ONE token. For example, we |
| 617 | // did "/ ## /" to get "//". |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 618 | IsComplete &= Result.isNot(tok::eof); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 619 | isInvalid = !IsComplete; |
| 620 | |
| 621 | // We're now done with the temporary lexer. |
| 622 | delete TL; |
| 623 | } |
| 624 | |
| 625 | // If pasting the two tokens didn't form a full new token, this is an error. |
| 626 | // This occurs with "x ## +" and other stuff. Return with Tok unmodified |
| 627 | // and with RHS as the next token to lex. |
| 628 | if (isInvalid) { |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 629 | // Test for the Microsoft extension of /##/ turning into // here on the |
| 630 | // error path. |
| 631 | if (PP.getLangOptions().Microsoft && Tok.is(tok::slash) && |
| 632 | RHS.is(tok::slash)) { |
| 633 | HandleMicrosoftCommentPaste(Tok); |
| 634 | return true; |
| 635 | } else { |
| 636 | // TODO: If not in assembler language mode. |
| 637 | PP.Diag(PasteOpLoc, diag::err_pp_bad_paste, |
| 638 | std::string(Buffer.begin(), Buffer.end()-1)); |
| 639 | return false; |
| 640 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 643 | // Turn ## into 'unknown' to avoid # ## # from looking like a paste |
| 644 | // operator. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 645 | if (Result.is(tok::hashhash)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 646 | Result.setKind(tok::unknown); |
Chris Lattner | 71a3a8d | 2008-01-29 07:54:23 +0000 | [diff] [blame] | 647 | // FIXME: Turn __VA_ARGS__ into "not a token"? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 648 | |
| 649 | // Transfer properties of the LHS over the the Result. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 650 | Result.setFlagValue(Token::StartOfLine , Tok.isAtStartOfLine()); |
| 651 | Result.setFlagValue(Token::LeadingSpace, Tok.hasLeadingSpace()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 652 | |
| 653 | // Finally, replace LHS with the result, consume the RHS, and iterate. |
| 654 | ++CurToken; |
| 655 | Tok = Result; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 656 | } while (!isAtEnd() && Tokens[CurToken].is(tok::hashhash)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | |
| 658 | // Now that we got the result token, it will be subject to expansion. Since |
| 659 | // token pasting re-lexes the result token in raw mode, identifier information |
| 660 | // 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] | 661 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 662 | // Look up the identifier info for the token. We disabled identifier lookup |
| 663 | // by saying we're skipping contents, so we need to do this manually. |
| 664 | Tok.setIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); |
| 665 | } |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 666 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /// isNextTokenLParen - If the next token lexed will pop this macro off the |
| 670 | /// expansion stack, return 2. If the next unexpanded token is a '(', return |
| 671 | /// 1, otherwise return 0. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 672 | unsigned TokenLexer::isNextTokenLParen() const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 673 | // Out of tokens? |
| 674 | if (isAtEnd()) |
| 675 | return 2; |
Chris Lattner | 8d89643 | 2008-03-09 02:07:49 +0000 | [diff] [blame] | 676 | return Tokens[CurToken].is(tok::l_paren); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 677 | } |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 678 | |
| 679 | |
| 680 | /// HandleMicrosoftCommentPaste - In microsoft compatibility mode, /##/ pastes |
| 681 | /// together to form a comment that comments out everything in the current |
| 682 | /// macro, other active macros, and anything left on the current physical |
| 683 | /// source line of the instantiated buffer. Handle this by returning the |
| 684 | /// first token on the next line. |
Chris Lattner | 1543e9c | 2008-03-09 02:18:51 +0000 | [diff] [blame^] | 685 | void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) { |
Chris Lattner | 3f1cc83 | 2008-02-07 06:03:59 +0000 | [diff] [blame] | 686 | // We 'comment out' the rest of this macro by just ignoring the rest of the |
| 687 | // tokens that have not been lexed yet, if any. |
| 688 | |
| 689 | // Since this must be a macro, mark the macro enabled now that it is no longer |
| 690 | // being expanded. |
| 691 | assert(Macro && "Token streams can't paste comments"); |
| 692 | Macro->EnableMacro(); |
| 693 | |
| 694 | PP.HandleMicrosoftCommentPaste(Tok); |
| 695 | } |