1 //===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the NumericLiteralParser, CharLiteralParser, and
10 // StringLiteralParser interfaces.
12 //===----------------------------------------------------------------------===//
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/Error.h"
29 #include "llvm/Support/ErrorHandling.h"
37 using namespace clang
;
39 static unsigned getCharWidth(tok::TokenKind kind
, const TargetInfo
&Target
) {
41 default: llvm_unreachable("Unknown token type!");
42 case tok::char_constant
:
43 case tok::string_literal
:
44 case tok::utf8_char_constant
:
45 case tok::utf8_string_literal
:
46 return Target
.getCharWidth();
47 case tok::wide_char_constant
:
48 case tok::wide_string_literal
:
49 return Target
.getWCharWidth();
50 case tok::utf16_char_constant
:
51 case tok::utf16_string_literal
:
52 return Target
.getChar16Width();
53 case tok::utf32_char_constant
:
54 case tok::utf32_string_literal
:
55 return Target
.getChar32Width();
59 static CharSourceRange
MakeCharSourceRange(const LangOptions
&Features
,
62 const char *TokRangeBegin
,
63 const char *TokRangeEnd
) {
64 SourceLocation Begin
=
65 Lexer::AdvanceToTokenCharacter(TokLoc
, TokRangeBegin
- TokBegin
,
66 TokLoc
.getManager(), Features
);
68 Lexer::AdvanceToTokenCharacter(Begin
, TokRangeEnd
- TokRangeBegin
,
69 TokLoc
.getManager(), Features
);
70 return CharSourceRange::getCharRange(Begin
, End
);
73 /// Produce a diagnostic highlighting some portion of a literal.
75 /// Emits the diagnostic \p DiagID, highlighting the range of characters from
76 /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
77 /// a substring of a spelling buffer for the token beginning at \p TokBegin.
78 static DiagnosticBuilder
Diag(DiagnosticsEngine
*Diags
,
79 const LangOptions
&Features
, FullSourceLoc TokLoc
,
80 const char *TokBegin
, const char *TokRangeBegin
,
81 const char *TokRangeEnd
, unsigned DiagID
) {
82 SourceLocation Begin
=
83 Lexer::AdvanceToTokenCharacter(TokLoc
, TokRangeBegin
- TokBegin
,
84 TokLoc
.getManager(), Features
);
85 return Diags
->Report(Begin
, DiagID
) <<
86 MakeCharSourceRange(Features
, TokLoc
, TokBegin
, TokRangeBegin
, TokRangeEnd
);
89 /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
90 /// either a character or a string literal.
91 static unsigned ProcessCharEscape(const char *ThisTokBegin
,
92 const char *&ThisTokBuf
,
93 const char *ThisTokEnd
, bool &HadError
,
94 FullSourceLoc Loc
, unsigned CharWidth
,
95 DiagnosticsEngine
*Diags
,
96 const LangOptions
&Features
) {
97 const char *EscapeBegin
= ThisTokBuf
;
98 bool Delimited
= false;
99 bool EndDelimiterFound
= false;
101 // Skip the '\' char.
104 // We know that this character can't be off the end of the buffer, because
105 // that would have been \", which would not have been the end of string.
106 unsigned ResultChar
= *ThisTokBuf
++;
107 switch (ResultChar
) {
108 // These map to themselves.
109 case '\\': case '\'': case '"': case '?': break;
111 // These have fixed mappings.
113 // TODO: K&R: the meaning of '\\a' is different in traditional C
121 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
122 diag::ext_nonstandard_escape
) << "e";
127 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
128 diag::ext_nonstandard_escape
) << "E";
146 case 'x': { // Hex escape.
148 if (ThisTokBuf
!= ThisTokEnd
&& *ThisTokBuf
== '{') {
151 if (*ThisTokBuf
== '}') {
152 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
153 diag::err_delimited_escape_empty
);
156 } else if (ThisTokBuf
== ThisTokEnd
|| !isHexDigit(*ThisTokBuf
)) {
158 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
159 diag::err_hex_escape_no_digits
) << "x";
163 // Hex escapes are a maximal series of hex digits.
164 bool Overflow
= false;
165 for (; ThisTokBuf
!= ThisTokEnd
; ++ThisTokBuf
) {
166 if (Delimited
&& *ThisTokBuf
== '}') {
168 EndDelimiterFound
= true;
171 int CharVal
= llvm::hexDigitValue(*ThisTokBuf
);
173 // Non delimited hex escape sequences stop at the first non-hex digit.
178 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
179 diag::err_delimited_escape_invalid
)
180 << StringRef(ThisTokBuf
, 1);
183 // About to shift out a digit?
184 if (ResultChar
& 0xF0000000)
187 ResultChar
|= CharVal
;
189 // See if any bits will be truncated when evaluated as a character.
190 if (CharWidth
!= 32 && (ResultChar
>> CharWidth
) != 0) {
192 ResultChar
&= ~0U >> (32-CharWidth
);
195 // Check for overflow.
196 if (!HadError
&& Overflow
) { // Too many digits to fit in
199 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
200 diag::err_escape_too_large
)
205 case '0': case '1': case '2': case '3':
206 case '4': case '5': case '6': case '7': {
211 // Octal escapes are a series of octal digits with maximum length 3.
212 // "\0123" is a two digit sequence equal to "\012" "3".
213 unsigned NumDigits
= 0;
216 ResultChar
|= *ThisTokBuf
++ - '0';
218 } while (ThisTokBuf
!= ThisTokEnd
&& NumDigits
< 3 &&
219 ThisTokBuf
[0] >= '0' && ThisTokBuf
[0] <= '7');
221 // Check for overflow. Reject '\777', but not L'\777'.
222 if (CharWidth
!= 32 && (ResultChar
>> CharWidth
) != 0) {
224 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
225 diag::err_escape_too_large
) << 1;
226 ResultChar
&= ~0U >> (32-CharWidth
);
231 bool Overflow
= false;
232 if (ThisTokBuf
== ThisTokEnd
|| *ThisTokBuf
!= '{') {
235 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
236 diag::err_delimited_escape_missing_brace
);
243 if (*ThisTokBuf
== '}') {
244 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
245 diag::err_delimited_escape_empty
);
249 while (ThisTokBuf
!= ThisTokEnd
) {
250 if (*ThisTokBuf
== '}') {
251 EndDelimiterFound
= true;
255 if (*ThisTokBuf
< '0' || *ThisTokBuf
> '7') {
258 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
259 diag::err_delimited_escape_invalid
)
260 << StringRef(ThisTokBuf
, 1);
264 if (ResultChar
& 0x020000000)
268 ResultChar
|= *ThisTokBuf
++ - '0';
270 // Check for overflow. Reject '\777', but not L'\777'.
272 (Overflow
|| (CharWidth
!= 32 && (ResultChar
>> CharWidth
) != 0))) {
275 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
276 diag::err_escape_too_large
)
278 ResultChar
&= ~0U >> (32 - CharWidth
);
282 // Otherwise, these are not valid escapes.
283 case '(': case '{': case '[': case '%':
284 // GCC accepts these as extensions. We warn about them as such though.
286 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
287 diag::ext_nonstandard_escape
)
288 << std::string(1, ResultChar
);
294 if (isPrintable(ResultChar
))
295 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
296 diag::ext_unknown_escape
)
297 << std::string(1, ResultChar
);
299 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
300 diag::ext_unknown_escape
)
301 << "x" + llvm::utohexstr(ResultChar
);
305 if (Delimited
&& Diags
) {
306 if (!EndDelimiterFound
)
307 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
310 else if (!HadError
) {
311 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
312 diag::ext_delimited_escape_sequence
);
319 static void appendCodePoint(unsigned Codepoint
,
320 llvm::SmallVectorImpl
<char> &Str
) {
322 char *ResultPtr
= ResultBuf
;
323 bool Res
= llvm::ConvertCodePointToUTF8(Codepoint
, ResultPtr
);
325 assert(Res
&& "Unexpected conversion failure");
326 Str
.append(ResultBuf
, ResultPtr
);
329 void clang::expandUCNs(SmallVectorImpl
<char> &Buf
, StringRef Input
) {
330 for (StringRef::iterator I
= Input
.begin(), E
= Input
.end(); I
!= E
; ++I
) {
340 assert(Kind
== 'u' || Kind
== 'U');
341 uint32_t CodePoint
= 0;
343 if (Kind
== 'u' && *I
== '{') {
344 for (++I
; *I
!= '}'; ++I
) {
345 unsigned Value
= llvm::hexDigitValue(*I
);
346 assert(Value
!= -1U);
350 appendCodePoint(CodePoint
, Buf
);
354 unsigned NumHexDigits
;
360 assert(I
+ NumHexDigits
<= E
);
362 for (; NumHexDigits
!= 0; ++I
, --NumHexDigits
) {
363 unsigned Value
= llvm::hexDigitValue(*I
);
364 assert(Value
!= -1U);
370 appendCodePoint(CodePoint
, Buf
);
375 /// ProcessUCNEscape - Read the Universal Character Name, check constraints and
376 /// return the UTF32.
377 static bool ProcessUCNEscape(const char *ThisTokBegin
, const char *&ThisTokBuf
,
378 const char *ThisTokEnd
,
379 uint32_t &UcnVal
, unsigned short &UcnLen
,
380 FullSourceLoc Loc
, DiagnosticsEngine
*Diags
,
381 const LangOptions
&Features
,
382 bool in_char_string_literal
= false) {
383 const char *UcnBegin
= ThisTokBuf
;
385 // Skip the '\u' char's.
388 bool Delimited
= false;
389 bool EndDelimiterFound
= false;
390 bool HasError
= false;
392 if (UcnBegin
[1] == 'u' && in_char_string_literal
&&
393 ThisTokBuf
!= ThisTokEnd
&& *ThisTokBuf
== '{') {
396 } else if (ThisTokBuf
== ThisTokEnd
|| !isHexDigit(*ThisTokBuf
)) {
398 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
399 diag::err_hex_escape_no_digits
) << StringRef(&ThisTokBuf
[-1], 1);
402 UcnLen
= (ThisTokBuf
[-1] == 'u' ? 4 : 8);
404 bool Overflow
= false;
405 unsigned short Count
= 0;
406 for (; ThisTokBuf
!= ThisTokEnd
&& (Delimited
|| Count
!= UcnLen
);
408 if (Delimited
&& *ThisTokBuf
== '}') {
410 EndDelimiterFound
= true;
413 int CharVal
= llvm::hexDigitValue(*ThisTokBuf
);
419 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
420 diag::err_delimited_escape_invalid
)
421 << StringRef(ThisTokBuf
, 1);
426 if (UcnVal
& 0xF0000000) {
437 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
438 diag::err_escape_too_large
)
443 if (Delimited
&& !EndDelimiterFound
) {
445 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
452 // If we didn't consume the proper number of digits, there is a problem.
453 if (Count
== 0 || (!Delimited
&& Count
!= UcnLen
)) {
455 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
456 Delimited
? diag::err_delimited_escape_empty
457 : diag::err_ucn_escape_incomplete
);
464 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
465 if ((0xD800 <= UcnVal
&& UcnVal
<= 0xDFFF) || // surrogate codepoints
466 UcnVal
> 0x10FFFF) { // maximum legal UTF32 value
468 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
469 diag::err_ucn_escape_invalid
);
473 // C++11 allows UCNs that refer to control characters and basic source
474 // characters inside character and string literals
476 (UcnVal
!= 0x24 && UcnVal
!= 0x40 && UcnVal
!= 0x60)) { // $, @, `
477 bool IsError
= (!Features
.CPlusPlus11
|| !in_char_string_literal
);
479 char BasicSCSChar
= UcnVal
;
480 if (UcnVal
>= 0x20 && UcnVal
< 0x7f)
481 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
482 IsError
? diag::err_ucn_escape_basic_scs
:
483 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs
)
484 << StringRef(&BasicSCSChar
, 1);
486 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
487 IsError
? diag::err_ucn_control_character
:
488 diag::warn_cxx98_compat_literal_ucn_control_character
);
494 if (!Features
.CPlusPlus
&& !Features
.C99
&& Diags
)
495 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
496 diag::warn_ucn_not_valid_in_c89_literal
);
498 if (Delimited
&& Diags
)
499 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
500 diag::ext_delimited_escape_sequence
);
505 /// MeasureUCNEscape - Determine the number of bytes within the resulting string
506 /// which this UCN will occupy.
507 static int MeasureUCNEscape(const char *ThisTokBegin
, const char *&ThisTokBuf
,
508 const char *ThisTokEnd
, unsigned CharByteWidth
,
509 const LangOptions
&Features
, bool &HadError
) {
510 // UTF-32: 4 bytes per escape.
511 if (CharByteWidth
== 4)
515 unsigned short UcnLen
= 0;
518 if (!ProcessUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, UcnVal
,
519 UcnLen
, Loc
, nullptr, Features
, true)) {
524 // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
525 if (CharByteWidth
== 2)
526 return UcnVal
<= 0xFFFF ? 2 : 4;
533 if (UcnVal
< 0x10000)
538 /// EncodeUCNEscape - Read the Universal Character Name, check constraints and
539 /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
540 /// StringLiteralParser. When we decide to implement UCN's for identifiers,
541 /// we will likely rework our support for UCN's.
542 static void EncodeUCNEscape(const char *ThisTokBegin
, const char *&ThisTokBuf
,
543 const char *ThisTokEnd
,
544 char *&ResultBuf
, bool &HadError
,
545 FullSourceLoc Loc
, unsigned CharByteWidth
,
546 DiagnosticsEngine
*Diags
,
547 const LangOptions
&Features
) {
548 typedef uint32_t UTF32
;
550 unsigned short UcnLen
= 0;
551 if (!ProcessUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, UcnVal
, UcnLen
,
552 Loc
, Diags
, Features
, true)) {
557 assert((CharByteWidth
== 1 || CharByteWidth
== 2 || CharByteWidth
== 4) &&
558 "only character widths of 1, 2, or 4 bytes supported");
561 assert((UcnLen
== 4 || UcnLen
== 8) && "only ucn length of 4 or 8 supported");
563 if (CharByteWidth
== 4) {
564 // FIXME: Make the type of the result buffer correct instead of
565 // using reinterpret_cast.
566 llvm::UTF32
*ResultPtr
= reinterpret_cast<llvm::UTF32
*>(ResultBuf
);
572 if (CharByteWidth
== 2) {
573 // FIXME: Make the type of the result buffer correct instead of
574 // using reinterpret_cast.
575 llvm::UTF16
*ResultPtr
= reinterpret_cast<llvm::UTF16
*>(ResultBuf
);
577 if (UcnVal
<= (UTF32
)0xFFFF) {
585 *ResultPtr
= 0xD800 + (UcnVal
>> 10);
586 *(ResultPtr
+1) = 0xDC00 + (UcnVal
& 0x3FF);
591 assert(CharByteWidth
== 1 && "UTF-8 encoding is only for 1 byte characters");
593 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
594 // The conversion below was inspired by:
595 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
596 // First, we determine how many bytes the result will require.
597 typedef uint8_t UTF8
;
599 unsigned short bytesToWrite
= 0;
600 if (UcnVal
< (UTF32
)0x80)
602 else if (UcnVal
< (UTF32
)0x800)
604 else if (UcnVal
< (UTF32
)0x10000)
609 const unsigned byteMask
= 0xBF;
610 const unsigned byteMark
= 0x80;
612 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
613 // into the first byte, depending on how many bytes follow.
614 static const UTF8 firstByteMark
[5] = {
615 0x00, 0x00, 0xC0, 0xE0, 0xF0
617 // Finally, we write the bytes into ResultBuf.
618 ResultBuf
+= bytesToWrite
;
619 switch (bytesToWrite
) { // note: everything falls through.
621 *--ResultBuf
= (UTF8
)((UcnVal
| byteMark
) & byteMask
); UcnVal
>>= 6;
624 *--ResultBuf
= (UTF8
)((UcnVal
| byteMark
) & byteMask
); UcnVal
>>= 6;
627 *--ResultBuf
= (UTF8
)((UcnVal
| byteMark
) & byteMask
); UcnVal
>>= 6;
630 *--ResultBuf
= (UTF8
) (UcnVal
| firstByteMark
[bytesToWrite
]);
632 // Update the buffer.
633 ResultBuf
+= bytesToWrite
;
636 /// integer-constant: [C99 6.4.4.1]
637 /// decimal-constant integer-suffix
638 /// octal-constant integer-suffix
639 /// hexadecimal-constant integer-suffix
640 /// binary-literal integer-suffix [GNU, C++1y]
641 /// user-defined-integer-literal: [C++11 lex.ext]
642 /// decimal-literal ud-suffix
643 /// octal-literal ud-suffix
644 /// hexadecimal-literal ud-suffix
645 /// binary-literal ud-suffix [GNU, C++1y]
646 /// decimal-constant:
648 /// decimal-constant digit
651 /// octal-constant octal-digit
652 /// hexadecimal-constant:
653 /// hexadecimal-prefix hexadecimal-digit
654 /// hexadecimal-constant hexadecimal-digit
655 /// hexadecimal-prefix: one of
660 /// binary-literal binary-digit
662 /// unsigned-suffix [long-suffix]
663 /// unsigned-suffix [long-long-suffix]
664 /// long-suffix [unsigned-suffix]
665 /// long-long-suffix [unsigned-sufix]
667 /// 1 2 3 4 5 6 7 8 9
670 /// hexadecimal-digit:
671 /// 0 1 2 3 4 5 6 7 8 9
677 /// unsigned-suffix: one of
679 /// long-suffix: one of
681 /// long-long-suffix: one of
684 /// floating-constant: [C99 6.4.4.2]
685 /// TODO: add rules...
687 NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling
,
688 SourceLocation TokLoc
,
689 const SourceManager
&SM
,
690 const LangOptions
&LangOpts
,
691 const TargetInfo
&Target
,
692 DiagnosticsEngine
&Diags
)
693 : SM(SM
), LangOpts(LangOpts
), Diags(Diags
),
694 ThisTokBegin(TokSpelling
.begin()), ThisTokEnd(TokSpelling
.end()) {
696 s
= DigitsBegin
= ThisTokBegin
;
697 saw_exponent
= false;
699 saw_ud_suffix
= false;
700 saw_fixed_point_suffix
= false;
710 MicrosoftInteger
= 0;
716 // This routine assumes that the range begin/end matches the regex for integer
717 // and FP constants (specifically, the 'pp-number' regex), and assumes that
718 // the byte at "*end" is both valid and not part of the regex. Because of
719 // this, it doesn't have to check for 'overscan' in various places.
720 if (isPreprocessingNumberBody(*ThisTokEnd
)) {
721 Diags
.Report(TokLoc
, diag::err_lexing_numeric
);
726 if (*s
== '0') { // parse radix
727 ParseNumberStartingWithZero(TokLoc
);
730 } else { // the first digit is non-zero
733 if (s
== ThisTokEnd
) {
736 ParseDecimalOrOctalCommon(TokLoc
);
743 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
745 // Initial scan to lookahead for fixed point suffix.
746 if (LangOpts
.FixedPoint
) {
747 for (const char *c
= s
; c
!= ThisTokEnd
; ++c
) {
748 if (*c
== 'r' || *c
== 'k' || *c
== 'R' || *c
== 'K') {
749 saw_fixed_point_suffix
= true;
755 // Parse the suffix. At this point we can classify whether we have an FP or
757 bool isFixedPointConstant
= isFixedPointLiteral();
758 bool isFPConstant
= isFloatingLiteral();
759 bool HasSize
= false;
761 // Loop over all of the characters of the suffix. If we see something bad,
762 // we break out of the loop.
763 for (; s
!= ThisTokEnd
; ++s
) {
767 if (!LangOpts
.FixedPoint
)
769 if (isFract
|| isAccum
) break;
770 if (!(saw_period
|| saw_exponent
)) break;
775 if (!LangOpts
.FixedPoint
)
777 if (isFract
|| isAccum
) break;
778 if (!(saw_period
|| saw_exponent
)) break;
781 case 'h': // FP Suffix for "half".
783 // OpenCL Extension v1.2 s9.5 - h or H suffix for half type.
784 if (!(LangOpts
.Half
|| LangOpts
.FixedPoint
))
786 if (isIntegerLiteral()) break; // Error for integer constant.
791 continue; // Success.
792 case 'f': // FP Suffix for "float"
794 if (!isFPConstant
) break; // Error for integer constant.
799 // CUDA host and device may have different _Float16 support, therefore
800 // allows f16 literals to avoid false alarm.
801 // ToDo: more precise check for CUDA.
802 if ((Target
.hasFloat16Type() || LangOpts
.CUDA
) && s
+ 2 < ThisTokEnd
&&
803 s
[1] == '1' && s
[2] == '6') {
804 s
+= 2; // success, eat up 2 characters.
810 continue; // Success.
811 case 'q': // FP Suffix for "__float128"
813 if (!isFPConstant
) break; // Error for integer constant.
818 continue; // Success.
821 if (isFPConstant
) break; // Error for floating constant.
822 if (isUnsigned
) break; // Cannot be repeated.
824 continue; // Success.
831 // Check for long long. The L's need to be adjacent and the same case.
833 assert(s
+ 1 < ThisTokEnd
&& "didn't maximally munch?");
834 if (isFPConstant
) break; // long long invalid for floats.
836 ++s
; // Eat both of them.
840 continue; // Success.
844 break; // Invalid for floats.
852 if (LangOpts
.MicrosoftExt
&& !isFPConstant
) {
853 // Allow i8, i16, i32, and i64. First, look ahead and check if
854 // suffixes are Microsoft integers and not the imaginary unit.
858 case '8': // i8 suffix
863 if (s
[2] == '6') { // i16 suffix
869 if (s
[2] == '2') { // i32 suffix
875 if (s
[2] == '4') { // i64 suffix
887 MicrosoftInteger
= Bits
;
889 assert(s
<= ThisTokEnd
&& "didn't maximally munch?");
896 if (isImaginary
) break; // Cannot be repeated.
898 continue; // Success.
902 break; // Invalid for floats.
904 break; // Invalid if we already have a size for the literal.
906 // wb and WB are allowed, but a mixture of cases like Wb or wB is not. We
907 // explicitly do not support the suffix in C++ as an extension because a
908 // library-based UDL that resolves to a library type may be more
909 // appropriate there.
910 if (!LangOpts
.CPlusPlus
&& ((s
[0] == 'w' && s
[1] == 'b') ||
911 (s
[0] == 'W' && s
[1] == 'B'))) {
914 ++s
; // Skip both characters (2nd char skipped on continue).
915 continue; // Success.
918 // If we reached here, there was an error or a ud-suffix.
922 // "i", "if", and "il" are user-defined suffixes in C++1y.
923 if (s
!= ThisTokEnd
|| isImaginary
) {
924 // FIXME: Don't bother expanding UCNs if !tok.hasUCN().
925 expandUCNs(UDSuffixBuf
, StringRef(SuffixBegin
, ThisTokEnd
- SuffixBegin
));
926 if (isValidUDSuffix(LangOpts
, UDSuffixBuf
)) {
928 // Any suffix pieces we might have parsed are actually part of the
939 MicrosoftInteger
= 0;
940 saw_fixed_point_suffix
= false;
945 saw_ud_suffix
= true;
949 if (s
!= ThisTokEnd
) {
950 // Report an error if there are any.
951 Diags
.Report(Lexer::AdvanceToTokenCharacter(
952 TokLoc
, SuffixBegin
- ThisTokBegin
, SM
, LangOpts
),
953 diag::err_invalid_suffix_constant
)
954 << StringRef(SuffixBegin
, ThisTokEnd
- SuffixBegin
)
955 << (isFixedPointConstant
? 2 : isFPConstant
);
960 if (!hadError
&& saw_fixed_point_suffix
) {
961 assert(isFract
|| isAccum
);
965 /// ParseDecimalOrOctalCommon - This method is called for decimal or octal
966 /// numbers. It issues an error for illegal digits, and handles floating point
967 /// parsing. If it detects a floating point number, the radix is set to 10.
968 void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc
){
969 assert((radix
== 8 || radix
== 10) && "Unexpected radix");
971 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
972 // the code is using an incorrect base.
973 if (isHexDigit(*s
) && *s
!= 'e' && *s
!= 'E' &&
974 !isValidUDSuffix(LangOpts
, StringRef(s
, ThisTokEnd
- s
))) {
976 Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
, LangOpts
),
977 diag::err_invalid_digit
)
978 << StringRef(s
, 1) << (radix
== 8 ? 1 : 0);
984 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
988 checkSeparator(TokLoc
, s
, CSK_BeforeDigits
);
989 s
= SkipDigits(s
); // Skip suffix.
991 if (*s
== 'e' || *s
== 'E') { // exponent
992 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
993 const char *Exponent
= s
;
997 if (s
!= ThisTokEnd
&& (*s
== '+' || *s
== '-')) s
++; // sign
998 const char *first_non_digit
= SkipDigits(s
);
999 if (containsDigits(s
, first_non_digit
)) {
1000 checkSeparator(TokLoc
, s
, CSK_BeforeDigits
);
1001 s
= first_non_digit
;
1004 Diags
.Report(Lexer::AdvanceToTokenCharacter(
1005 TokLoc
, Exponent
- ThisTokBegin
, SM
, LangOpts
),
1006 diag::err_exponent_has_no_digits
);
1014 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved
1015 /// suffixes as ud-suffixes, because the diagnostic experience is better if we
1016 /// treat it as an invalid suffix.
1017 bool NumericLiteralParser::isValidUDSuffix(const LangOptions
&LangOpts
,
1019 if (!LangOpts
.CPlusPlus11
|| Suffix
.empty())
1022 // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid.
1023 if (Suffix
[0] == '_')
1026 // In C++11, there are no library suffixes.
1027 if (!LangOpts
.CPlusPlus14
)
1030 // In C++14, "s", "h", "min", "ms", "us", and "ns" are used in the library.
1031 // Per tweaked N3660, "il", "i", and "if" are also used in the library.
1032 // In C++2a "d" and "y" are used in the library.
1033 return llvm::StringSwitch
<bool>(Suffix
)
1034 .Cases("h", "min", "s", true)
1035 .Cases("ms", "us", "ns", true)
1036 .Cases("il", "i", "if", true)
1037 .Cases("d", "y", LangOpts
.CPlusPlus20
)
1041 void NumericLiteralParser::checkSeparator(SourceLocation TokLoc
,
1043 CheckSeparatorKind IsAfterDigits
) {
1044 if (IsAfterDigits
== CSK_AfterDigits
) {
1045 if (Pos
== ThisTokBegin
)
1048 } else if (Pos
== ThisTokEnd
)
1051 if (isDigitSeparator(*Pos
)) {
1052 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, Pos
- ThisTokBegin
, SM
,
1054 diag::err_digit_separator_not_between_digits
)
1060 /// ParseNumberStartingWithZero - This method is called when the first character
1061 /// of the number is found to be a zero. This means it is either an octal
1062 /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
1063 /// a floating point number (01239.123e4). Eat the prefix, determining the
1065 void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc
) {
1066 assert(s
[0] == '0' && "Invalid method call");
1071 // Handle a hex number like 0x1234.
1072 if ((c1
== 'x' || c1
== 'X') && (isHexDigit(s
[1]) || s
[1] == '.')) {
1074 assert(s
< ThisTokEnd
&& "didn't maximally munch?");
1077 s
= SkipHexDigits(s
);
1078 bool HasSignificandDigits
= containsDigits(DigitsBegin
, s
);
1079 if (s
== ThisTokEnd
) {
1081 } else if (*s
== '.') {
1084 const char *floatDigitsBegin
= s
;
1085 s
= SkipHexDigits(s
);
1086 if (containsDigits(floatDigitsBegin
, s
))
1087 HasSignificandDigits
= true;
1088 if (HasSignificandDigits
)
1089 checkSeparator(TokLoc
, floatDigitsBegin
, CSK_BeforeDigits
);
1092 if (!HasSignificandDigits
) {
1093 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
,
1095 diag::err_hex_constant_requires
)
1096 << LangOpts
.CPlusPlus
<< 1;
1101 // A binary exponent can appear with or with a '.'. If dotted, the
1102 // binary exponent is required.
1103 if (*s
== 'p' || *s
== 'P') {
1104 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
1105 const char *Exponent
= s
;
1107 saw_exponent
= true;
1108 if (s
!= ThisTokEnd
&& (*s
== '+' || *s
== '-')) s
++; // sign
1109 const char *first_non_digit
= SkipDigits(s
);
1110 if (!containsDigits(s
, first_non_digit
)) {
1112 Diags
.Report(Lexer::AdvanceToTokenCharacter(
1113 TokLoc
, Exponent
- ThisTokBegin
, SM
, LangOpts
),
1114 diag::err_exponent_has_no_digits
);
1119 checkSeparator(TokLoc
, s
, CSK_BeforeDigits
);
1120 s
= first_non_digit
;
1122 if (!LangOpts
.HexFloats
)
1123 Diags
.Report(TokLoc
, LangOpts
.CPlusPlus
1124 ? diag::ext_hex_literal_invalid
1125 : diag::ext_hex_constant_invalid
);
1126 else if (LangOpts
.CPlusPlus17
)
1127 Diags
.Report(TokLoc
, diag::warn_cxx17_hex_literal
);
1128 } else if (saw_period
) {
1129 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
,
1131 diag::err_hex_constant_requires
)
1132 << LangOpts
.CPlusPlus
<< 0;
1138 // Handle simple binary numbers 0b01010
1139 if ((c1
== 'b' || c1
== 'B') && (s
[1] == '0' || s
[1] == '1')) {
1140 // 0b101010 is a C++1y / GCC extension.
1141 Diags
.Report(TokLoc
, LangOpts
.CPlusPlus14
1142 ? diag::warn_cxx11_compat_binary_literal
1143 : LangOpts
.CPlusPlus
? diag::ext_binary_literal_cxx14
1144 : diag::ext_binary_literal
);
1146 assert(s
< ThisTokEnd
&& "didn't maximally munch?");
1149 s
= SkipBinaryDigits(s
);
1150 if (s
== ThisTokEnd
) {
1152 } else if (isHexDigit(*s
) &&
1153 !isValidUDSuffix(LangOpts
, StringRef(s
, ThisTokEnd
- s
))) {
1154 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
,
1156 diag::err_invalid_digit
)
1157 << StringRef(s
, 1) << 2;
1160 // Other suffixes will be diagnosed by the caller.
1164 // For now, the radix is set to 8. If we discover that we have a
1165 // floating point constant, the radix will change to 10. Octal floating
1166 // point constants are not permitted (only decimal and hexadecimal).
1168 const char *PossibleNewDigitStart
= s
;
1169 s
= SkipOctalDigits(s
);
1170 // When the value is 0 followed by a suffix (like 0wb), we want to leave 0
1171 // as the start of the digits. So if skipping octal digits does not skip
1172 // anything, we leave the digit start where it was.
1173 if (s
!= PossibleNewDigitStart
)
1174 DigitsBegin
= PossibleNewDigitStart
;
1176 if (s
== ThisTokEnd
)
1177 return; // Done, simple octal number like 01234
1179 // If we have some other non-octal digit that *is* a decimal digit, see if
1180 // this is part of a floating point number like 094.123 or 09e1.
1182 const char *EndDecimal
= SkipDigits(s
);
1183 if (EndDecimal
[0] == '.' || EndDecimal
[0] == 'e' || EndDecimal
[0] == 'E') {
1189 ParseDecimalOrOctalCommon(TokLoc
);
1192 static bool alwaysFitsInto64Bits(unsigned Radix
, unsigned NumDigits
) {
1195 return NumDigits
<= 64;
1197 return NumDigits
<= 64 / 3; // Digits are groups of 3 bits.
1199 return NumDigits
<= 19; // floor(log10(2^64))
1201 return NumDigits
<= 64 / 4; // Digits are groups of 4 bits.
1203 llvm_unreachable("impossible Radix");
1207 /// GetIntegerValue - Convert this numeric literal value to an APInt that
1208 /// matches Val's input width. If there is an overflow, set Val to the low bits
1209 /// of the result and return true. Otherwise, return false.
1210 bool NumericLiteralParser::GetIntegerValue(llvm::APInt
&Val
) {
1211 // Fast path: Compute a conservative bound on the maximum number of
1212 // bits per digit in this radix. If we can't possibly overflow a
1213 // uint64 based on that bound then do the simple conversion to
1214 // integer. This avoids the expensive overflow checking below, and
1215 // handles the common cases that matter (small decimal integers and
1216 // hex/octal values which don't overflow).
1217 const unsigned NumDigits
= SuffixBegin
- DigitsBegin
;
1218 if (alwaysFitsInto64Bits(radix
, NumDigits
)) {
1220 for (const char *Ptr
= DigitsBegin
; Ptr
!= SuffixBegin
; ++Ptr
)
1221 if (!isDigitSeparator(*Ptr
))
1222 N
= N
* radix
+ llvm::hexDigitValue(*Ptr
);
1224 // This will truncate the value to Val's input width. Simply check
1225 // for overflow by comparing.
1227 return Val
.getZExtValue() != N
;
1231 const char *Ptr
= DigitsBegin
;
1233 llvm::APInt
RadixVal(Val
.getBitWidth(), radix
);
1234 llvm::APInt
CharVal(Val
.getBitWidth(), 0);
1235 llvm::APInt OldVal
= Val
;
1237 bool OverflowOccurred
= false;
1238 while (Ptr
< SuffixBegin
) {
1239 if (isDigitSeparator(*Ptr
)) {
1244 unsigned C
= llvm::hexDigitValue(*Ptr
++);
1246 // If this letter is out of bound for this radix, reject it.
1247 assert(C
< radix
&& "NumericLiteralParser ctor should have rejected this");
1251 // Add the digit to the value in the appropriate radix. If adding in digits
1252 // made the value smaller, then this overflowed.
1255 // Multiply by radix, did overflow occur on the multiply?
1257 OverflowOccurred
|= Val
.udiv(RadixVal
) != OldVal
;
1259 // Add value, did overflow occur on the value?
1260 // (a + b) ult b <=> overflow
1262 OverflowOccurred
|= Val
.ult(CharVal
);
1264 return OverflowOccurred
;
1267 llvm::APFloat::opStatus
1268 NumericLiteralParser::GetFloatValue(llvm::APFloat
&Result
) {
1269 using llvm::APFloat
;
1271 unsigned n
= std::min(SuffixBegin
- ThisTokBegin
, ThisTokEnd
- ThisTokBegin
);
1273 llvm::SmallString
<16> Buffer
;
1274 StringRef
Str(ThisTokBegin
, n
);
1275 if (Str
.contains('\'')) {
1277 std::remove_copy_if(Str
.begin(), Str
.end(), std::back_inserter(Buffer
),
1283 Result
.convertFromString(Str
, APFloat::rmNearestTiesToEven
);
1284 assert(StatusOrErr
&& "Invalid floating point representation");
1285 return !errorToBool(StatusOrErr
.takeError()) ? *StatusOrErr
1286 : APFloat::opInvalidOp
;
1289 static inline bool IsExponentPart(char c
) {
1290 return c
== 'p' || c
== 'P' || c
== 'e' || c
== 'E';
1293 bool NumericLiteralParser::GetFixedPointValue(llvm::APInt
&StoreVal
, unsigned Scale
) {
1294 assert(radix
== 16 || radix
== 10);
1296 // Find how many digits are needed to store the whole literal.
1297 unsigned NumDigits
= SuffixBegin
- DigitsBegin
;
1298 if (saw_period
) --NumDigits
;
1300 // Initial scan of the exponent if it exists
1301 bool ExpOverflowOccurred
= false;
1302 bool NegativeExponent
= false;
1303 const char *ExponentBegin
;
1304 uint64_t Exponent
= 0;
1305 int64_t BaseShift
= 0;
1307 const char *Ptr
= DigitsBegin
;
1309 while (!IsExponentPart(*Ptr
)) ++Ptr
;
1310 ExponentBegin
= Ptr
;
1312 NegativeExponent
= *Ptr
== '-';
1313 if (NegativeExponent
) ++Ptr
;
1315 unsigned NumExpDigits
= SuffixBegin
- Ptr
;
1316 if (alwaysFitsInto64Bits(radix
, NumExpDigits
)) {
1317 llvm::StringRef
ExpStr(Ptr
, NumExpDigits
);
1318 llvm::APInt
ExpInt(/*numBits=*/64, ExpStr
, /*radix=*/10);
1319 Exponent
= ExpInt
.getZExtValue();
1321 ExpOverflowOccurred
= true;
1324 if (NegativeExponent
) BaseShift
-= Exponent
;
1325 else BaseShift
+= Exponent
;
1328 // Number of bits needed for decimal literal is
1329 // ceil(NumDigits * log2(10)) Integral part
1330 // + Scale Fractional part
1331 // + ceil(Exponent * log2(10)) Exponent
1332 // --------------------------------------------------
1333 // ceil((NumDigits + Exponent) * log2(10)) + Scale
1335 // But for simplicity in handling integers, we can round up log2(10) to 4,
1337 // 4 * (NumDigits + Exponent) + Scale
1339 // Number of digits needed for hexadecimal literal is
1340 // 4 * NumDigits Integral part
1341 // + Scale Fractional part
1342 // + Exponent Exponent
1343 // --------------------------------------------------
1344 // (4 * NumDigits) + Scale + Exponent
1345 uint64_t NumBitsNeeded
;
1347 NumBitsNeeded
= 4 * (NumDigits
+ Exponent
) + Scale
;
1349 NumBitsNeeded
= 4 * NumDigits
+ Exponent
+ Scale
;
1351 if (NumBitsNeeded
> std::numeric_limits
<unsigned>::max())
1352 ExpOverflowOccurred
= true;
1353 llvm::APInt
Val(static_cast<unsigned>(NumBitsNeeded
), 0, /*isSigned=*/false);
1355 bool FoundDecimal
= false;
1357 int64_t FractBaseShift
= 0;
1358 const char *End
= saw_exponent
? ExponentBegin
: SuffixBegin
;
1359 for (const char *Ptr
= DigitsBegin
; Ptr
< End
; ++Ptr
) {
1361 FoundDecimal
= true;
1365 // Normal reading of an integer
1366 unsigned C
= llvm::hexDigitValue(*Ptr
);
1367 assert(C
< radix
&& "NumericLiteralParser ctor should have rejected this");
1373 // Keep track of how much we will need to adjust this value by from the
1374 // number of digits past the radix point.
1378 // For a radix of 16, we will be multiplying by 2 instead of 16.
1379 if (radix
== 16) FractBaseShift
*= 4;
1380 BaseShift
+= FractBaseShift
;
1384 uint64_t Base
= (radix
== 16) ? 2 : 10;
1385 if (BaseShift
> 0) {
1386 for (int64_t i
= 0; i
< BaseShift
; ++i
) {
1389 } else if (BaseShift
< 0) {
1390 for (int64_t i
= BaseShift
; i
< 0 && !Val
.isZero(); ++i
)
1391 Val
= Val
.udiv(Base
);
1394 bool IntOverflowOccurred
= false;
1395 auto MaxVal
= llvm::APInt::getMaxValue(StoreVal
.getBitWidth());
1396 if (Val
.getBitWidth() > StoreVal
.getBitWidth()) {
1397 IntOverflowOccurred
|= Val
.ugt(MaxVal
.zext(Val
.getBitWidth()));
1398 StoreVal
= Val
.trunc(StoreVal
.getBitWidth());
1399 } else if (Val
.getBitWidth() < StoreVal
.getBitWidth()) {
1400 IntOverflowOccurred
|= Val
.zext(MaxVal
.getBitWidth()).ugt(MaxVal
);
1401 StoreVal
= Val
.zext(StoreVal
.getBitWidth());
1406 return IntOverflowOccurred
|| ExpOverflowOccurred
;
1410 /// user-defined-character-literal: [C++11 lex.ext]
1411 /// character-literal ud-suffix
1414 /// character-literal: [C++11 lex.ccon]
1415 /// ' c-char-sequence '
1416 /// u' c-char-sequence '
1417 /// U' c-char-sequence '
1418 /// L' c-char-sequence '
1419 /// u8' c-char-sequence ' [C++1z lex.ccon]
1420 /// c-char-sequence:
1422 /// c-char-sequence c-char
1424 /// any member of the source character set except the single-quote ',
1425 /// backslash \, or new-line character
1427 /// universal-character-name
1428 /// escape-sequence:
1429 /// simple-escape-sequence
1430 /// octal-escape-sequence
1431 /// hexadecimal-escape-sequence
1432 /// simple-escape-sequence:
1433 /// one of \' \" \? \\ \a \b \f \n \r \t \v
1434 /// octal-escape-sequence:
1436 /// \ octal-digit octal-digit
1437 /// \ octal-digit octal-digit octal-digit
1438 /// hexadecimal-escape-sequence:
1439 /// \x hexadecimal-digit
1440 /// hexadecimal-escape-sequence hexadecimal-digit
1441 /// universal-character-name: [C++11 lex.charset]
1443 /// \U hex-quad hex-quad
1445 /// hex-digit hex-digit hex-digit hex-digit
1448 CharLiteralParser::CharLiteralParser(const char *begin
, const char *end
,
1449 SourceLocation Loc
, Preprocessor
&PP
,
1450 tok::TokenKind kind
) {
1451 // At this point we know that the character matches the regex "(L|u|U)?'.*'".
1456 const char *TokBegin
= begin
;
1458 // Skip over wide character determinant.
1459 if (Kind
!= tok::char_constant
)
1461 if (Kind
== tok::utf8_char_constant
)
1464 // Skip over the entry quote.
1465 if (begin
[0] != '\'') {
1466 PP
.Diag(Loc
, diag::err_lexing_char
);
1473 // Remove an optional ud-suffix.
1474 if (end
[-1] != '\'') {
1475 const char *UDSuffixEnd
= end
;
1478 } while (end
[-1] != '\'');
1479 // FIXME: Don't bother with this if !tok.hasUCN().
1480 expandUCNs(UDSuffixBuf
, StringRef(end
, UDSuffixEnd
- end
));
1481 UDSuffixOffset
= end
- TokBegin
;
1484 // Trim the ending quote.
1485 assert(end
!= begin
&& "Invalid token lexed");
1488 // FIXME: The "Value" is an uint64_t so we can handle char literals of
1490 // FIXME: This extensively assumes that 'char' is 8-bits.
1491 assert(PP
.getTargetInfo().getCharWidth() == 8 &&
1492 "Assumes char is 8 bits");
1493 assert(PP
.getTargetInfo().getIntWidth() <= 64 &&
1494 (PP
.getTargetInfo().getIntWidth() & 7) == 0 &&
1495 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1496 assert(PP
.getTargetInfo().getWCharWidth() <= 64 &&
1497 "Assumes sizeof(wchar) on target is <= 64");
1499 SmallVector
<uint32_t, 4> codepoint_buffer
;
1500 codepoint_buffer
.resize(end
- begin
);
1501 uint32_t *buffer_begin
= &codepoint_buffer
.front();
1502 uint32_t *buffer_end
= buffer_begin
+ codepoint_buffer
.size();
1504 // Unicode escapes representing characters that cannot be correctly
1505 // represented in a single code unit are disallowed in character literals
1506 // by this implementation.
1507 uint32_t largest_character_for_kind
;
1508 if (tok::wide_char_constant
== Kind
) {
1509 largest_character_for_kind
=
1510 0xFFFFFFFFu
>> (32-PP
.getTargetInfo().getWCharWidth());
1511 } else if (tok::utf8_char_constant
== Kind
) {
1512 largest_character_for_kind
= 0x7F;
1513 } else if (tok::utf16_char_constant
== Kind
) {
1514 largest_character_for_kind
= 0xFFFF;
1515 } else if (tok::utf32_char_constant
== Kind
) {
1516 largest_character_for_kind
= 0x10FFFF;
1518 largest_character_for_kind
= 0x7Fu
;
1521 while (begin
!= end
) {
1522 // Is this a span of non-escape characters?
1523 if (begin
[0] != '\\') {
1524 char const *start
= begin
;
1527 } while (begin
!= end
&& *begin
!= '\\');
1529 char const *tmp_in_start
= start
;
1530 uint32_t *tmp_out_start
= buffer_begin
;
1531 llvm::ConversionResult res
=
1532 llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8
const **>(&start
),
1533 reinterpret_cast<llvm::UTF8
const *>(begin
),
1534 &buffer_begin
, buffer_end
, llvm::strictConversion
);
1535 if (res
!= llvm::conversionOK
) {
1536 // If we see bad encoding for unprefixed character literals, warn and
1537 // simply copy the byte values, for compatibility with gcc and
1538 // older versions of clang.
1539 bool NoErrorOnBadEncoding
= isAscii();
1540 unsigned Msg
= diag::err_bad_character_encoding
;
1541 if (NoErrorOnBadEncoding
)
1542 Msg
= diag::warn_bad_character_encoding
;
1544 if (NoErrorOnBadEncoding
) {
1545 start
= tmp_in_start
;
1546 buffer_begin
= tmp_out_start
;
1547 for (; start
!= begin
; ++start
, ++buffer_begin
)
1548 *buffer_begin
= static_cast<uint8_t>(*start
);
1553 for (; tmp_out_start
< buffer_begin
; ++tmp_out_start
) {
1554 if (*tmp_out_start
> largest_character_for_kind
) {
1556 PP
.Diag(Loc
, diag::err_character_too_large
);
1563 // Is this a Universal Character Name escape?
1564 if (begin
[1] == 'u' || begin
[1] == 'U') {
1565 unsigned short UcnLen
= 0;
1566 if (!ProcessUCNEscape(TokBegin
, begin
, end
, *buffer_begin
, UcnLen
,
1567 FullSourceLoc(Loc
, PP
.getSourceManager()),
1568 &PP
.getDiagnostics(), PP
.getLangOpts(), true)) {
1570 } else if (*buffer_begin
> largest_character_for_kind
) {
1572 PP
.Diag(Loc
, diag::err_character_too_large
);
1578 unsigned CharWidth
= getCharWidth(Kind
, PP
.getTargetInfo());
1580 ProcessCharEscape(TokBegin
, begin
, end
, HadError
,
1581 FullSourceLoc(Loc
,PP
.getSourceManager()),
1582 CharWidth
, &PP
.getDiagnostics(), PP
.getLangOpts());
1583 *buffer_begin
++ = result
;
1586 unsigned NumCharsSoFar
= buffer_begin
- &codepoint_buffer
.front();
1588 if (NumCharsSoFar
> 1) {
1589 if (isAscii() && NumCharsSoFar
== 4)
1590 PP
.Diag(Loc
, diag::warn_four_char_character_literal
);
1592 PP
.Diag(Loc
, diag::warn_multichar_character_literal
);
1594 PP
.Diag(Loc
, diag::err_multichar_character_literal
) << (isWide() ? 0 : 1);
1599 IsMultiChar
= false;
1602 llvm::APInt
LitVal(PP
.getTargetInfo().getIntWidth(), 0);
1604 // Narrow character literals act as though their value is concatenated
1605 // in this implementation, but warn on overflow.
1606 bool multi_char_too_long
= false;
1607 if (isAscii() && isMultiChar()) {
1609 for (size_t i
= 0; i
< NumCharsSoFar
; ++i
) {
1610 // check for enough leading zeros to shift into
1611 multi_char_too_long
|= (LitVal
.countLeadingZeros() < 8);
1613 LitVal
= LitVal
+ (codepoint_buffer
[i
] & 0xFF);
1615 } else if (NumCharsSoFar
> 0) {
1616 // otherwise just take the last character
1617 LitVal
= buffer_begin
[-1];
1620 if (!HadError
&& multi_char_too_long
) {
1621 PP
.Diag(Loc
, diag::warn_char_constant_too_large
);
1624 // Transfer the value from APInt to uint64_t
1625 Value
= LitVal
.getZExtValue();
1627 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1628 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
1629 // character constants are not sign extended in the this implementation:
1630 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
1631 if (isAscii() && NumCharsSoFar
== 1 && (Value
& 128) &&
1632 PP
.getLangOpts().CharIsSigned
)
1633 Value
= (signed char)Value
;
1637 /// string-literal: [C++0x lex.string]
1638 /// encoding-prefix " [s-char-sequence] "
1639 /// encoding-prefix R raw-string
1640 /// encoding-prefix:
1645 /// s-char-sequence:
1647 /// s-char-sequence s-char
1649 /// any member of the source character set except the double-quote ",
1650 /// backslash \, or new-line character
1652 /// universal-character-name
1654 /// " d-char-sequence ( r-char-sequence ) d-char-sequence "
1655 /// r-char-sequence:
1657 /// r-char-sequence r-char
1659 /// any member of the source character set, except a right parenthesis )
1660 /// followed by the initial d-char-sequence (which may be empty)
1661 /// followed by a double quote ".
1662 /// d-char-sequence:
1664 /// d-char-sequence d-char
1666 /// any member of the basic source character set except:
1667 /// space, the left parenthesis (, the right parenthesis ),
1668 /// the backslash \, and the control characters representing horizontal
1669 /// tab, vertical tab, form feed, and newline.
1670 /// escape-sequence: [C++0x lex.ccon]
1671 /// simple-escape-sequence
1672 /// octal-escape-sequence
1673 /// hexadecimal-escape-sequence
1674 /// simple-escape-sequence:
1675 /// one of \' \" \? \\ \a \b \f \n \r \t \v
1676 /// octal-escape-sequence:
1678 /// \ octal-digit octal-digit
1679 /// \ octal-digit octal-digit octal-digit
1680 /// hexadecimal-escape-sequence:
1681 /// \x hexadecimal-digit
1682 /// hexadecimal-escape-sequence hexadecimal-digit
1683 /// universal-character-name:
1685 /// \U hex-quad hex-quad
1687 /// hex-digit hex-digit hex-digit hex-digit
1690 StringLiteralParser::
1691 StringLiteralParser(ArrayRef
<Token
> StringToks
,
1693 : SM(PP
.getSourceManager()), Features(PP
.getLangOpts()),
1694 Target(PP
.getTargetInfo()), Diags(&PP
.getDiagnostics()),
1695 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown
),
1696 ResultPtr(ResultBuf
.data()), hadError(false), Pascal(false) {
1700 void StringLiteralParser::init(ArrayRef
<Token
> StringToks
){
1701 // The literal token may have come from an invalid source location (e.g. due
1702 // to a PCH error), in which case the token length will be 0.
1703 if (StringToks
.empty() || StringToks
[0].getLength() < 2)
1704 return DiagnoseLexingError(SourceLocation());
1706 // Scan all of the string portions, remember the max individual token length,
1707 // computing a bound on the concatenated string length, and see whether any
1708 // piece is a wide-string. If any of the string portions is a wide-string
1709 // literal, the result is a wide-string literal [C99 6.4.5p4].
1710 assert(!StringToks
.empty() && "expected at least one token");
1711 MaxTokenLength
= StringToks
[0].getLength();
1712 assert(StringToks
[0].getLength() >= 2 && "literal token is invalid!");
1713 SizeBound
= StringToks
[0].getLength()-2; // -2 for "".
1714 Kind
= StringToks
[0].getKind();
1718 // Implement Translation Phase #6: concatenation of string literals
1719 /// (C99 5.1.1.2p1). The common case is only one string fragment.
1720 for (unsigned i
= 1; i
!= StringToks
.size(); ++i
) {
1721 if (StringToks
[i
].getLength() < 2)
1722 return DiagnoseLexingError(StringToks
[i
].getLocation());
1724 // The string could be shorter than this if it needs cleaning, but this is a
1725 // reasonable bound, which is all we need.
1726 assert(StringToks
[i
].getLength() >= 2 && "literal token is invalid!");
1727 SizeBound
+= StringToks
[i
].getLength()-2; // -2 for "".
1729 // Remember maximum string piece length.
1730 if (StringToks
[i
].getLength() > MaxTokenLength
)
1731 MaxTokenLength
= StringToks
[i
].getLength();
1733 // Remember if we see any wide or utf-8/16/32 strings.
1734 // Also check for illegal concatenations.
1735 if (StringToks
[i
].isNot(Kind
) && StringToks
[i
].isNot(tok::string_literal
)) {
1737 Kind
= StringToks
[i
].getKind();
1740 Diags
->Report(StringToks
[i
].getLocation(),
1741 diag::err_unsupported_string_concat
);
1747 // Include space for the null terminator.
1750 // TODO: K&R warning: "traditional C rejects string constant concatenation"
1752 // Get the width in bytes of char/wchar_t/char16_t/char32_t
1753 CharByteWidth
= getCharWidth(Kind
, Target
);
1754 assert((CharByteWidth
& 7) == 0 && "Assumes character size is byte multiple");
1757 // The output buffer size needs to be large enough to hold wide characters.
1758 // This is a worst-case assumption which basically corresponds to L"" "long".
1759 SizeBound
*= CharByteWidth
;
1761 // Size the temporary buffer to hold the result string data.
1762 ResultBuf
.resize(SizeBound
);
1764 // Likewise, but for each string piece.
1765 SmallString
<512> TokenBuf
;
1766 TokenBuf
.resize(MaxTokenLength
);
1768 // Loop over all the strings, getting their spelling, and expanding them to
1769 // wide strings as appropriate.
1770 ResultPtr
= &ResultBuf
[0]; // Next byte to fill in.
1774 SourceLocation UDSuffixTokLoc
;
1776 for (unsigned i
= 0, e
= StringToks
.size(); i
!= e
; ++i
) {
1777 const char *ThisTokBuf
= &TokenBuf
[0];
1778 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1779 // that ThisTokBuf points to a buffer that is big enough for the whole token
1780 // and 'spelled' tokens can only shrink.
1781 bool StringInvalid
= false;
1782 unsigned ThisTokLen
=
1783 Lexer::getSpelling(StringToks
[i
], ThisTokBuf
, SM
, Features
,
1786 return DiagnoseLexingError(StringToks
[i
].getLocation());
1788 const char *ThisTokBegin
= ThisTokBuf
;
1789 const char *ThisTokEnd
= ThisTokBuf
+ThisTokLen
;
1791 // Remove an optional ud-suffix.
1792 if (ThisTokEnd
[-1] != '"') {
1793 const char *UDSuffixEnd
= ThisTokEnd
;
1796 } while (ThisTokEnd
[-1] != '"');
1798 StringRef
UDSuffix(ThisTokEnd
, UDSuffixEnd
- ThisTokEnd
);
1800 if (UDSuffixBuf
.empty()) {
1801 if (StringToks
[i
].hasUCN())
1802 expandUCNs(UDSuffixBuf
, UDSuffix
);
1804 UDSuffixBuf
.assign(UDSuffix
);
1806 UDSuffixOffset
= ThisTokEnd
- ThisTokBuf
;
1807 UDSuffixTokLoc
= StringToks
[i
].getLocation();
1809 SmallString
<32> ExpandedUDSuffix
;
1810 if (StringToks
[i
].hasUCN()) {
1811 expandUCNs(ExpandedUDSuffix
, UDSuffix
);
1812 UDSuffix
= ExpandedUDSuffix
;
1815 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1816 // result of a concatenation involving at least one user-defined-string-
1817 // literal, all the participating user-defined-string-literals shall
1818 // have the same ud-suffix.
1819 if (UDSuffixBuf
!= UDSuffix
) {
1821 SourceLocation TokLoc
= StringToks
[i
].getLocation();
1822 Diags
->Report(TokLoc
, diag::err_string_concat_mixed_suffix
)
1823 << UDSuffixBuf
<< UDSuffix
1824 << SourceRange(UDSuffixTokLoc
, UDSuffixTokLoc
)
1825 << SourceRange(TokLoc
, TokLoc
);
1832 // Strip the end quote.
1835 // TODO: Input character set mapping support.
1837 // Skip marker for wide or unicode strings.
1838 if (ThisTokBuf
[0] == 'L' || ThisTokBuf
[0] == 'u' || ThisTokBuf
[0] == 'U') {
1840 // Skip 8 of u8 marker for utf8 strings.
1841 if (ThisTokBuf
[0] == '8')
1845 // Check for raw string
1846 if (ThisTokBuf
[0] == 'R') {
1847 if (ThisTokBuf
[1] != '"') {
1848 // The file may have come from PCH and then changed after loading the
1849 // PCH; Fail gracefully.
1850 return DiagnoseLexingError(StringToks
[i
].getLocation());
1852 ThisTokBuf
+= 2; // skip R"
1854 // C++11 [lex.string]p2: A `d-char-sequence` shall consist of at most 16
1856 constexpr unsigned MaxRawStrDelimLen
= 16;
1858 const char *Prefix
= ThisTokBuf
;
1859 while (static_cast<unsigned>(ThisTokBuf
- Prefix
) < MaxRawStrDelimLen
&&
1860 ThisTokBuf
[0] != '(')
1862 if (ThisTokBuf
[0] != '(')
1863 return DiagnoseLexingError(StringToks
[i
].getLocation());
1864 ++ThisTokBuf
; // skip '('
1866 // Remove same number of characters from the end
1867 ThisTokEnd
-= ThisTokBuf
- Prefix
;
1868 if (ThisTokEnd
< ThisTokBuf
)
1869 return DiagnoseLexingError(StringToks
[i
].getLocation());
1871 // C++14 [lex.string]p4: A source-file new-line in a raw string literal
1872 // results in a new-line in the resulting execution string-literal.
1873 StringRef
RemainingTokenSpan(ThisTokBuf
, ThisTokEnd
- ThisTokBuf
);
1874 while (!RemainingTokenSpan
.empty()) {
1875 // Split the string literal on \r\n boundaries.
1876 size_t CRLFPos
= RemainingTokenSpan
.find("\r\n");
1877 StringRef BeforeCRLF
= RemainingTokenSpan
.substr(0, CRLFPos
);
1878 StringRef AfterCRLF
= RemainingTokenSpan
.substr(CRLFPos
);
1880 // Copy everything before the \r\n sequence into the string literal.
1881 if (CopyStringFragment(StringToks
[i
], ThisTokBegin
, BeforeCRLF
))
1884 // Point into the \n inside the \r\n sequence and operate on the
1885 // remaining portion of the literal.
1886 RemainingTokenSpan
= AfterCRLF
.substr(1);
1889 if (ThisTokBuf
[0] != '"') {
1890 // The file may have come from PCH and then changed after loading the
1891 // PCH; Fail gracefully.
1892 return DiagnoseLexingError(StringToks
[i
].getLocation());
1894 ++ThisTokBuf
; // skip "
1896 // Check if this is a pascal string
1897 if (Features
.PascalStrings
&& ThisTokBuf
+ 1 != ThisTokEnd
&&
1898 ThisTokBuf
[0] == '\\' && ThisTokBuf
[1] == 'p') {
1900 // If the \p sequence is found in the first token, we have a pascal string
1901 // Otherwise, if we already have a pascal string, ignore the first \p
1909 while (ThisTokBuf
!= ThisTokEnd
) {
1910 // Is this a span of non-escape characters?
1911 if (ThisTokBuf
[0] != '\\') {
1912 const char *InStart
= ThisTokBuf
;
1915 } while (ThisTokBuf
!= ThisTokEnd
&& ThisTokBuf
[0] != '\\');
1917 // Copy the character span over.
1918 if (CopyStringFragment(StringToks
[i
], ThisTokBegin
,
1919 StringRef(InStart
, ThisTokBuf
- InStart
)))
1923 // Is this a Universal Character Name escape?
1924 if (ThisTokBuf
[1] == 'u' || ThisTokBuf
[1] == 'U') {
1925 EncodeUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
,
1926 ResultPtr
, hadError
,
1927 FullSourceLoc(StringToks
[i
].getLocation(), SM
),
1928 CharByteWidth
, Diags
, Features
);
1931 // Otherwise, this is a non-UCN escape character. Process it.
1932 unsigned ResultChar
=
1933 ProcessCharEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, hadError
,
1934 FullSourceLoc(StringToks
[i
].getLocation(), SM
),
1935 CharByteWidth
*8, Diags
, Features
);
1937 if (CharByteWidth
== 4) {
1938 // FIXME: Make the type of the result buffer correct instead of
1939 // using reinterpret_cast.
1940 llvm::UTF32
*ResultWidePtr
= reinterpret_cast<llvm::UTF32
*>(ResultPtr
);
1941 *ResultWidePtr
= ResultChar
;
1943 } else if (CharByteWidth
== 2) {
1944 // FIXME: Make the type of the result buffer correct instead of
1945 // using reinterpret_cast.
1946 llvm::UTF16
*ResultWidePtr
= reinterpret_cast<llvm::UTF16
*>(ResultPtr
);
1947 *ResultWidePtr
= ResultChar
& 0xFFFF;
1950 assert(CharByteWidth
== 1 && "Unexpected char width");
1951 *ResultPtr
++ = ResultChar
& 0xFF;
1958 if (CharByteWidth
== 4) {
1959 // FIXME: Make the type of the result buffer correct instead of
1960 // using reinterpret_cast.
1961 llvm::UTF32
*ResultWidePtr
= reinterpret_cast<llvm::UTF32
*>(ResultBuf
.data());
1962 ResultWidePtr
[0] = GetNumStringChars() - 1;
1963 } else if (CharByteWidth
== 2) {
1964 // FIXME: Make the type of the result buffer correct instead of
1965 // using reinterpret_cast.
1966 llvm::UTF16
*ResultWidePtr
= reinterpret_cast<llvm::UTF16
*>(ResultBuf
.data());
1967 ResultWidePtr
[0] = GetNumStringChars() - 1;
1969 assert(CharByteWidth
== 1 && "Unexpected char width");
1970 ResultBuf
[0] = GetNumStringChars() - 1;
1973 // Verify that pascal strings aren't too large.
1974 if (GetStringLength() > 256) {
1976 Diags
->Report(StringToks
.front().getLocation(),
1977 diag::err_pascal_string_too_long
)
1978 << SourceRange(StringToks
.front().getLocation(),
1979 StringToks
.back().getLocation());
1984 // Complain if this string literal has too many characters.
1985 unsigned MaxChars
= Features
.CPlusPlus
? 65536 : Features
.C99
? 4095 : 509;
1987 if (GetNumStringChars() > MaxChars
)
1988 Diags
->Report(StringToks
.front().getLocation(),
1989 diag::ext_string_too_long
)
1990 << GetNumStringChars() << MaxChars
1991 << (Features
.CPlusPlus
? 2 : Features
.C99
? 1 : 0)
1992 << SourceRange(StringToks
.front().getLocation(),
1993 StringToks
.back().getLocation());
1997 static const char *resyncUTF8(const char *Err
, const char *End
) {
2000 End
= Err
+ std::min
<unsigned>(llvm::getNumBytesForUTF8(*Err
), End
-Err
);
2001 while (++Err
!= End
&& (*Err
& 0xC0) == 0x80)
2006 /// This function copies from Fragment, which is a sequence of bytes
2007 /// within Tok's contents (which begin at TokBegin) into ResultPtr.
2008 /// Performs widening for multi-byte characters.
2009 bool StringLiteralParser::CopyStringFragment(const Token
&Tok
,
2010 const char *TokBegin
,
2011 StringRef Fragment
) {
2012 const llvm::UTF8
*ErrorPtrTmp
;
2013 if (ConvertUTF8toWide(CharByteWidth
, Fragment
, ResultPtr
, ErrorPtrTmp
))
2016 // If we see bad encoding for unprefixed string literals, warn and
2017 // simply copy the byte values, for compatibility with gcc and older
2018 // versions of clang.
2019 bool NoErrorOnBadEncoding
= isAscii();
2020 if (NoErrorOnBadEncoding
) {
2021 memcpy(ResultPtr
, Fragment
.data(), Fragment
.size());
2022 ResultPtr
+= Fragment
.size();
2026 const char *ErrorPtr
= reinterpret_cast<const char *>(ErrorPtrTmp
);
2028 FullSourceLoc
SourceLoc(Tok
.getLocation(), SM
);
2029 const DiagnosticBuilder
&Builder
=
2030 Diag(Diags
, Features
, SourceLoc
, TokBegin
,
2031 ErrorPtr
, resyncUTF8(ErrorPtr
, Fragment
.end()),
2032 NoErrorOnBadEncoding
? diag::warn_bad_string_encoding
2033 : diag::err_bad_string_encoding
);
2035 const char *NextStart
= resyncUTF8(ErrorPtr
, Fragment
.end());
2036 StringRef
NextFragment(NextStart
, Fragment
.end()-NextStart
);
2038 // Decode into a dummy buffer.
2039 SmallString
<512> Dummy
;
2040 Dummy
.reserve(Fragment
.size() * CharByteWidth
);
2041 char *Ptr
= Dummy
.data();
2043 while (!ConvertUTF8toWide(CharByteWidth
, NextFragment
, Ptr
, ErrorPtrTmp
)) {
2044 const char *ErrorPtr
= reinterpret_cast<const char *>(ErrorPtrTmp
);
2045 NextStart
= resyncUTF8(ErrorPtr
, Fragment
.end());
2046 Builder
<< MakeCharSourceRange(Features
, SourceLoc
, TokBegin
,
2047 ErrorPtr
, NextStart
);
2048 NextFragment
= StringRef(NextStart
, Fragment
.end()-NextStart
);
2051 return !NoErrorOnBadEncoding
;
2054 void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc
) {
2057 Diags
->Report(Loc
, diag::err_lexing_string
);
2060 /// getOffsetOfStringByte - This function returns the offset of the
2061 /// specified byte of the string data represented by Token. This handles
2062 /// advancing over escape sequences in the string.
2063 unsigned StringLiteralParser::getOffsetOfStringByte(const Token
&Tok
,
2064 unsigned ByteNo
) const {
2065 // Get the spelling of the token.
2066 SmallString
<32> SpellingBuffer
;
2067 SpellingBuffer
.resize(Tok
.getLength());
2069 bool StringInvalid
= false;
2070 const char *SpellingPtr
= &SpellingBuffer
[0];
2071 unsigned TokLen
= Lexer::getSpelling(Tok
, SpellingPtr
, SM
, Features
,
2076 const char *SpellingStart
= SpellingPtr
;
2077 const char *SpellingEnd
= SpellingPtr
+TokLen
;
2079 // Handle UTF-8 strings just like narrow strings.
2080 if (SpellingPtr
[0] == 'u' && SpellingPtr
[1] == '8')
2083 assert(SpellingPtr
[0] != 'L' && SpellingPtr
[0] != 'u' &&
2084 SpellingPtr
[0] != 'U' && "Doesn't handle wide or utf strings yet");
2086 // For raw string literals, this is easy.
2087 if (SpellingPtr
[0] == 'R') {
2088 assert(SpellingPtr
[1] == '"' && "Should be a raw string literal!");
2091 while (*SpellingPtr
!= '(') {
2093 assert(SpellingPtr
< SpellingEnd
&& "Missing ( for raw string literal");
2097 return SpellingPtr
- SpellingStart
+ ByteNo
;
2100 // Skip over the leading quote
2101 assert(SpellingPtr
[0] == '"' && "Should be a string literal!");
2104 // Skip over bytes until we find the offset we're looking for.
2106 assert(SpellingPtr
< SpellingEnd
&& "Didn't find byte offset!");
2108 // Step over non-escapes simply.
2109 if (*SpellingPtr
!= '\\') {
2115 // Otherwise, this is an escape character. Advance over it.
2116 bool HadError
= false;
2117 if (SpellingPtr
[1] == 'u' || SpellingPtr
[1] == 'U') {
2118 const char *EscapePtr
= SpellingPtr
;
2119 unsigned Len
= MeasureUCNEscape(SpellingStart
, SpellingPtr
, SpellingEnd
,
2120 1, Features
, HadError
);
2122 // ByteNo is somewhere within the escape sequence.
2123 SpellingPtr
= EscapePtr
;
2128 ProcessCharEscape(SpellingStart
, SpellingPtr
, SpellingEnd
, HadError
,
2129 FullSourceLoc(Tok
.getLocation(), SM
),
2130 CharByteWidth
*8, Diags
, Features
);
2133 assert(!HadError
&& "This method isn't valid on erroneous strings");
2136 return SpellingPtr
-SpellingStart
;
2139 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved
2140 /// suffixes as ud-suffixes, because the diagnostic experience is better if we
2141 /// treat it as an invalid suffix.
2142 bool StringLiteralParser::isValidUDSuffix(const LangOptions
&LangOpts
,
2144 return NumericLiteralParser::isValidUDSuffix(LangOpts
, Suffix
) ||