Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- LiteralSupport.cpp - Code to parse and process literals ----------===// |
| 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 | // |
| 10 | // This file implements the NumericLiteralParser, CharLiteralParser, and |
| 11 | // StringLiteralParser interfaces. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/LiteralSupport.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 | 136f93a | 2007-07-16 06:55:01 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Eli Friedman | f74a458 | 2011-11-01 02:14:50 +0000 | [diff] [blame] | 19 | #include "clang/Basic/ConvertUTF.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
David Blaikie | 9fe8c74 | 2011-09-23 05:35:21 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's |
| 25 | /// not valid. |
| 26 | static int HexDigitValue(char C) { |
| 27 | if (C >= '0' && C <= '9') return C-'0'; |
| 28 | if (C >= 'a' && C <= 'f') return C-'a'+10; |
| 29 | if (C >= 'A' && C <= 'F') return C-'A'+10; |
| 30 | return -1; |
| 31 | } |
| 32 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 33 | static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) { |
| 34 | switch (kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 35 | default: llvm_unreachable("Unknown token type!"); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 36 | case tok::char_constant: |
| 37 | case tok::string_literal: |
| 38 | case tok::utf8_string_literal: |
| 39 | return Target.getCharWidth(); |
| 40 | case tok::wide_char_constant: |
| 41 | case tok::wide_string_literal: |
| 42 | return Target.getWCharWidth(); |
| 43 | case tok::utf16_char_constant: |
| 44 | case tok::utf16_string_literal: |
| 45 | return Target.getChar16Width(); |
| 46 | case tok::utf32_char_constant: |
| 47 | case tok::utf32_string_literal: |
| 48 | return Target.getChar32Width(); |
| 49 | } |
| 50 | } |
| 51 | |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 52 | /// \brief Produce a diagnostic highlighting some portion of a literal. |
| 53 | /// |
| 54 | /// Emits the diagnostic \p DiagID, highlighting the range of characters from |
| 55 | /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be |
| 56 | /// a substring of a spelling buffer for the token beginning at \p TokBegin. |
| 57 | static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, |
| 58 | const LangOptions &Features, FullSourceLoc TokLoc, |
| 59 | const char *TokBegin, const char *TokRangeBegin, |
| 60 | const char *TokRangeEnd, unsigned DiagID) { |
| 61 | SourceLocation Begin = |
| 62 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
| 63 | TokLoc.getManager(), Features); |
| 64 | SourceLocation End = |
| 65 | Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin, |
| 66 | TokLoc.getManager(), Features); |
| 67 | return Diags->Report(Begin, DiagID) |
| 68 | << CharSourceRange::getCharRange(Begin, End); |
| 69 | } |
| 70 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in |
| 72 | /// either a character or a string literal. |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 73 | static unsigned ProcessCharEscape(const char *ThisTokBegin, |
| 74 | const char *&ThisTokBuf, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | const char *ThisTokEnd, bool &HadError, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 76 | FullSourceLoc Loc, unsigned CharWidth, |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 77 | DiagnosticsEngine *Diags, |
| 78 | const LangOptions &Features) { |
| 79 | const char *EscapeBegin = ThisTokBuf; |
| 80 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | // Skip the '\' char. |
| 82 | ++ThisTokBuf; |
| 83 | |
| 84 | // We know that this character can't be off the end of the buffer, because |
| 85 | // that would have been \", which would not have been the end of string. |
| 86 | unsigned ResultChar = *ThisTokBuf++; |
| 87 | switch (ResultChar) { |
| 88 | // These map to themselves. |
| 89 | case '\\': case '\'': case '"': case '?': break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | // These have fixed mappings. |
| 92 | case 'a': |
| 93 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 94 | ResultChar = 7; |
| 95 | break; |
| 96 | case 'b': |
| 97 | ResultChar = 8; |
| 98 | break; |
| 99 | case 'e': |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 100 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 101 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 102 | diag::ext_nonstandard_escape) << "e"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | ResultChar = 27; |
| 104 | break; |
Eli Friedman | 3c54801 | 2009-06-10 01:32:39 +0000 | [diff] [blame] | 105 | case 'E': |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 106 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 107 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 108 | diag::ext_nonstandard_escape) << "E"; |
Eli Friedman | 3c54801 | 2009-06-10 01:32:39 +0000 | [diff] [blame] | 109 | ResultChar = 27; |
| 110 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | case 'f': |
| 112 | ResultChar = 12; |
| 113 | break; |
| 114 | case 'n': |
| 115 | ResultChar = 10; |
| 116 | break; |
| 117 | case 'r': |
| 118 | ResultChar = 13; |
| 119 | break; |
| 120 | case 't': |
| 121 | ResultChar = 9; |
| 122 | break; |
| 123 | case 'v': |
| 124 | ResultChar = 11; |
| 125 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | case 'x': { // Hex escape. |
| 127 | ResultChar = 0; |
| 128 | if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) { |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 129 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 130 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 131 | diag::err_hex_escape_no_digits); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | HadError = 1; |
| 133 | break; |
| 134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | // Hex escapes are a maximal series of hex digits. |
| 137 | bool Overflow = false; |
| 138 | for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { |
| 139 | int CharVal = HexDigitValue(ThisTokBuf[0]); |
| 140 | if (CharVal == -1) break; |
Chris Lattner | c29bbde | 2008-09-30 20:45:40 +0000 | [diff] [blame] | 141 | // About to shift out a digit? |
| 142 | Overflow |= (ResultChar & 0xF0000000) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | ResultChar <<= 4; |
| 144 | ResultChar |= CharVal; |
| 145 | } |
| 146 | |
| 147 | // See if any bits will be truncated when evaluated as a character. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 149 | Overflow = true; |
| 150 | ResultChar &= ~0U >> (32-CharWidth); |
| 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | // Check for overflow. |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 154 | if (Overflow && Diags) // Too many digits to fit in |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 155 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 156 | diag::warn_hex_escape_too_large); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | case '0': case '1': case '2': case '3': |
| 160 | case '4': case '5': case '6': case '7': { |
| 161 | // Octal escapes. |
| 162 | --ThisTokBuf; |
| 163 | ResultChar = 0; |
| 164 | |
| 165 | // Octal escapes are a series of octal digits with maximum length 3. |
| 166 | // "\0123" is a two digit sequence equal to "\012" "3". |
| 167 | unsigned NumDigits = 0; |
| 168 | do { |
| 169 | ResultChar <<= 3; |
| 170 | ResultChar |= *ThisTokBuf++ - '0'; |
| 171 | ++NumDigits; |
| 172 | } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && |
| 173 | ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | // Check for overflow. Reject '\777', but not L'\777'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 177 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 178 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 179 | diag::warn_octal_escape_too_large); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | ResultChar &= ~0U >> (32-CharWidth); |
| 181 | } |
| 182 | break; |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | // Otherwise, these are not valid escapes. |
| 186 | case '(': case '{': case '[': case '%': |
| 187 | // GCC accepts these as extensions. We warn about them as such though. |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 188 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 189 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 190 | diag::ext_nonstandard_escape) |
| 191 | << std::string(1, ResultChar); |
Eli Friedman | f01fdff | 2009-04-28 00:51:18 +0000 | [diff] [blame] | 192 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | default: |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 194 | if (Diags == 0) |
Douglas Gregor | b90f4b3 | 2010-05-26 05:35:51 +0000 | [diff] [blame] | 195 | break; |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | 23ef69d | 2010-12-03 00:09:56 +0000 | [diff] [blame] | 197 | if (isgraph(ResultChar)) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 198 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 199 | diag::ext_unknown_escape) |
| 200 | << std::string(1, ResultChar); |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 201 | else |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 202 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
| 203 | diag::ext_unknown_escape) |
| 204 | << "x" + llvm::utohexstr(ResultChar); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | break; |
| 206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | return ResultChar; |
| 209 | } |
| 210 | |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 211 | /// ProcessUCNEscape - Read the Universal Character Name, check constraints and |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 212 | /// return the UTF32. |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 213 | static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 214 | const char *ThisTokEnd, |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 215 | uint32_t &UcnVal, unsigned short &UcnLen, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 216 | FullSourceLoc Loc, DiagnosticsEngine *Diags, |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 217 | const LangOptions &Features, |
| 218 | bool in_char_string_literal = false) { |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 219 | const char *UcnBegin = ThisTokBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 221 | // Skip the '\u' char's. |
| 222 | ThisTokBuf += 2; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 224 | if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) { |
Chris Lattner | 6c66f07 | 2010-11-17 06:46:14 +0000 | [diff] [blame] | 225 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 226 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 227 | diag::err_ucn_escape_no_digits); |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 228 | return false; |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 229 | } |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 230 | UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8); |
Fariborz Jahanian | 56bedef | 2010-08-31 23:34:27 +0000 | [diff] [blame] | 231 | unsigned short UcnLenSave = UcnLen; |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 232 | for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) { |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 233 | int CharVal = HexDigitValue(ThisTokBuf[0]); |
| 234 | if (CharVal == -1) break; |
| 235 | UcnVal <<= 4; |
| 236 | UcnVal |= CharVal; |
| 237 | } |
| 238 | // If we didn't consume the proper number of digits, there is a problem. |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 239 | if (UcnLenSave) { |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 240 | if (Diags) |
| 241 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 242 | diag::err_ucn_escape_incomplete); |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 243 | return false; |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 244 | } |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 245 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 246 | // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2] |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 247 | if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints |
| 248 | UcnVal > 0x10FFFF) { // maximum legal UTF32 value |
Chris Lattner | 6c66f07 | 2010-11-17 06:46:14 +0000 | [diff] [blame] | 249 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 250 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 251 | diag::err_ucn_escape_invalid); |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 252 | return false; |
| 253 | } |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 254 | |
| 255 | // C++11 allows UCNs that refer to control characters and basic source |
| 256 | // characters inside character and string literals |
| 257 | if (UcnVal < 0xa0 && |
| 258 | (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, ` |
| 259 | bool IsError = (!Features.CPlusPlus0x || !in_char_string_literal); |
| 260 | if (Diags) { |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 261 | char BasicSCSChar = UcnVal; |
| 262 | if (UcnVal >= 0x20 && UcnVal < 0x7f) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 263 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 264 | IsError ? diag::err_ucn_escape_basic_scs : |
| 265 | diag::warn_cxx98_compat_literal_ucn_escape_basic_scs) |
| 266 | << StringRef(&BasicSCSChar, 1); |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 267 | else |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 268 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 269 | IsError ? diag::err_ucn_control_character : |
| 270 | diag::warn_cxx98_compat_literal_ucn_control_character); |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 271 | } |
| 272 | if (IsError) |
| 273 | return false; |
| 274 | } |
| 275 | |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 276 | if (!Features.CPlusPlus && !Features.C99 && Diags) |
| 277 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
| 278 | diag::warn_ucn_not_valid_in_c89); |
| 279 | |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 283 | /// MeasureUCNEscape - Determine the number of bytes within the resulting string |
| 284 | /// which this UCN will occupy. |
| 285 | static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 286 | const char *ThisTokEnd, unsigned CharByteWidth, |
| 287 | const LangOptions &Features, bool &HadError) { |
| 288 | // UTF-32: 4 bytes per escape. |
| 289 | if (CharByteWidth == 4) |
| 290 | return 4; |
| 291 | |
| 292 | uint32_t UcnVal = 0; |
| 293 | unsigned short UcnLen = 0; |
| 294 | FullSourceLoc Loc; |
| 295 | |
| 296 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, |
| 297 | UcnLen, Loc, 0, Features, true)) { |
| 298 | HadError = true; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | // UTF-16: 2 bytes for BMP, 4 bytes otherwise. |
| 303 | if (CharByteWidth == 2) |
| 304 | return UcnVal <= 0xFFFF ? 2 : 4; |
| 305 | |
| 306 | // UTF-8. |
| 307 | if (UcnVal < 0x80) |
| 308 | return 1; |
| 309 | if (UcnVal < 0x800) |
| 310 | return 2; |
| 311 | if (UcnVal < 0x10000) |
| 312 | return 3; |
| 313 | return 4; |
| 314 | } |
| 315 | |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 316 | /// EncodeUCNEscape - Read the Universal Character Name, check constraints and |
| 317 | /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of |
| 318 | /// StringLiteralParser. When we decide to implement UCN's for identifiers, |
| 319 | /// we will likely rework our support for UCN's. |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 320 | static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
| 321 | const char *ThisTokEnd, |
Chris Lattner | a95880d | 2010-11-17 07:12:42 +0000 | [diff] [blame] | 322 | char *&ResultBuf, bool &HadError, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 323 | FullSourceLoc Loc, unsigned CharByteWidth, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 324 | DiagnosticsEngine *Diags, |
| 325 | const LangOptions &Features) { |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 326 | typedef uint32_t UTF32; |
| 327 | UTF32 UcnVal = 0; |
| 328 | unsigned short UcnLen = 0; |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 329 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, |
| 330 | Loc, Diags, Features, true)) { |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 331 | HadError = true; |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 332 | return; |
| 333 | } |
Nico Weber | 59705ae | 2010-10-09 00:27:47 +0000 | [diff] [blame] | 334 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 335 | assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth) && |
| 336 | "only character widths of 1, 2, or 4 bytes supported"); |
Nico Weber | a0f15b0 | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 338 | (void)UcnLen; |
| 339 | assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported"); |
Nico Weber | a0f15b0 | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 340 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 341 | if (CharByteWidth == 4) { |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 342 | // FIXME: Make the type of the result buffer correct instead of |
| 343 | // using reinterpret_cast. |
| 344 | UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf); |
| 345 | *ResultPtr = UcnVal; |
| 346 | ResultBuf += 4; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (CharByteWidth == 2) { |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 351 | // FIXME: Make the type of the result buffer correct instead of |
| 352 | // using reinterpret_cast. |
| 353 | UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf); |
| 354 | |
Richard Smith | 59b26d8 | 2012-06-13 05:41:29 +0000 | [diff] [blame] | 355 | if (UcnVal <= (UTF32)0xFFFF) { |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 356 | *ResultPtr = UcnVal; |
| 357 | ResultBuf += 2; |
Nico Weber | a0f15b0 | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 358 | return; |
| 359 | } |
Nico Weber | a0f15b0 | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 360 | |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 361 | // Convert to UTF16. |
Nico Weber | a0f15b0 | 2010-10-06 04:57:26 +0000 | [diff] [blame] | 362 | UcnVal -= 0x10000; |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 363 | *ResultPtr = 0xD800 + (UcnVal >> 10); |
| 364 | *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF); |
| 365 | ResultBuf += 4; |
Fariborz Jahanian | 56bedef | 2010-08-31 23:34:27 +0000 | [diff] [blame] | 366 | return; |
| 367 | } |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 368 | |
| 369 | assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters"); |
| 370 | |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 371 | // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8. |
| 372 | // The conversion below was inspired by: |
| 373 | // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | // First, we determine how many bytes the result will require. |
Steve Naroff | 4e93b34 | 2009-04-01 11:09:15 +0000 | [diff] [blame] | 375 | typedef uint8_t UTF8; |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 376 | |
| 377 | unsigned short bytesToWrite = 0; |
| 378 | if (UcnVal < (UTF32)0x80) |
| 379 | bytesToWrite = 1; |
| 380 | else if (UcnVal < (UTF32)0x800) |
| 381 | bytesToWrite = 2; |
| 382 | else if (UcnVal < (UTF32)0x10000) |
| 383 | bytesToWrite = 3; |
| 384 | else |
| 385 | bytesToWrite = 4; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 387 | const unsigned byteMask = 0xBF; |
| 388 | const unsigned byteMark = 0x80; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 390 | // Once the bits are split out into bytes of UTF8, this is a mask OR-ed |
Steve Naroff | 8a5c0cd | 2009-03-31 10:29:45 +0000 | [diff] [blame] | 391 | // into the first byte, depending on how many bytes follow. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | static const UTF8 firstByteMark[5] = { |
Steve Naroff | 8a5c0cd | 2009-03-31 10:29:45 +0000 | [diff] [blame] | 393 | 0x00, 0x00, 0xC0, 0xE0, 0xF0 |
Steve Naroff | 0e3e3eb | 2009-03-30 23:46:03 +0000 | [diff] [blame] | 394 | }; |
| 395 | // Finally, we write the bytes into ResultBuf. |
| 396 | ResultBuf += bytesToWrite; |
| 397 | switch (bytesToWrite) { // note: everything falls through. |
| 398 | case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 399 | case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 400 | case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
| 401 | case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]); |
| 402 | } |
| 403 | // Update the buffer. |
| 404 | ResultBuf += bytesToWrite; |
| 405 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | |
| 407 | |
| 408 | /// integer-constant: [C99 6.4.4.1] |
| 409 | /// decimal-constant integer-suffix |
| 410 | /// octal-constant integer-suffix |
| 411 | /// hexadecimal-constant integer-suffix |
Richard Smith | 49d5174 | 2012-03-08 21:59:28 +0000 | [diff] [blame] | 412 | /// user-defined-integer-literal: [C++11 lex.ext] |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 413 | /// decimal-literal ud-suffix |
| 414 | /// octal-literal ud-suffix |
| 415 | /// hexadecimal-literal ud-suffix |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | /// decimal-constant: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | /// nonzero-digit |
| 418 | /// decimal-constant digit |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | /// octal-constant: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 420 | /// 0 |
| 421 | /// octal-constant octal-digit |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | /// hexadecimal-constant: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | /// hexadecimal-prefix hexadecimal-digit |
| 424 | /// hexadecimal-constant hexadecimal-digit |
| 425 | /// hexadecimal-prefix: one of |
| 426 | /// 0x 0X |
| 427 | /// integer-suffix: |
| 428 | /// unsigned-suffix [long-suffix] |
| 429 | /// unsigned-suffix [long-long-suffix] |
| 430 | /// long-suffix [unsigned-suffix] |
| 431 | /// long-long-suffix [unsigned-sufix] |
| 432 | /// nonzero-digit: |
| 433 | /// 1 2 3 4 5 6 7 8 9 |
| 434 | /// octal-digit: |
| 435 | /// 0 1 2 3 4 5 6 7 |
| 436 | /// hexadecimal-digit: |
| 437 | /// 0 1 2 3 4 5 6 7 8 9 |
| 438 | /// a b c d e f |
| 439 | /// A B C D E F |
| 440 | /// unsigned-suffix: one of |
| 441 | /// u U |
| 442 | /// long-suffix: one of |
| 443 | /// l L |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | /// long-long-suffix: one of |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | /// ll LL |
| 446 | /// |
| 447 | /// floating-constant: [C99 6.4.4.2] |
| 448 | /// TODO: add rules... |
| 449 | /// |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 450 | NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, |
| 451 | SourceLocation TokLoc, |
| 452 | Preprocessor &PP) |
| 453 | : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Chris Lattner | c29bbde | 2008-09-30 20:45:40 +0000 | [diff] [blame] | 455 | // This routine assumes that the range begin/end matches the regex for integer |
| 456 | // and FP constants (specifically, the 'pp-number' regex), and assumes that |
| 457 | // the byte at "*end" is both valid and not part of the regex. Because of |
| 458 | // this, it doesn't have to check for 'overscan' in various places. |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 459 | assert(!isalnum(*ThisTokEnd) && *ThisTokEnd != '.' && *ThisTokEnd != '_' && |
Chris Lattner | c29bbde | 2008-09-30 20:45:40 +0000 | [diff] [blame] | 460 | "Lexer didn't maximally munch?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 462 | s = DigitsBegin = ThisTokBegin; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | saw_exponent = false; |
| 464 | saw_period = false; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 465 | saw_ud_suffix = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | isLong = false; |
| 467 | isUnsigned = false; |
| 468 | isLongLong = false; |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 469 | isFloat = false; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 470 | isImaginary = false; |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 471 | isMicrosoftInteger = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 472 | hadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | if (*s == '0') { // parse radix |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 475 | ParseNumberStartingWithZero(TokLoc); |
| 476 | if (hadError) |
| 477 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | } else { // the first digit is non-zero |
| 479 | radix = 10; |
| 480 | s = SkipDigits(s); |
| 481 | if (s == ThisTokEnd) { |
| 482 | // Done. |
Christopher Lamb | 016765e | 2007-11-29 06:06:27 +0000 | [diff] [blame] | 483 | } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) { |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 484 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 485 | diag::err_invalid_decimal_digit) << StringRef(s, 1); |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 486 | hadError = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | return; |
| 488 | } else if (*s == '.') { |
| 489 | s++; |
| 490 | saw_period = true; |
| 491 | s = SkipDigits(s); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | } |
Chris Lattner | 4411f46 | 2008-09-29 23:12:31 +0000 | [diff] [blame] | 493 | if ((*s == 'e' || *s == 'E')) { // exponent |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 494 | const char *Exponent = s; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | s++; |
| 496 | saw_exponent = true; |
| 497 | if (*s == '+' || *s == '-') s++; // sign |
| 498 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 499 | if (first_non_digit != s) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | s = first_non_digit; |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 501 | } else { |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 502 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin), |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 503 | diag::err_exponent_has_no_digits); |
| 504 | hadError = true; |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 505 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | SuffixBegin = s; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 512 | // Parse the suffix. At this point we can classify whether we have an FP or |
| 513 | // integer constant. |
| 514 | bool isFPConstant = isFloatingLiteral(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 516 | // Loop over all of the characters of the suffix. If we see something bad, |
| 517 | // we break out of the loop. |
| 518 | for (; s != ThisTokEnd; ++s) { |
| 519 | switch (*s) { |
| 520 | case 'f': // FP Suffix for "float" |
| 521 | case 'F': |
| 522 | if (!isFPConstant) break; // Error for integer constant. |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 523 | if (isFloat || isLong) break; // FF, LF invalid. |
| 524 | isFloat = true; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 525 | continue; // Success. |
| 526 | case 'u': |
| 527 | case 'U': |
| 528 | if (isFPConstant) break; // Error for floating constant. |
| 529 | if (isUnsigned) break; // Cannot be repeated. |
| 530 | isUnsigned = true; |
| 531 | continue; // Success. |
| 532 | case 'l': |
| 533 | case 'L': |
| 534 | if (isLong || isLongLong) break; // Cannot be repeated. |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 535 | if (isFloat) break; // LF invalid. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 537 | // Check for long long. The L's need to be adjacent and the same case. |
| 538 | if (s+1 != ThisTokEnd && s[1] == s[0]) { |
| 539 | if (isFPConstant) break; // long long invalid for floats. |
| 540 | isLongLong = true; |
| 541 | ++s; // Eat both of them. |
| 542 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | isLong = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 545 | continue; // Success. |
| 546 | case 'i': |
Chris Lattner | c637415 | 2010-10-14 00:24:10 +0000 | [diff] [blame] | 547 | case 'I': |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 548 | if (PP.getLangOpts().MicrosoftExt) { |
Fariborz Jahanian | a8be02b | 2010-01-22 21:36:53 +0000 | [diff] [blame] | 549 | if (isFPConstant || isLong || isLongLong) break; |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 550 | |
Steve Naroff | 0c29b22 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 551 | // Allow i8, i16, i32, i64, and i128. |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 552 | if (s + 1 != ThisTokEnd) { |
| 553 | switch (s[1]) { |
| 554 | case '8': |
| 555 | s += 2; // i8 suffix |
| 556 | isMicrosoftInteger = true; |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 557 | break; |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 558 | case '1': |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 559 | if (s + 2 == ThisTokEnd) break; |
Francois Pichet | d062b60 | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 560 | if (s[2] == '6') { |
| 561 | s += 3; // i16 suffix |
| 562 | isMicrosoftInteger = true; |
| 563 | } |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 564 | else if (s[2] == '2') { |
| 565 | if (s + 3 == ThisTokEnd) break; |
Francois Pichet | d062b60 | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 566 | if (s[3] == '8') { |
| 567 | s += 4; // i128 suffix |
| 568 | isMicrosoftInteger = true; |
| 569 | } |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 570 | } |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 571 | break; |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 572 | case '3': |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 573 | if (s + 2 == ThisTokEnd) break; |
Francois Pichet | d062b60 | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 574 | if (s[2] == '2') { |
| 575 | s += 3; // i32 suffix |
| 576 | isLong = true; |
| 577 | isMicrosoftInteger = true; |
| 578 | } |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 579 | break; |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 580 | case '6': |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 581 | if (s + 2 == ThisTokEnd) break; |
Francois Pichet | d062b60 | 2011-01-11 11:57:53 +0000 | [diff] [blame] | 582 | if (s[2] == '4') { |
| 583 | s += 3; // i64 suffix |
| 584 | isLongLong = true; |
| 585 | isMicrosoftInteger = true; |
| 586 | } |
Nuno Lopes | 6e8c7ac | 2009-11-28 13:37:52 +0000 | [diff] [blame] | 587 | break; |
Mike Stump | b79fe2d | 2009-10-08 22:55:36 +0000 | [diff] [blame] | 588 | default: |
| 589 | break; |
| 590 | } |
| 591 | break; |
Steve Naroff | 0c29b22 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 592 | } |
Steve Naroff | 0c29b22 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 593 | } |
| 594 | // fall through. |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 595 | case 'j': |
| 596 | case 'J': |
| 597 | if (isImaginary) break; // Cannot be repeated. |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 598 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 599 | diag::ext_imaginary_constant); |
| 600 | isImaginary = true; |
| 601 | continue; // Success. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | } |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 603 | // If we reached here, there was an error or a ud-suffix. |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 604 | break; |
| 605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 607 | if (s != ThisTokEnd) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 608 | if (PP.getLangOpts().CPlusPlus0x && s == SuffixBegin && *s == '_') { |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 609 | // We have a ud-suffix! By C++11 [lex.ext]p10, ud-suffixes not starting |
| 610 | // with an '_' are ill-formed. |
| 611 | saw_ud_suffix = true; |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | // Report an error if there are any. |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 616 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin), |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 617 | isFPConstant ? diag::err_invalid_suffix_float_constant : |
| 618 | diag::err_invalid_suffix_integer_constant) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 619 | << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin); |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 620 | hadError = true; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 621 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 625 | /// ParseNumberStartingWithZero - This method is called when the first character |
| 626 | /// of the number is found to be a zero. This means it is either an octal |
| 627 | /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | /// a floating point number (01239.123e4). Eat the prefix, determining the |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 629 | /// radix etc. |
| 630 | void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { |
| 631 | assert(s[0] == '0' && "Invalid method call"); |
| 632 | s++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 634 | // Handle a hex number like 0x1234. |
| 635 | if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) { |
| 636 | s++; |
| 637 | radix = 16; |
| 638 | DigitsBegin = s; |
| 639 | s = SkipHexDigits(s); |
Aaron Ballman | 66b0eba | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 640 | bool noSignificand = (s == DigitsBegin); |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 641 | if (s == ThisTokEnd) { |
| 642 | // Done. |
| 643 | } else if (*s == '.') { |
| 644 | s++; |
| 645 | saw_period = true; |
Aaron Ballman | 66b0eba | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 646 | const char *floatDigitsBegin = s; |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 647 | s = SkipHexDigits(s); |
Aaron Ballman | 66b0eba | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 648 | noSignificand &= (floatDigitsBegin == s); |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 649 | } |
Aaron Ballman | 66b0eba | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 650 | |
| 651 | if (noSignificand) { |
Dmitri Gribenko | fc97ea2 | 2012-09-24 09:53:54 +0000 | [diff] [blame] | 652 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), |
Aaron Ballman | 66b0eba | 2012-02-08 13:36:33 +0000 | [diff] [blame] | 653 | diag::err_hexconstant_requires_digits); |
| 654 | hadError = true; |
| 655 | return; |
| 656 | } |
| 657 | |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 658 | // A binary exponent can appear with or with a '.'. If dotted, the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | // binary exponent is required. |
Douglas Gregor | 1155c42 | 2011-08-30 22:40:35 +0000 | [diff] [blame] | 660 | if (*s == 'p' || *s == 'P') { |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 661 | const char *Exponent = s; |
| 662 | s++; |
| 663 | saw_exponent = true; |
| 664 | if (*s == '+' || *s == '-') s++; // sign |
| 665 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | 6ea6238 | 2008-07-25 18:18:34 +0000 | [diff] [blame] | 666 | if (first_non_digit == s) { |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 667 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), |
| 668 | diag::err_exponent_has_no_digits); |
| 669 | hadError = true; |
Chris Lattner | 6ea6238 | 2008-07-25 18:18:34 +0000 | [diff] [blame] | 670 | return; |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 6ea6238 | 2008-07-25 18:18:34 +0000 | [diff] [blame] | 672 | s = first_non_digit; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 674 | if (!PP.getLangOpts().HexFloats) |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 675 | PP.Diag(TokLoc, diag::ext_hexconstant_invalid); |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 676 | } else if (saw_period) { |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 677 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
| 678 | diag::err_hexconstant_requires_exponent); |
| 679 | hadError = true; |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 680 | } |
| 681 | return; |
| 682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 684 | // Handle simple binary numbers 0b01010 |
| 685 | if (*s == 'b' || *s == 'B') { |
| 686 | // 0b101010 is a GCC extension. |
Chris Lattner | 413d355 | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 687 | PP.Diag(TokLoc, diag::ext_binary_literal); |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 688 | ++s; |
| 689 | radix = 2; |
| 690 | DigitsBegin = s; |
| 691 | s = SkipBinaryDigits(s); |
| 692 | if (s == ThisTokEnd) { |
| 693 | // Done. |
| 694 | } else if (isxdigit(*s)) { |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 695 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 696 | diag::err_invalid_binary_digit) << StringRef(s, 1); |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 697 | hadError = true; |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 698 | } |
Chris Lattner | 413d355 | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 699 | // Other suffixes will be diagnosed by the caller. |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 700 | return; |
| 701 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 703 | // For now, the radix is set to 8. If we discover that we have a |
| 704 | // floating point constant, the radix will change to 10. Octal floating |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | // point constants are not permitted (only decimal and hexadecimal). |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 706 | radix = 8; |
| 707 | DigitsBegin = s; |
| 708 | s = SkipOctalDigits(s); |
| 709 | if (s == ThisTokEnd) |
| 710 | return; // Done, simple octal number like 01234 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 413d355 | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 712 | // If we have some other non-octal digit that *is* a decimal digit, see if |
| 713 | // this is part of a floating point number like 094.123 or 09e1. |
| 714 | if (isdigit(*s)) { |
| 715 | const char *EndDecimal = SkipDigits(s); |
| 716 | if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') { |
| 717 | s = EndDecimal; |
| 718 | radix = 10; |
| 719 | } |
| 720 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 413d355 | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 722 | // If we have a hex digit other than 'e' (which denotes a FP exponent) then |
| 723 | // the code is using an incorrect base. |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 724 | if (isxdigit(*s) && *s != 'e' && *s != 'E') { |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 725 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 726 | diag::err_invalid_octal_digit) << StringRef(s, 1); |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 727 | hadError = true; |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 731 | if (*s == '.') { |
| 732 | s++; |
| 733 | radix = 10; |
| 734 | saw_period = true; |
Chris Lattner | 413d355 | 2008-06-30 06:44:49 +0000 | [diff] [blame] | 735 | s = SkipDigits(s); // Skip suffix. |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 736 | } |
| 737 | if (*s == 'e' || *s == 'E') { // exponent |
| 738 | const char *Exponent = s; |
| 739 | s++; |
| 740 | radix = 10; |
| 741 | saw_exponent = true; |
| 742 | if (*s == '+' || *s == '-') s++; // sign |
| 743 | const char *first_non_digit = SkipDigits(s); |
| 744 | if (first_non_digit != s) { |
| 745 | s = first_non_digit; |
| 746 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), |
Chris Lattner | ac92d82 | 2008-11-22 07:23:31 +0000 | [diff] [blame] | 748 | diag::err_exponent_has_no_digits); |
| 749 | hadError = true; |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
Dmitri Gribenko | 191046d | 2012-09-25 19:09:15 +0000 | [diff] [blame^] | 755 | static bool CanFitInto64Bits(unsigned Radix, unsigned NumDigits) { |
| 756 | switch (Radix) { |
| 757 | case 2: |
| 758 | return NumDigits <= 64; |
| 759 | case 8: |
| 760 | return NumDigits <= 64 / 3; // Digits are groups of 3 bits. |
| 761 | case 10: |
| 762 | return NumDigits <= 19; // floor(log10(2^64)) |
| 763 | case 16: |
| 764 | return NumDigits <= 64 / 4; // Digits are groups of 4 bits. |
| 765 | default: |
| 766 | llvm_unreachable("impossible Radix"); |
| 767 | } |
| 768 | } |
Chris Lattner | 368328c | 2008-06-30 06:39:54 +0000 | [diff] [blame] | 769 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | /// GetIntegerValue - Convert this numeric literal value to an APInt that |
| 771 | /// matches Val's input width. If there is an overflow, set Val to the low bits |
| 772 | /// of the result and return true. Otherwise, return false. |
| 773 | bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { |
Daniel Dunbar | a179be3 | 2008-10-16 07:32:01 +0000 | [diff] [blame] | 774 | // Fast path: Compute a conservative bound on the maximum number of |
| 775 | // bits per digit in this radix. If we can't possibly overflow a |
| 776 | // uint64 based on that bound then do the simple conversion to |
| 777 | // integer. This avoids the expensive overflow checking below, and |
| 778 | // handles the common cases that matter (small decimal integers and |
| 779 | // hex/octal values which don't overflow). |
Dmitri Gribenko | 191046d | 2012-09-25 19:09:15 +0000 | [diff] [blame^] | 780 | const unsigned NumDigits = SuffixBegin - DigitsBegin; |
| 781 | if (CanFitInto64Bits(radix, NumDigits)) { |
Daniel Dunbar | a179be3 | 2008-10-16 07:32:01 +0000 | [diff] [blame] | 782 | uint64_t N = 0; |
Dmitri Gribenko | 191046d | 2012-09-25 19:09:15 +0000 | [diff] [blame^] | 783 | for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr) |
| 784 | N = N * radix + HexDigitValue(*Ptr); |
Daniel Dunbar | a179be3 | 2008-10-16 07:32:01 +0000 | [diff] [blame] | 785 | |
| 786 | // This will truncate the value to Val's input width. Simply check |
| 787 | // for overflow by comparing. |
| 788 | Val = N; |
| 789 | return Val.getZExtValue() != N; |
| 790 | } |
| 791 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | Val = 0; |
Dmitri Gribenko | 191046d | 2012-09-25 19:09:15 +0000 | [diff] [blame^] | 793 | const char *Ptr = DigitsBegin; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 794 | |
| 795 | llvm::APInt RadixVal(Val.getBitWidth(), radix); |
| 796 | llvm::APInt CharVal(Val.getBitWidth(), 0); |
| 797 | llvm::APInt OldVal = Val; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | bool OverflowOccurred = false; |
Dmitri Gribenko | 191046d | 2012-09-25 19:09:15 +0000 | [diff] [blame^] | 800 | while (Ptr < SuffixBegin) { |
| 801 | unsigned C = HexDigitValue(*Ptr++); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | // If this letter is out of bound for this radix, reject it. |
| 804 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 806 | CharVal = C; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 808 | // Add the digit to the value in the appropriate radix. If adding in digits |
| 809 | // made the value smaller, then this overflowed. |
| 810 | OldVal = Val; |
| 811 | |
| 812 | // Multiply by radix, did overflow occur on the multiply? |
| 813 | Val *= RadixVal; |
| 814 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
| 815 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | // Add value, did overflow occur on the value? |
Daniel Dunbar | d70cb64 | 2008-10-16 06:39:30 +0000 | [diff] [blame] | 817 | // (a + b) ult b <=> overflow |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | Val += CharVal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | OverflowOccurred |= Val.ult(CharVal); |
| 820 | } |
| 821 | return OverflowOccurred; |
| 822 | } |
| 823 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 824 | llvm::APFloat::opStatus |
| 825 | NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) { |
Ted Kremenek | 427d5af | 2007-11-26 23:12:30 +0000 | [diff] [blame] | 826 | using llvm::APFloat; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Erick Tryzelaar | e9f195f | 2009-08-16 23:36:28 +0000 | [diff] [blame] | 828 | unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin); |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 829 | return Result.convertFromString(StringRef(ThisTokBegin, n), |
| 830 | APFloat::rmNearestTiesToEven); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 833 | |
James Dennett | 58f9ce1 | 2012-06-17 03:34:42 +0000 | [diff] [blame] | 834 | /// \verbatim |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 835 | /// user-defined-character-literal: [C++11 lex.ext] |
| 836 | /// character-literal ud-suffix |
| 837 | /// ud-suffix: |
| 838 | /// identifier |
| 839 | /// character-literal: [C++11 lex.ccon] |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 840 | /// ' c-char-sequence ' |
| 841 | /// u' c-char-sequence ' |
| 842 | /// U' c-char-sequence ' |
| 843 | /// L' c-char-sequence ' |
| 844 | /// c-char-sequence: |
| 845 | /// c-char |
| 846 | /// c-char-sequence c-char |
| 847 | /// c-char: |
| 848 | /// any member of the source character set except the single-quote ', |
| 849 | /// backslash \, or new-line character |
| 850 | /// escape-sequence |
| 851 | /// universal-character-name |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 852 | /// escape-sequence: |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 853 | /// simple-escape-sequence |
| 854 | /// octal-escape-sequence |
| 855 | /// hexadecimal-escape-sequence |
| 856 | /// simple-escape-sequence: |
NAKAMURA Takumi | ddddd48 | 2011-08-12 05:49:51 +0000 | [diff] [blame] | 857 | /// one of \' \" \? \\ \a \b \f \n \r \t \v |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 858 | /// octal-escape-sequence: |
| 859 | /// \ octal-digit |
| 860 | /// \ octal-digit octal-digit |
| 861 | /// \ octal-digit octal-digit octal-digit |
| 862 | /// hexadecimal-escape-sequence: |
| 863 | /// \x hexadecimal-digit |
| 864 | /// hexadecimal-escape-sequence hexadecimal-digit |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 865 | /// universal-character-name: [C++11 lex.charset] |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 866 | /// \u hex-quad |
| 867 | /// \U hex-quad hex-quad |
| 868 | /// hex-quad: |
| 869 | /// hex-digit hex-digit hex-digit hex-digit |
James Dennett | 58f9ce1 | 2012-06-17 03:34:42 +0000 | [diff] [blame] | 870 | /// \endverbatim |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 871 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 872 | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 873 | SourceLocation Loc, Preprocessor &PP, |
| 874 | tok::TokenKind kind) { |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 875 | // At this point we know that the character matches the regex "(L|u|U)?'.*'". |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | HadError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 878 | Kind = kind; |
| 879 | |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 880 | const char *TokBegin = begin; |
| 881 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 882 | // Skip over wide character determinant. |
| 883 | if (Kind != tok::char_constant) { |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 884 | ++begin; |
| 885 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | // Skip over the entry quote. |
| 888 | assert(begin[0] == '\'' && "Invalid token lexed"); |
| 889 | ++begin; |
| 890 | |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 891 | // Remove an optional ud-suffix. |
| 892 | if (end[-1] != '\'') { |
| 893 | const char *UDSuffixEnd = end; |
| 894 | do { |
| 895 | --end; |
| 896 | } while (end[-1] != '\''); |
| 897 | UDSuffixBuf.assign(end, UDSuffixEnd); |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 898 | UDSuffixOffset = end - TokBegin; |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 901 | // Trim the ending quote. |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 902 | assert(end != begin && "Invalid token lexed"); |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 903 | --end; |
| 904 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | // FIXME: The "Value" is an uint64_t so we can handle char literals of |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 906 | // up to 64-bits. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 907 | // FIXME: This extensively assumes that 'char' is 8-bits. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 908 | assert(PP.getTargetInfo().getCharWidth() == 8 && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 909 | "Assumes char is 8 bits"); |
Chris Lattner | e3ad881 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 910 | assert(PP.getTargetInfo().getIntWidth() <= 64 && |
| 911 | (PP.getTargetInfo().getIntWidth() & 7) == 0 && |
| 912 | "Assumes sizeof(int) on target is <= 64 and a multiple of char"); |
| 913 | assert(PP.getTargetInfo().getWCharWidth() <= 64 && |
| 914 | "Assumes sizeof(wchar) on target is <= 64"); |
Sanjiv Gupta | 4bc11af | 2009-04-21 02:21:29 +0000 | [diff] [blame] | 915 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 916 | SmallVector<uint32_t,4> codepoint_buffer; |
| 917 | codepoint_buffer.resize(end-begin); |
| 918 | uint32_t *buffer_begin = &codepoint_buffer.front(); |
| 919 | uint32_t *buffer_end = buffer_begin + codepoint_buffer.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 921 | // Unicode escapes representing characters that cannot be correctly |
| 922 | // represented in a single code unit are disallowed in character literals |
| 923 | // by this implementation. |
| 924 | uint32_t largest_character_for_kind; |
| 925 | if (tok::wide_char_constant == Kind) { |
| 926 | largest_character_for_kind = 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth()); |
| 927 | } else if (tok::utf16_char_constant == Kind) { |
| 928 | largest_character_for_kind = 0xFFFF; |
| 929 | } else if (tok::utf32_char_constant == Kind) { |
| 930 | largest_character_for_kind = 0x10FFFF; |
| 931 | } else { |
| 932 | largest_character_for_kind = 0x7Fu; |
Chris Lattner | e3ad881 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 935 | while (begin!=end) { |
| 936 | // Is this a span of non-escape characters? |
| 937 | if (begin[0] != '\\') { |
| 938 | char const *start = begin; |
| 939 | do { |
| 940 | ++begin; |
| 941 | } while (begin != end && *begin != '\\'); |
| 942 | |
Eli Friedman | 9135930 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 943 | char const *tmp_in_start = start; |
| 944 | uint32_t *tmp_out_start = buffer_begin; |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 945 | ConversionResult res = |
| 946 | ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start), |
| 947 | reinterpret_cast<UTF8 const *>(begin), |
| 948 | &buffer_begin,buffer_end,strictConversion); |
| 949 | if (res!=conversionOK) { |
Eli Friedman | 9135930 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 950 | // If we see bad encoding for unprefixed character literals, warn and |
| 951 | // simply copy the byte values, for compatibility with gcc and |
| 952 | // older versions of clang. |
| 953 | bool NoErrorOnBadEncoding = isAscii(); |
| 954 | unsigned Msg = diag::err_bad_character_encoding; |
| 955 | if (NoErrorOnBadEncoding) |
| 956 | Msg = diag::warn_bad_character_encoding; |
| 957 | PP.Diag(Loc, Msg); |
| 958 | if (NoErrorOnBadEncoding) { |
| 959 | start = tmp_in_start; |
| 960 | buffer_begin = tmp_out_start; |
| 961 | for ( ; start != begin; ++start, ++buffer_begin) |
| 962 | *buffer_begin = static_cast<uint8_t>(*start); |
| 963 | } else { |
| 964 | HadError = true; |
| 965 | } |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 966 | } else { |
Eli Friedman | 9135930 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 967 | for (; tmp_out_start <buffer_begin; ++tmp_out_start) { |
| 968 | if (*tmp_out_start > largest_character_for_kind) { |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 969 | HadError = true; |
| 970 | PP.Diag(Loc, diag::err_character_too_large); |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | continue; |
| 976 | } |
| 977 | // Is this a Universal Character Name excape? |
| 978 | if (begin[1] == 'u' || begin[1] == 'U') { |
| 979 | unsigned short UcnLen = 0; |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 980 | if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen, |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 981 | FullSourceLoc(Loc, PP.getSourceManager()), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 982 | &PP.getDiagnostics(), PP.getLangOpts(), |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 983 | true)) |
| 984 | { |
| 985 | HadError = true; |
| 986 | } else if (*buffer_begin > largest_character_for_kind) { |
| 987 | HadError = true; |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 988 | PP.Diag(Loc, diag::err_character_too_large); |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | ++buffer_begin; |
| 992 | continue; |
| 993 | } |
| 994 | unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo()); |
| 995 | uint64_t result = |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 996 | ProcessCharEscape(TokBegin, begin, end, HadError, |
| 997 | FullSourceLoc(Loc,PP.getSourceManager()), |
| 998 | CharWidth, &PP.getDiagnostics(), PP.getLangOpts()); |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 999 | *buffer_begin++ = result; |
| 1000 | } |
| 1001 | |
| 1002 | unsigned NumCharsSoFar = buffer_begin-&codepoint_buffer.front(); |
| 1003 | |
Chris Lattner | e3ad881 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 1004 | if (NumCharsSoFar > 1) { |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1005 | if (isWide()) |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1006 | PP.Diag(Loc, diag::warn_extraneous_char_constant); |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1007 | else if (isAscii() && NumCharsSoFar == 4) |
| 1008 | PP.Diag(Loc, diag::ext_four_char_character_literal); |
| 1009 | else if (isAscii()) |
Chris Lattner | e3ad881 | 2009-04-28 21:51:46 +0000 | [diff] [blame] | 1010 | PP.Diag(Loc, diag::ext_multichar_character_literal); |
| 1011 | else |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1012 | PP.Diag(Loc, diag::err_multichar_utf_character_literal); |
Eli Friedman | 2a1c363 | 2009-06-01 05:25:02 +0000 | [diff] [blame] | 1013 | IsMultiChar = true; |
Daniel Dunbar | 930b71a | 2009-07-29 01:46:05 +0000 | [diff] [blame] | 1014 | } else |
| 1015 | IsMultiChar = false; |
Sanjiv Gupta | 4bc11af | 2009-04-21 02:21:29 +0000 | [diff] [blame] | 1016 | |
Seth Cantrell | be77352 | 2012-01-18 12:27:04 +0000 | [diff] [blame] | 1017 | llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0); |
| 1018 | |
| 1019 | // Narrow character literals act as though their value is concatenated |
| 1020 | // in this implementation, but warn on overflow. |
| 1021 | bool multi_char_too_long = false; |
| 1022 | if (isAscii() && isMultiChar()) { |
| 1023 | LitVal = 0; |
| 1024 | for (size_t i=0;i<NumCharsSoFar;++i) { |
| 1025 | // check for enough leading zeros to shift into |
| 1026 | multi_char_too_long |= (LitVal.countLeadingZeros() < 8); |
| 1027 | LitVal <<= 8; |
| 1028 | LitVal = LitVal + (codepoint_buffer[i] & 0xFF); |
| 1029 | } |
| 1030 | } else if (NumCharsSoFar > 0) { |
| 1031 | // otherwise just take the last character |
| 1032 | LitVal = buffer_begin[-1]; |
| 1033 | } |
| 1034 | |
| 1035 | if (!HadError && multi_char_too_long) { |
| 1036 | PP.Diag(Loc,diag::warn_char_constant_too_large); |
| 1037 | } |
| 1038 | |
Sanjiv Gupta | 4bc11af | 2009-04-21 02:21:29 +0000 | [diff] [blame] | 1039 | // Transfer the value from APInt to uint64_t |
| 1040 | Value = LitVal.getZExtValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") |
| 1043 | // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple |
| 1044 | // character constants are not sign extended in the this implementation: |
| 1045 | // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1046 | if (isAscii() && NumCharsSoFar == 1 && (Value & 128) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1047 | PP.getLangOpts().CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | Value = (signed char)Value; |
| 1049 | } |
| 1050 | |
James Dennett | a1263cf | 2012-06-19 21:04:25 +0000 | [diff] [blame] | 1051 | /// \verbatim |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1052 | /// string-literal: [C++0x lex.string] |
| 1053 | /// encoding-prefix " [s-char-sequence] " |
| 1054 | /// encoding-prefix R raw-string |
| 1055 | /// encoding-prefix: |
| 1056 | /// u8 |
| 1057 | /// u |
| 1058 | /// U |
| 1059 | /// L |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1060 | /// s-char-sequence: |
| 1061 | /// s-char |
| 1062 | /// s-char-sequence s-char |
| 1063 | /// s-char: |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1064 | /// any member of the source character set except the double-quote ", |
| 1065 | /// backslash \, or new-line character |
| 1066 | /// escape-sequence |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | /// universal-character-name |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1068 | /// raw-string: |
| 1069 | /// " d-char-sequence ( r-char-sequence ) d-char-sequence " |
| 1070 | /// r-char-sequence: |
| 1071 | /// r-char |
| 1072 | /// r-char-sequence r-char |
| 1073 | /// r-char: |
| 1074 | /// any member of the source character set, except a right parenthesis ) |
| 1075 | /// followed by the initial d-char-sequence (which may be empty) |
| 1076 | /// followed by a double quote ". |
| 1077 | /// d-char-sequence: |
| 1078 | /// d-char |
| 1079 | /// d-char-sequence d-char |
| 1080 | /// d-char: |
| 1081 | /// any member of the basic source character set except: |
| 1082 | /// space, the left parenthesis (, the right parenthesis ), |
| 1083 | /// the backslash \, and the control characters representing horizontal |
| 1084 | /// tab, vertical tab, form feed, and newline. |
| 1085 | /// escape-sequence: [C++0x lex.ccon] |
| 1086 | /// simple-escape-sequence |
| 1087 | /// octal-escape-sequence |
| 1088 | /// hexadecimal-escape-sequence |
| 1089 | /// simple-escape-sequence: |
NAKAMURA Takumi | ddddd48 | 2011-08-12 05:49:51 +0000 | [diff] [blame] | 1090 | /// one of \' \" \? \\ \a \b \f \n \r \t \v |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1091 | /// octal-escape-sequence: |
| 1092 | /// \ octal-digit |
| 1093 | /// \ octal-digit octal-digit |
| 1094 | /// \ octal-digit octal-digit octal-digit |
| 1095 | /// hexadecimal-escape-sequence: |
| 1096 | /// \x hexadecimal-digit |
| 1097 | /// hexadecimal-escape-sequence hexadecimal-digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1098 | /// universal-character-name: |
| 1099 | /// \u hex-quad |
| 1100 | /// \U hex-quad hex-quad |
| 1101 | /// hex-quad: |
| 1102 | /// hex-digit hex-digit hex-digit hex-digit |
James Dennett | a1263cf | 2012-06-19 21:04:25 +0000 | [diff] [blame] | 1103 | /// \endverbatim |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1104 | /// |
| 1105 | StringLiteralParser:: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1106 | StringLiteralParser(const Token *StringToks, unsigned NumStringToks, |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1107 | Preprocessor &PP, bool Complain) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1108 | : SM(PP.getSourceManager()), Features(PP.getLangOpts()), |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1109 | Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() : 0), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1110 | MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), |
| 1111 | ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) { |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1112 | init(StringToks, NumStringToks); |
| 1113 | } |
| 1114 | |
| 1115 | void StringLiteralParser::init(const Token *StringToks, unsigned NumStringToks){ |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1116 | // The literal token may have come from an invalid source location (e.g. due |
| 1117 | // to a PCH error), in which case the token length will be 0. |
Argyrios Kyrtzidis | 3144749 | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1118 | if (NumStringToks == 0 || StringToks[0].getLength() < 2) |
| 1119 | return DiagnoseLexingError(SourceLocation()); |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1120 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | // Scan all of the string portions, remember the max individual token length, |
| 1122 | // computing a bound on the concatenated string length, and see whether any |
| 1123 | // piece is a wide-string. If any of the string portions is a wide-string |
| 1124 | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1125 | assert(NumStringToks && "expected at least one token"); |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1126 | MaxTokenLength = StringToks[0].getLength(); |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1127 | assert(StringToks[0].getLength() >= 2 && "literal token is invalid!"); |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1128 | SizeBound = StringToks[0].getLength()-2; // -2 for "". |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1129 | Kind = StringToks[0].getKind(); |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1130 | |
| 1131 | hadError = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Implement Translation Phase #6: concatenation of string literals |
| 1134 | /// (C99 5.1.1.2p1). The common case is only one string fragment. |
| 1135 | for (unsigned i = 1; i != NumStringToks; ++i) { |
Argyrios Kyrtzidis | 3144749 | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1136 | if (StringToks[i].getLength() < 2) |
| 1137 | return DiagnoseLexingError(StringToks[i].getLocation()); |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1138 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1139 | // The string could be shorter than this if it needs cleaning, but this is a |
| 1140 | // reasonable bound, which is all we need. |
Argyrios Kyrtzidis | 403de3f | 2011-05-17 22:09:56 +0000 | [diff] [blame] | 1141 | assert(StringToks[i].getLength() >= 2 && "literal token is invalid!"); |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1142 | SizeBound += StringToks[i].getLength()-2; // -2 for "". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1144 | // Remember maximum string piece length. |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1145 | if (StringToks[i].getLength() > MaxTokenLength) |
| 1146 | MaxTokenLength = StringToks[i].getLength(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1148 | // Remember if we see any wide or utf-8/16/32 strings. |
| 1149 | // Also check for illegal concatenations. |
| 1150 | if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) { |
| 1151 | if (isAscii()) { |
| 1152 | Kind = StringToks[i].getKind(); |
| 1153 | } else { |
| 1154 | if (Diags) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1155 | Diags->Report(StringToks[i].getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1156 | diag::err_unsupported_string_concat); |
| 1157 | hadError = true; |
| 1158 | } |
| 1159 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1160 | } |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1161 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1162 | // Include space for the null terminator. |
| 1163 | ++SizeBound; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1165 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1167 | // Get the width in bytes of char/wchar_t/char16_t/char32_t |
| 1168 | CharByteWidth = getCharWidth(Kind, Target); |
| 1169 | assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); |
| 1170 | CharByteWidth /= 8; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1172 | // The output buffer size needs to be large enough to hold wide characters. |
| 1173 | // This is a worst-case assumption which basically corresponds to L"" "long". |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1174 | SizeBound *= CharByteWidth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1176 | // Size the temporary buffer to hold the result string data. |
| 1177 | ResultBuf.resize(SizeBound); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | // Likewise, but for each string piece. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1180 | SmallString<512> TokenBuf; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1181 | TokenBuf.resize(MaxTokenLength); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | // Loop over all the strings, getting their spelling, and expanding them to |
| 1184 | // wide strings as appropriate. |
| 1185 | ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 1187 | Pascal = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 1189 | SourceLocation UDSuffixTokLoc; |
| 1190 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | for (unsigned i = 0, e = NumStringToks; i != e; ++i) { |
| 1192 | const char *ThisTokBuf = &TokenBuf[0]; |
| 1193 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 1194 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 1195 | // and 'spelled' tokens can only shrink. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1196 | bool StringInvalid = false; |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1197 | unsigned ThisTokLen = |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 1198 | Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features, |
| 1199 | &StringInvalid); |
Argyrios Kyrtzidis | 3144749 | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1200 | if (StringInvalid) |
| 1201 | return DiagnoseLexingError(StringToks[i].getLocation()); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1202 | |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 1203 | const char *ThisTokBegin = ThisTokBuf; |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 1204 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen; |
| 1205 | |
| 1206 | // Remove an optional ud-suffix. |
| 1207 | if (ThisTokEnd[-1] != '"') { |
| 1208 | const char *UDSuffixEnd = ThisTokEnd; |
| 1209 | do { |
| 1210 | --ThisTokEnd; |
| 1211 | } while (ThisTokEnd[-1] != '"'); |
| 1212 | |
| 1213 | StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd); |
| 1214 | |
| 1215 | if (UDSuffixBuf.empty()) { |
| 1216 | UDSuffixBuf.assign(UDSuffix); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1217 | UDSuffixToken = i; |
| 1218 | UDSuffixOffset = ThisTokEnd - ThisTokBuf; |
Richard Smith | 5cc2c6e | 2012-03-05 04:02:15 +0000 | [diff] [blame] | 1219 | UDSuffixTokLoc = StringToks[i].getLocation(); |
| 1220 | } else if (!UDSuffixBuf.equals(UDSuffix)) { |
| 1221 | // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the |
| 1222 | // result of a concatenation involving at least one user-defined-string- |
| 1223 | // literal, all the participating user-defined-string-literals shall |
| 1224 | // have the same ud-suffix. |
| 1225 | if (Diags) { |
| 1226 | SourceLocation TokLoc = StringToks[i].getLocation(); |
| 1227 | Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix) |
| 1228 | << UDSuffixBuf << UDSuffix |
| 1229 | << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc) |
| 1230 | << SourceRange(TokLoc, TokLoc); |
| 1231 | } |
| 1232 | hadError = true; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | // Strip the end quote. |
| 1237 | --ThisTokEnd; |
| 1238 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1239 | // TODO: Input character set mapping support. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Craig Topper | 1661d71 | 2011-08-08 06:10:39 +0000 | [diff] [blame] | 1241 | // Skip marker for wide or unicode strings. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1242 | if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1243 | ++ThisTokBuf; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1244 | // Skip 8 of u8 marker for utf8 strings. |
| 1245 | if (ThisTokBuf[0] == '8') |
| 1246 | ++ThisTokBuf; |
Fariborz Jahanian | 56bedef | 2010-08-31 23:34:27 +0000 | [diff] [blame] | 1247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1249 | // Check for raw string |
| 1250 | if (ThisTokBuf[0] == 'R') { |
| 1251 | ThisTokBuf += 2; // skip R" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1253 | const char *Prefix = ThisTokBuf; |
| 1254 | while (ThisTokBuf[0] != '(') |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 1255 | ++ThisTokBuf; |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1256 | ++ThisTokBuf; // skip '(' |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Richard Smith | 49d5174 | 2012-03-08 21:59:28 +0000 | [diff] [blame] | 1258 | // Remove same number of characters from the end |
| 1259 | ThisTokEnd -= ThisTokBuf - Prefix; |
| 1260 | assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal"); |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Copy the string over |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1263 | if (CopyStringFragment(StringToks[i], ThisTokBegin, |
| 1264 | StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf))) |
| 1265 | hadError = true; |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1266 | } else { |
Argyrios Kyrtzidis | 07a0758 | 2012-05-03 01:01:56 +0000 | [diff] [blame] | 1267 | if (ThisTokBuf[0] != '"') { |
| 1268 | // The file may have come from PCH and then changed after loading the |
| 1269 | // PCH; Fail gracefully. |
Argyrios Kyrtzidis | 3144749 | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1270 | return DiagnoseLexingError(StringToks[i].getLocation()); |
Argyrios Kyrtzidis | 07a0758 | 2012-05-03 01:01:56 +0000 | [diff] [blame] | 1271 | } |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1272 | ++ThisTokBuf; // skip " |
| 1273 | |
| 1274 | // Check if this is a pascal string |
| 1275 | if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd && |
| 1276 | ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') { |
| 1277 | |
| 1278 | // If the \p sequence is found in the first token, we have a pascal string |
| 1279 | // Otherwise, if we already have a pascal string, ignore the first \p |
| 1280 | if (i == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | ++ThisTokBuf; |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1282 | Pascal = true; |
| 1283 | } else if (Pascal) |
| 1284 | ThisTokBuf += 2; |
| 1285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1287 | while (ThisTokBuf != ThisTokEnd) { |
| 1288 | // Is this a span of non-escape characters? |
| 1289 | if (ThisTokBuf[0] != '\\') { |
| 1290 | const char *InStart = ThisTokBuf; |
| 1291 | do { |
| 1292 | ++ThisTokBuf; |
| 1293 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 1294 | |
| 1295 | // Copy the character span over. |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1296 | if (CopyStringFragment(StringToks[i], ThisTokBegin, |
| 1297 | StringRef(InStart, ThisTokBuf - InStart))) |
| 1298 | hadError = true; |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1299 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | } |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1301 | // Is this a Universal Character Name escape? |
| 1302 | if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') { |
Richard Smith | 26b75c0 | 2012-03-09 22:27:51 +0000 | [diff] [blame] | 1303 | EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, |
| 1304 | ResultPtr, hadError, |
| 1305 | FullSourceLoc(StringToks[i].getLocation(), SM), |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1306 | CharByteWidth, Diags, Features); |
| 1307 | continue; |
| 1308 | } |
| 1309 | // Otherwise, this is a non-UCN escape character. Process it. |
| 1310 | unsigned ResultChar = |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1311 | ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError, |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1312 | FullSourceLoc(StringToks[i].getLocation(), SM), |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1313 | CharByteWidth*8, Diags, Features); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 1315 | if (CharByteWidth == 4) { |
| 1316 | // FIXME: Make the type of the result buffer correct instead of |
| 1317 | // using reinterpret_cast. |
| 1318 | UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr); |
Nico Weber | 9b483df | 2011-11-14 05:17:37 +0000 | [diff] [blame] | 1319 | *ResultWidePtr = ResultChar; |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 1320 | ResultPtr += 4; |
| 1321 | } else if (CharByteWidth == 2) { |
| 1322 | // FIXME: Make the type of the result buffer correct instead of |
| 1323 | // using reinterpret_cast. |
| 1324 | UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr); |
Nico Weber | 9b483df | 2011-11-14 05:17:37 +0000 | [diff] [blame] | 1325 | *ResultWidePtr = ResultChar & 0xFFFF; |
Eli Friedman | caf1f26 | 2011-11-02 23:06:23 +0000 | [diff] [blame] | 1326 | ResultPtr += 2; |
| 1327 | } else { |
| 1328 | assert(CharByteWidth == 1 && "Unexpected char width"); |
| 1329 | *ResultPtr++ = ResultChar & 0xFF; |
| 1330 | } |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1331 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1335 | if (Pascal) { |
Eli Friedman | 22508f4 | 2011-11-05 00:41:04 +0000 | [diff] [blame] | 1336 | if (CharByteWidth == 4) { |
| 1337 | // FIXME: Make the type of the result buffer correct instead of |
| 1338 | // using reinterpret_cast. |
| 1339 | UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data()); |
| 1340 | ResultWidePtr[0] = GetNumStringChars() - 1; |
| 1341 | } else if (CharByteWidth == 2) { |
| 1342 | // FIXME: Make the type of the result buffer correct instead of |
| 1343 | // using reinterpret_cast. |
| 1344 | UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data()); |
| 1345 | ResultWidePtr[0] = GetNumStringChars() - 1; |
| 1346 | } else { |
| 1347 | assert(CharByteWidth == 1 && "Unexpected char width"); |
| 1348 | ResultBuf[0] = GetNumStringChars() - 1; |
| 1349 | } |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1350 | |
| 1351 | // Verify that pascal strings aren't too large. |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1352 | if (GetStringLength() > 256) { |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1353 | if (Diags) |
| 1354 | Diags->Report(StringToks[0].getLocation(), |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1355 | diag::err_pascal_string_too_long) |
| 1356 | << SourceRange(StringToks[0].getLocation(), |
| 1357 | StringToks[NumStringToks-1].getLocation()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1358 | hadError = true; |
Eli Friedman | 57d7dde | 2009-04-01 03:17:08 +0000 | [diff] [blame] | 1359 | return; |
| 1360 | } |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1361 | } else if (Diags) { |
Douglas Gregor | 427c492 | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1362 | // Complain if this string literal has too many characters. |
Chris Lattner | a95880d | 2010-11-17 07:12:42 +0000 | [diff] [blame] | 1363 | unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509; |
Douglas Gregor | 427c492 | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (GetNumStringChars() > MaxChars) |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1366 | Diags->Report(StringToks[0].getLocation(), |
Chris Lattner | 0833dd0 | 2010-11-17 07:21:13 +0000 | [diff] [blame] | 1367 | diag::ext_string_too_long) |
Douglas Gregor | 427c492 | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1368 | << GetNumStringChars() << MaxChars |
Chris Lattner | a95880d | 2010-11-17 07:12:42 +0000 | [diff] [blame] | 1369 | << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0) |
Douglas Gregor | 427c492 | 2010-07-20 14:33:20 +0000 | [diff] [blame] | 1370 | << SourceRange(StringToks[0].getLocation(), |
| 1371 | StringToks[NumStringToks-1].getLocation()); |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1372 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | } |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1374 | |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1375 | /// \brief This function copies from Fragment, which is a sequence of bytes |
| 1376 | /// within Tok's contents (which begin at TokBegin) into ResultPtr. |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1377 | /// Performs widening for multi-byte characters. |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1378 | bool StringLiteralParser::CopyStringFragment(const Token &Tok, |
| 1379 | const char *TokBegin, |
| 1380 | StringRef Fragment) { |
| 1381 | const UTF8 *ErrorPtrTmp; |
| 1382 | if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp)) |
| 1383 | return false; |
| 1384 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1385 | |
Eli Friedman | 9135930 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1386 | // If we see bad encoding for unprefixed string literals, warn and |
| 1387 | // simply copy the byte values, for compatibility with gcc and older |
| 1388 | // versions of clang. |
| 1389 | bool NoErrorOnBadEncoding = isAscii(); |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1390 | if (NoErrorOnBadEncoding) { |
| 1391 | memcpy(ResultPtr, Fragment.data(), Fragment.size()); |
| 1392 | ResultPtr += Fragment.size(); |
| 1393 | } |
| 1394 | if (Diags) { |
| 1395 | Diag(Diags, Features, FullSourceLoc(Tok.getLocation(), SM), TokBegin, |
| 1396 | ErrorPtr, ErrorPtr + std::min<unsigned>(getNumBytesForUTF8(*ErrorPtr), |
| 1397 | Fragment.end() - ErrorPtr), |
| 1398 | NoErrorOnBadEncoding ? diag::warn_bad_string_encoding |
| 1399 | : diag::err_bad_string_encoding); |
| 1400 | } |
Eli Friedman | 9135930 | 2012-02-11 05:08:10 +0000 | [diff] [blame] | 1401 | return !NoErrorOnBadEncoding; |
| 1402 | } |
Craig Topper | 2fa4e86 | 2011-08-11 04:06:15 +0000 | [diff] [blame] | 1403 | |
Argyrios Kyrtzidis | 3144749 | 2012-05-03 17:50:32 +0000 | [diff] [blame] | 1404 | void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) { |
| 1405 | hadError = true; |
| 1406 | if (Diags) |
| 1407 | Diags->Report(Loc, diag::err_lexing_string); |
| 1408 | } |
| 1409 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1410 | /// getOffsetOfStringByte - This function returns the offset of the |
| 1411 | /// specified byte of the string data represented by Token. This handles |
| 1412 | /// advancing over escape sequences in the string. |
| 1413 | unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok, |
Chris Lattner | 6c66f07 | 2010-11-17 06:46:14 +0000 | [diff] [blame] | 1414 | unsigned ByteNo) const { |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1415 | // Get the spelling of the token. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1416 | SmallString<32> SpellingBuffer; |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1417 | SpellingBuffer.resize(Tok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1419 | bool StringInvalid = false; |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1420 | const char *SpellingPtr = &SpellingBuffer[0]; |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 1421 | unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features, |
| 1422 | &StringInvalid); |
Chris Lattner | 91f54ce | 2010-11-17 06:26:08 +0000 | [diff] [blame] | 1423 | if (StringInvalid) |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1424 | return 0; |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1425 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1426 | const char *SpellingStart = SpellingPtr; |
| 1427 | const char *SpellingEnd = SpellingPtr+TokLen; |
| 1428 | |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1429 | // Handle UTF-8 strings just like narrow strings. |
| 1430 | if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8') |
| 1431 | SpellingPtr += 2; |
| 1432 | |
| 1433 | assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && |
| 1434 | SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet"); |
| 1435 | |
| 1436 | // For raw string literals, this is easy. |
| 1437 | if (SpellingPtr[0] == 'R') { |
| 1438 | assert(SpellingPtr[1] == '"' && "Should be a raw string literal!"); |
| 1439 | // Skip 'R"'. |
| 1440 | SpellingPtr += 2; |
| 1441 | while (*SpellingPtr != '(') { |
| 1442 | ++SpellingPtr; |
| 1443 | assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal"); |
| 1444 | } |
| 1445 | // Skip '('. |
| 1446 | ++SpellingPtr; |
| 1447 | return SpellingPtr - SpellingStart + ByteNo; |
| 1448 | } |
| 1449 | |
| 1450 | // Skip over the leading quote |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1451 | assert(SpellingPtr[0] == '"' && "Should be a string literal!"); |
| 1452 | ++SpellingPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1454 | // Skip over bytes until we find the offset we're looking for. |
| 1455 | while (ByteNo) { |
| 1456 | assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1458 | // Step over non-escapes simply. |
| 1459 | if (*SpellingPtr != '\\') { |
| 1460 | ++SpellingPtr; |
| 1461 | --ByteNo; |
| 1462 | continue; |
| 1463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1465 | // Otherwise, this is an escape character. Advance over it. |
| 1466 | bool HadError = false; |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1467 | if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') { |
| 1468 | const char *EscapePtr = SpellingPtr; |
| 1469 | unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd, |
| 1470 | 1, Features, HadError); |
| 1471 | if (Len > ByteNo) { |
| 1472 | // ByteNo is somewhere within the escape sequence. |
| 1473 | SpellingPtr = EscapePtr; |
| 1474 | break; |
| 1475 | } |
| 1476 | ByteNo -= Len; |
| 1477 | } else { |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1478 | ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError, |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1479 | FullSourceLoc(Tok.getLocation(), SM), |
Richard Smith | e5f0588 | 2012-09-08 07:16:20 +0000 | [diff] [blame] | 1480 | CharByteWidth*8, Diags, Features); |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame] | 1481 | --ByteNo; |
| 1482 | } |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1483 | assert(!HadError && "This method isn't valid on erroneous strings"); |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 719e615 | 2009-02-18 19:21:10 +0000 | [diff] [blame] | 1486 | return SpellingPtr-SpellingStart; |
| 1487 | } |