ProcessUCNEscape(): Incorportate some feedback from Chris.

llvm-svn: 68198
This commit is contained in:
Steve Naroff 2009-04-01 11:09:15 +00:00
parent e80bd1897c
commit c94adda157
1 changed files with 21 additions and 17 deletions

View File

@ -160,6 +160,9 @@ static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
// FIXME: Add a warning - UCN's are only valid in C++ & C99. // FIXME: Add a warning - UCN's are only valid in C++ & C99.
// FIXME: Handle wide strings. // FIXME: Handle wide strings.
// Save the beginning of the string (for error diagnostics).
const char *ThisTokBegin = ThisTokBuf;
// Skip the '\u' char's. // Skip the '\u' char's.
ThisTokBuf += 2; ThisTokBuf += 2;
@ -168,7 +171,7 @@ static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
HadError = 1; HadError = 1;
return; return;
} }
typedef unsigned int UTF32; typedef uint32_t UTF32;
UTF32 UcnVal = 0; UTF32 UcnVal = 0;
unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8); unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
@ -180,7 +183,8 @@ static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
} }
// If we didn't consume the proper number of digits, there is a problem. // If we didn't consume the proper number of digits, there is a problem.
if (UcnLen) { if (UcnLen) {
PP.Diag(Loc, diag::err_ucn_escape_incomplete); PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
diag::err_ucn_escape_incomplete);
HadError = 1; HadError = 1;
return; return;
} }
@ -197,7 +201,7 @@ static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
// The conversion below was inspired by: // The conversion below was inspired by:
// http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
// First, we determine how many bytes the result will require. // First, we determine how many bytes the result will require.
typedef unsigned char UTF8; typedef uint8_t UTF8;
unsigned short bytesToWrite = 0; unsigned short bytesToWrite = 0;
if (UcnVal < (UTF32)0x80) if (UcnVal < (UTF32)0x80)
@ -838,23 +842,23 @@ StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
} }
continue; continue;
} }
// Is this a Universal Character Name escape?
if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') { if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr, ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
hadError, StringToks[i].getLocation(), ThisIsWide, PP); hadError, StringToks[i].getLocation(), ThisIsWide, PP);
} else { continue;
// Otherwise, this is a non-UCN escape character. Process it. }
unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError, // Otherwise, this is a non-UCN escape character. Process it.
StringToks[i].getLocation(), unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
ThisIsWide, PP); StringToks[i].getLocation(),
ThisIsWide, PP);
// Note: our internal rep of wide char tokens is always little-endian.
*ResultPtr++ = ResultChar & 0xFF; // Note: our internal rep of wide char tokens is always little-endian.
*ResultPtr++ = ResultChar & 0xFF;
if (AnyWide) {
for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) if (AnyWide) {
*ResultPtr++ = ResultChar >> i*8; for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
} *ResultPtr++ = ResultChar >> i*8;
} }
} }
} }