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); |
| 52 | UnexpArgTokens.back().push_back(EOFTok); |
| 53 | } |
| 54 | |
| 55 | |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 56 | /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of |
| 57 | /// tokens into the literal string token that should be produced by the C # |
| 58 | /// preprocessor operator. |
| 59 | /// |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 60 | static LexerToken StringifyArgument(const std::vector<LexerToken> &Toks, |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 61 | Preprocessor &PP, bool Charify = false) { |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 62 | LexerToken Tok; |
| 63 | Tok.StartToken(); |
| 64 | Tok.SetKind(tok::string_literal); |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 65 | |
| 66 | // Stringify all the tokens. |
| 67 | std::string Result = "\""; |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame^] | 68 | 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] | 69 | const LexerToken &Tok = Toks[i]; |
| 70 | // FIXME: Optimize this. |
| 71 | if (i != 0 && Tok.hasLeadingSpace()) |
| 72 | Result += ' '; |
| 73 | |
| 74 | // If this is a string or character constant, escape the token as specified |
| 75 | // by 6.10.3.2p2. |
| 76 | if (Tok.getKind() == tok::string_literal || // "foo" and L"foo". |
| 77 | Tok.getKind() == tok::char_constant) { // 'x' and L'x'. |
| 78 | Result += Lexer::Stringify(PP.getSpelling(Tok)); |
| 79 | } else { |
| 80 | // Otherwise, just append the token. |
| 81 | Result += PP.getSpelling(Tok); |
| 82 | } |
| 83 | } |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 85 | // If the last character of the string is a \, and if it isn't escaped, this |
| 86 | // is an invalid string literal, diagnose it as specified in C99. |
| 87 | if (Result[Result.size()-1] == '\\') { |
| 88 | // Count the number of consequtive \ characters. If even, then they are |
| 89 | // just escaped backslashes, otherwise it's an error. |
| 90 | unsigned FirstNonSlash = Result.size()-2; |
| 91 | // Guaranteed to find the starting " if nothing else. |
| 92 | while (Result[FirstNonSlash] == '\\') |
| 93 | --FirstNonSlash; |
| 94 | if ((Result.size()-1-FirstNonSlash) & 1) { |
Chris Lattner | f278150 | 2006-07-15 05:27:44 +0000 | [diff] [blame] | 95 | // Diagnose errors for things like: #define F(X) #X / F(\) |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 96 | PP.Diag(Toks.back(), diag::pp_invalid_string_literal); |
| 97 | Result.erase(Result.end()-1); // remove one of the \'s. |
| 98 | } |
| 99 | } |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 100 | Result += '"'; |
| 101 | |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 102 | // If this is the charify operation and the result is not a legal character |
| 103 | // constant, diagnose it. |
| 104 | if (Charify) { |
| 105 | // First step, turn double quotes into single quotes: |
| 106 | Result[0] = '\''; |
| 107 | Result[Result.size()-1] = '\''; |
| 108 | |
| 109 | // Check for bogus character. |
| 110 | bool isBad = false; |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame^] | 111 | if (Result.size() == 3) { |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 112 | isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above. |
| 113 | } else { |
| 114 | isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x' |
| 115 | } |
| 116 | |
| 117 | if (isBad) { |
| 118 | assert(!Toks.empty() && "No tokens to charize?"); |
| 119 | PP.Diag(Toks[0], diag::err_invalid_character_to_charify); |
| 120 | Result = "' '"; |
| 121 | } |
| 122 | } |
| 123 | |
Chris Lattner | 0707bd3 | 2006-07-15 05:23:58 +0000 | [diff] [blame] | 124 | Tok.SetLength(Result.size()); |
| 125 | Tok.SetLocation(PP.CreateString(&Result[0], Result.size())); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 126 | return Tok; |
| 127 | } |
| 128 | |
| 129 | /// getStringifiedArgument - Compute, cache, and return the specified argument |
| 130 | /// that has been 'stringified' as required by the # operator. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 131 | const LexerToken &MacroArgs::getStringifiedArgument(unsigned ArgNo, |
| 132 | Preprocessor &PP) { |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame^] | 133 | assert(ArgNo < UnexpArgTokens.size() && "Invalid argument number!"); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 134 | if (StringifiedArgs.empty()) { |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame^] | 135 | StringifiedArgs.resize(getNumArguments()); |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 136 | memset(&StringifiedArgs[0], 0, |
| 137 | sizeof(StringifiedArgs[0])*getNumArguments()); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 138 | } |
| 139 | if (StringifiedArgs[ArgNo].getKind() != tok::string_literal) |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame^] | 140 | StringifiedArgs[ArgNo] = StringifyArgument(UnexpArgTokens[ArgNo], PP); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 141 | return StringifiedArgs[ArgNo]; |
| 142 | } |
| 143 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 144 | //===----------------------------------------------------------------------===// |
| 145 | // MacroExpander Implementation |
| 146 | //===----------------------------------------------------------------------===// |
| 147 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 148 | MacroExpander::MacroExpander(LexerToken &Tok, MacroArgs *Actuals, |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 149 | Preprocessor &pp) |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 150 | : Macro(*Tok.getIdentifierInfo()->getMacroInfo()), |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 151 | ActualArgs(Actuals), PP(pp), CurToken(0), |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 152 | InstantiateLoc(Tok.getLocation()), |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 153 | AtStartOfLine(Tok.isAtStartOfLine()), |
| 154 | HasLeadingSpace(Tok.hasLeadingSpace()) { |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 155 | MacroTokens = &Macro.getReplacementTokens(); |
| 156 | |
| 157 | // If this is a function-like macro, expand the arguments and change |
| 158 | // MacroTokens to point to the expanded tokens. |
| 159 | if (Macro.isFunctionLike() && Macro.getNumArgs()) |
| 160 | ExpandFunctionArguments(); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 163 | MacroExpander::~MacroExpander() { |
| 164 | // If this was a function-like macro that actually uses its arguments, delete |
| 165 | // the expanded tokens. |
| 166 | if (MacroTokens != &Macro.getReplacementTokens()) |
| 167 | delete MacroTokens; |
| 168 | |
| 169 | // MacroExpander owns its formal arguments. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 170 | delete ActualArgs; |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 173 | |
| 174 | |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 175 | /// Expand the arguments of a function-like macro so that we can quickly |
| 176 | /// return preexpanded tokens from MacroTokens. |
| 177 | void MacroExpander::ExpandFunctionArguments() { |
| 178 | std::vector<LexerToken> ResultToks; |
| 179 | |
| 180 | // Loop through the MacroTokens tokens, expanding them into ResultToks. Keep |
| 181 | // track of whether we change anything. If not, no need to keep them. If so, |
| 182 | // we install the newly expanded sequence as MacroTokens. |
| 183 | bool MadeChange = false; |
| 184 | for (unsigned i = 0, e = MacroTokens->size(); i != e; ++i) { |
| 185 | // If we found the stringify operator, get the argument stringified. The |
| 186 | // preprocessor already verified that the following token is a macro name |
| 187 | // when the #define was parsed. |
| 188 | const LexerToken &CurTok = (*MacroTokens)[i]; |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 189 | if (CurTok.getKind() == tok::hash || CurTok.getKind() == tok::hashat) { |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 190 | int ArgNo = Macro.getArgumentNum((*MacroTokens)[i+1].getIdentifierInfo()); |
| 191 | assert(ArgNo != -1 && "Token following # is not an argument?"); |
| 192 | |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 193 | if (CurTok.getKind() == tok::hash) // Stringify |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 194 | ResultToks.push_back(ActualArgs->getStringifiedArgument(ArgNo, PP)); |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 195 | else { |
| 196 | // 'charify': don't bother caching these. |
| 197 | ResultToks.push_back(StringifyArgument( |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 198 | ActualArgs->getUnexpArgument(ArgNo), PP, true)); |
Chris Lattner | c783d1d | 2006-07-15 06:11:25 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 6016169 | 2006-07-15 06:48:02 +0000 | [diff] [blame] | 201 | // The stringified/charified string leading space flag gets set to match |
| 202 | // the #/#@ operator. |
| 203 | if (CurTok.hasLeadingSpace()) |
| 204 | ResultToks.back().SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 205 | |
| 206 | MadeChange = true; |
| 207 | ++i; // Skip arg name. |
| 208 | } else { |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 209 | ResultToks.push_back(CurTok); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // If anything changed, install this as the new MacroTokens list. |
| 214 | if (MadeChange) { |
| 215 | // This is deleted in the dtor. |
| 216 | std::vector<LexerToken> *Res = new std::vector<LexerToken>(); |
| 217 | Res->swap(ResultToks); |
| 218 | MacroTokens = Res; |
| 219 | } |
| 220 | } |
Chris Lattner | 67b07cb | 2006-06-26 02:03:42 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 222 | /// Lex - Lex and return a token from this macro stream. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 223 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 224 | void MacroExpander::Lex(LexerToken &Tok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 225 | // Lexing off the end of the macro, pop this macro off the expansion stack. |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 226 | if (isAtEnd()) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 227 | return PP.HandleEndOfMacro(Tok); |
| 228 | |
| 229 | // Get the next token to return. |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 230 | Tok = (*MacroTokens)[CurToken++]; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 231 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 232 | // The token's current location indicate where the token was lexed from. We |
| 233 | // need this information to compute the spelling of the token, but any |
| 234 | // diagnostics for the expanded token should appear as if they came from |
| 235 | // InstantiationLoc. Pull this information together into a new SourceLocation |
| 236 | // that captures all of this. |
| 237 | Tok.SetLocation(PP.getSourceManager().getInstantiationLoc(Tok.getLocation(), |
| 238 | InstantiateLoc)); |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 240 | // If this is the first token, set the lexical properties of the token to |
| 241 | // match the lexical properties of the macro identifier. |
| 242 | if (CurToken == 1) { |
| 243 | Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine); |
| 244 | Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace); |
| 245 | } |
| 246 | |
| 247 | // Handle recursive expansion! |
| 248 | if (Tok.getIdentifierInfo()) |
| 249 | return PP.HandleIdentifier(Tok); |
| 250 | |
| 251 | // Otherwise, return a normal token. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 253 | |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 254 | /// isNextTokenLParen - If the next token lexed will pop this macro off the |
| 255 | /// expansion stack, return 2. If the next unexpanded token is a '(', return |
| 256 | /// 1, otherwise return 0. |
| 257 | unsigned MacroExpander::isNextTokenLParen() const { |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 258 | // Out of tokens? |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 259 | if (isAtEnd()) |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 260 | return 2; |
Chris Lattner | b935d8c | 2006-07-14 06:54:44 +0000 | [diff] [blame] | 261 | return (*MacroTokens)[CurToken].getKind() == tok::l_paren; |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 262 | } |