2007-04-05 14:57:15 +08:00
|
|
|
//===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
|
2007-03-10 07:16:33 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-03-10 07:16:33 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-04-05 14:57:15 +08:00
|
|
|
// This file implements the NumericLiteralParser, CharLiteralParser, and
|
|
|
|
// StringLiteralParser interfaces.
|
2007-03-10 07:16:33 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2007-07-16 14:55:01 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-08-17 07:36:28 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2007-03-14 06:37:02 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2007-03-10 07:16:33 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
|
|
|
|
/// not valid.
|
|
|
|
static int HexDigitValue(char C) {
|
|
|
|
if (C >= '0' && C <= '9') return C-'0';
|
|
|
|
if (C >= 'a' && C <= 'f') return C-'a'+10;
|
|
|
|
if (C >= 'A' && C <= 'F') return C-'A'+10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
|
|
|
|
/// either a character or a string literal.
|
|
|
|
static unsigned ProcessCharEscape(const char *&ThisTokBuf,
|
|
|
|
const char *ThisTokEnd, bool &HadError,
|
2007-05-20 13:00:58 +08:00
|
|
|
SourceLocation Loc, bool IsWide,
|
|
|
|
Preprocessor &PP) {
|
2007-04-05 14:57:15 +08:00
|
|
|
// Skip the '\' char.
|
|
|
|
++ThisTokBuf;
|
|
|
|
|
|
|
|
// We know that this character can't be off the end of the buffer, because
|
|
|
|
// that would have been \", which would not have been the end of string.
|
|
|
|
unsigned ResultChar = *ThisTokBuf++;
|
|
|
|
switch (ResultChar) {
|
|
|
|
// These map to themselves.
|
|
|
|
case '\\': case '\'': case '"': case '?': break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
// These have fixed mappings.
|
|
|
|
case 'a':
|
|
|
|
// TODO: K&R: the meaning of '\\a' is different in traditional C
|
|
|
|
ResultChar = 7;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
ResultChar = 8;
|
|
|
|
break;
|
|
|
|
case 'e':
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(Loc, diag::ext_nonstandard_escape) << "e";
|
2007-04-05 14:57:15 +08:00
|
|
|
ResultChar = 27;
|
|
|
|
break;
|
2009-06-10 09:32:39 +08:00
|
|
|
case 'E':
|
|
|
|
PP.Diag(Loc, diag::ext_nonstandard_escape) << "E";
|
|
|
|
ResultChar = 27;
|
|
|
|
break;
|
2007-04-05 14:57:15 +08:00
|
|
|
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;
|
2007-05-20 13:00:58 +08:00
|
|
|
case 'x': { // Hex escape.
|
|
|
|
ResultChar = 0;
|
|
|
|
if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
|
2007-04-05 14:57:15 +08:00
|
|
|
PP.Diag(Loc, diag::err_hex_escape_no_digits);
|
|
|
|
HadError = 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-05-20 13:17:04 +08:00
|
|
|
// Hex escapes are a maximal series of hex digits.
|
2007-05-20 13:00:58 +08:00
|
|
|
bool Overflow = false;
|
|
|
|
for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
|
|
|
|
int CharVal = HexDigitValue(ThisTokBuf[0]);
|
|
|
|
if (CharVal == -1) break;
|
2008-10-01 04:45:40 +08:00
|
|
|
// About to shift out a digit?
|
|
|
|
Overflow |= (ResultChar & 0xF0000000) ? true : false;
|
2007-05-20 13:00:58 +08:00
|
|
|
ResultChar <<= 4;
|
|
|
|
ResultChar |= CharVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if any bits will be truncated when evaluated as a character.
|
2009-07-14 16:10:06 +08:00
|
|
|
unsigned CharWidth = IsWide
|
|
|
|
? PP.getTargetInfo().getWCharWidth()
|
|
|
|
: PP.getTargetInfo().getCharWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-05-20 13:00:58 +08:00
|
|
|
if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
|
|
|
|
Overflow = true;
|
|
|
|
ResultChar &= ~0U >> (32-CharWidth);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-05-20 13:00:58 +08:00
|
|
|
// Check for overflow.
|
|
|
|
if (Overflow) // Too many digits to fit in
|
|
|
|
PP.Diag(Loc, diag::warn_hex_escape_too_large);
|
2007-04-05 14:57:15 +08:00
|
|
|
break;
|
2007-05-20 13:00:58 +08:00
|
|
|
}
|
2007-04-05 14:57:15 +08:00
|
|
|
case '0': case '1': case '2': case '3':
|
2007-05-20 13:17:04 +08:00
|
|
|
case '4': case '5': case '6': case '7': {
|
2007-04-05 14:57:15 +08:00
|
|
|
// Octal escapes.
|
2007-06-09 14:20:47 +08:00
|
|
|
--ThisTokBuf;
|
2007-05-20 13:17:04 +08:00
|
|
|
ResultChar = 0;
|
|
|
|
|
|
|
|
// Octal escapes are a series of octal digits with maximum length 3.
|
|
|
|
// "\0123" is a two digit sequence equal to "\012" "3".
|
|
|
|
unsigned NumDigits = 0;
|
|
|
|
do {
|
|
|
|
ResultChar <<= 3;
|
|
|
|
ResultChar |= *ThisTokBuf++ - '0';
|
|
|
|
++NumDigits;
|
|
|
|
} while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
|
|
|
|
ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-05-20 13:17:04 +08:00
|
|
|
// Check for overflow. Reject '\777', but not L'\777'.
|
2009-07-14 16:10:06 +08:00
|
|
|
unsigned CharWidth = IsWide
|
|
|
|
? PP.getTargetInfo().getWCharWidth()
|
|
|
|
: PP.getTargetInfo().getCharWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-05-20 13:17:04 +08:00
|
|
|
if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
|
|
|
|
PP.Diag(Loc, diag::warn_octal_escape_too_large);
|
|
|
|
ResultChar &= ~0U >> (32-CharWidth);
|
|
|
|
}
|
2007-04-05 14:57:15 +08:00
|
|
|
break;
|
2007-05-20 13:17:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
// Otherwise, these are not valid escapes.
|
|
|
|
case '(': case '{': case '[': case '%':
|
|
|
|
// GCC accepts these as extensions. We warn about them as such though.
|
2009-04-28 08:51:18 +08:00
|
|
|
PP.Diag(Loc, diag::ext_nonstandard_escape)
|
|
|
|
<< std::string()+(char)ResultChar;
|
|
|
|
break;
|
2007-04-05 14:57:15 +08:00
|
|
|
default:
|
2008-11-22 15:23:31 +08:00
|
|
|
if (isgraph(ThisTokBuf[0]))
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(Loc, diag::ext_unknown_escape) << std::string()+(char)ResultChar;
|
2008-11-22 15:23:31 +08:00
|
|
|
else
|
2008-11-19 05:48:13 +08:00
|
|
|
PP.Diag(Loc, diag::ext_unknown_escape) << "x"+llvm::utohexstr(ResultChar);
|
2007-04-05 14:57:15 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
return ResultChar;
|
|
|
|
}
|
|
|
|
|
2009-03-31 07:46:03 +08:00
|
|
|
/// ProcessUCNEscape - Read the Universal Character Name, check constraints and
|
|
|
|
/// convert the UTF32 to UTF8. This is a subroutine of StringLiteralParser.
|
|
|
|
/// When we decide to implement UCN's for character constants and identifiers,
|
|
|
|
/// we will likely rework our support for UCN's.
|
2009-09-09 23:08:12 +08:00
|
|
|
static void ProcessUCNEscape(const char *&ThisTokBuf, const char *ThisTokEnd,
|
|
|
|
char *&ResultBuf, bool &HadError,
|
|
|
|
SourceLocation Loc, bool IsWide, Preprocessor &PP)
|
2009-03-31 18:29:45 +08:00
|
|
|
{
|
2009-03-31 07:46:03 +08:00
|
|
|
// FIXME: Add a warning - UCN's are only valid in C++ & C99.
|
2009-03-31 18:29:45 +08:00
|
|
|
// FIXME: Handle wide strings.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 19:09:15 +08:00
|
|
|
// Save the beginning of the string (for error diagnostics).
|
|
|
|
const char *ThisTokBegin = ThisTokBuf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 07:46:03 +08:00
|
|
|
// Skip the '\u' char's.
|
|
|
|
ThisTokBuf += 2;
|
|
|
|
|
|
|
|
if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) {
|
|
|
|
PP.Diag(Loc, diag::err_ucn_escape_no_digits);
|
|
|
|
HadError = 1;
|
|
|
|
return;
|
|
|
|
}
|
2009-04-01 19:09:15 +08:00
|
|
|
typedef uint32_t UTF32;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 07:46:03 +08:00
|
|
|
UTF32 UcnVal = 0;
|
|
|
|
unsigned short UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
|
|
|
|
for (; ThisTokBuf != ThisTokEnd && UcnLen; ++ThisTokBuf, UcnLen--) {
|
|
|
|
int CharVal = HexDigitValue(ThisTokBuf[0]);
|
|
|
|
if (CharVal == -1) break;
|
|
|
|
UcnVal <<= 4;
|
|
|
|
UcnVal |= CharVal;
|
|
|
|
}
|
|
|
|
// If we didn't consume the proper number of digits, there is a problem.
|
|
|
|
if (UcnLen) {
|
2009-04-01 19:09:15 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(Loc, ThisTokBuf-ThisTokBegin),
|
|
|
|
diag::err_ucn_escape_incomplete);
|
2009-03-31 07:46:03 +08:00
|
|
|
HadError = 1;
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
// Check UCN constraints (C99 6.4.3p2).
|
2009-03-31 07:46:03 +08:00
|
|
|
if ((UcnVal < 0xa0 &&
|
|
|
|
(UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60 )) // $, @, `
|
2009-09-09 23:08:12 +08:00
|
|
|
|| (UcnVal >= 0xD800 && UcnVal <= 0xDFFF)
|
2009-03-31 18:29:45 +08:00
|
|
|
|| (UcnVal > 0x10FFFF)) /* the maximum legal UTF32 value */ {
|
2009-03-31 07:46:03 +08:00
|
|
|
PP.Diag(Loc, diag::err_ucn_escape_invalid);
|
|
|
|
HadError = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
|
|
|
|
// The conversion below was inspired by:
|
|
|
|
// http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
|
2009-09-09 23:08:12 +08:00
|
|
|
// First, we determine how many bytes the result will require.
|
2009-04-01 19:09:15 +08:00
|
|
|
typedef uint8_t UTF8;
|
2009-03-31 07:46:03 +08:00
|
|
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 07:46:03 +08:00
|
|
|
const unsigned byteMask = 0xBF;
|
|
|
|
const unsigned byteMark = 0x80;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-31 07:46:03 +08:00
|
|
|
// Once the bits are split out into bytes of UTF8, this is a mask OR-ed
|
2009-03-31 18:29:45 +08:00
|
|
|
// into the first byte, depending on how many bytes follow.
|
2009-09-09 23:08:12 +08:00
|
|
|
static const UTF8 firstByteMark[5] = {
|
2009-03-31 18:29:45 +08:00
|
|
|
0x00, 0x00, 0xC0, 0xE0, 0xF0
|
2009-03-31 07:46:03 +08:00
|
|
|
};
|
|
|
|
// Finally, we write the bytes into ResultBuf.
|
|
|
|
ResultBuf += bytesToWrite;
|
|
|
|
switch (bytesToWrite) { // note: everything falls through.
|
|
|
|
case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
|
|
|
|
case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
|
|
|
|
case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
|
|
|
|
case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
|
|
|
|
}
|
|
|
|
// Update the buffer.
|
|
|
|
ResultBuf += bytesToWrite;
|
|
|
|
}
|
2007-04-05 14:57:15 +08:00
|
|
|
|
|
|
|
|
2007-03-10 07:16:33 +08:00
|
|
|
/// integer-constant: [C99 6.4.4.1]
|
|
|
|
/// decimal-constant integer-suffix
|
|
|
|
/// octal-constant integer-suffix
|
|
|
|
/// hexadecimal-constant integer-suffix
|
2009-09-09 23:08:12 +08:00
|
|
|
/// decimal-constant:
|
2007-03-10 07:16:33 +08:00
|
|
|
/// nonzero-digit
|
|
|
|
/// decimal-constant digit
|
2009-09-09 23:08:12 +08:00
|
|
|
/// octal-constant:
|
2007-03-10 07:16:33 +08:00
|
|
|
/// 0
|
|
|
|
/// octal-constant octal-digit
|
2009-09-09 23:08:12 +08:00
|
|
|
/// hexadecimal-constant:
|
2007-03-10 07:16:33 +08:00
|
|
|
/// hexadecimal-prefix hexadecimal-digit
|
|
|
|
/// hexadecimal-constant hexadecimal-digit
|
|
|
|
/// hexadecimal-prefix: one of
|
|
|
|
/// 0x 0X
|
|
|
|
/// integer-suffix:
|
|
|
|
/// unsigned-suffix [long-suffix]
|
|
|
|
/// unsigned-suffix [long-long-suffix]
|
|
|
|
/// long-suffix [unsigned-suffix]
|
|
|
|
/// long-long-suffix [unsigned-sufix]
|
|
|
|
/// nonzero-digit:
|
|
|
|
/// 1 2 3 4 5 6 7 8 9
|
|
|
|
/// octal-digit:
|
|
|
|
/// 0 1 2 3 4 5 6 7
|
|
|
|
/// hexadecimal-digit:
|
|
|
|
/// 0 1 2 3 4 5 6 7 8 9
|
|
|
|
/// a b c d e f
|
|
|
|
/// A B C D E F
|
|
|
|
/// unsigned-suffix: one of
|
|
|
|
/// u U
|
|
|
|
/// long-suffix: one of
|
|
|
|
/// l L
|
2009-09-09 23:08:12 +08:00
|
|
|
/// long-long-suffix: one of
|
2007-03-10 07:16:33 +08:00
|
|
|
/// ll LL
|
|
|
|
///
|
|
|
|
/// floating-constant: [C99 6.4.4.2]
|
|
|
|
/// TODO: add rules...
|
|
|
|
///
|
|
|
|
NumericLiteralParser::
|
|
|
|
NumericLiteralParser(const char *begin, const char *end,
|
2007-04-05 14:57:15 +08:00
|
|
|
SourceLocation TokLoc, Preprocessor &pp)
|
|
|
|
: PP(pp), ThisTokBegin(begin), ThisTokEnd(end) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-01 04:45:40 +08:00
|
|
|
// This routine assumes that the range begin/end matches the regex for integer
|
|
|
|
// and FP constants (specifically, the 'pp-number' regex), and assumes that
|
|
|
|
// the byte at "*end" is both valid and not part of the regex. Because of
|
|
|
|
// this, it doesn't have to check for 'overscan' in various places.
|
|
|
|
assert(!isalnum(*end) && *end != '.' && *end != '_' &&
|
|
|
|
"Lexer didn't maximally munch?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-10 07:16:33 +08:00
|
|
|
s = DigitsBegin = begin;
|
|
|
|
saw_exponent = false;
|
|
|
|
saw_period = false;
|
|
|
|
isLong = false;
|
|
|
|
isUnsigned = false;
|
|
|
|
isLongLong = false;
|
2007-08-26 11:29:23 +08:00
|
|
|
isFloat = false;
|
2007-08-26 09:58:14 +08:00
|
|
|
isImaginary = false;
|
2009-10-09 06:55:36 +08:00
|
|
|
isMicrosoftInteger = false;
|
2007-03-10 07:16:33 +08:00
|
|
|
hadError = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-10 07:16:33 +08:00
|
|
|
if (*s == '0') { // parse radix
|
2008-06-30 14:39:54 +08:00
|
|
|
ParseNumberStartingWithZero(TokLoc);
|
|
|
|
if (hadError)
|
|
|
|
return;
|
2007-03-10 07:16:33 +08:00
|
|
|
} else { // the first digit is non-zero
|
|
|
|
radix = 10;
|
|
|
|
s = SkipDigits(s);
|
|
|
|
if (s == ThisTokEnd) {
|
2007-06-09 01:12:06 +08:00
|
|
|
// Done.
|
2007-11-29 14:06:27 +08:00
|
|
|
} else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
|
|
|
|
diag::err_invalid_decimal_digit) << std::string(s, s+1);
|
|
|
|
hadError = true;
|
2007-06-09 01:12:06 +08:00
|
|
|
return;
|
2007-03-10 07:16:33 +08:00
|
|
|
} else if (*s == '.') {
|
|
|
|
s++;
|
|
|
|
saw_period = true;
|
|
|
|
s = SkipDigits(s);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-09-30 07:12:31 +08:00
|
|
|
if ((*s == 'e' || *s == 'E')) { // exponent
|
2008-04-21 02:47:55 +08:00
|
|
|
const char *Exponent = s;
|
2007-03-10 07:16:33 +08:00
|
|
|
s++;
|
|
|
|
saw_exponent = true;
|
|
|
|
if (*s == '+' || *s == '-') s++; // sign
|
|
|
|
const char *first_non_digit = SkipDigits(s);
|
2008-04-21 02:41:46 +08:00
|
|
|
if (first_non_digit != s) {
|
2007-03-10 07:16:33 +08:00
|
|
|
s = first_non_digit;
|
2008-04-21 02:41:46 +08:00
|
|
|
} else {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin),
|
|
|
|
diag::err_exponent_has_no_digits);
|
|
|
|
hadError = true;
|
2008-04-21 02:41:46 +08:00
|
|
|
return;
|
2007-03-10 07:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SuffixBegin = s;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-26 09:58:14 +08:00
|
|
|
// Parse the suffix. At this point we can classify whether we have an FP or
|
|
|
|
// integer constant.
|
|
|
|
bool isFPConstant = isFloatingLiteral();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-26 09:58:14 +08:00
|
|
|
// Loop over all of the characters of the suffix. If we see something bad,
|
|
|
|
// we break out of the loop.
|
|
|
|
for (; s != ThisTokEnd; ++s) {
|
|
|
|
switch (*s) {
|
|
|
|
case 'f': // FP Suffix for "float"
|
|
|
|
case 'F':
|
|
|
|
if (!isFPConstant) break; // Error for integer constant.
|
2007-08-26 11:29:23 +08:00
|
|
|
if (isFloat || isLong) break; // FF, LF invalid.
|
|
|
|
isFloat = true;
|
2007-08-26 09:58:14 +08:00
|
|
|
continue; // Success.
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
if (isFPConstant) break; // Error for floating constant.
|
|
|
|
if (isUnsigned) break; // Cannot be repeated.
|
|
|
|
isUnsigned = true;
|
|
|
|
continue; // Success.
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
if (isLong || isLongLong) break; // Cannot be repeated.
|
2007-08-26 11:29:23 +08:00
|
|
|
if (isFloat) break; // LF invalid.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-26 09:58:14 +08:00
|
|
|
// Check for long long. The L's need to be adjacent and the same case.
|
|
|
|
if (s+1 != ThisTokEnd && s[1] == s[0]) {
|
|
|
|
if (isFPConstant) break; // long long invalid for floats.
|
|
|
|
isLongLong = true;
|
|
|
|
++s; // Eat both of them.
|
|
|
|
} else {
|
2007-03-10 07:16:33 +08:00
|
|
|
isLong = true;
|
|
|
|
}
|
2007-08-26 09:58:14 +08:00
|
|
|
continue; // Success.
|
|
|
|
case 'i':
|
2008-04-05 05:02:54 +08:00
|
|
|
if (PP.getLangOptions().Microsoft) {
|
|
|
|
// Allow i8, i16, i32, i64, and i128.
|
2009-10-09 06:55:36 +08:00
|
|
|
if (s + 1 != ThisTokEnd) {
|
|
|
|
switch (s[1]) {
|
|
|
|
case '8':
|
|
|
|
s += 2; // i8 suffix
|
|
|
|
isMicrosoftInteger = true;
|
|
|
|
continue;
|
|
|
|
case '1':
|
|
|
|
s += 2;
|
|
|
|
if (s == ThisTokEnd) break;
|
|
|
|
if (*s == '6') s++; // i16 suffix
|
|
|
|
else if (*s == '2') {
|
|
|
|
if (++s == ThisTokEnd) break;
|
|
|
|
if (*s == '8') s++; // i128 suffix
|
|
|
|
}
|
|
|
|
isMicrosoftInteger = true;
|
|
|
|
continue;
|
|
|
|
case '3':
|
|
|
|
s += 2;
|
|
|
|
if (s == ThisTokEnd) break;
|
|
|
|
if (*s == '2') s++; // i32 suffix
|
|
|
|
isMicrosoftInteger = true;
|
|
|
|
continue;
|
|
|
|
case '6':
|
|
|
|
s += 2;
|
|
|
|
if (s == ThisTokEnd) break;
|
|
|
|
if (*s == '4') s++; // i64 suffix
|
|
|
|
isMicrosoftInteger = true;
|
|
|
|
continue;
|
|
|
|
case 'f': // FP Suffix for "float"
|
|
|
|
case 'F':
|
|
|
|
if (!isFPConstant) break; // Error for integer constant.
|
|
|
|
if (isFloat || isLong) break; // FF, LF invalid.
|
|
|
|
isFloat = true;
|
|
|
|
if (isImaginary) break; // Cannot be repeated.
|
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
|
|
|
|
diag::ext_imaginary_constant);
|
|
|
|
isImaginary = true;
|
|
|
|
s++;
|
|
|
|
continue; // Success.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2008-04-05 05:02:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// fall through.
|
2007-08-26 09:58:14 +08:00
|
|
|
case 'I':
|
|
|
|
case 'j':
|
|
|
|
case 'J':
|
|
|
|
if (isImaginary) break; // Cannot be repeated.
|
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
|
|
|
|
diag::ext_imaginary_constant);
|
|
|
|
isImaginary = true;
|
|
|
|
continue; // Success.
|
2007-03-10 07:16:33 +08:00
|
|
|
}
|
2007-08-26 09:58:14 +08:00
|
|
|
// If we reached here, there was an error.
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-26 09:58:14 +08:00
|
|
|
// Report an error if there are any.
|
|
|
|
if (s != ThisTokEnd) {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin),
|
|
|
|
isFPConstant ? diag::err_invalid_suffix_float_constant :
|
|
|
|
diag::err_invalid_suffix_integer_constant)
|
|
|
|
<< std::string(SuffixBegin, ThisTokEnd);
|
|
|
|
hadError = true;
|
2007-08-26 09:58:14 +08:00
|
|
|
return;
|
2007-03-10 07:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-30 14:39:54 +08:00
|
|
|
/// ParseNumberStartingWithZero - This method is called when the first character
|
|
|
|
/// of the number is found to be a zero. This means it is either an octal
|
|
|
|
/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
|
2009-09-09 23:08:12 +08:00
|
|
|
/// a floating point number (01239.123e4). Eat the prefix, determining the
|
2008-06-30 14:39:54 +08:00
|
|
|
/// radix etc.
|
|
|
|
void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
|
|
|
|
assert(s[0] == '0' && "Invalid method call");
|
|
|
|
s++;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-30 14:39:54 +08:00
|
|
|
// Handle a hex number like 0x1234.
|
|
|
|
if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) {
|
|
|
|
s++;
|
|
|
|
radix = 16;
|
|
|
|
DigitsBegin = s;
|
|
|
|
s = SkipHexDigits(s);
|
|
|
|
if (s == ThisTokEnd) {
|
|
|
|
// Done.
|
|
|
|
} else if (*s == '.') {
|
|
|
|
s++;
|
|
|
|
saw_period = true;
|
|
|
|
s = SkipHexDigits(s);
|
|
|
|
}
|
|
|
|
// A binary exponent can appear with or with a '.'. If dotted, the
|
2009-09-09 23:08:12 +08:00
|
|
|
// binary exponent is required.
|
2008-07-26 02:18:34 +08:00
|
|
|
if (*s == 'p' || *s == 'P') {
|
2008-06-30 14:39:54 +08:00
|
|
|
const char *Exponent = s;
|
|
|
|
s++;
|
|
|
|
saw_exponent = true;
|
|
|
|
if (*s == '+' || *s == '-') s++; // sign
|
|
|
|
const char *first_non_digit = SkipDigits(s);
|
2008-07-26 02:18:34 +08:00
|
|
|
if (first_non_digit == s) {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
|
|
|
|
diag::err_exponent_has_no_digits);
|
|
|
|
hadError = true;
|
2008-07-26 02:18:34 +08:00
|
|
|
return;
|
2008-06-30 14:39:54 +08:00
|
|
|
}
|
2008-07-26 02:18:34 +08:00
|
|
|
s = first_non_digit;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-22 15:39:03 +08:00
|
|
|
if (!PP.getLangOptions().HexFloats)
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
|
2008-06-30 14:39:54 +08:00
|
|
|
} else if (saw_period) {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
|
|
|
|
diag::err_hexconstant_requires_exponent);
|
|
|
|
hadError = true;
|
2008-06-30 14:39:54 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-30 14:39:54 +08:00
|
|
|
// Handle simple binary numbers 0b01010
|
|
|
|
if (*s == 'b' || *s == 'B') {
|
|
|
|
// 0b101010 is a GCC extension.
|
2008-06-30 14:44:49 +08:00
|
|
|
PP.Diag(TokLoc, diag::ext_binary_literal);
|
2008-06-30 14:39:54 +08:00
|
|
|
++s;
|
|
|
|
radix = 2;
|
|
|
|
DigitsBegin = s;
|
|
|
|
s = SkipBinaryDigits(s);
|
|
|
|
if (s == ThisTokEnd) {
|
|
|
|
// Done.
|
|
|
|
} else if (isxdigit(*s)) {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
|
|
|
|
diag::err_invalid_binary_digit) << std::string(s, s+1);
|
|
|
|
hadError = true;
|
2008-06-30 14:39:54 +08:00
|
|
|
}
|
2008-06-30 14:44:49 +08:00
|
|
|
// Other suffixes will be diagnosed by the caller.
|
2008-06-30 14:39:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-30 14:39:54 +08:00
|
|
|
// For now, the radix is set to 8. If we discover that we have a
|
|
|
|
// floating point constant, the radix will change to 10. Octal floating
|
2009-09-09 23:08:12 +08:00
|
|
|
// point constants are not permitted (only decimal and hexadecimal).
|
2008-06-30 14:39:54 +08:00
|
|
|
radix = 8;
|
|
|
|
DigitsBegin = s;
|
|
|
|
s = SkipOctalDigits(s);
|
|
|
|
if (s == ThisTokEnd)
|
|
|
|
return; // Done, simple octal number like 01234
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-30 14:44:49 +08:00
|
|
|
// If we have some other non-octal digit that *is* a decimal digit, see if
|
|
|
|
// this is part of a floating point number like 094.123 or 09e1.
|
|
|
|
if (isdigit(*s)) {
|
|
|
|
const char *EndDecimal = SkipDigits(s);
|
|
|
|
if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
|
|
|
|
s = EndDecimal;
|
|
|
|
radix = 10;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-30 14:44:49 +08:00
|
|
|
// If we have a hex digit other than 'e' (which denotes a FP exponent) then
|
|
|
|
// the code is using an incorrect base.
|
2008-06-30 14:39:54 +08:00
|
|
|
if (isxdigit(*s) && *s != 'e' && *s != 'E') {
|
2008-11-22 15:23:31 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
|
|
|
|
diag::err_invalid_octal_digit) << std::string(s, s+1);
|
|
|
|
hadError = true;
|
2008-06-30 14:39:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-30 14:39:54 +08:00
|
|
|
if (*s == '.') {
|
|
|
|
s++;
|
|
|
|
radix = 10;
|
|
|
|
saw_period = true;
|
2008-06-30 14:44:49 +08:00
|
|
|
s = SkipDigits(s); // Skip suffix.
|
2008-06-30 14:39:54 +08:00
|
|
|
}
|
|
|
|
if (*s == 'e' || *s == 'E') { // exponent
|
|
|
|
const char *Exponent = s;
|
|
|
|
s++;
|
|
|
|
radix = 10;
|
|
|
|
saw_exponent = true;
|
|
|
|
if (*s == '+' || *s == '-') s++; // sign
|
|
|
|
const char *first_non_digit = SkipDigits(s);
|
|
|
|
if (first_non_digit != s) {
|
|
|
|
s = first_non_digit;
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
|
2008-11-22 15:23:31 +08:00
|
|
|
diag::err_exponent_has_no_digits);
|
|
|
|
hadError = true;
|
2008-06-30 14:39:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-04 13:52:58 +08:00
|
|
|
/// GetIntegerValue - Convert this numeric literal value to an APInt that
|
2007-04-04 14:36:34 +08:00
|
|
|
/// matches Val's input width. If there is an overflow, set Val to the low bits
|
|
|
|
/// of the result and return true. Otherwise, return false.
|
2007-06-16 07:05:46 +08:00
|
|
|
bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
|
2008-10-16 15:32:01 +08:00
|
|
|
// Fast path: Compute a conservative bound on the maximum number of
|
|
|
|
// bits per digit in this radix. If we can't possibly overflow a
|
|
|
|
// uint64 based on that bound then do the simple conversion to
|
|
|
|
// integer. This avoids the expensive overflow checking below, and
|
|
|
|
// handles the common cases that matter (small decimal integers and
|
|
|
|
// hex/octal values which don't overflow).
|
|
|
|
unsigned MaxBitsPerDigit = 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
while ((1U << MaxBitsPerDigit) < radix)
|
2008-10-16 15:32:01 +08:00
|
|
|
MaxBitsPerDigit += 1;
|
|
|
|
if ((SuffixBegin - DigitsBegin) * MaxBitsPerDigit <= 64) {
|
|
|
|
uint64_t N = 0;
|
|
|
|
for (s = DigitsBegin; s != SuffixBegin; ++s)
|
|
|
|
N = N*radix + HexDigitValue(*s);
|
|
|
|
|
|
|
|
// This will truncate the value to Val's input width. Simply check
|
|
|
|
// for overflow by comparing.
|
|
|
|
Val = N;
|
|
|
|
return Val.getZExtValue() != N;
|
|
|
|
}
|
|
|
|
|
2007-04-04 13:52:58 +08:00
|
|
|
Val = 0;
|
|
|
|
s = DigitsBegin;
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::APInt RadixVal(Val.getBitWidth(), radix);
|
|
|
|
llvm::APInt CharVal(Val.getBitWidth(), 0);
|
|
|
|
llvm::APInt OldVal = Val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-04 14:36:34 +08:00
|
|
|
bool OverflowOccurred = false;
|
2007-04-04 13:52:58 +08:00
|
|
|
while (s < SuffixBegin) {
|
2007-04-05 14:57:15 +08:00
|
|
|
unsigned C = HexDigitValue(*s++);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-04 13:52:58 +08:00
|
|
|
// If this letter is out of bound for this radix, reject it.
|
2007-04-04 14:49:26 +08:00
|
|
|
assert(C < radix && "NumericLiteralParser ctor should have rejected this");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-04 13:52:58 +08:00
|
|
|
CharVal = C;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-04 14:36:34 +08:00
|
|
|
// Add the digit to the value in the appropriate radix. If adding in digits
|
|
|
|
// made the value smaller, then this overflowed.
|
2007-04-04 13:52:58 +08:00
|
|
|
OldVal = Val;
|
2007-04-04 14:36:34 +08:00
|
|
|
|
|
|
|
// Multiply by radix, did overflow occur on the multiply?
|
2007-04-04 13:52:58 +08:00
|
|
|
Val *= RadixVal;
|
2007-04-04 14:36:34 +08:00
|
|
|
OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
|
|
|
|
|
|
|
|
// Add value, did overflow occur on the value?
|
2008-10-16 14:39:30 +08:00
|
|
|
// (a + b) ult b <=> overflow
|
2007-04-04 13:52:58 +08:00
|
|
|
Val += CharVal;
|
2007-04-04 14:36:34 +08:00
|
|
|
OverflowOccurred |= Val.ult(CharVal);
|
2007-04-04 13:52:58 +08:00
|
|
|
}
|
2007-04-04 14:36:34 +08:00
|
|
|
return OverflowOccurred;
|
2007-04-04 13:52:58 +08:00
|
|
|
}
|
|
|
|
|
2007-09-23 02:29:59 +08:00
|
|
|
llvm::APFloat NumericLiteralParser::
|
2007-11-27 07:12:30 +08:00
|
|
|
GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) {
|
|
|
|
using llvm::APFloat;
|
2009-08-17 07:36:28 +08:00
|
|
|
using llvm::StringRef;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-29 08:54:29 +08:00
|
|
|
llvm::SmallVector<char,256> floatChars;
|
2009-08-17 07:36:28 +08:00
|
|
|
unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
|
|
|
|
for (unsigned i = 0; i != n; ++i)
|
2007-11-29 08:54:29 +08:00
|
|
|
floatChars.push_back(ThisTokBegin[i]);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-29 08:54:29 +08:00
|
|
|
floatChars.push_back('\0');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-27 07:12:30 +08:00
|
|
|
APFloat V (Format, APFloat::fcZero, false);
|
|
|
|
APFloat::opStatus status;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-17 07:36:28 +08:00
|
|
|
status = V.convertFromString(StringRef(&floatChars[0], n),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-27 07:12:30 +08:00
|
|
|
if (isExact)
|
|
|
|
*isExact = status == APFloat::opOK;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-27 07:12:30 +08:00
|
|
|
return V;
|
2007-07-10 07:53:58 +08:00
|
|
|
}
|
2007-04-04 13:52:58 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
|
|
|
|
CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
|
|
|
|
SourceLocation Loc, Preprocessor &PP) {
|
|
|
|
// At this point we know that the character matches the regex "L?'.*'".
|
|
|
|
HadError = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
// Determine if this is a wide character.
|
|
|
|
IsWide = begin[0] == 'L';
|
|
|
|
if (IsWide) ++begin;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
// Skip over the entry quote.
|
|
|
|
assert(begin[0] == '\'' && "Invalid token lexed");
|
|
|
|
++begin;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: The "Value" is an uint64_t so we can handle char literals of
|
2009-04-21 10:21:29 +08:00
|
|
|
// upto 64-bits.
|
2007-04-05 14:57:15 +08:00
|
|
|
// FIXME: This extensively assumes that 'char' is 8-bits.
|
2008-03-06 02:54:05 +08:00
|
|
|
assert(PP.getTargetInfo().getCharWidth() == 8 &&
|
2007-04-05 14:57:15 +08:00
|
|
|
"Assumes char is 8 bits");
|
2009-04-29 05:51:46 +08:00
|
|
|
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");
|
2009-04-21 10:21:29 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// This is what we will use for overflow detection
|
2009-04-21 10:21:29 +08:00
|
|
|
llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-29 05:51:46 +08:00
|
|
|
unsigned NumCharsSoFar = 0;
|
2007-04-05 14:57:15 +08:00
|
|
|
while (begin[0] != '\'') {
|
2009-04-21 10:21:29 +08:00
|
|
|
uint64_t ResultChar;
|
2007-04-05 14:57:15 +08:00
|
|
|
if (begin[0] != '\\') // If this is a normal character, consume it.
|
|
|
|
ResultChar = *begin++;
|
|
|
|
else // Otherwise, this is an escape character.
|
2007-05-20 13:00:58 +08:00
|
|
|
ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP);
|
2007-04-05 14:57:15 +08:00
|
|
|
|
|
|
|
// If this is a multi-character constant (e.g. 'abc'), handle it. These are
|
|
|
|
// implementation defined (C99 6.4.4.4p10).
|
2009-04-29 05:51:46 +08:00
|
|
|
if (NumCharsSoFar) {
|
2007-04-05 14:57:15 +08:00
|
|
|
if (IsWide) {
|
|
|
|
// Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'.
|
2009-04-21 10:21:29 +08:00
|
|
|
LitVal = 0;
|
2007-04-05 14:57:15 +08:00
|
|
|
} else {
|
|
|
|
// Narrow character literals act as though their value is concatenated
|
2009-04-29 05:51:46 +08:00
|
|
|
// in this implementation, but warn on overflow.
|
|
|
|
if (LitVal.countLeadingZeros() < 8)
|
2007-04-05 14:57:15 +08:00
|
|
|
PP.Diag(Loc, diag::warn_char_constant_too_large);
|
2009-04-21 10:21:29 +08:00
|
|
|
LitVal <<= 8;
|
2007-04-05 14:57:15 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 10:21:29 +08:00
|
|
|
LitVal = LitVal + ResultChar;
|
2009-04-29 05:51:46 +08:00
|
|
|
++NumCharsSoFar;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is the second character being processed, do special handling.
|
|
|
|
if (NumCharsSoFar > 1) {
|
|
|
|
// Warn about discarding the top bits for multi-char wide-character
|
|
|
|
// constants (L'abcd').
|
|
|
|
if (IsWide)
|
|
|
|
PP.Diag(Loc, diag::warn_extraneous_wide_char_constant);
|
|
|
|
else if (NumCharsSoFar != 4)
|
|
|
|
PP.Diag(Loc, diag::ext_multichar_character_literal);
|
|
|
|
else
|
|
|
|
PP.Diag(Loc, diag::ext_four_char_character_literal);
|
2009-06-01 13:25:02 +08:00
|
|
|
IsMultiChar = true;
|
2009-07-29 09:46:05 +08:00
|
|
|
} else
|
|
|
|
IsMultiChar = false;
|
2009-04-21 10:21:29 +08:00
|
|
|
|
|
|
|
// Transfer the value from APInt to uint64_t
|
|
|
|
Value = LitVal.getZExtValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-04-05 14:57:15 +08:00
|
|
|
// If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
|
|
|
|
// if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple
|
|
|
|
// character constants are not sign extended in the this implementation:
|
|
|
|
// '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
|
2009-04-29 05:51:46 +08:00
|
|
|
if (!IsWide && NumCharsSoFar == 1 && (Value & 128) &&
|
2009-06-05 15:05:05 +08:00
|
|
|
PP.getLangOptions().CharIsSigned)
|
2007-04-05 14:57:15 +08:00
|
|
|
Value = (signed char)Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
/// string-literal: [C99 6.4.5]
|
|
|
|
/// " [s-char-sequence] "
|
|
|
|
/// L" [s-char-sequence] "
|
|
|
|
/// s-char-sequence:
|
|
|
|
/// s-char
|
|
|
|
/// s-char-sequence s-char
|
|
|
|
/// s-char:
|
|
|
|
/// any source character except the double quote ",
|
|
|
|
/// backslash \, or newline character
|
|
|
|
/// escape-character
|
|
|
|
/// universal-character-name
|
|
|
|
/// escape-character: [C99 6.4.4.4]
|
|
|
|
/// \ escape-code
|
|
|
|
/// universal-character-name
|
|
|
|
/// escape-code:
|
|
|
|
/// character-escape-code
|
|
|
|
/// octal-escape-code
|
|
|
|
/// hex-escape-code
|
|
|
|
/// character-escape-code: one of
|
|
|
|
/// n t b r f v a
|
|
|
|
/// \ ' " ?
|
|
|
|
/// octal-escape-code:
|
|
|
|
/// octal-digit
|
|
|
|
/// octal-digit octal-digit
|
|
|
|
/// octal-digit octal-digit octal-digit
|
|
|
|
/// hex-escape-code:
|
|
|
|
/// x hex-digit
|
|
|
|
/// hex-escape-code hex-digit
|
|
|
|
/// universal-character-name:
|
|
|
|
/// \u hex-quad
|
|
|
|
/// \U hex-quad hex-quad
|
|
|
|
/// hex-quad:
|
|
|
|
/// hex-digit hex-digit hex-digit hex-digit
|
2007-04-05 14:57:15 +08:00
|
|
|
///
|
2007-03-14 06:37:02 +08:00
|
|
|
StringLiteralParser::
|
2007-07-21 00:59:19 +08:00
|
|
|
StringLiteralParser(const Token *StringToks, unsigned NumStringToks,
|
2009-01-17 02:51:42 +08:00
|
|
|
Preprocessor &pp) : PP(pp) {
|
2007-03-14 06:37:02 +08:00
|
|
|
// Scan all of the string portions, remember the max individual token length,
|
|
|
|
// computing a bound on the concatenated string length, and see whether any
|
|
|
|
// piece is a wide-string. If any of the string portions is a wide-string
|
|
|
|
// literal, the result is a wide-string literal [C99 6.4.5p4].
|
|
|
|
MaxTokenLength = StringToks[0].getLength();
|
|
|
|
SizeBound = StringToks[0].getLength()-2; // -2 for "".
|
2007-10-10 02:02:16 +08:00
|
|
|
AnyWide = StringToks[0].is(tok::wide_string_literal);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-24 06:27:02 +08:00
|
|
|
hadError = false;
|
2007-04-05 14:57:15 +08:00
|
|
|
|
|
|
|
// Implement Translation Phase #6: concatenation of string literals
|
|
|
|
/// (C99 5.1.1.2p1). The common case is only one string fragment.
|
2007-03-14 06:37:02 +08:00
|
|
|
for (unsigned i = 1; i != NumStringToks; ++i) {
|
|
|
|
// The string could be shorter than this if it needs cleaning, but this is a
|
|
|
|
// reasonable bound, which is all we need.
|
|
|
|
SizeBound += StringToks[i].getLength()-2; // -2 for "".
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Remember maximum string piece length.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (StringToks[i].getLength() > MaxTokenLength)
|
2007-03-14 06:37:02 +08:00
|
|
|
MaxTokenLength = StringToks[i].getLength();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Remember if we see any wide strings.
|
2007-10-10 02:02:16 +08:00
|
|
|
AnyWide |= StringToks[i].is(tok::wide_string_literal);
|
2007-03-14 06:37:02 +08:00
|
|
|
}
|
2009-02-27 07:01:51 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Include space for the null terminator.
|
|
|
|
++SizeBound;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// TODO: K&R warning: "traditional C rejects string constant concatenation"
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
|
|
|
|
// query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
|
|
|
|
wchar_tByteWidth = ~0U;
|
2007-04-05 14:57:15 +08:00
|
|
|
if (AnyWide) {
|
2009-01-17 02:51:42 +08:00
|
|
|
wchar_tByteWidth = PP.getTargetInfo().getWCharWidth();
|
2007-04-05 14:57:15 +08:00
|
|
|
assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!");
|
|
|
|
wchar_tByteWidth /= 8;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// The output buffer size needs to be large enough to hold wide characters.
|
|
|
|
// This is a worst-case assumption which basically corresponds to L"" "long".
|
|
|
|
if (AnyWide)
|
|
|
|
SizeBound *= wchar_tByteWidth;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Size the temporary buffer to hold the result string data.
|
|
|
|
ResultBuf.resize(SizeBound);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Likewise, but for each string piece.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallString<512> TokenBuf;
|
2007-03-14 06:37:02 +08:00
|
|
|
TokenBuf.resize(MaxTokenLength);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Loop over all the strings, getting their spelling, and expanding them to
|
|
|
|
// wide strings as appropriate.
|
|
|
|
ResultPtr = &ResultBuf[0]; // Next byte to fill in.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-15 10:50:23 +08:00
|
|
|
Pascal = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
for (unsigned i = 0, e = NumStringToks; i != e; ++i) {
|
|
|
|
const char *ThisTokBuf = &TokenBuf[0];
|
|
|
|
// Get the spelling of the token, which eliminates trigraphs, etc. We know
|
|
|
|
// that ThisTokBuf points to a buffer that is big enough for the whole token
|
|
|
|
// and 'spelled' tokens can only shrink.
|
|
|
|
unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
|
|
|
|
const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// TODO: Input character set mapping support.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Skip L marker for wide strings.
|
2007-05-20 13:00:58 +08:00
|
|
|
bool ThisIsWide = false;
|
|
|
|
if (ThisTokBuf[0] == 'L') {
|
|
|
|
++ThisTokBuf;
|
|
|
|
ThisIsWide = true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
|
|
|
|
++ThisTokBuf;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-15 10:50:23 +08:00
|
|
|
// Check if this is a pascal string
|
|
|
|
if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
|
|
|
|
ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-10-15 10:50:23 +08:00
|
|
|
// If the \p sequence is found in the first token, we have a pascal string
|
|
|
|
// Otherwise, if we already have a pascal string, ignore the first \p
|
|
|
|
if (i == 0) {
|
|
|
|
++ThisTokBuf;
|
|
|
|
Pascal = true;
|
|
|
|
} else if (Pascal)
|
|
|
|
ThisTokBuf += 2;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
while (ThisTokBuf != ThisTokEnd) {
|
|
|
|
// Is this a span of non-escape characters?
|
|
|
|
if (ThisTokBuf[0] != '\\') {
|
|
|
|
const char *InStart = ThisTokBuf;
|
|
|
|
do {
|
|
|
|
++ThisTokBuf;
|
|
|
|
} while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-03-14 06:37:02 +08:00
|
|
|
// Copy the character span over.
|
|
|
|
unsigned Len = ThisTokBuf-InStart;
|
|
|
|
if (!AnyWide) {
|
|
|
|
memcpy(ResultPtr, InStart, Len);
|
|
|
|
ResultPtr += Len;
|
|
|
|
} else {
|
|
|
|
// Note: our internal rep of wide char tokens is always little-endian.
|
|
|
|
for (; Len; --Len, ++InStart) {
|
|
|
|
*ResultPtr++ = InStart[0];
|
|
|
|
// Add zeros at the end.
|
|
|
|
for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
|
2009-03-31 07:46:03 +08:00
|
|
|
*ResultPtr++ = 0;
|
2007-03-14 06:37:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-01 19:09:15 +08:00
|
|
|
// Is this a Universal Character Name escape?
|
2009-03-31 07:46:03 +08:00
|
|
|
if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
|
2009-09-09 23:08:12 +08:00
|
|
|
ProcessUCNEscape(ThisTokBuf, ThisTokEnd, ResultPtr,
|
2009-03-31 18:29:45 +08:00
|
|
|
hadError, StringToks[i].getLocation(), ThisIsWide, PP);
|
2009-04-01 19:09:15 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Otherwise, this is a non-UCN escape character. Process it.
|
|
|
|
unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError,
|
|
|
|
StringToks[i].getLocation(),
|
|
|
|
ThisIsWide, PP);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 19:09:15 +08:00
|
|
|
// Note: our internal rep of wide char tokens is always little-endian.
|
|
|
|
*ResultPtr++ = ResultChar & 0xFF;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 19:09:15 +08:00
|
|
|
if (AnyWide) {
|
|
|
|
for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
|
|
|
|
*ResultPtr++ = ResultChar >> i*8;
|
2007-03-14 06:37:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 02:51:42 +08:00
|
|
|
if (Pascal) {
|
2007-10-15 10:50:23 +08:00
|
|
|
ResultBuf[0] = ResultPtr-&ResultBuf[0]-1;
|
2009-01-17 02:51:42 +08:00
|
|
|
|
|
|
|
// Verify that pascal strings aren't too large.
|
2009-04-01 11:17:08 +08:00
|
|
|
if (GetStringLength() > 256) {
|
2009-01-17 02:51:42 +08:00
|
|
|
PP.Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long)
|
|
|
|
<< SourceRange(StringToks[0].getLocation(),
|
|
|
|
StringToks[NumStringToks-1].getLocation());
|
2009-04-01 11:17:08 +08:00
|
|
|
hadError = 1;
|
|
|
|
return;
|
|
|
|
}
|
2009-01-17 02:51:42 +08:00
|
|
|
}
|
2007-03-14 06:37:02 +08:00
|
|
|
}
|
2009-02-19 03:21:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// getOffsetOfStringByte - This function returns the offset of the
|
|
|
|
/// specified byte of the string data represented by Token. This handles
|
|
|
|
/// advancing over escape sequences in the string.
|
|
|
|
unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
|
|
|
|
unsigned ByteNo,
|
|
|
|
Preprocessor &PP) {
|
|
|
|
// Get the spelling of the token.
|
|
|
|
llvm::SmallString<16> SpellingBuffer;
|
|
|
|
SpellingBuffer.resize(Tok.getLength());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 03:21:10 +08:00
|
|
|
const char *SpellingPtr = &SpellingBuffer[0];
|
|
|
|
unsigned TokLen = PP.getSpelling(Tok, SpellingPtr);
|
|
|
|
|
|
|
|
assert(SpellingPtr[0] != 'L' && "Doesn't handle wide strings yet");
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 03:21:10 +08:00
|
|
|
const char *SpellingStart = SpellingPtr;
|
|
|
|
const char *SpellingEnd = SpellingPtr+TokLen;
|
|
|
|
|
|
|
|
// Skip over the leading quote.
|
|
|
|
assert(SpellingPtr[0] == '"' && "Should be a string literal!");
|
|
|
|
++SpellingPtr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 03:21:10 +08:00
|
|
|
// Skip over bytes until we find the offset we're looking for.
|
|
|
|
while (ByteNo) {
|
|
|
|
assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 03:21:10 +08:00
|
|
|
// Step over non-escapes simply.
|
|
|
|
if (*SpellingPtr != '\\') {
|
|
|
|
++SpellingPtr;
|
|
|
|
--ByteNo;
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 03:21:10 +08:00
|
|
|
// Otherwise, this is an escape character. Advance over it.
|
|
|
|
bool HadError = false;
|
|
|
|
ProcessCharEscape(SpellingPtr, SpellingEnd, HadError,
|
|
|
|
Tok.getLocation(), false, PP);
|
|
|
|
assert(!HadError && "This method isn't valid on erroneous strings");
|
|
|
|
--ByteNo;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 03:21:10 +08:00
|
|
|
return SpellingPtr-SpellingStart;
|
|
|
|
}
|