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