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