1 //===--- LiteralSupport.cpp - Code to parse and process literals ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the NumericLiteralParser, CharLiteralParser, and 10 // StringLiteralParser interfaces. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Lex/LiteralSupport.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/LangOptions.h" 17 #include "clang/Basic/SourceLocation.h" 18 #include "clang/Basic/TargetInfo.h" 19 #include "clang/Lex/LexDiagnostic.h" 20 #include "clang/Lex/Lexer.h" 21 #include "clang/Lex/Preprocessor.h" 22 #include "clang/Lex/Token.h" 23 #include "llvm/ADT/APInt.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/ADT/StringExtras.h" 26 #include "llvm/ADT/StringSwitch.h" 27 #include "llvm/Support/ConvertUTF.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include <algorithm> 30 #include <cassert> 31 #include <cstddef> 32 #include <cstdint> 33 #include <cstring> 34 #include <string> 35 36 using namespace clang; 37 38 static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) { 39 switch (kind) { 40 default: llvm_unreachable("Unknown token type!"); 41 case tok::char_constant: 42 case tok::string_literal: 43 case tok::utf8_char_constant: 44 case tok::utf8_string_literal: 45 return Target.getCharWidth(); 46 case tok::wide_char_constant: 47 case tok::wide_string_literal: 48 return Target.getWCharWidth(); 49 case tok::utf16_char_constant: 50 case tok::utf16_string_literal: 51 return Target.getChar16Width(); 52 case tok::utf32_char_constant: 53 case tok::utf32_string_literal: 54 return Target.getChar32Width(); 55 } 56 } 57 58 static CharSourceRange MakeCharSourceRange(const LangOptions &Features, 59 FullSourceLoc TokLoc, 60 const char *TokBegin, 61 const char *TokRangeBegin, 62 const char *TokRangeEnd) { 63 SourceLocation Begin = 64 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, 65 TokLoc.getManager(), Features); 66 SourceLocation End = 67 Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin, 68 TokLoc.getManager(), Features); 69 return CharSourceRange::getCharRange(Begin, End); 70 } 71 72 /// Produce a diagnostic highlighting some portion of a literal. 73 /// 74 /// Emits the diagnostic \p DiagID, highlighting the range of characters from 75 /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be 76 /// a substring of a spelling buffer for the token beginning at \p TokBegin. 77 static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, 78 const LangOptions &Features, FullSourceLoc TokLoc, 79 const char *TokBegin, const char *TokRangeBegin, 80 const char *TokRangeEnd, unsigned DiagID) { 81 SourceLocation Begin = 82 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, 83 TokLoc.getManager(), Features); 84 return Diags->Report(Begin, DiagID) << 85 MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd); 86 } 87 88 /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in 89 /// either a character or a string literal. 90 static unsigned ProcessCharEscape(const char *ThisTokBegin, 91 const char *&ThisTokBuf, 92 const char *ThisTokEnd, bool &HadError, 93 FullSourceLoc Loc, unsigned CharWidth, 94 DiagnosticsEngine *Diags, 95 const LangOptions &Features) { 96 const char *EscapeBegin = ThisTokBuf; 97 98 // Skip the '\' char. 99 ++ThisTokBuf; 100 101 // We know that this character can't be off the end of the buffer, because 102 // that would have been \", which would not have been the end of string. 103 unsigned ResultChar = *ThisTokBuf++; 104 switch (ResultChar) { 105 // These map to themselves. 106 case '\\': case '\'': case '"': case '?': break; 107 108 // These have fixed mappings. 109 case 'a': 110 // TODO: K&R: the meaning of '\\a' is different in traditional C 111 ResultChar = 7; 112 break; 113 case 'b': 114 ResultChar = 8; 115 break; 116 case 'e': 117 if (Diags) 118 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 119 diag::ext_nonstandard_escape) << "e"; 120 ResultChar = 27; 121 break; 122 case 'E': 123 if (Diags) 124 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 125 diag::ext_nonstandard_escape) << "E"; 126 ResultChar = 27; 127 break; 128 case 'f': 129 ResultChar = 12; 130 break; 131 case 'n': 132 ResultChar = 10; 133 break; 134 case 'r': 135 ResultChar = 13; 136 break; 137 case 't': 138 ResultChar = 9; 139 break; 140 case 'v': 141 ResultChar = 11; 142 break; 143 case 'x': { // Hex escape. 144 ResultChar = 0; 145 if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { 146 if (Diags) 147 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 148 diag::err_hex_escape_no_digits) << "x"; 149 HadError = true; 150 break; 151 } 152 153 // Hex escapes are a maximal series of hex digits. 154 bool Overflow = false; 155 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { 156 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]); 157 if (CharVal == -1) break; 158 // About to shift out a digit? 159 if (ResultChar & 0xF0000000) 160 Overflow = true; 161 ResultChar <<= 4; 162 ResultChar |= CharVal; 163 } 164 165 // See if any bits will be truncated when evaluated as a character. 166 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { 167 Overflow = true; 168 ResultChar &= ~0U >> (32-CharWidth); 169 } 170 171 // Check for overflow. 172 if (Overflow && Diags) // Too many digits to fit in 173 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 174 diag::err_escape_too_large) << 0; 175 break; 176 } 177 case '0': case '1': case '2': case '3': 178 case '4': case '5': case '6': case '7': { 179 // Octal escapes. 180 --ThisTokBuf; 181 ResultChar = 0; 182 183 // Octal escapes are a series of octal digits with maximum length 3. 184 // "\0123" is a two digit sequence equal to "\012" "3". 185 unsigned NumDigits = 0; 186 do { 187 ResultChar <<= 3; 188 ResultChar |= *ThisTokBuf++ - '0'; 189 ++NumDigits; 190 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && 191 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); 192 193 // Check for overflow. Reject '\777', but not L'\777'. 194 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { 195 if (Diags) 196 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 197 diag::err_escape_too_large) << 1; 198 ResultChar &= ~0U >> (32-CharWidth); 199 } 200 break; 201 } 202 203 // Otherwise, these are not valid escapes. 204 case '(': case '{': case '[': case '%': 205 // GCC accepts these as extensions. We warn about them as such though. 206 if (Diags) 207 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 208 diag::ext_nonstandard_escape) 209 << std::string(1, ResultChar); 210 break; 211 default: 212 if (!Diags) 213 break; 214 215 if (isPrintable(ResultChar)) 216 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 217 diag::ext_unknown_escape) 218 << std::string(1, ResultChar); 219 else 220 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, 221 diag::ext_unknown_escape) 222 << "x" + llvm::utohexstr(ResultChar); 223 break; 224 } 225 226 return ResultChar; 227 } 228 229 static void appendCodePoint(unsigned Codepoint, 230 llvm::SmallVectorImpl<char> &Str) { 231 char ResultBuf[4]; 232 char *ResultPtr = ResultBuf; 233 bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr); 234 (void)Res; 235 assert(Res && "Unexpected conversion failure"); 236 Str.append(ResultBuf, ResultPtr); 237 } 238 239 void clang::expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) { 240 for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) { 241 if (*I != '\\') { 242 Buf.push_back(*I); 243 continue; 244 } 245 246 ++I; 247 assert(*I == 'u' || *I == 'U'); 248 249 unsigned NumHexDigits; 250 if (*I == 'u') 251 NumHexDigits = 4; 252 else 253 NumHexDigits = 8; 254 255 assert(I + NumHexDigits <= E); 256 257 uint32_t CodePoint = 0; 258 for (++I; NumHexDigits != 0; ++I, --NumHexDigits) { 259 unsigned Value = llvm::hexDigitValue(*I); 260 assert(Value != -1U); 261 262 CodePoint <<= 4; 263 CodePoint += Value; 264 } 265 266 appendCodePoint(CodePoint, Buf); 267 --I; 268 } 269 } 270 271 /// ProcessUCNEscape - Read the Universal Character Name, check constraints and 272 /// return the UTF32. 273 static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, 274 const char *ThisTokEnd, 275 uint32_t &UcnVal, unsigned short &UcnLen, 276 FullSourceLoc Loc, DiagnosticsEngine *Diags, 277 const LangOptions &Features, 278 bool in_char_string_literal = false) { 279 const char *UcnBegin = ThisTokBuf; 280 281 // Skip the '\u' char's. 282 ThisTokBuf += 2; 283 284 if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { 285 if (Diags) 286 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, 287 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1); 288 return false; 289 } 290 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8); 291 unsigned short UcnLenSave = UcnLen; 292 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) { 293 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]); 294 if (CharVal == -1) break; 295 UcnVal <<= 4; 296 UcnVal |= CharVal; 297 } 298 // If we didn't consume the proper number of digits, there is a problem. 299 if (UcnLenSave) { 300 if (Diags) 301 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, 302 diag::err_ucn_escape_incomplete); 303 return false; 304 } 305 306 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2] 307 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints 308 UcnVal > 0x10FFFF) { // maximum legal UTF32 value 309 if (Diags) 310 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, 311 diag::err_ucn_escape_invalid); 312 return false; 313 } 314 315 // C++11 allows UCNs that refer to control characters and basic source 316 // characters inside character and string literals 317 if (UcnVal < 0xa0 && 318 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, ` 319 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal); 320 if (Diags) { 321 char BasicSCSChar = UcnVal; 322 if (UcnVal >= 0x20 && UcnVal < 0x7f) 323 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, 324 IsError ? diag::err_ucn_escape_basic_scs : 325 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs) 326 << StringRef(&BasicSCSChar, 1); 327 else 328 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, 329 IsError ? diag::err_ucn_control_character : 330 diag::warn_cxx98_compat_literal_ucn_control_character); 331 } 332 if (IsError) 333 return false; 334 } 335 336 if (!Features.CPlusPlus && !Features.C99 && Diags) 337 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, 338 diag::warn_ucn_not_valid_in_c89_literal); 339 340 return true; 341 } 342 343 /// MeasureUCNEscape - Determine the number of bytes within the resulting string 344 /// which this UCN will occupy. 345 static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, 346 const char *ThisTokEnd, unsigned CharByteWidth, 347 const LangOptions &Features, bool &HadError) { 348 // UTF-32: 4 bytes per escape. 349 if (CharByteWidth == 4) 350 return 4; 351 352 uint32_t UcnVal = 0; 353 unsigned short UcnLen = 0; 354 FullSourceLoc Loc; 355 356 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, 357 UcnLen, Loc, nullptr, Features, true)) { 358 HadError = true; 359 return 0; 360 } 361 362 // UTF-16: 2 bytes for BMP, 4 bytes otherwise. 363 if (CharByteWidth == 2) 364 return UcnVal <= 0xFFFF ? 2 : 4; 365 366 // UTF-8. 367 if (UcnVal < 0x80) 368 return 1; 369 if (UcnVal < 0x800) 370 return 2; 371 if (UcnVal < 0x10000) 372 return 3; 373 return 4; 374 } 375 376 /// EncodeUCNEscape - Read the Universal Character Name, check constraints and 377 /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of 378 /// StringLiteralParser. When we decide to implement UCN's for identifiers, 379 /// we will likely rework our support for UCN's. 380 static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, 381 const char *ThisTokEnd, 382 char *&ResultBuf, bool &HadError, 383 FullSourceLoc Loc, unsigned CharByteWidth, 384 DiagnosticsEngine *Diags, 385 const LangOptions &Features) { 386 typedef uint32_t UTF32; 387 UTF32 UcnVal = 0; 388 unsigned short UcnLen = 0; 389 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, 390 Loc, Diags, Features, true)) { 391 HadError = true; 392 return; 393 } 394 395 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && 396 "only character widths of 1, 2, or 4 bytes supported"); 397 398 (void)UcnLen; 399 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported"); 400 401 if (CharByteWidth == 4) { 402 // FIXME: Make the type of the result buffer correct instead of 403 // using reinterpret_cast. 404 llvm::UTF32 *ResultPtr = reinterpret_cast<llvm::UTF32*>(ResultBuf); 405 *ResultPtr = UcnVal; 406 ResultBuf += 4; 407 return; 408 } 409 410 if (CharByteWidth == 2) { 411 // FIXME: Make the type of the result buffer correct instead of 412 // using reinterpret_cast. 413 llvm::UTF16 *ResultPtr = reinterpret_cast<llvm::UTF16*>(ResultBuf); 414 415 if (UcnVal <= (UTF32)0xFFFF) { 416 *ResultPtr = UcnVal; 417 ResultBuf += 2; 418 return; 419 } 420 421 // Convert to UTF16. 422 UcnVal -= 0x10000; 423 *ResultPtr = 0xD800 + (UcnVal >> 10); 424 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF); 425 ResultBuf += 4; 426 return; 427 } 428 429 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters"); 430 431 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8. 432 // The conversion below was inspired by: 433 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c 434 // First, we determine how many bytes the result will require. 435 typedef uint8_t UTF8; 436 437 unsigned short bytesToWrite = 0; 438 if (UcnVal < (UTF32)0x80) 439 bytesToWrite = 1; 440 else if (UcnVal < (UTF32)0x800) 441 bytesToWrite = 2; 442 else if (UcnVal < (UTF32)0x10000) 443 bytesToWrite = 3; 444 else 445 bytesToWrite = 4; 446 447 const unsigned byteMask = 0xBF; 448 const unsigned byteMark = 0x80; 449 450 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed 451 // into the first byte, depending on how many bytes follow. 452 static const UTF8 firstByteMark[5] = { 453 0x00, 0x00, 0xC0, 0xE0, 0xF0 454 }; 455 // Finally, we write the bytes into ResultBuf. 456 ResultBuf += bytesToWrite; 457 switch (bytesToWrite) { // note: everything falls through. 458 case 4: 459 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; 460 LLVM_FALLTHROUGH; 461 case 3: 462 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; 463 LLVM_FALLTHROUGH; 464 case 2: 465 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; 466 LLVM_FALLTHROUGH; 467 case 1: 468 *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]); 469 } 470 // Update the buffer. 471 ResultBuf += bytesToWrite; 472 } 473 474 /// integer-constant: [C99 6.4.4.1] 475 /// decimal-constant integer-suffix 476 /// octal-constant integer-suffix 477 /// hexadecimal-constant integer-suffix 478 /// binary-literal integer-suffix [GNU, C++1y] 479 /// user-defined-integer-literal: [C++11 lex.ext] 480 /// decimal-literal ud-suffix 481 /// octal-literal ud-suffix 482 /// hexadecimal-literal ud-suffix 483 /// binary-literal ud-suffix [GNU, C++1y] 484 /// decimal-constant: 485 /// nonzero-digit 486 /// decimal-constant digit 487 /// octal-constant: 488 /// 0 489 /// octal-constant octal-digit 490 /// hexadecimal-constant: 491 /// hexadecimal-prefix hexadecimal-digit 492 /// hexadecimal-constant hexadecimal-digit 493 /// hexadecimal-prefix: one of 494 /// 0x 0X 495 /// binary-literal: 496 /// 0b binary-digit 497 /// 0B binary-digit 498 /// binary-literal binary-digit 499 /// integer-suffix: 500 /// unsigned-suffix [long-suffix] 501 /// unsigned-suffix [long-long-suffix] 502 /// long-suffix [unsigned-suffix] 503 /// long-long-suffix [unsigned-sufix] 504 /// nonzero-digit: 505 /// 1 2 3 4 5 6 7 8 9 506 /// octal-digit: 507 /// 0 1 2 3 4 5 6 7 508 /// hexadecimal-digit: 509 /// 0 1 2 3 4 5 6 7 8 9 510 /// a b c d e f 511 /// A B C D E F 512 /// binary-digit: 513 /// 0 514 /// 1 515 /// unsigned-suffix: one of 516 /// u U 517 /// long-suffix: one of 518 /// l L 519 /// long-long-suffix: one of 520 /// ll LL 521 /// 522 /// floating-constant: [C99 6.4.4.2] 523 /// TODO: add rules... 524 /// 525 NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, 526 SourceLocation TokLoc, 527 Preprocessor &PP) 528 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) { 529 530 // This routine assumes that the range begin/end matches the regex for integer 531 // and FP constants (specifically, the 'pp-number' regex), and assumes that 532 // the byte at "*end" is both valid and not part of the regex. Because of 533 // this, it doesn't have to check for 'overscan' in various places. 534 assert(!isPreprocessingNumberBody(*ThisTokEnd) && "didn't maximally munch?"); 535 536 s = DigitsBegin = ThisTokBegin; 537 saw_exponent = false; 538 saw_period = false; 539 saw_ud_suffix = false; 540 saw_fixed_point_suffix = false; 541 isLong = false; 542 isUnsigned = false; 543 isLongLong = false; 544 isHalf = false; 545 isFloat = false; 546 isImaginary = false; 547 isFloat16 = false; 548 isFloat128 = false; 549 MicrosoftInteger = 0; 550 isFract = false; 551 isAccum = false; 552 hadError = false; 553 554 if (*s == '0') { // parse radix 555 ParseNumberStartingWithZero(TokLoc); 556 if (hadError) 557 return; 558 } else { // the first digit is non-zero 559 radix = 10; 560 s = SkipDigits(s); 561 if (s == ThisTokEnd) { 562 // Done. 563 } else { 564 ParseDecimalOrOctalCommon(TokLoc); 565 if (hadError) 566 return; 567 } 568 } 569 570 SuffixBegin = s; 571 checkSeparator(TokLoc, s, CSK_AfterDigits); 572 573 // Initial scan to lookahead for fixed point suffix. 574 if (PP.getLangOpts().FixedPoint) { 575 for (const char *c = s; c != ThisTokEnd; ++c) { 576 if (*c == 'r' || *c == 'k' || *c == 'R' || *c == 'K') { 577 saw_fixed_point_suffix = true; 578 break; 579 } 580 } 581 } 582 583 // Parse the suffix. At this point we can classify whether we have an FP or 584 // integer constant. 585 bool isFPConstant = isFloatingLiteral(); 586 587 // Loop over all of the characters of the suffix. If we see something bad, 588 // we break out of the loop. 589 for (; s != ThisTokEnd; ++s) { 590 switch (*s) { 591 case 'R': 592 case 'r': 593 if (!PP.getLangOpts().FixedPoint) break; 594 if (isFract || isAccum) break; 595 if (!(saw_period || saw_exponent)) break; 596 isFract = true; 597 continue; 598 case 'K': 599 case 'k': 600 if (!PP.getLangOpts().FixedPoint) break; 601 if (isFract || isAccum) break; 602 if (!(saw_period || saw_exponent)) break; 603 isAccum = true; 604 continue; 605 case 'h': // FP Suffix for "half". 606 case 'H': 607 // OpenCL Extension v1.2 s9.5 - h or H suffix for half type. 608 if (!(PP.getLangOpts().Half || PP.getLangOpts().FixedPoint)) break; 609 if (isIntegerLiteral()) break; // Error for integer constant. 610 if (isHalf || isFloat || isLong) break; // HH, FH, LH invalid. 611 isHalf = true; 612 continue; // Success. 613 case 'f': // FP Suffix for "float" 614 case 'F': 615 if (!isFPConstant) break; // Error for integer constant. 616 if (isHalf || isFloat || isLong || isFloat128) 617 break; // HF, FF, LF, QF invalid. 618 619 // CUDA host and device may have different _Float16 support, therefore 620 // allows f16 literals to avoid false alarm. 621 // ToDo: more precise check for CUDA. 622 if ((PP.getTargetInfo().hasFloat16Type() || PP.getLangOpts().CUDA) && 623 s + 2 < ThisTokEnd && s[1] == '1' && s[2] == '6') { 624 s += 2; // success, eat up 2 characters. 625 isFloat16 = true; 626 continue; 627 } 628 629 isFloat = true; 630 continue; // Success. 631 case 'q': // FP Suffix for "__float128" 632 case 'Q': 633 if (!isFPConstant) break; // Error for integer constant. 634 if (isHalf || isFloat || isLong || isFloat128) 635 break; // HQ, FQ, LQ, QQ invalid. 636 isFloat128 = true; 637 continue; // Success. 638 case 'u': 639 case 'U': 640 if (isFPConstant) break; // Error for floating constant. 641 if (isUnsigned) break; // Cannot be repeated. 642 isUnsigned = true; 643 continue; // Success. 644 case 'l': 645 case 'L': 646 if (isLong || isLongLong) break; // Cannot be repeated. 647 if (isHalf || isFloat || isFloat128) break; // LH, LF, LQ invalid. 648 649 // Check for long long. The L's need to be adjacent and the same case. 650 if (s[1] == s[0]) { 651 assert(s + 1 < ThisTokEnd && "didn't maximally munch?"); 652 if (isFPConstant) break; // long long invalid for floats. 653 isLongLong = true; 654 ++s; // Eat both of them. 655 } else { 656 isLong = true; 657 } 658 continue; // Success. 659 case 'i': 660 case 'I': 661 if (PP.getLangOpts().MicrosoftExt) { 662 if (isLong || isLongLong || MicrosoftInteger) 663 break; 664 665 if (!isFPConstant) { 666 // Allow i8, i16, i32, and i64. 667 switch (s[1]) { 668 case '8': 669 s += 2; // i8 suffix 670 MicrosoftInteger = 8; 671 break; 672 case '1': 673 if (s[2] == '6') { 674 s += 3; // i16 suffix 675 MicrosoftInteger = 16; 676 } 677 break; 678 case '3': 679 if (s[2] == '2') { 680 s += 3; // i32 suffix 681 MicrosoftInteger = 32; 682 } 683 break; 684 case '6': 685 if (s[2] == '4') { 686 s += 3; // i64 suffix 687 MicrosoftInteger = 64; 688 } 689 break; 690 default: 691 break; 692 } 693 } 694 if (MicrosoftInteger) { 695 assert(s <= ThisTokEnd && "didn't maximally munch?"); 696 break; 697 } 698 } 699 LLVM_FALLTHROUGH; 700 case 'j': 701 case 'J': 702 if (isImaginary) break; // Cannot be repeated. 703 isImaginary = true; 704 continue; // Success. 705 } 706 // If we reached here, there was an error or a ud-suffix. 707 break; 708 } 709 710 // "i", "if", and "il" are user-defined suffixes in C++1y. 711 if (s != ThisTokEnd || isImaginary) { 712 // FIXME: Don't bother expanding UCNs if !tok.hasUCN(). 713 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin)); 714 if (isValidUDSuffix(PP.getLangOpts(), UDSuffixBuf)) { 715 if (!isImaginary) { 716 // Any suffix pieces we might have parsed are actually part of the 717 // ud-suffix. 718 isLong = false; 719 isUnsigned = false; 720 isLongLong = false; 721 isFloat = false; 722 isFloat16 = false; 723 isHalf = false; 724 isImaginary = false; 725 MicrosoftInteger = 0; 726 saw_fixed_point_suffix = false; 727 isFract = false; 728 isAccum = false; 729 } 730 731 saw_ud_suffix = true; 732 return; 733 } 734 735 if (s != ThisTokEnd) { 736 // Report an error if there are any. 737 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin), 738 diag::err_invalid_suffix_constant) 739 << StringRef(SuffixBegin, ThisTokEnd - SuffixBegin) << isFPConstant; 740 hadError = true; 741 } 742 } 743 744 if (!hadError && saw_fixed_point_suffix) { 745 assert(isFract || isAccum); 746 } 747 } 748 749 /// ParseDecimalOrOctalCommon - This method is called for decimal or octal 750 /// numbers. It issues an error for illegal digits, and handles floating point 751 /// parsing. If it detects a floating point number, the radix is set to 10. 752 void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc){ 753 assert((radix == 8 || radix == 10) && "Unexpected radix"); 754 755 // If we have a hex digit other than 'e' (which denotes a FP exponent) then 756 // the code is using an incorrect base. 757 if (isHexDigit(*s) && *s != 'e' && *s != 'E' && 758 !isValidUDSuffix(PP.getLangOpts(), StringRef(s, ThisTokEnd - s))) { 759 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), 760 diag::err_invalid_digit) << StringRef(s, 1) << (radix == 8 ? 1 : 0); 761 hadError = true; 762 return; 763 } 764 765 if (*s == '.') { 766 checkSeparator(TokLoc, s, CSK_AfterDigits); 767 s++; 768 radix = 10; 769 saw_period = true; 770 checkSeparator(TokLoc, s, CSK_BeforeDigits); 771 s = SkipDigits(s); // Skip suffix. 772 } 773 if (*s == 'e' || *s == 'E') { // exponent 774 checkSeparator(TokLoc, s, CSK_AfterDigits); 775 const char *Exponent = s; 776 s++; 777 radix = 10; 778 saw_exponent = true; 779 if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; // sign 780 const char *first_non_digit = SkipDigits(s); 781 if (containsDigits(s, first_non_digit)) { 782 checkSeparator(TokLoc, s, CSK_BeforeDigits); 783 s = first_non_digit; 784 } else { 785 if (!hadError) { 786 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), 787 diag::err_exponent_has_no_digits); 788 hadError = true; 789 } 790 return; 791 } 792 } 793 } 794 795 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved 796 /// suffixes as ud-suffixes, because the diagnostic experience is better if we 797 /// treat it as an invalid suffix. 798 bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, 799 StringRef Suffix) { 800 if (!LangOpts.CPlusPlus11 || Suffix.empty()) 801 return false; 802 803 // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid. 804 if (Suffix[0] == '_') 805 return true; 806 807 // In C++11, there are no library suffixes. 808 if (!LangOpts.CPlusPlus14) 809 return false; 810 811 // In C++14, "s", "h", "min", "ms", "us", and "ns" are used in the library. 812 // Per tweaked N3660, "il", "i", and "if" are also used in the library. 813 // In C++2a "d" and "y" are used in the library. 814 return llvm::StringSwitch<bool>(Suffix) 815 .Cases("h", "min", "s", true) 816 .Cases("ms", "us", "ns", true) 817 .Cases("il", "i", "if", true) 818 .Cases("d", "y", LangOpts.CPlusPlus2a) 819 .Default(false); 820 } 821 822 void NumericLiteralParser::checkSeparator(SourceLocation TokLoc, 823 const char *Pos, 824 CheckSeparatorKind IsAfterDigits) { 825 if (IsAfterDigits == CSK_AfterDigits) { 826 if (Pos == ThisTokBegin) 827 return; 828 --Pos; 829 } else if (Pos == ThisTokEnd) 830 return; 831 832 if (isDigitSeparator(*Pos)) { 833 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Pos - ThisTokBegin), 834 diag::err_digit_separator_not_between_digits) 835 << IsAfterDigits; 836 hadError = true; 837 } 838 } 839 840 /// ParseNumberStartingWithZero - This method is called when the first character 841 /// of the number is found to be a zero. This means it is either an octal 842 /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or 843 /// a floating point number (01239.123e4). Eat the prefix, determining the 844 /// radix etc. 845 void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { 846 assert(s[0] == '0' && "Invalid method call"); 847 s++; 848 849 int c1 = s[0]; 850 851 // Handle a hex number like 0x1234. 852 if ((c1 == 'x' || c1 == 'X') && (isHexDigit(s[1]) || s[1] == '.')) { 853 s++; 854 assert(s < ThisTokEnd && "didn't maximally munch?"); 855 radix = 16; 856 DigitsBegin = s; 857 s = SkipHexDigits(s); 858 bool HasSignificandDigits = containsDigits(DigitsBegin, s); 859 if (s == ThisTokEnd) { 860 // Done. 861 } else if (*s == '.') { 862 s++; 863 saw_period = true; 864 const char *floatDigitsBegin = s; 865 s = SkipHexDigits(s); 866 if (containsDigits(floatDigitsBegin, s)) 867 HasSignificandDigits = true; 868 if (HasSignificandDigits) 869 checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits); 870 } 871 872 if (!HasSignificandDigits) { 873 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), 874 diag::err_hex_constant_requires) 875 << PP.getLangOpts().CPlusPlus << 1; 876 hadError = true; 877 return; 878 } 879 880 // A binary exponent can appear with or with a '.'. If dotted, the 881 // binary exponent is required. 882 if (*s == 'p' || *s == 'P') { 883 checkSeparator(TokLoc, s, CSK_AfterDigits); 884 const char *Exponent = s; 885 s++; 886 saw_exponent = true; 887 if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; // sign 888 const char *first_non_digit = SkipDigits(s); 889 if (!containsDigits(s, first_non_digit)) { 890 if (!hadError) { 891 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), 892 diag::err_exponent_has_no_digits); 893 hadError = true; 894 } 895 return; 896 } 897 checkSeparator(TokLoc, s, CSK_BeforeDigits); 898 s = first_non_digit; 899 900 if (!PP.getLangOpts().HexFloats) 901 PP.Diag(TokLoc, PP.getLangOpts().CPlusPlus 902 ? diag::ext_hex_literal_invalid 903 : diag::ext_hex_constant_invalid); 904 else if (PP.getLangOpts().CPlusPlus17) 905 PP.Diag(TokLoc, diag::warn_cxx17_hex_literal); 906 } else if (saw_period) { 907 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin), 908 diag::err_hex_constant_requires) 909 << PP.getLangOpts().CPlusPlus << 0; 910 hadError = true; 911 } 912 return; 913 } 914 915 // Handle simple binary numbers 0b01010 916 if ((c1 == 'b' || c1 == 'B') && (s[1] == '0' || s[1] == '1')) { 917 // 0b101010 is a C++1y / GCC extension. 918 PP.Diag(TokLoc, 919 PP.getLangOpts().CPlusPlus14 920 ? diag::warn_cxx11_compat_binary_literal 921 : PP.getLangOpts().CPlusPlus 922 ? diag::ext_binary_literal_cxx14 923 : diag::ext_binary_literal); 924 ++s; 925 assert(s < ThisTokEnd && "didn't maximally munch?"); 926 radix = 2; 927 DigitsBegin = s; 928 s = SkipBinaryDigits(s); 929 if (s == ThisTokEnd) { 930 // Done. 931 } else if (isHexDigit(*s) && 932 !isValidUDSuffix(PP.getLangOpts(), 933 StringRef(s, ThisTokEnd - s))) { 934 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), 935 diag::err_invalid_digit) << StringRef(s, 1) << 2; 936 hadError = true; 937 } 938 // Other suffixes will be diagnosed by the caller. 939 return; 940 } 941 942 // For now, the radix is set to 8. If we discover that we have a 943 // floating point constant, the radix will change to 10. Octal floating 944 // point constants are not permitted (only decimal and hexadecimal). 945 radix = 8; 946 DigitsBegin = s; 947 s = SkipOctalDigits(s); 948 if (s == ThisTokEnd) 949 return; // Done, simple octal number like 01234 950 951 // If we have some other non-octal digit that *is* a decimal digit, see if 952 // this is part of a floating point number like 094.123 or 09e1. 953 if (isDigit(*s)) { 954 const char *EndDecimal = SkipDigits(s); 955 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') { 956 s = EndDecimal; 957 radix = 10; 958 } 959 } 960 961 ParseDecimalOrOctalCommon(TokLoc); 962 } 963 964 static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) { 965 switch (Radix) { 966 case 2: 967 return NumDigits <= 64; 968 case 8: 969 return NumDigits <= 64 / 3; // Digits are groups of 3 bits. 970 case 10: 971 return NumDigits <= 19; // floor(log10(2^64)) 972 case 16: 973 return NumDigits <= 64 / 4; // Digits are groups of 4 bits. 974 default: 975 llvm_unreachable("impossible Radix"); 976 } 977 } 978 979 /// GetIntegerValue - Convert this numeric literal value to an APInt that 980 /// matches Val's input width. If there is an overflow, set Val to the low bits 981 /// of the result and return true. Otherwise, return false. 982 bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { 983 // Fast path: Compute a conservative bound on the maximum number of 984 // bits per digit in this radix. If we can't possibly overflow a 985 // uint64 based on that bound then do the simple conversion to 986 // integer. This avoids the expensive overflow checking below, and 987 // handles the common cases that matter (small decimal integers and 988 // hex/octal values which don't overflow). 989 const unsigned NumDigits = SuffixBegin - DigitsBegin; 990 if (alwaysFitsInto64Bits(radix, NumDigits)) { 991 uint64_t N = 0; 992 for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr) 993 if (!isDigitSeparator(*Ptr)) 994 N = N * radix + llvm::hexDigitValue(*Ptr); 995 996 // This will truncate the value to Val's input width. Simply check 997 // for overflow by comparing. 998 Val = N; 999 return Val.getZExtValue() != N; 1000 } 1001 1002 Val = 0; 1003 const char *Ptr = DigitsBegin; 1004 1005 llvm::APInt RadixVal(Val.getBitWidth(), radix); 1006 llvm::APInt CharVal(Val.getBitWidth(), 0); 1007 llvm::APInt OldVal = Val; 1008 1009 bool OverflowOccurred = false; 1010 while (Ptr < SuffixBegin) { 1011 if (isDigitSeparator(*Ptr)) { 1012 ++Ptr; 1013 continue; 1014 } 1015 1016 unsigned C = llvm::hexDigitValue(*Ptr++); 1017 1018 // If this letter is out of bound for this radix, reject it. 1019 assert(C < radix && "NumericLiteralParser ctor should have rejected this"); 1020 1021 CharVal = C; 1022 1023 // Add the digit to the value in the appropriate radix. If adding in digits 1024 // made the value smaller, then this overflowed. 1025 OldVal = Val; 1026 1027 // Multiply by radix, did overflow occur on the multiply? 1028 Val *= RadixVal; 1029 OverflowOccurred |= Val.udiv(RadixVal) != OldVal; 1030 1031 // Add value, did overflow occur on the value? 1032 // (a + b) ult b <=> overflow 1033 Val += CharVal; 1034 OverflowOccurred |= Val.ult(CharVal); 1035 } 1036 return OverflowOccurred; 1037 } 1038 1039 llvm::APFloat::opStatus 1040 NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) { 1041 using llvm::APFloat; 1042 1043 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin); 1044 1045 llvm::SmallString<16> Buffer; 1046 StringRef Str(ThisTokBegin, n); 1047 if (Str.find('\'') != StringRef::npos) { 1048 Buffer.reserve(n); 1049 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer), 1050 &isDigitSeparator); 1051 Str = Buffer; 1052 } 1053 1054 auto StatusOrErr = 1055 Result.convertFromString(Str, APFloat::rmNearestTiesToEven); 1056 assert(StatusOrErr && "Invalid floating point representation"); 1057 return !errorToBool(StatusOrErr.takeError()) ? *StatusOrErr 1058 : APFloat::opInvalidOp; 1059 } 1060 1061 static inline bool IsExponentPart(char c) { 1062 return c == 'p' || c == 'P' || c == 'e' || c == 'E'; 1063 } 1064 1065 bool NumericLiteralParser::GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale) { 1066 assert(radix == 16 || radix == 10); 1067 1068 // Find how many digits are needed to store the whole literal. 1069 unsigned NumDigits = SuffixBegin - DigitsBegin; 1070 if (saw_period) --NumDigits; 1071 1072 // Initial scan of the exponent if it exists 1073 bool ExpOverflowOccurred = false; 1074 bool NegativeExponent = false; 1075 const char *ExponentBegin; 1076 uint64_t Exponent = 0; 1077 int64_t BaseShift = 0; 1078 if (saw_exponent) { 1079 const char *Ptr = DigitsBegin; 1080 1081 while (!IsExponentPart(*Ptr)) ++Ptr; 1082 ExponentBegin = Ptr; 1083 ++Ptr; 1084 NegativeExponent = *Ptr == '-'; 1085 if (NegativeExponent) ++Ptr; 1086 1087 unsigned NumExpDigits = SuffixBegin - Ptr; 1088 if (alwaysFitsInto64Bits(radix, NumExpDigits)) { 1089 llvm::StringRef ExpStr(Ptr, NumExpDigits); 1090 llvm::APInt ExpInt(/*numBits=*/64, ExpStr, /*radix=*/10); 1091 Exponent = ExpInt.getZExtValue(); 1092 } else { 1093 ExpOverflowOccurred = true; 1094 } 1095 1096 if (NegativeExponent) BaseShift -= Exponent; 1097 else BaseShift += Exponent; 1098 } 1099 1100 // Number of bits needed for decimal literal is 1101 // ceil(NumDigits * log2(10)) Integral part 1102 // + Scale Fractional part 1103 // + ceil(Exponent * log2(10)) Exponent 1104 // -------------------------------------------------- 1105 // ceil((NumDigits + Exponent) * log2(10)) + Scale 1106 // 1107 // But for simplicity in handling integers, we can round up log2(10) to 4, 1108 // making: 1109 // 4 * (NumDigits + Exponent) + Scale 1110 // 1111 // Number of digits needed for hexadecimal literal is 1112 // 4 * NumDigits Integral part 1113 // + Scale Fractional part 1114 // + Exponent Exponent 1115 // -------------------------------------------------- 1116 // (4 * NumDigits) + Scale + Exponent 1117 uint64_t NumBitsNeeded; 1118 if (radix == 10) 1119 NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale; 1120 else 1121 NumBitsNeeded = 4 * NumDigits + Exponent + Scale; 1122 1123 if (NumBitsNeeded > std::numeric_limits<unsigned>::max()) 1124 ExpOverflowOccurred = true; 1125 llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0, /*isSigned=*/false); 1126 1127 bool FoundDecimal = false; 1128 1129 int64_t FractBaseShift = 0; 1130 const char *End = saw_exponent ? ExponentBegin : SuffixBegin; 1131 for (const char *Ptr = DigitsBegin; Ptr < End; ++Ptr) { 1132 if (*Ptr == '.') { 1133 FoundDecimal = true; 1134 continue; 1135 } 1136 1137 // Normal reading of an integer 1138 unsigned C = llvm::hexDigitValue(*Ptr); 1139 assert(C < radix && "NumericLiteralParser ctor should have rejected this"); 1140 1141 Val *= radix; 1142 Val += C; 1143 1144 if (FoundDecimal) 1145 // Keep track of how much we will need to adjust this value by from the 1146 // number of digits past the radix point. 1147 --FractBaseShift; 1148 } 1149 1150 // For a radix of 16, we will be multiplying by 2 instead of 16. 1151 if (radix == 16) FractBaseShift *= 4; 1152 BaseShift += FractBaseShift; 1153 1154 Val <<= Scale; 1155 1156 uint64_t Base = (radix == 16) ? 2 : 10; 1157 if (BaseShift > 0) { 1158 for (int64_t i = 0; i < BaseShift; ++i) { 1159 Val *= Base; 1160 } 1161 } else if (BaseShift < 0) { 1162 for (int64_t i = BaseShift; i < 0 && !Val.isNullValue(); ++i) 1163 Val = Val.udiv(Base); 1164 } 1165 1166 bool IntOverflowOccurred = false; 1167 auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth()); 1168 if (Val.getBitWidth() > StoreVal.getBitWidth()) { 1169 IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth())); 1170 StoreVal = Val.trunc(StoreVal.getBitWidth()); 1171 } else if (Val.getBitWidth() < StoreVal.getBitWidth()) { 1172 IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal); 1173 StoreVal = Val.zext(StoreVal.getBitWidth()); 1174 } else { 1175 StoreVal = Val; 1176 } 1177 1178 return IntOverflowOccurred || ExpOverflowOccurred; 1179 } 1180 1181 /// \verbatim 1182 /// user-defined-character-literal: [C++11 lex.ext] 1183 /// character-literal ud-suffix 1184 /// ud-suffix: 1185 /// identifier 1186 /// character-literal: [C++11 lex.ccon] 1187 /// ' c-char-sequence ' 1188 /// u' c-char-sequence ' 1189 /// U' c-char-sequence ' 1190 /// L' c-char-sequence ' 1191 /// u8' c-char-sequence ' [C++1z lex.ccon] 1192 /// c-char-sequence: 1193 /// c-char 1194 /// c-char-sequence c-char 1195 /// c-char: 1196 /// any member of the source character set except the single-quote ', 1197 /// backslash \, or new-line character 1198 /// escape-sequence 1199 /// universal-character-name 1200 /// escape-sequence: 1201 /// simple-escape-sequence 1202 /// octal-escape-sequence 1203 /// hexadecimal-escape-sequence 1204 /// simple-escape-sequence: 1205 /// one of \' \" \? \\ \a \b \f \n \r \t \v 1206 /// octal-escape-sequence: 1207 /// \ octal-digit 1208 /// \ octal-digit octal-digit 1209 /// \ octal-digit octal-digit octal-digit 1210 /// hexadecimal-escape-sequence: 1211 /// \x hexadecimal-digit 1212 /// hexadecimal-escape-sequence hexadecimal-digit 1213 /// universal-character-name: [C++11 lex.charset] 1214 /// \u hex-quad 1215 /// \U hex-quad hex-quad 1216 /// hex-quad: 1217 /// hex-digit hex-digit hex-digit hex-digit 1218 /// \endverbatim 1219 /// 1220 CharLiteralParser::CharLiteralParser(const char *begin, const char *end, 1221 SourceLocation Loc, Preprocessor &PP, 1222 tok::TokenKind kind) { 1223 // At this point we know that the character matches the regex "(L|u|U)?'.*'". 1224 HadError = false; 1225 1226 Kind = kind; 1227 1228 const char *TokBegin = begin; 1229 1230 // Skip over wide character determinant. 1231 if (Kind != tok::char_constant) 1232 ++begin; 1233 if (Kind == tok::utf8_char_constant) 1234 ++begin; 1235 1236 // Skip over the entry quote. 1237 assert(begin[0] == '\'' && "Invalid token lexed"); 1238 ++begin; 1239 1240 // Remove an optional ud-suffix. 1241 if (end[-1] != '\'') { 1242 const char *UDSuffixEnd = end; 1243 do { 1244 --end; 1245 } while (end[-1] != '\''); 1246 // FIXME: Don't bother with this if !tok.hasUCN(). 1247 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end)); 1248 UDSuffixOffset = end - TokBegin; 1249 } 1250 1251 // Trim the ending quote. 1252 assert(end != begin && "Invalid token lexed"); 1253 --end; 1254 1255 // FIXME: The "Value" is an uint64_t so we can handle char literals of 1256 // up to 64-bits. 1257 // FIXME: This extensively assumes that 'char' is 8-bits. 1258 assert(PP.getTargetInfo().getCharWidth() == 8 && 1259 "Assumes char is 8 bits"); 1260 assert(PP.getTargetInfo().getIntWidth() <= 64 && 1261 (PP.getTargetInfo().getIntWidth() & 7) == 0 && 1262 "Assumes sizeof(int) on target is <= 64 and a multiple of char"); 1263 assert(PP.getTargetInfo().getWCharWidth() <= 64 && 1264 "Assumes sizeof(wchar) on target is <= 64"); 1265 1266 SmallVector<uint32_t, 4> codepoint_buffer; 1267 codepoint_buffer.resize(end - begin); 1268 uint32_t *buffer_begin = &codepoint_buffer.front(); 1269 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size(); 1270 1271 // Unicode escapes representing characters that cannot be correctly 1272 // represented in a single code unit are disallowed in character literals 1273 // by this implementation. 1274 uint32_t largest_character_for_kind; 1275 if (tok::wide_char_constant == Kind) { 1276 largest_character_for_kind = 1277 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth()); 1278 } else if (tok::utf8_char_constant == Kind) { 1279 largest_character_for_kind = 0x7F; 1280 } else if (tok::utf16_char_constant == Kind) { 1281 largest_character_for_kind = 0xFFFF; 1282 } else if (tok::utf32_char_constant == Kind) { 1283 largest_character_for_kind = 0x10FFFF; 1284 } else { 1285 largest_character_for_kind = 0x7Fu; 1286 } 1287 1288 while (begin != end) { 1289 // Is this a span of non-escape characters? 1290 if (begin[0] != '\\') { 1291 char const *start = begin; 1292 do { 1293 ++begin; 1294 } while (begin != end && *begin != '\\'); 1295 1296 char const *tmp_in_start = start; 1297 uint32_t *tmp_out_start = buffer_begin; 1298 llvm::ConversionResult res = 1299 llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start), 1300 reinterpret_cast<llvm::UTF8 const *>(begin), 1301 &buffer_begin, buffer_end, llvm::strictConversion); 1302 if (res != llvm::conversionOK) { 1303 // If we see bad encoding for unprefixed character literals, warn and 1304 // simply copy the byte values, for compatibility with gcc and 1305 // older versions of clang. 1306 bool NoErrorOnBadEncoding = isAscii(); 1307 unsigned Msg = diag::err_bad_character_encoding; 1308 if (NoErrorOnBadEncoding) 1309 Msg = diag::warn_bad_character_encoding; 1310 PP.Diag(Loc, Msg); 1311 if (NoErrorOnBadEncoding) { 1312 start = tmp_in_start; 1313 buffer_begin = tmp_out_start; 1314 for (; start != begin; ++start, ++buffer_begin) 1315 *buffer_begin = static_cast<uint8_t>(*start); 1316 } else { 1317 HadError = true; 1318 } 1319 } else { 1320 for (; tmp_out_start < buffer_begin; ++tmp_out_start) { 1321 if (*tmp_out_start > largest_character_for_kind) { 1322 HadError = true; 1323 PP.Diag(Loc, diag::err_character_too_large); 1324 } 1325 } 1326 } 1327 1328 continue; 1329 } 1330 // Is this a Universal Character Name escape? 1331 if (begin[1] == 'u' || begin[1] == 'U') { 1332 unsigned short UcnLen = 0; 1333 if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen, 1334 FullSourceLoc(Loc, PP.getSourceManager()), 1335 &PP.getDiagnostics(), PP.getLangOpts(), true)) { 1336 HadError = true; 1337 } else if (*buffer_begin > largest_character_for_kind) { 1338 HadError = true; 1339 PP.Diag(Loc, diag::err_character_too_large); 1340 } 1341 1342 ++buffer_begin; 1343 continue; 1344 } 1345 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo()); 1346 uint64_t result = 1347 ProcessCharEscape(TokBegin, begin, end, HadError, 1348 FullSourceLoc(Loc,PP.getSourceManager()), 1349 CharWidth, &PP.getDiagnostics(), PP.getLangOpts()); 1350 *buffer_begin++ = result; 1351 } 1352 1353 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front(); 1354 1355 if (NumCharsSoFar > 1) { 1356 if (isWide()) 1357 PP.Diag(Loc, diag::warn_extraneous_char_constant); 1358 else if (isAscii() && NumCharsSoFar == 4) 1359 PP.Diag(Loc, diag::ext_four_char_character_literal); 1360 else if (isAscii()) 1361 PP.Diag(Loc, diag::ext_multichar_character_literal); 1362 else 1363 PP.Diag(Loc, diag::err_multichar_utf_character_literal); 1364 IsMultiChar = true; 1365 } else { 1366 IsMultiChar = false; 1367 } 1368 1369 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0); 1370 1371 // Narrow character literals act as though their value is concatenated 1372 // in this implementation, but warn on overflow. 1373 bool multi_char_too_long = false; 1374 if (isAscii() && isMultiChar()) { 1375 LitVal = 0; 1376 for (size_t i = 0; i < NumCharsSoFar; ++i) { 1377 // check for enough leading zeros to shift into 1378 multi_char_too_long |= (LitVal.countLeadingZeros() < 8); 1379 LitVal <<= 8; 1380 LitVal = LitVal + (codepoint_buffer[i] & 0xFF); 1381 } 1382 } else if (NumCharsSoFar > 0) { 1383 // otherwise just take the last character 1384 LitVal = buffer_begin[-1]; 1385 } 1386 1387 if (!HadError && multi_char_too_long) { 1388 PP.Diag(Loc, diag::warn_char_constant_too_large); 1389 } 1390 1391 // Transfer the value from APInt to uint64_t 1392 Value = LitVal.getZExtValue(); 1393 1394 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") 1395 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple 1396 // character constants are not sign extended in the this implementation: 1397 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. 1398 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) && 1399 PP.getLangOpts().CharIsSigned) 1400 Value = (signed char)Value; 1401 } 1402 1403 /// \verbatim 1404 /// string-literal: [C++0x lex.string] 1405 /// encoding-prefix " [s-char-sequence] " 1406 /// encoding-prefix R raw-string 1407 /// encoding-prefix: 1408 /// u8 1409 /// u 1410 /// U 1411 /// L 1412 /// s-char-sequence: 1413 /// s-char 1414 /// s-char-sequence s-char 1415 /// s-char: 1416 /// any member of the source character set except the double-quote ", 1417 /// backslash \, or new-line character 1418 /// escape-sequence 1419 /// universal-character-name 1420 /// raw-string: 1421 /// " d-char-sequence ( r-char-sequence ) d-char-sequence " 1422 /// r-char-sequence: 1423 /// r-char 1424 /// r-char-sequence r-char 1425 /// r-char: 1426 /// any member of the source character set, except a right parenthesis ) 1427 /// followed by the initial d-char-sequence (which may be empty) 1428 /// followed by a double quote ". 1429 /// d-char-sequence: 1430 /// d-char 1431 /// d-char-sequence d-char 1432 /// d-char: 1433 /// any member of the basic source character set except: 1434 /// space, the left parenthesis (, the right parenthesis ), 1435 /// the backslash \, and the control characters representing horizontal 1436 /// tab, vertical tab, form feed, and newline. 1437 /// escape-sequence: [C++0x lex.ccon] 1438 /// simple-escape-sequence 1439 /// octal-escape-sequence 1440 /// hexadecimal-escape-sequence 1441 /// simple-escape-sequence: 1442 /// one of \' \" \? \\ \a \b \f \n \r \t \v 1443 /// octal-escape-sequence: 1444 /// \ octal-digit 1445 /// \ octal-digit octal-digit 1446 /// \ octal-digit octal-digit octal-digit 1447 /// hexadecimal-escape-sequence: 1448 /// \x hexadecimal-digit 1449 /// hexadecimal-escape-sequence hexadecimal-digit 1450 /// universal-character-name: 1451 /// \u hex-quad 1452 /// \U hex-quad hex-quad 1453 /// hex-quad: 1454 /// hex-digit hex-digit hex-digit hex-digit 1455 /// \endverbatim 1456 /// 1457 StringLiteralParser:: 1458 StringLiteralParser(ArrayRef<Token> StringToks, 1459 Preprocessor &PP, bool Complain) 1460 : SM(PP.getSourceManager()), Features(PP.getLangOpts()), 1461 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr), 1462 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), 1463 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) { 1464 init(StringToks); 1465 } 1466 1467 void StringLiteralParser::init(ArrayRef<Token> StringToks){ 1468 // The literal token may have come from an invalid source location (e.g. due 1469 // to a PCH error), in which case the token length will be 0. 1470 if (StringToks.empty() || StringToks[0].getLength() < 2) 1471 return DiagnoseLexingError(SourceLocation()); 1472 1473 // Scan all of the string portions, remember the max individual token length, 1474 // computing a bound on the concatenated string length, and see whether any 1475 // piece is a wide-string. If any of the string portions is a wide-string 1476 // literal, the result is a wide-string literal [C99 6.4.5p4]. 1477 assert(!StringToks.empty() && "expected at least one token"); 1478 MaxTokenLength = StringToks[0].getLength(); 1479 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!"); 1480 SizeBound = StringToks[0].getLength()-2; // -2 for "". 1481 Kind = StringToks[0].getKind(); 1482 1483 hadError = false; 1484 1485 // Implement Translation Phase #6: concatenation of string literals 1486 /// (C99 5.1.1.2p1). The common case is only one string fragment. 1487 for (unsigned i = 1; i != StringToks.size(); ++i) { 1488 if (StringToks[i].getLength() < 2) 1489 return DiagnoseLexingError(StringToks[i].getLocation()); 1490 1491 // The string could be shorter than this if it needs cleaning, but this is a 1492 // reasonable bound, which is all we need. 1493 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!"); 1494 SizeBound += StringToks[i].getLength()-2; // -2 for "". 1495 1496 // Remember maximum string piece length. 1497 if (StringToks[i].getLength() > MaxTokenLength) 1498 MaxTokenLength = StringToks[i].getLength(); 1499 1500 // Remember if we see any wide or utf-8/16/32 strings. 1501 // Also check for illegal concatenations. 1502 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) { 1503 if (isAscii()) { 1504 Kind = StringToks[i].getKind(); 1505 } else { 1506 if (Diags) 1507 Diags->Report(StringToks[i].getLocation(), 1508 diag::err_unsupported_string_concat); 1509 hadError = true; 1510 } 1511 } 1512 } 1513 1514 // Include space for the null terminator. 1515 ++SizeBound; 1516 1517 // TODO: K&R warning: "traditional C rejects string constant concatenation" 1518 1519 // Get the width in bytes of char/wchar_t/char16_t/char32_t 1520 CharByteWidth = getCharWidth(Kind, Target); 1521 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); 1522 CharByteWidth /= 8; 1523 1524 // The output buffer size needs to be large enough to hold wide characters. 1525 // This is a worst-case assumption which basically corresponds to L"" "long". 1526 SizeBound *= CharByteWidth; 1527 1528 // Size the temporary buffer to hold the result string data. 1529 ResultBuf.resize(SizeBound); 1530 1531 // Likewise, but for each string piece. 1532 SmallString<512> TokenBuf; 1533 TokenBuf.resize(MaxTokenLength); 1534 1535 // Loop over all the strings, getting their spelling, and expanding them to 1536 // wide strings as appropriate. 1537 ResultPtr = &ResultBuf[0]; // Next byte to fill in. 1538 1539 Pascal = false; 1540 1541 SourceLocation UDSuffixTokLoc; 1542 1543 for (unsigned i = 0, e = StringToks.size(); i != e; ++i) { 1544 const char *ThisTokBuf = &TokenBuf[0]; 1545 // Get the spelling of the token, which eliminates trigraphs, etc. We know 1546 // that ThisTokBuf points to a buffer that is big enough for the whole token 1547 // and 'spelled' tokens can only shrink. 1548 bool StringInvalid = false; 1549 unsigned ThisTokLen = 1550 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features, 1551 &StringInvalid); 1552 if (StringInvalid) 1553 return DiagnoseLexingError(StringToks[i].getLocation()); 1554 1555 const char *ThisTokBegin = ThisTokBuf; 1556 const char *ThisTokEnd = ThisTokBuf+ThisTokLen; 1557 1558 // Remove an optional ud-suffix. 1559 if (ThisTokEnd[-1] != '"') { 1560 const char *UDSuffixEnd = ThisTokEnd; 1561 do { 1562 --ThisTokEnd; 1563 } while (ThisTokEnd[-1] != '"'); 1564 1565 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd); 1566 1567 if (UDSuffixBuf.empty()) { 1568 if (StringToks[i].hasUCN()) 1569 expandUCNs(UDSuffixBuf, UDSuffix); 1570 else 1571 UDSuffixBuf.assign(UDSuffix); 1572 UDSuffixToken = i; 1573 UDSuffixOffset = ThisTokEnd - ThisTokBuf; 1574 UDSuffixTokLoc = StringToks[i].getLocation(); 1575 } else { 1576 SmallString<32> ExpandedUDSuffix; 1577 if (StringToks[i].hasUCN()) { 1578 expandUCNs(ExpandedUDSuffix, UDSuffix); 1579 UDSuffix = ExpandedUDSuffix; 1580 } 1581 1582 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the 1583 // result of a concatenation involving at least one user-defined-string- 1584 // literal, all the participating user-defined-string-literals shall 1585 // have the same ud-suffix. 1586 if (UDSuffixBuf != UDSuffix) { 1587 if (Diags) { 1588 SourceLocation TokLoc = StringToks[i].getLocation(); 1589 Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix) 1590 << UDSuffixBuf << UDSuffix 1591 << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc) 1592 << SourceRange(TokLoc, TokLoc); 1593 } 1594 hadError = true; 1595 } 1596 } 1597 } 1598 1599 // Strip the end quote. 1600 --ThisTokEnd; 1601 1602 // TODO: Input character set mapping support. 1603 1604 // Skip marker for wide or unicode strings. 1605 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') { 1606 ++ThisTokBuf; 1607 // Skip 8 of u8 marker for utf8 strings. 1608 if (ThisTokBuf[0] == '8') 1609 ++ThisTokBuf; 1610 } 1611 1612 // Check for raw string 1613 if (ThisTokBuf[0] == 'R') { 1614 ThisTokBuf += 2; // skip R" 1615 1616 const char *Prefix = ThisTokBuf; 1617 while (ThisTokBuf[0] != '(') 1618 ++ThisTokBuf; 1619 ++ThisTokBuf; // skip '(' 1620 1621 // Remove same number of characters from the end 1622 ThisTokEnd -= ThisTokBuf - Prefix; 1623 assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal"); 1624 1625 // C++14 [lex.string]p4: A source-file new-line in a raw string literal 1626 // results in a new-line in the resulting execution string-literal. 1627 StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf); 1628 while (!RemainingTokenSpan.empty()) { 1629 // Split the string literal on \r\n boundaries. 1630 size_t CRLFPos = RemainingTokenSpan.find("\r\n"); 1631 StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos); 1632 StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos); 1633 1634 // Copy everything before the \r\n sequence into the string literal. 1635 if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF)) 1636 hadError = true; 1637 1638 // Point into the \n inside the \r\n sequence and operate on the 1639 // remaining portion of the literal. 1640 RemainingTokenSpan = AfterCRLF.substr(1); 1641 } 1642 } else { 1643 if (ThisTokBuf[0] != '"') { 1644 // The file may have come from PCH and then changed after loading the 1645 // PCH; Fail gracefully. 1646 return DiagnoseLexingError(StringToks[i].getLocation()); 1647 } 1648 ++ThisTokBuf; // skip " 1649 1650 // Check if this is a pascal string 1651 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd && 1652 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') { 1653 1654 // If the \p sequence is found in the first token, we have a pascal string 1655 // Otherwise, if we already have a pascal string, ignore the first \p 1656 if (i == 0) { 1657 ++ThisTokBuf; 1658 Pascal = true; 1659 } else if (Pascal) 1660 ThisTokBuf += 2; 1661 } 1662 1663 while (ThisTokBuf != ThisTokEnd) { 1664 // Is this a span of non-escape characters? 1665 if (ThisTokBuf[0] != '\\') { 1666 const char *InStart = ThisTokBuf; 1667 do { 1668 ++ThisTokBuf; 1669 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); 1670 1671 // Copy the character span over. 1672 if (CopyStringFragment(StringToks[i], ThisTokBegin, 1673 StringRef(InStart, ThisTokBuf - InStart))) 1674 hadError = true; 1675 continue; 1676 } 1677 // Is this a Universal Character Name escape? 1678 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') { 1679 EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, 1680 ResultPtr, hadError, 1681 FullSourceLoc(StringToks[i].getLocation(), SM), 1682 CharByteWidth, Diags, Features); 1683 continue; 1684 } 1685 // Otherwise, this is a non-UCN escape character. Process it. 1686 unsigned ResultChar = 1687 ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError, 1688 FullSourceLoc(StringToks[i].getLocation(), SM), 1689 CharByteWidth*8, Diags, Features); 1690 1691 if (CharByteWidth == 4) { 1692 // FIXME: Make the type of the result buffer correct instead of 1693 // using reinterpret_cast. 1694 llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultPtr); 1695 *ResultWidePtr = ResultChar; 1696 ResultPtr += 4; 1697 } else if (CharByteWidth == 2) { 1698 // FIXME: Make the type of the result buffer correct instead of 1699 // using reinterpret_cast. 1700 llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultPtr); 1701 *ResultWidePtr = ResultChar & 0xFFFF; 1702 ResultPtr += 2; 1703 } else { 1704 assert(CharByteWidth == 1 && "Unexpected char width"); 1705 *ResultPtr++ = ResultChar & 0xFF; 1706 } 1707 } 1708 } 1709 } 1710 1711 if (Pascal) { 1712 if (CharByteWidth == 4) { 1713 // FIXME: Make the type of the result buffer correct instead of 1714 // using reinterpret_cast. 1715 llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultBuf.data()); 1716 ResultWidePtr[0] = GetNumStringChars() - 1; 1717 } else if (CharByteWidth == 2) { 1718 // FIXME: Make the type of the result buffer correct instead of 1719 // using reinterpret_cast. 1720 llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultBuf.data()); 1721 ResultWidePtr[0] = GetNumStringChars() - 1; 1722 } else { 1723 assert(CharByteWidth == 1 && "Unexpected char width"); 1724 ResultBuf[0] = GetNumStringChars() - 1; 1725 } 1726 1727 // Verify that pascal strings aren't too large. 1728 if (GetStringLength() > 256) { 1729 if (Diags) 1730 Diags->Report(StringToks.front().getLocation(), 1731 diag::err_pascal_string_too_long) 1732 << SourceRange(StringToks.front().getLocation(), 1733 StringToks.back().getLocation()); 1734 hadError = true; 1735 return; 1736 } 1737 } else if (Diags) { 1738 // Complain if this string literal has too many characters. 1739 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509; 1740 1741 if (GetNumStringChars() > MaxChars) 1742 Diags->Report(StringToks.front().getLocation(), 1743 diag::ext_string_too_long) 1744 << GetNumStringChars() << MaxChars 1745 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0) 1746 << SourceRange(StringToks.front().getLocation(), 1747 StringToks.back().getLocation()); 1748 } 1749 } 1750 1751 static const char *resyncUTF8(const char *Err, const char *End) { 1752 if (Err == End) 1753 return End; 1754 End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err); 1755 while (++Err != End && (*Err & 0xC0) == 0x80) 1756 ; 1757 return Err; 1758 } 1759 1760 /// This function copies from Fragment, which is a sequence of bytes 1761 /// within Tok's contents (which begin at TokBegin) into ResultPtr. 1762 /// Performs widening for multi-byte characters. 1763 bool StringLiteralParser::CopyStringFragment(const Token &Tok, 1764 const char *TokBegin, 1765 StringRef Fragment) { 1766 const llvm::UTF8 *ErrorPtrTmp; 1767 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp)) 1768 return false; 1769 1770 // If we see bad encoding for unprefixed string literals, warn and 1771 // simply copy the byte values, for compatibility with gcc and older 1772 // versions of clang. 1773 bool NoErrorOnBadEncoding = isAscii(); 1774 if (NoErrorOnBadEncoding) { 1775 memcpy(ResultPtr, Fragment.data(), Fragment.size()); 1776 ResultPtr += Fragment.size(); 1777 } 1778 1779 if (Diags) { 1780 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); 1781 1782 FullSourceLoc SourceLoc(Tok.getLocation(), SM); 1783 const DiagnosticBuilder &Builder = 1784 Diag(Diags, Features, SourceLoc, TokBegin, 1785 ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()), 1786 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding 1787 : diag::err_bad_string_encoding); 1788 1789 const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end()); 1790 StringRef NextFragment(NextStart, Fragment.end()-NextStart); 1791 1792 // Decode into a dummy buffer. 1793 SmallString<512> Dummy; 1794 Dummy.reserve(Fragment.size() * CharByteWidth); 1795 char *Ptr = Dummy.data(); 1796 1797 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) { 1798 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); 1799 NextStart = resyncUTF8(ErrorPtr, Fragment.end()); 1800 Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin, 1801 ErrorPtr, NextStart); 1802 NextFragment = StringRef(NextStart, Fragment.end()-NextStart); 1803 } 1804 } 1805 return !NoErrorOnBadEncoding; 1806 } 1807 1808 void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) { 1809 hadError = true; 1810 if (Diags) 1811 Diags->Report(Loc, diag::err_lexing_string); 1812 } 1813 1814 /// getOffsetOfStringByte - This function returns the offset of the 1815 /// specified byte of the string data represented by Token. This handles 1816 /// advancing over escape sequences in the string. 1817 unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok, 1818 unsigned ByteNo) const { 1819 // Get the spelling of the token. 1820 SmallString<32> SpellingBuffer; 1821 SpellingBuffer.resize(Tok.getLength()); 1822 1823 bool StringInvalid = false; 1824 const char *SpellingPtr = &SpellingBuffer[0]; 1825 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features, 1826 &StringInvalid); 1827 if (StringInvalid) 1828 return 0; 1829 1830 const char *SpellingStart = SpellingPtr; 1831 const char *SpellingEnd = SpellingPtr+TokLen; 1832 1833 // Handle UTF-8 strings just like narrow strings. 1834 if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8') 1835 SpellingPtr += 2; 1836 1837 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && 1838 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet"); 1839 1840 // For raw string literals, this is easy. 1841 if (SpellingPtr[0] == 'R') { 1842 assert(SpellingPtr[1] == '"' && "Should be a raw string literal!"); 1843 // Skip 'R"'. 1844 SpellingPtr += 2; 1845 while (*SpellingPtr != '(') { 1846 ++SpellingPtr; 1847 assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal"); 1848 } 1849 // Skip '('. 1850 ++SpellingPtr; 1851 return SpellingPtr - SpellingStart + ByteNo; 1852 } 1853 1854 // Skip over the leading quote 1855 assert(SpellingPtr[0] == '"' && "Should be a string literal!"); 1856 ++SpellingPtr; 1857 1858 // Skip over bytes until we find the offset we're looking for. 1859 while (ByteNo) { 1860 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!"); 1861 1862 // Step over non-escapes simply. 1863 if (*SpellingPtr != '\\') { 1864 ++SpellingPtr; 1865 --ByteNo; 1866 continue; 1867 } 1868 1869 // Otherwise, this is an escape character. Advance over it. 1870 bool HadError = false; 1871 if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') { 1872 const char *EscapePtr = SpellingPtr; 1873 unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd, 1874 1, Features, HadError); 1875 if (Len > ByteNo) { 1876 // ByteNo is somewhere within the escape sequence. 1877 SpellingPtr = EscapePtr; 1878 break; 1879 } 1880 ByteNo -= Len; 1881 } else { 1882 ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError, 1883 FullSourceLoc(Tok.getLocation(), SM), 1884 CharByteWidth*8, Diags, Features); 1885 --ByteNo; 1886 } 1887 assert(!HadError && "This method isn't valid on erroneous strings"); 1888 } 1889 1890 return SpellingPtr-SpellingStart; 1891 } 1892 1893 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved 1894 /// suffixes as ud-suffixes, because the diagnostic experience is better if we 1895 /// treat it as an invalid suffix. 1896 bool StringLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, 1897 StringRef Suffix) { 1898 return NumericLiteralParser::isValidUDSuffix(LangOpts, Suffix) || 1899 Suffix == "sv"; 1900 } 1901