20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/ConvertUTF.h"
22 #include "llvm/Support/ErrorHandling.h"
24 using namespace clang;
28 default: llvm_unreachable(
"Unknown token type!");
29 case tok::char_constant:
30 case tok::string_literal:
31 case tok::utf8_char_constant:
32 case tok::utf8_string_literal:
34 case tok::wide_char_constant:
35 case tok::wide_string_literal:
37 case tok::utf16_char_constant:
38 case tok::utf16_string_literal:
40 case tok::utf32_char_constant:
41 case tok::utf32_string_literal:
49 const char *TokRangeBegin,
50 const char *TokRangeEnd) {
67 const char *TokBegin,
const char *TokRangeBegin,
68 const char *TokRangeEnd,
unsigned DiagID) {
72 return Diags->
Report(Begin, DiagID) <<
79 const char *&ThisTokBuf,
80 const char *ThisTokEnd,
bool &HadError,
84 const char *EscapeBegin = ThisTokBuf;
91 unsigned ResultChar = *ThisTokBuf++;
94 case '\\':
case '\'':
case '"':
case '?':
break;
106 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
107 diag::ext_nonstandard_escape) <<
"e";
112 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
113 diag::ext_nonstandard_escape) <<
"E";
133 if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
135 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
136 diag::err_hex_escape_no_digits) <<
"x";
142 bool Overflow =
false;
143 for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
144 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
145 if (CharVal == -1)
break;
147 if (ResultChar & 0xF0000000)
150 ResultChar |= CharVal;
154 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
156 ResultChar &= ~0U >> (32-CharWidth);
160 if (Overflow && Diags)
161 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
162 diag::err_escape_too_large) << 0;
165 case '0':
case '1':
case '2':
case '3':
166 case '4':
case '5':
case '6':
case '7': {
173 unsigned NumDigits = 0;
176 ResultChar |= *ThisTokBuf++ -
'0';
178 }
while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
179 ThisTokBuf[0] >=
'0' && ThisTokBuf[0] <=
'7');
182 if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
184 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
185 diag::err_escape_too_large) << 1;
186 ResultChar &= ~0U >> (32-CharWidth);
192 case '(':
case '{':
case '[':
case '%':
195 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
196 diag::ext_nonstandard_escape)
197 << std::string(1, ResultChar);
204 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
205 diag::ext_unknown_escape)
206 << std::string(1, ResultChar);
208 Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
209 diag::ext_unknown_escape)
210 <<
"x" + llvm::utohexstr(ResultChar);
220 char *ResultPtr = ResultBuf;
221 bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr);
223 assert(Res &&
"Unexpected conversion failure");
224 Str.append(ResultBuf, ResultPtr);
235 assert(*
I ==
'u' || *
I ==
'U');
237 unsigned NumHexDigits;
243 assert(
I + NumHexDigits <=
E);
245 uint32_t CodePoint = 0;
246 for (++
I; NumHexDigits != 0; ++
I, --NumHexDigits) {
247 unsigned Value = llvm::hexDigitValue(*
I);
248 assert(Value != -1U);
262 const char *ThisTokEnd,
263 uint32_t &UcnVal,
unsigned short &UcnLen,
266 bool in_char_string_literal =
false) {
267 const char *UcnBegin = ThisTokBuf;
272 if (ThisTokBuf == ThisTokEnd || !
isHexDigit(*ThisTokBuf)) {
274 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
275 diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
278 UcnLen = (ThisTokBuf[-1] ==
'u' ? 4 : 8);
279 unsigned short UcnLenSave = UcnLen;
280 for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
281 int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
282 if (CharVal == -1)
break;
289 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
290 diag::err_ucn_escape_incomplete);
295 if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) ||
298 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
299 diag::err_ucn_escape_invalid);
306 (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) {
307 bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
309 char BasicSCSChar = UcnVal;
310 if (UcnVal >= 0x20 && UcnVal < 0x7f)
311 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
312 IsError ? diag::err_ucn_escape_basic_scs :
313 diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
314 << StringRef(&BasicSCSChar, 1);
316 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
317 IsError ? diag::err_ucn_control_character :
318 diag::warn_cxx98_compat_literal_ucn_control_character);
324 if (!Features.CPlusPlus && !Features.C99 && Diags)
325 Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
326 diag::warn_ucn_not_valid_in_c89_literal);
334 const char *ThisTokEnd,
unsigned CharByteWidth,
337 if (CharByteWidth == 4)
341 unsigned short UcnLen = 0;
345 UcnLen, Loc,
nullptr, Features,
true)) {
351 if (CharByteWidth == 2)
352 return UcnVal <= 0xFFFF ? 2 : 4;
359 if (UcnVal < 0x10000)
369 const char *ThisTokEnd,
370 char *&ResultBuf,
bool &HadError,
374 typedef uint32_t UTF32;
376 unsigned short UcnLen = 0;
378 Loc, Diags, Features,
true)) {
383 assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
384 "only character widths of 1, 2, or 4 bytes supported");
387 assert((UcnLen== 4 || UcnLen== 8) &&
"only ucn length of 4 or 8 supported");
389 if (CharByteWidth == 4) {
392 UTF32 *ResultPtr =
reinterpret_cast<UTF32*
>(ResultBuf);
398 if (CharByteWidth == 2) {
401 UTF16 *ResultPtr =
reinterpret_cast<UTF16*
>(ResultBuf);
403 if (UcnVal <= (UTF32)0xFFFF) {
411 *ResultPtr = 0xD800 + (UcnVal >> 10);
412 *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
417 assert(CharByteWidth == 1 &&
"UTF-8 encoding is only for 1 byte characters");
423 typedef uint8_t UTF8;
425 unsigned short bytesToWrite = 0;
426 if (UcnVal < (UTF32)0x80)
428 else if (UcnVal < (UTF32)0x800)
430 else if (UcnVal < (UTF32)0x10000)
435 const unsigned byteMask = 0xBF;
436 const unsigned byteMark = 0x80;
440 static const UTF8 firstByteMark[5] = {
441 0x00, 0x00, 0xC0, 0xE0, 0xF0
444 ResultBuf += bytesToWrite;
445 switch (bytesToWrite) {
446 case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
447 case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
448 case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
449 case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
452 ResultBuf += bytesToWrite;
510 : PP(PP), ThisTokBegin(TokSpelling.
begin()), ThisTokEnd(TokSpelling.
end()) {
518 s = DigitsBegin = ThisTokBegin;
519 saw_exponent =
false;
521 saw_ud_suffix =
false;
533 ParseNumberStartingWithZero(TokLoc);
539 if (s == ThisTokEnd) {
542 ParseDecimalOrOctalCommon(TokLoc);
549 checkSeparator(TokLoc, s, CSK_AfterDigits);
554 const char *ImaginarySuffixLoc =
nullptr;
558 for (; s != ThisTokEnd; ++s) {
564 if (!isFPConstant)
break;
570 if (!isFPConstant)
break;
577 if (!isFPConstant)
break;
584 if (isFPConstant)
break;
595 assert(s + 1 < ThisTokEnd &&
"didn't maximally munch?");
596 if (isFPConstant)
break;
639 assert(s <= ThisTokEnd &&
"didn't maximally munch?");
651 ImaginarySuffixLoc = s;
658 if (s != ThisTokEnd) {
660 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
672 saw_ud_suffix =
true;
678 diag::err_invalid_suffix_constant)
679 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin) << isFPConstant;
686 ImaginarySuffixLoc - ThisTokBegin),
687 diag::ext_imaginary_constant);
694 void NumericLiteralParser::ParseDecimalOrOctalCommon(
SourceLocation TokLoc){
695 assert((radix == 8 || radix == 10) &&
"Unexpected radix");
699 if (
isHexDigit(*s) && *s !=
'e' && *s !=
'E') {
701 diag::err_invalid_digit) << StringRef(s, 1) << (radix == 8 ? 1 : 0);
707 checkSeparator(TokLoc, s, CSK_AfterDigits);
711 checkSeparator(TokLoc, s, CSK_BeforeDigits);
714 if (*s ==
'e' || *s ==
'E') {
715 checkSeparator(TokLoc, s, CSK_AfterDigits);
716 const char *Exponent = s;
720 if (*s ==
'+' || *s ==
'-') s++;
721 const char *first_non_digit = SkipDigits(s);
722 if (containsDigits(s, first_non_digit)) {
723 checkSeparator(TokLoc, s, CSK_BeforeDigits);
727 diag::err_exponent_has_no_digits);
739 if (!LangOpts.CPlusPlus11 || Suffix.empty())
743 if (Suffix[0] ==
'_')
747 if (!LangOpts.CPlusPlus14)
752 return llvm::StringSwitch<bool>(Suffix)
753 .Cases(
"h",
"min",
"s",
true)
754 .Cases(
"ms",
"us",
"ns",
true)
755 .Cases(
"il",
"i",
"if",
true)
761 CheckSeparatorKind IsAfterDigits) {
762 if (IsAfterDigits == CSK_AfterDigits) {
763 if (Pos == ThisTokBegin)
766 }
else if (Pos == ThisTokEnd)
769 if (isDigitSeparator(*Pos))
771 diag::err_digit_separator_not_between_digits)
780 void NumericLiteralParser::ParseNumberStartingWithZero(
SourceLocation TokLoc) {
781 assert(s[0] ==
'0' &&
"Invalid method call");
787 if ((c1 ==
'x' || c1 ==
'X') && (
isHexDigit(s[1]) || s[1] ==
'.')) {
789 assert(s < ThisTokEnd &&
"didn't maximally munch?");
792 s = SkipHexDigits(s);
793 bool HasSignificandDigits = containsDigits(DigitsBegin, s);
794 if (s == ThisTokEnd) {
796 }
else if (*s ==
'.') {
799 const char *floatDigitsBegin = s;
800 s = SkipHexDigits(s);
801 if (containsDigits(floatDigitsBegin, s))
802 HasSignificandDigits =
true;
803 if (HasSignificandDigits)
804 checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits);
807 if (!HasSignificandDigits) {
809 diag::err_hex_constant_requires)
817 if (*s ==
'p' || *s ==
'P') {
818 checkSeparator(TokLoc, s, CSK_AfterDigits);
819 const char *Exponent = s;
822 if (*s ==
'+' || *s ==
'-') s++;
823 const char *first_non_digit = SkipDigits(s);
824 if (!containsDigits(s, first_non_digit)) {
826 diag::err_exponent_has_no_digits);
830 checkSeparator(TokLoc, s, CSK_BeforeDigits);
835 ? diag::ext_hex_literal_invalid
836 : diag::ext_hex_constant_invalid);
838 PP.
Diag(TokLoc, diag::warn_cxx1z_hex_literal);
839 }
else if (saw_period) {
841 diag::err_hex_constant_requires)
849 if ((c1 ==
'b' || c1 ==
'B') && (s[1] ==
'0' || s[1] ==
'1')) {
853 ? diag::warn_cxx11_compat_binary_literal
855 ? diag::ext_binary_literal_cxx14
856 : diag::ext_binary_literal);
858 assert(s < ThisTokEnd &&
"didn't maximally munch?");
861 s = SkipBinaryDigits(s);
862 if (s == ThisTokEnd) {
866 diag::err_invalid_digit) << StringRef(s, 1) << 2;
878 s = SkipOctalDigits(s);
885 const char *EndDecimal = SkipDigits(s);
886 if (EndDecimal[0] ==
'.' || EndDecimal[0] ==
'e' || EndDecimal[0] ==
'E') {
892 ParseDecimalOrOctalCommon(TokLoc);
898 return NumDigits <= 64;
900 return NumDigits <= 64 / 3;
902 return NumDigits <= 19;
904 return NumDigits <= 64 / 4;
906 llvm_unreachable(
"impossible Radix");
920 const unsigned NumDigits = SuffixBegin - DigitsBegin;
923 for (
const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
924 if (!isDigitSeparator(*Ptr))
925 N = N * radix + llvm::hexDigitValue(*Ptr);
930 return Val.getZExtValue() != N;
934 const char *Ptr = DigitsBegin;
936 llvm::APInt RadixVal(Val.getBitWidth(), radix);
937 llvm::APInt CharVal(Val.getBitWidth(), 0);
938 llvm::APInt OldVal = Val;
940 bool OverflowOccurred =
false;
941 while (Ptr < SuffixBegin) {
942 if (isDigitSeparator(*Ptr)) {
947 unsigned C = llvm::hexDigitValue(*Ptr++);
950 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
960 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
965 OverflowOccurred |= Val.ult(CharVal);
967 return OverflowOccurred;
970 llvm::APFloat::opStatus
974 unsigned n =
std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
977 StringRef Str(ThisTokBegin, n);
978 if (Str.find(
'\'') != StringRef::npos) {
980 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
985 return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
1036 const char *TokBegin =
begin;
1039 if (
Kind != tok::char_constant)
1041 if (
Kind == tok::utf8_char_constant)
1045 assert(begin[0] ==
'\'' &&
"Invalid token lexed");
1049 if (end[-1] !=
'\'') {
1050 const char *UDSuffixEnd =
end;
1053 }
while (end[-1] !=
'\'');
1055 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1056 UDSuffixOffset = end - TokBegin;
1060 assert(end != begin &&
"Invalid token lexed");
1067 "Assumes char is 8 bits");
1070 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1072 "Assumes sizeof(wchar) on target is <= 64");
1075 codepoint_buffer.resize(end - begin);
1076 uint32_t *buffer_begin = &codepoint_buffer.front();
1077 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1082 uint32_t largest_character_for_kind;
1083 if (tok::wide_char_constant ==
Kind) {
1084 largest_character_for_kind =
1086 }
else if (tok::utf8_char_constant ==
Kind) {
1087 largest_character_for_kind = 0x7F;
1088 }
else if (tok::utf16_char_constant ==
Kind) {
1089 largest_character_for_kind = 0xFFFF;
1090 }
else if (tok::utf32_char_constant ==
Kind) {
1091 largest_character_for_kind = 0x10FFFF;
1093 largest_character_for_kind = 0x7Fu;
1096 while (begin != end) {
1098 if (begin[0] !=
'\\') {
1099 char const *start =
begin;
1102 }
while (begin != end && *begin !=
'\\');
1104 char const *tmp_in_start = start;
1105 uint32_t *tmp_out_start = buffer_begin;
1106 ConversionResult res =
1107 ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
1108 reinterpret_cast<UTF8 const *>(begin),
1109 &buffer_begin, buffer_end, strictConversion);
1110 if (res != conversionOK) {
1114 bool NoErrorOnBadEncoding =
isAscii();
1115 unsigned Msg = diag::err_bad_character_encoding;
1116 if (NoErrorOnBadEncoding)
1117 Msg = diag::warn_bad_character_encoding;
1119 if (NoErrorOnBadEncoding) {
1120 start = tmp_in_start;
1121 buffer_begin = tmp_out_start;
1122 for (; start !=
begin; ++start, ++buffer_begin)
1123 *buffer_begin = static_cast<uint8_t>(*start);
1128 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1129 if (*tmp_out_start > largest_character_for_kind) {
1131 PP.
Diag(Loc, diag::err_character_too_large);
1139 if (begin[1] ==
'u' || begin[1] ==
'U') {
1140 unsigned short UcnLen = 0;
1145 }
else if (*buffer_begin > largest_character_for_kind) {
1147 PP.
Diag(Loc, diag::err_character_too_large);
1158 *buffer_begin++ = result;
1161 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1163 if (NumCharsSoFar > 1) {
1165 PP.
Diag(Loc, diag::warn_extraneous_char_constant);
1166 else if (
isAscii() && NumCharsSoFar == 4)
1167 PP.
Diag(Loc, diag::ext_four_char_character_literal);
1169 PP.
Diag(Loc, diag::ext_multichar_character_literal);
1171 PP.
Diag(Loc, diag::err_multichar_utf_character_literal);
1174 IsMultiChar =
false;
1181 bool multi_char_too_long =
false;
1184 for (
size_t i = 0; i < NumCharsSoFar; ++i) {
1186 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1188 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1190 }
else if (NumCharsSoFar > 0) {
1192 LitVal = buffer_begin[-1];
1195 if (!HadError && multi_char_too_long) {
1196 PP.
Diag(Loc, diag::warn_char_constant_too_large);
1200 Value = LitVal.getZExtValue();
1206 if (
isAscii() && NumCharsSoFar == 1 && (
Value & 128) &&
1268 :
SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1269 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1270 MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown),
1271 ResultPtr(ResultBuf.data()), hadError(
false), Pascal(
false) {
1278 if (StringToks.empty() || StringToks[0].getLength() < 2)
1285 assert(!StringToks.empty() &&
"expected at least one token");
1286 MaxTokenLength = StringToks[0].getLength();
1287 assert(StringToks[0].getLength() >= 2 &&
"literal token is invalid!");
1288 SizeBound = StringToks[0].getLength()-2;
1289 Kind = StringToks[0].getKind();
1295 for (
unsigned i = 1; i != StringToks.size(); ++i) {
1296 if (StringToks[i].getLength() < 2)
1297 return DiagnoseLexingError(StringToks[i].getLocation());
1301 assert(StringToks[i].getLength() >= 2 &&
"literal token is invalid!");
1302 SizeBound += StringToks[i].getLength()-2;
1305 if (StringToks[i].getLength() > MaxTokenLength)
1306 MaxTokenLength = StringToks[i].getLength();
1310 if (StringToks[i].isNot(
Kind) && StringToks[i].isNot(tok::string_literal)) {
1312 Kind = StringToks[i].getKind();
1315 Diags->
Report(StringToks[i].getLocation(),
1316 diag::err_unsupported_string_concat);
1329 assert((CharByteWidth & 7) == 0 &&
"Assumes character size is byte multiple");
1334 SizeBound *= CharByteWidth;
1337 ResultBuf.resize(SizeBound);
1341 TokenBuf.resize(MaxTokenLength);
1345 ResultPtr = &ResultBuf[0];
1351 for (
unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1352 const char *ThisTokBuf = &TokenBuf[0];
1356 bool StringInvalid =
false;
1357 unsigned ThisTokLen =
1361 return DiagnoseLexingError(StringToks[i].getLocation());
1363 const char *ThisTokBegin = ThisTokBuf;
1364 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1367 if (ThisTokEnd[-1] !=
'"') {
1368 const char *UDSuffixEnd = ThisTokEnd;
1371 }
while (ThisTokEnd[-1] !=
'"');
1373 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1375 if (UDSuffixBuf.empty()) {
1376 if (StringToks[i].hasUCN())
1379 UDSuffixBuf.assign(UDSuffix);
1381 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1382 UDSuffixTokLoc = StringToks[i].getLocation();
1385 if (StringToks[i].hasUCN()) {
1387 UDSuffix = ExpandedUDSuffix;
1394 if (UDSuffixBuf != UDSuffix) {
1397 Diags->
Report(TokLoc, diag::err_string_concat_mixed_suffix)
1398 << UDSuffixBuf << UDSuffix
1413 if (ThisTokBuf[0] ==
'L' || ThisTokBuf[0] ==
'u' || ThisTokBuf[0] ==
'U') {
1416 if (ThisTokBuf[0] ==
'8')
1421 if (ThisTokBuf[0] ==
'R') {
1424 const char *Prefix = ThisTokBuf;
1425 while (ThisTokBuf[0] !=
'(')
1430 ThisTokEnd -= ThisTokBuf - Prefix;
1431 assert(ThisTokEnd >= ThisTokBuf &&
"malformed raw string literal");
1435 StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf);
1436 while (!RemainingTokenSpan.empty()) {
1438 size_t CRLFPos = RemainingTokenSpan.find(
"\r\n");
1439 StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos);
1440 StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos);
1443 if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF))
1448 RemainingTokenSpan = AfterCRLF.substr(1);
1451 if (ThisTokBuf[0] !=
'"') {
1454 return DiagnoseLexingError(StringToks[i].getLocation());
1459 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1460 ThisTokBuf[0] ==
'\\' && ThisTokBuf[1] ==
'p') {
1471 while (ThisTokBuf != ThisTokEnd) {
1473 if (ThisTokBuf[0] !=
'\\') {
1474 const char *InStart = ThisTokBuf;
1477 }
while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] !=
'\\');
1480 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1481 StringRef(InStart, ThisTokBuf - InStart)))
1486 if (ThisTokBuf[1] ==
'u' || ThisTokBuf[1] ==
'U') {
1490 CharByteWidth, Diags, Features);
1494 unsigned ResultChar =
1497 CharByteWidth*8, Diags, Features);
1499 if (CharByteWidth == 4) {
1502 UTF32 *ResultWidePtr =
reinterpret_cast<UTF32*
>(ResultPtr);
1503 *ResultWidePtr = ResultChar;
1505 }
else if (CharByteWidth == 2) {
1508 UTF16 *ResultWidePtr =
reinterpret_cast<UTF16*
>(ResultPtr);
1509 *ResultWidePtr = ResultChar & 0xFFFF;
1512 assert(CharByteWidth == 1 &&
"Unexpected char width");
1513 *ResultPtr++ = ResultChar & 0xFF;
1520 if (CharByteWidth == 4) {
1523 UTF32 *ResultWidePtr =
reinterpret_cast<UTF32*
>(ResultBuf.data());
1525 }
else if (CharByteWidth == 2) {
1528 UTF16 *ResultWidePtr =
reinterpret_cast<UTF16*
>(ResultBuf.data());
1531 assert(CharByteWidth == 1 &&
"Unexpected char width");
1538 Diags->
Report(StringToks.front().getLocation(),
1539 diag::err_pascal_string_too_long)
1541 StringToks.back().getLocation());
1547 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1550 Diags->
Report(StringToks.front().getLocation(),
1551 diag::ext_string_too_long)
1553 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1555 StringToks.back().getLocation());
1562 End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err);
1563 while (++Err != End && (*Err & 0xC0) == 0x80)
1571 bool StringLiteralParser::CopyStringFragment(
const Token &Tok,
1572 const char *TokBegin,
1573 StringRef Fragment) {
1574 const UTF8 *ErrorPtrTmp;
1575 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1581 bool NoErrorOnBadEncoding =
isAscii();
1582 if (NoErrorOnBadEncoding) {
1583 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1584 ResultPtr += Fragment.size();
1588 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1592 Diag(Diags, Features, SourceLoc, TokBegin,
1593 ErrorPtr,
resyncUTF8(ErrorPtr, Fragment.end()),
1594 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1595 : diag::err_bad_string_encoding);
1597 const char *NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1598 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1602 Dummy.reserve(Fragment.size() * CharByteWidth);
1603 char *Ptr = Dummy.data();
1605 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1606 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1607 NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1609 ErrorPtr, NextStart);
1610 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1613 return !NoErrorOnBadEncoding;
1616 void StringLiteralParser::DiagnoseLexingError(
SourceLocation Loc) {
1619 Diags->
Report(Loc, diag::err_lexing_string);
1626 unsigned ByteNo)
const {
1631 bool StringInvalid =
false;
1632 const char *SpellingPtr = &SpellingBuffer[0];
1638 const char *SpellingStart = SpellingPtr;
1639 const char *SpellingEnd = SpellingPtr+TokLen;
1642 if (SpellingPtr[0] ==
'u' && SpellingPtr[1] ==
'8')
1645 assert(SpellingPtr[0] !=
'L' && SpellingPtr[0] !=
'u' &&
1646 SpellingPtr[0] !=
'U' &&
"Doesn't handle wide or utf strings yet");
1649 if (SpellingPtr[0] ==
'R') {
1650 assert(SpellingPtr[1] ==
'"' &&
"Should be a raw string literal!");
1653 while (*SpellingPtr !=
'(') {
1655 assert(SpellingPtr < SpellingEnd &&
"Missing ( for raw string literal");
1659 return SpellingPtr - SpellingStart + ByteNo;
1663 assert(SpellingPtr[0] ==
'"' &&
"Should be a string literal!");
1668 assert(SpellingPtr < SpellingEnd &&
"Didn't find byte offset!");
1671 if (*SpellingPtr !=
'\\') {
1678 bool HadError =
false;
1679 if (SpellingPtr[1] ==
'u' || SpellingPtr[1] ==
'U') {
1680 const char *EscapePtr = SpellingPtr;
1682 1, Features, HadError);
1685 SpellingPtr = EscapePtr;
1692 CharByteWidth*8, Diags, Features);
1695 assert(!HadError &&
"This method isn't valid on erroneous strings");
1698 return SpellingPtr-SpellingStart;
SourceManager & getSourceManager() const
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.
static LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
unsigned getChar16Width() const
getChar16Width/Align - Return the size of 'char16_t' for this target, in bits.
StringLiteralParser(ArrayRef< Token > StringToks, Preprocessor &PP, bool Complain=true)
const SourceManager & getManager() const
std::unique_ptr< llvm::MemoryBuffer > Buffer
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getChar32Width() const
getChar32Width/Align - Return the size of 'char32_t' for this target, in bits.
static 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_.
static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits)
unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const
getOffsetOfStringByte - This function returns the offset of the specified byte of the string data rep...
const LangOptions & getLangOpts() 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...
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...
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...
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...
const TargetInfo & getTargetInfo() const
static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, unsigned Character, const SourceManager &SM, const LangOptions &LangOpts)
AdvanceToTokenCharacter - If the current SourceLocation specifies a location at the start of a token...
detail::InMemoryDirectory::const_iterator I
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
unsigned getWCharWidth() const
getWCharWidth/Align - Return the size of 'wchar_t' for this target, in bits.
A little helper class used to produce diagnostics.
bool isFloatingLiteral() const
Exposes information about the current target.
CharLiteralParser(const char *begin, const char *end, SourceLocation Loc, Preprocessor &PP, tok::TokenKind kind)
Represents a character-granular source range.
Defines the clang::Preprocessor interface.
static const char * resyncUTF8(const char *Err, const char *End)
char __ovld __cnfn min(char x, char y)
Returns y if y < x, otherwise it returns x.
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
static void appendCodePoint(unsigned Codepoint, llvm::SmallVectorImpl< char > &Str)
static CharSourceRange getCharRange(SourceRange R)
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...
const TemplateArgument * iterator
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.
DiagnosticsEngine & getDiagnostics() const
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 ...
static LLVM_READONLY bool isPrintable(unsigned char c)
Return true if this character is an ASCII printable character; that is, a character that should take ...
unsigned getCharWidth() const
detail::InMemoryDirectory::const_iterator E
unsigned getIntWidth() const
getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for this target, in bits.
BoundNodesTreeBuilder *const Builder
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.
unsigned GetStringLength() const
A SourceLocation and its associated SourceManager.
unsigned getLength() const
A trivial tuple used to represent a source range.
unsigned GetNumStringChars() const
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...
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.
static LLVM_READONLY bool isHexDigit(unsigned char c)
Return true if this character is an ASCII hex digit: [0-9a-fA-F].