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