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"
30 #include "llvm/Support/Unicode.h"
38 using namespace clang
;
40 static unsigned getCharWidth(tok::TokenKind kind
, const TargetInfo
&Target
) {
42 default: llvm_unreachable("Unknown token type!");
43 case tok::char_constant
:
44 case tok::string_literal
:
45 case tok::utf8_char_constant
:
46 case tok::utf8_string_literal
:
47 return Target
.getCharWidth();
48 case tok::wide_char_constant
:
49 case tok::wide_string_literal
:
50 return Target
.getWCharWidth();
51 case tok::utf16_char_constant
:
52 case tok::utf16_string_literal
:
53 return Target
.getChar16Width();
54 case tok::utf32_char_constant
:
55 case tok::utf32_string_literal
:
56 return Target
.getChar32Width();
60 static CharSourceRange
MakeCharSourceRange(const LangOptions
&Features
,
63 const char *TokRangeBegin
,
64 const char *TokRangeEnd
) {
65 SourceLocation Begin
=
66 Lexer::AdvanceToTokenCharacter(TokLoc
, TokRangeBegin
- TokBegin
,
67 TokLoc
.getManager(), Features
);
69 Lexer::AdvanceToTokenCharacter(Begin
, TokRangeEnd
- TokRangeBegin
,
70 TokLoc
.getManager(), Features
);
71 return CharSourceRange::getCharRange(Begin
, End
);
74 /// Produce a diagnostic highlighting some portion of a literal.
76 /// Emits the diagnostic \p DiagID, highlighting the range of characters from
77 /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
78 /// a substring of a spelling buffer for the token beginning at \p TokBegin.
79 static DiagnosticBuilder
Diag(DiagnosticsEngine
*Diags
,
80 const LangOptions
&Features
, FullSourceLoc TokLoc
,
81 const char *TokBegin
, const char *TokRangeBegin
,
82 const char *TokRangeEnd
, unsigned DiagID
) {
83 SourceLocation Begin
=
84 Lexer::AdvanceToTokenCharacter(TokLoc
, TokRangeBegin
- TokBegin
,
85 TokLoc
.getManager(), Features
);
86 return Diags
->Report(Begin
, DiagID
) <<
87 MakeCharSourceRange(Features
, TokLoc
, TokBegin
, TokRangeBegin
, TokRangeEnd
);
90 /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
91 /// either a character or a string literal.
92 static unsigned ProcessCharEscape(const char *ThisTokBegin
,
93 const char *&ThisTokBuf
,
94 const char *ThisTokEnd
, bool &HadError
,
95 FullSourceLoc Loc
, unsigned CharWidth
,
96 DiagnosticsEngine
*Diags
,
97 const LangOptions
&Features
) {
98 const char *EscapeBegin
= ThisTokBuf
;
99 bool Delimited
= false;
100 bool EndDelimiterFound
= false;
102 // Skip the '\' char.
105 // We know that this character can't be off the end of the buffer, because
106 // that would have been \", which would not have been the end of string.
107 unsigned ResultChar
= *ThisTokBuf
++;
108 switch (ResultChar
) {
109 // These map to themselves.
110 case '\\': case '\'': case '"': case '?': break;
112 // These have fixed mappings.
114 // TODO: K&R: the meaning of '\\a' is different in traditional C
122 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
123 diag::ext_nonstandard_escape
) << "e";
128 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
129 diag::ext_nonstandard_escape
) << "E";
147 case 'x': { // Hex escape.
149 if (ThisTokBuf
!= ThisTokEnd
&& *ThisTokBuf
== '{') {
152 if (*ThisTokBuf
== '}') {
153 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
154 diag::err_delimited_escape_empty
);
157 } else if (ThisTokBuf
== ThisTokEnd
|| !isHexDigit(*ThisTokBuf
)) {
159 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
160 diag::err_hex_escape_no_digits
) << "x";
164 // Hex escapes are a maximal series of hex digits.
165 bool Overflow
= false;
166 for (; ThisTokBuf
!= ThisTokEnd
; ++ThisTokBuf
) {
167 if (Delimited
&& *ThisTokBuf
== '}') {
169 EndDelimiterFound
= true;
172 int CharVal
= llvm::hexDigitValue(*ThisTokBuf
);
174 // Non delimited hex escape sequences stop at the first non-hex digit.
179 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
180 diag::err_delimited_escape_invalid
)
181 << StringRef(ThisTokBuf
, 1);
184 // About to shift out a digit?
185 if (ResultChar
& 0xF0000000)
188 ResultChar
|= CharVal
;
190 // See if any bits will be truncated when evaluated as a character.
191 if (CharWidth
!= 32 && (ResultChar
>> CharWidth
) != 0) {
193 ResultChar
&= ~0U >> (32-CharWidth
);
196 // Check for overflow.
197 if (!HadError
&& Overflow
) { // Too many digits to fit in
200 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
201 diag::err_escape_too_large
)
206 case '0': case '1': case '2': case '3':
207 case '4': case '5': case '6': case '7': {
212 // Octal escapes are a series of octal digits with maximum length 3.
213 // "\0123" is a two digit sequence equal to "\012" "3".
214 unsigned NumDigits
= 0;
217 ResultChar
|= *ThisTokBuf
++ - '0';
219 } while (ThisTokBuf
!= ThisTokEnd
&& NumDigits
< 3 &&
220 ThisTokBuf
[0] >= '0' && ThisTokBuf
[0] <= '7');
222 // Check for overflow. Reject '\777', but not L'\777'.
223 if (CharWidth
!= 32 && (ResultChar
>> CharWidth
) != 0) {
225 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
226 diag::err_escape_too_large
) << 1;
227 ResultChar
&= ~0U >> (32-CharWidth
);
232 bool Overflow
= false;
233 if (ThisTokBuf
== ThisTokEnd
|| *ThisTokBuf
!= '{') {
236 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
237 diag::err_delimited_escape_missing_brace
)
245 if (*ThisTokBuf
== '}') {
246 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
247 diag::err_delimited_escape_empty
);
251 while (ThisTokBuf
!= ThisTokEnd
) {
252 if (*ThisTokBuf
== '}') {
253 EndDelimiterFound
= true;
257 if (*ThisTokBuf
< '0' || *ThisTokBuf
> '7') {
260 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
261 diag::err_delimited_escape_invalid
)
262 << StringRef(ThisTokBuf
, 1);
266 if (ResultChar
& 0x020000000)
270 ResultChar
|= *ThisTokBuf
++ - '0';
272 // Check for overflow. Reject '\777', but not L'\777'.
274 (Overflow
|| (CharWidth
!= 32 && (ResultChar
>> CharWidth
) != 0))) {
277 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
278 diag::err_escape_too_large
)
280 ResultChar
&= ~0U >> (32 - CharWidth
);
284 // Otherwise, these are not valid escapes.
285 case '(': case '{': case '[': case '%':
286 // GCC accepts these as extensions. We warn about them as such though.
288 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
289 diag::ext_nonstandard_escape
)
290 << std::string(1, ResultChar
);
296 if (isPrintable(ResultChar
))
297 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
298 diag::ext_unknown_escape
)
299 << std::string(1, ResultChar
);
301 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
302 diag::ext_unknown_escape
)
303 << "x" + llvm::utohexstr(ResultChar
);
307 if (Delimited
&& Diags
) {
308 if (!EndDelimiterFound
)
309 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
312 else if (!HadError
) {
313 Diag(Diags
, Features
, Loc
, ThisTokBegin
, EscapeBegin
, ThisTokBuf
,
314 Features
.CPlusPlus2b
? diag::warn_cxx2b_delimited_escape_sequence
315 : diag::ext_delimited_escape_sequence
)
316 << /*delimited*/ 0 << (Features
.CPlusPlus
? 1 : 0);
323 static void appendCodePoint(unsigned Codepoint
,
324 llvm::SmallVectorImpl
<char> &Str
) {
326 char *ResultPtr
= ResultBuf
;
327 if (llvm::ConvertCodePointToUTF8(Codepoint
, ResultPtr
))
328 Str
.append(ResultBuf
, ResultPtr
);
331 void clang::expandUCNs(SmallVectorImpl
<char> &Buf
, StringRef Input
) {
332 for (StringRef::iterator I
= Input
.begin(), E
= Input
.end(); I
!= E
; ++I
) {
342 assert(Kind
== 'u' || Kind
== 'U' || Kind
== 'N');
343 uint32_t CodePoint
= 0;
345 if (Kind
== 'u' && *I
== '{') {
346 for (++I
; *I
!= '}'; ++I
) {
347 unsigned Value
= llvm::hexDigitValue(*I
);
348 assert(Value
!= -1U);
352 appendCodePoint(CodePoint
, Buf
);
359 auto Delim
= std::find(I
, Input
.end(), '}');
360 assert(Delim
!= Input
.end());
361 llvm::Optional
<llvm::sys::unicode::LooseMatchingResult
> Res
=
362 llvm::sys::unicode::nameToCodepointLooseMatching(
363 StringRef(I
, std::distance(I
, Delim
)));
365 CodePoint
= Res
->CodePoint
;
366 assert(CodePoint
!= 0xFFFFFFFF);
367 appendCodePoint(CodePoint
, Buf
);
372 unsigned NumHexDigits
;
378 assert(I
+ NumHexDigits
<= E
);
380 for (; NumHexDigits
!= 0; ++I
, --NumHexDigits
) {
381 unsigned Value
= llvm::hexDigitValue(*I
);
382 assert(Value
!= -1U);
388 appendCodePoint(CodePoint
, Buf
);
393 static bool ProcessNumericUCNEscape(const char *ThisTokBegin
,
394 const char *&ThisTokBuf
,
395 const char *ThisTokEnd
, uint32_t &UcnVal
,
396 unsigned short &UcnLen
, bool &Delimited
,
397 FullSourceLoc Loc
, DiagnosticsEngine
*Diags
,
398 const LangOptions
&Features
,
399 bool in_char_string_literal
= false) {
400 const char *UcnBegin
= ThisTokBuf
;
401 bool HasError
= false;
402 bool EndDelimiterFound
= false;
404 // Skip the '\u' char's.
407 if (UcnBegin
[1] == 'u' && in_char_string_literal
&&
408 ThisTokBuf
!= ThisTokEnd
&& *ThisTokBuf
== '{') {
411 } else if (ThisTokBuf
== ThisTokEnd
|| !isHexDigit(*ThisTokBuf
)) {
413 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
414 diag::err_hex_escape_no_digits
)
415 << StringRef(&ThisTokBuf
[-1], 1);
418 UcnLen
= (ThisTokBuf
[-1] == 'u' ? 4 : 8);
420 bool Overflow
= false;
421 unsigned short Count
= 0;
422 for (; ThisTokBuf
!= ThisTokEnd
&& (Delimited
|| Count
!= UcnLen
);
424 if (Delimited
&& *ThisTokBuf
== '}') {
426 EndDelimiterFound
= true;
429 int CharVal
= llvm::hexDigitValue(*ThisTokBuf
);
435 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
436 diag::err_delimited_escape_invalid
)
437 << StringRef(ThisTokBuf
, 1);
442 if (UcnVal
& 0xF0000000) {
453 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
454 diag::err_escape_too_large
)
459 if (Delimited
&& !EndDelimiterFound
) {
461 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
468 // If we didn't consume the proper number of digits, there is a problem.
469 if (Count
== 0 || (!Delimited
&& Count
!= UcnLen
)) {
471 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
472 Delimited
? diag::err_delimited_escape_empty
473 : diag::err_ucn_escape_incomplete
);
479 static void DiagnoseInvalidUnicodeCharacterName(
480 DiagnosticsEngine
*Diags
, const LangOptions
&Features
, FullSourceLoc Loc
,
481 const char *TokBegin
, const char *TokRangeBegin
, const char *TokRangeEnd
,
482 llvm::StringRef Name
) {
484 Diag(Diags
, Features
, Loc
, TokBegin
, TokRangeBegin
, TokRangeEnd
,
485 diag::err_invalid_ucn_name
)
488 namespace u
= llvm::sys::unicode
;
490 llvm::Optional
<u::LooseMatchingResult
> Res
=
491 u::nameToCodepointLooseMatching(Name
);
493 Diag(Diags
, Features
, Loc
, TokBegin
, TokRangeBegin
, TokRangeEnd
,
494 diag::note_invalid_ucn_name_loose_matching
)
495 << FixItHint::CreateReplacement(
496 MakeCharSourceRange(Features
, Loc
, TokBegin
, TokRangeBegin
,
502 unsigned Distance
= 0;
503 SmallVector
<u::MatchForCodepointName
> Matches
=
504 u::nearestMatchesForCodepointName(Name
, 5);
505 assert(!Matches
.empty() && "No unicode characters found");
507 for (const auto &Match
: Matches
) {
509 Distance
= Match
.Distance
;
510 if (std::max(Distance
, Match
.Distance
) -
511 std::min(Distance
, Match
.Distance
) >
514 Distance
= Match
.Distance
;
517 llvm::UTF32 V
= Match
.Value
;
518 LLVM_ATTRIBUTE_UNUSED
bool Converted
=
519 llvm::convertUTF32ToUTF8String(llvm::ArrayRef
<llvm::UTF32
>(&V
, 1), Str
);
520 assert(Converted
&& "Found a match wich is not a unicode character");
522 Diag(Diags
, Features
, Loc
, TokBegin
, TokRangeBegin
, TokRangeEnd
,
523 diag::note_invalid_ucn_name_candidate
)
524 << Match
.Name
<< llvm::utohexstr(Match
.Value
)
525 << Str
// FIXME: Fix the rendering of non printable characters
526 << FixItHint::CreateReplacement(
527 MakeCharSourceRange(Features
, Loc
, TokBegin
, TokRangeBegin
,
533 static bool ProcessNamedUCNEscape(const char *ThisTokBegin
,
534 const char *&ThisTokBuf
,
535 const char *ThisTokEnd
, uint32_t &UcnVal
,
536 unsigned short &UcnLen
, FullSourceLoc Loc
,
537 DiagnosticsEngine
*Diags
,
538 const LangOptions
&Features
) {
539 const char *UcnBegin
= ThisTokBuf
;
540 assert(UcnBegin
[0] == '\\' && UcnBegin
[1] == 'N');
542 if (ThisTokBuf
== ThisTokEnd
|| *ThisTokBuf
!= '{') {
544 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
545 diag::err_delimited_escape_missing_brace
)
546 << StringRef(&ThisTokBuf
[-1], 1);
552 const char *ClosingBrace
=
553 std::find_if_not(ThisTokBuf
, ThisTokEnd
, [](char C
) {
554 return llvm::isAlnum(C
) || llvm::isSpace(C
) || C
== '_' || C
== '-';
556 bool Incomplete
= ClosingBrace
== ThisTokEnd
|| *ClosingBrace
!= '}';
557 bool Empty
= ClosingBrace
== ThisTokBuf
;
558 if (Incomplete
|| Empty
) {
560 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
561 Incomplete
? diag::err_ucn_escape_incomplete
562 : diag::err_delimited_escape_empty
)
563 << StringRef(&UcnBegin
[1], 1);
565 ThisTokBuf
= ClosingBrace
== ThisTokEnd
? ClosingBrace
: ClosingBrace
+ 1;
568 StringRef
Name(ThisTokBuf
, ClosingBrace
- ThisTokBuf
);
569 ThisTokBuf
= ClosingBrace
+ 1;
570 llvm::Optional
<char32_t
> Res
=
571 llvm::sys::unicode::nameToCodepointStrict(Name
);
574 DiagnoseInvalidUnicodeCharacterName(Diags
, Features
, Loc
, ThisTokBegin
,
575 &UcnBegin
[3], ClosingBrace
, Name
);
579 UcnLen
= UcnVal
> 0xFFFF ? 8 : 4;
583 /// ProcessUCNEscape - Read the Universal Character Name, check constraints and
584 /// return the UTF32.
585 static bool ProcessUCNEscape(const char *ThisTokBegin
, const char *&ThisTokBuf
,
586 const char *ThisTokEnd
, uint32_t &UcnVal
,
587 unsigned short &UcnLen
, FullSourceLoc Loc
,
588 DiagnosticsEngine
*Diags
,
589 const LangOptions
&Features
,
590 bool in_char_string_literal
= false) {
593 const char *UcnBegin
= ThisTokBuf
;
594 bool IsDelimitedEscapeSequence
= false;
595 bool IsNamedEscapeSequence
= false;
596 if (ThisTokBuf
[1] == 'N') {
597 IsNamedEscapeSequence
= true;
598 HasError
= !ProcessNamedUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
,
599 UcnVal
, UcnLen
, Loc
, Diags
, Features
);
602 !ProcessNumericUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, UcnVal
,
603 UcnLen
, IsDelimitedEscapeSequence
, Loc
, Diags
,
604 Features
, in_char_string_literal
);
609 // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
610 if ((0xD800 <= UcnVal
&& UcnVal
<= 0xDFFF) || // surrogate codepoints
611 UcnVal
> 0x10FFFF) { // maximum legal UTF32 value
613 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
614 diag::err_ucn_escape_invalid
);
618 // C++11 allows UCNs that refer to control characters and basic source
619 // characters inside character and string literals
621 (UcnVal
!= 0x24 && UcnVal
!= 0x40 && UcnVal
!= 0x60)) { // $, @, `
622 bool IsError
= (!Features
.CPlusPlus11
|| !in_char_string_literal
);
624 char BasicSCSChar
= UcnVal
;
625 if (UcnVal
>= 0x20 && UcnVal
< 0x7f)
626 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
627 IsError
? diag::err_ucn_escape_basic_scs
:
628 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs
)
629 << StringRef(&BasicSCSChar
, 1);
631 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
632 IsError
? diag::err_ucn_control_character
:
633 diag::warn_cxx98_compat_literal_ucn_control_character
);
639 if (!Features
.CPlusPlus
&& !Features
.C99
&& Diags
)
640 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
641 diag::warn_ucn_not_valid_in_c89_literal
);
643 if ((IsDelimitedEscapeSequence
|| IsNamedEscapeSequence
) && Diags
)
644 Diag(Diags
, Features
, Loc
, ThisTokBegin
, UcnBegin
, ThisTokBuf
,
645 Features
.CPlusPlus2b
? diag::warn_cxx2b_delimited_escape_sequence
646 : diag::ext_delimited_escape_sequence
)
647 << (IsNamedEscapeSequence
? 1 : 0) << (Features
.CPlusPlus
? 1 : 0);
652 /// MeasureUCNEscape - Determine the number of bytes within the resulting string
653 /// which this UCN will occupy.
654 static int MeasureUCNEscape(const char *ThisTokBegin
, const char *&ThisTokBuf
,
655 const char *ThisTokEnd
, unsigned CharByteWidth
,
656 const LangOptions
&Features
, bool &HadError
) {
657 // UTF-32: 4 bytes per escape.
658 if (CharByteWidth
== 4)
662 unsigned short UcnLen
= 0;
665 if (!ProcessUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, UcnVal
,
666 UcnLen
, Loc
, nullptr, Features
, true)) {
671 // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
672 if (CharByteWidth
== 2)
673 return UcnVal
<= 0xFFFF ? 2 : 4;
680 if (UcnVal
< 0x10000)
685 /// EncodeUCNEscape - Read the Universal Character Name, check constraints and
686 /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
687 /// StringLiteralParser. When we decide to implement UCN's for identifiers,
688 /// we will likely rework our support for UCN's.
689 static void EncodeUCNEscape(const char *ThisTokBegin
, const char *&ThisTokBuf
,
690 const char *ThisTokEnd
,
691 char *&ResultBuf
, bool &HadError
,
692 FullSourceLoc Loc
, unsigned CharByteWidth
,
693 DiagnosticsEngine
*Diags
,
694 const LangOptions
&Features
) {
695 typedef uint32_t UTF32
;
697 unsigned short UcnLen
= 0;
698 if (!ProcessUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, UcnVal
, UcnLen
,
699 Loc
, Diags
, Features
, true)) {
704 assert((CharByteWidth
== 1 || CharByteWidth
== 2 || CharByteWidth
== 4) &&
705 "only character widths of 1, 2, or 4 bytes supported");
708 assert((UcnLen
== 4 || UcnLen
== 8) && "only ucn length of 4 or 8 supported");
710 if (CharByteWidth
== 4) {
711 // FIXME: Make the type of the result buffer correct instead of
712 // using reinterpret_cast.
713 llvm::UTF32
*ResultPtr
= reinterpret_cast<llvm::UTF32
*>(ResultBuf
);
719 if (CharByteWidth
== 2) {
720 // FIXME: Make the type of the result buffer correct instead of
721 // using reinterpret_cast.
722 llvm::UTF16
*ResultPtr
= reinterpret_cast<llvm::UTF16
*>(ResultBuf
);
724 if (UcnVal
<= (UTF32
)0xFFFF) {
732 *ResultPtr
= 0xD800 + (UcnVal
>> 10);
733 *(ResultPtr
+1) = 0xDC00 + (UcnVal
& 0x3FF);
738 assert(CharByteWidth
== 1 && "UTF-8 encoding is only for 1 byte characters");
740 // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
741 // The conversion below was inspired by:
742 // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
743 // First, we determine how many bytes the result will require.
744 typedef uint8_t UTF8
;
746 unsigned short bytesToWrite
= 0;
747 if (UcnVal
< (UTF32
)0x80)
749 else if (UcnVal
< (UTF32
)0x800)
751 else if (UcnVal
< (UTF32
)0x10000)
756 const unsigned byteMask
= 0xBF;
757 const unsigned byteMark
= 0x80;
759 // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
760 // into the first byte, depending on how many bytes follow.
761 static const UTF8 firstByteMark
[5] = {
762 0x00, 0x00, 0xC0, 0xE0, 0xF0
764 // Finally, we write the bytes into ResultBuf.
765 ResultBuf
+= bytesToWrite
;
766 switch (bytesToWrite
) { // note: everything falls through.
768 *--ResultBuf
= (UTF8
)((UcnVal
| byteMark
) & byteMask
); UcnVal
>>= 6;
771 *--ResultBuf
= (UTF8
)((UcnVal
| byteMark
) & byteMask
); UcnVal
>>= 6;
774 *--ResultBuf
= (UTF8
)((UcnVal
| byteMark
) & byteMask
); UcnVal
>>= 6;
777 *--ResultBuf
= (UTF8
) (UcnVal
| firstByteMark
[bytesToWrite
]);
779 // Update the buffer.
780 ResultBuf
+= bytesToWrite
;
783 /// integer-constant: [C99 6.4.4.1]
784 /// decimal-constant integer-suffix
785 /// octal-constant integer-suffix
786 /// hexadecimal-constant integer-suffix
787 /// binary-literal integer-suffix [GNU, C++1y]
788 /// user-defined-integer-literal: [C++11 lex.ext]
789 /// decimal-literal ud-suffix
790 /// octal-literal ud-suffix
791 /// hexadecimal-literal ud-suffix
792 /// binary-literal ud-suffix [GNU, C++1y]
793 /// decimal-constant:
795 /// decimal-constant digit
798 /// octal-constant octal-digit
799 /// hexadecimal-constant:
800 /// hexadecimal-prefix hexadecimal-digit
801 /// hexadecimal-constant hexadecimal-digit
802 /// hexadecimal-prefix: one of
807 /// binary-literal binary-digit
809 /// unsigned-suffix [long-suffix]
810 /// unsigned-suffix [long-long-suffix]
811 /// long-suffix [unsigned-suffix]
812 /// long-long-suffix [unsigned-sufix]
814 /// 1 2 3 4 5 6 7 8 9
817 /// hexadecimal-digit:
818 /// 0 1 2 3 4 5 6 7 8 9
824 /// unsigned-suffix: one of
826 /// long-suffix: one of
828 /// long-long-suffix: one of
831 /// floating-constant: [C99 6.4.4.2]
832 /// TODO: add rules...
834 NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling
,
835 SourceLocation TokLoc
,
836 const SourceManager
&SM
,
837 const LangOptions
&LangOpts
,
838 const TargetInfo
&Target
,
839 DiagnosticsEngine
&Diags
)
840 : SM(SM
), LangOpts(LangOpts
), Diags(Diags
),
841 ThisTokBegin(TokSpelling
.begin()), ThisTokEnd(TokSpelling
.end()) {
843 s
= DigitsBegin
= ThisTokBegin
;
844 saw_exponent
= false;
846 saw_ud_suffix
= false;
847 saw_fixed_point_suffix
= false;
857 MicrosoftInteger
= 0;
863 // This routine assumes that the range begin/end matches the regex for integer
864 // and FP constants (specifically, the 'pp-number' regex), and assumes that
865 // the byte at "*end" is both valid and not part of the regex. Because of
866 // this, it doesn't have to check for 'overscan' in various places.
867 if (isPreprocessingNumberBody(*ThisTokEnd
)) {
868 Diags
.Report(TokLoc
, diag::err_lexing_numeric
);
873 if (*s
== '0') { // parse radix
874 ParseNumberStartingWithZero(TokLoc
);
877 } else { // the first digit is non-zero
880 if (s
== ThisTokEnd
) {
883 ParseDecimalOrOctalCommon(TokLoc
);
890 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
892 // Initial scan to lookahead for fixed point suffix.
893 if (LangOpts
.FixedPoint
) {
894 for (const char *c
= s
; c
!= ThisTokEnd
; ++c
) {
895 if (*c
== 'r' || *c
== 'k' || *c
== 'R' || *c
== 'K') {
896 saw_fixed_point_suffix
= true;
902 // Parse the suffix. At this point we can classify whether we have an FP or
904 bool isFixedPointConstant
= isFixedPointLiteral();
905 bool isFPConstant
= isFloatingLiteral();
906 bool HasSize
= false;
908 // Loop over all of the characters of the suffix. If we see something bad,
909 // we break out of the loop.
910 for (; s
!= ThisTokEnd
; ++s
) {
914 if (!LangOpts
.FixedPoint
)
916 if (isFract
|| isAccum
) break;
917 if (!(saw_period
|| saw_exponent
)) break;
922 if (!LangOpts
.FixedPoint
)
924 if (isFract
|| isAccum
) break;
925 if (!(saw_period
|| saw_exponent
)) break;
928 case 'h': // FP Suffix for "half".
930 // OpenCL Extension v1.2 s9.5 - h or H suffix for half type.
931 if (!(LangOpts
.Half
|| LangOpts
.FixedPoint
))
933 if (isIntegerLiteral()) break; // Error for integer constant.
938 continue; // Success.
939 case 'f': // FP Suffix for "float"
941 if (!isFPConstant
) break; // Error for integer constant.
946 // CUDA host and device may have different _Float16 support, therefore
947 // allows f16 literals to avoid false alarm.
948 // ToDo: more precise check for CUDA.
949 if ((Target
.hasFloat16Type() || LangOpts
.CUDA
) && s
+ 2 < ThisTokEnd
&&
950 s
[1] == '1' && s
[2] == '6') {
951 s
+= 2; // success, eat up 2 characters.
957 continue; // Success.
958 case 'q': // FP Suffix for "__float128"
960 if (!isFPConstant
) break; // Error for integer constant.
965 continue; // Success.
968 if (isFPConstant
) break; // Error for floating constant.
969 if (isUnsigned
) break; // Cannot be repeated.
971 continue; // Success.
978 // Check for long long. The L's need to be adjacent and the same case.
980 assert(s
+ 1 < ThisTokEnd
&& "didn't maximally munch?");
981 if (isFPConstant
) break; // long long invalid for floats.
983 ++s
; // Eat both of them.
987 continue; // Success.
991 break; // Invalid for floats.
999 if (LangOpts
.MicrosoftExt
&& !isFPConstant
) {
1000 // Allow i8, i16, i32, and i64. First, look ahead and check if
1001 // suffixes are Microsoft integers and not the imaginary unit.
1005 case '8': // i8 suffix
1010 if (s
[2] == '6') { // i16 suffix
1016 if (s
[2] == '2') { // i32 suffix
1022 if (s
[2] == '4') { // i64 suffix
1034 MicrosoftInteger
= Bits
;
1036 assert(s
<= ThisTokEnd
&& "didn't maximally munch?");
1043 if (isImaginary
) break; // Cannot be repeated.
1045 continue; // Success.
1049 break; // Invalid for floats.
1051 break; // Invalid if we already have a size for the literal.
1053 // wb and WB are allowed, but a mixture of cases like Wb or wB is not. We
1054 // explicitly do not support the suffix in C++ as an extension because a
1055 // library-based UDL that resolves to a library type may be more
1056 // appropriate there.
1057 if (!LangOpts
.CPlusPlus
&& ((s
[0] == 'w' && s
[1] == 'b') ||
1058 (s
[0] == 'W' && s
[1] == 'B'))) {
1061 ++s
; // Skip both characters (2nd char skipped on continue).
1062 continue; // Success.
1065 // If we reached here, there was an error or a ud-suffix.
1069 // "i", "if", and "il" are user-defined suffixes in C++1y.
1070 if (s
!= ThisTokEnd
|| isImaginary
) {
1071 // FIXME: Don't bother expanding UCNs if !tok.hasUCN().
1072 expandUCNs(UDSuffixBuf
, StringRef(SuffixBegin
, ThisTokEnd
- SuffixBegin
));
1073 if (isValidUDSuffix(LangOpts
, UDSuffixBuf
)) {
1075 // Any suffix pieces we might have parsed are actually part of the
1084 isImaginary
= false;
1086 MicrosoftInteger
= 0;
1087 saw_fixed_point_suffix
= false;
1092 saw_ud_suffix
= true;
1096 if (s
!= ThisTokEnd
) {
1097 // Report an error if there are any.
1098 Diags
.Report(Lexer::AdvanceToTokenCharacter(
1099 TokLoc
, SuffixBegin
- ThisTokBegin
, SM
, LangOpts
),
1100 diag::err_invalid_suffix_constant
)
1101 << StringRef(SuffixBegin
, ThisTokEnd
- SuffixBegin
)
1102 << (isFixedPointConstant
? 2 : isFPConstant
);
1107 if (!hadError
&& saw_fixed_point_suffix
) {
1108 assert(isFract
|| isAccum
);
1112 /// ParseDecimalOrOctalCommon - This method is called for decimal or octal
1113 /// numbers. It issues an error for illegal digits, and handles floating point
1114 /// parsing. If it detects a floating point number, the radix is set to 10.
1115 void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc
){
1116 assert((radix
== 8 || radix
== 10) && "Unexpected radix");
1118 // If we have a hex digit other than 'e' (which denotes a FP exponent) then
1119 // the code is using an incorrect base.
1120 if (isHexDigit(*s
) && *s
!= 'e' && *s
!= 'E' &&
1121 !isValidUDSuffix(LangOpts
, StringRef(s
, ThisTokEnd
- s
))) {
1123 Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
, LangOpts
),
1124 diag::err_invalid_digit
)
1125 << StringRef(s
, 1) << (radix
== 8 ? 1 : 0);
1131 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
1135 checkSeparator(TokLoc
, s
, CSK_BeforeDigits
);
1136 s
= SkipDigits(s
); // Skip suffix.
1138 if (*s
== 'e' || *s
== 'E') { // exponent
1139 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
1140 const char *Exponent
= s
;
1143 saw_exponent
= true;
1144 if (s
!= ThisTokEnd
&& (*s
== '+' || *s
== '-')) s
++; // sign
1145 const char *first_non_digit
= SkipDigits(s
);
1146 if (containsDigits(s
, first_non_digit
)) {
1147 checkSeparator(TokLoc
, s
, CSK_BeforeDigits
);
1148 s
= first_non_digit
;
1151 Diags
.Report(Lexer::AdvanceToTokenCharacter(
1152 TokLoc
, Exponent
- ThisTokBegin
, SM
, LangOpts
),
1153 diag::err_exponent_has_no_digits
);
1161 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved
1162 /// suffixes as ud-suffixes, because the diagnostic experience is better if we
1163 /// treat it as an invalid suffix.
1164 bool NumericLiteralParser::isValidUDSuffix(const LangOptions
&LangOpts
,
1166 if (!LangOpts
.CPlusPlus11
|| Suffix
.empty())
1169 // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid.
1170 if (Suffix
[0] == '_')
1173 // In C++11, there are no library suffixes.
1174 if (!LangOpts
.CPlusPlus14
)
1177 // In C++14, "s", "h", "min", "ms", "us", and "ns" are used in the library.
1178 // Per tweaked N3660, "il", "i", and "if" are also used in the library.
1179 // In C++2a "d" and "y" are used in the library.
1180 return llvm::StringSwitch
<bool>(Suffix
)
1181 .Cases("h", "min", "s", true)
1182 .Cases("ms", "us", "ns", true)
1183 .Cases("il", "i", "if", true)
1184 .Cases("d", "y", LangOpts
.CPlusPlus20
)
1188 void NumericLiteralParser::checkSeparator(SourceLocation TokLoc
,
1190 CheckSeparatorKind IsAfterDigits
) {
1191 if (IsAfterDigits
== CSK_AfterDigits
) {
1192 if (Pos
== ThisTokBegin
)
1195 } else if (Pos
== ThisTokEnd
)
1198 if (isDigitSeparator(*Pos
)) {
1199 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, Pos
- ThisTokBegin
, SM
,
1201 diag::err_digit_separator_not_between_digits
)
1207 /// ParseNumberStartingWithZero - This method is called when the first character
1208 /// of the number is found to be a zero. This means it is either an octal
1209 /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
1210 /// a floating point number (01239.123e4). Eat the prefix, determining the
1212 void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc
) {
1213 assert(s
[0] == '0' && "Invalid method call");
1218 // Handle a hex number like 0x1234.
1219 if ((c1
== 'x' || c1
== 'X') && (isHexDigit(s
[1]) || s
[1] == '.')) {
1221 assert(s
< ThisTokEnd
&& "didn't maximally munch?");
1224 s
= SkipHexDigits(s
);
1225 bool HasSignificandDigits
= containsDigits(DigitsBegin
, s
);
1226 if (s
== ThisTokEnd
) {
1228 } else if (*s
== '.') {
1231 const char *floatDigitsBegin
= s
;
1232 s
= SkipHexDigits(s
);
1233 if (containsDigits(floatDigitsBegin
, s
))
1234 HasSignificandDigits
= true;
1235 if (HasSignificandDigits
)
1236 checkSeparator(TokLoc
, floatDigitsBegin
, CSK_BeforeDigits
);
1239 if (!HasSignificandDigits
) {
1240 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
,
1242 diag::err_hex_constant_requires
)
1243 << LangOpts
.CPlusPlus
<< 1;
1248 // A binary exponent can appear with or with a '.'. If dotted, the
1249 // binary exponent is required.
1250 if (*s
== 'p' || *s
== 'P') {
1251 checkSeparator(TokLoc
, s
, CSK_AfterDigits
);
1252 const char *Exponent
= s
;
1254 saw_exponent
= true;
1255 if (s
!= ThisTokEnd
&& (*s
== '+' || *s
== '-')) s
++; // sign
1256 const char *first_non_digit
= SkipDigits(s
);
1257 if (!containsDigits(s
, first_non_digit
)) {
1259 Diags
.Report(Lexer::AdvanceToTokenCharacter(
1260 TokLoc
, Exponent
- ThisTokBegin
, SM
, LangOpts
),
1261 diag::err_exponent_has_no_digits
);
1266 checkSeparator(TokLoc
, s
, CSK_BeforeDigits
);
1267 s
= first_non_digit
;
1269 if (!LangOpts
.HexFloats
)
1270 Diags
.Report(TokLoc
, LangOpts
.CPlusPlus
1271 ? diag::ext_hex_literal_invalid
1272 : diag::ext_hex_constant_invalid
);
1273 else if (LangOpts
.CPlusPlus17
)
1274 Diags
.Report(TokLoc
, diag::warn_cxx17_hex_literal
);
1275 } else if (saw_period
) {
1276 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
,
1278 diag::err_hex_constant_requires
)
1279 << LangOpts
.CPlusPlus
<< 0;
1285 // Handle simple binary numbers 0b01010
1286 if ((c1
== 'b' || c1
== 'B') && (s
[1] == '0' || s
[1] == '1')) {
1287 // 0b101010 is a C++1y / GCC extension.
1288 Diags
.Report(TokLoc
, LangOpts
.CPlusPlus14
1289 ? diag::warn_cxx11_compat_binary_literal
1290 : LangOpts
.CPlusPlus
? diag::ext_binary_literal_cxx14
1291 : diag::ext_binary_literal
);
1293 assert(s
< ThisTokEnd
&& "didn't maximally munch?");
1296 s
= SkipBinaryDigits(s
);
1297 if (s
== ThisTokEnd
) {
1299 } else if (isHexDigit(*s
) &&
1300 !isValidUDSuffix(LangOpts
, StringRef(s
, ThisTokEnd
- s
))) {
1301 Diags
.Report(Lexer::AdvanceToTokenCharacter(TokLoc
, s
- ThisTokBegin
, SM
,
1303 diag::err_invalid_digit
)
1304 << StringRef(s
, 1) << 2;
1307 // Other suffixes will be diagnosed by the caller.
1311 // For now, the radix is set to 8. If we discover that we have a
1312 // floating point constant, the radix will change to 10. Octal floating
1313 // point constants are not permitted (only decimal and hexadecimal).
1315 const char *PossibleNewDigitStart
= s
;
1316 s
= SkipOctalDigits(s
);
1317 // When the value is 0 followed by a suffix (like 0wb), we want to leave 0
1318 // as the start of the digits. So if skipping octal digits does not skip
1319 // anything, we leave the digit start where it was.
1320 if (s
!= PossibleNewDigitStart
)
1321 DigitsBegin
= PossibleNewDigitStart
;
1323 if (s
== ThisTokEnd
)
1324 return; // Done, simple octal number like 01234
1326 // If we have some other non-octal digit that *is* a decimal digit, see if
1327 // this is part of a floating point number like 094.123 or 09e1.
1329 const char *EndDecimal
= SkipDigits(s
);
1330 if (EndDecimal
[0] == '.' || EndDecimal
[0] == 'e' || EndDecimal
[0] == 'E') {
1336 ParseDecimalOrOctalCommon(TokLoc
);
1339 static bool alwaysFitsInto64Bits(unsigned Radix
, unsigned NumDigits
) {
1342 return NumDigits
<= 64;
1344 return NumDigits
<= 64 / 3; // Digits are groups of 3 bits.
1346 return NumDigits
<= 19; // floor(log10(2^64))
1348 return NumDigits
<= 64 / 4; // Digits are groups of 4 bits.
1350 llvm_unreachable("impossible Radix");
1354 /// GetIntegerValue - Convert this numeric literal value to an APInt that
1355 /// matches Val's input width. If there is an overflow, set Val to the low bits
1356 /// of the result and return true. Otherwise, return false.
1357 bool NumericLiteralParser::GetIntegerValue(llvm::APInt
&Val
) {
1358 // Fast path: Compute a conservative bound on the maximum number of
1359 // bits per digit in this radix. If we can't possibly overflow a
1360 // uint64 based on that bound then do the simple conversion to
1361 // integer. This avoids the expensive overflow checking below, and
1362 // handles the common cases that matter (small decimal integers and
1363 // hex/octal values which don't overflow).
1364 const unsigned NumDigits
= SuffixBegin
- DigitsBegin
;
1365 if (alwaysFitsInto64Bits(radix
, NumDigits
)) {
1367 for (const char *Ptr
= DigitsBegin
; Ptr
!= SuffixBegin
; ++Ptr
)
1368 if (!isDigitSeparator(*Ptr
))
1369 N
= N
* radix
+ llvm::hexDigitValue(*Ptr
);
1371 // This will truncate the value to Val's input width. Simply check
1372 // for overflow by comparing.
1374 return Val
.getZExtValue() != N
;
1378 const char *Ptr
= DigitsBegin
;
1380 llvm::APInt
RadixVal(Val
.getBitWidth(), radix
);
1381 llvm::APInt
CharVal(Val
.getBitWidth(), 0);
1382 llvm::APInt OldVal
= Val
;
1384 bool OverflowOccurred
= false;
1385 while (Ptr
< SuffixBegin
) {
1386 if (isDigitSeparator(*Ptr
)) {
1391 unsigned C
= llvm::hexDigitValue(*Ptr
++);
1393 // If this letter is out of bound for this radix, reject it.
1394 assert(C
< radix
&& "NumericLiteralParser ctor should have rejected this");
1398 // Add the digit to the value in the appropriate radix. If adding in digits
1399 // made the value smaller, then this overflowed.
1402 // Multiply by radix, did overflow occur on the multiply?
1404 OverflowOccurred
|= Val
.udiv(RadixVal
) != OldVal
;
1406 // Add value, did overflow occur on the value?
1407 // (a + b) ult b <=> overflow
1409 OverflowOccurred
|= Val
.ult(CharVal
);
1411 return OverflowOccurred
;
1414 llvm::APFloat::opStatus
1415 NumericLiteralParser::GetFloatValue(llvm::APFloat
&Result
) {
1416 using llvm::APFloat
;
1418 unsigned n
= std::min(SuffixBegin
- ThisTokBegin
, ThisTokEnd
- ThisTokBegin
);
1420 llvm::SmallString
<16> Buffer
;
1421 StringRef
Str(ThisTokBegin
, n
);
1422 if (Str
.contains('\'')) {
1424 std::remove_copy_if(Str
.begin(), Str
.end(), std::back_inserter(Buffer
),
1430 Result
.convertFromString(Str
, APFloat::rmNearestTiesToEven
);
1431 assert(StatusOrErr
&& "Invalid floating point representation");
1432 return !errorToBool(StatusOrErr
.takeError()) ? *StatusOrErr
1433 : APFloat::opInvalidOp
;
1436 static inline bool IsExponentPart(char c
) {
1437 return c
== 'p' || c
== 'P' || c
== 'e' || c
== 'E';
1440 bool NumericLiteralParser::GetFixedPointValue(llvm::APInt
&StoreVal
, unsigned Scale
) {
1441 assert(radix
== 16 || radix
== 10);
1443 // Find how many digits are needed to store the whole literal.
1444 unsigned NumDigits
= SuffixBegin
- DigitsBegin
;
1445 if (saw_period
) --NumDigits
;
1447 // Initial scan of the exponent if it exists
1448 bool ExpOverflowOccurred
= false;
1449 bool NegativeExponent
= false;
1450 const char *ExponentBegin
;
1451 uint64_t Exponent
= 0;
1452 int64_t BaseShift
= 0;
1454 const char *Ptr
= DigitsBegin
;
1456 while (!IsExponentPart(*Ptr
)) ++Ptr
;
1457 ExponentBegin
= Ptr
;
1459 NegativeExponent
= *Ptr
== '-';
1460 if (NegativeExponent
) ++Ptr
;
1462 unsigned NumExpDigits
= SuffixBegin
- Ptr
;
1463 if (alwaysFitsInto64Bits(radix
, NumExpDigits
)) {
1464 llvm::StringRef
ExpStr(Ptr
, NumExpDigits
);
1465 llvm::APInt
ExpInt(/*numBits=*/64, ExpStr
, /*radix=*/10);
1466 Exponent
= ExpInt
.getZExtValue();
1468 ExpOverflowOccurred
= true;
1471 if (NegativeExponent
) BaseShift
-= Exponent
;
1472 else BaseShift
+= Exponent
;
1475 // Number of bits needed for decimal literal is
1476 // ceil(NumDigits * log2(10)) Integral part
1477 // + Scale Fractional part
1478 // + ceil(Exponent * log2(10)) Exponent
1479 // --------------------------------------------------
1480 // ceil((NumDigits + Exponent) * log2(10)) + Scale
1482 // But for simplicity in handling integers, we can round up log2(10) to 4,
1484 // 4 * (NumDigits + Exponent) + Scale
1486 // Number of digits needed for hexadecimal literal is
1487 // 4 * NumDigits Integral part
1488 // + Scale Fractional part
1489 // + Exponent Exponent
1490 // --------------------------------------------------
1491 // (4 * NumDigits) + Scale + Exponent
1492 uint64_t NumBitsNeeded
;
1494 NumBitsNeeded
= 4 * (NumDigits
+ Exponent
) + Scale
;
1496 NumBitsNeeded
= 4 * NumDigits
+ Exponent
+ Scale
;
1498 if (NumBitsNeeded
> std::numeric_limits
<unsigned>::max())
1499 ExpOverflowOccurred
= true;
1500 llvm::APInt
Val(static_cast<unsigned>(NumBitsNeeded
), 0, /*isSigned=*/false);
1502 bool FoundDecimal
= false;
1504 int64_t FractBaseShift
= 0;
1505 const char *End
= saw_exponent
? ExponentBegin
: SuffixBegin
;
1506 for (const char *Ptr
= DigitsBegin
; Ptr
< End
; ++Ptr
) {
1508 FoundDecimal
= true;
1512 // Normal reading of an integer
1513 unsigned C
= llvm::hexDigitValue(*Ptr
);
1514 assert(C
< radix
&& "NumericLiteralParser ctor should have rejected this");
1520 // Keep track of how much we will need to adjust this value by from the
1521 // number of digits past the radix point.
1525 // For a radix of 16, we will be multiplying by 2 instead of 16.
1526 if (radix
== 16) FractBaseShift
*= 4;
1527 BaseShift
+= FractBaseShift
;
1531 uint64_t Base
= (radix
== 16) ? 2 : 10;
1532 if (BaseShift
> 0) {
1533 for (int64_t i
= 0; i
< BaseShift
; ++i
) {
1536 } else if (BaseShift
< 0) {
1537 for (int64_t i
= BaseShift
; i
< 0 && !Val
.isZero(); ++i
)
1538 Val
= Val
.udiv(Base
);
1541 bool IntOverflowOccurred
= false;
1542 auto MaxVal
= llvm::APInt::getMaxValue(StoreVal
.getBitWidth());
1543 if (Val
.getBitWidth() > StoreVal
.getBitWidth()) {
1544 IntOverflowOccurred
|= Val
.ugt(MaxVal
.zext(Val
.getBitWidth()));
1545 StoreVal
= Val
.trunc(StoreVal
.getBitWidth());
1546 } else if (Val
.getBitWidth() < StoreVal
.getBitWidth()) {
1547 IntOverflowOccurred
|= Val
.zext(MaxVal
.getBitWidth()).ugt(MaxVal
);
1548 StoreVal
= Val
.zext(StoreVal
.getBitWidth());
1553 return IntOverflowOccurred
|| ExpOverflowOccurred
;
1557 /// user-defined-character-literal: [C++11 lex.ext]
1558 /// character-literal ud-suffix
1561 /// character-literal: [C++11 lex.ccon]
1562 /// ' c-char-sequence '
1563 /// u' c-char-sequence '
1564 /// U' c-char-sequence '
1565 /// L' c-char-sequence '
1566 /// u8' c-char-sequence ' [C++1z lex.ccon]
1567 /// c-char-sequence:
1569 /// c-char-sequence c-char
1571 /// any member of the source character set except the single-quote ',
1572 /// backslash \, or new-line character
1574 /// universal-character-name
1575 /// escape-sequence:
1576 /// simple-escape-sequence
1577 /// octal-escape-sequence
1578 /// hexadecimal-escape-sequence
1579 /// simple-escape-sequence:
1580 /// one of \' \" \? \\ \a \b \f \n \r \t \v
1581 /// octal-escape-sequence:
1583 /// \ octal-digit octal-digit
1584 /// \ octal-digit octal-digit octal-digit
1585 /// hexadecimal-escape-sequence:
1586 /// \x hexadecimal-digit
1587 /// hexadecimal-escape-sequence hexadecimal-digit
1588 /// universal-character-name: [C++11 lex.charset]
1590 /// \U hex-quad hex-quad
1592 /// hex-digit hex-digit hex-digit hex-digit
1595 CharLiteralParser::CharLiteralParser(const char *begin
, const char *end
,
1596 SourceLocation Loc
, Preprocessor
&PP
,
1597 tok::TokenKind kind
) {
1598 // At this point we know that the character matches the regex "(L|u|U)?'.*'".
1603 const char *TokBegin
= begin
;
1605 // Skip over wide character determinant.
1606 if (Kind
!= tok::char_constant
)
1608 if (Kind
== tok::utf8_char_constant
)
1611 // Skip over the entry quote.
1612 if (begin
[0] != '\'') {
1613 PP
.Diag(Loc
, diag::err_lexing_char
);
1620 // Remove an optional ud-suffix.
1621 if (end
[-1] != '\'') {
1622 const char *UDSuffixEnd
= end
;
1625 } while (end
[-1] != '\'');
1626 // FIXME: Don't bother with this if !tok.hasUCN().
1627 expandUCNs(UDSuffixBuf
, StringRef(end
, UDSuffixEnd
- end
));
1628 UDSuffixOffset
= end
- TokBegin
;
1631 // Trim the ending quote.
1632 assert(end
!= begin
&& "Invalid token lexed");
1635 // FIXME: The "Value" is an uint64_t so we can handle char literals of
1637 // FIXME: This extensively assumes that 'char' is 8-bits.
1638 assert(PP
.getTargetInfo().getCharWidth() == 8 &&
1639 "Assumes char is 8 bits");
1640 assert(PP
.getTargetInfo().getIntWidth() <= 64 &&
1641 (PP
.getTargetInfo().getIntWidth() & 7) == 0 &&
1642 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1643 assert(PP
.getTargetInfo().getWCharWidth() <= 64 &&
1644 "Assumes sizeof(wchar) on target is <= 64");
1646 SmallVector
<uint32_t, 4> codepoint_buffer
;
1647 codepoint_buffer
.resize(end
- begin
);
1648 uint32_t *buffer_begin
= &codepoint_buffer
.front();
1649 uint32_t *buffer_end
= buffer_begin
+ codepoint_buffer
.size();
1651 // Unicode escapes representing characters that cannot be correctly
1652 // represented in a single code unit are disallowed in character literals
1653 // by this implementation.
1654 uint32_t largest_character_for_kind
;
1655 if (tok::wide_char_constant
== Kind
) {
1656 largest_character_for_kind
=
1657 0xFFFFFFFFu
>> (32-PP
.getTargetInfo().getWCharWidth());
1658 } else if (tok::utf8_char_constant
== Kind
) {
1659 largest_character_for_kind
= 0x7F;
1660 } else if (tok::utf16_char_constant
== Kind
) {
1661 largest_character_for_kind
= 0xFFFF;
1662 } else if (tok::utf32_char_constant
== Kind
) {
1663 largest_character_for_kind
= 0x10FFFF;
1665 largest_character_for_kind
= 0x7Fu
;
1668 while (begin
!= end
) {
1669 // Is this a span of non-escape characters?
1670 if (begin
[0] != '\\') {
1671 char const *start
= begin
;
1674 } while (begin
!= end
&& *begin
!= '\\');
1676 char const *tmp_in_start
= start
;
1677 uint32_t *tmp_out_start
= buffer_begin
;
1678 llvm::ConversionResult res
=
1679 llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8
const **>(&start
),
1680 reinterpret_cast<llvm::UTF8
const *>(begin
),
1681 &buffer_begin
, buffer_end
, llvm::strictConversion
);
1682 if (res
!= llvm::conversionOK
) {
1683 // If we see bad encoding for unprefixed character literals, warn and
1684 // simply copy the byte values, for compatibility with gcc and
1685 // older versions of clang.
1686 bool NoErrorOnBadEncoding
= isOrdinary();
1687 unsigned Msg
= diag::err_bad_character_encoding
;
1688 if (NoErrorOnBadEncoding
)
1689 Msg
= diag::warn_bad_character_encoding
;
1691 if (NoErrorOnBadEncoding
) {
1692 start
= tmp_in_start
;
1693 buffer_begin
= tmp_out_start
;
1694 for (; start
!= begin
; ++start
, ++buffer_begin
)
1695 *buffer_begin
= static_cast<uint8_t>(*start
);
1700 for (; tmp_out_start
< buffer_begin
; ++tmp_out_start
) {
1701 if (*tmp_out_start
> largest_character_for_kind
) {
1703 PP
.Diag(Loc
, diag::err_character_too_large
);
1710 // Is this a Universal Character Name escape?
1711 if (begin
[1] == 'u' || begin
[1] == 'U' || begin
[1] == 'N') {
1712 unsigned short UcnLen
= 0;
1713 if (!ProcessUCNEscape(TokBegin
, begin
, end
, *buffer_begin
, UcnLen
,
1714 FullSourceLoc(Loc
, PP
.getSourceManager()),
1715 &PP
.getDiagnostics(), PP
.getLangOpts(), true)) {
1717 } else if (*buffer_begin
> largest_character_for_kind
) {
1719 PP
.Diag(Loc
, diag::err_character_too_large
);
1725 unsigned CharWidth
= getCharWidth(Kind
, PP
.getTargetInfo());
1727 ProcessCharEscape(TokBegin
, begin
, end
, HadError
,
1728 FullSourceLoc(Loc
,PP
.getSourceManager()),
1729 CharWidth
, &PP
.getDiagnostics(), PP
.getLangOpts());
1730 *buffer_begin
++ = result
;
1733 unsigned NumCharsSoFar
= buffer_begin
- &codepoint_buffer
.front();
1735 if (NumCharsSoFar
> 1) {
1736 if (isOrdinary() && NumCharsSoFar
== 4)
1737 PP
.Diag(Loc
, diag::warn_four_char_character_literal
);
1738 else if (isOrdinary())
1739 PP
.Diag(Loc
, diag::warn_multichar_character_literal
);
1741 PP
.Diag(Loc
, diag::err_multichar_character_literal
) << (isWide() ? 0 : 1);
1746 IsMultiChar
= false;
1749 llvm::APInt
LitVal(PP
.getTargetInfo().getIntWidth(), 0);
1751 // Narrow character literals act as though their value is concatenated
1752 // in this implementation, but warn on overflow.
1753 bool multi_char_too_long
= false;
1754 if (isOrdinary() && isMultiChar()) {
1756 for (size_t i
= 0; i
< NumCharsSoFar
; ++i
) {
1757 // check for enough leading zeros to shift into
1758 multi_char_too_long
|= (LitVal
.countLeadingZeros() < 8);
1760 LitVal
= LitVal
+ (codepoint_buffer
[i
] & 0xFF);
1762 } else if (NumCharsSoFar
> 0) {
1763 // otherwise just take the last character
1764 LitVal
= buffer_begin
[-1];
1767 if (!HadError
&& multi_char_too_long
) {
1768 PP
.Diag(Loc
, diag::warn_char_constant_too_large
);
1771 // Transfer the value from APInt to uint64_t
1772 Value
= LitVal
.getZExtValue();
1774 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1775 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
1776 // character constants are not sign extended in the this implementation:
1777 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
1778 if (isOrdinary() && NumCharsSoFar
== 1 && (Value
& 128) &&
1779 PP
.getLangOpts().CharIsSigned
)
1780 Value
= (signed char)Value
;
1784 /// string-literal: [C++0x lex.string]
1785 /// encoding-prefix " [s-char-sequence] "
1786 /// encoding-prefix R raw-string
1787 /// encoding-prefix:
1792 /// s-char-sequence:
1794 /// s-char-sequence s-char
1796 /// any member of the source character set except the double-quote ",
1797 /// backslash \, or new-line character
1799 /// universal-character-name
1801 /// " d-char-sequence ( r-char-sequence ) d-char-sequence "
1802 /// r-char-sequence:
1804 /// r-char-sequence r-char
1806 /// any member of the source character set, except a right parenthesis )
1807 /// followed by the initial d-char-sequence (which may be empty)
1808 /// followed by a double quote ".
1809 /// d-char-sequence:
1811 /// d-char-sequence d-char
1813 /// any member of the basic source character set except:
1814 /// space, the left parenthesis (, the right parenthesis ),
1815 /// the backslash \, and the control characters representing horizontal
1816 /// tab, vertical tab, form feed, and newline.
1817 /// escape-sequence: [C++0x lex.ccon]
1818 /// simple-escape-sequence
1819 /// octal-escape-sequence
1820 /// hexadecimal-escape-sequence
1821 /// simple-escape-sequence:
1822 /// one of \' \" \? \\ \a \b \f \n \r \t \v
1823 /// octal-escape-sequence:
1825 /// \ octal-digit octal-digit
1826 /// \ octal-digit octal-digit octal-digit
1827 /// hexadecimal-escape-sequence:
1828 /// \x hexadecimal-digit
1829 /// hexadecimal-escape-sequence hexadecimal-digit
1830 /// universal-character-name:
1832 /// \U hex-quad hex-quad
1834 /// hex-digit hex-digit hex-digit hex-digit
1837 StringLiteralParser::
1838 StringLiteralParser(ArrayRef
<Token
> StringToks
,
1840 : SM(PP
.getSourceManager()), Features(PP
.getLangOpts()),
1841 Target(PP
.getTargetInfo()), Diags(&PP
.getDiagnostics()),
1842 MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown
),
1843 ResultPtr(ResultBuf
.data()), hadError(false), Pascal(false) {
1847 void StringLiteralParser::init(ArrayRef
<Token
> StringToks
){
1848 // The literal token may have come from an invalid source location (e.g. due
1849 // to a PCH error), in which case the token length will be 0.
1850 if (StringToks
.empty() || StringToks
[0].getLength() < 2)
1851 return DiagnoseLexingError(SourceLocation());
1853 // Scan all of the string portions, remember the max individual token length,
1854 // computing a bound on the concatenated string length, and see whether any
1855 // piece is a wide-string. If any of the string portions is a wide-string
1856 // literal, the result is a wide-string literal [C99 6.4.5p4].
1857 assert(!StringToks
.empty() && "expected at least one token");
1858 MaxTokenLength
= StringToks
[0].getLength();
1859 assert(StringToks
[0].getLength() >= 2 && "literal token is invalid!");
1860 SizeBound
= StringToks
[0].getLength()-2; // -2 for "".
1861 Kind
= StringToks
[0].getKind();
1865 // Implement Translation Phase #6: concatenation of string literals
1866 /// (C99 5.1.1.2p1). The common case is only one string fragment.
1867 for (unsigned i
= 1; i
!= StringToks
.size(); ++i
) {
1868 if (StringToks
[i
].getLength() < 2)
1869 return DiagnoseLexingError(StringToks
[i
].getLocation());
1871 // The string could be shorter than this if it needs cleaning, but this is a
1872 // reasonable bound, which is all we need.
1873 assert(StringToks
[i
].getLength() >= 2 && "literal token is invalid!");
1874 SizeBound
+= StringToks
[i
].getLength()-2; // -2 for "".
1876 // Remember maximum string piece length.
1877 if (StringToks
[i
].getLength() > MaxTokenLength
)
1878 MaxTokenLength
= StringToks
[i
].getLength();
1880 // Remember if we see any wide or utf-8/16/32 strings.
1881 // Also check for illegal concatenations.
1882 if (StringToks
[i
].isNot(Kind
) && StringToks
[i
].isNot(tok::string_literal
)) {
1884 Kind
= StringToks
[i
].getKind();
1887 Diags
->Report(StringToks
[i
].getLocation(),
1888 diag::err_unsupported_string_concat
);
1894 // Include space for the null terminator.
1897 // TODO: K&R warning: "traditional C rejects string constant concatenation"
1899 // Get the width in bytes of char/wchar_t/char16_t/char32_t
1900 CharByteWidth
= getCharWidth(Kind
, Target
);
1901 assert((CharByteWidth
& 7) == 0 && "Assumes character size is byte multiple");
1904 // The output buffer size needs to be large enough to hold wide characters.
1905 // This is a worst-case assumption which basically corresponds to L"" "long".
1906 SizeBound
*= CharByteWidth
;
1908 // Size the temporary buffer to hold the result string data.
1909 ResultBuf
.resize(SizeBound
);
1911 // Likewise, but for each string piece.
1912 SmallString
<512> TokenBuf
;
1913 TokenBuf
.resize(MaxTokenLength
);
1915 // Loop over all the strings, getting their spelling, and expanding them to
1916 // wide strings as appropriate.
1917 ResultPtr
= &ResultBuf
[0]; // Next byte to fill in.
1921 SourceLocation UDSuffixTokLoc
;
1923 for (unsigned i
= 0, e
= StringToks
.size(); i
!= e
; ++i
) {
1924 const char *ThisTokBuf
= &TokenBuf
[0];
1925 // Get the spelling of the token, which eliminates trigraphs, etc. We know
1926 // that ThisTokBuf points to a buffer that is big enough for the whole token
1927 // and 'spelled' tokens can only shrink.
1928 bool StringInvalid
= false;
1929 unsigned ThisTokLen
=
1930 Lexer::getSpelling(StringToks
[i
], ThisTokBuf
, SM
, Features
,
1933 return DiagnoseLexingError(StringToks
[i
].getLocation());
1935 const char *ThisTokBegin
= ThisTokBuf
;
1936 const char *ThisTokEnd
= ThisTokBuf
+ThisTokLen
;
1938 // Remove an optional ud-suffix.
1939 if (ThisTokEnd
[-1] != '"') {
1940 const char *UDSuffixEnd
= ThisTokEnd
;
1943 } while (ThisTokEnd
[-1] != '"');
1945 StringRef
UDSuffix(ThisTokEnd
, UDSuffixEnd
- ThisTokEnd
);
1947 if (UDSuffixBuf
.empty()) {
1948 if (StringToks
[i
].hasUCN())
1949 expandUCNs(UDSuffixBuf
, UDSuffix
);
1951 UDSuffixBuf
.assign(UDSuffix
);
1953 UDSuffixOffset
= ThisTokEnd
- ThisTokBuf
;
1954 UDSuffixTokLoc
= StringToks
[i
].getLocation();
1956 SmallString
<32> ExpandedUDSuffix
;
1957 if (StringToks
[i
].hasUCN()) {
1958 expandUCNs(ExpandedUDSuffix
, UDSuffix
);
1959 UDSuffix
= ExpandedUDSuffix
;
1962 // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1963 // result of a concatenation involving at least one user-defined-string-
1964 // literal, all the participating user-defined-string-literals shall
1965 // have the same ud-suffix.
1966 if (UDSuffixBuf
!= UDSuffix
) {
1968 SourceLocation TokLoc
= StringToks
[i
].getLocation();
1969 Diags
->Report(TokLoc
, diag::err_string_concat_mixed_suffix
)
1970 << UDSuffixBuf
<< UDSuffix
1971 << SourceRange(UDSuffixTokLoc
, UDSuffixTokLoc
)
1972 << SourceRange(TokLoc
, TokLoc
);
1979 // Strip the end quote.
1982 // TODO: Input character set mapping support.
1984 // Skip marker for wide or unicode strings.
1985 if (ThisTokBuf
[0] == 'L' || ThisTokBuf
[0] == 'u' || ThisTokBuf
[0] == 'U') {
1987 // Skip 8 of u8 marker for utf8 strings.
1988 if (ThisTokBuf
[0] == '8')
1992 // Check for raw string
1993 if (ThisTokBuf
[0] == 'R') {
1994 if (ThisTokBuf
[1] != '"') {
1995 // The file may have come from PCH and then changed after loading the
1996 // PCH; Fail gracefully.
1997 return DiagnoseLexingError(StringToks
[i
].getLocation());
1999 ThisTokBuf
+= 2; // skip R"
2001 // C++11 [lex.string]p2: A `d-char-sequence` shall consist of at most 16
2003 constexpr unsigned MaxRawStrDelimLen
= 16;
2005 const char *Prefix
= ThisTokBuf
;
2006 while (static_cast<unsigned>(ThisTokBuf
- Prefix
) < MaxRawStrDelimLen
&&
2007 ThisTokBuf
[0] != '(')
2009 if (ThisTokBuf
[0] != '(')
2010 return DiagnoseLexingError(StringToks
[i
].getLocation());
2011 ++ThisTokBuf
; // skip '('
2013 // Remove same number of characters from the end
2014 ThisTokEnd
-= ThisTokBuf
- Prefix
;
2015 if (ThisTokEnd
< ThisTokBuf
)
2016 return DiagnoseLexingError(StringToks
[i
].getLocation());
2018 // C++14 [lex.string]p4: A source-file new-line in a raw string literal
2019 // results in a new-line in the resulting execution string-literal.
2020 StringRef
RemainingTokenSpan(ThisTokBuf
, ThisTokEnd
- ThisTokBuf
);
2021 while (!RemainingTokenSpan
.empty()) {
2022 // Split the string literal on \r\n boundaries.
2023 size_t CRLFPos
= RemainingTokenSpan
.find("\r\n");
2024 StringRef BeforeCRLF
= RemainingTokenSpan
.substr(0, CRLFPos
);
2025 StringRef AfterCRLF
= RemainingTokenSpan
.substr(CRLFPos
);
2027 // Copy everything before the \r\n sequence into the string literal.
2028 if (CopyStringFragment(StringToks
[i
], ThisTokBegin
, BeforeCRLF
))
2031 // Point into the \n inside the \r\n sequence and operate on the
2032 // remaining portion of the literal.
2033 RemainingTokenSpan
= AfterCRLF
.substr(1);
2036 if (ThisTokBuf
[0] != '"') {
2037 // The file may have come from PCH and then changed after loading the
2038 // PCH; Fail gracefully.
2039 return DiagnoseLexingError(StringToks
[i
].getLocation());
2041 ++ThisTokBuf
; // skip "
2043 // Check if this is a pascal string
2044 if (Features
.PascalStrings
&& ThisTokBuf
+ 1 != ThisTokEnd
&&
2045 ThisTokBuf
[0] == '\\' && ThisTokBuf
[1] == 'p') {
2047 // If the \p sequence is found in the first token, we have a pascal string
2048 // Otherwise, if we already have a pascal string, ignore the first \p
2056 while (ThisTokBuf
!= ThisTokEnd
) {
2057 // Is this a span of non-escape characters?
2058 if (ThisTokBuf
[0] != '\\') {
2059 const char *InStart
= ThisTokBuf
;
2062 } while (ThisTokBuf
!= ThisTokEnd
&& ThisTokBuf
[0] != '\\');
2064 // Copy the character span over.
2065 if (CopyStringFragment(StringToks
[i
], ThisTokBegin
,
2066 StringRef(InStart
, ThisTokBuf
- InStart
)))
2070 // Is this a Universal Character Name escape?
2071 if (ThisTokBuf
[1] == 'u' || ThisTokBuf
[1] == 'U' ||
2072 ThisTokBuf
[1] == 'N') {
2073 EncodeUCNEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
,
2074 ResultPtr
, hadError
,
2075 FullSourceLoc(StringToks
[i
].getLocation(), SM
),
2076 CharByteWidth
, Diags
, Features
);
2079 // Otherwise, this is a non-UCN escape character. Process it.
2080 unsigned ResultChar
=
2081 ProcessCharEscape(ThisTokBegin
, ThisTokBuf
, ThisTokEnd
, hadError
,
2082 FullSourceLoc(StringToks
[i
].getLocation(), SM
),
2083 CharByteWidth
*8, Diags
, Features
);
2085 if (CharByteWidth
== 4) {
2086 // FIXME: Make the type of the result buffer correct instead of
2087 // using reinterpret_cast.
2088 llvm::UTF32
*ResultWidePtr
= reinterpret_cast<llvm::UTF32
*>(ResultPtr
);
2089 *ResultWidePtr
= ResultChar
;
2091 } else if (CharByteWidth
== 2) {
2092 // FIXME: Make the type of the result buffer correct instead of
2093 // using reinterpret_cast.
2094 llvm::UTF16
*ResultWidePtr
= reinterpret_cast<llvm::UTF16
*>(ResultPtr
);
2095 *ResultWidePtr
= ResultChar
& 0xFFFF;
2098 assert(CharByteWidth
== 1 && "Unexpected char width");
2099 *ResultPtr
++ = ResultChar
& 0xFF;
2106 if (CharByteWidth
== 4) {
2107 // FIXME: Make the type of the result buffer correct instead of
2108 // using reinterpret_cast.
2109 llvm::UTF32
*ResultWidePtr
= reinterpret_cast<llvm::UTF32
*>(ResultBuf
.data());
2110 ResultWidePtr
[0] = GetNumStringChars() - 1;
2111 } else if (CharByteWidth
== 2) {
2112 // FIXME: Make the type of the result buffer correct instead of
2113 // using reinterpret_cast.
2114 llvm::UTF16
*ResultWidePtr
= reinterpret_cast<llvm::UTF16
*>(ResultBuf
.data());
2115 ResultWidePtr
[0] = GetNumStringChars() - 1;
2117 assert(CharByteWidth
== 1 && "Unexpected char width");
2118 ResultBuf
[0] = GetNumStringChars() - 1;
2121 // Verify that pascal strings aren't too large.
2122 if (GetStringLength() > 256) {
2124 Diags
->Report(StringToks
.front().getLocation(),
2125 diag::err_pascal_string_too_long
)
2126 << SourceRange(StringToks
.front().getLocation(),
2127 StringToks
.back().getLocation());
2132 // Complain if this string literal has too many characters.
2133 unsigned MaxChars
= Features
.CPlusPlus
? 65536 : Features
.C99
? 4095 : 509;
2135 if (GetNumStringChars() > MaxChars
)
2136 Diags
->Report(StringToks
.front().getLocation(),
2137 diag::ext_string_too_long
)
2138 << GetNumStringChars() << MaxChars
2139 << (Features
.CPlusPlus
? 2 : Features
.C99
? 1 : 0)
2140 << SourceRange(StringToks
.front().getLocation(),
2141 StringToks
.back().getLocation());
2145 static const char *resyncUTF8(const char *Err
, const char *End
) {
2148 End
= Err
+ std::min
<unsigned>(llvm::getNumBytesForUTF8(*Err
), End
-Err
);
2149 while (++Err
!= End
&& (*Err
& 0xC0) == 0x80)
2154 /// This function copies from Fragment, which is a sequence of bytes
2155 /// within Tok's contents (which begin at TokBegin) into ResultPtr.
2156 /// Performs widening for multi-byte characters.
2157 bool StringLiteralParser::CopyStringFragment(const Token
&Tok
,
2158 const char *TokBegin
,
2159 StringRef Fragment
) {
2160 const llvm::UTF8
*ErrorPtrTmp
;
2161 if (ConvertUTF8toWide(CharByteWidth
, Fragment
, ResultPtr
, ErrorPtrTmp
))
2164 // If we see bad encoding for unprefixed string literals, warn and
2165 // simply copy the byte values, for compatibility with gcc and older
2166 // versions of clang.
2167 bool NoErrorOnBadEncoding
= isOrdinary();
2168 if (NoErrorOnBadEncoding
) {
2169 memcpy(ResultPtr
, Fragment
.data(), Fragment
.size());
2170 ResultPtr
+= Fragment
.size();
2174 const char *ErrorPtr
= reinterpret_cast<const char *>(ErrorPtrTmp
);
2176 FullSourceLoc
SourceLoc(Tok
.getLocation(), SM
);
2177 const DiagnosticBuilder
&Builder
=
2178 Diag(Diags
, Features
, SourceLoc
, TokBegin
,
2179 ErrorPtr
, resyncUTF8(ErrorPtr
, Fragment
.end()),
2180 NoErrorOnBadEncoding
? diag::warn_bad_string_encoding
2181 : diag::err_bad_string_encoding
);
2183 const char *NextStart
= resyncUTF8(ErrorPtr
, Fragment
.end());
2184 StringRef
NextFragment(NextStart
, Fragment
.end()-NextStart
);
2186 // Decode into a dummy buffer.
2187 SmallString
<512> Dummy
;
2188 Dummy
.reserve(Fragment
.size() * CharByteWidth
);
2189 char *Ptr
= Dummy
.data();
2191 while (!ConvertUTF8toWide(CharByteWidth
, NextFragment
, Ptr
, ErrorPtrTmp
)) {
2192 const char *ErrorPtr
= reinterpret_cast<const char *>(ErrorPtrTmp
);
2193 NextStart
= resyncUTF8(ErrorPtr
, Fragment
.end());
2194 Builder
<< MakeCharSourceRange(Features
, SourceLoc
, TokBegin
,
2195 ErrorPtr
, NextStart
);
2196 NextFragment
= StringRef(NextStart
, Fragment
.end()-NextStart
);
2199 return !NoErrorOnBadEncoding
;
2202 void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc
) {
2205 Diags
->Report(Loc
, diag::err_lexing_string
);
2208 /// getOffsetOfStringByte - This function returns the offset of the
2209 /// specified byte of the string data represented by Token. This handles
2210 /// advancing over escape sequences in the string.
2211 unsigned StringLiteralParser::getOffsetOfStringByte(const Token
&Tok
,
2212 unsigned ByteNo
) const {
2213 // Get the spelling of the token.
2214 SmallString
<32> SpellingBuffer
;
2215 SpellingBuffer
.resize(Tok
.getLength());
2217 bool StringInvalid
= false;
2218 const char *SpellingPtr
= &SpellingBuffer
[0];
2219 unsigned TokLen
= Lexer::getSpelling(Tok
, SpellingPtr
, SM
, Features
,
2224 const char *SpellingStart
= SpellingPtr
;
2225 const char *SpellingEnd
= SpellingPtr
+TokLen
;
2227 // Handle UTF-8 strings just like narrow strings.
2228 if (SpellingPtr
[0] == 'u' && SpellingPtr
[1] == '8')
2231 assert(SpellingPtr
[0] != 'L' && SpellingPtr
[0] != 'u' &&
2232 SpellingPtr
[0] != 'U' && "Doesn't handle wide or utf strings yet");
2234 // For raw string literals, this is easy.
2235 if (SpellingPtr
[0] == 'R') {
2236 assert(SpellingPtr
[1] == '"' && "Should be a raw string literal!");
2239 while (*SpellingPtr
!= '(') {
2241 assert(SpellingPtr
< SpellingEnd
&& "Missing ( for raw string literal");
2245 return SpellingPtr
- SpellingStart
+ ByteNo
;
2248 // Skip over the leading quote
2249 assert(SpellingPtr
[0] == '"' && "Should be a string literal!");
2252 // Skip over bytes until we find the offset we're looking for.
2254 assert(SpellingPtr
< SpellingEnd
&& "Didn't find byte offset!");
2256 // Step over non-escapes simply.
2257 if (*SpellingPtr
!= '\\') {
2263 // Otherwise, this is an escape character. Advance over it.
2264 bool HadError
= false;
2265 if (SpellingPtr
[1] == 'u' || SpellingPtr
[1] == 'U' ||
2266 SpellingPtr
[1] == 'N') {
2267 const char *EscapePtr
= SpellingPtr
;
2268 unsigned Len
= MeasureUCNEscape(SpellingStart
, SpellingPtr
, SpellingEnd
,
2269 1, Features
, HadError
);
2271 // ByteNo is somewhere within the escape sequence.
2272 SpellingPtr
= EscapePtr
;
2277 ProcessCharEscape(SpellingStart
, SpellingPtr
, SpellingEnd
, HadError
,
2278 FullSourceLoc(Tok
.getLocation(), SM
),
2279 CharByteWidth
*8, Diags
, Features
);
2282 assert(!HadError
&& "This method isn't valid on erroneous strings");
2285 return SpellingPtr
-SpellingStart
;
2288 /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved
2289 /// suffixes as ud-suffixes, because the diagnostic experience is better if we
2290 /// treat it as an invalid suffix.
2291 bool StringLiteralParser::isValidUDSuffix(const LangOptions
&LangOpts
,
2293 return NumericLiteralParser::isValidUDSuffix(LangOpts
, Suffix
) ||