Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- MacroExpander.cpp - Lex from a macro expansion -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MacroExpander interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/MacroExpander.h" |
| 15 | #include "clang/Lex/MacroInfo.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace clang; |
| 21 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 23 | // MacroArgs Implementation |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 26 | MacroArgs::MacroArgs(const MacroInfo *MI) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 27 | assert(MI->isFunctionLike() && |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 28 | "Can't have args for an object-like macro!"); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 29 | // Reserve space for arguments to avoid reallocation. |
| 30 | unsigned NumArgs = MI->getNumArgs(); |
| 31 | if (MI->isC99Varargs() || MI->isGNUVarargs()) |
| 32 | NumArgs += 3; // Varargs can have more than this, just some guess. |
| 33 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 34 | UnexpArgTokens.reserve(NumArgs); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 37 | /// addArgument - Add an argument for this invocation. This method destroys |
| 38 | /// the vector passed in to avoid extraneous memory copies. This adds the EOF |
| 39 | /// token to the end of the argument list as a marker. 'Loc' specifies a |
| 40 | /// location at the end of the argument, e.g. the ',' token or the ')'. |
| 41 | void MacroArgs::addArgument(std::vector<LexerToken> &ArgToks, |
| 42 | SourceLocation Loc) { |
| 43 | UnexpArgTokens.push_back(std::vector<LexerToken>()); |
| 44 | UnexpArgTokens.back().swap(ArgToks); |
| 45 | |
| 46 | // Add a marker EOF token to the end of the argument list, useful for handling |
| 47 | // empty arguments and macro pre-expansion. |
| 48 | LexerToken EOFTok; |
| 49 | EOFTok.StartToken(); |
| 50 | EOFTok.SetKind(tok::eof); |
| 51 | EOFTok.SetLocation(Loc); |
Chris Lattner | 203b456 | 2006-07-15 21:07:40 +0000 | [diff] [blame] | 52 | EOFTok.SetLength(0); |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 53 | UnexpArgTokens.back().push_back(EOFTok); |
| 54 | } |
| 55 | |
Chris Lattner | 203b456 | 2006-07-15 21:07:40 +0000 | [diff] [blame] | 56 | /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected |
| 57 | /// by pre-expansion, return false. Otherwise, conservatively return true. |
| 58 | bool MacroArgs::ArgNeedsPreexpansion(unsigned ArgNo) const { |
| 59 | const std::vector<LexerToken> &ArgTokens = getUnexpArgument(ArgNo); |
| 60 | |
| 61 | // If there are no identifiers in the argument list, or if the identifiers are |
| 62 | // known to not be macros, pre-expansion won't modify it. |
| 63 | for (unsigned i = 0, e = ArgTokens.size()-1; i != e; ++i) |
| 64 | if (IdentifierInfo *II = ArgTokens[i].getIdentifierInfo()) { |
| 65 | if (II->getMacroInfo() && II->getMacroInfo()->isEnabled()) |
| 66 | // Return true even though the macro could be a function-like macro |
| 67 | // without a following '(' token. |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 73 | /// getPreExpArgument - Return the pre-expanded form of the specified |
| 74 | /// argument. |
| 75 | const std::vector<LexerToken> & |
| 76 | MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) { |
| 77 | assert(Arg < UnexpArgTokens.size() && "Invalid argument number!"); |
| 78 | |
| 79 | // If we have already computed this, return it. |
| 80 | if (PreExpArgTokens.empty()) |
| 81 | PreExpArgTokens.resize(UnexpArgTokens.size()); |
| 82 | |
| 83 | std::vector<LexerToken> &Result = PreExpArgTokens[Arg]; |
| 84 | if (!Result.empty()) return Result; |
| 85 | |
| 86 | // Otherwise, we have to pre-expand this argument, populating Result. To do |
| 87 | // this, we set up a fake MacroExpander to lex from the unexpanded argument |
| 88 | // list. With this installed, we lex expanded tokens until we hit the EOF |
| 89 | // token at the end of the unexp list. |
| 90 | PP.EnterTokenStream(UnexpArgTokens[Arg]); |
| 91 | |
| 92 | // Lex all of the macro-expanded tokens into Result. |
| 93 | do { |
| 94 | Result.push_back(LexerToken()); |
| 95 | PP.Lex(Result.back()); |
| 96 | } while (Result.back().getKind() != tok::eof); |
| 97 | |
| 98 | // Pop the token stream off the top of the stack. We know that the internal |
| 99 | // pointer inside of it is to the "end" of the token stream, but the stack |
| 100 | // will not otherwise be popped until the next token is lexed. The problem is |
| 101 | // that the token may be lexed sometime after the vector of tokens itself is |
| 102 | // destroyed, which would be badness. |
| 103 | PP.RemoveTopOfLexerStack(); |
| 104 | return Result; |
| 105 | } |
| 106 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 108 | /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of |
| 109 | /// tokens into the literal string token that should be produced by the C # |
| 110 | /// preprocessor operator. |
| 111 | /// |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 112 | static LexerToken StringifyArgument(const std::vector<LexerToken> &Toks, |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 113 | Preprocessor &PP, bool Charify = false) { |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 114 | LexerToken Tok; |
| 115 | Tok.StartToken(); |
| 116 | Tok.SetKind(tok::string_literal); |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 117 | |
| 118 | // Stringify all the tokens. |
| 119 | std::string Result = "\""; |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 120 | // FIXME: Optimize this loop to not use std::strings. |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 121 | for (unsigned i = 0, e = Toks.size()-1 /*no eof*/; i != e; ++i) { |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 122 | const LexerToken &Tok = Toks[i]; |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 123 | if (i != 0 && Tok.hasLeadingSpace()) |
| 124 | Result += ' '; |
| 125 | |
| 126 | // If this is a string or character constant, escape the token as specified |
| 127 | // by 6.10.3.2p2. |
| 128 | if (Tok.getKind() == tok::string_literal || // "foo" and L"foo". |
| 129 | Tok.getKind() == tok::char_constant) { // 'x' and L'x'. |
| 130 | Result += Lexer::Stringify(PP.getSpelling(Tok)); |
| 131 | } else { |
| 132 | // Otherwise, just append the token. |
| 133 | Result += PP.getSpelling(Tok); |
| 134 | } |
| 135 | } |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 137 | // If the last character of the string is a \, and if it isn't escaped, this |
| 138 | // is an invalid string literal, diagnose it as specified in C99. |
| 139 | if (Result[Result.size()-1] == '\\') { |
| 140 | // Count the number of consequtive \ characters. If even, then they are |
| 141 | // just escaped backslashes, otherwise it's an error. |
| 142 | unsigned FirstNonSlash = Result.size()-2; |
| 143 | // Guaranteed to find the starting " if nothing else. |
| 144 | while (Result[FirstNonSlash] == '\\') |
| 145 | --FirstNonSlash; |
| 146 | if ((Result.size()-1-FirstNonSlash) & 1) { |
Chris Lattner | f278150 | 2006-07-15 05:27:44 +0000 | [diff] [blame] | 147 | // Diagnose errors for things like: #define F(X) #X / F(\) |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 148 | PP.Diag(Toks.back(), diag::pp_invalid_string_literal); |
| 149 | Result.erase(Result.end()-1); // remove one of the \'s. |
| 150 | } |
| 151 | } |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 152 | Result += '"'; |
| 153 | |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 154 | // If this is the charify operation and the result is not a legal character |
| 155 | // constant, diagnose it. |
| 156 | if (Charify) { |
| 157 | // First step, turn double quotes into single quotes: |
| 158 | Result[0] = '\''; |
| 159 | Result[Result.size()-1] = '\''; |
| 160 | |
| 161 | // Check for bogus character. |
| 162 | bool isBad = false; |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 163 | if (Result.size() == 3) { |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 164 | isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above. |
| 165 | } else { |
| 166 | isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x' |
| 167 | } |
| 168 | |
| 169 | if (isBad) { |
Chris Lattner | 7c58149 | 2006-07-15 07:56:31 +0000 | [diff] [blame] | 170 | assert(!Toks.empty() && "No eof token at least?"); |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 171 | PP.Diag(Toks[0], diag::err_invalid_character_to_charify); |
Chris Lattner | 7c58149 | 2006-07-15 07:56:31 +0000 | [diff] [blame] | 172 | Result = "' '"; // Use something arbitrary, but legal. |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 176 | Tok.SetLength(Result.size()); |
| 177 | Tok.SetLocation(PP.CreateString(&Result[0], Result.size())); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 178 | return Tok; |
| 179 | } |
| 180 | |
| 181 | /// getStringifiedArgument - Compute, cache, and return the specified argument |
| 182 | /// that has been 'stringified' as required by the # operator. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 183 | const LexerToken &MacroArgs::getStringifiedArgument(unsigned ArgNo, |
| 184 | Preprocessor &PP) { |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 185 | assert(ArgNo < UnexpArgTokens.size() && "Invalid argument number!"); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 186 | if (StringifiedArgs.empty()) { |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 187 | StringifiedArgs.resize(getNumArguments()); |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 188 | memset(&StringifiedArgs[0], 0, |
| 189 | sizeof(StringifiedArgs[0])*getNumArguments()); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 190 | } |
| 191 | if (StringifiedArgs[ArgNo].getKind() != tok::string_literal) |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 192 | StringifiedArgs[ArgNo] = StringifyArgument(UnexpArgTokens[ArgNo], PP); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 193 | return StringifiedArgs[ArgNo]; |
| 194 | } |
| 195 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
| 197 | // MacroExpander Implementation |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 200 | /// Create a macro expander for the specified macro with the specified actual |
| 201 | /// arguments. Note that this ctor takes ownership of the ActualArgs pointer. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 202 | MacroExpander::MacroExpander(LexerToken &Tok, MacroArgs *Actuals, |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 203 | Preprocessor &pp) |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 204 | : Macro(Tok.getIdentifierInfo()->getMacroInfo()), |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 205 | ActualArgs(Actuals), PP(pp), CurToken(0), |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 206 | InstantiateLoc(Tok.getLocation()), |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 207 | AtStartOfLine(Tok.isAtStartOfLine()), |
| 208 | HasLeadingSpace(Tok.hasLeadingSpace()) { |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 209 | MacroTokens = &Macro->getReplacementTokens(); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 210 | |
| 211 | // If this is a function-like macro, expand the arguments and change |
| 212 | // MacroTokens to point to the expanded tokens. |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 213 | if (Macro->isFunctionLike() && Macro->getNumArgs()) |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 214 | ExpandFunctionArguments(); |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 215 | |
| 216 | // Mark the macro as currently disabled, so that it is not recursively |
| 217 | // expanded. The macro must be disabled only after argument pre-expansion of |
| 218 | // function-like macro arguments occurs. |
| 219 | Macro->DisableMacro(); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 222 | /// Create a macro expander for the specified token stream. This does not |
| 223 | /// take ownership of the specified token vector. |
| 224 | MacroExpander::MacroExpander(const std::vector<LexerToken> &TokStream, |
| 225 | Preprocessor &pp) |
| 226 | : Macro(0), ActualArgs(0), PP(pp), MacroTokens(&TokStream), CurToken(0), |
| 227 | InstantiateLoc(SourceLocation()), AtStartOfLine(false), |
| 228 | HasLeadingSpace(false) { |
| 229 | |
| 230 | // Set HasLeadingSpace/AtStartOfLine so that the first token will be |
| 231 | // returned unmodified. |
| 232 | if (!TokStream.empty()) { |
| 233 | AtStartOfLine = TokStream[0].isAtStartOfLine(); |
| 234 | HasLeadingSpace = TokStream[0].hasLeadingSpace(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 239 | MacroExpander::~MacroExpander() { |
| 240 | // If this was a function-like macro that actually uses its arguments, delete |
| 241 | // the expanded tokens. |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 242 | if (Macro && MacroTokens != &Macro->getReplacementTokens()) |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 243 | delete MacroTokens; |
| 244 | |
| 245 | // MacroExpander owns its formal arguments. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 246 | delete ActualArgs; |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /// Expand the arguments of a function-like macro so that we can quickly |
| 250 | /// return preexpanded tokens from MacroTokens. |
| 251 | void MacroExpander::ExpandFunctionArguments() { |
| 252 | std::vector<LexerToken> ResultToks; |
| 253 | |
| 254 | // Loop through the MacroTokens tokens, expanding them into ResultToks. Keep |
| 255 | // track of whether we change anything. If not, no need to keep them. If so, |
| 256 | // we install the newly expanded sequence as MacroTokens. |
| 257 | bool MadeChange = false; |
| 258 | for (unsigned i = 0, e = MacroTokens->size(); i != e; ++i) { |
| 259 | // If we found the stringify operator, get the argument stringified. The |
| 260 | // preprocessor already verified that the following token is a macro name |
| 261 | // when the #define was parsed. |
| 262 | const LexerToken &CurTok = (*MacroTokens)[i]; |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 263 | if (CurTok.getKind() == tok::hash || CurTok.getKind() == tok::hashat) { |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 264 | int ArgNo =Macro->getArgumentNum((*MacroTokens)[i+1].getIdentifierInfo()); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 265 | assert(ArgNo != -1 && "Token following # is not an argument?"); |
| 266 | |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 267 | if (CurTok.getKind() == tok::hash) // Stringify |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 268 | ResultToks.push_back(ActualArgs->getStringifiedArgument(ArgNo, PP)); |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 269 | else { |
| 270 | // 'charify': don't bother caching these. |
| 271 | ResultToks.push_back(StringifyArgument( |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 272 | ActualArgs->getUnexpArgument(ArgNo), PP, true)); |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 6016169 | 2006-07-15 06:48:02 +0000 | [diff] [blame] | 275 | // The stringified/charified string leading space flag gets set to match |
| 276 | // the #/#@ operator. |
| 277 | if (CurTok.hasLeadingSpace()) |
| 278 | ResultToks.back().SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 279 | |
| 280 | MadeChange = true; |
| 281 | ++i; // Skip arg name. |
| 282 | } else { |
Chris Lattner | 203b456 | 2006-07-15 21:07:40 +0000 | [diff] [blame] | 283 | // Otherwise, if this is not an argument token, just add the token to the |
| 284 | // output buffer. |
| 285 | IdentifierInfo *II = CurTok.getIdentifierInfo(); |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 286 | int ArgNo = II ? Macro->getArgumentNum(II) : -1; |
Chris Lattner | 203b456 | 2006-07-15 21:07:40 +0000 | [diff] [blame] | 287 | if (ArgNo == -1) { |
| 288 | ResultToks.push_back(CurTok); |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | // An argument is expanded somehow, the result is different than the |
| 293 | // input. |
| 294 | MadeChange = true; |
| 295 | |
| 296 | // Otherwise, this is a use of the argument. Find out if there is a paste |
| 297 | // (##) operator before or after the argument. |
| 298 | bool PasteBefore = |
| 299 | !ResultToks.empty() && ResultToks.back().getKind() == tok::hashhash; |
| 300 | bool PasteAfter = |
| 301 | i+1 != e && (*MacroTokens)[i+1].getKind() == tok::hashhash; |
| 302 | |
| 303 | // If it is not the LHS/RHS of a ## operator, we must pre-expand the |
| 304 | // argument and substitute the expanded tokens into the result. This is |
| 305 | // C99 6.10.3.1p1. |
| 306 | if (!PasteBefore && !PasteAfter) { |
| 307 | const std::vector<LexerToken> *ArgToks; |
| 308 | // Only preexpand the argument if it could possibly need it. This |
| 309 | // avoids some work in common cases. |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 310 | if (ActualArgs->ArgNeedsPreexpansion(ArgNo)) |
| 311 | ArgToks = &ActualArgs->getPreExpArgument(ArgNo, PP); |
| 312 | else |
Chris Lattner | 203b456 | 2006-07-15 21:07:40 +0000 | [diff] [blame] | 313 | ArgToks = &ActualArgs->getUnexpArgument(ArgNo); |
Chris Lattner | 203b456 | 2006-07-15 21:07:40 +0000 | [diff] [blame] | 314 | |
| 315 | unsigned FirstTok = ResultToks.size(); |
| 316 | ResultToks.insert(ResultToks.end(), ArgToks->begin(), ArgToks->end()-1); |
| 317 | |
| 318 | // If any tokens were substituted from the argument, the whitespace |
| 319 | // before the first token should match the whitespace of the arg |
| 320 | // identifier. |
| 321 | if (FirstTok != ResultToks.size()) |
| 322 | ResultToks[FirstTok].SetFlagValue(LexerToken::LeadingSpace, |
| 323 | CurTok.hasLeadingSpace()); |
| 324 | continue; |
| 325 | } |
| 326 | |
Chris Lattner | 7e2e669 | 2006-07-19 03:51:26 +0000 | [diff] [blame] | 327 | // Okay, we have a token that is either the LHS or RHS of a paste (##) |
| 328 | // argument. It gets substituted as its non-pre-expanded tokens. |
| 329 | const std::vector<LexerToken> &ArgToks = |
| 330 | ActualArgs->getUnexpArgument(ArgNo); |
| 331 | assert(ArgToks.back().getKind() == tok::eof && "Bad argument!"); |
| 332 | |
| 333 | if (ArgToks.size() != 1) { // Not just an EOF token? |
| 334 | ResultToks.insert(ResultToks.end(), ArgToks.begin(), ArgToks.end()-1); |
| 335 | continue; |
| 336 | } |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 7e2e669 | 2006-07-19 03:51:26 +0000 | [diff] [blame] | 338 | // FIXME: Handle comma swallowing GNU extension. |
| 339 | // FIXME: Handle 'placemarker' stuff. |
| 340 | assert(0 && "FIXME: handle empty arguments!"); |
| 341 | //ResultToks.push_back(CurTok); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | // If anything changed, install this as the new MacroTokens list. |
| 346 | if (MadeChange) { |
| 347 | // This is deleted in the dtor. |
| 348 | std::vector<LexerToken> *Res = new std::vector<LexerToken>(); |
| 349 | Res->swap(ResultToks); |
| 350 | MacroTokens = Res; |
| 351 | } |
| 352 | } |
Chris Lattner | 67b07cb | 2006-06-26 02:03:42 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 354 | /// Lex - Lex and return a token from this macro stream. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 355 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 356 | void MacroExpander::Lex(LexerToken &Tok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 357 | // 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] | 358 | if (isAtEnd()) { |
| 359 | // If this is a macro (not a token stream), mark the macro enabled now |
| 360 | // that it is no longer being expanded. |
| 361 | if (Macro) Macro->EnableMacro(); |
| 362 | |
| 363 | // Pop this context off the preprocessors lexer stack and get the next |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 364 | // token. This will delete "this" so remember the PP instance var. |
| 365 | Preprocessor &PPCache = PP; |
| 366 | if (PP.HandleEndOfMacro(Tok)) |
| 367 | return; |
| 368 | |
| 369 | // HandleEndOfMacro may not return a token. If it doesn't, lex whatever is |
| 370 | // next. |
| 371 | return PPCache.Lex(Tok); |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 372 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 373 | |
| 374 | // Get the next token to return. |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 375 | Tok = (*MacroTokens)[CurToken++]; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 376 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 377 | // The token's current location indicate where the token was lexed from. We |
| 378 | // need this information to compute the spelling of the token, but any |
| 379 | // diagnostics for the expanded token should appear as if they came from |
| 380 | // InstantiationLoc. Pull this information together into a new SourceLocation |
| 381 | // that captures all of this. |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 382 | if (InstantiateLoc.isValid()) { // Don't do this for token streams. |
| 383 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 384 | // The token could have come from a prior macro expansion. In that case, |
| 385 | // ignore the macro expand part to get to the physloc. This happens for |
| 386 | // stuff like: #define A(X) X A(A(X)) A(1) |
| 387 | SourceLocation PhysLoc = SrcMgr.getPhysicalLoc(Tok.getLocation()); |
| 388 | Tok.SetLocation(SrcMgr.getInstantiationLoc(PhysLoc, InstantiateLoc)); |
| 389 | } |
| 390 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 391 | // If this is the first token, set the lexical properties of the token to |
| 392 | // match the lexical properties of the macro identifier. |
| 393 | if (CurToken == 1) { |
| 394 | Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine); |
| 395 | Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace); |
| 396 | } |
| 397 | |
| 398 | // Handle recursive expansion! |
| 399 | if (Tok.getIdentifierInfo()) |
| 400 | return PP.HandleIdentifier(Tok); |
| 401 | |
| 402 | // Otherwise, return a normal token. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 403 | } |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 404 | |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 405 | /// isNextTokenLParen - If the next token lexed will pop this macro off the |
| 406 | /// expansion stack, return 2. If the next unexpanded token is a '(', return |
| 407 | /// 1, otherwise return 0. |
| 408 | unsigned MacroExpander::isNextTokenLParen() const { |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 409 | // Out of tokens? |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 410 | if (isAtEnd()) |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 411 | return 2; |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 412 | return (*MacroTokens)[CurToken].getKind() == tok::l_paren; |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 413 | } |