24 #include "llvm/ADT/APInt.h" 25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/ADT/StringExtras.h" 27 #include "llvm/ADT/StringSwitch.h" 28 #include "llvm/Support/ConvertUTF.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:
47 case tok::wide_char_constant:
48 case tok::wide_string_literal:
50 case tok::utf16_char_constant:
51 case tok::utf16_string_literal:
53 case tok::utf32_char_constant:
54 case tok::utf32_string_literal:
62 const char *TokRangeBegin,
63 const char *TokRangeEnd) {
80 const char *TokBegin,
const char *TokRangeBegin,
81 const char *TokRangeEnd,
unsigned DiagID) {
85 return Diags->
Report(Begin, DiagID) <<
92 const char *&ThisTokBuf,
93 const char *ThisTokEnd,
bool &HadError,
97 const char *EscapeBegin = ThisTokBuf;
104 unsigned ResultChar = *ThisTokBuf++;
105 switch (ResultChar) {
107 case '\\':
case '\'':
case '"':
case '?':
break;
119 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
120 diag::ext_nonstandard_escape) <<
"e";
125 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
126 diag::ext_nonstandard_escape) <<
"E";
146 if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
148 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
149 diag::err_hex_escape_no_digits) <<
"x";
155 bool Overflow =
false;
156 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
157 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
158 if (CharVal == -1)
break;
160 if (ResultChar & 0xF0000000)
163 ResultChar |= CharVal;
167 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
169 ResultChar &= ~0U >> (32-CharWidth);
173 if (Overflow && Diags)
174 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
175 diag::err_escape_too_large) << 0;
178 case '0':
case '1':
case '2':
case '3':
179 case '4':
case '5':
case '6':
case '7': {
186 unsigned NumDigits = 0;
189 ResultChar |= *ThisTokBuf++ -
'0';
191 }
while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
192 ThisTokBuf[0] >=
'0' && ThisTokBuf[0] <=
'7');
195 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
197 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
198 diag::err_escape_too_large) << 1;
199 ResultChar &= ~0U >> (32-CharWidth);
205 case '(':
case '{':
case '[':
case '%':
208 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
209 diag::ext_nonstandard_escape)
210 << std::string(1, ResultChar);
217 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
218 diag::ext_unknown_escape)
219 << std::string(1, ResultChar);
221 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
222 diag::ext_unknown_escape)
223 <<
"x" + llvm::utohexstr(ResultChar);
233 char *ResultPtr = ResultBuf;
234 bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr);
236 assert(Res &&
"Unexpected conversion failure");
237 Str.append(ResultBuf, ResultPtr);
241 for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
248 assert(*I ==
'u' || *I ==
'U');
250 unsigned NumHexDigits;
256 assert(I + NumHexDigits <= E);
258 uint32_t CodePoint = 0;
259 for (++I; NumHexDigits != 0; ++I, --NumHexDigits) {
260 unsigned Value = llvm::hexDigitValue(*I);
261 assert(Value != -1U);
275 const char *ThisTokEnd,
276 uint32_t &UcnVal,
unsigned short &UcnLen,
279 bool in_char_string_literal =
false) {
280 const char *UcnBegin = ThisTokBuf;
285 if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
287 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
288 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
291 UcnLen = (ThisTokBuf[-1] ==
'u' ? 4 : 8);
292 unsigned short UcnLenSave = UcnLen;
293 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
294 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
295 if (CharVal == -1)
break;
302 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
303 diag::err_ucn_escape_incomplete);
308 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) ||
311 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
312 diag::err_ucn_escape_invalid);
319 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) {
320 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
322 char BasicSCSChar = UcnVal;
323 if (UcnVal >= 0x20 && UcnVal < 0x7f)
324 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
325 IsError ? diag::err_ucn_escape_basic_scs :
326 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
327 << StringRef(&BasicSCSChar, 1);
329 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
330 IsError ? diag::err_ucn_control_character :
331 diag::warn_cxx98_compat_literal_ucn_control_character);
337 if (!Features.CPlusPlus && !Features.C99 && Diags)
338 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
339 diag::warn_ucn_not_valid_in_c89_literal);
347 const char *ThisTokEnd,
unsigned CharByteWidth,
350 if (CharByteWidth == 4)
354 unsigned short UcnLen = 0;
358 UcnLen, Loc,
nullptr, Features,
true)) {
364 if (CharByteWidth == 2)
365 return UcnVal <= 0xFFFF ? 2 : 4;
372 if (UcnVal < 0x10000)
382 const char *ThisTokEnd,
383 char *&ResultBuf,
bool &HadError,
387 typedef uint32_t UTF32;
389 unsigned short UcnLen = 0;
391 Loc, Diags, Features,
true)) {
396 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
397 "only character widths of 1, 2, or 4 bytes supported");
400 assert((UcnLen== 4 || UcnLen== 8) &&
"only ucn length of 4 or 8 supported");
402 if (CharByteWidth == 4) {
405 llvm::UTF32 *ResultPtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf);
411 if (CharByteWidth == 2) {
414 llvm::UTF16 *ResultPtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf);
416 if (UcnVal <= (UTF32)0xFFFF) {
424 *ResultPtr = 0xD800 + (UcnVal >> 10);
425 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
430 assert(CharByteWidth == 1 &&
"UTF-8 encoding is only for 1 byte characters");
436 typedef uint8_t UTF8;
438 unsigned short bytesToWrite = 0;
439 if (UcnVal < (UTF32)0x80)
441 else if (UcnVal < (UTF32)0x800)
443 else if (UcnVal < (UTF32)0x10000)
448 const unsigned byteMask = 0xBF;
449 const unsigned byteMark = 0x80;
453 static const UTF8 firstByteMark[5] = {
454 0x00, 0x00, 0xC0, 0xE0, 0xF0
457 ResultBuf += bytesToWrite;
458 switch (bytesToWrite) {
460 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
463 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
466 *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
469 *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
472 ResultBuf += bytesToWrite;
529 : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
537 s = DigitsBegin = ThisTokBegin;
538 saw_exponent =
false;
540 saw_ud_suffix =
false;
541 saw_fixed_point_suffix =
false;
556 ParseNumberStartingWithZero(TokLoc);
562 if (s == ThisTokEnd) {
565 ParseDecimalOrOctalCommon(TokLoc);
572 checkSeparator(TokLoc, s, CSK_AfterDigits);
576 for (
const char *c = s; c != ThisTokEnd; ++c) {
577 if (*c ==
'r' || *c ==
'k' || *c ==
'R' || *c ==
'K') {
578 saw_fixed_point_suffix =
true;
590 for (; s != ThisTokEnd; ++s) {
596 if (!(saw_period || saw_exponent))
break;
603 if (!(saw_period || saw_exponent))
break;
616 if (!isFPConstant)
break;
621 s[1] ==
'1' && s[2] ==
'6') {
631 if (!isFPConstant)
break;
638 if (isFPConstant)
break;
649 assert(s + 1 < ThisTokEnd &&
"didn't maximally munch?");
650 if (isFPConstant)
break;
693 assert(s <= ThisTokEnd &&
"didn't maximally munch?");
711 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
724 saw_fixed_point_suffix =
false;
729 saw_ud_suffix =
true;
733 if (s != ThisTokEnd) {
736 diag::err_invalid_suffix_constant)
737 << StringRef(SuffixBegin, ThisTokEnd - SuffixBegin) << isFPConstant;
742 if (!
hadError && saw_fixed_point_suffix) {
750 void NumericLiteralParser::ParseDecimalOrOctalCommon(
SourceLocation TokLoc){
751 assert((radix == 8 || radix == 10) &&
"Unexpected radix");
755 if (
isHexDigit(*s) && *s !=
'e' && *s !=
'E' &&
758 diag::err_invalid_digit) << StringRef(s, 1) << (radix == 8 ? 1 : 0);
764 checkSeparator(TokLoc, s, CSK_AfterDigits);
768 checkSeparator(TokLoc, s, CSK_BeforeDigits);
771 if (*s ==
'e' || *s ==
'E') {
772 checkSeparator(TokLoc, s, CSK_AfterDigits);
773 const char *Exponent = s;
777 if (s != ThisTokEnd && (*s ==
'+' || *s ==
'-')) s++;
778 const char *first_non_digit = SkipDigits(s);
779 if (containsDigits(s, first_non_digit)) {
780 checkSeparator(TokLoc, s, CSK_BeforeDigits);
785 diag::err_exponent_has_no_digits);
798 if (!LangOpts.CPlusPlus11 || Suffix.empty())
802 if (Suffix[0] ==
'_')
806 if (!LangOpts.CPlusPlus14)
812 return llvm::StringSwitch<bool>(Suffix)
813 .Cases(
"h",
"min",
"s",
true)
814 .Cases(
"ms",
"us",
"ns",
true)
815 .Cases(
"il",
"i",
"if",
true)
816 .Cases(
"d",
"y", LangOpts.CPlusPlus2a)
822 CheckSeparatorKind IsAfterDigits) {
823 if (IsAfterDigits == CSK_AfterDigits) {
824 if (Pos == ThisTokBegin)
827 }
else if (Pos == ThisTokEnd)
830 if (isDigitSeparator(*Pos)) {
832 diag::err_digit_separator_not_between_digits)
843 void NumericLiteralParser::ParseNumberStartingWithZero(
SourceLocation TokLoc) {
844 assert(s[0] ==
'0' &&
"Invalid method call");
850 if ((c1 ==
'x' || c1 ==
'X') && (
isHexDigit(s[1]) || s[1] ==
'.')) {
852 assert(s < ThisTokEnd &&
"didn't maximally munch?");
855 s = SkipHexDigits(s);
856 bool HasSignificandDigits = containsDigits(DigitsBegin, s);
857 if (s == ThisTokEnd) {
859 }
else if (*s ==
'.') {
862 const char *floatDigitsBegin = s;
863 s = SkipHexDigits(s);
864 if (containsDigits(floatDigitsBegin, s))
865 HasSignificandDigits =
true;
866 if (HasSignificandDigits)
867 checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits);
870 if (!HasSignificandDigits) {
872 diag::err_hex_constant_requires)
880 if (*s ==
'p' || *s ==
'P') {
881 checkSeparator(TokLoc, s, CSK_AfterDigits);
882 const char *Exponent = s;
885 if (s != ThisTokEnd && (*s ==
'+' || *s ==
'-')) s++;
886 const char *first_non_digit = SkipDigits(s);
887 if (!containsDigits(s, first_non_digit)) {
890 diag::err_exponent_has_no_digits);
895 checkSeparator(TokLoc, s, CSK_BeforeDigits);
900 ? diag::ext_hex_literal_invalid
901 : diag::ext_hex_constant_invalid);
903 PP.
Diag(TokLoc, diag::warn_cxx17_hex_literal);
904 }
else if (saw_period) {
906 diag::err_hex_constant_requires)
914 if ((c1 ==
'b' || c1 ==
'B') && (s[1] ==
'0' || s[1] ==
'1')) {
918 ? diag::warn_cxx11_compat_binary_literal
920 ? diag::ext_binary_literal_cxx14
921 : diag::ext_binary_literal);
923 assert(s < ThisTokEnd &&
"didn't maximally munch?");
926 s = SkipBinaryDigits(s);
927 if (s == ThisTokEnd) {
931 StringRef(s, ThisTokEnd - s))) {
933 diag::err_invalid_digit) << StringRef(s, 1) << 2;
945 s = SkipOctalDigits(s);
952 const char *EndDecimal = SkipDigits(s);
953 if (EndDecimal[0] ==
'.' || EndDecimal[0] ==
'e' || EndDecimal[0] ==
'E') {
959 ParseDecimalOrOctalCommon(TokLoc);
965 return NumDigits <= 64;
967 return NumDigits <= 64 / 3;
969 return NumDigits <= 19;
971 return NumDigits <= 64 / 4;
973 llvm_unreachable(
"impossible Radix");
987 const unsigned NumDigits = SuffixBegin - DigitsBegin;
990 for (
const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
991 if (!isDigitSeparator(*Ptr))
992 N = N * radix + llvm::hexDigitValue(*Ptr);
997 return Val.getZExtValue() != N;
1001 const char *Ptr = DigitsBegin;
1003 llvm::APInt RadixVal(Val.getBitWidth(), radix);
1004 llvm::APInt CharVal(Val.getBitWidth(), 0);
1005 llvm::APInt OldVal = Val;
1007 bool OverflowOccurred =
false;
1008 while (Ptr < SuffixBegin) {
1009 if (isDigitSeparator(*Ptr)) {
1014 unsigned C = llvm::hexDigitValue(*Ptr++);
1017 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1027 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
1032 OverflowOccurred |= Val.ult(CharVal);
1034 return OverflowOccurred;
1037 llvm::APFloat::opStatus
1039 using llvm::APFloat;
1041 unsigned n =
std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
1044 StringRef Str(ThisTokBegin, n);
1045 if (Str.find(
'\'') != StringRef::npos) {
1047 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
1052 return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
1056 return c ==
'p' || c ==
'P' || c ==
'e' || c ==
'E';
1060 assert(radix == 16 || radix == 10);
1063 unsigned NumDigits = SuffixBegin - DigitsBegin;
1064 if (saw_period) --NumDigits;
1067 bool ExpOverflowOccurred =
false;
1068 bool NegativeExponent =
false;
1069 const char *ExponentBegin;
1070 uint64_t Exponent = 0;
1071 int64_t BaseShift = 0;
1073 const char *Ptr = DigitsBegin;
1076 ExponentBegin = Ptr;
1078 NegativeExponent = *Ptr ==
'-';
1079 if (NegativeExponent) ++Ptr;
1081 unsigned NumExpDigits = SuffixBegin - Ptr;
1083 llvm::StringRef ExpStr(Ptr, NumExpDigits);
1084 llvm::APInt ExpInt(64, ExpStr, 10);
1085 Exponent = ExpInt.getZExtValue();
1087 ExpOverflowOccurred =
true;
1090 if (NegativeExponent) BaseShift -= Exponent;
1091 else BaseShift += Exponent;
1111 uint64_t NumBitsNeeded;
1113 NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale;
1115 NumBitsNeeded = 4 * NumDigits + Exponent + Scale;
1118 ExpOverflowOccurred =
true;
1119 llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0,
false);
1121 bool FoundDecimal =
false;
1123 int64_t FractBaseShift = 0;
1124 const char *
End = saw_exponent ? ExponentBegin : SuffixBegin;
1125 for (
const char *Ptr = DigitsBegin; Ptr <
End; ++Ptr) {
1127 FoundDecimal =
true;
1132 unsigned C = llvm::hexDigitValue(*Ptr);
1133 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1145 if (radix == 16) FractBaseShift *= 4;
1146 BaseShift += FractBaseShift;
1150 uint64_t
Base = (radix == 16) ? 2 : 10;
1151 if (BaseShift > 0) {
1152 for (int64_t i = 0; i < BaseShift; ++i) {
1155 }
else if (BaseShift < 0) {
1156 for (int64_t i = BaseShift; i < 0 && !Val.isNullValue(); ++i)
1157 Val = Val.udiv(Base);
1160 bool IntOverflowOccurred =
false;
1161 auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth());
1162 if (Val.getBitWidth() > StoreVal.getBitWidth()) {
1163 IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth()));
1164 StoreVal = Val.trunc(StoreVal.getBitWidth());
1165 }
else if (Val.getBitWidth() < StoreVal.getBitWidth()) {
1166 IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal);
1167 StoreVal = Val.zext(StoreVal.getBitWidth());
1172 return IntOverflowOccurred || ExpOverflowOccurred;
1222 const char *TokBegin = begin;
1225 if (
Kind != tok::char_constant)
1227 if (
Kind == tok::utf8_char_constant)
1231 assert(begin[0] ==
'\'' &&
"Invalid token lexed");
1235 if (end[-1] !=
'\'') {
1236 const char *UDSuffixEnd = end;
1239 }
while (end[-1] !=
'\'');
1241 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1242 UDSuffixOffset = end - TokBegin;
1246 assert(end != begin &&
"Invalid token lexed");
1253 "Assumes char is 8 bits");
1256 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1258 "Assumes sizeof(wchar) on target is <= 64");
1261 codepoint_buffer.resize(end - begin);
1262 uint32_t *buffer_begin = &codepoint_buffer.front();
1263 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1268 uint32_t largest_character_for_kind;
1269 if (tok::wide_char_constant ==
Kind) {
1270 largest_character_for_kind =
1272 }
else if (tok::utf8_char_constant ==
Kind) {
1273 largest_character_for_kind = 0x7F;
1274 }
else if (tok::utf16_char_constant ==
Kind) {
1275 largest_character_for_kind = 0xFFFF;
1276 }
else if (tok::utf32_char_constant ==
Kind) {
1277 largest_character_for_kind = 0x10FFFF;
1279 largest_character_for_kind = 0x7Fu;
1282 while (begin != end) {
1284 if (begin[0] !=
'\\') {
1285 char const *start = begin;
1288 }
while (begin != end && *begin !=
'\\');
1290 char const *tmp_in_start = start;
1291 uint32_t *tmp_out_start = buffer_begin;
1292 llvm::ConversionResult res =
1293 llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start),
1294 reinterpret_cast<llvm::UTF8 const *>(begin),
1295 &buffer_begin, buffer_end, llvm::strictConversion);
1296 if (res != llvm::conversionOK) {
1300 bool NoErrorOnBadEncoding = isAscii();
1301 unsigned Msg = diag::err_bad_character_encoding;
1302 if (NoErrorOnBadEncoding)
1303 Msg = diag::warn_bad_character_encoding;
1305 if (NoErrorOnBadEncoding) {
1306 start = tmp_in_start;
1307 buffer_begin = tmp_out_start;
1308 for (; start != begin; ++start, ++buffer_begin)
1309 *buffer_begin = static_cast<uint8_t>(*start);
1314 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1315 if (*tmp_out_start > largest_character_for_kind) {
1317 PP.
Diag(Loc, diag::err_character_too_large);
1325 if (begin[1] ==
'u' || begin[1] ==
'U') {
1326 unsigned short UcnLen = 0;
1331 }
else if (*buffer_begin > largest_character_for_kind) {
1333 PP.
Diag(Loc, diag::err_character_too_large);
1344 *buffer_begin++ = result;
1347 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1349 if (NumCharsSoFar > 1) {
1351 PP.
Diag(Loc, diag::warn_extraneous_char_constant);
1352 else if (isAscii() && NumCharsSoFar == 4)
1353 PP.
Diag(Loc, diag::ext_four_char_character_literal);
1355 PP.
Diag(Loc, diag::ext_multichar_character_literal);
1357 PP.
Diag(Loc, diag::err_multichar_utf_character_literal);
1360 IsMultiChar =
false;
1367 bool multi_char_too_long =
false;
1368 if (isAscii() && isMultiChar()) {
1370 for (
size_t i = 0; i < NumCharsSoFar; ++i) {
1372 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1374 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1376 }
else if (NumCharsSoFar > 0) {
1378 LitVal = buffer_begin[-1];
1381 if (!HadError && multi_char_too_long) {
1382 PP.
Diag(Loc, diag::warn_char_constant_too_large);
1386 Value = LitVal.getZExtValue();
1392 if (isAscii() && NumCharsSoFar == 1 && (
Value & 128) &&
1454 :
SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1455 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1456 MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown),
1464 if (StringToks.empty() || StringToks[0].getLength() < 2)
1471 assert(!StringToks.empty() &&
"expected at least one token");
1472 MaxTokenLength = StringToks[0].getLength();
1473 assert(StringToks[0].getLength() >= 2 &&
"literal token is invalid!");
1474 SizeBound = StringToks[0].getLength()-2;
1475 Kind = StringToks[0].getKind();
1481 for (
unsigned i = 1; i != StringToks.size(); ++i) {
1482 if (StringToks[i].getLength() < 2)
1483 return DiagnoseLexingError(StringToks[i].getLocation());
1487 assert(StringToks[i].getLength() >= 2 &&
"literal token is invalid!");
1488 SizeBound += StringToks[i].getLength()-2;
1491 if (StringToks[i].getLength() > MaxTokenLength)
1492 MaxTokenLength = StringToks[i].getLength();
1496 if (StringToks[i].isNot(
Kind) && StringToks[i].isNot(tok::string_literal)) {
1498 Kind = StringToks[i].getKind();
1501 Diags->
Report(StringToks[i].getLocation(),
1502 diag::err_unsupported_string_concat);
1515 assert((CharByteWidth & 7) == 0 &&
"Assumes character size is byte multiple");
1520 SizeBound *= CharByteWidth;
1523 ResultBuf.resize(SizeBound);
1527 TokenBuf.resize(MaxTokenLength);
1531 ResultPtr = &ResultBuf[0];
1537 for (
unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1538 const char *ThisTokBuf = &TokenBuf[0];
1542 bool StringInvalid =
false;
1543 unsigned ThisTokLen =
1547 return DiagnoseLexingError(StringToks[i].getLocation());
1549 const char *ThisTokBegin = ThisTokBuf;
1550 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1553 if (ThisTokEnd[-1] !=
'"') {
1554 const char *UDSuffixEnd = ThisTokEnd;
1557 }
while (ThisTokEnd[-1] !=
'"');
1559 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1561 if (UDSuffixBuf.empty()) {
1562 if (StringToks[i].hasUCN())
1565 UDSuffixBuf.assign(UDSuffix);
1567 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1568 UDSuffixTokLoc = StringToks[i].getLocation();
1571 if (StringToks[i].hasUCN()) {
1573 UDSuffix = ExpandedUDSuffix;
1580 if (UDSuffixBuf != UDSuffix) {
1583 Diags->
Report(TokLoc, diag::err_string_concat_mixed_suffix)
1584 << UDSuffixBuf << UDSuffix
1599 if (ThisTokBuf[0] ==
'L' || ThisTokBuf[0] ==
'u' || ThisTokBuf[0] ==
'U') {
1602 if (ThisTokBuf[0] ==
'8')
1607 if (ThisTokBuf[0] ==
'R') {
1610 const char *Prefix = ThisTokBuf;
1611 while (ThisTokBuf[0] !=
'(')
1616 ThisTokEnd -= ThisTokBuf - Prefix;
1617 assert(ThisTokEnd >= ThisTokBuf &&
"malformed raw string literal");
1621 StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf);
1622 while (!RemainingTokenSpan.empty()) {
1624 size_t CRLFPos = RemainingTokenSpan.find(
"\r\n");
1625 StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos);
1626 StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos);
1629 if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF))
1634 RemainingTokenSpan = AfterCRLF.substr(1);
1637 if (ThisTokBuf[0] !=
'"') {
1640 return DiagnoseLexingError(StringToks[i].getLocation());
1645 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1646 ThisTokBuf[0] ==
'\\' && ThisTokBuf[1] ==
'p') {
1657 while (ThisTokBuf != ThisTokEnd) {
1659 if (ThisTokBuf[0] !=
'\\') {
1660 const char *InStart = ThisTokBuf;
1663 }
while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] !=
'\\');
1666 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1667 StringRef(InStart, ThisTokBuf - InStart)))
1672 if (ThisTokBuf[1] ==
'u' || ThisTokBuf[1] ==
'U') {
1676 CharByteWidth, Diags, Features);
1680 unsigned ResultChar =
1683 CharByteWidth*8, Diags, Features);
1685 if (CharByteWidth == 4) {
1688 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultPtr);
1689 *ResultWidePtr = ResultChar;
1691 }
else if (CharByteWidth == 2) {
1694 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultPtr);
1695 *ResultWidePtr = ResultChar & 0xFFFF;
1698 assert(CharByteWidth == 1 &&
"Unexpected char width");
1699 *ResultPtr++ = ResultChar & 0xFF;
1706 if (CharByteWidth == 4) {
1709 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf.data());
1711 }
else if (CharByteWidth == 2) {
1714 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf.data());
1717 assert(CharByteWidth == 1 &&
"Unexpected char width");
1724 Diags->
Report(StringToks.front().getLocation(),
1725 diag::err_pascal_string_too_long)
1727 StringToks.back().getLocation());
1733 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1736 Diags->
Report(StringToks.front().getLocation(),
1737 diag::ext_string_too_long)
1739 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1741 StringToks.back().getLocation());
1748 End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err);
1749 while (++Err != End && (*Err & 0xC0) == 0x80)
1757 bool StringLiteralParser::CopyStringFragment(
const Token &
Tok,
1758 const char *TokBegin,
1759 StringRef Fragment) {
1760 const llvm::UTF8 *ErrorPtrTmp;
1761 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1767 bool NoErrorOnBadEncoding =
isAscii();
1768 if (NoErrorOnBadEncoding) {
1769 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1770 ResultPtr += Fragment.size();
1774 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1778 Diag(Diags, Features, SourceLoc, TokBegin,
1779 ErrorPtr,
resyncUTF8(ErrorPtr, Fragment.end()),
1780 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1781 : diag::err_bad_string_encoding);
1783 const char *NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1784 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1788 Dummy.reserve(Fragment.size() * CharByteWidth);
1789 char *Ptr = Dummy.data();
1791 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1792 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1793 NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1795 ErrorPtr, NextStart);
1796 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1799 return !NoErrorOnBadEncoding;
1802 void StringLiteralParser::DiagnoseLexingError(
SourceLocation Loc) {
1805 Diags->
Report(Loc, diag::err_lexing_string);
1812 unsigned ByteNo)
const {
1817 bool StringInvalid =
false;
1818 const char *SpellingPtr = &SpellingBuffer[0];
1824 const char *SpellingStart = SpellingPtr;
1825 const char *SpellingEnd = SpellingPtr+TokLen;
1828 if (SpellingPtr[0] ==
'u' && SpellingPtr[1] ==
'8')
1831 assert(SpellingPtr[0] !=
'L' && SpellingPtr[0] !=
'u' &&
1832 SpellingPtr[0] !=
'U' &&
"Doesn't handle wide or utf strings yet");
1835 if (SpellingPtr[0] ==
'R') {
1836 assert(SpellingPtr[1] ==
'"' &&
"Should be a raw string literal!");
1839 while (*SpellingPtr !=
'(') {
1841 assert(SpellingPtr < SpellingEnd &&
"Missing ( for raw string literal");
1845 return SpellingPtr - SpellingStart + ByteNo;
1849 assert(SpellingPtr[0] ==
'"' &&
"Should be a string literal!");
1854 assert(SpellingPtr < SpellingEnd &&
"Didn't find byte offset!");
1857 if (*SpellingPtr !=
'\\') {
1864 bool HadError =
false;
1865 if (SpellingPtr[1] ==
'u' || SpellingPtr[1] ==
'U') {
1866 const char *EscapePtr = SpellingPtr;
1868 1, Features, HadError);
1871 SpellingPtr = EscapePtr;
1878 CharByteWidth*8, Diags, Features);
1881 assert(!HadError &&
"This method isn't valid on erroneous strings");
1884 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.
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.
Defines the clang::TargetInfo interface.
A SourceLocation and its associated SourceManager.
__DEVICE__ int max(int __a, int __b)
__DEVICE__ int min(int __a, int __b)
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.