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 | // |
| 5 | // This file was developed by Steve Naroff and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 136f93a | 2007-07-16 06:55:01 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/APInt.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | using namespace clang; |
| 23 | |
| 24 | /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's |
| 25 | /// not valid. |
| 26 | static int HexDigitValue(char C) { |
| 27 | if (C >= '0' && C <= '9') return C-'0'; |
| 28 | if (C >= 'a' && C <= 'f') return C-'a'+10; |
| 29 | if (C >= 'A' && C <= 'F') return C-'A'+10; |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in |
| 34 | /// either a character or a string literal. |
| 35 | static unsigned ProcessCharEscape(const char *&ThisTokBuf, |
| 36 | const char *ThisTokEnd, bool &HadError, |
| 37 | SourceLocation Loc, bool IsWide, |
| 38 | Preprocessor &PP) { |
| 39 | // Skip the '\' char. |
| 40 | ++ThisTokBuf; |
| 41 | |
| 42 | // We know that this character can't be off the end of the buffer, because |
| 43 | // that would have been \", which would not have been the end of string. |
| 44 | unsigned ResultChar = *ThisTokBuf++; |
| 45 | switch (ResultChar) { |
| 46 | // These map to themselves. |
| 47 | case '\\': case '\'': case '"': case '?': break; |
| 48 | |
| 49 | // These have fixed mappings. |
| 50 | case 'a': |
| 51 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 52 | ResultChar = 7; |
| 53 | break; |
| 54 | case 'b': |
| 55 | ResultChar = 8; |
| 56 | break; |
| 57 | case 'e': |
| 58 | PP.Diag(Loc, diag::ext_nonstandard_escape, "e"); |
| 59 | ResultChar = 27; |
| 60 | break; |
| 61 | case 'f': |
| 62 | ResultChar = 12; |
| 63 | break; |
| 64 | case 'n': |
| 65 | ResultChar = 10; |
| 66 | break; |
| 67 | case 'r': |
| 68 | ResultChar = 13; |
| 69 | break; |
| 70 | case 't': |
| 71 | ResultChar = 9; |
| 72 | break; |
| 73 | case 'v': |
| 74 | ResultChar = 11; |
| 75 | break; |
| 76 | |
| 77 | //case 'u': case 'U': // FIXME: UCNs. |
| 78 | case 'x': { // Hex escape. |
| 79 | ResultChar = 0; |
| 80 | if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) { |
| 81 | PP.Diag(Loc, diag::err_hex_escape_no_digits); |
| 82 | HadError = 1; |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | // Hex escapes are a maximal series of hex digits. |
| 87 | bool Overflow = false; |
| 88 | for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { |
| 89 | int CharVal = HexDigitValue(ThisTokBuf[0]); |
| 90 | if (CharVal == -1) break; |
| 91 | Overflow |= ResultChar & 0xF0000000; // About to shift out a digit? |
| 92 | ResultChar <<= 4; |
| 93 | ResultChar |= CharVal; |
| 94 | } |
| 95 | |
| 96 | // See if any bits will be truncated when evaluated as a character. |
| 97 | unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc) |
| 98 | : PP.getTargetInfo().getCharWidth(Loc); |
| 99 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 100 | Overflow = true; |
| 101 | ResultChar &= ~0U >> (32-CharWidth); |
| 102 | } |
| 103 | |
| 104 | // Check for overflow. |
| 105 | if (Overflow) // Too many digits to fit in |
| 106 | PP.Diag(Loc, diag::warn_hex_escape_too_large); |
| 107 | break; |
| 108 | } |
| 109 | case '0': case '1': case '2': case '3': |
| 110 | case '4': case '5': case '6': case '7': { |
| 111 | // Octal escapes. |
| 112 | --ThisTokBuf; |
| 113 | ResultChar = 0; |
| 114 | |
| 115 | // Octal escapes are a series of octal digits with maximum length 3. |
| 116 | // "\0123" is a two digit sequence equal to "\012" "3". |
| 117 | unsigned NumDigits = 0; |
| 118 | do { |
| 119 | ResultChar <<= 3; |
| 120 | ResultChar |= *ThisTokBuf++ - '0'; |
| 121 | ++NumDigits; |
| 122 | } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && |
| 123 | ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); |
| 124 | |
| 125 | // Check for overflow. Reject '\777', but not L'\777'. |
| 126 | unsigned CharWidth = IsWide ? PP.getTargetInfo().getWCharWidth(Loc) |
| 127 | : PP.getTargetInfo().getCharWidth(Loc); |
| 128 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 129 | PP.Diag(Loc, diag::warn_octal_escape_too_large); |
| 130 | ResultChar &= ~0U >> (32-CharWidth); |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | // Otherwise, these are not valid escapes. |
| 136 | case '(': case '{': case '[': case '%': |
| 137 | // GCC accepts these as extensions. We warn about them as such though. |
| 138 | if (!PP.getLangOptions().NoExtensions) { |
| 139 | PP.Diag(Loc, diag::ext_nonstandard_escape, |
| 140 | std::string()+(char)ResultChar); |
| 141 | break; |
| 142 | } |
| 143 | // FALL THROUGH. |
| 144 | default: |
| 145 | if (isgraph(ThisTokBuf[0])) { |
| 146 | PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar); |
| 147 | } else { |
| 148 | PP.Diag(Loc, diag::ext_unknown_escape, "x"+llvm::utohexstr(ResultChar)); |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | return ResultChar; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | /// integer-constant: [C99 6.4.4.1] |
| 160 | /// decimal-constant integer-suffix |
| 161 | /// octal-constant integer-suffix |
| 162 | /// hexadecimal-constant integer-suffix |
| 163 | /// decimal-constant: |
| 164 | /// nonzero-digit |
| 165 | /// decimal-constant digit |
| 166 | /// octal-constant: |
| 167 | /// 0 |
| 168 | /// octal-constant octal-digit |
| 169 | /// hexadecimal-constant: |
| 170 | /// hexadecimal-prefix hexadecimal-digit |
| 171 | /// hexadecimal-constant hexadecimal-digit |
| 172 | /// hexadecimal-prefix: one of |
| 173 | /// 0x 0X |
| 174 | /// integer-suffix: |
| 175 | /// unsigned-suffix [long-suffix] |
| 176 | /// unsigned-suffix [long-long-suffix] |
| 177 | /// long-suffix [unsigned-suffix] |
| 178 | /// long-long-suffix [unsigned-sufix] |
| 179 | /// nonzero-digit: |
| 180 | /// 1 2 3 4 5 6 7 8 9 |
| 181 | /// octal-digit: |
| 182 | /// 0 1 2 3 4 5 6 7 |
| 183 | /// hexadecimal-digit: |
| 184 | /// 0 1 2 3 4 5 6 7 8 9 |
| 185 | /// a b c d e f |
| 186 | /// A B C D E F |
| 187 | /// unsigned-suffix: one of |
| 188 | /// u U |
| 189 | /// long-suffix: one of |
| 190 | /// l L |
| 191 | /// long-long-suffix: one of |
| 192 | /// ll LL |
| 193 | /// |
| 194 | /// floating-constant: [C99 6.4.4.2] |
| 195 | /// TODO: add rules... |
| 196 | /// |
| 197 | |
| 198 | NumericLiteralParser:: |
| 199 | NumericLiteralParser(const char *begin, const char *end, |
| 200 | SourceLocation TokLoc, Preprocessor &pp) |
| 201 | : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) { |
| 202 | s = DigitsBegin = begin; |
| 203 | saw_exponent = false; |
| 204 | saw_period = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | isLong = false; |
| 206 | isUnsigned = false; |
| 207 | isLongLong = false; |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame^] | 208 | isFloat = false; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 209 | isImaginary = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | hadError = false; |
| 211 | |
| 212 | if (*s == '0') { // parse radix |
| 213 | s++; |
| 214 | if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) { |
| 215 | s++; |
| 216 | radix = 16; |
| 217 | DigitsBegin = s; |
| 218 | s = SkipHexDigits(s); |
| 219 | if (s == ThisTokEnd) { |
| 220 | // Done. |
| 221 | } else if (*s == '.') { |
| 222 | s++; |
| 223 | saw_period = true; |
| 224 | s = SkipHexDigits(s); |
| 225 | } |
| 226 | // A binary exponent can appear with or with a '.'. If dotted, the |
| 227 | // binary exponent is required. |
| 228 | if (*s == 'p' || *s == 'P') { |
| 229 | s++; |
| 230 | saw_exponent = true; |
| 231 | if (*s == '+' || *s == '-') s++; // sign |
| 232 | const char *first_non_digit = SkipDigits(s); |
| 233 | if (first_non_digit == s) { |
| 234 | Diag(TokLoc, diag::err_exponent_has_no_digits); |
| 235 | return; |
| 236 | } else { |
| 237 | s = first_non_digit; |
| 238 | } |
| 239 | } else if (saw_period) { |
| 240 | Diag(TokLoc, diag::err_hexconstant_requires_exponent); |
| 241 | return; |
| 242 | } |
| 243 | } else if (*s == 'b' || *s == 'B') { |
| 244 | // 0b101010 is a GCC extension. |
| 245 | ++s; |
| 246 | radix = 2; |
| 247 | DigitsBegin = s; |
| 248 | s = SkipBinaryDigits(s); |
| 249 | if (s == ThisTokEnd) { |
| 250 | // Done. |
| 251 | } else if (isxdigit(*s)) { |
| 252 | Diag(TokLoc, diag::err_invalid_binary_digit, std::string(s, s+1)); |
| 253 | return; |
| 254 | } |
| 255 | PP.Diag(TokLoc, diag::ext_binary_literal); |
| 256 | } else { |
| 257 | // For now, the radix is set to 8. If we discover that we have a |
| 258 | // floating point constant, the radix will change to 10. Octal floating |
| 259 | // point constants are not permitted (only decimal and hexadecimal). |
| 260 | radix = 8; |
| 261 | DigitsBegin = s; |
| 262 | s = SkipOctalDigits(s); |
| 263 | if (s == ThisTokEnd) { |
| 264 | // Done. |
| 265 | } else if (isxdigit(*s)) { |
Chris Lattner | 136f93a | 2007-07-16 06:55:01 +0000 | [diff] [blame] | 266 | TokLoc = PP.AdvanceToTokenCharacter(TokLoc, s-begin); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | Diag(TokLoc, diag::err_invalid_octal_digit, std::string(s, s+1)); |
| 268 | return; |
| 269 | } else if (*s == '.') { |
| 270 | s++; |
| 271 | radix = 10; |
| 272 | saw_period = true; |
| 273 | s = SkipDigits(s); |
| 274 | } |
| 275 | if (*s == 'e' || *s == 'E') { // exponent |
| 276 | s++; |
| 277 | radix = 10; |
| 278 | saw_exponent = true; |
| 279 | if (*s == '+' || *s == '-') s++; // sign |
| 280 | const char *first_non_digit = SkipDigits(s); |
| 281 | if (first_non_digit == s) { |
| 282 | Diag(TokLoc, diag::err_exponent_has_no_digits); |
| 283 | return; |
| 284 | } else { |
| 285 | s = first_non_digit; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } else { // the first digit is non-zero |
| 290 | radix = 10; |
| 291 | s = SkipDigits(s); |
| 292 | if (s == ThisTokEnd) { |
| 293 | // Done. |
| 294 | } else if (isxdigit(*s)) { |
| 295 | Diag(TokLoc, diag::err_invalid_decimal_digit, std::string(s, s+1)); |
| 296 | return; |
| 297 | } else if (*s == '.') { |
| 298 | s++; |
| 299 | saw_period = true; |
| 300 | s = SkipDigits(s); |
| 301 | } |
| 302 | if (*s == 'e' || *s == 'E') { // exponent |
| 303 | s++; |
| 304 | saw_exponent = true; |
| 305 | if (*s == '+' || *s == '-') s++; // sign |
| 306 | const char *first_non_digit = SkipDigits(s); |
| 307 | if (first_non_digit == s) { |
| 308 | Diag(TokLoc, diag::err_exponent_has_no_digits); |
| 309 | return; |
| 310 | } else { |
| 311 | s = first_non_digit; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | SuffixBegin = s; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 317 | |
| 318 | // Parse the suffix. At this point we can classify whether we have an FP or |
| 319 | // integer constant. |
| 320 | bool isFPConstant = isFloatingLiteral(); |
| 321 | |
| 322 | // Loop over all of the characters of the suffix. If we see something bad, |
| 323 | // we break out of the loop. |
| 324 | for (; s != ThisTokEnd; ++s) { |
| 325 | switch (*s) { |
| 326 | case 'f': // FP Suffix for "float" |
| 327 | case 'F': |
| 328 | if (!isFPConstant) break; // Error for integer constant. |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame^] | 329 | if (isFloat || isLong) break; // FF, LF invalid. |
| 330 | isFloat = true; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 331 | continue; // Success. |
| 332 | case 'u': |
| 333 | case 'U': |
| 334 | if (isFPConstant) break; // Error for floating constant. |
| 335 | if (isUnsigned) break; // Cannot be repeated. |
| 336 | isUnsigned = true; |
| 337 | continue; // Success. |
| 338 | case 'l': |
| 339 | case 'L': |
| 340 | if (isLong || isLongLong) break; // Cannot be repeated. |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame^] | 341 | if (isFloat) break; // LF invalid. |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 342 | |
| 343 | // Check for long long. The L's need to be adjacent and the same case. |
| 344 | if (s+1 != ThisTokEnd && s[1] == s[0]) { |
| 345 | if (isFPConstant) break; // long long invalid for floats. |
| 346 | isLongLong = true; |
| 347 | ++s; // Eat both of them. |
| 348 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | isLong = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 351 | continue; // Success. |
| 352 | case 'i': |
| 353 | case 'I': |
| 354 | case 'j': |
| 355 | case 'J': |
| 356 | if (isImaginary) break; // Cannot be repeated. |
| 357 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 358 | diag::ext_imaginary_constant); |
| 359 | isImaginary = true; |
| 360 | continue; // Success. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 362 | // If we reached here, there was an error. |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | // Report an error if there are any. |
| 367 | if (s != ThisTokEnd) { |
| 368 | TokLoc = PP.AdvanceToTokenCharacter(TokLoc, s-begin); |
| 369 | Diag(TokLoc, isFPConstant ? diag::err_invalid_suffix_float_constant : |
| 370 | diag::err_invalid_suffix_integer_constant, |
| 371 | std::string(SuffixBegin, ThisTokEnd)); |
| 372 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | /// GetIntegerValue - Convert this numeric literal value to an APInt that |
| 377 | /// matches Val's input width. If there is an overflow, set Val to the low bits |
| 378 | /// of the result and return true. Otherwise, return false. |
| 379 | bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { |
| 380 | Val = 0; |
| 381 | s = DigitsBegin; |
| 382 | |
| 383 | llvm::APInt RadixVal(Val.getBitWidth(), radix); |
| 384 | llvm::APInt CharVal(Val.getBitWidth(), 0); |
| 385 | llvm::APInt OldVal = Val; |
| 386 | |
| 387 | bool OverflowOccurred = false; |
| 388 | while (s < SuffixBegin) { |
| 389 | unsigned C = HexDigitValue(*s++); |
| 390 | |
| 391 | // If this letter is out of bound for this radix, reject it. |
| 392 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
| 393 | |
| 394 | CharVal = C; |
| 395 | |
| 396 | // Add the digit to the value in the appropriate radix. If adding in digits |
| 397 | // made the value smaller, then this overflowed. |
| 398 | OldVal = Val; |
| 399 | |
| 400 | // Multiply by radix, did overflow occur on the multiply? |
| 401 | Val *= RadixVal; |
| 402 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
| 403 | |
| 404 | OldVal = Val; |
| 405 | // Add value, did overflow occur on the value? |
| 406 | Val += CharVal; |
| 407 | OverflowOccurred |= Val.ult(OldVal); |
| 408 | OverflowOccurred |= Val.ult(CharVal); |
| 409 | } |
| 410 | return OverflowOccurred; |
| 411 | } |
| 412 | |
| 413 | // GetFloatValue - Poor man's floatvalue (FIXME). |
| 414 | float NumericLiteralParser::GetFloatValue() { |
| 415 | char floatChars[256]; |
| 416 | strncpy(floatChars, ThisTokBegin, ThisTokEnd-ThisTokBegin); |
| 417 | floatChars[ThisTokEnd-ThisTokBegin] = '\0'; |
Chris Lattner | 67798cb | 2007-07-17 15:27:33 +0000 | [diff] [blame] | 418 | return (float)strtod(floatChars, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID, |
| 422 | const std::string &M) { |
| 423 | PP.Diag(Loc, DiagID, M); |
| 424 | hadError = true; |
| 425 | } |
| 426 | |
| 427 | |
| 428 | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
| 429 | SourceLocation Loc, Preprocessor &PP) { |
| 430 | // At this point we know that the character matches the regex "L?'.*'". |
| 431 | HadError = false; |
| 432 | Value = 0; |
| 433 | |
| 434 | // Determine if this is a wide character. |
| 435 | IsWide = begin[0] == 'L'; |
| 436 | if (IsWide) ++begin; |
| 437 | |
| 438 | // Skip over the entry quote. |
| 439 | assert(begin[0] == '\'' && "Invalid token lexed"); |
| 440 | ++begin; |
| 441 | |
| 442 | // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the |
| 443 | // size of "value". |
| 444 | assert(PP.getTargetInfo().getIntWidth(Loc) == 32 && |
| 445 | "Assumes sizeof(int) == 4 for now"); |
| 446 | // FIXME: This assumes that wchar_t is 32-bits for now. |
| 447 | assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 && |
| 448 | "Assumes sizeof(wchar_t) == 4 for now"); |
| 449 | // FIXME: This extensively assumes that 'char' is 8-bits. |
| 450 | assert(PP.getTargetInfo().getCharWidth(Loc) == 8 && |
| 451 | "Assumes char is 8 bits"); |
| 452 | |
| 453 | bool isFirstChar = true; |
| 454 | bool isMultiChar = false; |
| 455 | while (begin[0] != '\'') { |
| 456 | unsigned ResultChar; |
| 457 | if (begin[0] != '\\') // If this is a normal character, consume it. |
| 458 | ResultChar = *begin++; |
| 459 | else // Otherwise, this is an escape character. |
| 460 | ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP); |
| 461 | |
| 462 | // If this is a multi-character constant (e.g. 'abc'), handle it. These are |
| 463 | // implementation defined (C99 6.4.4.4p10). |
| 464 | if (!isFirstChar) { |
| 465 | // If this is the second character being processed, do special handling. |
| 466 | if (!isMultiChar) { |
| 467 | isMultiChar = true; |
| 468 | |
| 469 | // Warn about discarding the top bits for multi-char wide-character |
| 470 | // constants (L'abcd'). |
| 471 | if (IsWide) |
| 472 | PP.Diag(Loc, diag::warn_extraneous_wide_char_constant); |
| 473 | } |
| 474 | |
| 475 | if (IsWide) { |
| 476 | // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'. |
| 477 | Value = 0; |
| 478 | } else { |
| 479 | // Narrow character literals act as though their value is concatenated |
| 480 | // in this implementation. |
| 481 | if (((Value << 8) >> 8) != Value) |
| 482 | PP.Diag(Loc, diag::warn_char_constant_too_large); |
| 483 | Value <<= 8; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | Value += ResultChar; |
| 488 | isFirstChar = false; |
| 489 | } |
| 490 | |
| 491 | // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") |
| 492 | // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple |
| 493 | // character constants are not sign extended in the this implementation: |
| 494 | // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. |
| 495 | if (!IsWide && !isMultiChar && (Value & 128) && |
| 496 | PP.getTargetInfo().isCharSigned(Loc)) |
| 497 | Value = (signed char)Value; |
| 498 | } |
| 499 | |
| 500 | |
| 501 | /// string-literal: [C99 6.4.5] |
| 502 | /// " [s-char-sequence] " |
| 503 | /// L" [s-char-sequence] " |
| 504 | /// s-char-sequence: |
| 505 | /// s-char |
| 506 | /// s-char-sequence s-char |
| 507 | /// s-char: |
| 508 | /// any source character except the double quote ", |
| 509 | /// backslash \, or newline character |
| 510 | /// escape-character |
| 511 | /// universal-character-name |
| 512 | /// escape-character: [C99 6.4.4.4] |
| 513 | /// \ escape-code |
| 514 | /// universal-character-name |
| 515 | /// escape-code: |
| 516 | /// character-escape-code |
| 517 | /// octal-escape-code |
| 518 | /// hex-escape-code |
| 519 | /// character-escape-code: one of |
| 520 | /// n t b r f v a |
| 521 | /// \ ' " ? |
| 522 | /// octal-escape-code: |
| 523 | /// octal-digit |
| 524 | /// octal-digit octal-digit |
| 525 | /// octal-digit octal-digit octal-digit |
| 526 | /// hex-escape-code: |
| 527 | /// x hex-digit |
| 528 | /// hex-escape-code hex-digit |
| 529 | /// universal-character-name: |
| 530 | /// \u hex-quad |
| 531 | /// \U hex-quad hex-quad |
| 532 | /// hex-quad: |
| 533 | /// hex-digit hex-digit hex-digit hex-digit |
| 534 | /// |
| 535 | StringLiteralParser:: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 536 | StringLiteralParser(const Token *StringToks, unsigned NumStringToks, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 537 | Preprocessor &pp, TargetInfo &t) |
| 538 | : PP(pp), Target(t) { |
| 539 | // Scan all of the string portions, remember the max individual token length, |
| 540 | // computing a bound on the concatenated string length, and see whether any |
| 541 | // piece is a wide-string. If any of the string portions is a wide-string |
| 542 | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
| 543 | MaxTokenLength = StringToks[0].getLength(); |
| 544 | SizeBound = StringToks[0].getLength()-2; // -2 for "". |
| 545 | AnyWide = StringToks[0].getKind() == tok::wide_string_literal; |
| 546 | |
| 547 | hadError = false; |
| 548 | |
| 549 | // Implement Translation Phase #6: concatenation of string literals |
| 550 | /// (C99 5.1.1.2p1). The common case is only one string fragment. |
| 551 | for (unsigned i = 1; i != NumStringToks; ++i) { |
| 552 | // The string could be shorter than this if it needs cleaning, but this is a |
| 553 | // reasonable bound, which is all we need. |
| 554 | SizeBound += StringToks[i].getLength()-2; // -2 for "". |
| 555 | |
| 556 | // Remember maximum string piece length. |
| 557 | if (StringToks[i].getLength() > MaxTokenLength) |
| 558 | MaxTokenLength = StringToks[i].getLength(); |
| 559 | |
| 560 | // Remember if we see any wide strings. |
| 561 | AnyWide |= StringToks[i].getKind() == tok::wide_string_literal; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | // Include space for the null terminator. |
| 566 | ++SizeBound; |
| 567 | |
| 568 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
| 569 | |
| 570 | // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not |
| 571 | // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true. |
| 572 | wchar_tByteWidth = ~0U; |
| 573 | if (AnyWide) { |
| 574 | wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation()); |
| 575 | assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!"); |
| 576 | wchar_tByteWidth /= 8; |
| 577 | } |
| 578 | |
| 579 | // The output buffer size needs to be large enough to hold wide characters. |
| 580 | // This is a worst-case assumption which basically corresponds to L"" "long". |
| 581 | if (AnyWide) |
| 582 | SizeBound *= wchar_tByteWidth; |
| 583 | |
| 584 | // Size the temporary buffer to hold the result string data. |
| 585 | ResultBuf.resize(SizeBound); |
| 586 | |
| 587 | // Likewise, but for each string piece. |
| 588 | llvm::SmallString<512> TokenBuf; |
| 589 | TokenBuf.resize(MaxTokenLength); |
| 590 | |
| 591 | // Loop over all the strings, getting their spelling, and expanding them to |
| 592 | // wide strings as appropriate. |
| 593 | ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
| 594 | |
| 595 | for (unsigned i = 0, e = NumStringToks; i != e; ++i) { |
| 596 | const char *ThisTokBuf = &TokenBuf[0]; |
| 597 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 598 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 599 | // and 'spelled' tokens can only shrink. |
| 600 | unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf); |
| 601 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote. |
| 602 | |
| 603 | // TODO: Input character set mapping support. |
| 604 | |
| 605 | // Skip L marker for wide strings. |
| 606 | bool ThisIsWide = false; |
| 607 | if (ThisTokBuf[0] == 'L') { |
| 608 | ++ThisTokBuf; |
| 609 | ThisIsWide = true; |
| 610 | } |
| 611 | |
| 612 | assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?"); |
| 613 | ++ThisTokBuf; |
| 614 | |
| 615 | while (ThisTokBuf != ThisTokEnd) { |
| 616 | // Is this a span of non-escape characters? |
| 617 | if (ThisTokBuf[0] != '\\') { |
| 618 | const char *InStart = ThisTokBuf; |
| 619 | do { |
| 620 | ++ThisTokBuf; |
| 621 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 622 | |
| 623 | // Copy the character span over. |
| 624 | unsigned Len = ThisTokBuf-InStart; |
| 625 | if (!AnyWide) { |
| 626 | memcpy(ResultPtr, InStart, Len); |
| 627 | ResultPtr += Len; |
| 628 | } else { |
| 629 | // Note: our internal rep of wide char tokens is always little-endian. |
| 630 | for (; Len; --Len, ++InStart) { |
| 631 | *ResultPtr++ = InStart[0]; |
| 632 | // Add zeros at the end. |
| 633 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 634 | *ResultPtr++ = 0; |
| 635 | } |
| 636 | } |
| 637 | continue; |
| 638 | } |
| 639 | |
| 640 | // Otherwise, this is an escape character. Process it. |
| 641 | unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError, |
| 642 | StringToks[i].getLocation(), |
| 643 | ThisIsWide, PP); |
| 644 | |
| 645 | // Note: our internal rep of wide char tokens is always little-endian. |
| 646 | *ResultPtr++ = ResultChar & 0xFF; |
| 647 | |
| 648 | if (AnyWide) { |
| 649 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 650 | *ResultPtr++ = ResultChar >> i*8; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // Add zero terminator. |
| 656 | *ResultPtr = 0; |
| 657 | if (AnyWide) { |
| 658 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 659 | *ResultPtr++ = 0; |
| 660 | } |
| 661 | } |