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