23 #include "llvm/ADT/APInt.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/ADT/StringExtras.h" 26 #include "llvm/ADT/StringSwitch.h" 27 #include "llvm/Support/ConvertUTF.h" 28 #include "llvm/Support/ErrorHandling.h" 36 using namespace clang;
40 default: llvm_unreachable(
"Unknown token type!");
41 case tok::char_constant:
42 case tok::string_literal:
43 case tok::utf8_char_constant:
44 case tok::utf8_string_literal:
46 case tok::wide_char_constant:
47 case tok::wide_string_literal:
49 case tok::utf16_char_constant:
50 case tok::utf16_string_literal:
52 case tok::utf32_char_constant:
53 case tok::utf32_string_literal:
61 const char *TokRangeBegin,
62 const char *TokRangeEnd) {
79 const char *TokBegin,
const char *TokRangeBegin,
80 const char *TokRangeEnd,
unsigned DiagID) {
84 return Diags->
Report(Begin, DiagID) <<
91 const char *&ThisTokBuf,
92 const char *ThisTokEnd,
bool &HadError,
96 const char *EscapeBegin = ThisTokBuf;
103 unsigned ResultChar = *ThisTokBuf++;
104 switch (ResultChar) {
106 case '\\':
case '\'':
case '"':
case '?':
break;
118 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
119 diag::ext_nonstandard_escape) <<
"e";
124 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
125 diag::ext_nonstandard_escape) <<
"E";
145 if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
147 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
148 diag::err_hex_escape_no_digits) <<
"x";
154 bool Overflow =
false;
155 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
156 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
157 if (CharVal == -1)
break;
159 if (ResultChar & 0xF0000000)
162 ResultChar |= CharVal;
166 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
168 ResultChar &= ~0U >> (32-CharWidth);
172 if (Overflow && Diags)
173 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
174 diag::err_escape_too_large) << 0;
177 case '0':
case '1':
case '2':
case '3':
178 case '4':
case '5':
case '6':
case '7': {
185 unsigned NumDigits = 0;
188 ResultChar |= *ThisTokBuf++ -
'0';
190 }
while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
191 ThisTokBuf[0] >=
'0' && ThisTokBuf[0] <=
'7');
194 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
196 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
197 diag::err_escape_too_large) << 1;
198 ResultChar &= ~0U >> (32-CharWidth);
204 case '(':
case '{':
case '[':
case '%':
207 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
208 diag::ext_nonstandard_escape)
209 << std::string(1, ResultChar);
216 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
217 diag::ext_unknown_escape)
218 << std::string(1, ResultChar);
220 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
221 diag::ext_unknown_escape)
222 <<
"x" + llvm::utohexstr(ResultChar);
232 char *ResultPtr = ResultBuf;
233 bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr);
235 assert(Res &&
"Unexpected conversion failure");
236 Str.append(ResultBuf, ResultPtr);
240 for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
247 assert(*I ==
'u' || *I ==
'U');
249 unsigned NumHexDigits;
255 assert(I + NumHexDigits <= E);
257 uint32_t CodePoint = 0;
258 for (++I; NumHexDigits != 0; ++I, --NumHexDigits) {
259 unsigned Value = llvm::hexDigitValue(*I);
260 assert(Value != -1U);
274 const char *ThisTokEnd,
275 uint32_t &UcnVal,
unsigned short &UcnLen,
278 bool in_char_string_literal =
false) {
279 const char *UcnBegin = ThisTokBuf;
284 if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
286 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
287 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
290 UcnLen = (ThisTokBuf[-1] ==
'u' ? 4 : 8);
291 unsigned short UcnLenSave = UcnLen;
292 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
293 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
294 if (CharVal == -1)
break;
301 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
302 diag::err_ucn_escape_incomplete);
307 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) ||
310 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
311 diag::err_ucn_escape_invalid);
318 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) {
319 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
321 char BasicSCSChar = UcnVal;
322 if (UcnVal >= 0x20 && UcnVal < 0x7f)
323 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
324 IsError ? diag::err_ucn_escape_basic_scs :
325 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
326 << StringRef(&BasicSCSChar, 1);
328 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
329 IsError ? diag::err_ucn_control_character :
330 diag::warn_cxx98_compat_literal_ucn_control_character);
336 if (!Features.CPlusPlus && !Features.C99 && Diags)
337 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
338 diag::warn_ucn_not_valid_in_c89_literal);
346 const char *ThisTokEnd,
unsigned CharByteWidth,
349 if (CharByteWidth == 4)
353 unsigned short UcnLen = 0;
357 UcnLen, Loc,
nullptr, Features,
true)) {
363 if (CharByteWidth == 2)
364 return UcnVal <= 0xFFFF ? 2 : 4;
371 if (UcnVal < 0x10000)
381 const char *ThisTokEnd,
382 char *&ResultBuf,
bool &HadError,
386 typedef uint32_t UTF32;
388 unsigned short UcnLen = 0;
390 Loc, Diags, Features,
true)) {
395 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
396 "only character widths of 1, 2, or 4 bytes supported");
399 assert((UcnLen== 4 || UcnLen== 8) &&
"only ucn length of 4 or 8 supported");
401 if (CharByteWidth == 4) {
404 llvm::UTF32 *ResultPtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf);
410 if (CharByteWidth == 2) {
413 llvm::UTF16 *ResultPtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf);
415 if (UcnVal <= (UTF32)0xFFFF) {
423 *ResultPtr = 0xD800 + (UcnVal >> 10);
424 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
429 assert(CharByteWidth == 1 &&
"UTF-8 encoding is only for 1 byte characters");
435 typedef uint8_t UTF8;
437 unsigned short bytesToWrite = 0;
438 if (UcnVal < (UTF32)0x80)
440 else if (UcnVal < (UTF32)0x800)
442 else if (UcnVal < (UTF32)0x10000)
447 const unsigned byteMask = 0xBF;
448 const unsigned byteMark = 0x80;
452 static const UTF8 firstByteMark[5] = {
453 0x00, 0x00, 0xC0, 0xE0, 0xF0
456 ResultBuf += bytesToWrite;
457 switch (bytesToWrite) {
459 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
462 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
465 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
468 *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
471 ResultBuf += bytesToWrite;
528 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
536 s = DigitsBegin = ThisTokBegin;
537 saw_exponent =
false;
539 saw_ud_suffix =
false;
540 saw_fixed_point_suffix =
false;
555 ParseNumberStartingWithZero(TokLoc);
561 if (s == ThisTokEnd) {
564 ParseDecimalOrOctalCommon(TokLoc);
571 checkSeparator(TokLoc, s, CSK_AfterDigits);
575 for (
const char *c = s; c != ThisTokEnd; ++c) {
576 if (*c ==
'r' || *c ==
'k' || *c ==
'R' || *c ==
'K') {
577 saw_fixed_point_suffix =
true;
589 for (; s != ThisTokEnd; ++s) {
595 if (!(saw_period || saw_exponent))
break;
602 if (!(saw_period || saw_exponent))
break;
615 if (!isFPConstant)
break;
623 s + 2 < ThisTokEnd && s[1] ==
'1' && s[2] ==
'6') {
633 if (!isFPConstant)
break;
640 if (isFPConstant)
break;
651 assert(s + 1 < ThisTokEnd &&
"didn't maximally munch?");
652 if (isFPConstant)
break;
695 assert(s <= ThisTokEnd &&
"didn't maximally munch?");
713 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
726 saw_fixed_point_suffix =
false;
731 saw_ud_suffix =
true;
735 if (s != ThisTokEnd) {
738 diag::err_invalid_suffix_constant)
739 << StringRef(SuffixBegin, ThisTokEnd - SuffixBegin) << isFPConstant;
744 if (!
hadError && saw_fixed_point_suffix) {
752 void NumericLiteralParser::ParseDecimalOrOctalCommon(
SourceLocation TokLoc){
753 assert((radix == 8 || radix == 10) &&
"Unexpected radix");
757 if (
isHexDigit(*s) && *s !=
'e' && *s !=
'E' &&
760 diag::err_invalid_digit) << StringRef(s, 1) << (radix == 8 ? 1 : 0);
766 checkSeparator(TokLoc, s, CSK_AfterDigits);
770 checkSeparator(TokLoc, s, CSK_BeforeDigits);
773 if (*s ==
'e' || *s ==
'E') {
774 checkSeparator(TokLoc, s, CSK_AfterDigits);
775 const char *Exponent = s;
779 if (s != ThisTokEnd && (*s ==
'+' || *s ==
'-')) s++;
780 const char *first_non_digit = SkipDigits(s);
781 if (containsDigits(s, first_non_digit)) {
782 checkSeparator(TokLoc, s, CSK_BeforeDigits);
787 diag::err_exponent_has_no_digits);
800 if (!LangOpts.CPlusPlus11 || Suffix.empty())
804 if (Suffix[0] ==
'_')
808 if (!LangOpts.CPlusPlus14)
814 return llvm::StringSwitch<bool>(Suffix)
815 .Cases(
"h",
"min",
"s",
true)
816 .Cases(
"ms",
"us",
"ns",
true)
817 .Cases(
"il",
"i",
"if",
true)
818 .Cases(
"d",
"y", LangOpts.CPlusPlus2a)
824 CheckSeparatorKind IsAfterDigits) {
825 if (IsAfterDigits == CSK_AfterDigits) {
826 if (Pos == ThisTokBegin)
829 }
else if (Pos == ThisTokEnd)
832 if (isDigitSeparator(*Pos)) {
834 diag::err_digit_separator_not_between_digits)
845 void NumericLiteralParser::ParseNumberStartingWithZero(
SourceLocation TokLoc) {
846 assert(s[0] ==
'0' &&
"Invalid method call");
852 if ((c1 ==
'x' || c1 ==
'X') && (
isHexDigit(s[1]) || s[1] ==
'.')) {
854 assert(s < ThisTokEnd &&
"didn't maximally munch?");
857 s = SkipHexDigits(s);
858 bool HasSignificandDigits = containsDigits(DigitsBegin, s);
859 if (s == ThisTokEnd) {
861 }
else if (*s ==
'.') {
864 const char *floatDigitsBegin = s;
865 s = SkipHexDigits(s);
866 if (containsDigits(floatDigitsBegin, s))
867 HasSignificandDigits =
true;
868 if (HasSignificandDigits)
869 checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits);
872 if (!HasSignificandDigits) {
874 diag::err_hex_constant_requires)
882 if (*s ==
'p' || *s ==
'P') {
883 checkSeparator(TokLoc, s, CSK_AfterDigits);
884 const char *Exponent = s;
887 if (s != ThisTokEnd && (*s ==
'+' || *s ==
'-')) s++;
888 const char *first_non_digit = SkipDigits(s);
889 if (!containsDigits(s, first_non_digit)) {
892 diag::err_exponent_has_no_digits);
897 checkSeparator(TokLoc, s, CSK_BeforeDigits);
902 ? diag::ext_hex_literal_invalid
903 : diag::ext_hex_constant_invalid);
905 PP.
Diag(TokLoc, diag::warn_cxx17_hex_literal);
906 }
else if (saw_period) {
908 diag::err_hex_constant_requires)
916 if ((c1 ==
'b' || c1 ==
'B') && (s[1] ==
'0' || s[1] ==
'1')) {
920 ? diag::warn_cxx11_compat_binary_literal
922 ? diag::ext_binary_literal_cxx14
923 : diag::ext_binary_literal);
925 assert(s < ThisTokEnd &&
"didn't maximally munch?");
928 s = SkipBinaryDigits(s);
929 if (s == ThisTokEnd) {
933 StringRef(s, ThisTokEnd - s))) {
935 diag::err_invalid_digit) << StringRef(s, 1) << 2;
947 s = SkipOctalDigits(s);
954 const char *EndDecimal = SkipDigits(s);
955 if (EndDecimal[0] ==
'.' || EndDecimal[0] ==
'e' || EndDecimal[0] ==
'E') {
961 ParseDecimalOrOctalCommon(TokLoc);
967 return NumDigits <= 64;
969 return NumDigits <= 64 / 3;
971 return NumDigits <= 19;
973 return NumDigits <= 64 / 4;
975 llvm_unreachable(
"impossible Radix");
989 const unsigned NumDigits = SuffixBegin - DigitsBegin;
992 for (
const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
993 if (!isDigitSeparator(*Ptr))
994 N = N * radix + llvm::hexDigitValue(*Ptr);
999 return Val.getZExtValue() != N;
1003 const char *Ptr = DigitsBegin;
1009 bool OverflowOccurred =
false;
1010 while (Ptr < SuffixBegin) {
1011 if (isDigitSeparator(*Ptr)) {
1016 unsigned C = llvm::hexDigitValue(*Ptr++);
1019 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1029 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
1034 OverflowOccurred |= Val.ult(CharVal);
1036 return OverflowOccurred;
1039 llvm::APFloat::opStatus
1041 using llvm::APFloat;
1043 unsigned n =
std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
1046 StringRef Str(ThisTokBegin, n);
1047 if (Str.find(
'\'') != StringRef::npos) {
1049 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
1055 Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
1056 assert(StatusOrErr &&
"Invalid floating point representation");
1057 return !errorToBool(StatusOrErr.takeError()) ? *StatusOrErr
1058 : APFloat::opInvalidOp;
1062 return c ==
'p' || c ==
'P' || c ==
'e' || c ==
'E';
1066 assert(radix == 16 || radix == 10);
1069 unsigned NumDigits = SuffixBegin - DigitsBegin;
1070 if (saw_period) --NumDigits;
1073 bool ExpOverflowOccurred =
false;
1074 bool NegativeExponent =
false;
1075 const char *ExponentBegin;
1076 uint64_t Exponent = 0;
1077 int64_t BaseShift = 0;
1079 const char *Ptr = DigitsBegin;
1082 ExponentBegin = Ptr;
1084 NegativeExponent = *Ptr ==
'-';
1085 if (NegativeExponent) ++Ptr;
1087 unsigned NumExpDigits = SuffixBegin - Ptr;
1089 llvm::StringRef ExpStr(Ptr, NumExpDigits);
1091 Exponent = ExpInt.getZExtValue();
1093 ExpOverflowOccurred =
true;
1096 if (NegativeExponent) BaseShift -= Exponent;
1097 else BaseShift += Exponent;
1117 uint64_t NumBitsNeeded;
1119 NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale;
1121 NumBitsNeeded = 4 * NumDigits + Exponent + Scale;
1124 ExpOverflowOccurred =
true;
1125 llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0,
false);
1127 bool FoundDecimal =
false;
1129 int64_t FractBaseShift = 0;
1130 const char *
End = saw_exponent ? ExponentBegin : SuffixBegin;
1131 for (
const char *Ptr = DigitsBegin; Ptr <
End; ++Ptr) {
1133 FoundDecimal =
true;
1138 unsigned C = llvm::hexDigitValue(*Ptr);
1139 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1151 if (radix == 16) FractBaseShift *= 4;
1152 BaseShift += FractBaseShift;
1156 uint64_t
Base = (radix == 16) ? 2 : 10;
1157 if (BaseShift > 0) {
1158 for (int64_t i = 0; i < BaseShift; ++i) {
1161 }
else if (BaseShift < 0) {
1162 for (int64_t i = BaseShift; i < 0 && !Val.isNullValue(); ++i)
1163 Val = Val.udiv(Base);
1166 bool IntOverflowOccurred =
false;
1167 auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth());
1168 if (Val.getBitWidth() > StoreVal.getBitWidth()) {
1169 IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth()));
1170 StoreVal = Val.trunc(StoreVal.getBitWidth());
1171 }
else if (Val.getBitWidth() < StoreVal.getBitWidth()) {
1172 IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal);
1173 StoreVal = Val.zext(StoreVal.getBitWidth());
1178 return IntOverflowOccurred || ExpOverflowOccurred;
1228 const char *TokBegin = begin;
1231 if (
Kind != tok::char_constant)
1233 if (
Kind == tok::utf8_char_constant)
1237 assert(begin[0] ==
'\'' &&
"Invalid token lexed");
1241 if (end[-1] !=
'\'') {
1242 const char *UDSuffixEnd = end;
1245 }
while (end[-1] !=
'\'');
1247 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1248 UDSuffixOffset = end - TokBegin;
1252 assert(end != begin &&
"Invalid token lexed");
1259 "Assumes char is 8 bits");
1262 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1264 "Assumes sizeof(wchar) on target is <= 64");
1267 codepoint_buffer.resize(end - begin);
1268 uint32_t *buffer_begin = &codepoint_buffer.front();
1269 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1274 uint32_t largest_character_for_kind;
1275 if (tok::wide_char_constant ==
Kind) {
1276 largest_character_for_kind =
1278 }
else if (tok::utf8_char_constant ==
Kind) {
1279 largest_character_for_kind = 0x7F;
1280 }
else if (tok::utf16_char_constant ==
Kind) {
1281 largest_character_for_kind = 0xFFFF;
1282 }
else if (tok::utf32_char_constant ==
Kind) {
1283 largest_character_for_kind = 0x10FFFF;
1285 largest_character_for_kind = 0x7Fu;
1288 while (begin != end) {
1290 if (begin[0] !=
'\\') {
1291 char const *start = begin;
1294 }
while (begin != end && *begin !=
'\\');
1296 char const *tmp_in_start = start;
1297 uint32_t *tmp_out_start = buffer_begin;
1298 llvm::ConversionResult res =
1299 llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start),
1300 reinterpret_cast<llvm::UTF8 const *>(begin),
1301 &buffer_begin, buffer_end, llvm::strictConversion);
1302 if (res != llvm::conversionOK) {
1306 bool NoErrorOnBadEncoding = isAscii();
1307 unsigned Msg = diag::err_bad_character_encoding;
1308 if (NoErrorOnBadEncoding)
1309 Msg = diag::warn_bad_character_encoding;
1311 if (NoErrorOnBadEncoding) {
1312 start = tmp_in_start;
1313 buffer_begin = tmp_out_start;
1314 for (; start != begin; ++start, ++buffer_begin)
1315 *buffer_begin = static_cast<uint8_t>(*start);
1320 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1321 if (*tmp_out_start > largest_character_for_kind) {
1323 PP.
Diag(Loc, diag::err_character_too_large);
1331 if (begin[1] ==
'u' || begin[1] ==
'U') {
1332 unsigned short UcnLen = 0;
1337 }
else if (*buffer_begin > largest_character_for_kind) {
1339 PP.
Diag(Loc, diag::err_character_too_large);
1350 *buffer_begin++ = result;
1353 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1355 if (NumCharsSoFar > 1) {
1357 PP.
Diag(Loc, diag::warn_extraneous_char_constant);
1358 else if (isAscii() && NumCharsSoFar == 4)
1359 PP.
Diag(Loc, diag::ext_four_char_character_literal);
1361 PP.
Diag(Loc, diag::ext_multichar_character_literal);
1363 PP.
Diag(Loc, diag::err_multichar_utf_character_literal);
1366 IsMultiChar =
false;
1373 bool multi_char_too_long =
false;
1374 if (isAscii() && isMultiChar()) {
1376 for (
size_t i = 0; i < NumCharsSoFar; ++i) {
1378 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1380 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1382 }
else if (NumCharsSoFar > 0) {
1384 LitVal = buffer_begin[-1];
1387 if (!HadError && multi_char_too_long) {
1388 PP.
Diag(Loc, diag::warn_char_constant_too_large);
1392 Value = LitVal.getZExtValue();
1398 if (isAscii() && NumCharsSoFar == 1 && (
Value & 128) &&
1460 :
SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1461 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1462 MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown),
1470 if (StringToks.empty() || StringToks[0].getLength() < 2)
1477 assert(!StringToks.empty() &&
"expected at least one token");
1478 MaxTokenLength = StringToks[0].getLength();
1479 assert(StringToks[0].getLength() >= 2 &&
"literal token is invalid!");
1480 SizeBound = StringToks[0].getLength()-2;
1481 Kind = StringToks[0].getKind();
1487 for (
unsigned i = 1; i != StringToks.size(); ++i) {
1488 if (StringToks[i].getLength() < 2)
1489 return DiagnoseLexingError(StringToks[i].getLocation());
1493 assert(StringToks[i].getLength() >= 2 &&
"literal token is invalid!");
1494 SizeBound += StringToks[i].getLength()-2;
1497 if (StringToks[i].getLength() > MaxTokenLength)
1498 MaxTokenLength = StringToks[i].getLength();
1502 if (StringToks[i].isNot(
Kind) && StringToks[i].isNot(tok::string_literal)) {
1504 Kind = StringToks[i].getKind();
1507 Diags->
Report(StringToks[i].getLocation(),
1508 diag::err_unsupported_string_concat);
1521 assert((CharByteWidth & 7) == 0 &&
"Assumes character size is byte multiple");
1526 SizeBound *= CharByteWidth;
1529 ResultBuf.resize(SizeBound);
1533 TokenBuf.resize(MaxTokenLength);
1537 ResultPtr = &ResultBuf[0];
1543 for (
unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1544 const char *ThisTokBuf = &TokenBuf[0];
1548 bool StringInvalid =
false;
1549 unsigned ThisTokLen =
1553 return DiagnoseLexingError(StringToks[i].getLocation());
1555 const char *ThisTokBegin = ThisTokBuf;
1556 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1559 if (ThisTokEnd[-1] !=
'"') {
1560 const char *UDSuffixEnd = ThisTokEnd;
1563 }
while (ThisTokEnd[-1] !=
'"');
1565 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1567 if (UDSuffixBuf.empty()) {
1568 if (StringToks[i].hasUCN())
1571 UDSuffixBuf.assign(UDSuffix);
1573 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1574 UDSuffixTokLoc = StringToks[i].getLocation();
1577 if (StringToks[i].hasUCN()) {
1579 UDSuffix = ExpandedUDSuffix;
1586 if (UDSuffixBuf != UDSuffix) {
1589 Diags->
Report(TokLoc, diag::err_string_concat_mixed_suffix)
1590 << UDSuffixBuf << UDSuffix
1605 if (ThisTokBuf[0] ==
'L' || ThisTokBuf[0] ==
'u' || ThisTokBuf[0] ==
'U') {
1608 if (ThisTokBuf[0] ==
'8')
1613 if (ThisTokBuf[0] ==
'R') {
1616 const char *Prefix = ThisTokBuf;
1617 while (ThisTokBuf[0] !=
'(')
1622 ThisTokEnd -= ThisTokBuf - Prefix;
1623 assert(ThisTokEnd >= ThisTokBuf &&
"malformed raw string literal");
1627 StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf);
1628 while (!RemainingTokenSpan.empty()) {
1630 size_t CRLFPos = RemainingTokenSpan.find(
"\r\n");
1631 StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos);
1632 StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos);
1635 if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF))
1640 RemainingTokenSpan = AfterCRLF.substr(1);
1643 if (ThisTokBuf[0] !=
'"') {
1646 return DiagnoseLexingError(StringToks[i].getLocation());
1651 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1652 ThisTokBuf[0] ==
'\\' && ThisTokBuf[1] ==
'p') {
1663 while (ThisTokBuf != ThisTokEnd) {
1665 if (ThisTokBuf[0] !=
'\\') {
1666 const char *InStart = ThisTokBuf;
1669 }
while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] !=
'\\');
1672 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1673 StringRef(InStart, ThisTokBuf - InStart)))
1678 if (ThisTokBuf[1] ==
'u' || ThisTokBuf[1] ==
'U') {
1682 CharByteWidth, Diags, Features);
1686 unsigned ResultChar =
1689 CharByteWidth*8, Diags, Features);
1691 if (CharByteWidth == 4) {
1694 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultPtr);
1695 *ResultWidePtr = ResultChar;
1697 }
else if (CharByteWidth == 2) {
1700 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultPtr);
1701 *ResultWidePtr = ResultChar & 0xFFFF;
1704 assert(CharByteWidth == 1 &&
"Unexpected char width");
1705 *ResultPtr++ = ResultChar & 0xFF;
1712 if (CharByteWidth == 4) {
1715 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf.data());
1717 }
else if (CharByteWidth == 2) {
1720 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf.data());
1723 assert(CharByteWidth == 1 &&
"Unexpected char width");
1730 Diags->
Report(StringToks.front().getLocation(),
1731 diag::err_pascal_string_too_long)
1733 StringToks.back().getLocation());
1739 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1742 Diags->
Report(StringToks.front().getLocation(),
1743 diag::ext_string_too_long)
1745 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1747 StringToks.back().getLocation());
1754 End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err);
1755 while (++Err != End && (*Err & 0xC0) == 0x80)
1763 bool StringLiteralParser::CopyStringFragment(
const Token &
Tok,
1764 const char *TokBegin,
1765 StringRef Fragment) {
1766 const llvm::UTF8 *ErrorPtrTmp;
1767 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1773 bool NoErrorOnBadEncoding =
isAscii();
1774 if (NoErrorOnBadEncoding) {
1775 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1776 ResultPtr += Fragment.size();
1780 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1784 Diag(Diags, Features, SourceLoc, TokBegin,
1785 ErrorPtr,
resyncUTF8(ErrorPtr, Fragment.end()),
1786 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1787 : diag::err_bad_string_encoding);
1789 const char *NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1790 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1794 Dummy.reserve(Fragment.size() * CharByteWidth);
1795 char *Ptr = Dummy.data();
1797 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1798 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1799 NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1801 ErrorPtr, NextStart);
1802 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1805 return !NoErrorOnBadEncoding;
1808 void StringLiteralParser::DiagnoseLexingError(
SourceLocation Loc) {
1811 Diags->
Report(Loc, diag::err_lexing_string);
1818 unsigned ByteNo)
const {
1823 bool StringInvalid =
false;
1824 const char *SpellingPtr = &SpellingBuffer[0];
1830 const char *SpellingStart = SpellingPtr;
1831 const char *SpellingEnd = SpellingPtr+TokLen;
1834 if (SpellingPtr[0] ==
'u' && SpellingPtr[1] ==
'8')
1837 assert(SpellingPtr[0] !=
'L' && SpellingPtr[0] !=
'u' &&
1838 SpellingPtr[0] !=
'U' &&
"Doesn't handle wide or utf strings yet");
1841 if (SpellingPtr[0] ==
'R') {
1842 assert(SpellingPtr[1] ==
'"' &&
"Should be a raw string literal!");
1845 while (*SpellingPtr !=
'(') {
1847 assert(SpellingPtr < SpellingEnd &&
"Missing ( for raw string literal");
1851 return SpellingPtr - SpellingStart + ByteNo;
1855 assert(SpellingPtr[0] ==
'"' &&
"Should be a string literal!");
1860 assert(SpellingPtr < SpellingEnd &&
"Didn't find byte offset!");
1863 if (*SpellingPtr !=
'\\') {
1870 bool HadError =
false;
1871 if (SpellingPtr[1] ==
'u' || SpellingPtr[1] ==
'U') {
1872 const char *EscapePtr = SpellingPtr;
1874 1, Features, HadError);
1877 SpellingPtr = EscapePtr;
1884 CharByteWidth*8, Diags, Features);
1887 assert(!HadError &&
"This method isn't valid on erroneous strings");
1890 return SpellingPtr-SpellingStart;
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
getSpelling - This method is used to get the spelling of a token into a preallocated buffer...
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const
getOffsetOfStringByte - This function returns the offset of the specified byte of the string data rep...
StringLiteralParser(ArrayRef< Token > StringToks, Preprocessor &PP, bool Complain=true)
unsigned GetNumStringChars() const
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCharWidth() const
unsigned GetStringLength() const
static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits)
unsigned getChar32Width() const
getChar32Width/Align - Return the size of 'char32_t' for this target, in bits.
const TargetInfo & getTargetInfo() const
Token - This structure provides full information about a lexed token.
__DEVICE__ int max(int __a, int __b)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const LangOptions & getLangOpts() const
static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, const char *ThisTokEnd, unsigned CharByteWidth, const LangOptions &Features, bool &HadError)
MeasureUCNEscape - Determine the number of bytes within the resulting string which this UCN will occu...
SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Char) const
Given a location that specifies the start of a token, return a new location that specifies a characte...
Concrete class used by the front-end to report problems and issues.
static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix)
Determine whether a suffix is a valid ud-suffix.
NumericLiteralParser(StringRef TokSpelling, SourceLocation TokLoc, Preprocessor &PP)
integer-constant: [C99 6.4.4.1] decimal-constant integer-suffix octal-constant integer-suffix hexadec...
static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Characters, const SourceManager &SM, const LangOptions &LangOpts)
AdvanceToTokenCharacter - If the current SourceLocation specifies a location at the start of a token...
static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix)
Determine whether a suffix is a valid ud-suffix.
A little helper class used to produce diagnostics.
Exposes information about the current target.
bool GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale)
GetFixedPointValue - Convert this numeric literal value into a scaled integer that represents this va...
CharLiteralParser(const char *begin, const char *end, SourceLocation Loc, Preprocessor &PP, tok::TokenKind kind)
Defines the clang::LangOptions interface.
Represents a character-granular source range.
unsigned getIntWidth() const
getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for this target, in bits.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Defines the clang::Preprocessor interface.
const SourceManager & getManager() const
static const char * resyncUTF8(const char *Err, const char *End)
static void appendCodePoint(unsigned Codepoint, llvm::SmallVectorImpl< char > &Str)
The result type of a method or function.
static CharSourceRange getCharRange(SourceRange R)
SourceManager & getSourceManager() const
unsigned getWCharWidth() const
getWCharWidth/Align - Return the size of 'wchar_t' for this target, in bits.
bool GetIntegerValue(llvm::APInt &Val)
GetIntegerValue - Convert this numeric literal value to an APInt that matches Val's input width...
Encodes a location in the source.
llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result)
GetFloatValue - Convert this numeric literal to a floating value, using the specified APFloat fltSema...
unsigned getChar16Width() const
getChar16Width/Align - Return the size of 'char16_t' for this target, in bits.
static unsigned ProcessCharEscape(const char *ThisTokBegin, const char *&ThisTokBuf, const char *ThisTokEnd, bool &HadError, FullSourceLoc Loc, unsigned CharWidth, DiagnosticsEngine *Diags, const LangOptions &Features)
ProcessCharEscape - Parse a standard C escape sequence, which can occur in either a character or a st...
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
void expandUCNs(SmallVectorImpl< char > &Buf, StringRef Input)
Copy characters from Input to Buf, expanding any UCNs.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
LLVM_READONLY bool isPrintable(unsigned char c)
Return true if this character is an ASCII printable character; that is, a character that should take ...
static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, const char *ThisTokEnd, char *&ResultBuf, bool &HadError, FullSourceLoc Loc, unsigned CharByteWidth, DiagnosticsEngine *Diags, const LangOptions &Features)
EncodeUCNEscape - Read the Universal Character Name, check constraints and convert the UTF32 to UTF8 ...
Dataflow Directional Tag Classes.
virtual bool hasFloat16Type() const
Determine whether the _Float16 type is supported on this target.
unsigned getLength() const
LLVM_READONLY bool isHexDigit(unsigned char c)
Return true if this character is an ASCII hex digit: [0-9a-fA-F].
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
bool isIntegerLiteral() const
bool isFloatingLiteral() const
Defines the clang::SourceLocation class and associated facilities.
static bool IsExponentPart(char c)
DiagnosticsEngine & getDiagnostics() const
static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target)
unsigned kind
All of the diagnostics that can be emitted by the frontend.
__DEVICE__ int min(int __a, int __b)
Defines the clang::TargetInfo interface.
A SourceLocation and its associated SourceManager.
A trivial tuple used to represent a source range.
static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, const char *ThisTokEnd, uint32_t &UcnVal, unsigned short &UcnLen, FullSourceLoc Loc, DiagnosticsEngine *Diags, const LangOptions &Features, bool in_char_string_literal=false)
ProcessUCNEscape - Read the Universal Character Name, check constraints and return the UTF32...
LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
Return true if this is the body character of a C preprocessing number, which is [a-zA-Z0-9_.
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
static CharSourceRange MakeCharSourceRange(const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd)
Engages in a tight little dance with the lexer to efficiently preprocess tokens.