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_hex_escape_too_large);
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_octal_escape_too_large);
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);
228 for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
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;
531 ParseNumberStartingWithZero(TokLoc);
537 if (s == ThisTokEnd) {
539 }
else if (
isHexDigit(*s) && !(*s ==
'e' || *s ==
'E')) {
541 diag::err_invalid_decimal_digit) << StringRef(s, 1);
544 }
else if (*s ==
'.') {
545 checkSeparator(TokLoc, s, CSK_AfterDigits);
548 checkSeparator(TokLoc, s, CSK_BeforeDigits);
551 if ((*s ==
'e' || *s ==
'E')) {
552 checkSeparator(TokLoc, s, CSK_AfterDigits);
553 const char *Exponent = s;
556 if (*s ==
'+' || *s ==
'-') s++;
557 checkSeparator(TokLoc, s, CSK_BeforeDigits);
558 const char *first_non_digit = SkipDigits(s);
559 if (first_non_digit != s) {
563 diag::err_exponent_has_no_digits);
571 checkSeparator(TokLoc, s, CSK_AfterDigits);
576 const char *ImaginarySuffixLoc =
nullptr;
580 for (; s != ThisTokEnd; ++s) {
584 if (!isFPConstant)
break;
590 if (isFPConstant)
break;
601 assert(s + 1 < ThisTokEnd &&
"didn't maximally munch?");
602 if (isFPConstant)
break;
626 }
else if (s[2] ==
'2' && s[3] ==
'8') {
648 assert(s <= ThisTokEnd &&
"didn't maximally munch?");
660 ImaginarySuffixLoc = s;
667 if (s != ThisTokEnd) {
669 expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
680 saw_ud_suffix =
true;
686 isFPConstant ? diag::err_invalid_suffix_float_constant :
687 diag::err_invalid_suffix_integer_constant)
688 << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
695 ImaginarySuffixLoc - ThisTokBegin),
696 diag::ext_imaginary_constant);
705 if (!LangOpts.CPlusPlus11 || Suffix.empty())
709 if (Suffix[0] ==
'_')
713 if (!LangOpts.CPlusPlus14)
718 return llvm::StringSwitch<bool>(Suffix)
719 .Cases(
"h",
"min",
"s",
true)
720 .Cases(
"ms",
"us",
"ns",
true)
721 .Cases(
"il",
"i",
"if",
true)
727 CheckSeparatorKind IsAfterDigits) {
728 if (IsAfterDigits == CSK_AfterDigits) {
729 if (Pos == ThisTokBegin)
732 }
else if (Pos == ThisTokEnd)
735 if (isDigitSeparator(*Pos))
737 diag::err_digit_separator_not_between_digits)
746 void NumericLiteralParser::ParseNumberStartingWithZero(
SourceLocation TokLoc) {
747 assert(s[0] ==
'0' &&
"Invalid method call");
753 if ((c1 ==
'x' || c1 ==
'X') && (
isHexDigit(s[1]) || s[1] ==
'.')) {
755 assert(s < ThisTokEnd &&
"didn't maximally munch?");
758 s = SkipHexDigits(s);
759 bool noSignificand = (s == DigitsBegin);
760 if (s == ThisTokEnd) {
762 }
else if (*s ==
'.') {
765 const char *floatDigitsBegin = s;
766 checkSeparator(TokLoc, s, CSK_BeforeDigits);
767 s = SkipHexDigits(s);
768 noSignificand &= (floatDigitsBegin == s);
773 diag::err_hexconstant_requires_digits);
780 if (*s ==
'p' || *s ==
'P') {
781 checkSeparator(TokLoc, s, CSK_AfterDigits);
782 const char *Exponent = s;
785 if (*s ==
'+' || *s ==
'-') s++;
786 const char *first_non_digit = SkipDigits(s);
787 if (first_non_digit == s) {
789 diag::err_exponent_has_no_digits);
793 checkSeparator(TokLoc, s, CSK_BeforeDigits);
797 PP.
Diag(TokLoc, diag::ext_hexconstant_invalid);
798 }
else if (saw_period) {
800 diag::err_hexconstant_requires_exponent);
807 if ((c1 ==
'b' || c1 ==
'B') && (s[1] ==
'0' || s[1] ==
'1')) {
811 ? diag::warn_cxx11_compat_binary_literal
813 ? diag::ext_binary_literal_cxx14
814 : diag::ext_binary_literal);
816 assert(s < ThisTokEnd &&
"didn't maximally munch?");
819 s = SkipBinaryDigits(s);
820 if (s == ThisTokEnd) {
824 diag::err_invalid_binary_digit) << StringRef(s, 1);
836 s = SkipOctalDigits(s);
843 const char *EndDecimal = SkipDigits(s);
844 if (EndDecimal[0] ==
'.' || EndDecimal[0] ==
'e' || EndDecimal[0] ==
'E') {
852 if (
isHexDigit(*s) && *s !=
'e' && *s !=
'E') {
854 diag::err_invalid_octal_digit) << StringRef(s, 1);
863 checkSeparator(TokLoc, s, CSK_BeforeDigits);
866 if (*s ==
'e' || *s ==
'E') {
867 checkSeparator(TokLoc, s, CSK_AfterDigits);
868 const char *Exponent = s;
872 if (*s ==
'+' || *s ==
'-') s++;
873 const char *first_non_digit = SkipDigits(s);
874 if (first_non_digit != s) {
875 checkSeparator(TokLoc, s, CSK_BeforeDigits);
879 diag::err_exponent_has_no_digits);
889 return NumDigits <= 64;
891 return NumDigits <= 64 / 3;
893 return NumDigits <= 19;
895 return NumDigits <= 64 / 4;
897 llvm_unreachable(
"impossible Radix");
911 const unsigned NumDigits = SuffixBegin - DigitsBegin;
914 for (
const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
915 if (!isDigitSeparator(*Ptr))
916 N = N * radix + llvm::hexDigitValue(*Ptr);
921 return Val.getZExtValue() != N;
925 const char *Ptr = DigitsBegin;
927 llvm::APInt RadixVal(Val.getBitWidth(), radix);
928 llvm::APInt CharVal(Val.getBitWidth(), 0);
929 llvm::APInt OldVal = Val;
931 bool OverflowOccurred =
false;
932 while (Ptr < SuffixBegin) {
933 if (isDigitSeparator(*Ptr)) {
938 unsigned C = llvm::hexDigitValue(*Ptr++);
941 assert(C < radix &&
"NumericLiteralParser ctor should have rejected this");
951 OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
956 OverflowOccurred |= Val.ult(CharVal);
958 return OverflowOccurred;
961 llvm::APFloat::opStatus
965 unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
968 StringRef Str(ThisTokBegin, n);
969 if (Str.find(
'\'') != StringRef::npos) {
971 std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
976 return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
1026 const char *TokBegin = begin;
1029 if (
Kind != tok::char_constant)
1031 if (
Kind == tok::utf8_char_constant)
1035 assert(begin[0] ==
'\'' &&
"Invalid token lexed");
1039 if (end[-1] !=
'\'') {
1040 const char *UDSuffixEnd = end;
1043 }
while (end[-1] !=
'\'');
1045 expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1046 UDSuffixOffset = end - TokBegin;
1050 assert(end != begin &&
"Invalid token lexed");
1057 "Assumes char is 8 bits");
1060 "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1062 "Assumes sizeof(wchar) on target is <= 64");
1065 codepoint_buffer.resize(end - begin);
1066 uint32_t *buffer_begin = &codepoint_buffer.front();
1067 uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1072 uint32_t largest_character_for_kind;
1073 if (tok::wide_char_constant ==
Kind) {
1074 largest_character_for_kind =
1076 }
else if (tok::utf8_char_constant ==
Kind) {
1077 largest_character_for_kind = 0x7F;
1078 }
else if (tok::utf16_char_constant ==
Kind) {
1079 largest_character_for_kind = 0xFFFF;
1080 }
else if (tok::utf32_char_constant ==
Kind) {
1081 largest_character_for_kind = 0x10FFFF;
1083 largest_character_for_kind = 0x7Fu;
1086 while (begin != end) {
1088 if (begin[0] !=
'\\') {
1089 char const *start = begin;
1092 }
while (begin != end && *begin !=
'\\');
1094 char const *tmp_in_start = start;
1095 uint32_t *tmp_out_start = buffer_begin;
1096 ConversionResult res =
1097 ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
1098 reinterpret_cast<UTF8 const *>(begin),
1099 &buffer_begin, buffer_end, strictConversion);
1100 if (res != conversionOK) {
1104 bool NoErrorOnBadEncoding =
isAscii();
1105 unsigned Msg = diag::err_bad_character_encoding;
1106 if (NoErrorOnBadEncoding)
1107 Msg = diag::warn_bad_character_encoding;
1109 if (NoErrorOnBadEncoding) {
1110 start = tmp_in_start;
1111 buffer_begin = tmp_out_start;
1112 for (; start != begin; ++start, ++buffer_begin)
1113 *buffer_begin = static_cast<uint8_t>(*start);
1118 for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1119 if (*tmp_out_start > largest_character_for_kind) {
1121 PP.
Diag(Loc, diag::err_character_too_large);
1129 if (begin[1] ==
'u' || begin[1] ==
'U') {
1130 unsigned short UcnLen = 0;
1135 }
else if (*buffer_begin > largest_character_for_kind) {
1137 PP.
Diag(Loc, diag::err_character_too_large);
1148 *buffer_begin++ = result;
1151 unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1153 if (NumCharsSoFar > 1) {
1155 PP.
Diag(Loc, diag::warn_extraneous_char_constant);
1156 else if (
isAscii() && NumCharsSoFar == 4)
1157 PP.
Diag(Loc, diag::ext_four_char_character_literal);
1159 PP.
Diag(Loc, diag::ext_multichar_character_literal);
1161 PP.
Diag(Loc, diag::err_multichar_utf_character_literal);
1164 IsMultiChar =
false;
1171 bool multi_char_too_long =
false;
1174 for (
size_t i = 0; i < NumCharsSoFar; ++i) {
1176 multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1178 LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1180 }
else if (NumCharsSoFar > 0) {
1182 LitVal = buffer_begin[-1];
1185 if (!HadError && multi_char_too_long) {
1186 PP.
Diag(Loc, diag::warn_char_constant_too_large);
1190 Value = LitVal.getZExtValue();
1196 if (
isAscii() && NumCharsSoFar == 1 && (
Value & 128) &&
1258 :
SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1259 Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1260 MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown),
1261 ResultPtr(ResultBuf.data()), hadError(
false), Pascal(
false) {
1268 if (StringToks.empty() || StringToks[0].getLength() < 2)
1275 assert(!StringToks.empty() &&
"expected at least one token");
1276 MaxTokenLength = StringToks[0].getLength();
1277 assert(StringToks[0].getLength() >= 2 &&
"literal token is invalid!");
1278 SizeBound = StringToks[0].getLength()-2;
1279 Kind = StringToks[0].getKind();
1285 for (
unsigned i = 1; i != StringToks.size(); ++i) {
1286 if (StringToks[i].getLength() < 2)
1287 return DiagnoseLexingError(StringToks[i].getLocation());
1291 assert(StringToks[i].getLength() >= 2 &&
"literal token is invalid!");
1292 SizeBound += StringToks[i].getLength()-2;
1295 if (StringToks[i].getLength() > MaxTokenLength)
1296 MaxTokenLength = StringToks[i].getLength();
1300 if (StringToks[i].isNot(
Kind) && StringToks[i].isNot(tok::string_literal)) {
1302 Kind = StringToks[i].getKind();
1305 Diags->
Report(StringToks[i].getLocation(),
1306 diag::err_unsupported_string_concat);
1319 assert((CharByteWidth & 7) == 0 &&
"Assumes character size is byte multiple");
1324 SizeBound *= CharByteWidth;
1327 ResultBuf.resize(SizeBound);
1331 TokenBuf.resize(MaxTokenLength);
1335 ResultPtr = &ResultBuf[0];
1341 for (
unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1342 const char *ThisTokBuf = &TokenBuf[0];
1346 bool StringInvalid =
false;
1347 unsigned ThisTokLen =
1351 return DiagnoseLexingError(StringToks[i].getLocation());
1353 const char *ThisTokBegin = ThisTokBuf;
1354 const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1357 if (ThisTokEnd[-1] !=
'"') {
1358 const char *UDSuffixEnd = ThisTokEnd;
1361 }
while (ThisTokEnd[-1] !=
'"');
1363 StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1365 if (UDSuffixBuf.empty()) {
1366 if (StringToks[i].hasUCN())
1369 UDSuffixBuf.assign(UDSuffix);
1371 UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1372 UDSuffixTokLoc = StringToks[i].getLocation();
1375 if (StringToks[i].hasUCN()) {
1377 UDSuffix = ExpandedUDSuffix;
1384 if (UDSuffixBuf != UDSuffix) {
1387 Diags->
Report(TokLoc, diag::err_string_concat_mixed_suffix)
1388 << UDSuffixBuf << UDSuffix
1403 if (ThisTokBuf[0] ==
'L' || ThisTokBuf[0] ==
'u' || ThisTokBuf[0] ==
'U') {
1406 if (ThisTokBuf[0] ==
'8')
1411 if (ThisTokBuf[0] ==
'R') {
1414 const char *Prefix = ThisTokBuf;
1415 while (ThisTokBuf[0] !=
'(')
1420 ThisTokEnd -= ThisTokBuf - Prefix;
1421 assert(ThisTokEnd >= ThisTokBuf &&
"malformed raw string literal");
1424 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1425 StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf)))
1428 if (ThisTokBuf[0] !=
'"') {
1431 return DiagnoseLexingError(StringToks[i].getLocation());
1436 if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1437 ThisTokBuf[0] ==
'\\' && ThisTokBuf[1] ==
'p') {
1448 while (ThisTokBuf != ThisTokEnd) {
1450 if (ThisTokBuf[0] !=
'\\') {
1451 const char *InStart = ThisTokBuf;
1454 }
while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] !=
'\\');
1457 if (CopyStringFragment(StringToks[i], ThisTokBegin,
1458 StringRef(InStart, ThisTokBuf - InStart)))
1463 if (ThisTokBuf[1] ==
'u' || ThisTokBuf[1] ==
'U') {
1467 CharByteWidth, Diags, Features);
1471 unsigned ResultChar =
1474 CharByteWidth*8, Diags, Features);
1476 if (CharByteWidth == 4) {
1479 UTF32 *ResultWidePtr =
reinterpret_cast<UTF32*
>(ResultPtr);
1480 *ResultWidePtr = ResultChar;
1482 }
else if (CharByteWidth == 2) {
1485 UTF16 *ResultWidePtr =
reinterpret_cast<UTF16*
>(ResultPtr);
1486 *ResultWidePtr = ResultChar & 0xFFFF;
1489 assert(CharByteWidth == 1 &&
"Unexpected char width");
1490 *ResultPtr++ = ResultChar & 0xFF;
1497 if (CharByteWidth == 4) {
1500 UTF32 *ResultWidePtr =
reinterpret_cast<UTF32*
>(ResultBuf.data());
1502 }
else if (CharByteWidth == 2) {
1505 UTF16 *ResultWidePtr =
reinterpret_cast<UTF16*
>(ResultBuf.data());
1508 assert(CharByteWidth == 1 &&
"Unexpected char width");
1515 Diags->
Report(StringToks.front().getLocation(),
1516 diag::err_pascal_string_too_long)
1518 StringToks.back().getLocation());
1524 unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1527 Diags->
Report(StringToks.front().getLocation(),
1528 diag::ext_string_too_long)
1530 << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1532 StringToks.back().getLocation());
1539 End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err);
1540 while (++Err != End && (*Err & 0xC0) == 0x80)
1548 bool StringLiteralParser::CopyStringFragment(
const Token &Tok,
1549 const char *TokBegin,
1550 StringRef Fragment) {
1551 const UTF8 *ErrorPtrTmp;
1552 if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1558 bool NoErrorOnBadEncoding =
isAscii();
1559 if (NoErrorOnBadEncoding) {
1560 memcpy(ResultPtr, Fragment.data(), Fragment.size());
1561 ResultPtr += Fragment.size();
1565 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1569 Diag(Diags, Features, SourceLoc, TokBegin,
1570 ErrorPtr,
resyncUTF8(ErrorPtr, Fragment.end()),
1571 NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1572 : diag::err_bad_string_encoding);
1574 const char *NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1575 StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1579 Dummy.reserve(Fragment.size() * CharByteWidth);
1580 char *Ptr = Dummy.data();
1582 while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1583 const char *ErrorPtr =
reinterpret_cast<const char *
>(ErrorPtrTmp);
1584 NextStart =
resyncUTF8(ErrorPtr, Fragment.end());
1586 ErrorPtr, NextStart);
1587 NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1590 return !NoErrorOnBadEncoding;
1593 void StringLiteralParser::DiagnoseLexingError(
SourceLocation Loc) {
1596 Diags->
Report(Loc, diag::err_lexing_string);
1603 unsigned ByteNo)
const {
1608 bool StringInvalid =
false;
1609 const char *SpellingPtr = &SpellingBuffer[0];
1615 const char *SpellingStart = SpellingPtr;
1616 const char *SpellingEnd = SpellingPtr+TokLen;
1619 if (SpellingPtr[0] ==
'u' && SpellingPtr[1] ==
'8')
1622 assert(SpellingPtr[0] !=
'L' && SpellingPtr[0] !=
'u' &&
1623 SpellingPtr[0] !=
'U' &&
"Doesn't handle wide or utf strings yet");
1626 if (SpellingPtr[0] ==
'R') {
1627 assert(SpellingPtr[1] ==
'"' &&
"Should be a raw string literal!");
1630 while (*SpellingPtr !=
'(') {
1632 assert(SpellingPtr < SpellingEnd &&
"Missing ( for raw string literal");
1636 return SpellingPtr - SpellingStart + ByteNo;
1640 assert(SpellingPtr[0] ==
'"' &&
"Should be a string literal!");
1645 assert(SpellingPtr < SpellingEnd &&
"Didn't find byte offset!");
1648 if (*SpellingPtr !=
'\\') {
1655 bool HadError =
false;
1656 if (SpellingPtr[1] ==
'u' || SpellingPtr[1] ==
'U') {
1657 const char *EscapePtr = SpellingPtr;
1659 1, Features, HadError);
1662 SpellingPtr = EscapePtr;
1669 CharByteWidth*8, Diags, Features);
1672 assert(!HadError &&
"This method isn't valid on erroneous strings");
1675 return SpellingPtr-SpellingStart;
SourceManager & getSourceManager() const
static unsigned getSpelling(const Token &Tok, const char *&Buffer, const SourceManager &SourceMgr, const LangOptions &LangOpts, bool *Invalid=nullptr)
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
StringLiteralParser(ArrayRef< Token > StringToks, Preprocessor &PP, bool Complain=true)
const SourceManager & getManager() const
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getChar32Width() const
static LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits)
unsigned getOffsetOfStringByte(const Token &TheTok, unsigned ByteNo) const
const LangOptions & getLangOpts() const
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)
Concrete class used by the front-end to report problems and issues.
static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix)
NumericLiteralParser(StringRef TokSpelling, SourceLocation TokLoc, Preprocessor &PP)
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)
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
unsigned getWCharWidth() const
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)
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)
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result)
static unsigned ProcessCharEscape(const char *ThisTokBegin, const char *&ThisTokBuf, const char *ThisTokEnd, bool &HadError, FullSourceLoc Loc, unsigned CharWidth, DiagnosticsEngine *Diags, const LangOptions &Features)
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)
static LLVM_READONLY bool isPrintable(unsigned char c)
unsigned getCharWidth() const
unsigned getIntWidth() const
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)
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].