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