Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 1 | //===--- TokenLexer.cpp - Lex from a token stream -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the TokenLexer interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "MacroArgs.h" |
| 15 | #include "clang/Lex/MacroInfo.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 17 | #include "clang/Lex/LexDiagnostic.h" |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | /// MacroArgs ctor function - This destroys the vector passed in. |
| 21 | MacroArgs *MacroArgs::create(const MacroInfo *MI, |
| 22 | const Token *UnexpArgTokens, |
Chris Lattner | 561395b | 2009-12-14 22:12:52 +0000 | [diff] [blame] | 23 | unsigned NumToks, bool VarargsElided, |
| 24 | Preprocessor &PP) { |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 25 | assert(MI->isFunctionLike() && |
| 26 | "Can't have args for an object-like macro!"); |
Chris Lattner | 4608459 | 2009-12-15 20:48:12 +0000 | [diff] [blame] | 27 | MacroArgs *Result = 0; |
| 28 | |
| 29 | // See if we have an entry with a big enough argument list to reuse on the |
| 30 | // free list. If so, reuse it. |
| 31 | for (MacroArgs **Entry = &PP.MacroArgCache; *Entry; |
| 32 | Entry = &(*Entry)->ArgCache) |
| 33 | if ((*Entry)->NumUnexpArgTokens >= NumToks) { |
| 34 | Result = *Entry; |
| 35 | // Unlink this node from the preprocessors singly linked list. |
| 36 | *Entry = Result->ArgCache; |
| 37 | break; |
| 38 | } |
| 39 | |
| 40 | if (Result == 0) { |
| 41 | // Allocate memory for a MacroArgs object with the lexer tokens at the end. |
| 42 | Result = (MacroArgs*)malloc(sizeof(MacroArgs) + NumToks*sizeof(Token)); |
| 43 | // Construct the MacroArgs object. |
| 44 | new (Result) MacroArgs(NumToks, VarargsElided); |
| 45 | } else { |
| 46 | Result->NumUnexpArgTokens = NumToks; |
| 47 | Result->VarargsElided = VarargsElided; |
| 48 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 50 | // Copy the actual unexpanded tokens to immediately after the result ptr. |
| 51 | if (NumToks) |
| 52 | memcpy(const_cast<Token*>(Result->getUnexpArgument(0)), |
| 53 | UnexpArgTokens, NumToks*sizeof(Token)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 55 | return Result; |
| 56 | } |
| 57 | |
| 58 | /// destroy - Destroy and deallocate the memory for this object. |
| 59 | /// |
Chris Lattner | 561395b | 2009-12-14 22:12:52 +0000 | [diff] [blame] | 60 | void MacroArgs::destroy(Preprocessor &PP) { |
Chris Lattner | 4608459 | 2009-12-15 20:48:12 +0000 | [diff] [blame] | 61 | StringifiedArgs.clear(); |
| 62 | |
| 63 | // Don't clear PreExpArgTokens, just clear the entries. Clearing the entries |
| 64 | // would deallocate the element vectors. |
| 65 | for (unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i) |
| 66 | PreExpArgTokens[i].clear(); |
| 67 | |
| 68 | // Add this to the preprocessor's free list. |
| 69 | ArgCache = PP.MacroArgCache; |
| 70 | PP.MacroArgCache = this; |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 73 | /// deallocate - This should only be called by the Preprocessor when managing |
| 74 | /// its freelist. |
| 75 | MacroArgs *MacroArgs::deallocate() { |
| 76 | MacroArgs *Next = ArgCache; |
| 77 | |
| 78 | // Run the dtor to deallocate the vectors. |
| 79 | this->~MacroArgs(); |
| 80 | // Release the memory for the object. |
| 81 | free(this); |
| 82 | |
| 83 | return Next; |
| 84 | } |
| 85 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 86 | |
| 87 | /// getArgLength - Given a pointer to an expanded or unexpanded argument, |
| 88 | /// return the number of tokens, not counting the EOF, that make up the |
| 89 | /// argument. |
| 90 | unsigned MacroArgs::getArgLength(const Token *ArgPtr) { |
| 91 | unsigned NumArgTokens = 0; |
| 92 | for (; ArgPtr->isNot(tok::eof); ++ArgPtr) |
| 93 | ++NumArgTokens; |
| 94 | return NumArgTokens; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /// getUnexpArgument - Return the unexpanded tokens for the specified formal. |
| 99 | /// |
| 100 | const Token *MacroArgs::getUnexpArgument(unsigned Arg) const { |
| 101 | // The unexpanded argument tokens start immediately after the MacroArgs object |
| 102 | // in memory. |
| 103 | const Token *Start = (const Token *)(this+1); |
| 104 | const Token *Result = Start; |
| 105 | // Scan to find Arg. |
| 106 | for (; Arg; ++Result) { |
| 107 | assert(Result < Start+NumUnexpArgTokens && "Invalid arg #"); |
| 108 | if (Result->is(tok::eof)) |
| 109 | --Arg; |
| 110 | } |
Chris Lattner | 9fc9e77 | 2009-05-13 00:55:26 +0000 | [diff] [blame] | 111 | assert(Result < Start+NumUnexpArgTokens && "Invalid arg #"); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 112 | return Result; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected |
| 117 | /// by pre-expansion, return false. Otherwise, conservatively return true. |
| 118 | bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok, |
| 119 | Preprocessor &PP) const { |
| 120 | // If there are no identifiers in the argument list, or if the identifiers are |
| 121 | // known to not be macros, pre-expansion won't modify it. |
| 122 | for (; ArgTok->isNot(tok::eof); ++ArgTok) |
| 123 | if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) { |
| 124 | if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled()) |
| 125 | // Return true even though the macro could be a function-like macro |
| 126 | // without a following '(' token. |
| 127 | return true; |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /// getPreExpArgument - Return the pre-expanded form of the specified |
| 133 | /// argument. |
| 134 | const std::vector<Token> & |
| 135 | MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) { |
| 136 | assert(Arg < NumUnexpArgTokens && "Invalid argument number!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 138 | // If we have already computed this, return it. |
| 139 | if (PreExpArgTokens.empty()) |
| 140 | PreExpArgTokens.resize(NumUnexpArgTokens); |
| 141 | |
| 142 | std::vector<Token> &Result = PreExpArgTokens[Arg]; |
| 143 | if (!Result.empty()) return Result; |
| 144 | |
| 145 | const Token *AT = getUnexpArgument(Arg); |
| 146 | unsigned NumToks = getArgLength(AT)+1; // Include the EOF. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 148 | // Otherwise, we have to pre-expand this argument, populating Result. To do |
| 149 | // this, we set up a fake TokenLexer to lex from the unexpanded argument |
| 150 | // list. With this installed, we lex expanded tokens until we hit the EOF |
| 151 | // token at the end of the unexp list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | PP.EnterTokenStream(AT, NumToks, false /*disable expand*/, |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 153 | false /*owns tokens*/); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 154 | |
| 155 | // Lex all of the macro-expanded tokens into Result. |
| 156 | do { |
| 157 | Result.push_back(Token()); |
Chris Lattner | 7c35122 | 2009-01-26 04:33:10 +0000 | [diff] [blame] | 158 | Token &Tok = Result.back(); |
| 159 | PP.Lex(Tok); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 160 | } while (Result.back().isNot(tok::eof)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 162 | // Pop the token stream off the top of the stack. We know that the internal |
| 163 | // pointer inside of it is to the "end" of the token stream, but the stack |
| 164 | // will not otherwise be popped until the next token is lexed. The problem is |
| 165 | // that the token may be lexed sometime after the vector of tokens itself is |
| 166 | // destroyed, which would be badness. |
| 167 | PP.RemoveTopOfLexerStack(); |
| 168 | return Result; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of |
| 173 | /// tokens into the literal string token that should be produced by the C # |
| 174 | /// preprocessor operator. If Charify is true, then it should be turned into |
| 175 | /// a character literal for the Microsoft charize (#@) extension. |
| 176 | /// |
| 177 | Token MacroArgs::StringifyArgument(const Token *ArgToks, |
| 178 | Preprocessor &PP, bool Charify) { |
| 179 | Token Tok; |
| 180 | Tok.startToken(); |
Chris Lattner | 6633522 | 2009-12-23 19:15:27 +0000 | [diff] [blame] | 181 | Tok.setKind(Charify ? tok::char_constant : tok::string_literal); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 182 | |
| 183 | const Token *ArgTokStart = ArgToks; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 185 | // Stringify all the tokens. |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 186 | llvm::SmallString<128> Result; |
| 187 | Result += "\""; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 189 | bool isFirst = true; |
| 190 | for (; ArgToks->isNot(tok::eof); ++ArgToks) { |
| 191 | const Token &Tok = *ArgToks; |
| 192 | if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine())) |
| 193 | Result += ' '; |
| 194 | isFirst = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 196 | // If this is a string or character constant, escape the token as specified |
| 197 | // by 6.10.3.2p2. |
| 198 | if (Tok.is(tok::string_literal) || // "foo" |
| 199 | Tok.is(tok::wide_string_literal) || // L"foo" |
| 200 | Tok.is(tok::char_constant)) { // 'x' and L'x'. |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 201 | std::string Str = Lexer::Stringify(PP.getSpelling(Tok)); |
| 202 | Result.append(Str.begin(), Str.end()); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 203 | } else { |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 204 | // Otherwise, just append the token. Do some gymnastics to get the token |
| 205 | // in place and avoid copies where possible. |
| 206 | unsigned CurStrLen = Result.size(); |
| 207 | Result.resize(CurStrLen+Tok.getLength()); |
| 208 | const char *BufPtr = &Result[CurStrLen]; |
| 209 | unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 211 | // If getSpelling returned a pointer to an already uniqued version of the |
| 212 | // string instead of filling in BufPtr, memcpy it onto our string. |
| 213 | if (BufPtr != &Result[CurStrLen]) |
| 214 | memcpy(&Result[CurStrLen], BufPtr, ActualTokLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 216 | // If the token was dirty, the spelling may be shorter than the token. |
| 217 | if (ActualTokLen != Tok.getLength()) |
| 218 | Result.resize(CurStrLen+ActualTokLen); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 222 | // If the last character of the string is a \, and if it isn't escaped, this |
| 223 | // is an invalid string literal, diagnose it as specified in C99. |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 224 | if (Result.back() == '\\') { |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 225 | // Count the number of consequtive \ characters. If even, then they are |
| 226 | // just escaped backslashes, otherwise it's an error. |
| 227 | unsigned FirstNonSlash = Result.size()-2; |
| 228 | // Guaranteed to find the starting " if nothing else. |
| 229 | while (Result[FirstNonSlash] == '\\') |
| 230 | --FirstNonSlash; |
| 231 | if ((Result.size()-1-FirstNonSlash) & 1) { |
| 232 | // Diagnose errors for things like: #define F(X) #X / F(\) |
| 233 | PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal); |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 234 | Result.pop_back(); // remove one of the \'s. |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | Result += '"'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 239 | // If this is the charify operation and the result is not a legal character |
| 240 | // constant, diagnose it. |
| 241 | if (Charify) { |
| 242 | // First step, turn double quotes into single quotes: |
| 243 | Result[0] = '\''; |
| 244 | Result[Result.size()-1] = '\''; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 246 | // Check for bogus character. |
| 247 | bool isBad = false; |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 248 | if (Result.size() == 3) |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 249 | isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above. |
Chris Lattner | c19e8a2 | 2009-01-05 23:04:18 +0000 | [diff] [blame] | 250 | else |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 251 | isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 253 | if (isBad) { |
| 254 | PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify); |
| 255 | Result = "' '"; // Use something arbitrary, but legal. |
| 256 | } |
| 257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 259 | PP.CreateString(&Result[0], Result.size(), Tok); |
Chris Lattner | e5c8ffe | 2008-03-09 02:55:12 +0000 | [diff] [blame] | 260 | return Tok; |
| 261 | } |
| 262 | |
| 263 | /// getStringifiedArgument - Compute, cache, and return the specified argument |
| 264 | /// that has been 'stringified' as required by the # operator. |
| 265 | const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo, |
| 266 | Preprocessor &PP) { |
| 267 | assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!"); |
| 268 | if (StringifiedArgs.empty()) { |
| 269 | StringifiedArgs.resize(getNumArguments()); |
| 270 | memset(&StringifiedArgs[0], 0, |
| 271 | sizeof(StringifiedArgs[0])*getNumArguments()); |
| 272 | } |
| 273 | if (StringifiedArgs[ArgNo].isNot(tok::string_literal)) |
| 274 | StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP); |
| 275 | return StringifiedArgs[ArgNo]; |
| 276 | } |