1 //===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the NumericLiteralParser, CharLiteralParser, and
11 // StringLiteralParser interfaces.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Lex/LiteralSupport.h"
16 #include "clang/Basic/CharInfo.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Lex/LexDiagnostic.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/ConvertUTF.h"
22 #include "llvm/Support/ErrorHandling.h"
24 using namespace clang;
26 static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
28 default: llvm_unreachable("Unknown token type!");
29 case tok::char_constant:
30 case tok::string_literal:
31 case tok::utf8_string_literal:
32 return Target.getCharWidth();
33 case tok::wide_char_constant:
34 case tok::wide_string_literal:
35 return Target.getWCharWidth();
36 case tok::utf16_char_constant:
37 case tok::utf16_string_literal:
38 return Target.getChar16Width();
39 case tok::utf32_char_constant:
40 case tok::utf32_string_literal:
41 return Target.getChar32Width();
45 static CharSourceRange MakeCharSourceRange(const LangOptions &Features,
48 const char *TokRangeBegin,
49 const char *TokRangeEnd) {
50 SourceLocation Begin =
51 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
52 TokLoc.getManager(), Features);
54 Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin,
55 TokLoc.getManager(), Features);
56 return CharSourceRange::getCharRange(Begin, End);
59 /// \brief Produce a diagnostic highlighting some portion of a literal.
61 /// Emits the diagnostic \p DiagID, highlighting the range of characters from
62 /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
63 /// a substring of a spelling buffer for the token beginning at \p TokBegin.
64 static DiagnosticBuilder Diag(DiagnosticsEngine *Diags,
65 const LangOptions &Features, FullSourceLoc TokLoc,
66 const char *TokBegin, const char *TokRangeBegin,
67 const char *TokRangeEnd, unsigned DiagID) {
68 SourceLocation Begin =
69 Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
70 TokLoc.getManager(), Features);
71 return Diags->Report(Begin, DiagID) <<
72 MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd);
75 /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
76 /// either a character or a string literal.
77 static unsigned ProcessCharEscape(const char *ThisTokBegin,
78 const char *&ThisTokBuf,
79 const char *ThisTokEnd, bool &HadError,
80 FullSourceLoc Loc, unsigned CharWidth,
81 DiagnosticsEngine *Diags,
82 const LangOptions &Features) {
83 const char *EscapeBegin = ThisTokBuf;
88 // We know that this character can't be off the end of the buffer, because
89 // that would have been \", which would not have been the end of string.
90 unsigned ResultChar = *ThisTokBuf++;
92 // These map to themselves.
93 case '\\': case '\'': case '"': case '?': break;
95 // These have fixed mappings.
97 // TODO: K&R: the meaning of '\\a' is different in traditional C
105 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
106 diag::ext_nonstandard_escape) << "e";
111 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
112 diag::ext_nonstandard_escape) << "E";
130 case 'x': { // Hex escape.
132 if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
134 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
135 diag::err_hex_escape_no_digits) << "x";
140 // Hex escapes are a maximal series of hex digits.
141 bool Overflow = false;
142 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
143 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
144 if (CharVal == -1) break;
145 // About to shift out a digit?
146 Overflow |= (ResultChar & 0xF0000000) ? true : false;
148 ResultChar |= CharVal;
151 // See if any bits will be truncated when evaluated as a character.
152 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
154 ResultChar &= ~0U >> (32-CharWidth);
157 // Check for overflow.
158 if (Overflow && Diags) // Too many digits to fit in
159 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
160 diag::err_hex_escape_too_large);
163 case '0': case '1': case '2': case '3':
164 case '4': case '5': case '6': case '7': {
169 // Octal escapes are a series of octal digits with maximum length 3.
170 // "\0123" is a two digit sequence equal to "\012" "3".
171 unsigned NumDigits = 0;
174 ResultChar |= *ThisTokBuf++ - '0';
176 } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
177 ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
179 // Check for overflow. Reject '\777', but not L'\777'.
180 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
182 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
183 diag::err_octal_escape_too_large);
184 ResultChar &= ~0U >> (32-CharWidth);
189 // Otherwise, these are not valid escapes.
190 case '(': case '{': case '[': case '%':
191 // GCC accepts these as extensions. We warn about them as such though.
193 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
194 diag::ext_nonstandard_escape)
195 << std::string(1, ResultChar);
201 if (isPrintable(ResultChar))
202 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
203 diag::ext_unknown_escape)
204 << std::string(1, ResultChar);
206 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
207 diag::ext_unknown_escape)
208 << "x" + llvm::utohexstr(ResultChar);
215 static void appendCodePoint(unsigned Codepoint,
216 llvm::SmallVectorImpl<char> &Str) {
218 char *ResultPtr = ResultBuf;
219 bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr);
221 assert(Res && "Unexpected conversion failure");
222 Str.append(ResultBuf, ResultPtr);
225 void clang::expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) {
226 for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
233 assert(*I == 'u' || *I == 'U');
235 unsigned NumHexDigits;
241 assert(I + NumHexDigits <= E);
243 uint32_t CodePoint = 0;
244 for (++I; NumHexDigits != 0; ++I, --NumHexDigits) {
245 unsigned Value = llvm::hexDigitValue(*I);
246 assert(Value != -1U);
252 appendCodePoint(CodePoint, Buf);
257 /// ProcessUCNEscape - Read the Universal Character Name, check constraints and
258 /// return the UTF32.
259 static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
260 const char *ThisTokEnd,
261 uint32_t &UcnVal, unsigned short &UcnLen,
262 FullSourceLoc Loc, DiagnosticsEngine *Diags,
263 const LangOptions &Features,
264 bool in_char_string_literal = false) {
265 const char *UcnBegin = ThisTokBuf;
267 // Skip the '\u' char's.
270 if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
272 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
273 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
276 UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
277 unsigned short UcnLenSave = UcnLen;
278 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
279 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
280 if (CharVal == -1) break;
284 // If we didn't consume the proper number of digits, there is a problem.
287 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
288 diag::err_ucn_escape_incomplete);
292 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
293 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints
294 UcnVal > 0x10FFFF) { // maximum legal UTF32 value
296 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
297 diag::err_ucn_escape_invalid);
301 // C++11 allows UCNs that refer to control characters and basic source
302 // characters inside character and string literals
304 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { // $, @, `
305 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
307 char BasicSCSChar = UcnVal;
308 if (UcnVal >= 0x20 && UcnVal < 0x7f)
309 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
310 IsError ? diag::err_ucn_escape_basic_scs :
311 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
312 << StringRef(&BasicSCSChar, 1);
314 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
315 IsError ? diag::err_ucn_control_character :
316 diag::warn_cxx98_compat_literal_ucn_control_character);
322 if (!Features.CPlusPlus && !Features.C99 && Diags)
323 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
324 diag::warn_ucn_not_valid_in_c89_literal);
329 /// MeasureUCNEscape - Determine the number of bytes within the resulting string
330 /// which this UCN will occupy.
331 static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
332 const char *ThisTokEnd, unsigned CharByteWidth,
333 const LangOptions &Features, bool &HadError) {
334 // UTF-32: 4 bytes per escape.
335 if (CharByteWidth == 4)
339 unsigned short UcnLen = 0;
342 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
343 UcnLen, Loc, nullptr, Features, true)) {
348 // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
349 if (CharByteWidth == 2)
350 return UcnVal <= 0xFFFF ? 2 : 4;
357 if (UcnVal < 0x10000)
362 /// EncodeUCNEscape - Read the Universal Character Name, check constraints and
363 /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
364 /// StringLiteralParser. When we decide to implement UCN's for identifiers,
365 /// we will likely rework our support for UCN's.
366 static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
367 const char *ThisTokEnd,
368 char *&ResultBuf, bool &HadError,
369 FullSourceLoc Loc, unsigned CharByteWidth,
370 DiagnosticsEngine *Diags,
371 const LangOptions &Features) {
372 typedef uint32_t UTF32;
374 unsigned short UcnLen = 0;
375 if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
376 Loc, Diags, Features, true)) {
381 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
382 "only character widths of 1, 2, or 4 bytes supported");
385 assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
387 if (CharByteWidth == 4) {
388 // FIXME: Make the type of the result buffer correct instead of
389 // using reinterpret_cast.
390 UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf);
396 if (CharByteWidth == 2) {
397 // FIXME: Make the type of the result buffer correct instead of
398 // using reinterpret_cast.
399 UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf);
401 if (UcnVal <= (UTF32)0xFFFF) {
409 *ResultPtr = 0xD800 + (UcnVal >> 10);
410 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
415 assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
417 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
418 // The conversion below was inspired by:
419 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
420 // First, we determine how many bytes the result will require.
421 typedef uint8_t UTF8;
423 unsigned short bytesToWrite = 0;
424 if (UcnVal < (UTF32)0x80)
426 else if (UcnVal < (UTF32)0x800)
428 else if (UcnVal < (UTF32)0x10000)
433 const unsigned byteMask = 0xBF;
434 const unsigned byteMark = 0x80;
436 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
437 // into the first byte, depending on how many bytes follow.
438 static const UTF8 firstByteMark[5] = {
439 0x00, 0x00, 0xC0, 0xE0, 0xF0
441 // Finally, we write the bytes into ResultBuf.
442 ResultBuf += bytesToWrite;
443 switch (bytesToWrite) { // note: everything falls through.
444 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
445 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
446 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
447 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
449 // Update the buffer.
450 ResultBuf += bytesToWrite;
454 /// integer-constant: [C99 6.4.4.1]
455 /// decimal-constant integer-suffix
456 /// octal-constant integer-suffix
457 /// hexadecimal-constant integer-suffix
458 /// binary-literal integer-suffix [GNU, C++1y]
459 /// user-defined-integer-literal: [C++11 lex.ext]
460 /// decimal-literal ud-suffix
461 /// octal-literal ud-suffix
462 /// hexadecimal-literal ud-suffix
463 /// binary-literal ud-suffix [GNU, C++1y]
464 /// decimal-constant:
466 /// decimal-constant digit
469 /// octal-constant octal-digit
470 /// hexadecimal-constant:
471 /// hexadecimal-prefix hexadecimal-digit
472 /// hexadecimal-constant hexadecimal-digit
473 /// hexadecimal-prefix: one of
478 /// binary-literal binary-digit
480 /// unsigned-suffix [long-suffix]
481 /// unsigned-suffix [long-long-suffix]
482 /// long-suffix [unsigned-suffix]
483 /// long-long-suffix [unsigned-sufix]
485 /// 1 2 3 4 5 6 7 8 9
488 /// hexadecimal-digit:
489 /// 0 1 2 3 4 5 6 7 8 9
495 /// unsigned-suffix: one of
497 /// long-suffix: one of
499 /// long-long-suffix: one of
502 /// floating-constant: [C99 6.4.4.2]
503 /// TODO: add rules...
505 NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
506 SourceLocation TokLoc,
508 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
510 // This routine assumes that the range begin/end matches the regex for integer
511 // and FP constants (specifically, the 'pp-number' regex), and assumes that
512 // the byte at "*end" is both valid and not part of the regex. Because of
513 // this, it doesn't have to check for 'overscan' in various places.
514 assert(!isPreprocessingNumberBody(*ThisTokEnd) && "didn't maximally munch?");
516 s = DigitsBegin = ThisTokBegin;
517 saw_exponent = false;
519 saw_ud_suffix = false;
525 MicrosoftInteger = 0;
528 if (*s == '0') { // parse radix
529 ParseNumberStartingWithZero(TokLoc);
532 } else { // the first digit is non-zero
535 if (s == ThisTokEnd) {
537 } else if (isHexDigit(*s) && !(*s == 'e' || *s == 'E')) {
538 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
539 diag::err_invalid_decimal_digit) << StringRef(s, 1);
542 } else if (*s == '.') {
543 checkSeparator(TokLoc, s, CSK_AfterDigits);
546 checkSeparator(TokLoc, s, CSK_BeforeDigits);
549 if ((*s == 'e' || *s == 'E')) { // exponent
550 checkSeparator(TokLoc, s, CSK_AfterDigits);
551 const char *Exponent = s;
554 if (*s == '+' || *s == '-') s++; // sign
555 checkSeparator(TokLoc, s, CSK_BeforeDigits);
556 const char *first_non_digit = SkipDigits(s);
557 if (first_non_digit != s) {
560 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin),
561 diag::err_exponent_has_no_digits);
569 checkSeparator(TokLoc, s, CSK_AfterDigits);
571 // Parse the suffix. At this point we can classify whether we have an FP or
573 bool isFPConstant = isFloatingLiteral();
574 const char *ImaginarySuffixLoc = nullptr;
576 // Loop over all of the characters of the suffix. If we see something bad,
577 // we break out of the loop.
578 for (; s != ThisTokEnd; ++s) {
580 case 'f': // FP Suffix for "float"
582 if (!isFPConstant) break; // Error for integer constant.
583 if (isFloat || isLong) break; // FF, LF invalid.
585 continue; // Success.
588 if (isFPConstant) break; // Error for floating constant.
589 if (isUnsigned) break; // Cannot be repeated.
591 continue; // Success.
594 if (isLong || isLongLong) break; // Cannot be repeated.
595 if (isFloat) break; // LF invalid.
597 // Check for long long. The L's need to be adjacent and the same case.
598 if (s+1 != ThisTokEnd && s[1] == s[0]) {
599 if (isFPConstant) break; // long long invalid for floats.
601 ++s; // Eat both of them.
605 continue; // Success.
608 if (PP.getLangOpts().MicrosoftExt) {
609 if (isLong || isLongLong || MicrosoftInteger)
612 // Allow i8, i16, i32, i64, and i128.
613 if (s + 1 != ThisTokEnd) {
616 if (isFPConstant) break;
618 MicrosoftInteger = 8;
621 if (isFPConstant) break;
622 if (s + 2 == ThisTokEnd) break;
624 s += 3; // i16 suffix
625 MicrosoftInteger = 16;
627 else if (s[2] == '2') {
628 if (s + 3 == ThisTokEnd) break;
630 s += 4; // i128 suffix
631 MicrosoftInteger = 128;
636 if (isFPConstant) break;
637 if (s + 2 == ThisTokEnd) break;
639 s += 3; // i32 suffix
640 MicrosoftInteger = 32;
644 if (isFPConstant) break;
645 if (s + 2 == ThisTokEnd) break;
647 s += 3; // i64 suffix
648 MicrosoftInteger = 64;
654 if (MicrosoftInteger)
658 // "i", "if", and "il" are user-defined suffixes in C++1y.
659 if (PP.getLangOpts().CPlusPlus1y && *s == 'i')
664 if (isImaginary) break; // Cannot be repeated.
666 ImaginarySuffixLoc = s;
667 continue; // Success.
669 // If we reached here, there was an error or a ud-suffix.
673 if (s != ThisTokEnd) {
674 // FIXME: Don't bother expanding UCNs if !tok.hasUCN().
675 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
676 if (isValidUDSuffix(PP.getLangOpts(), UDSuffixBuf)) {
677 // Any suffix pieces we might have parsed are actually part of the
684 MicrosoftInteger = 0;
686 saw_ud_suffix = true;
690 // Report an error if there are any.
691 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin),
692 isFPConstant ? diag::err_invalid_suffix_float_constant :
693 diag::err_invalid_suffix_integer_constant)
694 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
700 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc,
701 ImaginarySuffixLoc - ThisTokBegin),
702 diag::ext_imaginary_constant);
706 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved
707 /// suffixes as ud-suffixes, because the diagnostic experience is better if we
708 /// treat it as an invalid suffix.
709 bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts,
711 if (!LangOpts.CPlusPlus11 || Suffix.empty())
714 // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid.
715 if (Suffix[0] == '_')
718 // In C++11, there are no library suffixes.
719 if (!LangOpts.CPlusPlus1y)
722 // In C++1y, "s", "h", "min", "ms", "us", and "ns" are used in the library.
723 // Per tweaked N3660, "il", "i", and "if" are also used in the library.
724 return llvm::StringSwitch<bool>(Suffix)
725 .Cases("h", "min", "s", true)
726 .Cases("ms", "us", "ns", true)
727 .Cases("il", "i", "if", true)
731 void NumericLiteralParser::checkSeparator(SourceLocation TokLoc,
733 CheckSeparatorKind IsAfterDigits) {
734 if (IsAfterDigits == CSK_AfterDigits) {
735 if (Pos == ThisTokBegin)
738 } else if (Pos == ThisTokEnd)
741 if (isDigitSeparator(*Pos))
742 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Pos - ThisTokBegin),
743 diag::err_digit_separator_not_between_digits)
747 /// ParseNumberStartingWithZero - This method is called when the first character
748 /// of the number is found to be a zero. This means it is either an octal
749 /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
750 /// a floating point number (01239.123e4). Eat the prefix, determining the
752 void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
753 assert(s[0] == '0' && "Invalid method call");
759 // Handle a hex number like 0x1234.
760 if ((c1 == 'x' || c1 == 'X') && (isHexDigit(c2) || c2 == '.')) {
764 s = SkipHexDigits(s);
765 bool noSignificand = (s == DigitsBegin);
766 if (s == ThisTokEnd) {
768 } else if (*s == '.') {
771 const char *floatDigitsBegin = s;
772 checkSeparator(TokLoc, s, CSK_BeforeDigits);
773 s = SkipHexDigits(s);
774 noSignificand &= (floatDigitsBegin == s);
778 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
779 diag::err_hexconstant_requires_digits);
784 // A binary exponent can appear with or with a '.'. If dotted, the
785 // binary exponent is required.
786 if (*s == 'p' || *s == 'P') {
787 checkSeparator(TokLoc, s, CSK_AfterDigits);
788 const char *Exponent = s;
791 if (*s == '+' || *s == '-') s++; // sign
792 const char *first_non_digit = SkipDigits(s);
793 if (first_non_digit == s) {
794 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
795 diag::err_exponent_has_no_digits);
799 checkSeparator(TokLoc, s, CSK_BeforeDigits);
802 if (!PP.getLangOpts().HexFloats)
803 PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
804 } else if (saw_period) {
805 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
806 diag::err_hexconstant_requires_exponent);
812 // Handle simple binary numbers 0b01010
813 if ((c1 == 'b' || c1 == 'B') && (c2 == '0' || c2 == '1')) {
814 // 0b101010 is a C++1y / GCC extension.
816 PP.getLangOpts().CPlusPlus1y
817 ? diag::warn_cxx11_compat_binary_literal
818 : PP.getLangOpts().CPlusPlus
819 ? diag::ext_binary_literal_cxx1y
820 : diag::ext_binary_literal);
824 s = SkipBinaryDigits(s);
825 if (s == ThisTokEnd) {
827 } else if (isHexDigit(*s)) {
828 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
829 diag::err_invalid_binary_digit) << StringRef(s, 1);
832 // Other suffixes will be diagnosed by the caller.
836 // For now, the radix is set to 8. If we discover that we have a
837 // floating point constant, the radix will change to 10. Octal floating
838 // point constants are not permitted (only decimal and hexadecimal).
841 s = SkipOctalDigits(s);
843 return; // Done, simple octal number like 01234
845 // If we have some other non-octal digit that *is* a decimal digit, see if
846 // this is part of a floating point number like 094.123 or 09e1.
848 const char *EndDecimal = SkipDigits(s);
849 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
855 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
856 // the code is using an incorrect base.
857 if (isHexDigit(*s) && *s != 'e' && *s != 'E') {
858 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
859 diag::err_invalid_octal_digit) << StringRef(s, 1);
868 checkSeparator(TokLoc, s, CSK_BeforeDigits);
869 s = SkipDigits(s); // Skip suffix.
871 if (*s == 'e' || *s == 'E') { // exponent
872 checkSeparator(TokLoc, s, CSK_AfterDigits);
873 const char *Exponent = s;
877 if (*s == '+' || *s == '-') s++; // sign
878 const char *first_non_digit = SkipDigits(s);
879 if (first_non_digit != s) {
880 checkSeparator(TokLoc, s, CSK_BeforeDigits);
883 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
884 diag::err_exponent_has_no_digits);
891 static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) {
894 return NumDigits <= 64;
896 return NumDigits <= 64 / 3; // Digits are groups of 3 bits.
898 return NumDigits <= 19; // floor(log10(2^64))
900 return NumDigits <= 64 / 4; // Digits are groups of 4 bits.
902 llvm_unreachable("impossible Radix");
906 /// GetIntegerValue - Convert this numeric literal value to an APInt that
907 /// matches Val's input width. If there is an overflow, set Val to the low bits
908 /// of the result and return true. Otherwise, return false.
909 bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
910 // Fast path: Compute a conservative bound on the maximum number of
911 // bits per digit in this radix. If we can't possibly overflow a
912 // uint64 based on that bound then do the simple conversion to
913 // integer. This avoids the expensive overflow checking below, and
914 // handles the common cases that matter (small decimal integers and
915 // hex/octal values which don't overflow).
916 const unsigned NumDigits = SuffixBegin - DigitsBegin;
917 if (alwaysFitsInto64Bits(radix, NumDigits)) {
919 for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
920 if (!isDigitSeparator(*Ptr))
921 N = N * radix + llvm::hexDigitValue(*Ptr);
923 // This will truncate the value to Val's input width. Simply check
924 // for overflow by comparing.
926 return Val.getZExtValue() != N;
930 const char *Ptr = DigitsBegin;
932 llvm::APInt RadixVal(Val.getBitWidth(), radix);
933 llvm::APInt CharVal(Val.getBitWidth(), 0);
934 llvm::APInt OldVal = Val;
936 bool OverflowOccurred = false;
937 while (Ptr < SuffixBegin) {
938 if (isDigitSeparator(*Ptr)) {
943 unsigned C = llvm::hexDigitValue(*Ptr++);
945 // If this letter is out of bound for this radix, reject it.
946 assert(C < radix && "NumericLiteralParser ctor should have rejected this");
950 // Add the digit to the value in the appropriate radix. If adding in digits
951 // made the value smaller, then this overflowed.
954 // Multiply by radix, did overflow occur on the multiply?
956 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
958 // Add value, did overflow occur on the value?
959 // (a + b) ult b <=> overflow
961 OverflowOccurred |= Val.ult(CharVal);
963 return OverflowOccurred;
966 llvm::APFloat::opStatus
967 NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
970 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
972 llvm::SmallString<16> Buffer;
973 StringRef Str(ThisTokBegin, n);
974 if (Str.find('\'') != StringRef::npos) {
976 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
981 return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
986 /// user-defined-character-literal: [C++11 lex.ext]
987 /// character-literal ud-suffix
990 /// character-literal: [C++11 lex.ccon]
991 /// ' c-char-sequence '
992 /// u' c-char-sequence '
993 /// U' c-char-sequence '
994 /// L' c-char-sequence '
997 /// c-char-sequence c-char
999 /// any member of the source character set except the single-quote ',
1000 /// backslash \, or new-line character
1002 /// universal-character-name
1003 /// escape-sequence:
1004 /// simple-escape-sequence
1005 /// octal-escape-sequence
1006 /// hexadecimal-escape-sequence
1007 /// simple-escape-sequence:
1008 /// one of \' \" \? \\ \a \b \f \n \r \t \v
1009 /// octal-escape-sequence:
1011 /// \ octal-digit octal-digit
1012 /// \ octal-digit octal-digit octal-digit
1013 /// hexadecimal-escape-sequence:
1014 /// \x hexadecimal-digit
1015 /// hexadecimal-escape-sequence hexadecimal-digit
1016 /// universal-character-name: [C++11 lex.charset]
1018 /// \U hex-quad hex-quad
1020 /// hex-digit hex-digit hex-digit hex-digit
1023 CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
1024 SourceLocation Loc, Preprocessor &PP,
1025 tok::TokenKind kind) {
1026 // At this point we know that the character matches the regex "(L|u|U)?'.*'".
1031 const char *TokBegin = begin;
1033 // Skip over wide character determinant.
1034 if (Kind != tok::char_constant) {
1038 // Skip over the entry quote.
1039 assert(begin[0] == '\'' && "Invalid token lexed");
1042 // Remove an optional ud-suffix.
1043 if (end[-1] != '\'') {
1044 const char *UDSuffixEnd = end;
1047 } while (end[-1] != '\'');
1048 // FIXME: Don't bother with this if !tok.hasUCN().
1049 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1050 UDSuffixOffset = end - TokBegin;
1053 // Trim the ending quote.
1054 assert(end != begin && "Invalid token lexed");
1057 // FIXME: The "Value" is an uint64_t so we can handle char literals of
1059 // FIXME: This extensively assumes that 'char' is 8-bits.
1060 assert(PP.getTargetInfo().getCharWidth() == 8 &&
1061 "Assumes char is 8 bits");
1062 assert(PP.getTargetInfo().getIntWidth() <= 64 &&
1063 (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
1064 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1065 assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
1066 "Assumes sizeof(wchar) on target is <= 64");
1068 SmallVector<uint32_t, 4> codepoint_buffer;
1069 codepoint_buffer.resize(end - begin);
1070 uint32_t *buffer_begin = &codepoint_buffer.front();
1071 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1073 // Unicode escapes representing characters that cannot be correctly
1074 // represented in a single code unit are disallowed in character literals
1075 // by this implementation.
1076 uint32_t largest_character_for_kind;
1077 if (tok::wide_char_constant == Kind) {
1078 largest_character_for_kind =
1079 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
1080 } else if (tok::utf16_char_constant == Kind) {
1081 largest_character_for_kind = 0xFFFF;
1082 } else if (tok::utf32_char_constant == Kind) {
1083 largest_character_for_kind = 0x10FFFF;
1085 largest_character_for_kind = 0x7Fu;
1088 while (begin != end) {
1089 // Is this a span of non-escape characters?
1090 if (begin[0] != '\\') {
1091 char const *start = begin;
1094 } while (begin != end && *begin != '\\');
1096 char const *tmp_in_start = start;
1097 uint32_t *tmp_out_start = buffer_begin;
1098 ConversionResult res =
1099 ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
1100 reinterpret_cast<UTF8 const *>(begin),
1101 &buffer_begin, buffer_end, strictConversion);
1102 if (res != conversionOK) {
1103 // If we see bad encoding for unprefixed character literals, warn and
1104 // simply copy the byte values, for compatibility with gcc and
1105 // older versions of clang.
1106 bool NoErrorOnBadEncoding = isAscii();
1107 unsigned Msg = diag::err_bad_character_encoding;
1108 if (NoErrorOnBadEncoding)
1109 Msg = diag::warn_bad_character_encoding;
1111 if (NoErrorOnBadEncoding) {
1112 start = tmp_in_start;
1113 buffer_begin = tmp_out_start;
1114 for (; start != begin; ++start, ++buffer_begin)
1115 *buffer_begin = static_cast<uint8_t>(*start);
1120 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1121 if (*tmp_out_start > largest_character_for_kind) {
1123 PP.Diag(Loc, diag::err_character_too_large);
1130 // Is this a Universal Character Name escape?
1131 if (begin[1] == 'u' || begin[1] == 'U') {
1132 unsigned short UcnLen = 0;
1133 if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen,
1134 FullSourceLoc(Loc, PP.getSourceManager()),
1135 &PP.getDiagnostics(), PP.getLangOpts(), true)) {
1137 } else if (*buffer_begin > largest_character_for_kind) {
1139 PP.Diag(Loc, diag::err_character_too_large);
1145 unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
1147 ProcessCharEscape(TokBegin, begin, end, HadError,
1148 FullSourceLoc(Loc,PP.getSourceManager()),
1149 CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
1150 *buffer_begin++ = result;
1153 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1155 if (NumCharsSoFar > 1) {
1157 PP.Diag(Loc, diag::warn_extraneous_char_constant);
1158 else if (isAscii() && NumCharsSoFar == 4)
1159 PP.Diag(Loc, diag::ext_four_char_character_literal);
1161 PP.Diag(Loc, diag::ext_multichar_character_literal);
1163 PP.Diag(Loc, diag::err_multichar_utf_character_literal);
1166 IsMultiChar = false;
1169 llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
1171 // Narrow character literals act as though their value is concatenated
1172 // in this implementation, but warn on overflow.
1173 bool multi_char_too_long = false;
1174 if (isAscii() && isMultiChar()) {
1176 for (size_t i = 0; i < NumCharsSoFar; ++i) {
1177 // check for enough leading zeros to shift into
1178 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1180 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1182 } else if (NumCharsSoFar > 0) {
1183 // otherwise just take the last character
1184 LitVal = buffer_begin[-1];
1187 if (!HadError && multi_char_too_long) {
1188 PP.Diag(Loc, diag::warn_char_constant_too_large);
1191 // Transfer the value from APInt to uint64_t
1192 Value = LitVal.getZExtValue();
1194 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1195 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
1196 // character constants are not sign extended in the this implementation:
1197 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
1198 if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
1199 PP.getLangOpts().CharIsSigned)
1200 Value = (signed char)Value;
1204 /// string-literal: [C++0x lex.string]
1205 /// encoding-prefix " [s-char-sequence] "
1206 /// encoding-prefix R raw-string
1207 /// encoding-prefix:
1212 /// s-char-sequence:
1214 /// s-char-sequence s-char
1216 /// any member of the source character set except the double-quote ",
1217 /// backslash \, or new-line character
1219 /// universal-character-name
1221 /// " d-char-sequence ( r-char-sequence ) d-char-sequence "
1222 /// r-char-sequence:
1224 /// r-char-sequence r-char
1226 /// any member of the source character set, except a right parenthesis )
1227 /// followed by the initial d-char-sequence (which may be empty)
1228 /// followed by a double quote ".
1229 /// d-char-sequence:
1231 /// d-char-sequence d-char
1233 /// any member of the basic source character set except:
1234 /// space, the left parenthesis (, the right parenthesis ),
1235 /// the backslash \, and the control characters representing horizontal
1236 /// tab, vertical tab, form feed, and newline.
1237 /// escape-sequence: [C++0x lex.ccon]
1238 /// simple-escape-sequence
1239 /// octal-escape-sequence
1240 /// hexadecimal-escape-sequence
1241 /// simple-escape-sequence:
1242 /// one of \' \" \? \\ \a \b \f \n \r \t \v
1243 /// octal-escape-sequence:
1245 /// \ octal-digit octal-digit
1246 /// \ octal-digit octal-digit octal-digit
1247 /// hexadecimal-escape-sequence:
1248 /// \x hexadecimal-digit
1249 /// hexadecimal-escape-sequence hexadecimal-digit
1250 /// universal-character-name:
1252 /// \U hex-quad hex-quad
1254 /// hex-digit hex-digit hex-digit hex-digit
1257 StringLiteralParser::
1258 StringLiteralParser(ArrayRef<Token> StringToks,
1259 Preprocessor &PP, bool Complain)
1260 : SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1261 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1262 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
1263 ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
1267 void StringLiteralParser::init(ArrayRef<Token> StringToks){
1268 // The literal token may have come from an invalid source location (e.g. due
1269 // to a PCH error), in which case the token length will be 0.
1270 if (StringToks.empty() || StringToks[0].getLength() < 2)
1271 return DiagnoseLexingError(SourceLocation());
1273 // Scan all of the string portions, remember the max individual token length,
1274 // computing a bound on the concatenated string length, and see whether any
1275 // piece is a wide-string. If any of the string portions is a wide-string
1276 // literal, the result is a wide-string literal [C99 6.4.5p4].
1277 assert(!StringToks.empty() && "expected at least one token");
1278 MaxTokenLength = StringToks[0].getLength();
1279 assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
1280 SizeBound = StringToks[0].getLength()-2; // -2 for "".
1281 Kind = StringToks[0].getKind();
1285 // Implement Translation Phase #6: concatenation of string literals
1286 /// (C99 5.1.1.2p1). The common case is only one string fragment.
1287 for (unsigned i = 1; i != StringToks.size(); ++i) {
1288 if (StringToks[i].getLength() < 2)
1289 return DiagnoseLexingError(StringToks[i].getLocation());
1291 // The string could be shorter than this if it needs cleaning, but this is a
1292 // reasonable bound, which is all we need.
1293 assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
1294 SizeBound += StringToks[i].getLength()-2; // -2 for "".
1296 // Remember maximum string piece length.
1297 if (StringToks[i].getLength() > MaxTokenLength)
1298 MaxTokenLength = StringToks[i].getLength();
1300 // Remember if we see any wide or utf-8/16/32 strings.
1301 // Also check for illegal concatenations.
1302 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
1304 Kind = StringToks[i].getKind();
1307 Diags->Report(StringToks[i].getLocation(),
1308 diag::err_unsupported_string_concat);
1314 // Include space for the null terminator.
1317 // TODO: K&R warning: "traditional C rejects string constant concatenation"
1319 // Get the width in bytes of char/wchar_t/char16_t/char32_t
1320 CharByteWidth = getCharWidth(Kind, Target);
1321 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
1324 // The output buffer size needs to be large enough to hold wide characters.
1325 // This is a worst-case assumption which basically corresponds to L"" "long".
1326 SizeBound *= CharByteWidth;
1328 // Size the temporary buffer to hold the result string data.
1329 ResultBuf.resize(SizeBound);
1331 // Likewise, but for each string piece.
1332 SmallString<512> TokenBuf;
1333 TokenBuf.resize(MaxTokenLength);
1335 // Loop over all the strings, getting their spelling, and expanding them to
1336 // wide strings as appropriate.
1337 ResultPtr = &ResultBuf[0]; // Next byte to fill in.
1341 SourceLocation UDSuffixTokLoc;
1343 for (unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1344 const char *ThisTokBuf = &TokenBuf[0];
1345 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1346 // that ThisTokBuf points to a buffer that is big enough for the whole token
1347 // and 'spelled' tokens can only shrink.
1348 bool StringInvalid = false;
1349 unsigned ThisTokLen =
1350 Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1353 return DiagnoseLexingError(StringToks[i].getLocation());
1355 const char *ThisTokBegin = ThisTokBuf;
1356 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1358 // Remove an optional ud-suffix.
1359 if (ThisTokEnd[-1] != '"') {
1360 const char *UDSuffixEnd = ThisTokEnd;
1363 } while (ThisTokEnd[-1] != '"');
1365 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1367 if (UDSuffixBuf.empty()) {
1368 if (StringToks[i].hasUCN())
1369 expandUCNs(UDSuffixBuf, UDSuffix);
1371 UDSuffixBuf.assign(UDSuffix);
1373 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1374 UDSuffixTokLoc = StringToks[i].getLocation();
1376 SmallString<32> ExpandedUDSuffix;
1377 if (StringToks[i].hasUCN()) {
1378 expandUCNs(ExpandedUDSuffix, UDSuffix);
1379 UDSuffix = ExpandedUDSuffix;
1382 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1383 // result of a concatenation involving at least one user-defined-string-
1384 // literal, all the participating user-defined-string-literals shall
1385 // have the same ud-suffix.
1386 if (UDSuffixBuf != UDSuffix) {
1388 SourceLocation TokLoc = StringToks[i].getLocation();
1389 Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix)
1390 << UDSuffixBuf << UDSuffix
1391 << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc)
1392 << SourceRange(TokLoc, TokLoc);
1399 // Strip the end quote.
1402 // TODO: Input character set mapping support.
1404 // Skip marker for wide or unicode strings.
1405 if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
1407 // Skip 8 of u8 marker for utf8 strings.
1408 if (ThisTokBuf[0] == '8')
1412 // Check for raw string
1413 if (ThisTokBuf[0] == 'R') {
1414 ThisTokBuf += 2; // skip R"
1416 const char *Prefix = ThisTokBuf;
1417 while (ThisTokBuf[0] != '(')
1419 ++ThisTokBuf; // skip '('
1421 // Remove same number of characters from the end
1422 ThisTokEnd -= ThisTokBuf - Prefix;
1423 assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal");
1425 // Copy the string over
1426 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1427 StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf)))
1430 if (ThisTokBuf[0] != '"') {
1431 // The file may have come from PCH and then changed after loading the
1432 // PCH; Fail gracefully.
1433 return DiagnoseLexingError(StringToks[i].getLocation());
1435 ++ThisTokBuf; // skip "
1437 // Check if this is a pascal string
1438 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1439 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1441 // If the \p sequence is found in the first token, we have a pascal string
1442 // Otherwise, if we already have a pascal string, ignore the first \p
1450 while (ThisTokBuf != ThisTokEnd) {
1451 // Is this a span of non-escape characters?
1452 if (ThisTokBuf[0] != '\\') {
1453 const char *InStart = ThisTokBuf;
1456 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1458 // Copy the character span over.
1459 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1460 StringRef(InStart, ThisTokBuf - InStart)))
1464 // Is this a Universal Character Name escape?
1465 if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
1466 EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
1467 ResultPtr, hadError,
1468 FullSourceLoc(StringToks[i].getLocation(), SM),
1469 CharByteWidth, Diags, Features);
1472 // Otherwise, this is a non-UCN escape character. Process it.
1473 unsigned ResultChar =
1474 ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError,
1475 FullSourceLoc(StringToks[i].getLocation(), SM),
1476 CharByteWidth*8, Diags, Features);
1478 if (CharByteWidth == 4) {
1479 // FIXME: Make the type of the result buffer correct instead of
1480 // using reinterpret_cast.
1481 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr);
1482 *ResultWidePtr = ResultChar;
1484 } else if (CharByteWidth == 2) {
1485 // FIXME: Make the type of the result buffer correct instead of
1486 // using reinterpret_cast.
1487 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr);
1488 *ResultWidePtr = ResultChar & 0xFFFF;
1491 assert(CharByteWidth == 1 && "Unexpected char width");
1492 *ResultPtr++ = ResultChar & 0xFF;
1499 if (CharByteWidth == 4) {
1500 // FIXME: Make the type of the result buffer correct instead of
1501 // using reinterpret_cast.
1502 UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data());
1503 ResultWidePtr[0] = GetNumStringChars() - 1;
1504 } else if (CharByteWidth == 2) {
1505 // FIXME: Make the type of the result buffer correct instead of
1506 // using reinterpret_cast.
1507 UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data());
1508 ResultWidePtr[0] = GetNumStringChars() - 1;
1510 assert(CharByteWidth == 1 && "Unexpected char width");
1511 ResultBuf[0] = GetNumStringChars() - 1;
1514 // Verify that pascal strings aren't too large.
1515 if (GetStringLength() > 256) {
1517 Diags->Report(StringToks.front().getLocation(),
1518 diag::err_pascal_string_too_long)
1519 << SourceRange(StringToks.front().getLocation(),
1520 StringToks.back().getLocation());
1525 // Complain if this string literal has too many characters.
1526 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1528 if (GetNumStringChars() > MaxChars)
1529 Diags->Report(StringToks.front().getLocation(),
1530 diag::ext_string_too_long)
1531 << GetNumStringChars() << MaxChars
1532 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1533 << SourceRange(StringToks.front().getLocation(),
1534 StringToks.back().getLocation());
1538 static const char *resyncUTF8(const char *Err, const char *End) {
1541 End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err);
1542 while (++Err != End && (*Err & 0xC0) == 0x80)
1547 /// \brief This function copies from Fragment, which is a sequence of bytes
1548 /// within Tok's contents (which begin at TokBegin) into ResultPtr.
1549 /// Performs widening for multi-byte characters.
1550 bool StringLiteralParser::CopyStringFragment(const Token &Tok,
1551 const char *TokBegin,
1552 StringRef Fragment) {
1553 const UTF8 *ErrorPtrTmp;
1554 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1557 // If we see bad encoding for unprefixed string literals, warn and
1558 // simply copy the byte values, for compatibility with gcc and older
1559 // versions of clang.
1560 bool NoErrorOnBadEncoding = isAscii();
1561 if (NoErrorOnBadEncoding) {
1562 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1563 ResultPtr += Fragment.size();
1567 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1569 FullSourceLoc SourceLoc(Tok.getLocation(), SM);
1570 const DiagnosticBuilder &Builder =
1571 Diag(Diags, Features, SourceLoc, TokBegin,
1572 ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()),
1573 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1574 : diag::err_bad_string_encoding);
1576 const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end());
1577 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1579 // Decode into a dummy buffer.
1580 SmallString<512> Dummy;
1581 Dummy.reserve(Fragment.size() * CharByteWidth);
1582 char *Ptr = Dummy.data();
1584 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1585 const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1586 NextStart = resyncUTF8(ErrorPtr, Fragment.end());
1587 Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin,
1588 ErrorPtr, NextStart);
1589 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1592 return !NoErrorOnBadEncoding;
1595 void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) {
1598 Diags->Report(Loc, diag::err_lexing_string);
1601 /// getOffsetOfStringByte - This function returns the offset of the
1602 /// specified byte of the string data represented by Token. This handles
1603 /// advancing over escape sequences in the string.
1604 unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
1605 unsigned ByteNo) const {
1606 // Get the spelling of the token.
1607 SmallString<32> SpellingBuffer;
1608 SpellingBuffer.resize(Tok.getLength());
1610 bool StringInvalid = false;
1611 const char *SpellingPtr = &SpellingBuffer[0];
1612 unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1617 const char *SpellingStart = SpellingPtr;
1618 const char *SpellingEnd = SpellingPtr+TokLen;
1620 // Handle UTF-8 strings just like narrow strings.
1621 if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8')
1624 assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1625 SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
1627 // For raw string literals, this is easy.
1628 if (SpellingPtr[0] == 'R') {
1629 assert(SpellingPtr[1] == '"' && "Should be a raw string literal!");
1632 while (*SpellingPtr != '(') {
1634 assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal");
1638 return SpellingPtr - SpellingStart + ByteNo;
1641 // Skip over the leading quote
1642 assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1645 // Skip over bytes until we find the offset we're looking for.
1647 assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
1649 // Step over non-escapes simply.
1650 if (*SpellingPtr != '\\') {
1656 // Otherwise, this is an escape character. Advance over it.
1657 bool HadError = false;
1658 if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') {
1659 const char *EscapePtr = SpellingPtr;
1660 unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd,
1661 1, Features, HadError);
1663 // ByteNo is somewhere within the escape sequence.
1664 SpellingPtr = EscapePtr;
1669 ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError,
1670 FullSourceLoc(Tok.getLocation(), SM),
1671 CharByteWidth*8, Diags, Features);
1674 assert(!HadError && "This method isn't valid on erroneous strings");
1677 return SpellingPtr-SpellingStart;