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