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;
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();
62 const char *TokRangeBegin,
63 const char *TokRangeEnd) {
80 const char *TokBegin,
const char *TokRangeBegin,
81 const char *TokRangeEnd,
unsigned DiagID) {
92 const char *&ThisTokBuf,
93 const char *ThisTokEnd,
bool &HadError,
97 const char *EscapeBegin = ThisTokBuf;
98 bool Delimited =
false;
99 bool EndDelimiterFound =
false;
106 unsigned ResultChar = *ThisTokBuf++;
107 switch (ResultChar) {
109 case '\\':
case '\'':
case '"':
case '?':
break;
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";
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";
164 bool Overflow =
false;
165 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
166 if (Delimited && *ThisTokBuf ==
'}') {
168 EndDelimiterFound =
true;
171 int CharVal = llvm::hexDigitValue(*ThisTokBuf);
178 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
179 diag::err_delimited_escape_invalid)
180 << StringRef(ThisTokBuf, 1);
184 if (ResultChar & 0xF0000000)
187 ResultChar |= CharVal;
190 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
192 ResultChar &= ~0
U >> (32-CharWidth);
196 if (!HadError && Overflow) {
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': {
213 unsigned NumDigits = 0;
216 ResultChar |= *ThisTokBuf++ -
'0';
218 }
while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
219 ThisTokBuf[0] >=
'0' && ThisTokBuf[0] <=
'7');
222 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
224 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
225 diag::err_escape_too_large) << 1;
226 ResultChar &= ~0
U >> (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';
272 (Overflow || (CharWidth != 32 && (ResultChar >> CharWidth) != 0))) {
275 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
276 diag::err_escape_too_large)
278 ResultChar &= ~0
U >> (32 - CharWidth);
283 case '(':
case '{':
case '[':
case '%':
286 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
287 diag::ext_nonstandard_escape)
295 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
296 diag::ext_unknown_escape)
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);
322 char *ResultPtr = ResultBuf;
323 if (llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr))
324 Str.append(ResultBuf, ResultPtr);
328 for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
339 uint32_t CodePoint = 0;
341 if (
Kind ==
'u' && *I ==
'{') {
342 for (++I; *I !=
'}'; ++I) {
343 unsigned Value = llvm::hexDigitValue(*I);
352 unsigned NumHexDigits;
358 assert(I + NumHexDigits <= E);
360 for (; NumHexDigits != 0; ++I, --NumHexDigits) {
361 unsigned Value = llvm::hexDigitValue(*I);
376 const char *ThisTokEnd,
377 uint32_t &UcnVal,
unsigned short &UcnLen,
380 bool in_char_string_literal =
false) {
381 const char *UcnBegin = ThisTokBuf;
386 bool Delimited =
false;
387 bool EndDelimiterFound =
false;
388 bool HasError =
false;
390 if (UcnBegin[1] ==
'u' && in_char_string_literal &&
391 ThisTokBuf != ThisTokEnd && *ThisTokBuf ==
'{') {
394 }
else if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
396 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
397 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
400 UcnLen = (ThisTokBuf[-1] ==
'u' ? 4 : 8);
402 bool Overflow =
false;
403 unsigned short Count = 0;
404 for (; ThisTokBuf != ThisTokEnd && (Delimited || Count != UcnLen);
406 if (Delimited && *ThisTokBuf ==
'}') {
408 EndDelimiterFound =
true;
411 int CharVal = llvm::hexDigitValue(*ThisTokBuf);
417 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
418 diag::err_delimited_escape_invalid)
419 << StringRef(ThisTokBuf, 1);
424 if (UcnVal & 0xF0000000) {
435 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
436 diag::err_escape_too_large)
441 if (Delimited && !EndDelimiterFound) {
443 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
451 if (Count == 0 || (!Delimited && Count != UcnLen)) {
453 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
454 Delimited ? diag::err_delimited_escape_empty
455 : diag::err_ucn_escape_incomplete);
463 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) ||
466 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
467 diag::err_ucn_escape_invalid);
474 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) {
475 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
477 char BasicSCSChar = UcnVal;
478 if (UcnVal >= 0x20 && UcnVal < 0x7f)
479 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
480 IsError ? diag::err_ucn_escape_basic_scs :
481 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
482 << StringRef(&BasicSCSChar, 1);
484 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
485 IsError ? diag::err_ucn_control_character :
486 diag::warn_cxx98_compat_literal_ucn_control_character);
492 if (!Features.CPlusPlus && !Features.C99 && Diags)
493 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
494 diag::warn_ucn_not_valid_in_c89_literal);
496 if (Delimited && Diags)
497 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
498 diag::ext_delimited_escape_sequence);
506 const char *ThisTokEnd,
unsigned CharByteWidth,
509 if (CharByteWidth == 4)
513 unsigned short UcnLen = 0;
517 UcnLen, Loc,
nullptr, Features,
true)) {
523 if (CharByteWidth == 2)
524 return UcnVal <= 0xFFFF ? 2 : 4;
531 if (UcnVal < 0x10000)
541 const char *ThisTokEnd,
542 char *&ResultBuf,
bool &HadError,
546 typedef uint32_t UTF32;
548 unsigned short UcnLen = 0;
550 Loc, Diags, Features,
true)) {
555 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
556 "only character widths of 1, 2, or 4 bytes supported");
559 assert((UcnLen== 4 || UcnLen== 8) &&
"only ucn length of 4 or 8 supported");
561 if (CharByteWidth == 4) {
564 llvm::UTF32 *ResultPtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf);
570 if (CharByteWidth == 2) {
573 llvm::UTF16 *ResultPtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf);
575 if (UcnVal <= (UTF32)0xFFFF) {
583 *ResultPtr = 0xD800 + (UcnVal >> 10);
584 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
589 assert(CharByteWidth == 1 &&
"UTF-8 encoding is only for 1 byte characters");
595 typedef uint8_t UTF8;
597 unsigned short bytesToWrite = 0;
598 if (UcnVal < (UTF32)0x80)
600 else if (UcnVal < (UTF32)0x800)
602 else if (UcnVal < (UTF32)0x10000)
607 const unsigned byteMask = 0xBF;
608 const unsigned byteMark = 0x80;
612 static const UTF8 firstByteMark[5] = {
613 0x00, 0x00, 0xC0, 0xE0, 0xF0
616 ResultBuf += bytesToWrite;
617 switch (bytesToWrite) {
619 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
622 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
625 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
628 *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
631 ResultBuf += bytesToWrite;
691 :
SM(
SM), LangOpts(LangOpts), Diags(Diags),
692 ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
694 s = DigitsBegin = ThisTokBegin;
695 saw_exponent =
false;
697 saw_ud_suffix =
false;
698 saw_fixed_point_suffix =
false;
719 Diags.
Report(TokLoc, diag::err_lexing_numeric);
725 ParseNumberStartingWithZero(TokLoc);
731 if (s == ThisTokEnd) {
734 ParseDecimalOrOctalCommon(TokLoc);
741 checkSeparator(TokLoc, s, CSK_AfterDigits);
744 if (LangOpts.FixedPoint) {
745 for (
const char *
c = s;
c != ThisTokEnd; ++
c) {
746 if (*
c ==
'r' || *
c ==
'k' || *
c ==
'R' || *
c ==
'K') {
747 saw_fixed_point_suffix =
true;
757 bool HasSize =
false;
761 for (; s != ThisTokEnd; ++s) {
765 if (!LangOpts.FixedPoint)
768 if (!(saw_period || saw_exponent))
break;
773 if (!LangOpts.FixedPoint)
776 if (!(saw_period || saw_exponent))
break;
782 if (!(LangOpts.Half || LangOpts.FixedPoint))
792 if (!isFPConstant)
break;
800 if ((
Target.hasFloat16Type() || LangOpts.CUDA) && s + 2 < ThisTokEnd &&
801 s[1] ==
'1' && s[2] ==
'6') {
811 if (!isFPConstant)
break;
819 if (isFPConstant)
break;
831 assert(s + 1 < ThisTokEnd &&
"didn't maximally munch?");
832 if (isFPConstant)
break;
850 if (LangOpts.MicrosoftExt && !isFPConstant) {
887 assert(s <= ThisTokEnd &&
"didn't maximally munch?");
908 if (!LangOpts.CPlusPlus && ((s[0] ==
'w' && s[1] ==
'b') ||
909 (s[0] ==
'W' && s[1] ==
'B'))) {
923 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
938 saw_fixed_point_suffix =
false;
943 saw_ud_suffix =
true;
947 if (s != ThisTokEnd) {
950 TokLoc, SuffixBegin - ThisTokBegin,
SM, LangOpts),
951 diag::err_invalid_suffix_constant)
952 << StringRef(SuffixBegin, ThisTokEnd - SuffixBegin)
953 << (isFixedPointConstant ? 2 : isFPConstant);
958 if (!
hadError && saw_fixed_point_suffix) {
966 void NumericLiteralParser::ParseDecimalOrOctalCommon(
SourceLocation TokLoc){
967 assert((radix == 8 || radix == 10) &&
"Unexpected radix");
971 if (
isHexDigit(*s) && *s !=
'e' && *s !=
'E' &&
975 diag::err_invalid_digit)
976 << StringRef(s, 1) << (radix == 8 ? 1 : 0);
982 checkSeparator(TokLoc, s, CSK_AfterDigits);
986 checkSeparator(TokLoc, s, CSK_BeforeDigits);
989 if (*s ==
'e' || *s ==
'E') {
990 checkSeparator(TokLoc, s, CSK_AfterDigits);
991 const char *Exponent = s;
995 if (s != ThisTokEnd && (*s ==
'+' || *s ==
'-')) s++;
996 const char *first_non_digit = SkipDigits(s);
997 if (containsDigits(s, first_non_digit)) {
998 checkSeparator(TokLoc, s, CSK_BeforeDigits);
1003 TokLoc, Exponent - ThisTokBegin, SM, LangOpts),
1004 diag::err_exponent_has_no_digits);
1017 if (!LangOpts.CPlusPlus11 || Suffix.empty())
1021 if (Suffix[0] ==
'_')
1025 if (!LangOpts.CPlusPlus14)
1031 return llvm::StringSwitch<bool>(Suffix)
1032 .Cases(
"h",
"min",
"s",
true)
1033 .Cases(
"ms",
"us",
"ns",
true)
1034 .Cases(
"il",
"i",
"if",
true)
1035 .Cases(
"d",
"y", LangOpts.CPlusPlus20)
1041 CheckSeparatorKind IsAfterDigits) {
1042 if (IsAfterDigits == CSK_AfterDigits) {
1043 if (Pos == ThisTokBegin)
1046 }
else if (Pos == ThisTokEnd)
1049 if (isDigitSeparator(*Pos)) {
1052 diag::err_digit_separator_not_between_digits)
1063 void NumericLiteralParser::ParseNumberStartingWithZero(
SourceLocation TokLoc) {
1064 assert(s[0] ==
'0' &&
"Invalid method call");
1070 if ((c1 ==
'x' || c1 ==
'X') && (
isHexDigit(s[1]) || s[1] ==
'.')) {
1072 assert(s < ThisTokEnd &&
"didn't maximally munch?");
1075 s = SkipHexDigits(s);
1076 bool HasSignificandDigits = containsDigits(DigitsBegin, s);
1077 if (s == ThisTokEnd) {
1079 }
else if (*s ==
'.') {
1082 const char *floatDigitsBegin = s;
1083 s = SkipHexDigits(s);
1084 if (containsDigits(floatDigitsBegin, s))
1085 HasSignificandDigits =
true;
1086 if (HasSignificandDigits)
1087 checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits);
1090 if (!HasSignificandDigits) {
1093 diag::err_hex_constant_requires)
1094 << LangOpts.CPlusPlus << 1;
1101 if (*s ==
'p' || *s ==
'P') {
1102 checkSeparator(TokLoc, s, CSK_AfterDigits);
1103 const char *Exponent = s;
1105 saw_exponent =
true;
1106 if (s != ThisTokEnd && (*s ==
'+' || *s ==
'-')) s++;
1107 const char *first_non_digit = SkipDigits(s);
1108 if (!containsDigits(s, first_non_digit)) {
1111 TokLoc, Exponent - ThisTokBegin, SM, LangOpts),
1112 diag::err_exponent_has_no_digits);
1117 checkSeparator(TokLoc, s, CSK_BeforeDigits);
1118 s = first_non_digit;
1120 if (!LangOpts.HexFloats)
1121 Diags.
Report(TokLoc, LangOpts.CPlusPlus
1122 ? diag::ext_hex_literal_invalid
1123 : diag::ext_hex_constant_invalid);
1124 else if (LangOpts.CPlusPlus17)
1125 Diags.
Report(TokLoc, diag::warn_cxx17_hex_literal);
1126 }
else if (saw_period) {
1129 diag::err_hex_constant_requires)
1130 << LangOpts.CPlusPlus << 0;
1137 if ((c1 ==
'b' || c1 ==
'B') && (s[1] ==
'0' || s[1] ==
'1')) {
1139 Diags.
Report(TokLoc, LangOpts.CPlusPlus14
1140 ? diag::warn_cxx11_compat_binary_literal
1141 : LangOpts.CPlusPlus ? diag::ext_binary_literal_cxx14
1142 : diag::ext_binary_literal);
1144 assert(s < ThisTokEnd &&
"didn't maximally munch?");
1147 s = SkipBinaryDigits(s);
1148 if (s == ThisTokEnd) {
1154 diag::err_invalid_digit)
1155 << StringRef(s, 1) << 2;
1166 const char *PossibleNewDigitStart = s;
1167 s = SkipOctalDigits(s);
1171 if (s != PossibleNewDigitStart)
1172 DigitsBegin = PossibleNewDigitStart;
1174 if (s == ThisTokEnd)
1180 const char *EndDecimal = SkipDigits(s);
1181 if (EndDecimal[0] ==
'.' || EndDecimal[0] ==
'e' || EndDecimal[0] ==
'E') {
1187 ParseDecimalOrOctalCommon(TokLoc);
1193 return NumDigits <= 64;
1195 return NumDigits <= 64 / 3;
1197 return NumDigits <= 19;
1199 return NumDigits <= 64 / 4;
1201 llvm_unreachable(
"impossible Radix");
1215 const unsigned NumDigits = SuffixBegin - DigitsBegin;
1218 for (
const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
1219 if (!isDigitSeparator(*Ptr))
1220 N = N * radix + llvm::hexDigitValue(*Ptr);
1225 return Val.getZExtValue() != N;
1229 const char *Ptr = DigitsBegin;
1235 bool OverflowOccurred =
false;
1236 while (Ptr < SuffixBegin) {
1237 if (isDigitSeparator(*Ptr)) {
1242 unsigned C = llvm::hexDigitValue(*Ptr++);
1245 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1255 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
1260 OverflowOccurred |= Val.ult(CharVal);
1262 return OverflowOccurred;
1265 llvm::APFloat::opStatus
1267 using llvm::APFloat;
1269 unsigned n =
std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
1272 StringRef Str(ThisTokBegin, n);
1273 if (Str.contains(
'\'')) {
1275 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
1281 Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
1282 assert(StatusOrErr &&
"Invalid floating point representation");
1283 return !errorToBool(StatusOrErr.takeError()) ? *StatusOrErr
1284 : APFloat::opInvalidOp;
1288 return c ==
'p' ||
c ==
'P' ||
c ==
'e' ||
c ==
'E';
1292 assert(radix == 16 || radix == 10);
1295 unsigned NumDigits = SuffixBegin - DigitsBegin;
1296 if (saw_period) --NumDigits;
1299 bool ExpOverflowOccurred =
false;
1300 bool NegativeExponent =
false;
1301 const char *ExponentBegin;
1302 uint64_t Exponent = 0;
1303 int64_t BaseShift = 0;
1305 const char *Ptr = DigitsBegin;
1308 ExponentBegin = Ptr;
1310 NegativeExponent = *Ptr ==
'-';
1311 if (NegativeExponent) ++Ptr;
1313 unsigned NumExpDigits = SuffixBegin - Ptr;
1315 llvm::StringRef ExpStr(Ptr, NumExpDigits);
1317 Exponent = ExpInt.getZExtValue();
1319 ExpOverflowOccurred =
true;
1322 if (NegativeExponent) BaseShift -= Exponent;
1323 else BaseShift += Exponent;
1343 uint64_t NumBitsNeeded;
1345 NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale;
1347 NumBitsNeeded = 4 * NumDigits + Exponent + Scale;
1350 ExpOverflowOccurred =
true;
1351 llvm::APInt Val(
static_cast<unsigned>(NumBitsNeeded), 0,
false);
1353 bool FoundDecimal =
false;
1355 int64_t FractBaseShift = 0;
1356 const char *
End = saw_exponent ? ExponentBegin : SuffixBegin;
1357 for (
const char *Ptr = DigitsBegin; Ptr <
End; ++Ptr) {
1359 FoundDecimal =
true;
1364 unsigned C = llvm::hexDigitValue(*Ptr);
1365 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1377 if (radix == 16) FractBaseShift *= 4;
1378 BaseShift += FractBaseShift;
1382 uint64_t
Base = (radix == 16) ? 2 : 10;
1383 if (BaseShift > 0) {
1384 for (int64_t i = 0; i < BaseShift; ++i) {
1387 }
else if (BaseShift < 0) {
1388 for (int64_t i = BaseShift; i < 0 && !Val.isZero(); ++i)
1389 Val = Val.udiv(
Base);
1392 bool IntOverflowOccurred =
false;
1393 auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth());
1394 if (Val.getBitWidth() > StoreVal.getBitWidth()) {
1395 IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth()));
1396 StoreVal = Val.trunc(StoreVal.getBitWidth());
1397 }
else if (Val.getBitWidth() < StoreVal.getBitWidth()) {
1398 IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal);
1399 StoreVal = Val.zext(StoreVal.getBitWidth());
1404 return IntOverflowOccurred || ExpOverflowOccurred;
1454 const char *TokBegin = begin;
1457 if (Kind != tok::char_constant)
1459 if (Kind == tok::utf8_char_constant)
1463 if (begin[0] !=
'\'') {
1464 PP.
Diag(Loc, diag::err_lexing_char);
1472 if (end[-1] !=
'\'') {
1473 const char *UDSuffixEnd = end;
1476 }
while (end[-1] !=
'\'');
1478 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1479 UDSuffixOffset = end - TokBegin;
1483 assert(end != begin &&
"Invalid token lexed");
1490 "Assumes char is 8 bits");
1493 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1495 "Assumes sizeof(wchar) on target is <= 64");
1498 codepoint_buffer.resize(end - begin);
1499 uint32_t *buffer_begin = &codepoint_buffer.front();
1500 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1505 uint32_t largest_character_for_kind;
1506 if (tok::wide_char_constant == Kind) {
1507 largest_character_for_kind =
1509 }
else if (tok::utf8_char_constant == Kind) {
1510 largest_character_for_kind = 0x7F;
1511 }
else if (tok::utf16_char_constant == Kind) {
1512 largest_character_for_kind = 0xFFFF;
1513 }
else if (tok::utf32_char_constant == Kind) {
1514 largest_character_for_kind = 0x10FFFF;
1516 largest_character_for_kind = 0x7Fu;
1519 while (begin != end) {
1521 if (begin[0] !=
'\\') {
1522 char const *start = begin;
1525 }
while (begin != end && *begin !=
'\\');
1527 char const *tmp_in_start = start;
1528 uint32_t *tmp_out_start = buffer_begin;
1529 llvm::ConversionResult res =
1530 llvm::ConvertUTF8toUTF32(
reinterpret_cast<llvm::UTF8
const **
>(&start),
1531 reinterpret_cast<llvm::UTF8
const *
>(begin),
1532 &buffer_begin, buffer_end, llvm::strictConversion);
1533 if (res != llvm::conversionOK) {
1537 bool NoErrorOnBadEncoding =
isAscii();
1538 unsigned Msg = diag::err_bad_character_encoding;
1539 if (NoErrorOnBadEncoding)
1540 Msg = diag::warn_bad_character_encoding;
1542 if (NoErrorOnBadEncoding) {
1543 start = tmp_in_start;
1544 buffer_begin = tmp_out_start;
1545 for (; start != begin; ++start, ++buffer_begin)
1546 *buffer_begin =
static_cast<uint8_t
>(*start);
1551 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1552 if (*tmp_out_start > largest_character_for_kind) {
1554 PP.
Diag(Loc, diag::err_character_too_large);
1562 if (begin[1] ==
'u' || begin[1] ==
'U') {
1563 unsigned short UcnLen = 0;
1568 }
else if (*buffer_begin > largest_character_for_kind) {
1570 PP.
Diag(Loc, diag::err_character_too_large);
1581 *buffer_begin++ = result;
1584 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1586 if (NumCharsSoFar > 1) {
1587 if (
isAscii() && NumCharsSoFar == 4)
1588 PP.
Diag(Loc, diag::warn_four_char_character_literal);
1590 PP.
Diag(Loc, diag::warn_multichar_character_literal);
1592 PP.
Diag(Loc, diag::err_multichar_character_literal) << (
isWide() ? 0 : 1);
1597 IsMultiChar =
false;
1604 bool multi_char_too_long =
false;
1607 for (
size_t i = 0; i < NumCharsSoFar; ++i) {
1609 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1611 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1613 }
else if (NumCharsSoFar > 0) {
1615 LitVal = buffer_begin[-1];
1618 if (!HadError && multi_char_too_long) {
1619 PP.
Diag(Loc, diag::warn_char_constant_too_large);
1623 Value = LitVal.getZExtValue();
1629 if (
isAscii() && NumCharsSoFar == 1 && (
Value & 128) &&
1691 :
SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1692 Target(PP.getTargetInfo()), Diags(&PP.getDiagnostics()),
1693 MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown),
1694 ResultPtr(ResultBuf.data()), hadError(
false), Pascal(
false) {
1701 if (StringToks.empty() || StringToks[0].getLength() < 2)
1708 assert(!StringToks.empty() &&
"expected at least one token");
1709 MaxTokenLength = StringToks[0].getLength();
1710 assert(StringToks[0].getLength() >= 2 &&
"literal token is invalid!");
1711 SizeBound = StringToks[0].getLength()-2;
1712 Kind = StringToks[0].getKind();
1718 for (
unsigned i = 1; i != StringToks.size(); ++i) {
1719 if (StringToks[i].getLength() < 2)
1720 return DiagnoseLexingError(StringToks[i].getLocation());
1724 assert(StringToks[i].getLength() >= 2 &&
"literal token is invalid!");
1725 SizeBound += StringToks[i].getLength()-2;
1728 if (StringToks[i].getLength() > MaxTokenLength)
1729 MaxTokenLength = StringToks[i].getLength();
1733 if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
1735 Kind = StringToks[i].getKind();
1738 Diags->
Report(StringToks[i].getLocation(),
1739 diag::err_unsupported_string_concat);
1752 assert((CharByteWidth & 7) == 0 &&
"Assumes character size is byte multiple");
1757 SizeBound *= CharByteWidth;
1760 ResultBuf.resize(SizeBound);
1764 TokenBuf.resize(MaxTokenLength);
1768 ResultPtr = &ResultBuf[0];
1774 for (
unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1775 const char *ThisTokBuf = &TokenBuf[0];
1779 bool StringInvalid =
false;
1780 unsigned ThisTokLen =
1784 return DiagnoseLexingError(StringToks[i].getLocation());
1786 const char *ThisTokBegin = ThisTokBuf;
1787 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1790 if (ThisTokEnd[-1] !=
'"') {
1791 const char *UDSuffixEnd = ThisTokEnd;
1794 }
while (ThisTokEnd[-1] !=
'"');
1796 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1798 if (UDSuffixBuf.empty()) {
1799 if (StringToks[i].hasUCN())
1802 UDSuffixBuf.assign(UDSuffix);
1804 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1805 UDSuffixTokLoc = StringToks[i].getLocation();
1808 if (StringToks[i].hasUCN()) {
1810 UDSuffix = ExpandedUDSuffix;
1817 if (UDSuffixBuf != UDSuffix) {
1820 Diags->
Report(TokLoc, diag::err_string_concat_mixed_suffix)
1821 << UDSuffixBuf << UDSuffix
1836 if (ThisTokBuf[0] ==
'L' || ThisTokBuf[0] ==
'u' || ThisTokBuf[0] ==
'U') {
1839 if (ThisTokBuf[0] ==
'8')
1844 if (ThisTokBuf[0] ==
'R') {
1845 if (ThisTokBuf[1] !=
'"') {
1848 return DiagnoseLexingError(StringToks[i].getLocation());
1854 constexpr
unsigned MaxRawStrDelimLen = 16;
1856 const char *Prefix = ThisTokBuf;
1857 while (
static_cast<unsigned>(ThisTokBuf - Prefix) < MaxRawStrDelimLen &&
1858 ThisTokBuf[0] !=
'(')
1860 if (ThisTokBuf[0] !=
'(')
1861 return DiagnoseLexingError(StringToks[i].getLocation());
1865 ThisTokEnd -= ThisTokBuf - Prefix;
1866 if (ThisTokEnd < ThisTokBuf)
1867 return DiagnoseLexingError(StringToks[i].getLocation());
1871 StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf);
1872 while (!RemainingTokenSpan.empty()) {
1874 size_t CRLFPos = RemainingTokenSpan.find(
"\r\n");
1875 StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos);
1876 StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos);
1879 if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF))
1884 RemainingTokenSpan = AfterCRLF.substr(1);
1887 if (ThisTokBuf[0] !=
'"') {
1890 return DiagnoseLexingError(StringToks[i].getLocation());
1895 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1896 ThisTokBuf[0] ==
'\\' && ThisTokBuf[1] ==
'p') {
1907 while (ThisTokBuf != ThisTokEnd) {
1909 if (ThisTokBuf[0] !=
'\\') {
1910 const char *InStart = ThisTokBuf;
1913 }
while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] !=
'\\');
1916 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1917 StringRef(InStart, ThisTokBuf - InStart)))
1922 if (ThisTokBuf[1] ==
'u' || ThisTokBuf[1] ==
'U') {
1926 CharByteWidth, Diags, Features);
1930 unsigned ResultChar =
1933 CharByteWidth*8, Diags, Features);
1935 if (CharByteWidth == 4) {
1938 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultPtr);
1939 *ResultWidePtr = ResultChar;
1941 }
else if (CharByteWidth == 2) {
1944 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultPtr);
1945 *ResultWidePtr = ResultChar & 0xFFFF;
1948 assert(CharByteWidth == 1 &&
"Unexpected char width");
1949 *ResultPtr++ = ResultChar & 0xFF;
1956 if (CharByteWidth == 4) {
1959 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf.data());
1961 }
else if (CharByteWidth == 2) {
1964 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf.data());
1967 assert(CharByteWidth == 1 &&
"Unexpected char width");
1974 Diags->
Report(StringToks.front().getLocation(),
1975 diag::err_pascal_string_too_long)
1977 StringToks.back().getLocation());
1983 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1986 Diags->
Report(StringToks.front().getLocation(),
1987 diag::ext_string_too_long)
1989 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1991 StringToks.back().getLocation());
1998 End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err),
End-Err);
1999 while (++Err !=
End && (*Err & 0xC0) == 0x80)
2007 bool StringLiteralParser::CopyStringFragment(
const Token &Tok,
2008 const char *TokBegin,
2009 StringRef Fragment) {
2010 const llvm::UTF8 *ErrorPtrTmp;
2011 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
2017 bool NoErrorOnBadEncoding =
isAscii();
2018 if (NoErrorOnBadEncoding) {
2019 memcpy(ResultPtr, Fragment.data(), Fragment.size());
2020 ResultPtr += Fragment.size();
2024 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
2028 Diag(Diags, Features, SourceLoc, TokBegin,
2029 ErrorPtr,
resyncUTF8(ErrorPtr, Fragment.end()),
2030 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
2031 : diag::err_bad_string_encoding);
2033 const char *NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
2034 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
2038 Dummy.reserve(Fragment.size() * CharByteWidth);
2039 char *Ptr = Dummy.data();
2041 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
2042 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
2043 NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
2045 ErrorPtr, NextStart);
2046 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
2049 return !NoErrorOnBadEncoding;
2052 void StringLiteralParser::DiagnoseLexingError(
SourceLocation Loc) {
2055 Diags->
Report(Loc, diag::err_lexing_string);
2062 unsigned ByteNo)
const {
2067 bool StringInvalid =
false;
2068 const char *SpellingPtr = &SpellingBuffer[0];
2074 const char *SpellingStart = SpellingPtr;
2075 const char *SpellingEnd = SpellingPtr+TokLen;
2078 if (SpellingPtr[0] ==
'u' && SpellingPtr[1] ==
'8')
2081 assert(SpellingPtr[0] !=
'L' && SpellingPtr[0] !=
'u' &&
2082 SpellingPtr[0] !=
'U' &&
"Doesn't handle wide or utf strings yet");
2085 if (SpellingPtr[0] ==
'R') {
2086 assert(SpellingPtr[1] ==
'"' &&
"Should be a raw string literal!");
2089 while (*SpellingPtr !=
'(') {
2091 assert(SpellingPtr < SpellingEnd &&
"Missing ( for raw string literal");
2095 return SpellingPtr - SpellingStart + ByteNo;
2099 assert(SpellingPtr[0] ==
'"' &&
"Should be a string literal!");
2104 assert(SpellingPtr < SpellingEnd &&
"Didn't find byte offset!");
2107 if (*SpellingPtr !=
'\\') {
2114 bool HadError =
false;
2115 if (SpellingPtr[1] ==
'u' || SpellingPtr[1] ==
'U') {
2116 const char *EscapePtr = SpellingPtr;
2118 1, Features, HadError);
2121 SpellingPtr = EscapePtr;
2128 CharByteWidth*8, Diags, Features);
2131 assert(!HadError &&
"This method isn't valid on erroneous strings");
2134 return SpellingPtr-SpellingStart;