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