#include "clang/Lex/LiteralSupport.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/LexDiagnostic.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Unicode.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
using namespace clang;
static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
switch (kind) {
default: llvm_unreachable("Unknown token type!");
case tok::char_constant:
case tok::string_literal:
case tok::utf8_char_constant:
case tok::utf8_string_literal:
return Target.getCharWidth();
case tok::wide_char_constant:
case tok::wide_string_literal:
return Target.getWCharWidth();
case tok::utf16_char_constant:
case tok::utf16_string_literal:
return Target.getChar16Width();
case tok::utf32_char_constant:
case tok::utf32_string_literal:
return Target.getChar32Width();
}
}
static CharSourceRange MakeCharSourceRange(const LangOptions &Features,
FullSourceLoc TokLoc,
const char *TokBegin,
const char *TokRangeBegin,
const char *TokRangeEnd) {
SourceLocation Begin =
Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
TokLoc.getManager(), Features);
SourceLocation End =
Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin,
TokLoc.getManager(), Features);
return CharSourceRange::getCharRange(Begin, End);
}
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags,
const LangOptions &Features, FullSourceLoc TokLoc,
const char *TokBegin, const char *TokRangeBegin,
const char *TokRangeEnd, unsigned DiagID) {
SourceLocation Begin =
Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
TokLoc.getManager(), Features);
return Diags->Report(Begin, DiagID) <<
MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd);
}
static unsigned ProcessCharEscape(const char *ThisTokBegin,
const char *&ThisTokBuf,
const char *ThisTokEnd, bool &HadError,
FullSourceLoc Loc, unsigned CharWidth,
DiagnosticsEngine *Diags,
const LangOptions &Features) {
const char *EscapeBegin = ThisTokBuf;
bool Delimited = false;
bool EndDelimiterFound = false;
++ThisTokBuf;
unsigned ResultChar = *ThisTokBuf++;
switch (ResultChar) {
case '\\': case '\'': case '"': case '?': break;
case 'a':
ResultChar = 7;
break;
case 'b':
ResultChar = 8;
break;
case 'e':
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::ext_nonstandard_escape) << "e";
ResultChar = 27;
break;
case 'E':
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::ext_nonstandard_escape) << "E";
ResultChar = 27;
break;
case 'f':
ResultChar = 12;
break;
case 'n':
ResultChar = 10;
break;
case 'r':
ResultChar = 13;
break;
case 't':
ResultChar = 9;
break;
case 'v':
ResultChar = 11;
break;
case 'x': { ResultChar = 0;
if (ThisTokBuf != ThisTokEnd && *ThisTokBuf == '{') {
Delimited = true;
ThisTokBuf++;
if (*ThisTokBuf == '}') {
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_delimited_escape_empty);
return ResultChar;
}
} else if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_hex_escape_no_digits) << "x";
return ResultChar;
}
bool Overflow = false;
for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
if (Delimited && *ThisTokBuf == '}') {
ThisTokBuf++;
EndDelimiterFound = true;
break;
}
int CharVal = llvm::hexDigitValue(*ThisTokBuf);
if (CharVal == -1) {
if (!Delimited)
break;
HadError = true;
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_delimited_escape_invalid)
<< StringRef(ThisTokBuf, 1);
continue;
}
if (ResultChar & 0xF0000000)
Overflow = true;
ResultChar <<= 4;
ResultChar |= CharVal;
}
if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
Overflow = true;
ResultChar &= ~0U >> (32-CharWidth);
}
if (!HadError && Overflow) { HadError = true;
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_escape_too_large)
<< 0;
}
break;
}
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7': {
--ThisTokBuf;
ResultChar = 0;
unsigned NumDigits = 0;
do {
ResultChar <<= 3;
ResultChar |= *ThisTokBuf++ - '0';
++NumDigits;
} while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_escape_too_large) << 1;
ResultChar &= ~0U >> (32-CharWidth);
}
break;
}
case 'o': {
bool Overflow = false;
if (ThisTokBuf == ThisTokEnd || *ThisTokBuf != '{') {
HadError = true;
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_delimited_escape_missing_brace)
<< "o";
break;
}
ResultChar = 0;
Delimited = true;
++ThisTokBuf;
if (*ThisTokBuf == '}') {
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_delimited_escape_empty);
return ResultChar;
}
while (ThisTokBuf != ThisTokEnd) {
if (*ThisTokBuf == '}') {
EndDelimiterFound = true;
ThisTokBuf++;
break;
}
if (*ThisTokBuf < '0' || *ThisTokBuf > '7') {
HadError = true;
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_delimited_escape_invalid)
<< StringRef(ThisTokBuf, 1);
ThisTokBuf++;
continue;
}
if (ResultChar & 0x020000000)
Overflow = true;
ResultChar <<= 3;
ResultChar |= *ThisTokBuf++ - '0';
}
if (!HadError &&
(Overflow || (CharWidth != 32 && (ResultChar >> CharWidth) != 0))) {
HadError = true;
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_escape_too_large)
<< 1;
ResultChar &= ~0U >> (32 - CharWidth);
}
break;
}
case '(': case '{': case '[': case '%':
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::ext_nonstandard_escape)
<< std::string(1, ResultChar);
break;
default:
if (!Diags)
break;
if (isPrintable(ResultChar))
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::ext_unknown_escape)
<< std::string(1, ResultChar);
else
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::ext_unknown_escape)
<< "x" + llvm::utohexstr(ResultChar);
break;
}
if (Delimited && Diags) {
if (!EndDelimiterFound)
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
diag::err_expected)
<< tok::r_brace;
else if (!HadError) {
Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
Features.CPlusPlus2b ? diag::warn_cxx2b_delimited_escape_sequence
: diag::ext_delimited_escape_sequence)
<< 0 << (Features.CPlusPlus ? 1 : 0);
}
}
return ResultChar;
}
static void appendCodePoint(unsigned Codepoint,
llvm::SmallVectorImpl<char> &Str) {
char ResultBuf[4];
char *ResultPtr = ResultBuf;
if (llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr))
Str.append(ResultBuf, ResultPtr);
}
void clang::expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) {
for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
if (*I != '\\') {
Buf.push_back(*I);
continue;
}
++I;
char Kind = *I;
++I;
assert(Kind == 'u' || Kind == 'U' || Kind == 'N');
uint32_t CodePoint = 0;
if (Kind == 'u' && *I == '{') {
for (++I; *I != '}'; ++I) {
unsigned Value = llvm::hexDigitValue(*I);
assert(Value != -1U);
CodePoint <<= 4;
CodePoint += Value;
}
appendCodePoint(CodePoint, Buf);
continue;
}
if (Kind == 'N') {
assert(*I == '{');
++I;
auto Delim = std::find(I, Input.end(), '}');
assert(Delim != Input.end());
llvm::Optional<llvm::sys::unicode::LooseMatchingResult> Res =
llvm::sys::unicode::nameToCodepointLooseMatching(
StringRef(I, std::distance(I, Delim)));
assert(Res);
CodePoint = Res->CodePoint;
assert(CodePoint != 0xFFFFFFFF);
appendCodePoint(CodePoint, Buf);
I = Delim;
continue;
}
unsigned NumHexDigits;
if (Kind == 'u')
NumHexDigits = 4;
else
NumHexDigits = 8;
assert(I + NumHexDigits <= E);
for (; NumHexDigits != 0; ++I, --NumHexDigits) {
unsigned Value = llvm::hexDigitValue(*I);
assert(Value != -1U);
CodePoint <<= 4;
CodePoint += Value;
}
appendCodePoint(CodePoint, Buf);
--I;
}
}
static bool ProcessNumericUCNEscape(const char *ThisTokBegin,
const char *&ThisTokBuf,
const char *ThisTokEnd, uint32_t &UcnVal,
unsigned short &UcnLen, bool &Delimited,
FullSourceLoc Loc, DiagnosticsEngine *Diags,
const LangOptions &Features,
bool in_char_string_literal = false) {
const char *UcnBegin = ThisTokBuf;
bool HasError = false;
bool EndDelimiterFound = false;
ThisTokBuf += 2;
Delimited = false;
if (UcnBegin[1] == 'u' && in_char_string_literal &&
ThisTokBuf != ThisTokEnd && *ThisTokBuf == '{') {
Delimited = true;
ThisTokBuf++;
} else if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::err_hex_escape_no_digits)
<< StringRef(&ThisTokBuf[-1], 1);
return false;
}
UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
bool Overflow = false;
unsigned short Count = 0;
for (; ThisTokBuf != ThisTokEnd && (Delimited || Count != UcnLen);
++ThisTokBuf) {
if (Delimited && *ThisTokBuf == '}') {
++ThisTokBuf;
EndDelimiterFound = true;
break;
}
int CharVal = llvm::hexDigitValue(*ThisTokBuf);
if (CharVal == -1) {
HasError = true;
if (!Delimited)
break;
if (Diags) {
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::err_delimited_escape_invalid)
<< StringRef(ThisTokBuf, 1);
}
Count++;
continue;
}
if (UcnVal & 0xF0000000) {
Overflow = true;
continue;
}
UcnVal <<= 4;
UcnVal |= CharVal;
Count++;
}
if (Overflow) {
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::err_escape_too_large)
<< 0;
return false;
}
if (Delimited && !EndDelimiterFound) {
if (Diags) {
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::err_expected)
<< tok::r_brace;
}
return false;
}
if (Count == 0 || (!Delimited && Count != UcnLen)) {
if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
Delimited ? diag::err_delimited_escape_empty
: diag::err_ucn_escape_incomplete);
return false;
}
return !HasError;
}
static void DiagnoseInvalidUnicodeCharacterName(
DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc Loc,
const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd,
llvm::StringRef Name) {
Diag(Diags, Features, Loc, TokBegin, TokRangeBegin, TokRangeEnd,
diag::err_invalid_ucn_name)
<< Name;
namespace u = llvm::sys::unicode;
llvm::Optional<u::LooseMatchingResult> Res =
u::nameToCodepointLooseMatching(Name);
if (Res) {
Diag(Diags, Features, Loc, TokBegin, TokRangeBegin, TokRangeEnd,
diag::note_invalid_ucn_name_loose_matching)
<< FixItHint::CreateReplacement(
MakeCharSourceRange(Features, Loc, TokBegin, TokRangeBegin,
TokRangeEnd),
Res->Name);
return;
}
unsigned Distance = 0;
SmallVector<u::MatchForCodepointName> Matches =
u::nearestMatchesForCodepointName(Name, 5);
assert(!Matches.empty() && "No unicode characters found");
for (const auto &Match : Matches) {
if (Distance == 0)
Distance = Match.Distance;
if (std::max(Distance, Match.Distance) -
std::min(Distance, Match.Distance) >
3)
break;
Distance = Match.Distance;
std::string Str;
llvm::UTF32 V = Match.Value;
LLVM_ATTRIBUTE_UNUSED bool Converted =
llvm::convertUTF32ToUTF8String(llvm::ArrayRef<llvm::UTF32>(&V, 1), Str);
assert(Converted && "Found a match wich is not a unicode character");
Diag(Diags, Features, Loc, TokBegin, TokRangeBegin, TokRangeEnd,
diag::note_invalid_ucn_name_candidate)
<< Match.Name << llvm::utohexstr(Match.Value)
<< Str << FixItHint::CreateReplacement(
MakeCharSourceRange(Features, Loc, TokBegin, TokRangeBegin,
TokRangeEnd),
Match.Name);
}
}
static bool ProcessNamedUCNEscape(const char *ThisTokBegin,
const char *&ThisTokBuf,
const char *ThisTokEnd, uint32_t &UcnVal,
unsigned short &UcnLen, FullSourceLoc Loc,
DiagnosticsEngine *Diags,
const LangOptions &Features) {
const char *UcnBegin = ThisTokBuf;
assert(UcnBegin[0] == '\\' && UcnBegin[1] == 'N');
ThisTokBuf += 2;
if (ThisTokBuf == ThisTokEnd || *ThisTokBuf != '{') {
if (Diags) {
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::err_delimited_escape_missing_brace)
<< StringRef(&ThisTokBuf[-1], 1);
}
ThisTokBuf++;
return false;
}
ThisTokBuf++;
const char *ClosingBrace =
std::find_if_not(ThisTokBuf, ThisTokEnd, [](char C) {
return llvm::isAlnum(C) || llvm::isSpace(C) || C == '_' || C == '-';
});
bool Incomplete = ClosingBrace == ThisTokEnd || *ClosingBrace != '}';
bool Empty = ClosingBrace == ThisTokBuf;
if (Incomplete || Empty) {
if (Diags) {
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
Incomplete ? diag::err_ucn_escape_incomplete
: diag::err_delimited_escape_empty)
<< StringRef(&UcnBegin[1], 1);
}
ThisTokBuf = ClosingBrace == ThisTokEnd ? ClosingBrace : ClosingBrace + 1;
return false;
}
StringRef Name(ThisTokBuf, ClosingBrace - ThisTokBuf);
ThisTokBuf = ClosingBrace + 1;
llvm::Optional<char32_t> Res =
llvm::sys::unicode::nameToCodepointStrict(Name);
if (!Res) {
if (Diags)
DiagnoseInvalidUnicodeCharacterName(Diags, Features, Loc, ThisTokBegin,
&UcnBegin[3], ClosingBrace, Name);
return false;
}
UcnVal = *Res;
UcnLen = UcnVal > 0xFFFF ? 8 : 4;
return true;
}
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) {
bool HasError;
const char *UcnBegin = ThisTokBuf;
bool IsDelimitedEscapeSequence = false;
bool IsNamedEscapeSequence = false;
if (ThisTokBuf[1] == 'N') {
IsNamedEscapeSequence = true;
HasError = !ProcessNamedUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
UcnVal, UcnLen, Loc, Diags, Features);
} else {
HasError =
!ProcessNumericUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
UcnLen, IsDelimitedEscapeSequence, Loc, Diags,
Features, in_char_string_literal);
}
if (HasError)
return false;
if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || UcnVal > 0x10FFFF) { if (Diags)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::err_ucn_escape_invalid);
return false;
}
if (UcnVal < 0xa0 &&
(UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
if (Diags) {
char BasicSCSChar = UcnVal;
if (UcnVal >= 0x20 && UcnVal < 0x7f)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
IsError ? diag::err_ucn_escape_basic_scs :
diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
<< StringRef(&BasicSCSChar, 1);
else
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
IsError ? diag::err_ucn_control_character :
diag::warn_cxx98_compat_literal_ucn_control_character);
}
if (IsError)
return false;
}
if (!Features.CPlusPlus && !Features.C99 && Diags)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
diag::warn_ucn_not_valid_in_c89_literal);
if ((IsDelimitedEscapeSequence || IsNamedEscapeSequence) && Diags)
Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
Features.CPlusPlus2b ? diag::warn_cxx2b_delimited_escape_sequence
: diag::ext_delimited_escape_sequence)
<< (IsNamedEscapeSequence ? 1 : 0) << (Features.CPlusPlus ? 1 : 0);
return true;
}
static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
const char *ThisTokEnd, unsigned CharByteWidth,
const LangOptions &Features, bool &HadError) {
if (CharByteWidth == 4)
return 4;
uint32_t UcnVal = 0;
unsigned short UcnLen = 0;
FullSourceLoc Loc;
if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
UcnLen, Loc, nullptr, Features, true)) {
HadError = true;
return 0;
}
if (CharByteWidth == 2)
return UcnVal <= 0xFFFF ? 2 : 4;
if (UcnVal < 0x80)
return 1;
if (UcnVal < 0x800)
return 2;
if (UcnVal < 0x10000)
return 3;
return 4;
}
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) {
typedef uint32_t UTF32;
UTF32 UcnVal = 0;
unsigned short UcnLen = 0;
if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
Loc, Diags, Features, true)) {
HadError = true;
return;
}
assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
"only character widths of 1, 2, or 4 bytes supported");
(void)UcnLen;
assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
if (CharByteWidth == 4) {
llvm::UTF32 *ResultPtr = reinterpret_cast<llvm::UTF32*>(ResultBuf);
*ResultPtr = UcnVal;
ResultBuf += 4;
return;
}
if (CharByteWidth == 2) {
llvm::UTF16 *ResultPtr = reinterpret_cast<llvm::UTF16*>(ResultBuf);
if (UcnVal <= (UTF32)0xFFFF) {
*ResultPtr = UcnVal;
ResultBuf += 2;
return;
}
UcnVal -= 0x10000;
*ResultPtr = 0xD800 + (UcnVal >> 10);
*(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
ResultBuf += 4;
return;
}
assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
typedef uint8_t UTF8;
unsigned short bytesToWrite = 0;
if (UcnVal < (UTF32)0x80)
bytesToWrite = 1;
else if (UcnVal < (UTF32)0x800)
bytesToWrite = 2;
else if (UcnVal < (UTF32)0x10000)
bytesToWrite = 3;
else
bytesToWrite = 4;
const unsigned byteMask = 0xBF;
const unsigned byteMark = 0x80;
static const UTF8 firstByteMark[5] = {
0x00, 0x00, 0xC0, 0xE0, 0xF0
};
ResultBuf += bytesToWrite;
switch (bytesToWrite) { case 4:
*--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
LLVM_FALLTHROUGH;
case 3:
*--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
LLVM_FALLTHROUGH;
case 2:
*--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
LLVM_FALLTHROUGH;
case 1:
*--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
}
ResultBuf += bytesToWrite;
}
NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
SourceLocation TokLoc,
const SourceManager &SM,
const LangOptions &LangOpts,
const TargetInfo &Target,
DiagnosticsEngine &Diags)
: SM(SM), LangOpts(LangOpts), Diags(Diags),
ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
s = DigitsBegin = ThisTokBegin;
saw_exponent = false;
saw_period = false;
saw_ud_suffix = false;
saw_fixed_point_suffix = false;
isLong = false;
isUnsigned = false;
isLongLong = false;
isSizeT = false;
isHalf = false;
isFloat = false;
isImaginary = false;
isFloat16 = false;
isFloat128 = false;
MicrosoftInteger = 0;
isFract = false;
isAccum = false;
hadError = false;
isBitInt = false;
if (isPreprocessingNumberBody(*ThisTokEnd)) {
Diags.Report(TokLoc, diag::err_lexing_numeric);
hadError = true;
return;
}
if (*s == '0') { ParseNumberStartingWithZero(TokLoc);
if (hadError)
return;
} else { radix = 10;
s = SkipDigits(s);
if (s == ThisTokEnd) {
} else {
ParseDecimalOrOctalCommon(TokLoc);
if (hadError)
return;
}
}
SuffixBegin = s;
checkSeparator(TokLoc, s, CSK_AfterDigits);
if (LangOpts.FixedPoint) {
for (const char *c = s; c != ThisTokEnd; ++c) {
if (*c == 'r' || *c == 'k' || *c == 'R' || *c == 'K') {
saw_fixed_point_suffix = true;
break;
}
}
}
bool isFixedPointConstant = isFixedPointLiteral();
bool isFPConstant = isFloatingLiteral();
bool HasSize = false;
for (; s != ThisTokEnd; ++s) {
switch (*s) {
case 'R':
case 'r':
if (!LangOpts.FixedPoint)
break;
if (isFract || isAccum) break;
if (!(saw_period || saw_exponent)) break;
isFract = true;
continue;
case 'K':
case 'k':
if (!LangOpts.FixedPoint)
break;
if (isFract || isAccum) break;
if (!(saw_period || saw_exponent)) break;
isAccum = true;
continue;
case 'h': case 'H':
if (!(LangOpts.Half || LangOpts.FixedPoint))
break;
if (isIntegerLiteral()) break; if (HasSize)
break;
HasSize = true;
isHalf = true;
continue; case 'f': case 'F':
if (!isFPConstant) break; if (HasSize)
break;
HasSize = true;
if ((Target.hasFloat16Type() || LangOpts.CUDA) && s + 2 < ThisTokEnd &&
s[1] == '1' && s[2] == '6') {
s += 2; isFloat16 = true;
continue;
}
isFloat = true;
continue; case 'q': case 'Q':
if (!isFPConstant) break; if (HasSize)
break;
HasSize = true;
isFloat128 = true;
continue; case 'u':
case 'U':
if (isFPConstant) break; if (isUnsigned) break; isUnsigned = true;
continue; case 'l':
case 'L':
if (HasSize)
break;
HasSize = true;
if (s[1] == s[0]) {
assert(s + 1 < ThisTokEnd && "didn't maximally munch?");
if (isFPConstant) break; isLongLong = true;
++s; } else {
isLong = true;
}
continue; case 'z':
case 'Z':
if (isFPConstant)
break; if (HasSize)
break;
HasSize = true;
isSizeT = true;
continue;
case 'i':
case 'I':
if (LangOpts.MicrosoftExt && !isFPConstant) {
uint8_t Bits = 0;
size_t ToSkip = 0;
switch (s[1]) {
case '8': Bits = 8;
ToSkip = 2;
break;
case '1':
if (s[2] == '6') { Bits = 16;
ToSkip = 3;
}
break;
case '3':
if (s[2] == '2') { Bits = 32;
ToSkip = 3;
}
break;
case '6':
if (s[2] == '4') { Bits = 64;
ToSkip = 3;
}
break;
default:
break;
}
if (Bits) {
if (HasSize)
break;
HasSize = true;
MicrosoftInteger = Bits;
s += ToSkip;
assert(s <= ThisTokEnd && "didn't maximally munch?");
break;
}
}
LLVM_FALLTHROUGH;
case 'j':
case 'J':
if (isImaginary) break; isImaginary = true;
continue; case 'w':
case 'W':
if (isFPConstant)
break; if (HasSize)
break;
if (!LangOpts.CPlusPlus && ((s[0] == 'w' && s[1] == 'b') ||
(s[0] == 'W' && s[1] == 'B'))) {
isBitInt = true;
HasSize = true;
++s; continue; }
}
break;
}
if (s != ThisTokEnd || isImaginary) {
expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
if (isValidUDSuffix(LangOpts, UDSuffixBuf)) {
if (!isImaginary) {
isLong = false;
isUnsigned = false;
isLongLong = false;
isSizeT = false;
isFloat = false;
isFloat16 = false;
isHalf = false;
isImaginary = false;
isBitInt = false;
MicrosoftInteger = 0;
saw_fixed_point_suffix = false;
isFract = false;
isAccum = false;
}
saw_ud_suffix = true;
return;
}
if (s != ThisTokEnd) {
Diags.Report(Lexer::AdvanceToTokenCharacter(
TokLoc, SuffixBegin - ThisTokBegin, SM, LangOpts),
diag::err_invalid_suffix_constant)
<< StringRef(SuffixBegin, ThisTokEnd - SuffixBegin)
<< (isFixedPointConstant ? 2 : isFPConstant);
hadError = true;
}
}
if (!hadError && saw_fixed_point_suffix) {
assert(isFract || isAccum);
}
}
void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc){
assert((radix == 8 || radix == 10) && "Unexpected radix");
if (isHexDigit(*s) && *s != 'e' && *s != 'E' &&
!isValidUDSuffix(LangOpts, StringRef(s, ThisTokEnd - s))) {
Diags.Report(
Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM, LangOpts),
diag::err_invalid_digit)
<< StringRef(s, 1) << (radix == 8 ? 1 : 0);
hadError = true;
return;
}
if (*s == '.') {
checkSeparator(TokLoc, s, CSK_AfterDigits);
s++;
radix = 10;
saw_period = true;
checkSeparator(TokLoc, s, CSK_BeforeDigits);
s = SkipDigits(s); }
if (*s == 'e' || *s == 'E') { checkSeparator(TokLoc, s, CSK_AfterDigits);
const char *Exponent = s;
s++;
radix = 10;
saw_exponent = true;
if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; const char *first_non_digit = SkipDigits(s);
if (containsDigits(s, first_non_digit)) {
checkSeparator(TokLoc, s, CSK_BeforeDigits);
s = first_non_digit;
} else {
if (!hadError) {
Diags.Report(Lexer::AdvanceToTokenCharacter(
TokLoc, Exponent - ThisTokBegin, SM, LangOpts),
diag::err_exponent_has_no_digits);
hadError = true;
}
return;
}
}
}
bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts,
StringRef Suffix) {
if (!LangOpts.CPlusPlus11 || Suffix.empty())
return false;
if (Suffix[0] == '_')
return true;
if (!LangOpts.CPlusPlus14)
return false;
return llvm::StringSwitch<bool>(Suffix)
.Cases("h", "min", "s", true)
.Cases("ms", "us", "ns", true)
.Cases("il", "i", "if", true)
.Cases("d", "y", LangOpts.CPlusPlus20)
.Default(false);
}
void NumericLiteralParser::checkSeparator(SourceLocation TokLoc,
const char *Pos,
CheckSeparatorKind IsAfterDigits) {
if (IsAfterDigits == CSK_AfterDigits) {
if (Pos == ThisTokBegin)
return;
--Pos;
} else if (Pos == ThisTokEnd)
return;
if (isDigitSeparator(*Pos)) {
Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, Pos - ThisTokBegin, SM,
LangOpts),
diag::err_digit_separator_not_between_digits)
<< IsAfterDigits;
hadError = true;
}
}
void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
assert(s[0] == '0' && "Invalid method call");
s++;
int c1 = s[0];
if ((c1 == 'x' || c1 == 'X') && (isHexDigit(s[1]) || s[1] == '.')) {
s++;
assert(s < ThisTokEnd && "didn't maximally munch?");
radix = 16;
DigitsBegin = s;
s = SkipHexDigits(s);
bool HasSignificandDigits = containsDigits(DigitsBegin, s);
if (s == ThisTokEnd) {
} else if (*s == '.') {
s++;
saw_period = true;
const char *floatDigitsBegin = s;
s = SkipHexDigits(s);
if (containsDigits(floatDigitsBegin, s))
HasSignificandDigits = true;
if (HasSignificandDigits)
checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits);
}
if (!HasSignificandDigits) {
Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM,
LangOpts),
diag::err_hex_constant_requires)
<< LangOpts.CPlusPlus << 1;
hadError = true;
return;
}
if (*s == 'p' || *s == 'P') {
checkSeparator(TokLoc, s, CSK_AfterDigits);
const char *Exponent = s;
s++;
saw_exponent = true;
if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; const char *first_non_digit = SkipDigits(s);
if (!containsDigits(s, first_non_digit)) {
if (!hadError) {
Diags.Report(Lexer::AdvanceToTokenCharacter(
TokLoc, Exponent - ThisTokBegin, SM, LangOpts),
diag::err_exponent_has_no_digits);
hadError = true;
}
return;
}
checkSeparator(TokLoc, s, CSK_BeforeDigits);
s = first_non_digit;
if (!LangOpts.HexFloats)
Diags.Report(TokLoc, LangOpts.CPlusPlus
? diag::ext_hex_literal_invalid
: diag::ext_hex_constant_invalid);
else if (LangOpts.CPlusPlus17)
Diags.Report(TokLoc, diag::warn_cxx17_hex_literal);
} else if (saw_period) {
Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM,
LangOpts),
diag::err_hex_constant_requires)
<< LangOpts.CPlusPlus << 0;
hadError = true;
}
return;
}
if ((c1 == 'b' || c1 == 'B') && (s[1] == '0' || s[1] == '1')) {
Diags.Report(TokLoc, LangOpts.CPlusPlus14
? diag::warn_cxx11_compat_binary_literal
: LangOpts.CPlusPlus ? diag::ext_binary_literal_cxx14
: diag::ext_binary_literal);
++s;
assert(s < ThisTokEnd && "didn't maximally munch?");
radix = 2;
DigitsBegin = s;
s = SkipBinaryDigits(s);
if (s == ThisTokEnd) {
} else if (isHexDigit(*s) &&
!isValidUDSuffix(LangOpts, StringRef(s, ThisTokEnd - s))) {
Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM,
LangOpts),
diag::err_invalid_digit)
<< StringRef(s, 1) << 2;
hadError = true;
}
return;
}
radix = 8;
const char *PossibleNewDigitStart = s;
s = SkipOctalDigits(s);
if (s != PossibleNewDigitStart)
DigitsBegin = PossibleNewDigitStart;
if (s == ThisTokEnd)
return;
if (isDigit(*s)) {
const char *EndDecimal = SkipDigits(s);
if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
s = EndDecimal;
radix = 10;
}
}
ParseDecimalOrOctalCommon(TokLoc);
}
static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) {
switch (Radix) {
case 2:
return NumDigits <= 64;
case 8:
return NumDigits <= 64 / 3; case 10:
return NumDigits <= 19; case 16:
return NumDigits <= 64 / 4; default:
llvm_unreachable("impossible Radix");
}
}
bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
const unsigned NumDigits = SuffixBegin - DigitsBegin;
if (alwaysFitsInto64Bits(radix, NumDigits)) {
uint64_t N = 0;
for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
if (!isDigitSeparator(*Ptr))
N = N * radix + llvm::hexDigitValue(*Ptr);
Val = N;
return Val.getZExtValue() != N;
}
Val = 0;
const char *Ptr = DigitsBegin;
llvm::APInt RadixVal(Val.getBitWidth(), radix);
llvm::APInt CharVal(Val.getBitWidth(), 0);
llvm::APInt OldVal = Val;
bool OverflowOccurred = false;
while (Ptr < SuffixBegin) {
if (isDigitSeparator(*Ptr)) {
++Ptr;
continue;
}
unsigned C = llvm::hexDigitValue(*Ptr++);
assert(C < radix && "NumericLiteralParser ctor should have rejected this");
CharVal = C;
OldVal = Val;
Val *= RadixVal;
OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
Val += CharVal;
OverflowOccurred |= Val.ult(CharVal);
}
return OverflowOccurred;
}
llvm::APFloat::opStatus
NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
using llvm::APFloat;
unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
llvm::SmallString<16> Buffer;
StringRef Str(ThisTokBegin, n);
if (Str.contains('\'')) {
Buffer.reserve(n);
std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
&isDigitSeparator);
Str = Buffer;
}
auto StatusOrErr =
Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
assert(StatusOrErr && "Invalid floating point representation");
return !errorToBool(StatusOrErr.takeError()) ? *StatusOrErr
: APFloat::opInvalidOp;
}
static inline bool IsExponentPart(char c) {
return c == 'p' || c == 'P' || c == 'e' || c == 'E';
}
bool NumericLiteralParser::GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale) {
assert(radix == 16 || radix == 10);
unsigned NumDigits = SuffixBegin - DigitsBegin;
if (saw_period) --NumDigits;
bool ExpOverflowOccurred = false;
bool NegativeExponent = false;
const char *ExponentBegin;
uint64_t Exponent = 0;
int64_t BaseShift = 0;
if (saw_exponent) {
const char *Ptr = DigitsBegin;
while (!IsExponentPart(*Ptr)) ++Ptr;
ExponentBegin = Ptr;
++Ptr;
NegativeExponent = *Ptr == '-';
if (NegativeExponent) ++Ptr;
unsigned NumExpDigits = SuffixBegin - Ptr;
if (alwaysFitsInto64Bits(radix, NumExpDigits)) {
llvm::StringRef ExpStr(Ptr, NumExpDigits);
llvm::APInt ExpInt(64, ExpStr, 10);
Exponent = ExpInt.getZExtValue();
} else {
ExpOverflowOccurred = true;
}
if (NegativeExponent) BaseShift -= Exponent;
else BaseShift += Exponent;
}
uint64_t NumBitsNeeded;
if (radix == 10)
NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale;
else
NumBitsNeeded = 4 * NumDigits + Exponent + Scale;
if (NumBitsNeeded > std::numeric_limits<unsigned>::max())
ExpOverflowOccurred = true;
llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0, false);
bool FoundDecimal = false;
int64_t FractBaseShift = 0;
const char *End = saw_exponent ? ExponentBegin : SuffixBegin;
for (const char *Ptr = DigitsBegin; Ptr < End; ++Ptr) {
if (*Ptr == '.') {
FoundDecimal = true;
continue;
}
unsigned C = llvm::hexDigitValue(*Ptr);
assert(C < radix && "NumericLiteralParser ctor should have rejected this");
Val *= radix;
Val += C;
if (FoundDecimal)
--FractBaseShift;
}
if (radix == 16) FractBaseShift *= 4;
BaseShift += FractBaseShift;
Val <<= Scale;
uint64_t Base = (radix == 16) ? 2 : 10;
if (BaseShift > 0) {
for (int64_t i = 0; i < BaseShift; ++i) {
Val *= Base;
}
} else if (BaseShift < 0) {
for (int64_t i = BaseShift; i < 0 && !Val.isZero(); ++i)
Val = Val.udiv(Base);
}
bool IntOverflowOccurred = false;
auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth());
if (Val.getBitWidth() > StoreVal.getBitWidth()) {
IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth()));
StoreVal = Val.trunc(StoreVal.getBitWidth());
} else if (Val.getBitWidth() < StoreVal.getBitWidth()) {
IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal);
StoreVal = Val.zext(StoreVal.getBitWidth());
} else {
StoreVal = Val;
}
return IntOverflowOccurred || ExpOverflowOccurred;
}
CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
SourceLocation Loc, Preprocessor &PP,
tok::TokenKind kind) {
HadError = false;
Kind = kind;
const char *TokBegin = begin;
if (Kind != tok::char_constant)
++begin;
if (Kind == tok::utf8_char_constant)
++begin;
if (begin[0] != '\'') {
PP.Diag(Loc, diag::err_lexing_char);
HadError = true;
return;
}
++begin;
if (end[-1] != '\'') {
const char *UDSuffixEnd = end;
do {
--end;
} while (end[-1] != '\'');
expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
UDSuffixOffset = end - TokBegin;
}
assert(end != begin && "Invalid token lexed");
--end;
assert(PP.getTargetInfo().getCharWidth() == 8 &&
"Assumes char is 8 bits");
assert(PP.getTargetInfo().getIntWidth() <= 64 &&
(PP.getTargetInfo().getIntWidth() & 7) == 0 &&
"Assumes sizeof(int) on target is <= 64 and a multiple of char");
assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
"Assumes sizeof(wchar) on target is <= 64");
SmallVector<uint32_t, 4> codepoint_buffer;
codepoint_buffer.resize(end - begin);
uint32_t *buffer_begin = &codepoint_buffer.front();
uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
uint32_t largest_character_for_kind;
if (tok::wide_char_constant == Kind) {
largest_character_for_kind =
0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
} else if (tok::utf8_char_constant == Kind) {
largest_character_for_kind = 0x7F;
} else if (tok::utf16_char_constant == Kind) {
largest_character_for_kind = 0xFFFF;
} else if (tok::utf32_char_constant == Kind) {
largest_character_for_kind = 0x10FFFF;
} else {
largest_character_for_kind = 0x7Fu;
}
while (begin != end) {
if (begin[0] != '\\') {
char const *start = begin;
do {
++begin;
} while (begin != end && *begin != '\\');
char const *tmp_in_start = start;
uint32_t *tmp_out_start = buffer_begin;
llvm::ConversionResult res =
llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start),
reinterpret_cast<llvm::UTF8 const *>(begin),
&buffer_begin, buffer_end, llvm::strictConversion);
if (res != llvm::conversionOK) {
bool NoErrorOnBadEncoding = isOrdinary();
unsigned Msg = diag::err_bad_character_encoding;
if (NoErrorOnBadEncoding)
Msg = diag::warn_bad_character_encoding;
PP.Diag(Loc, Msg);
if (NoErrorOnBadEncoding) {
start = tmp_in_start;
buffer_begin = tmp_out_start;
for (; start != begin; ++start, ++buffer_begin)
*buffer_begin = static_cast<uint8_t>(*start);
} else {
HadError = true;
}
} else {
for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
if (*tmp_out_start > largest_character_for_kind) {
HadError = true;
PP.Diag(Loc, diag::err_character_too_large);
}
}
}
continue;
}
if (begin[1] == 'u' || begin[1] == 'U' || begin[1] == 'N') {
unsigned short UcnLen = 0;
if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen,
FullSourceLoc(Loc, PP.getSourceManager()),
&PP.getDiagnostics(), PP.getLangOpts(), true)) {
HadError = true;
} else if (*buffer_begin > largest_character_for_kind) {
HadError = true;
PP.Diag(Loc, diag::err_character_too_large);
}
++buffer_begin;
continue;
}
unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
uint64_t result =
ProcessCharEscape(TokBegin, begin, end, HadError,
FullSourceLoc(Loc,PP.getSourceManager()),
CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
*buffer_begin++ = result;
}
unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
if (NumCharsSoFar > 1) {
if (isOrdinary() && NumCharsSoFar == 4)
PP.Diag(Loc, diag::warn_four_char_character_literal);
else if (isOrdinary())
PP.Diag(Loc, diag::warn_multichar_character_literal);
else {
PP.Diag(Loc, diag::err_multichar_character_literal) << (isWide() ? 0 : 1);
HadError = true;
}
IsMultiChar = true;
} else {
IsMultiChar = false;
}
llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
bool multi_char_too_long = false;
if (isOrdinary() && isMultiChar()) {
LitVal = 0;
for (size_t i = 0; i < NumCharsSoFar; ++i) {
multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
LitVal <<= 8;
LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
}
} else if (NumCharsSoFar > 0) {
LitVal = buffer_begin[-1];
}
if (!HadError && multi_char_too_long) {
PP.Diag(Loc, diag::warn_char_constant_too_large);
}
Value = LitVal.getZExtValue();
if (isOrdinary() && NumCharsSoFar == 1 && (Value & 128) &&
PP.getLangOpts().CharIsSigned)
Value = (signed char)Value;
}
StringLiteralParser::
StringLiteralParser(ArrayRef<Token> StringToks,
Preprocessor &PP)
: SM(PP.getSourceManager()), Features(PP.getLangOpts()),
Target(PP.getTargetInfo()), Diags(&PP.getDiagnostics()),
MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
init(StringToks);
}
void StringLiteralParser::init(ArrayRef<Token> StringToks){
if (StringToks.empty() || StringToks[0].getLength() < 2)
return DiagnoseLexingError(SourceLocation());
assert(!StringToks.empty() && "expected at least one token");
MaxTokenLength = StringToks[0].getLength();
assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
SizeBound = StringToks[0].getLength()-2; Kind = StringToks[0].getKind();
hadError = false;
for (unsigned i = 1; i != StringToks.size(); ++i) {
if (StringToks[i].getLength() < 2)
return DiagnoseLexingError(StringToks[i].getLocation());
assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
SizeBound += StringToks[i].getLength()-2;
if (StringToks[i].getLength() > MaxTokenLength)
MaxTokenLength = StringToks[i].getLength();
if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
if (isOrdinary()) {
Kind = StringToks[i].getKind();
} else {
if (Diags)
Diags->Report(StringToks[i].getLocation(),
diag::err_unsupported_string_concat);
hadError = true;
}
}
}
++SizeBound;
CharByteWidth = getCharWidth(Kind, Target);
assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
CharByteWidth /= 8;
SizeBound *= CharByteWidth;
ResultBuf.resize(SizeBound);
SmallString<512> TokenBuf;
TokenBuf.resize(MaxTokenLength);
ResultPtr = &ResultBuf[0];
Pascal = false;
SourceLocation UDSuffixTokLoc;
for (unsigned i = 0, e = StringToks.size(); i != e; ++i) {
const char *ThisTokBuf = &TokenBuf[0];
bool StringInvalid = false;
unsigned ThisTokLen =
Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
&StringInvalid);
if (StringInvalid)
return DiagnoseLexingError(StringToks[i].getLocation());
const char *ThisTokBegin = ThisTokBuf;
const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
if (ThisTokEnd[-1] != '"') {
const char *UDSuffixEnd = ThisTokEnd;
do {
--ThisTokEnd;
} while (ThisTokEnd[-1] != '"');
StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
if (UDSuffixBuf.empty()) {
if (StringToks[i].hasUCN())
expandUCNs(UDSuffixBuf, UDSuffix);
else
UDSuffixBuf.assign(UDSuffix);
UDSuffixToken = i;
UDSuffixOffset = ThisTokEnd - ThisTokBuf;
UDSuffixTokLoc = StringToks[i].getLocation();
} else {
SmallString<32> ExpandedUDSuffix;
if (StringToks[i].hasUCN()) {
expandUCNs(ExpandedUDSuffix, UDSuffix);
UDSuffix = ExpandedUDSuffix;
}
if (UDSuffixBuf != UDSuffix) {
if (Diags) {
SourceLocation TokLoc = StringToks[i].getLocation();
Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix)
<< UDSuffixBuf << UDSuffix
<< SourceRange(UDSuffixTokLoc, UDSuffixTokLoc)
<< SourceRange(TokLoc, TokLoc);
}
hadError = true;
}
}
}
--ThisTokEnd;
if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
++ThisTokBuf;
if (ThisTokBuf[0] == '8')
++ThisTokBuf;
}
if (ThisTokBuf[0] == 'R') {
if (ThisTokBuf[1] != '"') {
return DiagnoseLexingError(StringToks[i].getLocation());
}
ThisTokBuf += 2;
constexpr unsigned MaxRawStrDelimLen = 16;
const char *Prefix = ThisTokBuf;
while (static_cast<unsigned>(ThisTokBuf - Prefix) < MaxRawStrDelimLen &&
ThisTokBuf[0] != '(')
++ThisTokBuf;
if (ThisTokBuf[0] != '(')
return DiagnoseLexingError(StringToks[i].getLocation());
++ThisTokBuf;
ThisTokEnd -= ThisTokBuf - Prefix;
if (ThisTokEnd < ThisTokBuf)
return DiagnoseLexingError(StringToks[i].getLocation());
StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf);
while (!RemainingTokenSpan.empty()) {
size_t CRLFPos = RemainingTokenSpan.find("\r\n");
StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos);
StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos);
if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF))
hadError = true;
RemainingTokenSpan = AfterCRLF.substr(1);
}
} else {
if (ThisTokBuf[0] != '"') {
return DiagnoseLexingError(StringToks[i].getLocation());
}
++ThisTokBuf;
if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
if (i == 0) {
++ThisTokBuf;
Pascal = true;
} else if (Pascal)
ThisTokBuf += 2;
}
while (ThisTokBuf != ThisTokEnd) {
if (ThisTokBuf[0] != '\\') {
const char *InStart = ThisTokBuf;
do {
++ThisTokBuf;
} while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
if (CopyStringFragment(StringToks[i], ThisTokBegin,
StringRef(InStart, ThisTokBuf - InStart)))
hadError = true;
continue;
}
if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U' ||
ThisTokBuf[1] == 'N') {
EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
ResultPtr, hadError,
FullSourceLoc(StringToks[i].getLocation(), SM),
CharByteWidth, Diags, Features);
continue;
}
unsigned ResultChar =
ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError,
FullSourceLoc(StringToks[i].getLocation(), SM),
CharByteWidth*8, Diags, Features);
if (CharByteWidth == 4) {
llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultPtr);
*ResultWidePtr = ResultChar;
ResultPtr += 4;
} else if (CharByteWidth == 2) {
llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultPtr);
*ResultWidePtr = ResultChar & 0xFFFF;
ResultPtr += 2;
} else {
assert(CharByteWidth == 1 && "Unexpected char width");
*ResultPtr++ = ResultChar & 0xFF;
}
}
}
}
if (Pascal) {
if (CharByteWidth == 4) {
llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultBuf.data());
ResultWidePtr[0] = GetNumStringChars() - 1;
} else if (CharByteWidth == 2) {
llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultBuf.data());
ResultWidePtr[0] = GetNumStringChars() - 1;
} else {
assert(CharByteWidth == 1 && "Unexpected char width");
ResultBuf[0] = GetNumStringChars() - 1;
}
if (GetStringLength() > 256) {
if (Diags)
Diags->Report(StringToks.front().getLocation(),
diag::err_pascal_string_too_long)
<< SourceRange(StringToks.front().getLocation(),
StringToks.back().getLocation());
hadError = true;
return;
}
} else if (Diags) {
unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
if (GetNumStringChars() > MaxChars)
Diags->Report(StringToks.front().getLocation(),
diag::ext_string_too_long)
<< GetNumStringChars() << MaxChars
<< (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
<< SourceRange(StringToks.front().getLocation(),
StringToks.back().getLocation());
}
}
static const char *resyncUTF8(const char *Err, const char *End) {
if (Err == End)
return End;
End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err);
while (++Err != End && (*Err & 0xC0) == 0x80)
;
return Err;
}
bool StringLiteralParser::CopyStringFragment(const Token &Tok,
const char *TokBegin,
StringRef Fragment) {
const llvm::UTF8 *ErrorPtrTmp;
if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
return false;
bool NoErrorOnBadEncoding = isOrdinary();
if (NoErrorOnBadEncoding) {
memcpy(ResultPtr, Fragment.data(), Fragment.size());
ResultPtr += Fragment.size();
}
if (Diags) {
const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
FullSourceLoc SourceLoc(Tok.getLocation(), SM);
const DiagnosticBuilder &Builder =
Diag(Diags, Features, SourceLoc, TokBegin,
ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()),
NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
: diag::err_bad_string_encoding);
const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end());
StringRef NextFragment(NextStart, Fragment.end()-NextStart);
SmallString<512> Dummy;
Dummy.reserve(Fragment.size() * CharByteWidth);
char *Ptr = Dummy.data();
while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
NextStart = resyncUTF8(ErrorPtr, Fragment.end());
Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin,
ErrorPtr, NextStart);
NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
}
}
return !NoErrorOnBadEncoding;
}
void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) {
hadError = true;
if (Diags)
Diags->Report(Loc, diag::err_lexing_string);
}
unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
unsigned ByteNo) const {
SmallString<32> SpellingBuffer;
SpellingBuffer.resize(Tok.getLength());
bool StringInvalid = false;
const char *SpellingPtr = &SpellingBuffer[0];
unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
&StringInvalid);
if (StringInvalid)
return 0;
const char *SpellingStart = SpellingPtr;
const char *SpellingEnd = SpellingPtr+TokLen;
if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8')
SpellingPtr += 2;
assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
if (SpellingPtr[0] == 'R') {
assert(SpellingPtr[1] == '"' && "Should be a raw string literal!");
SpellingPtr += 2;
while (*SpellingPtr != '(') {
++SpellingPtr;
assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal");
}
++SpellingPtr;
return SpellingPtr - SpellingStart + ByteNo;
}
assert(SpellingPtr[0] == '"' && "Should be a string literal!");
++SpellingPtr;
while (ByteNo) {
assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
if (*SpellingPtr != '\\') {
++SpellingPtr;
--ByteNo;
continue;
}
bool HadError = false;
if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U' ||
SpellingPtr[1] == 'N') {
const char *EscapePtr = SpellingPtr;
unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd,
1, Features, HadError);
if (Len > ByteNo) {
SpellingPtr = EscapePtr;
break;
}
ByteNo -= Len;
} else {
ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError,
FullSourceLoc(Tok.getLocation(), SM),
CharByteWidth*8, Diags, Features);
--ByteNo;
}
assert(!HadError && "This method isn't valid on erroneous strings");
}
return SpellingPtr-SpellingStart;
}
bool StringLiteralParser::isValidUDSuffix(const LangOptions &LangOpts,
StringRef Suffix) {
return NumericLiteralParser::isValidUDSuffix(LangOpts, Suffix) ||
Suffix == "sv";
}