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