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