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;
1005 llvm::APInt RadixVal(Val.getBitWidth(), radix);
1006 llvm::APInt CharVal(Val.getBitWidth(), 0);
1007 llvm::APInt OldVal = Val;
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),
1054 return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
1058 return c ==
'p' || c ==
'P' || c ==
'e' || c ==
'E';
1062 assert(radix == 16 || radix == 10);
1065 unsigned NumDigits = SuffixBegin - DigitsBegin;
1066 if (saw_period) --NumDigits;
1069 bool ExpOverflowOccurred =
false;
1070 bool NegativeExponent =
false;
1071 const char *ExponentBegin;
1072 uint64_t Exponent = 0;
1073 int64_t BaseShift = 0;
1075 const char *Ptr = DigitsBegin;
1078 ExponentBegin = Ptr;
1080 NegativeExponent = *Ptr ==
'-';
1081 if (NegativeExponent) ++Ptr;
1083 unsigned NumExpDigits = SuffixBegin - Ptr;
1085 llvm::StringRef ExpStr(Ptr, NumExpDigits);
1086 llvm::APInt ExpInt(64, ExpStr, 10);
1087 Exponent = ExpInt.getZExtValue();
1089 ExpOverflowOccurred =
true;
1092 if (NegativeExponent) BaseShift -= Exponent;
1093 else BaseShift += Exponent;
1113 uint64_t NumBitsNeeded;
1115 NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale;
1117 NumBitsNeeded = 4 * NumDigits + Exponent + Scale;
1120 ExpOverflowOccurred =
true;
1121 llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0,
false);
1123 bool FoundDecimal =
false;
1125 int64_t FractBaseShift = 0;
1126 const char *
End = saw_exponent ? ExponentBegin : SuffixBegin;
1127 for (
const char *Ptr = DigitsBegin; Ptr <
End; ++Ptr) {
1129 FoundDecimal =
true;
1134 unsigned C = llvm::hexDigitValue(*Ptr);
1135 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
1147 if (radix == 16) FractBaseShift *= 4;
1148 BaseShift += FractBaseShift;
1152 uint64_t
Base = (radix == 16) ? 2 : 10;
1153 if (BaseShift > 0) {
1154 for (int64_t
i = 0;
i < BaseShift; ++
i) {
1157 }
else if (BaseShift < 0) {
1158 for (int64_t
i = BaseShift;
i < 0 && !Val.isNullValue(); ++
i)
1159 Val = Val.udiv(Base);
1162 bool IntOverflowOccurred =
false;
1163 auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth());
1164 if (Val.getBitWidth() > StoreVal.getBitWidth()) {
1165 IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth()));
1166 StoreVal = Val.trunc(StoreVal.getBitWidth());
1167 }
else if (Val.getBitWidth() < StoreVal.getBitWidth()) {
1168 IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal);
1169 StoreVal = Val.zext(StoreVal.getBitWidth());
1174 return IntOverflowOccurred || ExpOverflowOccurred;
1224 const char *TokBegin = begin;
1227 if (
Kind != tok::char_constant)
1229 if (
Kind == tok::utf8_char_constant)
1233 assert(begin[0] ==
'\'' &&
"Invalid token lexed");
1237 if (end[-1] !=
'\'') {
1238 const char *UDSuffixEnd = end;
1241 }
while (end[-1] !=
'\'');
1243 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1244 UDSuffixOffset = end - TokBegin;
1248 assert(end != begin &&
"Invalid token lexed");
1255 "Assumes char is 8 bits");
1258 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1260 "Assumes sizeof(wchar) on target is <= 64");
1263 codepoint_buffer.resize(end - begin);
1264 uint32_t *buffer_begin = &codepoint_buffer.front();
1265 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1270 uint32_t largest_character_for_kind;
1271 if (tok::wide_char_constant ==
Kind) {
1272 largest_character_for_kind =
1274 }
else if (tok::utf8_char_constant ==
Kind) {
1275 largest_character_for_kind = 0x7F;
1276 }
else if (tok::utf16_char_constant ==
Kind) {
1277 largest_character_for_kind = 0xFFFF;
1278 }
else if (tok::utf32_char_constant ==
Kind) {
1279 largest_character_for_kind = 0x10FFFF;
1281 largest_character_for_kind = 0x7Fu;
1284 while (begin != end) {
1286 if (begin[0] !=
'\\') {
1287 char const *start = begin;
1290 }
while (begin != end && *begin !=
'\\');
1292 char const *tmp_in_start = start;
1293 uint32_t *tmp_out_start = buffer_begin;
1294 llvm::ConversionResult
res =
1295 llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start),
1296 reinterpret_cast<llvm::UTF8 const *>(begin),
1297 &buffer_begin, buffer_end, llvm::strictConversion);
1298 if (res != llvm::conversionOK) {
1302 bool NoErrorOnBadEncoding = isAscii();
1303 unsigned Msg = diag::err_bad_character_encoding;
1304 if (NoErrorOnBadEncoding)
1305 Msg = diag::warn_bad_character_encoding;
1307 if (NoErrorOnBadEncoding) {
1308 start = tmp_in_start;
1309 buffer_begin = tmp_out_start;
1310 for (; start != begin; ++start, ++buffer_begin)
1311 *buffer_begin = static_cast<uint8_t>(*start);
1316 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1317 if (*tmp_out_start > largest_character_for_kind) {
1319 PP.
Diag(Loc, diag::err_character_too_large);
1327 if (begin[1] ==
'u' || begin[1] ==
'U') {
1328 unsigned short UcnLen = 0;
1333 }
else if (*buffer_begin > largest_character_for_kind) {
1335 PP.
Diag(Loc, diag::err_character_too_large);
1346 *buffer_begin++ =
result;
1349 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1351 if (NumCharsSoFar > 1) {
1353 PP.
Diag(Loc, diag::warn_extraneous_char_constant);
1354 else if (isAscii() && NumCharsSoFar == 4)
1355 PP.
Diag(Loc, diag::ext_four_char_character_literal);
1357 PP.
Diag(Loc, diag::ext_multichar_character_literal);
1359 PP.
Diag(Loc, diag::err_multichar_utf_character_literal);
1362 IsMultiChar =
false;
1369 bool multi_char_too_long =
false;
1370 if (isAscii() && isMultiChar()) {
1372 for (
size_t i = 0;
i < NumCharsSoFar; ++
i) {
1374 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1376 LitVal = LitVal + (codepoint_buffer[
i] & 0xFF);
1378 }
else if (NumCharsSoFar > 0) {
1380 LitVal = buffer_begin[-1];
1383 if (!HadError && multi_char_too_long) {
1384 PP.
Diag(Loc, diag::warn_char_constant_too_large);
1388 Value = LitVal.getZExtValue();
1394 if (isAscii() && NumCharsSoFar == 1 && (
Value & 128) &&
1456 :
SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1457 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1458 MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown),
1466 if (StringToks.empty() || StringToks[0].getLength() < 2)
1473 assert(!StringToks.empty() &&
"expected at least one token");
1474 MaxTokenLength = StringToks[0].getLength();
1475 assert(StringToks[0].getLength() >= 2 &&
"literal token is invalid!");
1476 SizeBound = StringToks[0].getLength()-2;
1477 Kind = StringToks[0].getKind();
1483 for (
unsigned i = 1;
i != StringToks.size(); ++
i) {
1484 if (StringToks[
i].getLength() < 2)
1485 return DiagnoseLexingError(StringToks[
i].getLocation());
1489 assert(StringToks[
i].getLength() >= 2 &&
"literal token is invalid!");
1490 SizeBound += StringToks[
i].getLength()-2;
1493 if (StringToks[
i].getLength() > MaxTokenLength)
1494 MaxTokenLength = StringToks[
i].getLength();
1498 if (StringToks[
i].isNot(
Kind) && StringToks[
i].isNot(tok::string_literal)) {
1500 Kind = StringToks[
i].getKind();
1503 Diags->
Report(StringToks[
i].getLocation(),
1504 diag::err_unsupported_string_concat);
1517 assert((CharByteWidth & 7) == 0 &&
"Assumes character size is byte multiple");
1522 SizeBound *= CharByteWidth;
1525 ResultBuf.resize(SizeBound);
1529 TokenBuf.resize(MaxTokenLength);
1533 ResultPtr = &ResultBuf[0];
1539 for (
unsigned i = 0, e = StringToks.size();
i != e; ++
i) {
1540 const char *ThisTokBuf = &TokenBuf[0];
1544 bool StringInvalid =
false;
1545 unsigned ThisTokLen =
1549 return DiagnoseLexingError(StringToks[i].getLocation());
1551 const char *ThisTokBegin = ThisTokBuf;
1552 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1555 if (ThisTokEnd[-1] !=
'"') {
1556 const char *UDSuffixEnd = ThisTokEnd;
1559 }
while (ThisTokEnd[-1] !=
'"');
1561 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1563 if (UDSuffixBuf.empty()) {
1564 if (StringToks[i].hasUCN())
1567 UDSuffixBuf.assign(UDSuffix);
1569 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1570 UDSuffixTokLoc = StringToks[
i].getLocation();
1573 if (StringToks[i].hasUCN()) {
1575 UDSuffix = ExpandedUDSuffix;
1582 if (UDSuffixBuf != UDSuffix) {
1585 Diags->
Report(TokLoc, diag::err_string_concat_mixed_suffix)
1586 << UDSuffixBuf << UDSuffix
1601 if (ThisTokBuf[0] ==
'L' || ThisTokBuf[0] ==
'u' || ThisTokBuf[0] ==
'U') {
1604 if (ThisTokBuf[0] ==
'8')
1609 if (ThisTokBuf[0] ==
'R') {
1612 const char *Prefix = ThisTokBuf;
1613 while (ThisTokBuf[0] !=
'(')
1618 ThisTokEnd -= ThisTokBuf - Prefix;
1619 assert(ThisTokEnd >= ThisTokBuf &&
"malformed raw string literal");
1623 StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf);
1624 while (!RemainingTokenSpan.empty()) {
1626 size_t CRLFPos = RemainingTokenSpan.find(
"\r\n");
1627 StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos);
1628 StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos);
1631 if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF))
1636 RemainingTokenSpan = AfterCRLF.substr(1);
1639 if (ThisTokBuf[0] !=
'"') {
1642 return DiagnoseLexingError(StringToks[i].getLocation());
1647 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1648 ThisTokBuf[0] ==
'\\' && ThisTokBuf[1] ==
'p') {
1659 while (ThisTokBuf != ThisTokEnd) {
1661 if (ThisTokBuf[0] !=
'\\') {
1662 const char *InStart = ThisTokBuf;
1665 }
while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] !=
'\\');
1668 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1669 StringRef(InStart, ThisTokBuf - InStart)))
1674 if (ThisTokBuf[1] ==
'u' || ThisTokBuf[1] ==
'U') {
1678 CharByteWidth, Diags, Features);
1682 unsigned ResultChar =
1685 CharByteWidth*8, Diags, Features);
1687 if (CharByteWidth == 4) {
1690 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultPtr);
1691 *ResultWidePtr = ResultChar;
1693 }
else if (CharByteWidth == 2) {
1696 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultPtr);
1697 *ResultWidePtr = ResultChar & 0xFFFF;
1700 assert(CharByteWidth == 1 &&
"Unexpected char width");
1701 *ResultPtr++ = ResultChar & 0xFF;
1708 if (CharByteWidth == 4) {
1711 llvm::UTF32 *ResultWidePtr =
reinterpret_cast<llvm::UTF32*
>(ResultBuf.data());
1713 }
else if (CharByteWidth == 2) {
1716 llvm::UTF16 *ResultWidePtr =
reinterpret_cast<llvm::UTF16*
>(ResultBuf.data());
1719 assert(CharByteWidth == 1 &&
"Unexpected char width");
1726 Diags->
Report(StringToks.front().getLocation(),
1727 diag::err_pascal_string_too_long)
1729 StringToks.back().getLocation());
1735 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1738 Diags->
Report(StringToks.front().getLocation(),
1739 diag::ext_string_too_long)
1741 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1743 StringToks.back().getLocation());
1750 End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err);
1751 while (++Err != End && (*Err & 0xC0) == 0x80)
1759 bool StringLiteralParser::CopyStringFragment(
const Token &
Tok,
1760 const char *TokBegin,
1761 StringRef Fragment) {
1762 const llvm::UTF8 *ErrorPtrTmp;
1763 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1769 bool NoErrorOnBadEncoding =
isAscii();
1770 if (NoErrorOnBadEncoding) {
1771 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1772 ResultPtr += Fragment.size();
1776 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1780 Diag(Diags, Features, SourceLoc, TokBegin,
1781 ErrorPtr,
resyncUTF8(ErrorPtr, Fragment.end()),
1782 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1783 : diag::err_bad_string_encoding);
1785 const char *NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1786 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1790 Dummy.reserve(Fragment.size() * CharByteWidth);
1791 char *Ptr = Dummy.data();
1793 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1794 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1795 NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1797 ErrorPtr, NextStart);
1798 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1801 return !NoErrorOnBadEncoding;
1804 void StringLiteralParser::DiagnoseLexingError(
SourceLocation Loc) {
1807 Diags->
Report(Loc, diag::err_lexing_string);
1814 unsigned ByteNo)
const {
1819 bool StringInvalid =
false;
1820 const char *SpellingPtr = &SpellingBuffer[0];
1826 const char *SpellingStart = SpellingPtr;
1827 const char *SpellingEnd = SpellingPtr+TokLen;
1830 if (SpellingPtr[0] ==
'u' && SpellingPtr[1] ==
'8')
1833 assert(SpellingPtr[0] !=
'L' && SpellingPtr[0] !=
'u' &&
1834 SpellingPtr[0] !=
'U' &&
"Doesn't handle wide or utf strings yet");
1837 if (SpellingPtr[0] ==
'R') {
1838 assert(SpellingPtr[1] ==
'"' &&
"Should be a raw string literal!");
1841 while (*SpellingPtr !=
'(') {
1843 assert(SpellingPtr < SpellingEnd &&
"Missing ( for raw string literal");
1847 return SpellingPtr - SpellingStart + ByteNo;
1851 assert(SpellingPtr[0] ==
'"' &&
"Should be a string literal!");
1856 assert(SpellingPtr < SpellingEnd &&
"Didn't find byte offset!");
1859 if (*SpellingPtr !=
'\\') {
1866 bool HadError =
false;
1867 if (SpellingPtr[1] ==
'u' || SpellingPtr[1] ==
'U') {
1868 const char *EscapePtr = SpellingPtr;
1870 1, Features, HadError);
1873 SpellingPtr = EscapePtr;
1880 CharByteWidth*8, Diags, Features);
1883 assert(!HadError &&
"This method isn't valid on erroneous strings");
1886 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.